-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathtest_time_series.cpp
More file actions
495 lines (431 loc) · 15.8 KB
/
test_time_series.cpp
File metadata and controls
495 lines (431 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
* Copyright (C) 2020-2026 MEmilio
*
* Authors: Daniel Abele
*
* Contact: Martin J. Kuehn <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "memilio/utils/time_series.h"
#include "memilio/utils/stl_util.h"
#include "matchers.h"
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "temp_file_register.h"
template <class T>
using TestTimeSeries = ::testing::Test;
using FloatingPointTypes = ::testing::Types<float, double>;
TYPED_TEST_SUITE(TestTimeSeries, FloatingPointTypes);
TYPED_TEST(TestTimeSeries, createEmpty)
{
mio::TimeSeries<TypeParam> ts(10);
ASSERT_EQ(ts.get_num_elements(), 10);
ASSERT_EQ(ts.get_num_rows(), 11);
ASSERT_EQ(ts.get_num_time_points(), 0);
ASSERT_EQ(ts.get_capacity(), 0);
}
TYPED_TEST(TestTimeSeries, createInit)
{
auto v = mio::TimeSeries<TypeParam>::Vector::Random(5).eval();
mio::TimeSeries<TypeParam> ts(0.0, v);
ASSERT_EQ(ts.get_num_elements(), 5);
ASSERT_EQ(ts.get_num_rows(), 6);
ASSERT_EQ(ts.get_num_time_points(), 1);
ASSERT_EQ(ts.get_capacity(), 1);
ASSERT_EQ(ts.get_time(0), 0.0);
ASSERT_EQ(print_wrap(ts.get_value(0)), print_wrap(v));
}
TYPED_TEST(TestTimeSeries, zeroElements)
{
mio::TimeSeries<TypeParam> ts(0);
ASSERT_EQ(ts.get_num_elements(), 0);
ASSERT_EQ(ts.get_num_rows(), 1);
ASSERT_EQ(ts.get_num_time_points(), 0);
ASSERT_EQ(ts.get_capacity(), 0);
ASSERT_NO_FATAL_FAILURE(ts.add_time_point(0.0));
}
TYPED_TEST(TestTimeSeries, addPoints)
{
mio::TimeSeries<TypeParam> ts(5);
ts.add_time_point(0.0);
ASSERT_EQ(ts.get_num_time_points(), 1);
ASSERT_EQ(ts.get_capacity(), 1 << 0);
ts.add_time_point(1.0);
ASSERT_EQ(ts.get_num_time_points(), 2);
ASSERT_EQ(ts.get_capacity(), 1 << 1);
ts.add_time_point(2.0);
ASSERT_EQ(ts.get_num_time_points(), 3);
ASSERT_EQ(ts.get_capacity(), 1 << 2);
float i = 3.0f;
while (i < 7) {
ts.add_time_point(i);
++i;
}
ASSERT_EQ(ts.get_num_time_points(), 7);
ASSERT_EQ(ts.get_capacity(), 1 << 3);
while (i < 123456) {
ts.add_time_point(i);
++i;
}
ASSERT_EQ(ts.get_num_time_points(), 123456);
ASSERT_EQ(ts.get_capacity(), 1 << 17);
}
TYPED_TEST(TestTimeSeries, assignValues)
{
mio::TimeSeries<TypeParam> ts(2);
auto v0 = mio::TimeSeries<TypeParam>::Vector::Random(2).eval();
ts.add_time_point(0.) = v0;
auto v1 = mio::TimeSeries<TypeParam>::Vector::Random(2).eval();
ts.add_time_point(1.);
ts[1] = v1;
ts.add_time_point(2., mio::TimeSeries<TypeParam>::Vector::Constant(2, 1));
auto v2 = mio::TimeSeries<TypeParam>::Vector::Constant(2, 1);
ASSERT_EQ(print_wrap(ts[0]), print_wrap(v0));
ASSERT_EQ(print_wrap(ts[1]), print_wrap(v1));
ASSERT_EQ(print_wrap(ts[2]), print_wrap(v2));
}
TYPED_TEST(TestTimeSeries, copyEmpty)
{
mio::TimeSeries<TypeParam> ts(10);
mio::TimeSeries<TypeParam> ts_copy1(ts);
mio::TimeSeries<TypeParam> ts_copy2(1);
ts_copy2 = ts;
for (auto&& ts_copy : {&ts_copy1, &ts_copy2}) {
ASSERT_EQ(ts_copy->get_num_elements(), 10);
ASSERT_EQ(ts_copy->get_num_rows(), 11);
ASSERT_EQ(ts_copy->get_num_time_points(), 0);
ASSERT_EQ(ts_copy->get_capacity(), 0);
}
}
TYPED_TEST(TestTimeSeries, reserve)
{
mio::TimeSeries<TypeParam> ts(2);
ts.reserve(10);
ASSERT_EQ(ts.get_capacity(), 16);
ts.reserve(200);
ASSERT_EQ(ts.get_capacity(), 256);
ts.reserve(10);
ASSERT_EQ(ts.get_capacity(), 256);
}
TYPED_TEST(TestTimeSeries, constAccess)
{
mio::TimeSeries<TypeParam> ts(1);
ts.add_time_point(0., mio::TimeSeries<TypeParam>::Vector::Random(1));
const auto& constref = ts;
static_assert(
std::is_same<decltype(constref[0]), Eigen::Ref<const typename mio::TimeSeries<TypeParam>::Vector>>::value,
"wrong type");
static_assert(std::is_same<decltype(constref.get_value(0)),
Eigen::Ref<const typename mio::TimeSeries<TypeParam>::Vector>>::value,
"wrong type");
static_assert(std::is_same<decltype(constref.get_last_value()),
Eigen::Ref<const typename mio::TimeSeries<TypeParam>::Vector>>::value,
"wrong type");
ASSERT_EQ(print_wrap(ts[0]), print_wrap(constref[0]));
}
#ifndef NDEBUG
TYPED_TEST(TestTimeSeries, createInvalidDim)
{
if (std::is_signed<Eigen::Index>::value) {
auto create = []() {
mio::TimeSeries<TypeParam> ts(-1);
return ts;
};
ASSERT_DEATH(create(), ".*");
}
}
TYPED_TEST(TestTimeSeries, accessInvalidRange)
{
mio::TimeSeries<TypeParam> ts(1);
for (Eigen::Index i = 0; i < 123; i++) {
ts.add_time_point();
}
ASSERT_DEATH(ts.get_value(-1), testing::ContainsRegex(".*"));
ASSERT_DEATH(ts.get_value(123), testing::ContainsRegex(".*"));
ASSERT_DEATH(ts.get_value(1231556), testing::ContainsRegex(".*"));
}
#endif
TYPED_TEST(TestTimeSeries, data)
{
mio::TimeSeries<TypeParam> ts(1);
auto v0 = mio::TimeSeries<TypeParam>::Vector::Constant(1, 0.5);
auto v1 = mio::TimeSeries<TypeParam>::Vector::Constant(1, 1.5);
auto v2 = mio::TimeSeries<TypeParam>::Vector::Constant(1, 2.5);
auto v3 = mio::TimeSeries<TypeParam>::Vector::Constant(1, 3.5);
ts.add_time_point(0.0, v0);
ts.add_time_point(1.0, v1);
ts.add_time_point(2.0, v2);
ts.add_time_point(3.0, v3);
auto data_range = mio::Range(ts.data(), ts.data() + 8);
ASSERT_THAT(data_range, testing::ElementsAre(TypeParam(0.0), TypeParam(0.5), TypeParam(1.0), TypeParam(1.5),
TypeParam(2.0), TypeParam(2.5), TypeParam(3.0), TypeParam(3.5)));
}
TYPED_TEST(TestTimeSeries, iteratorsRange)
{
mio::TimeSeries<TypeParam> ts(1);
auto v0 = mio::TimeSeries<TypeParam>::Vector::Constant(1, 0.5);
auto v1 = mio::TimeSeries<TypeParam>::Vector::Constant(1, 1.5);
auto v2 = mio::TimeSeries<TypeParam>::Vector::Constant(1, 2.5);
auto v3 = mio::TimeSeries<TypeParam>::Vector::Constant(1, 3.5);
ts.add_time_point(0.0, v0);
ts.add_time_point(1.0, v1);
ts.add_time_point(2.0, v2);
ts.add_time_point(3.0, v3);
//the range-loops and range-assert check the same condition in different ways
int i = 0;
for (auto&& v : ts) {
ASSERT_EQ(print_wrap(v), print_wrap(ts[i]));
++i;
}
i = 0;
const auto& ts_constref = ts;
for (auto&& v : ts_constref) {
ASSERT_EQ(print_wrap(v), print_wrap(ts[i]));
++i;
}
i = 3;
for (auto&& v : mio::Range(ts.rbegin(), ts.rend())) {
ASSERT_EQ(print_wrap(v), print_wrap(ts[i]));
--i;
}
ASSERT_THAT(ts, testing::ElementsAre(v0, v1, v2, v3));
ASSERT_THAT(ts_constref, testing::ElementsAre(v0, v1, v2, v3));
ASSERT_THAT(mio::Range(ts.rbegin(), ts.rend()), testing::ElementsAre(v3, v2, v1, v0));
}
TYPED_TEST(TestTimeSeries, timeIteratorsRange)
{
mio::TimeSeries<TypeParam> ts(1);
auto v0 = mio::TimeSeries<TypeParam>::Vector::Constant(1, 0.5);
auto v1 = mio::TimeSeries<TypeParam>::Vector::Constant(1, 1.5);
auto v2 = mio::TimeSeries<TypeParam>::Vector::Constant(1, 2.5);
auto v3 = mio::TimeSeries<TypeParam>::Vector::Constant(1, 3.5);
ts.add_time_point(0.0, v0);
ts.add_time_point(1.0, v1);
ts.add_time_point(2.0, v2);
ts.add_time_point(3.0, v3);
//the range-loops and range-assert check the same condition in different ways
int i = 0;
for (auto&& t : ts.get_times()) {
ASSERT_EQ(t, ts.get_time(i));
++i;
}
i = 0;
const auto& ts_constref = ts;
for (auto&& t : ts_constref.get_times()) {
ASSERT_EQ(t, ts.get_time(i));
++i;
}
i = 3;
for (auto&& t : ts.get_reverse_times()) {
ASSERT_EQ(t, ts.get_time(i));
--i;
}
ASSERT_THAT(ts.get_times(), testing::ElementsAre(TypeParam(0.0), TypeParam(1.0), TypeParam(2.0), TypeParam(3.0)));
ASSERT_THAT(ts_constref.get_times(),
testing::ElementsAre(TypeParam(0.0), TypeParam(1.0), TypeParam(2.0), TypeParam(3.0)));
ASSERT_THAT(ts.get_reverse_times(),
testing::ElementsAre(TypeParam(3.0), TypeParam(2.0), TypeParam(1.0), TypeParam(0.0)));
}
TYPED_TEST(TestTimeSeries, iteratorsRandomAccess)
{
mio::TimeSeries<TypeParam> ts(1);
auto v0 = mio::TimeSeries<TypeParam>::Vector::Constant(1, 0.5);
auto v1 = mio::TimeSeries<TypeParam>::Vector::Constant(1, 1.5);
auto v2 = mio::TimeSeries<TypeParam>::Vector::Constant(1, 2.5);
auto v3 = mio::TimeSeries<TypeParam>::Vector::Constant(1, 3.5);
ts.add_time_point(0.0, v0);
ts.add_time_point(1.0, v1);
ts.add_time_point(2.0, v2);
ts.add_time_point(3.0, v3);
auto it0 = ts.begin();
auto it1 = ts.begin() + 1;
auto it2 = ts.begin() + 2;
auto it3 = ts.begin() + 3;
auto itEnd = ts.end();
//deref
ASSERT_EQ(print_wrap(*it0), print_wrap(v0));
ASSERT_EQ(print_wrap(*it1), print_wrap(v1));
ASSERT_EQ(print_wrap(*it2), print_wrap(v2));
ASSERT_EQ(print_wrap(*it3), print_wrap(v3));
//index
ASSERT_EQ(print_wrap(it1[1]), print_wrap(v2));
//addition
auto it2c1 = it2;
ASSERT_EQ(++it2c1, it3);
auto it2c2 = it2;
ASSERT_EQ(it2c2++, it2);
auto it2c3 = it2;
ASSERT_EQ(it2c3 += 1, it3);
ASSERT_EQ(it2 + 2, itEnd);
//subtraction
auto it3c1 = it3;
ASSERT_EQ(--it3c1, it2);
auto it3c2 = it3;
ASSERT_EQ(it3c2--, it3);
auto it3c3 = it3;
ASSERT_EQ(it3c3 -= 3, it0);
ASSERT_EQ(itEnd - 1, it3);
//comparison
ASSERT_LT(it0, it1);
ASSERT_GT(it3, it1);
ASSERT_LE(it1, itEnd);
ASSERT_GE(it2, it0);
}
TYPED_TEST(TestTimeSeries, create)
{
auto ts = mio::TimeSeries<TypeParam>::zero(5, 10);
for (int i = 0; i < 5; i++) {
ASSERT_EQ(ts.get_time(i), 0.0);
for (int j = 0; j < 10; j++) {
ASSERT_EQ(ts[i][j], 0.0);
}
}
}
TYPED_TEST(TestTimeSeries, print_table)
{
std::stringstream output;
mio::TimeSeries<TypeParam> ts = mio::TimeSeries<TypeParam>::zero(2, 2);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
ts[i][j] = static_cast<TypeParam>(i + j + 0.123456);
}
}
ts.get_time((Eigen::Index)0) = static_cast<TypeParam>(0.0);
ts.get_time((Eigen::Index)1) = static_cast<TypeParam>(1.0);
std::string expected_output_1 = "\nTime col_1 col_2\n0.00 0.12 1.12\n1.00 1.12 2.12\n";
ts.print_table(output, {"col_1", "col_2"}, 4, 2);
std::string actual_output_1 = output.str();
EXPECT_EQ(expected_output_1, actual_output_1);
output.str("");
std::string expected_output_2 = "\nTime C1 C2 \n 0.0 0.1 1.1\n 1.0 1.1 2.1\n";
ts.print_table(output, {}, 6, 1);
std::string actual_output_2 = output.str();
EXPECT_EQ(expected_output_2, actual_output_2);
output.str("");
std::string expected_output_3 = "\nTime col_1 C2 \n 0.0000 0.1235 "
"1.1235\n 1.0000 1.1235 2.1235\n";
ts.print_table(output, {"col_1"}, 12, 4);
std::string actual_output_3 = output.str();
EXPECT_EQ(expected_output_3, actual_output_3);
}
TYPED_TEST(TestTimeSeries, print_table_cout_overload)
{
// Just test that the print_table overload without ostream argument doesn't throw any exceptions.
// The function behaviour is tested in "TestTimeSeries.print_table".
mio::TimeSeries<TypeParam> ts = mio::TimeSeries<TypeParam>::zero(1, 1);
std::cout.setstate(std::ios_base::failbit);
ASSERT_NO_FATAL_FAILURE(ts.print_table());
std::cout.clear();
}
TYPED_TEST(TestTimeSeries, export_csv)
{
// Fill time series with test data
mio::TimeSeries<TypeParam> ts = mio::TimeSeries<TypeParam>::zero(2, 2);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
ts[i][j] = static_cast<TypeParam>(i + j + 0.123456);
}
}
ts.get_time((Eigen::Index)0) = static_cast<TypeParam>(0.0);
ts.get_time((Eigen::Index)1) = static_cast<TypeParam>(1.0);
// Create a temp file for testing
TempFileRegister file_register;
auto csv_file_path = file_register.get_unique_path("test_csv-%%%%-%%%%.csv");
// Test export_csv function
auto result = ts.export_csv(csv_file_path, {"column1", "column2"});
ASSERT_TRUE(result);
// Read file and check data
std::ifstream file(csv_file_path);
ASSERT_TRUE(file.is_open());
std::string line;
std::getline(file, line);
EXPECT_EQ(line, "Time,column1,column2");
std::getline(file, line);
EXPECT_EQ(line, "0.000000,0.123456,1.123456");
std::getline(file, line);
EXPECT_EQ(line, "1.000000,1.123456,2.123456");
file.close();
}
TYPED_TEST(TestTimeSeries, export_csv_no_labels)
{
// Fill time series with test data
mio::TimeSeries<TypeParam> ts = mio::TimeSeries<TypeParam>::zero(2, 2);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
ts[i][j] = static_cast<TypeParam>(i + j + 0.123456);
}
}
ts.get_time((Eigen::Index)0) = static_cast<TypeParam>(0.0);
ts.get_time((Eigen::Index)1) = static_cast<TypeParam>(1.0);
// Create a temp file for testing
TempFileRegister file_register;
auto csv_file_path = file_register.get_unique_path("test_csv-%%%%-%%%%.csv");
// Test export_csv function without column names
auto result = ts.export_csv(csv_file_path);
ASSERT_TRUE(result);
// Read file and check data
std::ifstream file(csv_file_path);
ASSERT_TRUE(file.is_open());
std::string line;
std::getline(file, line);
EXPECT_EQ(line, "Time,C1,C2");
std::getline(file, line);
EXPECT_EQ(line, "0.000000,0.123456,1.123456");
std::getline(file, line);
EXPECT_EQ(line, "1.000000,1.123456,2.123456");
file.close();
}
TYPED_TEST(TestTimeSeries, export_csv_different_separator)
{
// Fill time series with test data
mio::TimeSeries<TypeParam> ts = mio::TimeSeries<TypeParam>::zero(2, 2);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
ts[i][j] = static_cast<TypeParam>(i + j + 0.123456);
}
}
ts.get_time((Eigen::Index)0) = static_cast<TypeParam>(0.0);
ts.get_time((Eigen::Index)1) = static_cast<TypeParam>(1.0);
// Create a temp file for testing
TempFileRegister file_register;
auto csv_file_path = file_register.get_unique_path("test_csv-%%%%-%%%%.csv");
// Export using semicolon as separator and precision of 3
auto result = ts.export_csv(csv_file_path, {"col1", "col2"}, ';', 3);
ASSERT_TRUE(result);
// Read file and check data
std::ifstream file(csv_file_path);
ASSERT_TRUE(file.is_open());
std::string line;
std::getline(file, line);
EXPECT_EQ(line, "Time;col1;col2");
std::getline(file, line);
EXPECT_EQ(line, "0.000;0.123;1.123");
std::getline(file, line);
EXPECT_EQ(line, "1.000;1.123;2.123");
file.close();
}
TYPED_TEST(TestTimeSeries, export_csv_failed)
{
mio::TimeSeries<TypeParam> ts = mio::TimeSeries<TypeParam>::zero(2, 2);
auto result = ts.export_csv("/test_false_dir/file.csv");
ASSERT_FALSE(result);
}
TYPED_TEST(TestTimeSeries, printTo)
{
//PrintTo is test code, so we don't check the exact output, just that it exists and doesn't fail
auto ts = mio::TimeSeries<TypeParam>::zero(3, 2);
std::stringstream ss;
PrintTo(ts, &ss);
ASSERT_FALSE(ss.str().empty());
}