From a4c9973d47d5932b217cfad474ec1a384b1ade20 Mon Sep 17 00:00:00 2001 From: Michael Tryby Date: Fri, 16 Nov 2018 13:49:45 -0500 Subject: [PATCH] Adding test_save_reopen, minor refactoring. --- tests/test_demand_categories.cpp | 75 ++++++++++++++++++++------------ tests/test_setid.cpp | 36 +++++++-------- tests/test_toolkit.cpp | 74 +++++++++++++++++++++---------- 3 files changed, 115 insertions(+), 70 deletions(-) diff --git a/tests/test_demand_categories.cpp b/tests/test_demand_categories.cpp index a910500..bbfc43f 100644 --- a/tests/test_demand_categories.cpp +++ b/tests/test_demand_categories.cpp @@ -20,53 +20,72 @@ A demand category name is set, the network is saved, reopened and the new demand using namespace std; +boost::test_tools::predicate_result check_string(std::string test, std::string ref) +{ + if (ref.compare(test) == 0) + return true; + else + return false; +} + BOOST_AUTO_TEST_SUITE (test_toolkit) BOOST_AUTO_TEST_CASE(test_demand_categories) { - int error = 0; - int Nindex, ndem; - char demname[80]; - EN_ProjectHandle ph = NULL; + string path_inp(DATA_PATH_INP); + string inp_save("net1_dem_cat.inp"); + string path_rpt(DATA_PATH_RPT); + string path_out(DATA_PATH_OUT); + + char node_id[] = "12"; + char demand_category[] = "Demand category name"; + char demname[80]; + - std::string path_inp = std::string(DATA_PATH_INP); - std::string path_rpt = std::string(DATA_PATH_RPT); - std::string path_out = std::string(DATA_PATH_OUT); + int error = 0; + int Nindex, ndem; + EN_ProjectHandle ph = NULL; + error = EN_createproject(&ph); - BOOST_REQUIRE(error == 0); error = EN_open(ph, path_inp.c_str(), path_rpt.c_str(), path_out.c_str()); - BOOST_REQUIRE(error == 0); - error = EN_getnodeindex(ph, (char *)"12", &Nindex); + + error = EN_getnodeindex(ph, node_id, &Nindex); BOOST_REQUIRE(error == 0); error = EN_getnumdemands(ph, Nindex, &ndem); BOOST_REQUIRE(error == 0); - BOOST_REQUIRE(ndem == 1); - error = EN_setdemandname(ph, Nindex, ndem, (char *)"Demand category name"); + BOOST_CHECK(ndem == 1); + + error = EN_setdemandname(ph, Nindex, ndem, demand_category); BOOST_REQUIRE(error == 0); - error = EN_saveinpfile(ph, "net1_dem_cat.inp"); - BOOST_REQUIRE(error == 0); - error = EN_close(ph); - BOOST_REQUIRE(error == 0); - error = EN_deleteproject(&ph); + error = EN_saveinpfile(ph, inp_save.c_str()); BOOST_REQUIRE(error == 0); + error = EN_close(ph); + BOOST_REQUIRE(error == 0); + error = EN_deleteproject(&ph); + BOOST_REQUIRE(error == 0); + + BOOST_TEST_CHECKPOINT("Reopening saved input file"); error = EN_createproject(&ph); - BOOST_REQUIRE(error == 0); - error = EN_open(ph, "net1_dem_cat.inp", path_rpt.c_str(), path_out.c_str()); - BOOST_REQUIRE(error == 0); - error = EN_getnodeindex(ph, (char *)"12", &Nindex); + BOOST_REQUIRE(error == 0); + error = EN_open(ph, inp_save.c_str(), path_rpt.c_str(), path_out.c_str()); + BOOST_REQUIRE(error == 0); + + error = EN_getnodeindex(ph, node_id, &Nindex); BOOST_REQUIRE(error == 0); error = EN_getnumdemands(ph, Nindex, &ndem); BOOST_REQUIRE(error == 0); - BOOST_REQUIRE(ndem == 1); - error = EN_getdemandname(ph, Nindex, ndem, demname); - BOOST_REQUIRE(error == 0); - BOOST_REQUIRE(strcmp(demname, "Demand category name")==0); - error = EN_close(ph); - BOOST_REQUIRE(error == 0); - error = EN_deleteproject(&ph); + BOOST_CHECK(ndem == 1); + + error = EN_getdemandname(ph, Nindex, ndem, demname); BOOST_REQUIRE(error == 0); + BOOST_CHECK(check_string(demname, demand_category)); + + error = EN_close(ph); + BOOST_REQUIRE(error == 0); + error = EN_deleteproject(&ph); + BOOST_REQUIRE(error == 0); } BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/tests/test_setid.cpp b/tests/test_setid.cpp index ee9e1da..68fe5cc 100644 --- a/tests/test_setid.cpp +++ b/tests/test_setid.cpp @@ -22,38 +22,37 @@ BOOST_AUTO_TEST_SUITE (test_toolkit) BOOST_AUTO_TEST_CASE(test_setid) { + string path_inp(DATA_PATH_INP); + string path_rpt(DATA_PATH_RPT); + string path_out(DATA_PATH_OUT); + int error = 0; int index; - string newid; EN_ProjectHandle ph = NULL; EN_createproject(&ph); - std::string path_inp = std::string(DATA_PATH_INP); - std::string path_rpt = std::string(DATA_PATH_RPT); - std::string path_out = std::string(DATA_PATH_OUT); - error = EN_open(ph, path_inp.c_str(), path_rpt.c_str(), ""); BOOST_REQUIRE(error == 0); // Test of illegal node name change - newid = "Illegal; node name"; - error = EN_setnodeid(ph, 3, (char *)newid.c_str()); + char newid_1[] = "Illegal; node name"; + error = EN_setnodeid(ph, 3, newid_1); BOOST_REQUIRE(error > 0); // Test of legal node name change - newid = "Node3"; - error = EN_setnodeid(ph, 3, (char *)newid.c_str()); + char newid_2[] = "Node3"; + error = EN_setnodeid(ph, 3, newid_2); BOOST_REQUIRE(error == 0); // Test of illegal link name change - newid = "Illegal; link name"; - error = EN_setlinkid(ph, 3, (char *)newid.c_str()); + char newid_3[] = "Illegal; link name"; + error = EN_setlinkid(ph, 3, newid_3); BOOST_REQUIRE(error > 0); // Test of legal link name change - newid = "Link3"; - error = EN_setlinkid(ph, 3, (char *)newid.c_str()); + char newid_4[] = "Link3"; + error = EN_setlinkid(ph, 3, newid_4); BOOST_REQUIRE(error == 0); // Save the project @@ -64,20 +63,21 @@ BOOST_AUTO_TEST_CASE(test_setid) BOOST_REQUIRE(error == 0); EN_deleteproject(&ph); - // Re-open the saved project + + // Re-open the saved project EN_createproject(&ph); error = EN_open(ph, "net1_setid.inp", path_rpt.c_str(), ""); BOOST_REQUIRE(error == 0); // Check that 3rd node has its new name - error = EN_getnodeindex(ph, "Node3", &index); + error = EN_getnodeindex(ph, newid_2, &index); BOOST_REQUIRE(error == 0); - BOOST_REQUIRE(index == 3); + BOOST_CHECK(index == 3); // Check that 3rd link has its new name - error = EN_getlinkindex(ph, "Link3", &index); + error = EN_getlinkindex(ph, newid_4, &index); BOOST_REQUIRE(error == 0); - BOOST_REQUIRE(index == 3); + BOOST_CHECK(index == 3); error = EN_close(ph); BOOST_REQUIRE(error == 0); diff --git a/tests/test_toolkit.cpp b/tests/test_toolkit.cpp index d92ebbc..535e6fa 100644 --- a/tests/test_toolkit.cpp +++ b/tests/test_toolkit.cpp @@ -9,6 +9,10 @@ //#define BOOST_TEST_DYN_LINK +#define _CRTDBG_MAP_ALLOC +#include +#include + #define BOOST_TEST_MODULE "toolkit" #include @@ -43,12 +47,12 @@ BOOST_AUTO_TEST_CASE (test_alloc_free) BOOST_AUTO_TEST_CASE (test_open_close) { - EN_ProjectHandle ph = NULL; - EN_createproject(&ph); + string path_inp(DATA_PATH_INP); + string path_rpt(DATA_PATH_RPT); + string path_out(DATA_PATH_OUT); - std::string path_inp = std::string(DATA_PATH_INP); - std::string path_rpt = std::string(DATA_PATH_RPT); - std::string path_out = std::string(DATA_PATH_OUT); + EN_ProjectHandle ph = NULL; + EN_createproject(&ph); int error = EN_open(ph, path_inp.c_str(), path_rpt.c_str(), path_out.c_str()); BOOST_REQUIRE(error == 0); @@ -59,11 +63,37 @@ BOOST_AUTO_TEST_CASE (test_open_close) EN_deleteproject(&ph); } +BOOST_AUTO_TEST_CASE(test_save_reopen) +{ + string path_inp(DATA_PATH_INP); + string inp_save("test_reopen.inp"); + string path_rpt(DATA_PATH_RPT); + string path_out(DATA_PATH_OUT); + + EN_ProjectHandle ph_save, ph_reopen; + + EN_createproject(&ph_save); + + EN_open(ph_save, path_inp.c_str(), path_rpt.c_str(), path_out.c_str()); + EN_saveinpfile(ph_save, inp_save.c_str()); + EN_close(ph_save); + + EN_deleteproject(&ph_save); + BOOST_TEST_CHECKPOINT("Saved input file"); + + EN_createproject(&ph_reopen); + + EN_open(ph_reopen, inp_save.c_str(), path_rpt.c_str(), path_out.c_str()); + EN_close(ph_reopen); + + EN_deleteproject(&ph_reopen); +} + BOOST_AUTO_TEST_CASE(test_epanet) { - std::string path_inp = std::string(DATA_PATH_INP); - std::string path_rpt = std::string(DATA_PATH_RPT); - std::string path_out = std::string(DATA_PATH_OUT); + string path_inp(DATA_PATH_INP); + string path_rpt(DATA_PATH_RPT); + string path_out(DATA_PATH_OUT); int error = ENepanet(path_inp.c_str(), path_rpt.c_str(), path_out.c_str(), NULL); BOOST_REQUIRE(error == 0); @@ -74,13 +104,12 @@ BOOST_AUTO_TEST_SUITE_END() struct Fixture{ Fixture() { - path_inp = std::string(DATA_PATH_INP); - path_rpt = std::string(DATA_PATH_RPT); - path_out = std::string(DATA_PATH_OUT); + path_inp = string(DATA_PATH_INP); + path_rpt = string(DATA_PATH_RPT); + path_out = string(DATA_PATH_OUT); EN_createproject(&ph); error = EN_open(ph, path_inp.c_str(), path_rpt.c_str(), path_out.c_str()); - } ~Fixture() { @@ -88,13 +117,12 @@ struct Fixture{ EN_deleteproject(&ph); } - std::string path_inp; - std::string path_rpt; - std::string path_out; + string path_inp; + string path_rpt; + string path_out; int error; EN_ProjectHandle ph; - }; BOOST_AUTO_TEST_SUITE(test_epanet_fixture) @@ -180,8 +208,6 @@ BOOST_FIXTURE_TEST_CASE(test_progressive_stepping, Fixture) BOOST_REQUIRE(error == 0); do { - - error = EN_runH(ph, &t); BOOST_REQUIRE(error == 0); @@ -233,7 +259,7 @@ BOOST_FIXTURE_TEST_CASE(test_setdemandpattern, Fixture) // get demand patterns should be the same with set error = EN_getdemandpattern(ph, i, j, &pat_index_2); BOOST_REQUIRE(error == 0); - BOOST_REQUIRE(pat_index == pat_index_2); + BOOST_CHECK(pat_index == pat_index_2); } } } @@ -253,11 +279,11 @@ BOOST_FIXTURE_TEST_CASE(test_addpattern, Fixture) // get the new patterns count, shoul dbe the old one + 1 error = EN_getcount(ph, EN_PATCOUNT, &n_patterns_2); BOOST_REQUIRE(error == 0); - BOOST_REQUIRE(n_patterns_1 + 1 == n_patterns_2); + BOOST_CHECK(n_patterns_1 + 1 == n_patterns_2); // gwt the new patterns index, should be as the number of patterns error = EN_getpatternindex(ph, newpat, &pat_index); - BOOST_REQUIRE(pat_index == n_patterns_2); + BOOST_CHECK(pat_index == n_patterns_2); } BOOST_FIXTURE_TEST_CASE(test_add_control, Fixture) @@ -293,10 +319,10 @@ BOOST_FIXTURE_TEST_CASE(test_add_control, Fixture) // add new controls error = EN_addcontrol(ph, &Cindex, 0, 13, 1, 11, 110); BOOST_REQUIRE(error == 0); - BOOST_REQUIRE(Cindex == 3); + BOOST_CHECK(Cindex == 3); error = EN_addcontrol(ph, &Cindex, 1, 13, 0, 11, 140); BOOST_REQUIRE(error == 0); - BOOST_REQUIRE(Cindex == 4); + BOOST_CHECK(Cindex == 4); // run with new controls error = EN_openH(ph); @@ -315,7 +341,7 @@ BOOST_FIXTURE_TEST_CASE(test_add_control, Fixture) error = EN_closeH(ph); BOOST_REQUIRE(error == 0); - BOOST_REQUIRE(h1 == h2); // end head should be the same with new controls + BOOST_CHECK(h1 == h2); // end head should be the same with new controls } BOOST_AUTO_TEST_SUITE_END()