From edf4a09ebeb2fa9b6ed0417de229151acd9caf41 Mon Sep 17 00:00:00 2001 From: Elad Salomons Date: Wed, 29 Aug 2018 15:14:49 +0300 Subject: [PATCH 1/2] Fix EN_addpattern Closes #231. Also adds unit test for the function. --- src/epanet.c | 4 ++-- tests/test_toolkit.cpp | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/epanet.c b/src/epanet.c index 95d99c9..8cb5449 100644 --- a/src/epanet.c +++ b/src/epanet.c @@ -3103,7 +3103,7 @@ int DLLEXPORT EN_addpattern(EN_ProjectHandle ph, char *id) { if (!p->Openflag) return set_error(p->error_handle, 102); - if (ENgetpatternindex(id, &i) == 0) + if (EN_getpatternindex(ph, id, &i) == 0) return set_error(p->error_handle, 215); /* Check that id name is not too long */ @@ -3156,7 +3156,7 @@ int DLLEXPORT EN_addpattern(EN_ProjectHandle ph, char *id) { for (i = 0; i <= Npats; i++) free(Pattern[i].F); free(Pattern); - Pattern = tmpPat; + net->Pattern = tmpPat; net->Npats = n; par->MaxPats = n; return set_error(p->error_handle, 0); diff --git a/tests/test_toolkit.cpp b/tests/test_toolkit.cpp index 6ca6322..3c7aa5b 100644 --- a/tests/test_toolkit.cpp +++ b/tests/test_toolkit.cpp @@ -204,4 +204,26 @@ BOOST_FIXTURE_TEST_CASE(test_progressive_stepping, Fixture) } +BOOST_FIXTURE_TEST_CASE(test_addpattern, Fixture) +{ + int pat_index, n_patterns_1, n_patterns_2; + + // get the number of current patterns + error = EN_getcount(ph, EN_PATCOUNT, &n_patterns_1); + BOOST_REQUIRE(error == 0); + + // add a new pattern + error = EN_addpattern(ph, 'new_pattern'); + BOOST_REQUIRE(error == 0); + + // 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); + + // gwt the new patterns index, should be as the number of patterns + error = EN_getpatternindex(ph, 'new_pattern', &pat_index); + BOOST_REQUIRE(pat_index == n_patterns_2); +} + BOOST_AUTO_TEST_SUITE_END() From f3d680dc54d494e107d848b645d7d7bc5c52dfd1 Mon Sep 17 00:00:00 2001 From: Elad Salomons Date: Wed, 29 Aug 2018 18:12:25 +0300 Subject: [PATCH 2/2] Fix test_addpattern test Thanks @mariosmsk Co-Authored-By: Marios S. Kyriakou --- tests/test_toolkit.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_toolkit.cpp b/tests/test_toolkit.cpp index 3c7aa5b..433614e 100644 --- a/tests/test_toolkit.cpp +++ b/tests/test_toolkit.cpp @@ -207,13 +207,14 @@ BOOST_FIXTURE_TEST_CASE(test_progressive_stepping, Fixture) BOOST_FIXTURE_TEST_CASE(test_addpattern, Fixture) { int pat_index, n_patterns_1, n_patterns_2; + char newpat[] = "new_pattern"; // get the number of current patterns error = EN_getcount(ph, EN_PATCOUNT, &n_patterns_1); BOOST_REQUIRE(error == 0); // add a new pattern - error = EN_addpattern(ph, 'new_pattern'); + error = EN_addpattern(ph, newpat); BOOST_REQUIRE(error == 0); // get the new patterns count, shoul dbe the old one + 1 @@ -222,7 +223,7 @@ BOOST_FIXTURE_TEST_CASE(test_addpattern, Fixture) BOOST_REQUIRE(n_patterns_1 + 1 == n_patterns_2); // gwt the new patterns index, should be as the number of patterns - error = EN_getpatternindex(ph, 'new_pattern', &pat_index); + error = EN_getpatternindex(ph, newpat, &pat_index); BOOST_REQUIRE(pat_index == n_patterns_2); }