diff --git a/.travis.yml b/.travis.yml index fc37c27..3349562 100644 --- a/.travis.yml +++ b/.travis.yml @@ -47,7 +47,7 @@ script: - cmake --build . # run unit tests - cd tests - - ctest + - ctest --output-on-failure # run regression tests - cd $EPANET_HOME - pip install -r tools/requirements.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 92f834e..93fbda1 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,8 +32,7 @@ project(EPANET) # Append local dir to module search path list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) - -option(BUILD_TESTS "Build unit tests (requires Boost test)" OFF) +option(BUILD_TESTS "Build tests (requires Boost)" OFF) option(BUILD_PY_LIB "Build library for Python wrapper" OFF) option(BUILD_COVERAGE "Build library for coverage" OFF) @@ -41,10 +40,24 @@ option(BUILD_COVERAGE "Build library for coverage" OFF) IF (NOT BUILD_PY_LIB) add_subdirectory(run) ENDIF (NOT BUILD_PY_LIB) -add_subdirectory(tools/epanet-output) +add_subdirectory(src/outfile) IF (BUILD_TESTS) - add_subdirectory(tests) + #Prep ourselves for compiling with boost + IF(WIN32) + set(Boost_USE_STATIC_LIBS ON) + ELSE(TRUE) + set(Boost_USE_STATIC_LIBS OFF) + add_definitions(-DBOOST_ALL_DYN_LINK) + ENDIF(WIN32) + + find_package(Boost COMPONENTS unit_test_framework system thread filesystem) + include_directories (${Boost_INCLUDE_DIRS}) + + enable_testing() + add_subdirectory(tests) + add_subdirectory(tests/outfile) + add_subdirectory(tests/util) ENDIF (BUILD_TESTS) diff --git a/appveyor.yml b/appveyor.yml index 7db9355..1d3f5ae 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -21,6 +21,7 @@ environment: GENERATOR: "Visual Studio 15 2017" GROUP: "SUPPORTED" BOOST_ROOT: "C:/Libraries/boost_1_67_0" + PLATFORM: "win32" REF_BUILD_ID: "220dev5" # New build on Visual Studio 15 2017 @@ -37,6 +38,7 @@ init: - set EPANET_HOME=%APPVEYOR_BUILD_FOLDER% - set BUILD_HOME=buildprod - set TEST_HOME=nrtestsuite + - set PATH=%PATH%;%BOOST_ROOT%/ # See values set - echo %APPVEYOR_BUILD_WORKER_IMAGE% @@ -54,8 +56,7 @@ before_build: - cd %BUILD_HOME% - cmake -G "%GENERATOR%" -DBUILD_TESTS=ON - -DBOOST_ROOT="%BOOST_ROOT%" - -DBoost_USE_STATIC_LIBS="ON" .. + -DBOOST_ROOT="%BOOST_ROOT%" .. # run custom build script build_script: @@ -69,7 +70,7 @@ before_test: test_script: # run unit tests - cd %BUILD_HOME%\tests - - ctest -C Release + - ctest -C Release --output-on-failure # run regression tests - cd %EPANET_HOME% - tools\run-nrtest.cmd %REF_BUILD_ID% %SUT_BUILD_ID% diff --git a/src/epanet_py.c b/src/epanet_py.c index 05e7a20..3ee3898 100644 --- a/src/epanet_py.c +++ b/src/epanet_py.c @@ -93,7 +93,7 @@ int EXPORT_PY_API proj_gettitle(Handle ph, char *line1, char *line2, char *line3 int EXPORT_PY_API proj_settitle(Handle ph, const char *line1, const char *line2, const char *line3) { handle_t *pr = (handle_t *)ph; - return error_set(pr->error, EN_settitle(pr->project, line1, line2, line3)); + return error_set(pr->error, EN_settitle(pr->project, (char *)line1, (char *)line2, (char *)line3)); } int EXPORT_PY_API proj_getcount(Handle ph, EN_CountType code, int *count) @@ -761,28 +761,13 @@ void EXPORT_PY_API err_clear(Handle ph) int EXPORT_PY_API err_check(Handle ph, char** msg_buffer) // -// Purpose: Returns the error message or NULL. +// Purpose: Returns the error code and message or 0 and NULL respectively. // // Note: Caller must free memory allocated by EN_check_error // { - int errorcode = 0; - char *temp = NULL; - handle_t *pr = (handle_t *)ph; - - - if (pr == NULL) return -1; - else - { - errorcode = pr->error->error_status; - if (errorcode) - temp = error_check(pr->error); - - *msg_buffer = temp; - } - - return errorcode; + return error_check(pr->error, msg_buffer); } int EXPORT_PY_API toolkit_getversion(int *version) diff --git a/tools/epanet-output/CMakeLists.txt b/src/outfile/CMakeLists.txt similarity index 84% rename from tools/epanet-output/CMakeLists.txt rename to src/outfile/CMakeLists.txt index 0d85da4..cdd2f0f 100644 --- a/tools/epanet-output/CMakeLists.txt +++ b/src/outfile/CMakeLists.txt @@ -6,8 +6,6 @@ # US EPA ORD/NRMRL # -cmake_minimum_required (VERSION 3.0) - # Sets for output directory for executables and libraries. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) @@ -22,12 +20,14 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON) # configure file groups -set(EPANET_OUT_SOURCES src/epanet_output.c src/errormanager.c) +set(EPANET_OUT_SOURCES src/epanet_output.c + ../util/errormanager.c) # the binary output file API add_library(epanet-output SHARED ${EPANET_OUT_SOURCES}) -target_include_directories(epanet-output PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) +target_include_directories(epanet-output PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include + ${CMAKE_CURRENT_SOURCE_DIR}/../) include(GenerateExportHeader) generate_export_header(epanet-output diff --git a/tools/epanet-output/include/epanet_output.h b/src/outfile/include/epanet_output.h similarity index 100% rename from tools/epanet-output/include/epanet_output.h rename to src/outfile/include/epanet_output.h diff --git a/tools/epanet-output/include/epanet_output_enums.h b/src/outfile/include/epanet_output_enums.h similarity index 100% rename from tools/epanet-output/include/epanet_output_enums.h rename to src/outfile/include/epanet_output_enums.h diff --git a/tools/epanet-output/src/epanet_output.c b/src/outfile/src/epanet_output.c similarity index 98% rename from tools/epanet-output/src/epanet_output.c rename to src/outfile/src/epanet_output.c index eef6957..dcbcd75 100644 --- a/tools/epanet-output/src/epanet_output.c +++ b/src/outfile/src/epanet_output.c @@ -35,15 +35,16 @@ // //----------------------------------------------------------------------------- -#include "epanet_output.h" - #include #include #include -#include "errormanager.h" +#include "util/errormanager.h" + +#include "epanet_output.h" #include "messages.h" + // NOTE: These depend on machine data model and may change when porting // F_OFF Must be a 8 byte / 64 bit integer for large file support #ifdef _WIN32 // Windows (32-bit and 64-bit) @@ -112,7 +113,7 @@ int EXPORT_OUT_API ENR_init(ENR_Handle* dp_handle) p_data = (data_t*)calloc(1, sizeof(data_t)); if (p_data != NULL){ - p_data->error_handle = new_errormanager(&errorLookup); + p_data->error_handle = create_error_manager(&errorLookup); *dp_handle = p_data; } else @@ -147,7 +148,7 @@ int EXPORT_OUT_API ENR_close(ENR_Handle* p_handle) else { - dst_errormanager(p_data->error_handle); + delete_error_manager(p_data->error_handle); fclose(p_data->file); free(p_data); @@ -796,23 +797,12 @@ void EXPORT_OUT_API ENR_clearError(ENR_Handle p_handle) int EXPORT_OUT_API ENR_checkError(ENR_Handle p_handle, char** msg_buffer) { - int errorcode = 0; - char *temp = NULL; data_t* p_data; - p_data = (data_t*)p_handle; if (p_data == NULL) return -1; - else - { - errorcode = p_data->error_handle->error_status; - if (errorcode) - temp = check_error(p_data->error_handle); - *msg_buffer = temp; - } - - return errorcode; + return check_error(p_data->error_handle, msg_buffer); } diff --git a/tools/epanet-output/src/messages.h b/src/outfile/src/messages.h similarity index 100% rename from tools/epanet-output/src/messages.h rename to src/outfile/src/messages.h diff --git a/src/util/errormanager.c b/src/util/errormanager.c index 3f4f47c..5a9876c 100644 --- a/src/util/errormanager.c +++ b/src/util/errormanager.c @@ -9,16 +9,25 @@ // Author: Michael E. Tryby // US EPA - ORD/NRMRL //----------------------------------------------------------------------------- + +//#ifdef _WIN32 +//#define _CRTDBG_MAP_ALLOC +//#include +//#include +//#else #include +//#endif #include + #include "errormanager.h" -error_handle_t* error_new_manager(void (*p_error_message)(int, char*, int)) + +error_handle_t *create_error_manager(void (*p_error_message)(int, char*, int)) // // Purpose: Constructs a new error handle. // { - error_handle_t* error_handle; + error_handle_t *error_handle; error_handle = (error_handle_t*)calloc(1, sizeof(error_handle_t)); error_handle->p_msg_lookup = p_error_message; @@ -26,7 +35,7 @@ error_handle_t* error_new_manager(void (*p_error_message)(int, char*, int)) return error_handle; } -void error_dst_manager(error_handle_t* error_handle) +void delete_error_manager(error_handle_t *error_handle) // // Purpose: Destroys the error handle. // @@ -34,38 +43,39 @@ void error_dst_manager(error_handle_t* error_handle) free(error_handle); } -int error_set(error_handle_t* error_handle, int errorcode) +int set_error(error_handle_t *error_handle, int error_code) // // Purpose: Sets an error code in the handle. // { // If the error code is 0 no action is taken and 0 is returned. // This is a feature not a bug. - if (errorcode) - error_handle->error_status = errorcode; + if (error_code) + error_handle->error_status = error_code; - return errorcode; + return error_code; } -char* error_check(error_handle_t* error_handle) +int check_error(error_handle_t *error_handle, char **error_message) // // Purpose: Returns the error message or NULL. // // Note: Caller must free memory allocated by check_error // -{ - char* temp = NULL; +{ int error_code = error_handle->error_status; + char *temp = NULL; - if (error_handle->error_status != 0) { - temp = (char*) calloc(ERR_MAXMSG, sizeof(char)); + if (error_code != 0) { + temp = (char*) calloc(ERR_MAXMSG + 1, sizeof(char)); if (temp) - error_handle->p_msg_lookup(error_handle->error_status, temp, ERR_MAXMSG); + error_handle->p_msg_lookup(error_code, temp, ERR_MAXMSG); } - return temp; + *error_message = temp; + return error_code; } -void error_clear(error_handle_t* error_handle) +void clear_error(error_handle_t *error_handle) // // Purpose: Clears the error from the handle. // diff --git a/src/util/errormanager.h b/src/util/errormanager.h index 2a8099f..6939f35 100644 --- a/src/util/errormanager.h +++ b/src/util/errormanager.h @@ -12,16 +12,27 @@ #define ERR_MAXMSG 256 + +#if defined(__cplusplus) +extern "C" { +#endif + + typedef struct error_s { - int error_status; + int error_status; void (*p_msg_lookup)(int, char*, int); } error_handle_t; -error_handle_t* error_new_manager(void (*p_error_message)(int, char*, int)); -void error_dst_manager(error_handle_t* error_handle); +error_handle_t* create_error_manager(void (*p_error_message)(int, char*, int)); +void delete_error_manager(error_handle_t* error_handle); -int error_set(error_handle_t* error_handle, int errorcode); -char* error_check(error_handle_t* error_handle); -void error_clear(error_handle_t* error_handle); +int set_error(error_handle_t* error_handle, int error_code); +int check_error(error_handle_t* error_handle, char **error_message); +void clear_error(error_handle_t* error_handle); + + +#if defined(__cplusplus) +} +#endif #endif /* ERRORMANAGER_H_ */ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2b285f1..d16c376 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -2,16 +2,11 @@ # CMakeLists.txt - CMake configuration file for epanet/tests # # Created: February 13, 2018 -# Author: Constantin Savtchenko -# Ref: http://neyasystems.com/an-engineers-guide-to-unit-testing-cmake-and-boost-unit-tests/ # # Modified by: Michael E. Tryby # US EPA ORD/NRMRL # -#Setup CMake to run tests -enable_testing() - # Sets for output directory for executables and libraries. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) @@ -20,45 +15,50 @@ if(UNIX) set(CMAKE_CXX_FLAGS "-std=c++11") endif(UNIX) -#Prep ourselves for compiling boost + +set(toolkit_test_srcs + test_toolkit.cpp + test_project.cpp + test_hydraulics.cpp + test_quality.cpp + test_report.cpp + test_analysis.cpp + test_node.cpp + test_demand.cpp + test_link.cpp +# test_pump.cpp + test_pattern.cpp + test_curve.cpp + test_control.cpp + test_net_builder.cpp) + +add_executable(test_toolkit ${toolkit_test_srcs}) + +target_link_libraries(test_toolkit ${Boost_LIBRARIES} epanet2) + +add_test(NAME test_toolkit + COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_toolkit + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/data) + + + +add_executable(test_reent test_reent.cpp) + IF(MSVC) - set(Boost_DEBUG OFF) - set(Boost_DETAILED_FAILURE_MSG OFF) - set(Boost_USE_STATIC_LIBS ON) + target_link_libraries(test_reent ${Boost_LIBRARIES} epanet2) +ELSE(TRUE) + target_link_libraries(test_reent ${Boost_LIBRARIES} pthread epanet2) ENDIF(MSVC) -set(Boost_THREAD_FOUND OFF) -find_package(Boost COMPONENTS unit_test_framework system thread filesystem) -include_directories (${Boost_INCLUDE_DIRS}) +add_test(NAME test_reent + COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_reent + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/data) -#I like to keep test files in a separate source directory called test -file(GLOB TEST_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} test_*.cpp) +# ctest doesn't like tests added in subdirectories so adding them here +add_test(NAME test_errormanager + COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_errormanager) -add_library(shared_test OBJECT shared_test.cpp) - -#Run through each source -foreach(testSrc ${TEST_SRCS}) - #Extract the filename without an extension (NAME_WE) - get_filename_component(testName ${testSrc} NAME_WE) - - #Add compile target - IF(${testName} MATCHES "test_reent") - add_executable(${testName} ${testSrc}) - ELSE (TRUE) - add_executable(${testName} ${testSrc} $) - ENDIF(${testName} MATCHES "test_reent") - - #link to Boost libraries AND your targets and dependencies - IF(MSVC) - target_link_libraries(${testName} ${Boost_LIBRARIES} epanet2 epanet-output) - ELSE(TRUE) - target_link_libraries(${testName} ${Boost_LIBRARIES} pthread epanet2 epanet-output) - ENDIF(MSVC) - - #Finally add it to test execution - #Notice the WORKING_DIRECTORY and COMMAND - add_test(NAME ${testName} - COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${testName} - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/data) -endforeach(testSrc) +add_test(NAME test_output + COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_output + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/outfile/data) diff --git a/tests/outfile/CMakeLists.txt b/tests/outfile/CMakeLists.txt new file mode 100644 index 0000000..162a2ee --- /dev/null +++ b/tests/outfile/CMakeLists.txt @@ -0,0 +1,12 @@ + + + +# Sets for output directory for executables and libraries +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) + + +add_executable(test_output test_output.cpp) +target_include_directories(test_output PUBLIC ../../src/outfile/include) +target_link_libraries(test_output ${Boost_LIBRARIES} epanet-output) diff --git a/tools/epanet-output/test/data/net1.out b/tests/outfile/data/example1.out similarity index 88% rename from tools/epanet-output/test/data/net1.out rename to tests/outfile/data/example1.out index 6cede26..416c98b 100644 Binary files a/tools/epanet-output/test/data/net1.out and b/tests/outfile/data/example1.out differ diff --git a/tests/test_output.cpp b/tests/outfile/test_output.cpp similarity index 87% rename from tests/test_output.cpp rename to tests/outfile/test_output.cpp index 89feede..bcae21d 100644 --- a/tests/test_output.cpp +++ b/tests/outfile/test_output.cpp @@ -1,5 +1,5 @@ /* - * test_epanet_output.cpp + * test_output.cpp * * Created: 8/4/2017 * Author: Michael E. Tryby @@ -8,22 +8,17 @@ * Unit testing for EPANET Output API. */ -// NOTE: Travis installs libboost test version 1.5.4 -// 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 -#define BOOST_TEST_MODULE "output" -//#include -#include "shared_test.hpp" +#include #include "epanet_output.h" -#include "epanet2_2.h" +//#include "epanet2_2.h" boost::test_tools::predicate_result check_cdd_float(std::vector& test, @@ -60,6 +55,14 @@ boost::test_tools::predicate_result check_cdd_float(std::vector& test, 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" @@ -85,45 +88,45 @@ BOOST_AUTO_TEST_CASE(OpenCloseTest) { } -// Test access to output file with the project open -BOOST_AUTO_TEST_CASE(AccessTest){ - - std::string path_inp(DATA_PATH_NET1); - std::string path_rpt(DATA_PATH_RPT); - std::string path_out(DATA_PATH_OUT); - - EN_Project ph = NULL; - ENR_Handle p_handle = 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); - - error = EN_solveH(ph); - BOOST_REQUIRE(error == 0); - - error = EN_solveQ(ph); - BOOST_REQUIRE(error == 0); - - error = EN_report(ph); - BOOST_REQUIRE(error == 0); - - - // Access to output file prior to project close - error = ENR_init(&p_handle); - BOOST_REQUIRE(error == 0); - error = ENR_open(p_handle, path_out.c_str()); - BOOST_REQUIRE(error == 0); - error = ENR_close(&p_handle); - BOOST_REQUIRE(error == 0); - - - error = EN_close(ph); - BOOST_REQUIRE(error == 0); - - EN_deleteproject(&ph); -} +// // Test access to output file with the project open +// BOOST_AUTO_TEST_CASE(AccessTest){ +// +// std::string path_inp(DATA_PATH_NET1); +// std::string path_rpt(DATA_PATH_RPT); +// std::string path_out(DATA_PATH_OUT); +// +// EN_Project ph = NULL; +// ENR_Handle p_handle = 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); +// +// error = EN_solveH(ph); +// BOOST_REQUIRE(error == 0); +// +// error = EN_solveQ(ph); +// BOOST_REQUIRE(error == 0); +// +// error = EN_report(ph); +// BOOST_REQUIRE(error == 0); +// +// +// // Access to output file prior to project close +// error = ENR_init(&p_handle); +// BOOST_REQUIRE(error == 0); +// error = ENR_open(p_handle, path_out.c_str()); +// BOOST_REQUIRE(error == 0); +// error = ENR_close(&p_handle); +// BOOST_REQUIRE(error == 0); +// +// +// error = EN_close(ph); +// BOOST_REQUIRE(error == 0); +// +// EN_deleteproject(&ph); +// } BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/shared_test.cpp b/tests/shared_test.cpp deleted file mode 100644 index 97387a1..0000000 --- a/tests/shared_test.cpp +++ /dev/null @@ -1,5 +0,0 @@ -// -// Hack to get boost unit test to compile faster -// - -#include diff --git a/tests/test_rprtanlys.cpp b/tests/test_analysis.cpp similarity index 57% rename from tests/test_rprtanlys.cpp rename to tests/test_analysis.cpp index 01e452c..f7120f1 100644 --- a/tests/test_rprtanlys.cpp +++ b/tests/test_analysis.cpp @@ -1,49 +1,23 @@ -// -// test_rprtanlys.cpp -// -// Date Created: February 28, 2019 -// -// Author: Michael E. Tryby -// US EPA - ORD/NRMRL -// +/* + ****************************************************************************** + Project: OWA EPANET + Version: 2.2 + Module: test_analysis.cpp + Description: Tests EPANET toolkit api functions + Authors: see AUTHORS + Copyright: see AUTHORS + License: see LICENSE + Last Updated: 03/21/2019 + ****************************************************************************** +*/ -#define BOOST_TEST_MODULE hydqual +//#define BOOST_ALL_DYN_LINK +#include -#include "shared_test.hpp" - -using namespace boost; +#include "test_toolkit.hpp" -BOOST_AUTO_TEST_SUITE (test_rprtanlys) - -BOOST_FIXTURE_TEST_CASE(test_rprt_anlysstats, FixtureOpenClose) -{ - int i; - double array[5]; - - std::vector test; - std::vector ref = {3.0, 7.0799498320679432e-06, 1.6680242187483429e-08, - 0.0089173150106518495, 0.99999998187144024}; - - error = EN_solveH(ph); - BOOST_REQUIRE(error == 0); - - error = EN_solveQ(ph); - BOOST_REQUIRE(error == 0); - - - for (i=EN_ITERATIONS; i<=EN_MASSBALANCE; i++) { - error = EN_getstatistic(ph, i, &array[i]); - BOOST_REQUIRE(error == 0); - } - - test.assign(array, array + 5); -// 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); -} +BOOST_AUTO_TEST_SUITE (test_analysis) BOOST_FIXTURE_TEST_CASE(test_anlys_getoption, FixtureOpenClose) { diff --git a/tests/test_comments.cpp b/tests/test_comments.cpp deleted file mode 100644 index 404b5ce..0000000 --- a/tests/test_comments.cpp +++ /dev/null @@ -1,131 +0,0 @@ -// Test of EPANET's Comment Handling Functions -// -// This is a test of the API functions EN_getcomment and EN_setcomment -// -#define _CRT_SECURE_NO_DEPRECATE - -//#define NO_BOOST - -#ifndef NO_BOOST -#define BOOST_TEST_MODULE "toolkit" -#include -#endif - -#include -#include -#include "epanet2_2.h" - -#define DATA_PATH_INP "./net1.inp" -#define DATA_PATH_RPT "./test.rpt" -#define DATA_PATH_OUT "./test.out" -#define DATA_PATH_TMP "./tmp.inp" - -#ifdef NO_BOOST -#define BOOST_REQUIRE(x) (((x)) ? cout << "\nPassed at line " << __LINE__ : cout << "\nFailed at line " << __LINE__ ) -#endif - -using namespace std; - -int checkComments(EN_Project ph) -{ - int index; - char comment[EN_MAXMSG + 1]; - EN_getnodeindex(ph, (char *)"11", &index); - EN_getcomment(ph, EN_NODE, index, comment); - if (strcmp(comment, (char *)"J11") != 0) return 0; - - EN_getnodeindex(ph, (char *)"23", &index); - EN_getcomment(ph, EN_NODE, index, comment); - if (strcmp(comment, (char *)"Junc23") != 0) return 0; - - EN_getlinkindex(ph, (char *)"11", &index); - EN_getcomment(ph, EN_LINK, index, comment); - if (strcmp(comment, (char *)"P11") != 0) return 0; - - EN_getlinkindex(ph, (char *)"9", &index); - EN_getcomment(ph, EN_LINK, index, comment); - if (strcmp(comment, (char *)"Pump9") != 0) return 0; - - EN_getpatternindex(ph, (char *)"1", &index); - EN_getcomment(ph, EN_TIMEPAT, index, comment); - if (strcmp(comment, (char *)"Time Pattern 1") != 0) return 0; - - EN_getcurveindex(ph, (char *)"1", &index); - EN_getcomment(ph, EN_CURVE, index, comment); - if (strcmp(comment, (char *)"Curve 1") != 0) return 0; - return 1; -} - -#ifndef NO_BOOST -BOOST_AUTO_TEST_SUITE (test_toolkit) -BOOST_AUTO_TEST_CASE(test_setlinktype) -{ -#else -int main(int argc, char *argv[]) -{ -#endif - - int error = 0; - int index; - char comment[EN_MAXMSG+1]; - - // Create & load a project - EN_Project ph = NULL; - EN_createproject(&ph); - std::string path_inp = string(DATA_PATH_INP); - std::string path_rpt = string(DATA_PATH_RPT); - std::string path_out = string(DATA_PATH_OUT); - error = EN_open(ph, path_inp.c_str(), path_rpt.c_str(), ""); - BOOST_REQUIRE(error == 0); - - // Add comments to selected objects - EN_getnodeindex(ph, (char *)"11", &index); - EN_setcomment(ph, EN_NODE, index, (char *)"J11"); - EN_getnodeindex(ph, (char *)"23", &index); - EN_setcomment(ph, EN_NODE, index, (char *)"Junc23"); - EN_getlinkindex(ph, (char *)"11", &index); - - EN_setcomment(ph, EN_LINK, index, (char *)"P11"); - EN_getlinkindex(ph, (char *)"9", &index); - EN_setcomment(ph, EN_LINK, index, (char *)"Pump9"); - - EN_getpatternindex(ph, (char *)"1", &index); - EN_setcomment(ph, EN_TIMEPAT, index, (char *)"Time Pattern 1"); - - EN_getcurveindex(ph, (char *)"1", &index); - EN_setcomment(ph, EN_CURVE, index, (char *)"Curve 1"); - - // Retrieve comments and test their values - BOOST_REQUIRE(checkComments(ph) == 1); - - // Replace short comment with longer one and vice versa - EN_getnodeindex(ph, (char *)"11", &index); - EN_setcomment(ph, EN_NODE, index, (char *)"Junction11"); - EN_getcomment(ph, EN_NODE, index, comment); - BOOST_REQUIRE(strcmp(comment, (char *)"Junction11") == 0); - EN_setcomment(ph, EN_NODE, index, (char *)"J11"); - EN_getcomment(ph, EN_NODE, index, comment); - BOOST_REQUIRE(strcmp(comment, (char *)"J11") == 0); - - // Save & re-open project - string path_tmp = string(DATA_PATH_TMP); - EN_saveinpfile(ph, path_tmp.c_str()); - EN_close(ph); - error = EN_open(ph, path_tmp.c_str(), path_rpt.c_str(), ""); - BOOST_REQUIRE(error == 0); - - // Check that comments were saved & read correctly - BOOST_REQUIRE(checkComments(ph) == 1); - remove(path_tmp.c_str()); - - // Close project - EN_close(ph); - EN_deleteproject(&ph); - -#ifdef NO_BOOST - return 0; -#endif -} -#ifndef NO_BOOST -BOOST_AUTO_TEST_SUITE_END() -#endif \ No newline at end of file diff --git a/tests/test_addrule.cpp b/tests/test_control.cpp similarity index 82% rename from tests/test_addrule.cpp rename to tests/test_control.cpp index bbc6172..32b3f5f 100644 --- a/tests/test_addrule.cpp +++ b/tests/test_control.cpp @@ -1,5 +1,15 @@ -// Test of EN_addrule, EN_deletenode & EN_deletelink EPANET API Functions -#define _CRT_SECURE_NO_DEPRECATE +/* + ****************************************************************************** + Project: OWA EPANET + Version: 2.2 + Module: test_control.cpp + Description: Tests EPANET toolkit api functions + Authors: see AUTHORS + Copyright: see AUTHORS + License: see LICENSE + Last Updated: 03/21/2019 + ****************************************************************************** +*/ /* This is a test for the API functions that adds rules and deletes @@ -7,10 +17,9 @@ nodes and links from a project. Deletion can be conditional on node or link appearing in any simple or rule-based controls. */ +#include -#define BOOST_TEST_MODULE "rules" - -#include "shared_test.hpp" +#include "test_toolkit.hpp" char R1[] = "RULE 1 \n IF NODE 2 LEVEL < 100 \n THEN LINK 9 STATUS = OPEN"; @@ -19,7 +28,7 @@ char R3[] = "RULE 3\nIF NODE 23 PRESSURE ABOVE 140\nAND NODE 2 LEVEL > 120\n" "THEN LINK 113 STATUS = CLOSED\nELSE LINK 22 STATUS = CLOSED"; -BOOST_AUTO_TEST_SUITE (test_addrule) +BOOST_AUTO_TEST_SUITE (test_controls) BOOST_FIXTURE_TEST_CASE(test_add_get_rule, FixtureOpenClose) { diff --git a/tests/test_curve.cpp b/tests/test_curve.cpp new file mode 100644 index 0000000..941b473 --- /dev/null +++ b/tests/test_curve.cpp @@ -0,0 +1,40 @@ +/* + ****************************************************************************** + Project: OWA EPANET + Version: 2.2 + Module: test_curve.cpp + Description: Tests EPANET toolkit api functions + Authors: see AUTHORS + Copyright: see AUTHORS + License: see LICENSE + Last Updated: 03/21/2019 + ****************************************************************************** +*/ + +#include + +#include "test_toolkit.hpp" + + +BOOST_AUTO_TEST_SUITE (curve) + +BOOST_FIXTURE_TEST_CASE(test_curve_comments, FixtureOpenClose) +{ + int index; + char comment[EN_MAXMSG + 1]; + + // Set curve comments + error = EN_getcurveindex(ph, (char *)"1", &index); + BOOST_REQUIRE(error == 0); + error = EN_setcomment(ph, EN_CURVE, index, (char *)"Curve 1"); + BOOST_REQUIRE(error == 0); + + // Check curve comments + error = EN_getcurveindex(ph, (char *)"1", &index); + BOOST_REQUIRE(error == 0); + error = EN_getcomment(ph, EN_CURVE, index, comment); + BOOST_REQUIRE(error == 0); + BOOST_CHECK(check_string(comment, (char *)"Curve 1")); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/test_demand_categories.cpp b/tests/test_demand.cpp similarity index 74% rename from tests/test_demand_categories.cpp rename to tests/test_demand.cpp index f4ee864..2955079 100644 --- a/tests/test_demand_categories.cpp +++ b/tests/test_demand.cpp @@ -1,18 +1,22 @@ -// -// test_demand_categories.cpp -// - /* -This is a test for the demand categories names get\set APIs -A demand category name is set, the network is saved, reopened and the new demand category name is checked. + ****************************************************************************** + Project: OWA EPANET + Version: 2.2 + Module: test_demand.cpp + Description: Tests EPANET toolkit api functions + Authors: see AUTHORS + Copyright: see AUTHORS + License: see LICENSE + Last Updated: 03/21/2019 + ****************************************************************************** */ -#define BOOST_TEST_MODULE "demands" +#include -#include "shared_test.hpp" +#include "test_toolkit.hpp" -BOOST_AUTO_TEST_SUITE (test_demands) +BOOST_AUTO_TEST_SUITE (test_demand) BOOST_AUTO_TEST_CASE(test_categories_save) { @@ -41,7 +45,7 @@ BOOST_AUTO_TEST_CASE(test_categories_save) BOOST_REQUIRE(error == 0); } -BOOST_AUTO_TEST_CASE(test_categories_reopen, * boost::unit_test::depends_on("test_demands/test_categories_save")) +BOOST_AUTO_TEST_CASE(test_categories_reopen, * boost::unit_test::depends_on("test_demand/test_categories_save")) { int error = 0; int Nindex, ndem; @@ -59,11 +63,11 @@ BOOST_AUTO_TEST_CASE(test_categories_reopen, * boost::unit_test::depends_on("tes error = EN_getnumdemands(ph, Nindex, &ndem); BOOST_REQUIRE(error == 0); BOOST_CHECK(ndem == 1); - + char demname[80]; error = EN_getdemandname(ph, Nindex, ndem, demname); BOOST_REQUIRE(error == 0); - + BOOST_CHECK(check_string(demname, "Demand category name")); error = EN_close(ph); diff --git a/tests/test_hydraulics.cpp b/tests/test_hydraulics.cpp new file mode 100644 index 0000000..8fe81a0 --- /dev/null +++ b/tests/test_hydraulics.cpp @@ -0,0 +1,84 @@ +/* + ****************************************************************************** + Project: OWA EPANET + Version: 2.2 + Module: test_hydraulics.cpp + Description: Tests EPANET toolkit api functions + Authors: see AUTHORS + Copyright: see AUTHORS + License: see LICENSE + Last Updated: 03/21/2019 + ****************************************************************************** +*/ + +#include +#include + +#include "test_toolkit.hpp" + + +BOOST_AUTO_TEST_SUITE (test_hydraulics) + +BOOST_FIXTURE_TEST_CASE(test_solveH, FixtureOpenClose) +{ + error = EN_solveH(ph); + BOOST_REQUIRE(error == 0); +} + +BOOST_FIXTURE_TEST_CASE(test_hyd_step, FixtureOpenClose) +{ + int flag = 00; + long t, tstep; + + error = EN_openH(ph); + BOOST_REQUIRE(error == 0); + + error = EN_initH(ph, flag); + BOOST_REQUIRE(error == 0); + + do { + error = EN_runH(ph, &t); + BOOST_REQUIRE(error == 0); + + error = EN_nextH(ph, &tstep); + BOOST_REQUIRE(error == 0); + + } while (tstep > 0); + + error = EN_closeH(ph); + BOOST_REQUIRE(error == 0); +} + +BOOST_FIXTURE_TEST_CASE(test_hydr_save, FixtureOpenClose) +{ + error = EN_solveH(ph); + BOOST_REQUIRE(error == 0); + + error = EN_saveH(ph); + BOOST_REQUIRE(error == 0); + + error = EN_report(ph); + BOOST_REQUIRE(error == 0); +} + +BOOST_FIXTURE_TEST_CASE(test_hydr_savefile, FixtureOpenClose) +{ + error = EN_solveH(ph); + BOOST_REQUIRE(error == 0); + + error = EN_savehydfile(ph, "test_savefile.hyd"); + BOOST_REQUIRE(error == 0); + + BOOST_CHECK(boost::filesystem::exists("test_savefile.hyd") == true); +} + +BOOST_FIXTURE_TEST_CASE(test_hydr_usefile, FixtureOpenClose, * boost::unit_test::depends_on("test_hydraulics/test_hydr_savefile")) +{ + error = EN_usehydfile(ph, "test_savefile.hyd"); + BOOST_REQUIRE(error == 0); + + error = EN_solveQ(ph); + BOOST_REQUIRE(error == 0); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/test_hydrqual.cpp b/tests/test_hydrqual.cpp deleted file mode 100644 index 4364545..0000000 --- a/tests/test_hydrqual.cpp +++ /dev/null @@ -1,163 +0,0 @@ -// -// test_hydrqual.cpp -// -// Date Created: February 28, 2019 -// -// Author: Michael E. Tryby -// US EPA - ORD/NRMRL -// - - -#define BOOST_TEST_MODULE hydrqual - -#include "shared_test.hpp" - - -using namespace std; -using namespace boost; - - -BOOST_AUTO_TEST_SUITE (test_hydrqual) - -BOOST_FIXTURE_TEST_CASE(test_solveH_solveQ, FixtureOpenClose) -{ - error = EN_solveH(ph); - BOOST_REQUIRE(error == 0); - - error = EN_solveQ(ph); - BOOST_REQUIRE(error == 0); - - error = EN_report(ph); - BOOST_REQUIRE(error == 0); -} - -BOOST_FIXTURE_TEST_CASE(test_hyd_step, FixtureOpenClose) -{ - int flag = 00; - long t, tstep; - - error = EN_openH(ph); - BOOST_REQUIRE(error == 0); - - error = EN_initH(ph, flag); - BOOST_REQUIRE(error == 0); - - do { - error = EN_runH(ph, &t); - BOOST_REQUIRE(error == 0); - - error = EN_nextH(ph, &tstep); - BOOST_REQUIRE(error == 0); - - } while (tstep > 0); - - error = EN_closeH(ph); - BOOST_REQUIRE(error == 0); -} - -BOOST_FIXTURE_TEST_CASE(test_qual_step, FixtureOpenClose) -{ - int flag = 0; - long t, tstep; - - error = EN_solveH(ph); - BOOST_REQUIRE(error == 0); - - error = EN_openQ(ph); - BOOST_REQUIRE(error == 0); - - error = EN_initQ(ph, flag); - BOOST_REQUIRE(error == 0); - - do { - error = EN_runQ(ph, &t); - BOOST_REQUIRE(error == 0); - - error = EN_stepQ(ph, &tstep); - BOOST_REQUIRE(error == 0); - - } while (tstep > 0); - - error = EN_closeQ(ph); - BOOST_REQUIRE(error == 0); -} - -BOOST_FIXTURE_TEST_CASE(test_progressive_step, FixtureOpenClose) -{ - int flag = EN_NOSAVE; - long t, tstep_h, tstep_q; - - error = EN_openH(ph); - BOOST_REQUIRE(error == 0); - - error = EN_initH(ph, flag); - BOOST_REQUIRE(error == 0); - - error = EN_openQ(ph); - BOOST_REQUIRE(error == 0); - - error = EN_initQ(ph, flag); - BOOST_REQUIRE(error == 0); - - do { - error = EN_runH(ph, &t); - BOOST_REQUIRE(error == 0); - - error = EN_runQ(ph, &t); - BOOST_REQUIRE(error == 0); - - error = EN_nextH(ph, &tstep_h); - BOOST_REQUIRE(error == 0); - - error = EN_nextQ(ph, &tstep_q); - BOOST_REQUIRE(error == 0); - - } while (tstep_h > 0); - - error = EN_closeH(ph); - BOOST_REQUIRE(error == 0); - - error = EN_closeQ(ph); - BOOST_REQUIRE(error == 0); - -} - -BOOST_FIXTURE_TEST_CASE(test_hydr_save, FixtureOpenClose) -{ - error = EN_solveH(ph); - BOOST_REQUIRE(error == 0); - - error = EN_saveH(ph); - BOOST_REQUIRE(error == 0); - - error = EN_report(ph); - BOOST_REQUIRE(error == 0); -} - -BOOST_FIXTURE_TEST_CASE(test_hydr_savefile, FixtureOpenClose) -{ - string hyd_file("test_savefile.hyd"); - - error = EN_solveH(ph); - BOOST_REQUIRE(error == 0); - - error = EN_savehydfile(ph, hyd_file.c_str()); - BOOST_REQUIRE(error == 0); - - BOOST_CHECK(filesystem::exists(hyd_file) == true); -} - -BOOST_FIXTURE_TEST_CASE(test_hydr_usefile, FixtureOpenClose, * unit_test::depends_on("test_hydrqual/test_hydr_savefile")) -{ - string hyd_file("test_savefile.hyd"); - - error = EN_usehydfile(ph, hyd_file.c_str()); - BOOST_REQUIRE(error == 0); - - error = EN_solveQ(ph); - BOOST_REQUIRE(error == 0); -} - - - -BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/test_link.cpp b/tests/test_link.cpp index a712b0f..15f451e 100644 --- a/tests/test_link.cpp +++ b/tests/test_link.cpp @@ -1,17 +1,19 @@ -// Test of ENsetlinktype EPANET API Function -#define _CRT_SECURE_NO_DEPRECATE - /* -This is a test for the API function that changes a link's type. -Two links in Net1.inp are changed: Pipe 113 is reversed with a CV added -and Pipe 121 is changed to a 100 psi PRV. After running the revised model, -at hour 0 the flow in Pipe 113 should be zero and the pressure at node 31 -of the PRV 121 should be 100. + ****************************************************************************** + Project: OWA EPANET + Version: 2.2 + Module: test_link.cpp + Description: Tests EPANET toolkit api functions + Authors: see AUTHORS + Copyright: see AUTHORS + License: see LICENSE + Last Updated: 03/21/2019 + ****************************************************************************** */ -#define BOOST_TEST_MODULE "link" +#include -#include "shared_test.hpp" +#include "test_toolkit.hpp" BOOST_AUTO_TEST_SUITE (test_link) @@ -86,12 +88,8 @@ BOOST_AUTO_TEST_CASE(test_setlinktype) EN_deleteproject(&ph); } -BOOST_AUTO_TEST_SUITE_END() - -BOOST_AUTO_TEST_SUITE(setid_save_reopen) - -BOOST_AUTO_TEST_CASE(test_setid_save) +BOOST_AUTO_TEST_CASE(test_link_setid_save) { int error = 0; @@ -120,7 +118,7 @@ BOOST_AUTO_TEST_CASE(test_setid_save) EN_deleteproject(&ph); } -BOOST_AUTO_TEST_CASE(test_setid_reopen, * boost::unit_test::depends_on("setid_save_reopen/test_setid_save")) +BOOST_AUTO_TEST_CASE(test_link_setid_reopen, * boost::unit_test::depends_on("test_link/test_link_setid_save")) { int error = 0; int index; @@ -135,12 +133,41 @@ BOOST_AUTO_TEST_CASE(test_setid_reopen, * boost::unit_test::depends_on("setid_sa // Check that 3rd link has its new name error = EN_getlinkindex(ph, (char *)"Link3", &index); BOOST_REQUIRE(error == 0); - BOOST_REQUIRE(index == 3); + BOOST_CHECK(index == 3); error = EN_close(ph); BOOST_REQUIRE(error == 0); EN_deleteproject(&ph); } +BOOST_FIXTURE_TEST_CASE(test_link_comments, FixtureOpenClose) +{ + int index; + char comment[EN_MAXMSG + 1]; + + // Set link comments + error = EN_getlinkindex(ph, (char *)"11", &index); + BOOST_REQUIRE(error == 0); + error = EN_setcomment(ph, EN_LINK, index, (char *)"P11"); + BOOST_REQUIRE(error == 0); + + error = EN_getlinkindex(ph, (char *)"9", &index); + BOOST_REQUIRE(error == 0); + error = EN_setcomment(ph, EN_LINK, index, (char *)"Pump9"); + BOOST_REQUIRE(error == 0); + + // Check link comments + error = EN_getlinkindex(ph, (char *)"11", &index); + BOOST_REQUIRE(error == 0); + error = EN_getcomment(ph, EN_LINK, index, comment); + BOOST_REQUIRE(error == 0); + BOOST_CHECK(check_string(comment, (char *)"P11")); + + error = EN_getlinkindex(ph, (char *)"9", &index); + BOOST_REQUIRE(error == 0); + error = EN_getcomment(ph, EN_LINK, index, comment); + BOOST_REQUIRE(error == 0); + BOOST_CHECK(check_string(comment, (char *)"Pump9")); +} BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/test_net_builder.cpp b/tests/test_net_builder.cpp index 056e4e6..5c6f274 100644 --- a/tests/test_net_builder.cpp +++ b/tests/test_net_builder.cpp @@ -1,11 +1,19 @@ -// Test of EPANET's Network Building Functions -// -// This is a test of the API functions EN_setjuncdata, EN_settankdata & EN_setpipedata -// +/* + ****************************************************************************** + Project: OWA EPANET + Version: 2.2 + Module: test_net_builder.cpp + Description: Tests EPANET toolkit api functions + Authors: see AUTHORS + Copyright: see AUTHORS + License: see LICENSE + Last Updated: 03/21/2019 + ****************************************************************************** +*/ -#define BOOST_TEST_MODULE "net_builder" +#include -#include "shared_test.hpp" +#include "test_toolkit.hpp" BOOST_AUTO_TEST_SUITE(test_net_builder) @@ -246,7 +254,7 @@ BOOST_AUTO_TEST_CASE(test_open_net1, * boost::unit_test::depends_on("test_net_bu EN_createproject(&ph); error = EN_open(ph, "net_builder.inp", DATA_PATH_RPT, DATA_PATH_OUT); BOOST_REQUIRE(error == 0); - + error = EN_getnodeindex(ph, (char *)"2", &Nindex); BOOST_REQUIRE(error == 0); @@ -351,7 +359,7 @@ BOOST_FIXTURE_TEST_CASE(test_save_net2, FixtureInitClose) BOOST_AUTO_TEST_CASE(test_reopen_net2, *boost::unit_test::depends_on("test_net_builder/test_save_net2")) { int error, index; - + double p1_2, p2_2; double q1_2, q2_2; diff --git a/tests/test_node.cpp b/tests/test_node.cpp index 21ca9df..cc79c48 100644 --- a/tests/test_node.cpp +++ b/tests/test_node.cpp @@ -1,18 +1,22 @@ -// -// test_node.cpp -// -// Date Created: February 8, 2019 -// -// Author: Michael E. Tryby -// US EPA - ORD/NRMRL -// +/* + ****************************************************************************** + Project: OWA EPANET + Version: 2.2 + Module: test_node.cpp + Description: Tests EPANET toolkit api functions + Authors: see AUTHORS + Copyright: see AUTHORS + License: see LICENSE + Last Updated: 03/21/2019 + ****************************************************************************** +*/ -#define BOOST_TEST_MODULE "node" +#include -#include "shared_test.hpp" +#include "test_toolkit.hpp" -BOOST_AUTO_TEST_SUITE (node_props_after_open) +BOOST_AUTO_TEST_SUITE (test_node) BOOST_FIXTURE_TEST_CASE(test_junc_props, FixtureOpenClose) { @@ -188,3 +192,109 @@ BOOST_AUTO_TEST_CASE(test_setid_reopen, * boost::unit_test::depends_on("setid_sa } BOOST_AUTO_TEST_SUITE_END() + + +BOOST_AUTO_TEST_SUITE(node_comments) + +BOOST_FIXTURE_TEST_CASE(test_node_comments, FixtureOpenClose) +{ + int index; + char comment[EN_MAXMSG + 1]; + + // Add comments to selected objects + error = EN_getnodeindex(ph, (char *)"11", &index); + BOOST_REQUIRE(error == 0); + error = EN_setcomment(ph, EN_NODE, index, (char *)"J11"); + BOOST_REQUIRE(error == 0); + + error = EN_getnodeindex(ph, (char *)"23", &index); + BOOST_REQUIRE(error == 0); + error = EN_setcomment(ph, EN_NODE, index, (char *)"Junc23"); + BOOST_REQUIRE(error == 0); + + // Check comments + error = EN_getnodeindex(ph, (char *)"11", &index); + BOOST_REQUIRE(error == 0); + error = EN_getcomment(ph, EN_NODE, index, comment); + BOOST_REQUIRE(error == 0); + BOOST_CHECK(check_string(comment, (char *)"J11")); + + error = EN_getnodeindex(ph, (char *)"23", &index); + BOOST_REQUIRE(error == 0); + error = EN_getcomment(ph, EN_NODE, index, comment); + BOOST_REQUIRE(error == 0); + BOOST_CHECK(check_string(comment, (char *)"Junc23")); +} + +BOOST_FIXTURE_TEST_CASE(test_replace_comment, FixtureOpenClose) +{ + int index; + char comment[EN_MAXMSG + 1]; + + // Replace short comment with longer one and vice versa + error = EN_getnodeindex(ph, (char *)"11", &index); + BOOST_REQUIRE(error == 0); + error = EN_setcomment(ph, EN_NODE, index, (char *)"Junction11"); + BOOST_REQUIRE(error == 0); + error = EN_getcomment(ph, EN_NODE, index, comment); + BOOST_REQUIRE(error == 0); + BOOST_CHECK(check_string(comment, (char *)"Junction11")); + + error = EN_setcomment(ph, EN_NODE, index, (char *)"J11"); + BOOST_REQUIRE(error == 0); + error = EN_getcomment(ph, EN_NODE, index, comment); + BOOST_REQUIRE(error == 0); + BOOST_CHECK(check_string(comment, (char *)"J11")); +} + +BOOST_FIXTURE_TEST_CASE(test_save_comment, FixtureOpenClose) +{ + int index; + + // Add comments to selected objects + error = EN_getnodeindex(ph, (char *)"11", &index); + BOOST_REQUIRE(error == 0); + error = EN_setcomment(ph, EN_NODE, index, (char *)"J11"); + BOOST_REQUIRE(error == 0); + + error = EN_getnodeindex(ph, (char *)"23", &index); + BOOST_REQUIRE(error == 0); + error = EN_setcomment(ph, EN_NODE, index, (char *)"Junc23"); + BOOST_REQUIRE(error == 0); + + error = EN_saveinpfile(ph, DATA_PATH_TMP); + BOOST_REQUIRE(error == 0); +} + +BOOST_AUTO_TEST_CASE(test_reopen_comment, * boost::unit_test::depends_on("node_comments/test_save_comment")) +{ + int error, index; + char comment[EN_MAXMSG + 1]; + + // Create & load a project + EN_Project ph = NULL; + EN_createproject(&ph); + + error = EN_open(ph, DATA_PATH_TMP, DATA_PATH_RPT, ""); + BOOST_REQUIRE(error == 0); + + // Check that comments were saved & read correctly + // Check comments + error = EN_getnodeindex(ph, (char *)"11", &index); + BOOST_REQUIRE(error == 0); + error = EN_getcomment(ph, EN_NODE, index, comment); + BOOST_REQUIRE(error == 0); + BOOST_CHECK(check_string(comment, (char *)"J11")); + + error = EN_getnodeindex(ph, (char *)"23", &index); + BOOST_REQUIRE(error == 0); + error = EN_getcomment(ph, EN_NODE, index, comment); + BOOST_REQUIRE(error == 0); + BOOST_CHECK(check_string(comment, (char *)"Junc23")); + + // Close project + EN_close(ph); + EN_deleteproject(&ph); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/test_pattern_curve_funcs.cpp b/tests/test_pattern.cpp similarity index 75% rename from tests/test_pattern_curve_funcs.cpp rename to tests/test_pattern.cpp index a3e90e9..674ea76 100644 --- a/tests/test_pattern_curve_funcs.cpp +++ b/tests/test_pattern.cpp @@ -1,9 +1,19 @@ -// Test of the EN_setpatternid, EN_setcurveid, EN_deletepattern & EN_deletecurve -// EPANET 2.2 API functions +/* + ****************************************************************************** + Project: OWA EPANET + Version: 2.2 + Module: test_pattern.cpp + Description: Tests EPANET toolkit api functions + Authors: see AUTHORS + Copyright: see AUTHORS + License: see LICENSE + Last Updated: 03/21/2019 + ****************************************************************************** +*/ -#define BOOST_TEST_MODULE "pattern_curve" +#include -#include "shared_test.hpp" +#include "test_toolkit.hpp" BOOST_AUTO_TEST_SUITE (pattern) @@ -121,4 +131,23 @@ BOOST_AUTO_TEST_CASE(add_set_pattern) EN_deleteproject(&ph); } +BOOST_FIXTURE_TEST_CASE(test_pattern_comments, FixtureOpenClose) +{ + int index; + char comment[EN_MAXMSG + 1]; + + // Set pattern comment + error = EN_getpatternindex(ph, (char *)"1", &index); + BOOST_REQUIRE(error == 0); + error = EN_setcomment(ph, EN_TIMEPAT, index, (char *)"Time Pattern 1"); + BOOST_REQUIRE(error == 0); + + // Check pattern comment + error = EN_getpatternindex(ph, (char *)"1", &index); + BOOST_REQUIRE(error == 0); + error = EN_getcomment(ph, EN_TIMEPAT, index, comment); + BOOST_REQUIRE(error == 0); + BOOST_CHECK(check_string(comment, (char *)"Time Pattern 1")); +} + BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/test_proj.cpp b/tests/test_project.cpp similarity index 90% rename from tests/test_proj.cpp rename to tests/test_project.cpp index 1fa9258..6f0b68c 100644 --- a/tests/test_proj.cpp +++ b/tests/test_project.cpp @@ -1,17 +1,21 @@ -// -// test_project.cpp -// -// Date Created: January 24, 2018 -// -// Author: Michael E. Tryby -// US EPA - ORD/NRMRL -// +/* + ****************************************************************************** + Project: OWA EPANET + Version: 2.2 + Module: test_project.cpp + Description: Tests EPANET toolkit api functions + Authors: see AUTHORS + Copyright: see AUTHORS + License: see LICENSE + Last Updated: 03/21/2019 + ****************************************************************************** +*/ -#define BOOST_TEST_MODULE "project" +#include +#include -#include "shared_test.hpp" +#include "test_toolkit.hpp" -using namespace boost; BOOST_AUTO_TEST_SUITE (test_project) @@ -61,14 +65,14 @@ BOOST_AUTO_TEST_CASE(test_save) error = EN_saveinpfile(ph_save, "test_reopen.inp"); BOOST_REQUIRE(error == 0); - BOOST_CHECK(filesystem::exists("test_reopen.inp") == true); + BOOST_CHECK(boost::filesystem::exists("test_reopen.inp") == true); error = EN_close(ph_save); BOOST_REQUIRE(error == 0); EN_deleteproject(&ph_save); } -BOOST_AUTO_TEST_CASE(test_reopen, * unit_test::depends_on("test_project/test_save")) +BOOST_AUTO_TEST_CASE(test_reopen, * boost::unit_test::depends_on("test_project/test_save")) { int error; @@ -100,7 +104,6 @@ BOOST_AUTO_TEST_CASE(test_run) BOOST_AUTO_TEST_SUITE_END() - BOOST_AUTO_TEST_SUITE(test_proj_fixture) BOOST_FIXTURE_TEST_CASE(test_title, FixtureOpenClose) diff --git a/tests/test_quality.cpp b/tests/test_quality.cpp new file mode 100644 index 0000000..973732a --- /dev/null +++ b/tests/test_quality.cpp @@ -0,0 +1,100 @@ +/* + ****************************************************************************** + Project: OWA EPANET + Version: 2.2 + Module: test_quality.cpp + Description: Tests EPANET toolkit api functions + Authors: see AUTHORS + Copyright: see AUTHORS + License: see LICENSE + Last Updated: 03/21/2019 + ****************************************************************************** +*/ + +#include + +#include "test_toolkit.hpp" + + +BOOST_AUTO_TEST_SUITE (test_quality) + +BOOST_FIXTURE_TEST_CASE(test_solveQ, FixtureOpenClose) +{ + error = EN_solveH(ph); + BOOST_REQUIRE(error == 0); + + error = EN_solveQ(ph); + BOOST_REQUIRE(error == 0); + + error = EN_report(ph); + BOOST_REQUIRE(error == 0); +} + +BOOST_FIXTURE_TEST_CASE(test_qual_step, FixtureOpenClose) +{ + int flag = 0; + long t, tstep; + + error = EN_solveH(ph); + BOOST_REQUIRE(error == 0); + + error = EN_openQ(ph); + BOOST_REQUIRE(error == 0); + + error = EN_initQ(ph, flag); + BOOST_REQUIRE(error == 0); + + do { + error = EN_runQ(ph, &t); + BOOST_REQUIRE(error == 0); + + error = EN_stepQ(ph, &tstep); + BOOST_REQUIRE(error == 0); + + } while (tstep > 0); + + error = EN_closeQ(ph); + BOOST_REQUIRE(error == 0); +} + +BOOST_FIXTURE_TEST_CASE(test_progressive_step, FixtureOpenClose) +{ + int flag = EN_NOSAVE; + long t, tstep_h, tstep_q; + + error = EN_openH(ph); + BOOST_REQUIRE(error == 0); + + error = EN_initH(ph, flag); + BOOST_REQUIRE(error == 0); + + error = EN_openQ(ph); + BOOST_REQUIRE(error == 0); + + error = EN_initQ(ph, flag); + BOOST_REQUIRE(error == 0); + + do { + error = EN_runH(ph, &t); + BOOST_REQUIRE(error == 0); + + error = EN_runQ(ph, &t); + BOOST_REQUIRE(error == 0); + + error = EN_nextH(ph, &tstep_h); + BOOST_REQUIRE(error == 0); + + error = EN_nextQ(ph, &tstep_q); + BOOST_REQUIRE(error == 0); + + } while (tstep_h > 0); + + error = EN_closeH(ph); + BOOST_REQUIRE(error == 0); + + error = EN_closeQ(ph); + BOOST_REQUIRE(error == 0); + +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/test_reent.cpp b/tests/test_reent.cpp index 3a59425..14cf154 100644 --- a/tests/test_reent.cpp +++ b/tests/test_reent.cpp @@ -1,19 +1,22 @@ /* - * test_reent.cpp - * - * Created: 8/30/2018 - * Author: Michael E. Tryby - * US EPA - ORD/NRMRL - * - * Multi-threading / reentrancy test for EPANET Toolkit API. + ****************************************************************************** + Project: OWA EPANET + Version: 2.2 + Module: test_reent.cpp + Description: Tests EPANET toolkit api functions + Authors: see AUTHORS + Copyright: see AUTHORS + License: see LICENSE + Last Updated: 03/21/2019 + ****************************************************************************** */ +#include + #include #include #include -#include - #include "epanet2_2.h" #define NUM_THREADS 2 diff --git a/tests/test_report.cpp b/tests/test_report.cpp new file mode 100644 index 0000000..f1268a3 --- /dev/null +++ b/tests/test_report.cpp @@ -0,0 +1,50 @@ +/* + ****************************************************************************** + Project: OWA EPANET + Version: 2.2 + Module: test_report.cpp + Description: Tests EPANET toolkit api functions + Authors: see AUTHORS + Copyright: see AUTHORS + License: see LICENSE + Last Updated: 03/21/2019 + ****************************************************************************** +*/ + +#include + +#include "test_toolkit.hpp" + + +BOOST_AUTO_TEST_SUITE (test_report) + +BOOST_FIXTURE_TEST_CASE(test_rprt_anlysstats, FixtureOpenClose) +{ + int i; + double array[5]; + + std::vector test; + std::vector ref = {3.0, 7.0799498320679432e-06, 1.6680242187483429e-08, + 0.0089173150106518495, 0.99999998187144024}; + + error = EN_solveH(ph); + BOOST_REQUIRE(error == 0); + + error = EN_solveQ(ph); + BOOST_REQUIRE(error == 0); + + + for (i=EN_ITERATIONS; i<=EN_MASSBALANCE; i++) { + error = EN_getstatistic(ph, i, &array[i]); + BOOST_REQUIRE(error == 0); + } + + test.assign(array, array + 5); +// 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); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/test_toolkit.cpp b/tests/test_toolkit.cpp new file mode 100644 index 0000000..f1cb75d --- /dev/null +++ b/tests/test_toolkit.cpp @@ -0,0 +1,66 @@ +/* + ****************************************************************************** + Project: OWA EPANET + Version: 2.2 + Module: test_toolkit.cpp + Description: Tests EPANET toolkit api functions + Authors: see AUTHORS + Copyright: see AUTHORS + License: see LICENSE + Last Updated: 03/21/2019 + ****************************************************************************** +*/ + +#define BOOST_TEST_MAIN +#define BOOST_TEST_MODULE toolkit + +#include + +#include + +#include "test_toolkit.hpp" + + +// 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){ + double 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_string(std::string test, std::string ref) +{ + if (ref.compare(test) == 0) + return true; + else + return false; +} diff --git a/tests/shared_test.hpp b/tests/test_toolkit.hpp similarity index 53% rename from tests/shared_test.hpp rename to tests/test_toolkit.hpp index 19fe8b7..03ac664 100644 --- a/tests/shared_test.hpp +++ b/tests/test_toolkit.hpp @@ -1,70 +1,25 @@ +/* + ****************************************************************************** + Project: OWA EPANET + Version: 2.2 + Module: test_toolkit.hpp + Description: Tests EPANET toolkit api functions + Authors: see AUTHORS + Copyright: see AUTHORS + License: see LICENSE + Last Updated: 03/21/2019 + ****************************************************************************** +*/ +#ifndef TEST_TOOLKIT_HPP +#define TEST_TOOLKIT_HPP -#ifdef _WIN32 -#define _CRTDBG_MAP_ALLOC -#include -#include -#else -#include -#endif - - -#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){ - double 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_string(std::string test, std::string ref) -{ - if (ref.compare(test) == 0) - return true; - else - return false; -} - - #define DATA_PATH_NET1 "./net1.inp" +#define DATA_PATH_TMP "./tmp.inp" #define DATA_PATH_RPT "./test.rpt" #define DATA_PATH_OUT "./test.out" @@ -123,3 +78,10 @@ struct FixtureAfterStep{ long t, tstep, tstop; EN_Project ph; }; + +boost::test_tools::predicate_result check_cdd_double(std::vector& test, + std::vector& ref, long cdd_tol); +boost::test_tools::predicate_result check_string(std::string test, std::string ref); + + +#endif //TEST_TOOLKIT_HPP diff --git a/tests/util/CMakeLists.txt b/tests/util/CMakeLists.txt new file mode 100644 index 0000000..6173a07 --- /dev/null +++ b/tests/util/CMakeLists.txt @@ -0,0 +1,17 @@ + + + +# Sets for output directory for executables and libraries +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) + + +set (test_source +./test_errormanager.cpp +../../src/util/errormanager.c +) + +add_executable(test_errormanager ${test_source}) +target_include_directories(test_errormanager PUBLIC ../../src/) +target_link_libraries(test_errormanager ${Boost_LIBRARIES}) diff --git a/tests/util/test_errormanager.cpp b/tests/util/test_errormanager.cpp new file mode 100644 index 0000000..0003ee2 --- /dev/null +++ b/tests/util/test_errormanager.cpp @@ -0,0 +1,81 @@ + + +#define BOOST_TEST_MODULE errormanager +//#define BOOST_TEST_DYN_LINK +#include + +#include "util/errormanager.h" + + +#define MESSAGE_STRING "This is unit testing!" + + +void mock_lookup(int errcode, char *errmsg, int len) +{ + char *msg = NULL; + + if (errcode == 100) { + msg = MESSAGE_STRING; + } + else { + msg = ""; + } + strncpy(errmsg, msg, len); +} + +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_errormanager) + +BOOST_AUTO_TEST_CASE (test_create_destroy) +{ + error_handle_t *error_handle = NULL; + error_handle = create_error_manager(&mock_lookup); + + delete_error_manager(error_handle); +} + + +struct Fixture{ + Fixture() { + error_message = NULL; + error_handle = create_error_manager(&mock_lookup); + } + ~Fixture() { + delete_error_manager(error_handle); + free(error_message); + } + int error; + error_handle_t *error_handle; + char *error_message; +}; + + +BOOST_FIXTURE_TEST_CASE (test_set_clear, Fixture) +{ + error = set_error(error_handle, 100); + BOOST_CHECK(error == 100); + + clear_error(error_handle); + error = check_error(error_handle, &error_message); + BOOST_CHECK(error == 0); + BOOST_CHECK(error_message == NULL); +} + +BOOST_FIXTURE_TEST_CASE(test_set_check, Fixture) +{ + error = set_error(error_handle, 100); + BOOST_CHECK(error == 100); + + error = check_error(error_handle, &error_message); + BOOST_CHECK(check_string(error_message, MESSAGE_STRING)); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/tools/epanet-output/setup.py b/tools/epanet-output/setup.py deleted file mode 100644 index 92881ce..0000000 --- a/tools/epanet-output/setup.py +++ /dev/null @@ -1,41 +0,0 @@ -# -*- coding: utf-8 -*- -# -# setup.py -# -# Created: 9/20/2017 -# Author: Michael E. Tryby -# US EPA - ORD/NRMRL -# -# Setup up script for en_outputapi python extension -# -# Requires: -# Platform C language compiler -# Python packages: numpy -# - -try: - from setuptools import setup, Extension - from setuptools.command.build_ext import build_ext -except ImportError: - from distutils.core import setup, Extension - from distutils.command.build_ext import build_ext - -setup( - name = "epanet-output", - version = "0.1.0-alpha", - ext_modules = [ - Extension("_epanet_output", - define_macros = [('epanet_output_EXPORTS', None)], - include_dirs = ['include'], - sources = ['src/epanet_output.i', 'src/epanet_output.c', 'src/errormanager.c'], - swig_opts=['-modern'], - language = 'C' - ) - ], - package_dir = {'':'src'}, - py_modules = ['epanet_output'], - - install_requires = [ - 'enum34' - ] -) diff --git a/tools/epanet-output/src/epanet_output.i b/tools/epanet-output/src/epanet_output.i deleted file mode 100644 index 8a239d6..0000000 --- a/tools/epanet-output/src/epanet_output.i +++ /dev/null @@ -1,203 +0,0 @@ -/* - * epanet_output.i - SWIG interface description file for EPANET Output API - * - * Created: 9/20/2017 - * - * Author: Michael E. Tryby - * US EPA - ORD/NRMRL - * -*/ -%module epanet_output -%{ -#include "errormanager.h" -#include "messages.h" -#include "epanet_output.h" - -#define SWIG_FILE_WITH_INIT -%} - -%include "typemaps.i" - -/* DEFINE AND TYPEDEF MUST BE INCLUDED */ -#define MAXMSG 53 - -typedef void* ENR_Handle; - - -%include "epanet_output_enums.h" - - -/* TYPEMAPS FOR OPAQUE POINTER */ -/* Used for functions that output a new opaque pointer */ -%typemap(in, numinputs=0) ENR_Handle* p_handle_out (ENR_Handle retval) -{ - /* OUTPUT in */ - retval = NULL; - $1 = &retval; -} -/* used for functions that take in an opaque pointer (or NULL) -and return a (possibly) different pointer */ -%typemap(argout) ENR_Handle* p_handle_out -{ - /* OUTPUT argout */ - %append_output(SWIG_NewPointerObj(SWIG_as_voidptr(retval$argnum), $1_descriptor, 0)); -} -/* No need for special IN typemap for opaque pointers, it works anyway */ - - -/* TYPEMAP FOR IGNORING INT ERROR CODE RETURN VALUE */ -%typemap(out) int { - $result = Py_None; - Py_INCREF($result); -} - -/* TYPEMAPS FOR INT ARGUMENT AS RETURN VALUE */ -%typemap(in, numinputs=0) int* int_out (int temp) { - $1 = &temp; -} -%typemap(argout) int* int_out { - %append_output(PyInt_FromLong(*$1)); -} - -/* TYPEMAP FOR MEMORY MANAGEMENT AND ENCODING OF STRINGS */ -%typemap(in, numinputs=0)char** string_out (char* temp), int* slen (int temp){ - $1 = &temp; -} -%typemap(argout)(char** string_out, int* slen) { - if (*$1) { - PyObject* o; - o = PyUnicode_FromStringAndSize(*$1, *$2); - - $result = SWIG_Python_AppendOutput($result, o); - free(*$1); - } -} - -/* TYPEMAPS FOR MEMORY MANAGEMNET OF FLOAT ARRAYS */ -%typemap(in, numinputs=0)float** float_out (float* temp), int* int_dim (int temp){ - $1 = &temp; -} -%typemap(argout) (float** float_out, int* int_dim) { - if (*$1) { - PyObject *o = PyList_New(*$2); - int i; - float* temp = *$1; - for(i=0; i<*$2; i++) { - PyList_SetItem(o, i, PyFloat_FromDouble((double)temp[i])); - } - $result = SWIG_Python_AppendOutput($result, o); - free(*$1); - } -} - -/* TYPEMAPS FOR MEMORY MANAGEMENT OF INT ARRAYS */ -%typemap(in, numinputs=0)int** int_out (long* temp), int* int_dim (int temp){ - $1 = &temp; -} -%typemap(argout) (int** int_out, int* int_dim) { - if (*$1) { - PyObject *o = PyList_New(*$2); - int i; - long* temp = *$1; - for(i=0; i<*$2; i++) { - PyList_SetItem(o, i, PyInt_FromLong(temp[i])); - } - $result = SWIG_Python_AppendOutput($result, o); - free(*$1); - } -} - -/* TYPEMAP FOR ENUMERATED TYPES */ -%typemap(in) EnumeratedType (int val, int ecode = 0) { - if (PyObject_HasAttrString($input,"value")) { - PyObject* o; - o = PyObject_GetAttrString($input, "value"); - ecode = SWIG_AsVal_int(o, &val); - } - else { - SWIG_exception_fail(SWIG_ArgError(ecode), "in method '" "$symname" "', argument " "$argnum"" of type '" "$ltype""'"); - } - - $1 = ($1_type)(val); -} -%apply EnumeratedType {ENR_ElementType, ENR_Units, ENR_Time, ENR_NodeAttribute, ENR_LinkAttribute} - - -/* RENAME FUNCTIONS PYTHON STYLE */ -%rename("%(undercase)s") ""; - -/* INSERTS CUSTOM EXCEPTION HANDLING IN WRAPPER */ -%exception -{ - char* err_msg; - ENR_clearError(arg1); - $function - if (ENR_checkError(arg1, &err_msg)) - { - PyErr_SetString(PyExc_Exception, err_msg); - SWIG_fail; - } -} -/* INSERT EXCEPTION HANDLING FOR THESE FUNCTIONS */ -int ENR_open(ENR_Handle p_handle, const char* path); - -int ENR_getVersion(ENR_Handle p_handle, int* int_out); -int ENR_getNetSize(ENR_Handle p_handle, int** int_out, int* int_dim); -int ENR_getUnits(ENR_Handle p_handle, ENR_Units t_enum, int* int_out); -int ENR_getTimes(ENR_Handle p_handle, ENR_Time t_enum, int* int_out); -int ENR_getElementName(ENR_Handle p_handle, ENR_ElementType t_enum, - int elementIndex, char** string_out, int* slen); -int ENR_getEnergyUsage(ENR_Handle p_handle, int pumpIndex, - int* int_out, float** float_out, int* int_dim); -int ENR_getNetReacts(ENR_Handle p_handle, float** float_out, int* int_dim); - - -int ENR_getNodeAttribute(ENR_Handle p_handle, int periodIndex, - ENR_NodeAttribute t_enum, float** float_out, int* int_dim); -int ENR_getLinkAttribute(ENR_Handle p_handle, int periodIndex, - ENR_LinkAttribute t_enum, float** float_out, int* int_dim); -%exception; - -/* NO EXCEPTION HANDLING FOR THESE FUNCTIONS */ -int ENR_init(ENR_Handle* p_handle_out); -int ENR_close(ENR_Handle* p_handle_out); -void ENR_free(void** array); - -void ENR_clearError(ENR_Handle p_handle); -int ENR_checkError(ENR_Handle p_handle, char** msg_buffer); - - -/* CODE ADDED DIRECTLY TO SWIGGED INTERFACE MODULE */ -%pythoncode%{ -import enum - -class ElementType(enum.Enum): - NODE = ENR_node - LINK = ENR_link - -class Units(enum.Enum): - FLOW_UNIT = ENR_flowUnits - PRESS_UNIT = ENR_pressUnits - -class Time(enum.Enum): - REPORT_START = ENR_reportStart - REPORT_STEP = ENR_reportStep - SIM_DURATION = ENR_simDuration - NUM_PERIODS = ENR_numPeriods - -class NodeAttribute(enum.Enum): - DEMAND = ENR_demand - HEAD = ENR_head - PRESSURE = ENR_pressure - QUALITY = ENR_quality - -class LinkAttribute(enum.Enum): - FLOW = ENR_flow - VELOCITY = ENR_velocity - HEADLOSS = ENR_headloss - AVG_QUALITY = ENR_avgQuality - STATUS = ENR_status - SETTING = ENR_setting - RX_RATE = ENR_rxRate - FRCTN_FCTR = ENR_frctnFctr -%} diff --git a/tools/epanet-output/src/errormanager.c b/tools/epanet-output/src/errormanager.c deleted file mode 100644 index 49e41db..0000000 --- a/tools/epanet-output/src/errormanager.c +++ /dev/null @@ -1,74 +0,0 @@ -//----------------------------------------------------------------------------- -// -// errormanager.c -// -// Purpose: Provides a simple interface for managing runtime error messages. -// -// Date: 08/25/2017 -// -// Author: Michael E. Tryby -// US EPA - ORD/NRMRL -//----------------------------------------------------------------------------- -#include -#include -#include "errormanager.h" - -error_handle_t* new_errormanager(void (*p_error_message)(int, char*, int)) -// -// Purpose: Constructs a new error handle. -// -{ - error_handle_t* error_handle; - error_handle = (error_handle_t*)calloc(1, sizeof(error_handle_t)); - - error_handle->p_msg_lookup = p_error_message; - - return error_handle; -} - -void dst_errormanager(error_handle_t* error_handle) -// -// Purpose: Destroys the error handle. -// -{ - free(error_handle); -} - -int set_error(error_handle_t* error_handle, int errorcode) -// -// Purpose: Sets an error code in the handle. -// -{ - // If the error code is 0 no action is taken and 0 is returned. - // This is a feature not a bug. - if (errorcode) - error_handle->error_status = errorcode; - - return errorcode; -} - -char* check_error(error_handle_t* error_handle) -// -// Purpose: Returns the error message or NULL. -// -// Note: Caller must free memory allocated by check_error -// -{ - char* temp = NULL; - - if (error_handle->error_status != 0) { - temp = (char*) calloc(ERR_MAXMSG, sizeof(char)); - - if (temp) - error_handle->p_msg_lookup(error_handle->error_status, temp, ERR_MAXMSG); - } - return temp; -} - -void clear_error(error_handle_t* error_handle) -// -// Purpose: Clears the error from the handle. -// -{ - error_handle->error_status = 0; -} diff --git a/tools/epanet-output/src/errormanager.h b/tools/epanet-output/src/errormanager.h deleted file mode 100644 index d62b6a3..0000000 --- a/tools/epanet-output/src/errormanager.h +++ /dev/null @@ -1,27 +0,0 @@ -/* - * errormanager.h - * - * Created on: Aug 25, 2017 - * - * Author: Michael E. Tryby - * US EPA - ORD/NRMRL - */ - -#ifndef ERRORMANAGER_H_ -#define ERRORMANAGER_H_ - -#define ERR_MAXMSG 256 - -typedef struct error_s { - int error_status; - void (*p_msg_lookup)(int, char*, int); -} error_handle_t; - -error_handle_t* new_errormanager(void (*p_error_message)(int, char*, int)); -void dst_errormanager(error_handle_t* error_handle); - -int set_error(error_handle_t* error_handle, int errorcode); -char* check_error(error_handle_t* error_handle); -void clear_error(error_handle_t* error_handle); - -#endif /* ERRORMANAGER_H_ */ diff --git a/tools/epanet-output/test/data/__init__.py b/tools/epanet-output/test/data/__init__.py deleted file mode 100644 index ca2e575..0000000 --- a/tools/epanet-output/test/data/__init__.py +++ /dev/null @@ -1,14 +0,0 @@ -# -*- coding: utf-8 -*- - -# __init__.py -# -# Created: 11/13/2017 -# Author: Michael E. Tryby -# US EPA - ORD/NRMRL -# - -import os - -DATA_PATH = os.path.abspath(os.path.dirname(__file__)) - -OUTPUT_FILE_EXAMPLE1 = os.path.join(DATA_PATH, 'net1.out') diff --git a/tools/epanet-output/test/test_epanet_output.cpp b/tools/epanet-output/test/test_epanet_output.cpp deleted file mode 100644 index ed4f79d..0000000 --- a/tools/epanet-output/test/test_epanet_output.cpp +++ /dev/null @@ -1,251 +0,0 @@ -/* - * test_epanet_output.cpp - * - * Created: 8/4/2017 - * Author: Michael E. Tryby - * US EPA - ORD/NRMRL - * - * Unit testing for EPANET Output API using google test. -*/ - -#include -#include -#include -#include "gtest/gtest.h" - -#include "../src/epanet_output.h" - -#define PROJECT_HOME "C:/Users/mtryby/Workspace/GitRepo/michaeltryby/epanet/" -#define DATA_PATH "tools/epanet-output/test/data/net1.out" - - -namespace { - -TEST(ENR_init, InitTest) { - ENR_Handle p_handle; - - int error = ENR_init(&p_handle); - ASSERT_EQ(0, error); - ASSERT_TRUE(p_handle != NULL); -} - -TEST(ENR_open, OpenTest) { - std::string path = std::string(PROJECT_HOME) + std::string(DATA_PATH); - ENR_Handle p_handle; - ENR_init(&p_handle); - - int error = ENR_open(p_handle, path.c_str()); - ASSERT_EQ(0, error); - ENR_close(&p_handle); -} - -TEST(ENR_close, CloseTest) { - ENR_Handle p_handle; - int error = ENR_init(&p_handle); - - error = ENR_close(&p_handle); - ASSERT_EQ(-1, error); - ASSERT_TRUE(p_handle != NULL); -} - - - -class OutputapiTest : public testing::Test { -protected: - // SetUp for OutputapiTest fixture - virtual void SetUp() { - std::string path = std::string(PROJECT_HOME) + std::string(DATA_PATH); - - error = ENR_init(&p_handle); - ENR_clearError(p_handle); - error = ENR_open(p_handle, path.c_str()); - } - - // TearDown for OutputapiTest fixture - virtual void TearDown() { - ENR_free((void**)&array); - error = ENR_close(&p_handle); - } - - int error = 0; - ENR_Handle p_handle = NULL; - - float* array = NULL; - int array_dim = 0; -}; - -TEST_F(OutputapiTest, getNetSizeTest) { - int* i_array = NULL; - // nodes, tanks, links, pumps, valves - int ref_array[5] = {11,2,13,1,0}; - - error = ENR_getNetSize(p_handle, &i_array, &array_dim); - ASSERT_EQ(0, error); - - for (int i = 0; i < array_dim; i++) - EXPECT_EQ(ref_array[i], i_array[i]); - - ENR_free((void**)&i_array); -} - -TEST_F(OutputapiTest, getElementName) { - char* name = new char[MAXID]; - int length, index = 1; - - error = ENR_getElementName(p_handle, ENR_node, index, &name, &length); - ASSERT_EQ(0, error); - - EXPECT_STREQ("10", name); - - delete(name); -} - -TEST_F(OutputapiTest, getNodeAttributeTest) { - float ref_array[11] = { 1.0, - 0.44407997, - 0.43766347, - 0.42827705, - 0.41342604, - 0.42804748, - 0.44152543, - 0.40502965, - 0.38635802, - 1.0, - 0.96745253 }; - - error = ENR_getNodeAttribute(p_handle, 1, ENR_quality, &array, &array_dim); - ASSERT_EQ(0, error); - - for (int i = 0; i < array_dim; i++) - EXPECT_FLOAT_EQ(ref_array[i], array[i]); -} - -TEST_F(OutputapiTest, getLinkAttributeTest) { - float ref_array[13] = { 1848.5812, - 1220.4274, - 130.11162, - 187.6893, - 119.8884, - 40.464489, - -748.58112, - 478.15378, - 191.73459, - 30.111609, - 140.46449, - 59.535515, - 1848.5812}; - - error = ENR_getLinkAttribute(p_handle, 1, ENR_flow, &array ,&array_dim); - ASSERT_EQ(0, error); - - for (int i = 0; i < array_dim; i++) - EXPECT_FLOAT_EQ(ref_array[i], array[i]); -} - -TEST_F(OutputapiTest, getNodeResultTest) { - float ref_array[4] = {0.041142918, - 150.0, - 987.98358, - 120.45029}; - - error = ENR_getNodeResult(p_handle, 1, 2, &array, &array_dim); - ASSERT_EQ(0, error); - - for (int i = 0; i < array_dim; i++) - EXPECT_FLOAT_EQ(ref_array[i], array[i]); -} - -TEST_F(OutputapiTest, getLinkResultTest) { - float ref_array[8] = {0.58586824, - 1892.2433, - 0.0, - -200.71875, - 1.0, - 3.0, - 1.0, - 0.0}; - - error = ENR_getLinkResult(p_handle, 24, 13, &array, &array_dim); - ASSERT_EQ(0, error); - - for (int i = 0; i < array_dim; i++) - EXPECT_FLOAT_EQ(ref_array[i], array[i]); -} - -TEST_F(OutputapiTest, getNodeSeriesTest){ - float ref_array[10] = {119.25731, - 120.45029, - 121.19854, - 122.00622, - 122.37414, - 122.8122, - 122.82034, - 122.90379, - 123.40434, - 123.81807}; - - error = ENR_getNodeSeries(p_handle, 2, ENR_pressure, 0, 10, &array, &array_dim); - ASSERT_EQ(0, error); - - for (int i = 0; i < array_dim; i++) - EXPECT_FLOAT_EQ(ref_array[i], array[i]); -} - -TEST_F(OutputapiTest, getLinkSeriesTest) { - float ref_array[10] = {1234.2072, - 1220.4274, - 1164.4, - 1154.8175, - 1100.0635, - 1094.759, - 1041.7854, - 1040.7617, - 1087.556, - 1082.5011}; - - error = ENR_getLinkSeries(p_handle, 2, ENR_flow, 0, 10, &array, &array_dim); - ASSERT_EQ(0, error); - - for (int i = 0; i < array_dim; i++) - EXPECT_FLOAT_EQ(ref_array[i], array[i]); -} - -TEST_F(OutputapiTest, getNetReactsTest) { - float ref_array[4] = {18806.59, - 85424.438, - 115174.05, - 238972.66}; - - error = ENR_getNetReacts(p_handle, &array, &array_dim); - ASSERT_EQ(0, error); - - for (int i = 0; i < array_dim; i++) - EXPECT_FLOAT_EQ(ref_array[i], array[i]); -} - -TEST_F(OutputapiTest, getEnergyUsageTest) { - float ref_array[6] = {57.712959, - 75.0, - 880.41583, - 96.254318, - 96.707115, - 0.0}; - - int linkIdx; - - error = ENR_getEnergyUsage(p_handle, 1, &linkIdx, &array, &array_dim); - ASSERT_EQ(0, error); - - for (int i = 0; i < array_dim; i++) - EXPECT_FLOAT_EQ(ref_array[i], array[i]); -} - -} - -GTEST_API_ int main(int argc, char **argv) { - - printf("Running main() from gtest_main.cc\n"); - testing::InitGoogleTest(&argc, argv); - - return RUN_ALL_TESTS(); -}