Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests for timeseries #1713

Merged
merged 7 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions src/libs/antares/study/parts/series/series.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions src/tests/src/libs/antares/study/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions src/tests/src/libs/antares/study/series/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
153 changes: 153 additions & 0 deletions src/tests/src/libs/antares/study/series/timeseries-tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
#define BOOST_TEST_MODULE "test time series"
#define BOOST_TEST_DYN_LINK

#define WIN32_LEAN_AND_MEAN

#include <boost/test/unit_test.hpp>

#include <include/antares/series/series.h>

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is bad test data:
the index and what it points to are the same, so we don't really test that the indirection works well.
Some explicit hard coded values would be better like {3,0,2,1} or whatever.

}

// ==================
// 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised by this behaviour! Is it not a situation where we should throw ?

We have an index for series, but because we only have one timeseries, we don't take that index into account. it looks like an inconsistency in input data.

Let's not change it in this work, but I note it for later.


ts.resize(2, HOURS_PER_YEAR);
for (unsigned int i = 0; i < 10; i++)
BOOST_CHECK_EQUAL(ts.getSeriesIndex(i), i);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this behaviour is even more problematic:
our timeseries has only size 2, but we get an index greater than 2.

For me we should throw an exception in that case, it's too fragile.

}

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me this should throw too. However we need to make sure it's not a regression, I'll look into it too.

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);
sylvlecl marked this conversation as resolved.
Show resolved Hide resolved
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);
sylvlecl marked this conversation as resolved.
Show resolved Hide resolved
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()