From d645ab8051808e53751ceb801b70925ec11d784b Mon Sep 17 00:00:00 2001 From: payetvin <113102157+payetvin@users.noreply.github.com> Date: Tue, 24 Oct 2023 13:25:10 +0200 Subject: [PATCH] Unit tests for timeseries (#1713) --- .../series/include/antares/series/series.h | 2 +- .../antares/study/parts/series/series.cpp | 6 +- .../src/libs/antares/study/CMakeLists.txt | 1 + .../libs/antares/study/series/CMakeLists.txt | 20 +++ .../antares/study/series/timeseries-tests.cpp | 153 ++++++++++++++++++ 5 files changed, 179 insertions(+), 3 deletions(-) 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/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 f165e9f3de..fd90dbf6d1 100644 --- a/src/libs/antares/study/parts/series/series.cpp +++ b/src/libs/antares/study/parts/series/series.cpp @@ -90,9 +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) { - return timeSeries[year]; + if (timeSeries.width <= index) + return nullptr; + return timeSeries[index]; } void TimeSeries::reset() 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..0afbfe34ef --- /dev/null +++ b/src/tests/src/libs/antares/study/series/CMakeLists.txt @@ -0,0 +1,20 @@ +# ==================================== +# Tests on TimeSeries class +# ==================================== +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 +) + +# 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..aac91b6400 --- /dev/null +++ b/src/tests/src/libs/antares/study/series/timeseries-tests.cpp @@ -0,0 +1,153 @@ +#define BOOST_TEST_MODULE "test time series" +#define BOOST_TEST_DYN_LINK + +#define WIN32_LEAN_AND_MEAN + +#include + +#include + +using namespace Antares::Data; + +// ================= +// The fixture +// ================= +struct Fixture +{ + 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 +// ================== + +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_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_CHECK_EQUAL(ts.getCoefficient(1, 0), 12.5); +} + +BOOST_AUTO_TEST_SUITE_END()