From fca0df7a7b93878a9c1c48e659111ba2276dc513 Mon Sep 17 00:00:00 2001 From: Michael Tryby Date: Fri, 1 Mar 2019 14:14:09 -0500 Subject: [PATCH] Moving custom testing predicates to shared header --- tests/test_fixtures.hpp | 35 ------------ tests/test_hydrqual.cpp | 8 +-- tests/test_output.cpp | 78 ++++++-------------------- tests/test_proj.cpp | 17 ++---- tests/test_rprtanlys.cpp | 11 ++-- tests/test_shared.hpp | 118 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 150 insertions(+), 117 deletions(-) delete mode 100644 tests/test_fixtures.hpp create mode 100644 tests/test_shared.hpp diff --git a/tests/test_fixtures.hpp b/tests/test_fixtures.hpp deleted file mode 100644 index 17260e9..0000000 --- a/tests/test_fixtures.hpp +++ /dev/null @@ -1,35 +0,0 @@ - - -#include - -#include "epanet2_2.h" - - -// NOTE: Project Home needs to be updated to run unit test -#define DATA_PATH_INP "./net1.inp" -#define DATA_PATH_RPT "./test.rpt" -#define DATA_PATH_OUT "./test.out" - - -struct FixtureOpenClose{ - FixtureOpenClose() { - path_inp = std::string(DATA_PATH_INP); - path_rpt = std::string(DATA_PATH_RPT); - path_out = std::string(DATA_PATH_OUT); - - EN_createproject(&ph); - error = EN_open(ph, path_inp.c_str(), path_rpt.c_str(), path_out.c_str()); - } - - ~FixtureOpenClose() { - error = EN_close(ph); - EN_deleteproject(&ph); - } - - std::string path_inp; - std::string path_rpt; - std::string path_out; - - int error; - EN_Project ph; -}; diff --git a/tests/test_hydrqual.cpp b/tests/test_hydrqual.cpp index 24e2100..8db105c 100644 --- a/tests/test_hydrqual.cpp +++ b/tests/test_hydrqual.cpp @@ -17,11 +17,11 @@ #include #endif -#define BOOST_TEST_MODULE hydqual -#include -#include -#include "test_fixtures.hpp" +#define BOOST_TEST_MODULE hydqual + +#include "test_shared.hpp" + using namespace std; using namespace boost; diff --git a/tests/test_output.cpp b/tests/test_output.cpp index b0848a1..e2098e3 100644 --- a/tests/test_output.cpp +++ b/tests/test_output.cpp @@ -12,68 +12,26 @@ // NOTE: Can not dyn link boost using Visual Studio 10 2010 //#define BOOST_TEST_DYN_LINK -#define BOOST_TEST_MODULE "output" -#include + + #include #include -#include -#include + + +#define BOOST_TEST_MODULE "output" + +#include "test_shared.hpp" #include "epanet_output.h" -#define DATA_PATH "./example1.out" +#define DATA_PATH_OUTPUT "./example1.out" -using namespace std; - -// Custom test to check the minimum number of correct decimal digits between -// the test and the ref vectors. -boost::test_tools::predicate_result check_cdd(std::vector& test, - std::vector& ref, long cdd_tol){ - float tmp, min_cdd = 10.0; - - // TODO: What if the vectors aren't the same length? - - std::vector::iterator test_it; - std::vector::iterator ref_it; - - for (test_it = test.begin(), ref_it = ref.begin(); - (test_it < test.end()) && (ref_it < ref.end()); - ++test_it, ++ref_it) - { - if (*test_it != *ref_it) { - // Compute log absolute error - tmp = abs(*test_it - *ref_it); - if (tmp < 1.0e-7f) - tmp = 1.0e-7f; - - else if (tmp > 2.0f) - tmp = 1.0f; - - tmp = -log10(tmp); - if (tmp < 0.0f) - tmp = 0.0f; - - if (tmp < min_cdd) - min_cdd = tmp; - } - } - - return floor(min_cdd) >= cdd_tol; -} - -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_output_auto) BOOST_AUTO_TEST_CASE(OpenCloseTest) { - std::string path = std::string(DATA_PATH); + std::string path = std::string(DATA_PATH_OUTPUT); ENR_Handle p_handle = NULL; ENR_init(&p_handle); @@ -91,7 +49,7 @@ BOOST_AUTO_TEST_SUITE_END() struct Fixture{ Fixture() { - path = std::string(DATA_PATH); + path = std::string(DATA_PATH_OUTPUT); error = ENR_init(&p_handle); ENR_clearError(p_handle); @@ -176,7 +134,7 @@ BOOST_FIXTURE_TEST_CASE(test_getNodeAttribute, Fixture) { std::vector test_vec; test_vec.assign(array, array + array_dim); - BOOST_CHECK(check_cdd(test_vec, ref_vec, 3)); + BOOST_CHECK(check_cdd_float(test_vec, ref_vec, 3)); } BOOST_FIXTURE_TEST_CASE(test_getLinkAttribute, Fixture) { @@ -201,7 +159,7 @@ BOOST_FIXTURE_TEST_CASE(test_getLinkAttribute, Fixture) { std::vector test_vec; test_vec.assign(array, array + array_dim); - BOOST_CHECK(check_cdd(test_vec, ref_vec, 3)); + BOOST_CHECK(check_cdd_float(test_vec, ref_vec, 3)); } BOOST_FIXTURE_TEST_CASE(test_getNodeResult, Fixture) { @@ -217,7 +175,7 @@ BOOST_FIXTURE_TEST_CASE(test_getNodeResult, Fixture) { std::vector test_vec; test_vec.assign(array, array + array_dim); - BOOST_CHECK(check_cdd(test_vec, ref_vec, 3)); + BOOST_CHECK(check_cdd_float(test_vec, ref_vec, 3)); } BOOST_FIXTURE_TEST_CASE(test_getLinkResult, Fixture) { @@ -237,7 +195,7 @@ BOOST_FIXTURE_TEST_CASE(test_getLinkResult, Fixture) { std::vector test_vec; test_vec.assign(array, array + array_dim); - BOOST_CHECK(check_cdd(test_vec, ref_vec, 3)); + BOOST_CHECK(check_cdd_float(test_vec, ref_vec, 3)); } BOOST_FIXTURE_TEST_CASE(test_getNodeSeries, Fixture){ @@ -259,7 +217,7 @@ BOOST_FIXTURE_TEST_CASE(test_getNodeSeries, Fixture){ std::vector test_vec; test_vec.assign(array, array + array_dim); - BOOST_CHECK(check_cdd(test_vec, ref_vec, 3)); + BOOST_CHECK(check_cdd_float(test_vec, ref_vec, 3)); } BOOST_FIXTURE_TEST_CASE(test_getLinkSeries, Fixture) { @@ -281,7 +239,7 @@ BOOST_FIXTURE_TEST_CASE(test_getLinkSeries, Fixture) { std::vector test_vec; test_vec.assign(array, array + array_dim); - BOOST_CHECK(check_cdd(test_vec, ref_vec, 3)); + BOOST_CHECK(check_cdd_float(test_vec, ref_vec, 3)); } BOOST_FIXTURE_TEST_CASE(test_getNetReacts, Fixture) { @@ -297,7 +255,7 @@ BOOST_FIXTURE_TEST_CASE(test_getNetReacts, Fixture) { std::vector test_vec; test_vec.assign(array, array + array_dim); - BOOST_CHECK(check_cdd(test_vec, ref_vec, 2)); + BOOST_CHECK(check_cdd_float(test_vec, ref_vec, 2)); } BOOST_FIXTURE_TEST_CASE(test_getEnergyUsage, Fixture) { @@ -317,7 +275,7 @@ BOOST_FIXTURE_TEST_CASE(test_getEnergyUsage, Fixture) { std::vector test_vec; test_vec.assign(array, array + array_dim); - BOOST_CHECK(check_cdd(test_vec, ref_vec, 3)); + BOOST_CHECK(check_cdd_float(test_vec, ref_vec, 3)); } BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/test_proj.cpp b/tests/test_proj.cpp index 295d1e1..d88872a 100644 --- a/tests/test_proj.cpp +++ b/tests/test_proj.cpp @@ -17,25 +17,16 @@ #include #endif -#define BOOST_TEST_MODULE "project" -#include -#include -#include "test_fixtures.hpp" +#define BOOST_TEST_MODULE "project" + +#include "test_shared.hpp" + 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_proj) BOOST_AUTO_TEST_CASE (test_proj_create_delete) diff --git a/tests/test_rprtanlys.cpp b/tests/test_rprtanlys.cpp index 4af6604..f91e166 100644 --- a/tests/test_rprtanlys.cpp +++ b/tests/test_rprtanlys.cpp @@ -17,11 +17,11 @@ #include #endif -#define BOOST_TEST_MODULE hydqual -#include -#include -#include "test_fixtures.hpp" +#define BOOST_TEST_MODULE hydqual + +#include "test_shared.hpp" + using namespace std; using namespace boost; @@ -51,7 +51,8 @@ BOOST_FIXTURE_TEST_CASE(test_rprt_anlysstats, FixtureOpenClose) } test.assign(array, array + 5); - BOOST_CHECK_EQUAL_COLLECTIONS(ref.begin(), ref.end(), test.begin(), test.end()); +// BOOST_CHECK_EQUAL_COLLECTIONS(ref.begin(), ref.end(), test.begin(), test.end()); + BOOST_CHECK(check_cdd_double(test, ref, 3)); error = EN_getstatistic(ph, 8, &array[0]); BOOST_CHECK(error == 251); diff --git a/tests/test_shared.hpp b/tests/test_shared.hpp new file mode 100644 index 0000000..e852754 --- /dev/null +++ b/tests/test_shared.hpp @@ -0,0 +1,118 @@ + + +#include +#include + +#include +#include + +#include "epanet2_2.h" + + +// Custom test to check the minimum number of correct decimal digits between +// the test and the ref vectors. +boost::test_tools::predicate_result check_cdd_double(std::vector& test, + std::vector& ref, long cdd_tol){ + float tmp, min_cdd = 10.0; + + // TODO: What if the vectors aren't the same length? + + std::vector::iterator test_it; + std::vector::iterator ref_it; + + for (test_it = test.begin(), ref_it = ref.begin(); + (test_it < test.end()) && (ref_it < ref.end()); + ++test_it, ++ref_it) + { + if (*test_it != *ref_it) { + // Compute log absolute error + tmp = abs(*test_it - *ref_it); + if (tmp < 1.0e-7) + tmp = 1.0e-7; + + else if (tmp > 2.0) + tmp = 1.0; + + tmp = -log10(tmp); + if (tmp < 0.0) + tmp = 0.0; + + if (tmp < min_cdd) + min_cdd = tmp; + } + } + + return floor(min_cdd) >= cdd_tol; +} + +boost::test_tools::predicate_result check_cdd_float(std::vector& test, + std::vector& ref, long cdd_tol){ + float tmp, min_cdd = 10.0; + + // TODO: What if the vectors aren't the same length? + + std::vector::iterator test_it; + std::vector::iterator ref_it; + + for (test_it = test.begin(), ref_it = ref.begin(); + (test_it < test.end()) && (ref_it < ref.end()); + ++test_it, ++ref_it) + { + if (*test_it != *ref_it) { + // Compute log absolute error + tmp = abs(*test_it - *ref_it); + if (tmp < 1.0e-7f) + tmp = 1.0e-7f; + + else if (tmp > 2.0f) + tmp = 1.0f; + + tmp = -log10(tmp); + if (tmp < 0.0f) + tmp = 0.0f; + + if (tmp < min_cdd) + min_cdd = tmp; + } + } + + return floor(min_cdd) >= cdd_tol; +} + +boost::test_tools::predicate_result check_string(std::string test, std::string ref) +{ + if (ref.compare(test) == 0) + return true; + else + return false; +} + + +// NOTE: Project Home needs to be updated to run unit test +#define DATA_PATH_INP "./net1.inp" +#define DATA_PATH_RPT "./test.rpt" +#define DATA_PATH_OUT "./test.out" + + +struct FixtureOpenClose{ + FixtureOpenClose() { + path_inp = std::string(DATA_PATH_INP); + path_rpt = std::string(DATA_PATH_RPT); + path_out = std::string(DATA_PATH_OUT); + + EN_createproject(&ph); + error = EN_open(ph, path_inp.c_str(), path_rpt.c_str(), path_out.c_str()); + } + + ~FixtureOpenClose() { + error = EN_close(ph); + EN_deleteproject(&ph); + } + + std::string path_inp; + std::string path_rpt; + std::string path_out; + + int error; + EN_Project ph; +};