From c7d533c6e4d159d3ba2972958a908f740bdc5ba5 Mon Sep 17 00:00:00 2001 From: Michael Tryby Date: Thu, 28 Feb 2019 08:11:59 -0500 Subject: [PATCH] Adding tests for gettitle and saveinpfile --- src/epanet.c | 8 +-- src/input2.c | 2 +- tests/CMakeLists.txt | 2 +- tests/{test_toolkit.cpp => test_project.cpp} | 74 +++++++++++++++++--- 4 files changed, 69 insertions(+), 17 deletions(-) rename tests/{test_toolkit.cpp => test_project.cpp} (82%) diff --git a/src/epanet.c b/src/epanet.c index 626d65b..8a829ae 100644 --- a/src/epanet.c +++ b/src/epanet.c @@ -247,9 +247,9 @@ int DLLEXPORT EN_gettitle(EN_Project p, char *line1, char *line2, char *line3) */ { if (!p->Openflag) return 102; - strcpy(line1, p->Title[0]); - strcpy(line2, p->Title[1]); - strcpy(line3, p->Title[2]); + strncpy(line1, p->Title[0], TITLELEN); + strncpy(line2, p->Title[1], TITLELEN); + strncpy(line3, p->Title[2], TITLELEN); return 0; } @@ -265,7 +265,7 @@ int DLLEXPORT EN_settitle(EN_Project p, char *line1, char *line2, char *line3) strncpy(p->Title[0], line1, TITLELEN); strncpy(p->Title[1], line2, TITLELEN); strncpy(p->Title[2], line3, TITLELEN); - return 123; + return 0; } int DLLEXPORT EN_getcount(EN_Project p, int object, int *count) diff --git a/src/input2.c b/src/input2.c index b358ec5..b63a9a9 100644 --- a/src/input2.c +++ b/src/input2.c @@ -272,7 +272,7 @@ int newline(Project *pr, int sect, char *line) { n = (int)strlen(line); if (line[n - 1] == 10) - line[n - 1] = ' '; + line[n - 1] = '\0'; strncpy(pr->Title[parser->Ntitle], line, TITLELEN); parser->Ntitle++; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1e203b5..3737f43 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -27,7 +27,7 @@ if(MSVC) set(Boost_USE_STATIC_LIBS ON) endif(MSVC) set(Boost_THREAD_FOUND OFF) -find_package(Boost COMPONENTS thread) +find_package(Boost COMPONENTS thread filesystem) include_directories (${Boost_INCLUDE_DIRS}) diff --git a/tests/test_toolkit.cpp b/tests/test_project.cpp similarity index 82% rename from tests/test_toolkit.cpp rename to tests/test_project.cpp index 943a400..2e80ec0 100644 --- a/tests/test_toolkit.cpp +++ b/tests/test_project.cpp @@ -19,6 +19,8 @@ #define BOOST_TEST_MODULE "toolkit" #include +#include + #include #include "epanet2_2.h" @@ -29,11 +31,20 @@ #define DATA_PATH_OUT "./test.out" using namespace std; +using namespace boost; + +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_alloc_free) +BOOST_AUTO_TEST_CASE (test_create_delete) { int error = 0; EN_Project ph = NULL; @@ -69,7 +80,9 @@ BOOST_AUTO_TEST_CASE (test_open_close) BOOST_AUTO_TEST_CASE(test_save_reopen) { - string path_inp(DATA_PATH_INP); + int error; + + string path_inp(DATA_PATH_INP); string inp_save("test_reopen.inp"); string path_rpt(DATA_PATH_RPT); string path_out(DATA_PATH_OUT); @@ -78,23 +91,27 @@ BOOST_AUTO_TEST_CASE(test_save_reopen) EN_Project ph_reopen; EN_createproject(&ph_save); + error = EN_open(ph_save, path_inp.c_str(), path_rpt.c_str(), path_out.c_str()); + BOOST_REQUIRE(error == 0); - 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); + error = EN_saveinpfile(ph_save, inp_save.c_str()); + BOOST_REQUIRE(error == 0); - EN_deleteproject(&ph_save); - BOOST_TEST_CHECKPOINT("Saved input file"); + error = EN_close(ph_save); + BOOST_REQUIRE(error == 0); + EN_deleteproject(&ph_save); + BOOST_TEST_CHECKPOINT("Saved input file"); EN_createproject(&ph_reopen); + error = EN_open(ph_reopen, inp_save.c_str(), path_rpt.c_str(), path_out.c_str()); + BOOST_REQUIRE(error == 0); - EN_open(ph_reopen, inp_save.c_str(), path_rpt.c_str(), path_out.c_str()); - EN_close(ph_reopen); - + error = EN_close(ph_reopen); + BOOST_REQUIRE(error == 0); EN_deleteproject(&ph_reopen); } -BOOST_AUTO_TEST_CASE(test_epanet) +BOOST_AUTO_TEST_CASE(test_runproject) { string path_inp(DATA_PATH_INP); string path_rpt(DATA_PATH_RPT); @@ -136,8 +153,43 @@ struct Fixture{ EN_Project ph; }; + BOOST_AUTO_TEST_SUITE(test_epanet_fixture) + +BOOST_FIXTURE_TEST_CASE(test_proj_save, Fixture) +{ + string inp_save("test_projsave.inp"); + + error = EN_saveinpfile(ph, inp_save.c_str()); + BOOST_REQUIRE(error == 0); + + BOOST_CHECK(filesystem::exists(inp_save) == true); + +} + +BOOST_FIXTURE_TEST_CASE(test_proj_title, Fixture) +{ + char c_test[3][80], c_ref[3][80]; + + strncpy(c_ref[0], " EPANET Example Network 1", 26); + strncpy(c_ref[1], "A simple example of modeling chlorine decay. Both bulk and", 59); + strncpy(c_ref[2], "wall reactions are included. ", 30); + + error = EN_gettitle(ph, c_test[0], c_test[1], c_test[2]); + BOOST_REQUIRE(error == 0); + + for (int i = 0; i < 3; i++) { + string test (c_test[i]); + string ref (c_ref[i]); + BOOST_CHECK(check_string(test, ref)); + } + + // Need a test for EN_settitle +} + + + BOOST_FIXTURE_TEST_CASE(test_epanet, Fixture) { error = EN_solveH(ph);