22
22
#include " matchers.h"
23
23
#include < gtest/gtest.h>
24
24
#include < gmock/gmock.h>
25
+ #include " temp_file_register.h"
25
26
26
27
template <class T >
27
28
using TestTimeSeries = ::testing::Test;
@@ -314,7 +315,7 @@ TYPED_TEST(TestTimeSeries, iteratorsRandomAccess)
314
315
315
316
TYPED_TEST (TestTimeSeries, create)
316
317
{
317
- auto ts = mio::TimeSeries<double >::zero (5 , 10 );
318
+ auto ts = mio::TimeSeries<TypeParam >::zero (5 , 10 );
318
319
for (int i = 0 ; i < 5 ; i++) {
319
320
ASSERT_EQ (ts.get_time (i), 0.0 );
320
321
for (int j = 0 ; j < 10 ; j++) {
@@ -326,40 +327,166 @@ TYPED_TEST(TestTimeSeries, create)
326
327
TYPED_TEST (TestTimeSeries, print_table)
327
328
{
328
329
std::stringstream output;
329
- mio::TimeSeries<double > ts = mio::TimeSeries<double >::zero (2 , 2 );
330
+ mio::TimeSeries<TypeParam > ts = mio::TimeSeries<TypeParam >::zero (2 , 2 );
330
331
for (int i = 0 ; i < 2 ; i++) {
331
332
for (int j = 0 ; j < 2 ; j++) {
332
- ts[i][j] = i + j + 0.123456789 ;
333
+ ts[i][j] = static_cast <TypeParam>( i + j + 0.123456 ) ;
333
334
}
334
335
}
335
- ts.get_time ((Eigen::Index)0 ) = 0.0 ;
336
- ts.get_time ((Eigen::Index)1 ) = 1.0 ;
336
+ ts.get_time ((Eigen::Index)0 ) = static_cast <TypeParam>( 0.0 ) ;
337
+ ts.get_time ((Eigen::Index)1 ) = static_cast <TypeParam>( 1.0 ) ;
337
338
338
339
std::string expected_output_1 = " \n Time col_1 col_2\n 0.00 0.12 1.12\n 1.00 1.12 2.12\n " ;
339
- ts.print_table ({" col_1" , " col_2" }, 4 , 2 , output );
340
+ ts.print_table (output, {" col_1" , " col_2" }, 4 , 2 );
340
341
std::string actual_output_1 = output.str ();
341
342
EXPECT_EQ (expected_output_1, actual_output_1);
342
343
343
344
output.str (" " );
344
345
345
- std::string expected_output_2 = " \n Time #1 #2 \n 0.0 0.1 1.1\n 1.0 1.1 2.1\n " ;
346
- ts.print_table ({}, 6 , 1 , output );
346
+ std::string expected_output_2 = " \n Time C1 C2 \n 0.0 0.1 1.1\n 1.0 1.1 2.1\n " ;
347
+ ts.print_table (output, {}, 6 , 1 );
347
348
std::string actual_output_2 = output.str ();
348
349
EXPECT_EQ (expected_output_2, actual_output_2);
349
350
350
351
output.str (" " );
351
352
352
- std::string expected_output_3 = " \n Time col_1 #2 \n 0.0000 0.1235 "
353
+ std::string expected_output_3 = " \n Time col_1 C2 \n 0.0000 0.1235 "
353
354
" 1.1235\n 1.0000 1.1235 2.1235\n " ;
354
- ts.print_table ({" col_1" }, 12 , 4 , output );
355
+ ts.print_table (output, {" col_1" }, 12 , 4 );
355
356
std::string actual_output_3 = output.str ();
356
357
EXPECT_EQ (expected_output_3, actual_output_3);
357
358
}
358
359
359
- TEST (TestTimeSeries, printTo)
360
+ TYPED_TEST (TestTimeSeries, print_table_cout_overload)
361
+ {
362
+ // Just test that the print_table overload without ostream argument doesn't throw any exceptions.
363
+ // The function behaviour is tested in "TestTimeSeries.print_table".
364
+ mio::TimeSeries<TypeParam> ts = mio::TimeSeries<TypeParam>::zero (1 , 1 );
365
+ ASSERT_NO_FATAL_FAILURE (ts.print_table ());
366
+ }
367
+
368
+ TYPED_TEST (TestTimeSeries, export_csv)
369
+ {
370
+ // Fill time series with test data
371
+ mio::TimeSeries<TypeParam> ts = mio::TimeSeries<TypeParam>::zero (2 , 2 );
372
+ for (int i = 0 ; i < 2 ; i++) {
373
+ for (int j = 0 ; j < 2 ; j++) {
374
+ ts[i][j] = static_cast <TypeParam>(i + j + 0.123456 );
375
+ }
376
+ }
377
+ ts.get_time ((Eigen::Index)0 ) = static_cast <TypeParam>(0.0 );
378
+ ts.get_time ((Eigen::Index)1 ) = static_cast <TypeParam>(1.0 );
379
+
380
+ // Create a temp file for testing
381
+ TempFileRegister file_register;
382
+ auto csv_file_path = file_register.get_unique_path (" test_csv-%%%%-%%%%.csv" );
383
+
384
+ // Test export_csv function
385
+ auto result = ts.export_csv (csv_file_path, {" column1" , " column2" });
386
+ ASSERT_TRUE (result);
387
+
388
+ // Read file and check data
389
+ std::ifstream file (csv_file_path);
390
+ ASSERT_TRUE (file.is_open ());
391
+
392
+ std::string line;
393
+ std::getline (file, line);
394
+ EXPECT_EQ (line, " Time,column1,column2" );
395
+
396
+ std::getline (file, line);
397
+ EXPECT_EQ (line, " 0.000000,0.123456,1.123456" );
398
+
399
+ std::getline (file, line);
400
+ EXPECT_EQ (line, " 1.000000,1.123456,2.123456" );
401
+
402
+ file.close ();
403
+ }
404
+
405
+ TYPED_TEST (TestTimeSeries, export_csv_no_labels)
406
+ {
407
+ // Fill time series with test data
408
+ mio::TimeSeries<TypeParam> ts = mio::TimeSeries<TypeParam>::zero (2 , 2 );
409
+ for (int i = 0 ; i < 2 ; i++) {
410
+ for (int j = 0 ; j < 2 ; j++) {
411
+ ts[i][j] = static_cast <TypeParam>(i + j + 0.123456 );
412
+ }
413
+ }
414
+ ts.get_time ((Eigen::Index)0 ) = static_cast <TypeParam>(0.0 );
415
+ ts.get_time ((Eigen::Index)1 ) = static_cast <TypeParam>(1.0 );
416
+
417
+ // Create a temp file for testing
418
+ TempFileRegister file_register;
419
+ auto csv_file_path = file_register.get_unique_path (" test_csv-%%%%-%%%%.csv" );
420
+
421
+ // Test export_csv function without column names
422
+ auto result = ts.export_csv (csv_file_path);
423
+ ASSERT_TRUE (result);
424
+
425
+ // Read file and check data
426
+ std::ifstream file (csv_file_path);
427
+ ASSERT_TRUE (file.is_open ());
428
+
429
+ std::string line;
430
+ std::getline (file, line);
431
+ EXPECT_EQ (line, " Time,C1,C2" );
432
+
433
+ std::getline (file, line);
434
+ EXPECT_EQ (line, " 0.000000,0.123456,1.123456" );
435
+
436
+ std::getline (file, line);
437
+ EXPECT_EQ (line, " 1.000000,1.123456,2.123456" );
438
+
439
+ file.close ();
440
+ }
441
+
442
+ TYPED_TEST (TestTimeSeries, export_csv_different_separator)
443
+ {
444
+ // Fill time series with test data
445
+ mio::TimeSeries<TypeParam> ts = mio::TimeSeries<TypeParam>::zero (2 , 2 );
446
+ for (int i = 0 ; i < 2 ; i++) {
447
+ for (int j = 0 ; j < 2 ; j++) {
448
+ ts[i][j] = static_cast <TypeParam>(i + j + 0.123456 );
449
+ }
450
+ }
451
+ ts.get_time ((Eigen::Index)0 ) = static_cast <TypeParam>(0.0 );
452
+ ts.get_time ((Eigen::Index)1 ) = static_cast <TypeParam>(1.0 );
453
+
454
+ // Create a temp file for testing
455
+ TempFileRegister file_register;
456
+ auto csv_file_path = file_register.get_unique_path (" test_csv-%%%%-%%%%.csv" );
457
+
458
+ // Export using semicolon as separator and precision of 3
459
+ auto result = ts.export_csv (csv_file_path, {" col1" , " col2" }, ' ;' , 3 );
460
+ ASSERT_TRUE (result);
461
+
462
+ // Read file and check data
463
+ std::ifstream file (csv_file_path);
464
+ ASSERT_TRUE (file.is_open ());
465
+
466
+ std::string line;
467
+ std::getline (file, line);
468
+ EXPECT_EQ (line, " Time;col1;col2" );
469
+
470
+ std::getline (file, line);
471
+ EXPECT_EQ (line, " 0.000;0.123;1.123" );
472
+
473
+ std::getline (file, line);
474
+ EXPECT_EQ (line, " 1.000;1.123;2.123" );
475
+
476
+ file.close ();
477
+ }
478
+
479
+ TYPED_TEST (TestTimeSeries, export_csv_failed)
480
+ {
481
+ mio::TimeSeries<TypeParam> ts = mio::TimeSeries<TypeParam>::zero (2 , 2 );
482
+ auto result = ts.export_csv (" /test_false_dir/file.csv" );
483
+ ASSERT_FALSE (result);
484
+ }
485
+
486
+ TYPED_TEST (TestTimeSeries, printTo)
360
487
{
361
488
// PrintTo is test code, so we don't check the exact output, just that it exists and doesn't fail
362
- auto ts = mio::TimeSeries<double >::zero (3 , 2 );
489
+ auto ts = mio::TimeSeries<TypeParam >::zero (3 , 2 );
363
490
std::stringstream ss;
364
491
PrintTo (ts, &ss);
365
492
ASSERT_FALSE (ss.str ().empty ());
0 commit comments