-
Notifications
You must be signed in to change notification settings - Fork 25
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
Changes from all commits
5a29880
e5152fa
1fdc5b4
47244fa
ad0f5b7
9a88758
d2e78d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) |
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; | ||
} | ||
|
||
// ================== | ||
// 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this behaviour is even more problematic: 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() |
There was a problem hiding this comment.
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.