Merge pull request #341 from michaeltryby/dev

Adding test_save_reopen, minor refactoring.
This commit is contained in:
Michael Tryby
2018-11-16 16:39:09 -05:00
committed by GitHub
3 changed files with 119 additions and 70 deletions

View File

@@ -20,49 +20,68 @@ 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)
{
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];
int error = 0;
int Nindex, ndem;
char demname[80];
EN_ProjectHandle ph = NULL;
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_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");
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());
error = EN_open(ph, inp_save.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);
BOOST_CHECK(ndem == 1);
error = EN_getdemandname(ph, Nindex, ndem, demname);
BOOST_REQUIRE(error == 0);
BOOST_REQUIRE(strcmp(demname, "Demand category name")==0);
BOOST_CHECK(check_string(demname, demand_category));
error = EN_close(ph);
BOOST_REQUIRE(error == 0);
error = EN_deleteproject(&ph);

View File

@@ -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
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);

View File

@@ -9,6 +9,14 @@
//#define BOOST_TEST_DYN_LINK
#ifdef _WIN32
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#else
#include <stdlib.h>
#endif
#define BOOST_TEST_MODULE "toolkit"
#include <boost/test/included/unit_test.hpp>
@@ -43,13 +51,13 @@ BOOST_AUTO_TEST_CASE (test_alloc_free)
BOOST_AUTO_TEST_CASE (test_open_close)
{
string path_inp(DATA_PATH_INP);
string path_rpt(DATA_PATH_RPT);
string path_out(DATA_PATH_OUT);
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);
int error = EN_open(ph, path_inp.c_str(), path_rpt.c_str(), path_out.c_str());
BOOST_REQUIRE(error == 0);
@@ -59,11 +67,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 +108,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 +121,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 +212,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 +263,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 +283,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 +323,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 +345,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()