From 5a29880837f6d7dce2e2db40c13a1f4efa3e9aed Mon Sep 17 00:00:00 2001 From: Vincent Payet Date: Fri, 20 Oct 2023 13:00:44 +0200 Subject: [PATCH 1/7] [TESTS] Add skeleton for timeseries unit tests --- .../src/libs/antares/study/CMakeLists.txt | 1 + .../libs/antares/study/series/CMakeLists.txt | 25 ++++++++++++ .../antares/study/series/timeseries-tests.cpp | 39 +++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 src/tests/src/libs/antares/study/series/CMakeLists.txt create mode 100644 src/tests/src/libs/antares/study/series/timeseries-tests.cpp diff --git a/src/tests/src/libs/antares/study/CMakeLists.txt b/src/tests/src/libs/antares/study/CMakeLists.txt index ab0bc3f5aa..d735c1218a 100644 --- a/src/tests/src/libs/antares/study/CMakeLists.txt +++ b/src/tests/src/libs/antares/study/CMakeLists.txt @@ -6,6 +6,7 @@ add_subdirectory(output-folder) add_subdirectory(short-term-storage-input) add_subdirectory(thermal-price-definition) add_subdirectory(constraint) +add_subdirectory(series) add_executable(test-study) target_sources(test-study PRIVATE test_study.cpp) diff --git a/src/tests/src/libs/antares/study/series/CMakeLists.txt b/src/tests/src/libs/antares/study/series/CMakeLists.txt new file mode 100644 index 0000000000..78e2c42040 --- /dev/null +++ b/src/tests/src/libs/antares/study/series/CMakeLists.txt @@ -0,0 +1,25 @@ +# ==================================== +# Tests on thermal price definition +# ==================================== +set(SRC_TIMESERIES_TESTS + timeseries-tests.cpp +) +add_executable(timeseries-tests ${SRC_TIMESERIES_TESTS}) + +target_link_libraries(timeseries-tests + PRIVATE + Boost::unit_test_framework + Antares::series +) +# Linux +if(UNIX AND NOT APPLE) +target_link_libraries(timeseries-tests PRIVATE stdc++fs) +endif() + + +# Storing timeseries-tests under the folder Unit-tests in the IDE +set_target_properties(timeseries-tests PROPERTIES FOLDER Unit-tests/timeseries-tests) + +add_test(NAME timeseries-tests COMMAND timeseries-tests) + +set_property(TEST timeseries-tests PROPERTY LABELS unit) diff --git a/src/tests/src/libs/antares/study/series/timeseries-tests.cpp b/src/tests/src/libs/antares/study/series/timeseries-tests.cpp new file mode 100644 index 0000000000..6b7f8e2176 --- /dev/null +++ b/src/tests/src/libs/antares/study/series/timeseries-tests.cpp @@ -0,0 +1,39 @@ +#define BOOST_TEST_MODULE "test time series" +#define BOOST_TEST_DYN_LINK + +#define WIN32_LEAN_AND_MEAN + +#include +#include +#include + +#include + +using namespace Antares::Data; +using std::filesystem::temp_directory_path; + +// ================= +// The fixture +// ================= +struct FixtureFull +{ + FixtureFull(const FixtureFull& f) = delete; + FixtureFull(const FixtureFull&& f) = delete; + FixtureFull& operator=(const FixtureFull& f) = delete; + FixtureFull& operator=(const FixtureFull&& f) = delete; + FixtureFull() + { + } + + std::string folder; + +}; + +// ================== +// Tests section +// ================== + +// Here, we need the "lightweight fixture" +BOOST_AUTO_TEST_SUITE(timeseries_tests) + +BOOST_AUTO_TEST_SUITE_END() From e5152fa0f0e2ce0f6428c88594ef821508bf515d Mon Sep 17 00:00:00 2001 From: Vincent Payet Date: Mon, 23 Oct 2023 10:55:37 +0200 Subject: [PATCH 2/7] [TESTS] Add unit tests for class series --- .../antares/study/parts/series/series.cpp | 2 + .../antares/study/series/timeseries-tests.cpp | 119 ++++++++++++++++-- 2 files changed, 111 insertions(+), 10 deletions(-) diff --git a/src/libs/antares/study/parts/series/series.cpp b/src/libs/antares/study/parts/series/series.cpp index f165e9f3de..9e3474684d 100644 --- a/src/libs/antares/study/parts/series/series.cpp +++ b/src/libs/antares/study/parts/series/series.cpp @@ -92,6 +92,8 @@ uint32_t TimeSeries::getSeriesIndex(uint32_t year) const double* TimeSeries::operator[](uint32_t year) { + if (timeSeries.width < year) + return nullptr; return timeSeries[year]; } diff --git a/src/tests/src/libs/antares/study/series/timeseries-tests.cpp b/src/tests/src/libs/antares/study/series/timeseries-tests.cpp index 6b7f8e2176..a12502e079 100644 --- a/src/tests/src/libs/antares/study/series/timeseries-tests.cpp +++ b/src/tests/src/libs/antares/study/series/timeseries-tests.cpp @@ -4,31 +4,55 @@ #define WIN32_LEAN_AND_MEAN #include -#include -#include #include using namespace Antares::Data; -using std::filesystem::temp_directory_path; // ================= // The fixture // ================= -struct FixtureFull +struct Fixture { - FixtureFull(const FixtureFull& f) = delete; - FixtureFull(const FixtureFull&& f) = delete; - FixtureFull& operator=(const FixtureFull& f) = delete; - FixtureFull& operator=(const FixtureFull&& f) = delete; - FixtureFull() + Fixture(const Fixture& f) = delete; + Fixture(const Fixture&& f) = delete; + Fixture& operator=(const Fixture& f) = delete; + Fixture& operator=(const Fixture&& f) = delete; + Fixture() : ts(tsnum) { + ts.resize(1, HOURS_PER_YEAR); + tsnum.resize(1, 1); } - + TimeSeries ts; + TimeSeries::TSNumbers tsnum; std::string folder; + void fillColumn(unsigned int idx); + void fillColumnReverse(unsigned int idx); + + void fillTsnum(); + }; +void Fixture::fillColumn(unsigned int idx) +{ + for (unsigned int i = 0; i < ts.timeSeries.height; i++) + ts.timeSeries[idx][i] = i; +} + +void Fixture::fillColumnReverse(unsigned int idx) +{ + for (unsigned int i = 0; i < ts.timeSeries.height; i++) + ts.timeSeries[idx][i] = HOURS_PER_YEAR - i; +} + +void Fixture::fillTsnum() +{ + tsnum.resize(1, ts.timeSeries.width); + for (unsigned int i = 0; i < ts.timeSeries.width; i++) + tsnum[0][i] = i; +} + // ================== // Tests section // ================== @@ -36,4 +60,79 @@ struct FixtureFull // Here, we need the "lightweight fixture" BOOST_AUTO_TEST_SUITE(timeseries_tests) +BOOST_FIXTURE_TEST_CASE(getSeriesIndex, Fixture) +{ + tsnum.resize(1, 10); + for (unsigned int i = 0; i < 10; i++) + tsnum[0][i] = i; + + //timeSeries.width == 1 so returns 0 + BOOST_CHECK_EQUAL(ts.getSeriesIndex(5), 0); + + ts.resize(2, HOURS_PER_YEAR); + for (unsigned int i = 0; i < 10; i++) + BOOST_CHECK_EQUAL(ts.getSeriesIndex(i), i); +} + +BOOST_FIXTURE_TEST_CASE(getCoefficientWidth1, Fixture) +{ + fillColumn(0); + BOOST_CHECK_EQUAL(ts.getCoefficient(0, 12), 12); + BOOST_CHECK_EQUAL(ts.getCoefficient(0, 8750), 8750); +} + +BOOST_FIXTURE_TEST_CASE(getCoefficientWidth0, Fixture) +{ + ts.resize(0, HOURS_PER_YEAR); + BOOST_CHECK_EQUAL(ts.getCoefficient(0, 12), 0); + BOOST_CHECK_EQUAL(ts.getCoefficient(0, 8750), 0); +} + +BOOST_FIXTURE_TEST_CASE(getCoefficientNotInitialized, Fixture) +{ + ts.resize(4, HOURS_PER_YEAR); + fillTsnum(); + BOOST_CHECK_EQUAL(ts.getCoefficient(3, 12), 0); + BOOST_CHECK_EQUAL(ts.getCoefficient(3, 8750), 0); +} + +BOOST_FIXTURE_TEST_CASE(getCoefficientWidthMoreThan1, Fixture) +{ + ts.resize(5, HOURS_PER_YEAR); + fillTsnum(); + fillColumn(3); + + BOOST_CHECK_EQUAL(ts.getCoefficient(3, 12), 12); + BOOST_CHECK_EQUAL(ts.getCoefficient(3, 4858), 4858); + + fillColumnReverse(2); + BOOST_CHECK_EQUAL(ts.getCoefficient(2, 20), 8740); + BOOST_CHECK_EQUAL(ts.getCoefficient(2, 4567), HOURS_PER_YEAR - 4567); +} + +BOOST_FIXTURE_TEST_CASE(getColumn, Fixture) +{ + ts.resize(0, HOURS_PER_YEAR); + auto col = ts.getColumn(3); //emptyColumn + BOOST_CHECK_EQUAL(col[38], 0); + BOOST_CHECK_EQUAL(col[7463], 0); + + ts.resize(4, HOURS_PER_YEAR); + fillTsnum(); + fillColumn(2); + + col = ts.getColumn(2); + BOOST_CHECK_EQUAL(col[38], 38); + BOOST_CHECK_EQUAL(col[7463], 7463); +} + +BOOST_FIXTURE_TEST_CASE(operatorArray, Fixture) +{ + ts.resize(4, HOURS_PER_YEAR); + fillTsnum(); + auto* col = ts[2]; + col[27] = 12; + BOOST_CHECK_EQUAL(ts.getCoefficient(2, 27), 12); +} + BOOST_AUTO_TEST_SUITE_END() From 1fdc5b48db4be28fd0412b5956739cba444493c2 Mon Sep 17 00:00:00 2001 From: Vincent Payet Date: Mon, 23 Oct 2023 11:00:30 +0200 Subject: [PATCH 3/7] [TESTS] Clean Cmakelists --- src/tests/src/libs/antares/study/series/CMakeLists.txt | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/tests/src/libs/antares/study/series/CMakeLists.txt b/src/tests/src/libs/antares/study/series/CMakeLists.txt index 78e2c42040..27f8e4ec05 100644 --- a/src/tests/src/libs/antares/study/series/CMakeLists.txt +++ b/src/tests/src/libs/antares/study/series/CMakeLists.txt @@ -11,11 +11,6 @@ target_link_libraries(timeseries-tests Boost::unit_test_framework Antares::series ) -# Linux -if(UNIX AND NOT APPLE) -target_link_libraries(timeseries-tests PRIVATE stdc++fs) -endif() - # Storing timeseries-tests under the folder Unit-tests in the IDE set_target_properties(timeseries-tests PROPERTIES FOLDER Unit-tests/timeseries-tests) From 47244fa7f863c9c71e0a80909de1a36d63feb7c9 Mon Sep 17 00:00:00 2001 From: Vincent Payet Date: Mon, 23 Oct 2023 12:54:13 +0200 Subject: [PATCH 4/7] [FIX] Condition for operator[] --- src/libs/antares/study/parts/series/series.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/antares/study/parts/series/series.cpp b/src/libs/antares/study/parts/series/series.cpp index 9e3474684d..ce7ab00a65 100644 --- a/src/libs/antares/study/parts/series/series.cpp +++ b/src/libs/antares/study/parts/series/series.cpp @@ -92,7 +92,7 @@ uint32_t TimeSeries::getSeriesIndex(uint32_t year) const double* TimeSeries::operator[](uint32_t year) { - if (timeSeries.width < year) + if (timeSeries.width <= year) return nullptr; return timeSeries[year]; } From ad0f5b71292571184b50b1a3fbba92afd184e282 Mon Sep 17 00:00:00 2001 From: Vincent Payet Date: Mon, 23 Oct 2023 13:37:48 +0200 Subject: [PATCH 5/7] [FIX] Renamed year with index for operator[] --- .../study/parts/series/include/antares/series/series.h | 2 +- src/libs/antares/study/parts/series/series.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libs/antares/study/parts/series/include/antares/series/series.h b/src/libs/antares/study/parts/series/include/antares/series/series.h index 7dd51e51a8..5dbfbd2a9d 100644 --- a/src/libs/antares/study/parts/series/include/antares/series/series.h +++ b/src/libs/antares/study/parts/series/include/antares/series/series.h @@ -68,7 +68,7 @@ class TimeSeries const double* getColumn(uint32_t year) const; uint32_t getSeriesIndex(uint32_t year) const; - double* operator[](uint32_t year); + double* operator[](uint32_t index); void reset(); void unloadFromMemory() const; diff --git a/src/libs/antares/study/parts/series/series.cpp b/src/libs/antares/study/parts/series/series.cpp index ce7ab00a65..fd90dbf6d1 100644 --- a/src/libs/antares/study/parts/series/series.cpp +++ b/src/libs/antares/study/parts/series/series.cpp @@ -90,11 +90,11 @@ uint32_t TimeSeries::getSeriesIndex(uint32_t year) const return timeseriesNumbers[0][year]; } -double* TimeSeries::operator[](uint32_t year) +double* TimeSeries::operator[](uint32_t index) { - if (timeSeries.width <= year) + if (timeSeries.width <= index) return nullptr; - return timeSeries[year]; + return timeSeries[index]; } void TimeSeries::reset() From 9a88758378c118fe7038f511c5ba4594fd3b9efe Mon Sep 17 00:00:00 2001 From: Vincent Payet Date: Mon, 23 Oct 2023 14:36:13 +0200 Subject: [PATCH 6/7] [DEV] test with deifferent tsnumbers --- .../libs/antares/study/series/CMakeLists.txt | 2 +- .../antares/study/series/timeseries-tests.cpp | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/tests/src/libs/antares/study/series/CMakeLists.txt b/src/tests/src/libs/antares/study/series/CMakeLists.txt index 27f8e4ec05..0afbfe34ef 100644 --- a/src/tests/src/libs/antares/study/series/CMakeLists.txt +++ b/src/tests/src/libs/antares/study/series/CMakeLists.txt @@ -1,5 +1,5 @@ # ==================================== -# Tests on thermal price definition +# Tests on TimeSeries class # ==================================== set(SRC_TIMESERIES_TESTS timeseries-tests.cpp diff --git a/src/tests/src/libs/antares/study/series/timeseries-tests.cpp b/src/tests/src/libs/antares/study/series/timeseries-tests.cpp index a12502e079..9652c6a27b 100644 --- a/src/tests/src/libs/antares/study/series/timeseries-tests.cpp +++ b/src/tests/src/libs/antares/study/series/timeseries-tests.cpp @@ -100,8 +100,8 @@ BOOST_FIXTURE_TEST_CASE(getCoefficientWidthMoreThan1, Fixture) { ts.resize(5, HOURS_PER_YEAR); fillTsnum(); - fillColumn(3); + fillColumn(3); BOOST_CHECK_EQUAL(ts.getCoefficient(3, 12), 12); BOOST_CHECK_EQUAL(ts.getCoefficient(3, 4858), 4858); @@ -135,4 +135,20 @@ BOOST_FIXTURE_TEST_CASE(operatorArray, Fixture) BOOST_CHECK_EQUAL(ts.getCoefficient(2, 27), 12); } +BOOST_FIXTURE_TEST_CASE(getCoefficientSpecificData, Fixture) +{ + ts.resize(2, 2); + fillTsnum(); + tsnum[0][0] = 1; + tsnum[0][1] = 0; + ts.timeSeries[0][0] = 12.5; + ts.timeSeries[0][1] = 74.74; + ts.timeSeries[1][0] = -57; + ts.timeSeries[1][1] = 29; + + BOOST_CHECK_EQUAL(ts.getCoefficient(1, 1), 74.74); + BOOST_CHECK_EQUAL(ts.getCoefficient(0, 0), -57); + +} + BOOST_AUTO_TEST_SUITE_END() From d2e78d2cd727ad2f648dc1c66471ee72e3298cfb Mon Sep 17 00:00:00 2001 From: Vincent Payet Date: Mon, 23 Oct 2023 14:48:34 +0200 Subject: [PATCH 7/7] [FIX] remove comment --- src/tests/src/libs/antares/study/series/timeseries-tests.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/tests/src/libs/antares/study/series/timeseries-tests.cpp b/src/tests/src/libs/antares/study/series/timeseries-tests.cpp index 9652c6a27b..aac91b6400 100644 --- a/src/tests/src/libs/antares/study/series/timeseries-tests.cpp +++ b/src/tests/src/libs/antares/study/series/timeseries-tests.cpp @@ -57,7 +57,6 @@ void Fixture::fillTsnum() // Tests section // ================== -// Here, we need the "lightweight fixture" BOOST_AUTO_TEST_SUITE(timeseries_tests) BOOST_FIXTURE_TEST_CASE(getSeriesIndex, Fixture) @@ -148,7 +147,7 @@ BOOST_FIXTURE_TEST_CASE(getCoefficientSpecificData, Fixture) BOOST_CHECK_EQUAL(ts.getCoefficient(1, 1), 74.74); BOOST_CHECK_EQUAL(ts.getCoefficient(0, 0), -57); - + BOOST_CHECK_EQUAL(ts.getCoefficient(1, 0), 12.5); } BOOST_AUTO_TEST_SUITE_END()