From 73a6c1c847ed7df21759c1b3c79ab8de60beb6f9 Mon Sep 17 00:00:00 2001 From: Michael Tryby Date: Fri, 1 Mar 2019 17:55:45 -0500 Subject: [PATCH] Separating header for test_output --- tests/CMakeLists.txt | 2 +- tests/test_output.cpp | 79 ++++++++++++++++++++++++++++++++++++++++--- tests/test_shared.hpp | 63 +--------------------------------- 3 files changed, 77 insertions(+), 67 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index abb0a31..bb521e0 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -28,7 +28,7 @@ if(MSVC) endif(MSVC) set(Boost_THREAD_FOUND OFF) find_package(Boost COMPONENTS system thread filesystem) -include_directories (${Boost_INCLUDE_DIRS} ${PROJECT_SOURCE_DIR}/include ${PROJECT_SOURCE_DIR}/tools/epanet-output/include) +include_directories (${Boost_INCLUDE_DIRS}) #I like to keep test files in a separate source directory called test diff --git a/tests/test_output.cpp b/tests/test_output.cpp index 82d70e7..6b7766f 100644 --- a/tests/test_output.cpp +++ b/tests/test_output.cpp @@ -13,17 +13,64 @@ //#define BOOST_TEST_DYN_LINK - - #include #include +#include +#include - +#include #define BOOST_TEST_MODULE "output" +#include -#include "test_shared.hpp" +#include "epanet_output.h" +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; +} + + +#define DATA_PATH_OUTPUT "./example1.out" + BOOST_AUTO_TEST_SUITE (test_output_auto) BOOST_AUTO_TEST_CASE(OpenCloseTest) { @@ -43,6 +90,30 @@ BOOST_AUTO_TEST_CASE(OpenCloseTest) { BOOST_AUTO_TEST_SUITE_END() +struct FixtureOutput{ + FixtureOutput() { + path = std::string(DATA_PATH_OUTPUT); + + error = ENR_init(&p_handle); + ENR_clearError(p_handle); + error = ENR_open(p_handle, path.c_str()); + + array = NULL; + array_dim = 0; + } + ~FixtureOutput() { + free((void*)array); + error = ENR_close(&p_handle); + } + + std::string path; + int error; + ENR_Handle p_handle; + + float* array; + int array_dim; +}; + BOOST_AUTO_TEST_SUITE(test_output_fixture) diff --git a/tests/test_shared.hpp b/tests/test_shared.hpp index 07bf161..91176ca 100644 --- a/tests/test_shared.hpp +++ b/tests/test_shared.hpp @@ -7,7 +7,7 @@ #include #include "epanet2_2.h" -#include "epanet_output.h" + // Custom test to check the minimum number of correct decimal digits between @@ -46,40 +46,6 @@ boost::test_tools::predicate_result check_cdd_double(std::vector& test, 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) @@ -116,30 +82,3 @@ struct FixtureOpenClose{ int error; EN_Project ph; }; - - -#define DATA_PATH_OUTPUT "./example1.out" - -struct FixtureOutput{ - FixtureOutput() { - path = std::string(DATA_PATH_OUTPUT); - - error = ENR_init(&p_handle); - ENR_clearError(p_handle); - error = ENR_open(p_handle, path.c_str()); - - array = NULL; - array_dim = 0; - } - ~FixtureOutput() { - free((void*)array); - error = ENR_close(&p_handle); - } - - std::string path; - int error; - ENR_Handle p_handle; - - float* array; - int array_dim; -};