diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index ea2ca69..7572101 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,179 +1,178 @@ ##################### External packages ######################### find_package(CxxTest) # Necessary if you want the tests # catch for unit testing include(ExternalProject) find_package(Git REQUIRED) ExternalProject_Add( catch PREFIX ${CMAKE_BINARY_DIR}/catch GIT_REPOSITORY https://github.com/philsquared/Catch.git TIMEOUT 10 UPDATE_COMMAND ${GIT_EXECUTABLE} pull CONFIGURE_COMMAND "" BUILD_COMMAND "" INSTALL_COMMAND "" LOG_DOWNLOAD ON ) ################## Configuration ################################ set(PROJECT_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}) ExternalProject_Get_Property(catch source_dir) set(CATCH_INCLUDE_DIR ${source_dir}/include CACHE INTERNAL "Path to include folder for Catch") include_directories(${CATCH_INCLUDE_DIR}) include_directories(${PROJECT_TEST_DIR}) ################## Tests ######################################## enable_testing(true) # CXX Test # ======== if(CXXTEST_FOUND) set(CXXTEST_USE_PYTHON TRUE) include_directories(${CXXTEST_INCLUDE_DIR}) enable_testing() # MiCP Solver # ------------ CXXTEST_ADD_TEST(test_cond_number test_cond_number.cpp ${PROJECT_TEST_DIR}/micpsolver/test_cond_number.hpp) CXXTEST_ADD_TEST(test_ncp_funtion test_ncp_function.cpp ${PROJECT_TEST_DIR}/micpsolver/test_ncp_function.hpp) CXXTEST_ADD_TEST(test_micpsolver test_micpsolver.cpp ${PROJECT_TEST_DIR}/micpsolver/test_micpsolver.hpp) - # Database - # -------- - - CXXTEST_ADD_TEST(test_data_reader - test_data_reader.cpp - ${PROJECT_TEST_DIR}/database/test_data_reader.hpp) - target_link_libraries(test_data_reader specmicp_database jsoncpp) - - - CXXTEST_ADD_TEST(test_data_selector - test_data_selector.cpp - ${PROJECT_TEST_DIR}/database/test_data_selector.hpp) - target_link_libraries(test_data_selector specmicp_database jsoncpp) - # Odeint # ------ CXXTEST_ADD_TEST(test_butchertableau test_butchertableau.cpp ${PROJECT_TEST_DIR}/odeint/test_butchertableau.hpp) CXXTEST_ADD_TEST(test_embeddedrungekutta test_embeddedrungekutta.cpp ${PROJECT_TEST_DIR}/odeint/test_embeddedrungekutta.hpp) endif() # Test 'Perso' # ============ add_executable(thermocarbo ${PROJECT_TEST_DIR}/specmicp/thermocarbo.cpp) target_link_libraries(thermocarbo specmicp specmicp_database) add_executable(carboalu ${PROJECT_TEST_DIR}/specmicp/carboalu.cpp) target_link_libraries(carboalu specmicp specmicp_database) add_executable(alkaliactivated ${PROJECT_TEST_DIR}/specmicp/alkaliactivated.cpp) target_link_libraries(alkaliactivated specmicp specmicp_database) add_executable(rate_nicoleau ${PROJECT_TEST_DIR}/specmicp/rate_nicoleau.cpp) target_link_libraries(rate_nicoleau specmicp specmicp_database) add_executable(kinetics ${PROJECT_TEST_DIR}/specmicp/kinetics.cpp) target_link_libraries(kinetics specmicp specmicp_database) set(DATA_TEST ${PROJECT_TEST_DIR}/specmicp/data_test/data_nicoleau_c3sm.csv ${PROJECT_TEST_DIR}/specmicp/data_test/data_nicoleau_c3st1.csv ${PROJECT_TEST_DIR}/specmicp/data_test/data_nicoleau_c3st2.csv ) add_custom_target(data_nicoleau SOURCES ${DATA_TEST}) file(INSTALL ${DATA_TEST} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data_test/) # Catch test #=========== +# Database +# -------- + +set(DATABASE_TEST_DIR ${PROJECT_TEST_DIR}/database) + +add_executable(test_database + ${DATABASE_TEST_DIR}/test_database.cpp + ${DATABASE_TEST_DIR}/database_reader.cpp + ${DATABASE_TEST_DIR}/database_selector.cpp ) +set(TEST_DATABASE_PATH \\"../data/specmicp_database.js\\") +set_target_properties(test_database PROPERTIES COMPILE_FLAGS -DTEST_DATABASE_PATH=${TEST_DATABASE_PATH}) + +target_link_libraries(test_database specmicp_database) # Mesh # ---- add_executable(test_meshes ${PROJECT_TEST_DIR}/reactmicp/meshes/test_meshes.cpp ${PROJECT_TEST_DIR}/reactmicp/meshes/test_uniform_mesh_1d.cpp ${PROJECT_TEST_DIR}/reactmicp/meshes/test_axisymmetric_uniform_mesh_1d.cpp ) # Saturated Diffusion # ------------------- add_executable(test_reactmicp_saturated_diffusion ${PROJECT_TEST_DIR}/reactmicp/systems/saturated_diffusion/test_reactmicp_saturated_diffusion.cpp ${PROJECT_TEST_DIR}/reactmicp/systems/saturated_diffusion/variables.cpp ${PROJECT_TEST_DIR}/reactmicp/systems/saturated_diffusion/solver.cpp ${PROJECT_TEST_DIR}/reactmicp/systems/saturated_diffusion/neutrality_solver.cpp ${PROJECT_TEST_DIR}/reactmicp/systems/saturated_diffusion/transport_program.cpp ${PROJECT_TEST_DIR}/reactmicp/systems/saturated_diffusion/reactive_transport.cpp ${PROJECT_TEST_DIR}/reactmicp/systems/saturated_diffusion/utils.cpp ) target_link_libraries(test_reactmicp_saturated_diffusion reactmicp specmicp specmicp_database) # Specmicp : Adim system # ---------------------- add_executable(test_specmicp_adim ${PROJECT_TEST_DIR}/specmicp/adim/test_specmicp_adim.cpp ${PROJECT_TEST_DIR}/specmicp/adim/adimensional_system.cpp ${PROJECT_TEST_DIR}/specmicp/adim/adimensional_system_solver.cpp ${PROJECT_TEST_DIR}/specmicp/adim/adimensional_system_conditions.cpp ${PROJECT_TEST_DIR}/specmicp/adim/adimensional_system_problem_solver.cpp ${PROJECT_TEST_DIR}/specmicp/adim/adimensional_system_thermocarbo.cpp ${PROJECT_TEST_DIR}/specmicp/adim/adimensional_system_carboalu.cpp ) target_link_libraries(test_specmicp_adim specmicp specmicp_database) # Reactmicp : Reactive transport solver # ------------------------------------- add_executable(test_reactive_transport_solver ${PROJECT_TEST_DIR}/reactmicp/solver/test_reactive_transport_solver.cpp ${PROJECT_TEST_DIR}/reactmicp/solver/test_coupling.cpp ) target_link_libraries(test_reactive_transport_solver reactmicp specmicp specmicp_database) # Saturated diffusion using new reactive transport solver # ------------------------------------------------------- add_executable(test_reactmicp_saturated_react ${PROJECT_TEST_DIR}/reactmicp/systems/saturated_react/test_reactmicp_saturated_react.cpp ${PROJECT_TEST_DIR}/reactmicp/systems/saturated_react/speciation_system.cpp ${PROJECT_TEST_DIR}/reactmicp/systems/saturated_react/variables.cpp ${PROJECT_TEST_DIR}/reactmicp/systems/saturated_react/transport_program.cpp ${PROJECT_TEST_DIR}/reactmicp/systems/saturated_react/equilibrium_stagger.cpp ) target_link_libraries(test_reactmicp_saturated_react reactmicp specmicp specmicp_database) diff --git a/tests/database/database_reader.cpp b/tests/database/database_reader.cpp new file mode 100644 index 0000000..3b79b9f --- /dev/null +++ b/tests/database/database_reader.cpp @@ -0,0 +1,66 @@ +#include "catch.hpp" +#include "database/reader.hpp" + +TEST_CASE("Database reader", "[Database, readeer, jsoncpp]") +{ + SECTION("Equation Parsing") + { + std::map compo; + std::string eq = "A, 2 B, -C, -3D, SDF, - 3 de, - X"; + specmicp::database::parse_equation(eq, compo); + + CHECK(compo["A"] == 1); + CHECK(compo["B"] == 2); + CHECK(compo["C"] == -1); + CHECK(compo["D"] == -3); + CHECK(compo["SDF"] == 1); + CHECK(compo["de"] == -3); + CHECK(compo["X"] == -1); + } + + SECTION("Charge parsing") + { + CHECK(specmicp::database::charge_from_label("neutral") == 0); + CHECK(specmicp::database::charge_from_label("neutral[]") == 0); + CHECK(specmicp::database::charge_from_label("charge[+]") == 1); + CHECK(specmicp::database::charge_from_label("charge[-]") == -1); + CHECK(specmicp::database::charge_from_label("charge[2]") == 2); + CHECK(specmicp::database::charge_from_label("charge[2-]") == -2); + CHECK(specmicp::database::charge_from_label("charge[2+]") == +2); + CHECK(specmicp::database::charge_from_label("charge[-3]") == -3); + CHECK(specmicp::database::charge_from_label("charge[+1]") == +1); + + CHECK_THROWS_AS(specmicp::database::charge_from_label("charge[+3"), specmicp::database::db_invalid_syntax); + CHECK_THROWS_AS(specmicp::database::charge_from_label("charge[ddd]"), specmicp::database::db_invalid_syntax); + } + + SECTION("Test reader") + { + specmicp::database::DataReader reader(TEST_DATABASE_PATH); + REQUIRE_NOTHROW(reader.parse()); + + specmicp::database::DataReader reader2("does_not_exist"); + REQUIRE_THROWS_AS(reader2.parse(), std::invalid_argument); + } + + SECTION("Test parsing") + { + specmicp::database::DataReader reader(TEST_DATABASE_PATH); + REQUIRE_NOTHROW(reader.parse()); + + std::shared_ptr thedata = reader.get_database(); + + // Check basic numbering + REQUIRE(thedata->nb_component > 0); + CHECK(thedata->nb_component == thedata->labels_basis.size()); + CHECK(thedata->nb_component == thedata->_molar_mass_basis.size()); + CHECK(thedata->nb_aqueous == thedata->labels_aqueous.size()); + CHECK(thedata->nb_aqueous == thedata->logk_aqueous.rows()); + CHECK(thedata->nb_aqueous == thedata->nu_aqueous.rows()); + CHECK((thedata->nb_component + thedata->nb_aqueous) == thedata->nu_aqueous.cols()); + CHECK(thedata->nb_mineral == thedata->labels_minerals.size()); + CHECK(thedata->nb_mineral == thedata->logk_mineral.rows()); + CHECK(thedata->nb_mineral == thedata->nu_mineral.rows()); + CHECK((thedata->nb_component + thedata->nb_aqueous) == thedata->nu_mineral.cols()); + } +} diff --git a/tests/database/database_selector.cpp b/tests/database/database_selector.cpp new file mode 100644 index 0000000..5134690 --- /dev/null +++ b/tests/database/database_selector.cpp @@ -0,0 +1,63 @@ +#include "catch.hpp" + +#include "database/database.hpp" +#include "database/selector.hpp" + +using namespace specmicp; + +TEST_CASE("DatabaseSelector", "[Database, Component]") +{ + SECTION("Remove a component") + { + specmicp::database::Database database(TEST_DATABASE_PATH); + database.make_canonical(); + std::shared_ptr data = database.get_database(); + + const index_t component_to_remove = 2; + + const index_t nb_component_orig = data->nb_component; + const std::string label_component_removed = data->labels_basis[component_to_remove]; + + index_t aqueous_to_remove = 0; + for (index_t aqueous: data->range_aqueous()) + { + if (data->nu_aqueous(aqueous, component_to_remove) != 0.0) + ++aqueous_to_remove; + } + const index_t new_nb_aqueous = data->nb_aqueous - aqueous_to_remove; + + index_t mineral_to_remove = 0; + for (index_t mineral: data->range_mineral()) + { + if (data->nu_mineral(mineral, component_to_remove) != 0.0) + ++mineral_to_remove; + } + const index_t new_nb_mineral = data->nb_mineral - mineral_to_remove; + + index_t mineral_kinetic_to_remove = 0; + for (index_t mineral_kinetic: data->range_mineral_kinetic()) + { + if (data->nu_mineral_kinetic(mineral_kinetic, component_to_remove) != 0.0) + ++mineral_kinetic_to_remove; + } + const index_t new_nb_mineral_kinetic = data->nb_mineral_kinetic - mineral_kinetic_to_remove; + + + specmicp::database::DatabaseSelector selector(data); + selector.remove_component(std::vector({2})); + + REQUIRE(data->nb_component == nb_component_orig -1); + + // The component is not present anymore + auto it = std::find(data->labels_basis.begin(), data->labels_basis.end(), label_component_removed); + REQUIRE(it == data->labels_basis.end()); + // Corresponding species have been removed + CHECK(new_nb_aqueous == data->nb_aqueous); + CHECK(new_nb_mineral == data->nb_mineral); + CHECK(new_nb_mineral_kinetic == data->nb_mineral_kinetic); + // the data containers should be resized correctly; + CHECK(data->nu_aqueous.cols() == data->nb_component); + CHECK(data->nu_mineral.cols() == data->nb_component); + CHECK(data->nu_mineral_kinetic.cols() == data->nb_component); + } +} diff --git a/tests/database/test_data_reader.hpp b/tests/database/test_data_reader.hpp deleted file mode 100644 index 51b9777..0000000 --- a/tests/database/test_data_reader.hpp +++ /dev/null @@ -1,96 +0,0 @@ -/*------------------------------------------------------- - - - Module : chemy - - File : check_data_reader.h - - Author : Fabien Georget - -Copyright (c) 2014, Fabien Georget , Princeton University -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - * Neither the name of the Princeton University nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - This is a test file for databasereader. - ----------------------------------------------------------*/ - - -#include -#include "database/reader.hpp" - -#define DATABASE_PATH "../data/specmicp_database.js" - -class TestSuite_DatabaseReader : public CxxTest::TestSuite -{ -public: - void test_reader() - { - specmicp::database::DataReader reader(DATABASE_PATH); - reader.parse(); - - specmicp::database::DataReader reader2("does_not_exist"); - TS_ASSERT_THROWS(reader2.parse(), std::invalid_argument); - - } - - void test_parse_equation() - { - std::map compo; - std::string eq = "A, 2 B, -C, -3D, SDF, - 3 de, - X"; - specmicp::database::parse_equation(eq, compo); - - TS_ASSERT_EQUALS(compo["A"],1); - TS_ASSERT_EQUALS(compo["B"],2); - TS_ASSERT_EQUALS(compo["C"],-1); - TS_ASSERT_EQUALS(compo["D"],-3); - TS_ASSERT_EQUALS(compo["SDF"],1); - TS_ASSERT_EQUALS(compo["de"],-3); - TS_ASSERT_EQUALS(compo["X"], -1); - - } - - void test_parse_charge() - { - TS_ASSERT_EQUALS(specmicp::database::charge_from_label("neutral"), 0); - TS_ASSERT_EQUALS(specmicp::database::charge_from_label("neutral[]"), 0); - TS_ASSERT_EQUALS(specmicp::database::charge_from_label("charge[+]"), 1); - TS_ASSERT_EQUALS(specmicp::database::charge_from_label("charge[-]"), -1); - TS_ASSERT_EQUALS(specmicp::database::charge_from_label("charge[2]"), 2); - TS_ASSERT_EQUALS(specmicp::database::charge_from_label("charge[2-]"), -2); - TS_ASSERT_EQUALS(specmicp::database::charge_from_label("charge[2+]"), +2); - TS_ASSERT_EQUALS(specmicp::database::charge_from_label("charge[-3]"), -3); - TS_ASSERT_EQUALS(specmicp::database::charge_from_label("charge[+1]"), +1); - - TS_ASSERT_THROWS(specmicp::database::charge_from_label("charge[+3"), specmicp::database::db_invalid_syntax); - TS_ASSERT_THROWS(specmicp::database::charge_from_label("charge[ddd]"), specmicp::database::db_invalid_syntax); - } - - void test_parse() - { - specmicp::database::DataReader reader(DATABASE_PATH); - reader.parse(); - - std::shared_ptr thedata = reader.get_database(); - - TS_ASSERT_EQUALS(thedata->nb_component, 8); - } -}; diff --git a/tests/database/test_data_selector.hpp b/tests/database/test_data_selector.hpp deleted file mode 100644 index 201a13d..0000000 --- a/tests/database/test_data_selector.hpp +++ /dev/null @@ -1,69 +0,0 @@ -/*------------------------------------------------------- - - - Module : test/database - - File : test_data_selector.hpp - - Author : Fabien Georget - -Copyright (c) 2014, Fabien Georget , Princeton University -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - * Neither the name of the Princeton University nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ----------------------------------------------------------*/ - -#include -#include "database/database.hpp" -#include "database/selector.hpp" -class DatabaseSelectorTestSuite : public CxxTest::TestSuite -{ -public: - - void test_doit() - { - specmicp::database::Database database("../data/cemdata_specmicp.js"); - database.make_canonical(); - std::shared_ptr data = database.get_database(); - specmicp::database::DatabaseSelector selector(data); - selector.remove_component(std::vector({2})); - - std::cout << "## Basis : " << std::endl; - for (int i=0; inb_component; ++i) - { - std::cout << data->labels_basis[i] << std::endl; - } - std::cout << "## Aqueous : " << std::endl; - for (int j=0; jnb_aqueous; ++j) - { - std::cout << data->labels_aqueous[j] << " : " - << data->nu_aqueous.row(j) << " : " - << data->logk_aqueous(j) << std::endl; - } - std::cout << "## Minerals : " << std::endl; - for (int m=0; mnb_mineral; ++m) - { - std::cout << data->labels_minerals[m] << " : " - << data->nu_mineral.row(m) << " : " - << data->logk_mineral(m) << std::endl; - } - } -}; diff --git a/tests/database/test_database.cpp b/tests/database/test_database.cpp new file mode 100644 index 0000000..178916e --- /dev/null +++ b/tests/database/test_database.cpp @@ -0,0 +1,3 @@ +#define CATCH_CONFIG_MAIN +#include "catch.hpp" +