forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimeSeriesPropertyTest.h
2345 lines (1913 loc) · 87.2 KB
/
TimeSeriesPropertyTest.h
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
#include "MantidKernel/Exception.h"
#include "MantidKernel/PropertyWithValue.h"
#include "MantidKernel/TimeSeriesProperty.h"
#include "MantidKernel/TimeSplitter.h"
#include <cxxtest/TestSuite.h>
#include <boost/scoped_ptr.hpp>
#include <cmath>
#include <json/value.h>
#include <memory>
#include <vector>
using namespace Mantid::Kernel;
using Mantid::Types::Core::DateAndTime;
class TimeSeriesPropertyTest : public CxxTest::TestSuite {
// Create a small TSP<double>. Callee owns the returned object.
TimeSeriesProperty<double> *createDoubleTSP() {
TimeSeriesProperty<double> *p = new TimeSeriesProperty<double>("doubleProp");
TS_ASSERT_THROWS_NOTHING(p->addValue("2007-11-30T16:17:00", 9.99));
TS_ASSERT_THROWS_NOTHING(p->addValue("2007-11-30T16:17:10", 7.55));
TS_ASSERT_THROWS_NOTHING(p->addValue("2007-11-30T16:17:20", 5.55));
TS_ASSERT_THROWS_NOTHING(p->addValue("2007-11-30T16:17:30", 10.55));
return p;
}
// Create a small TSP<int>. Callee owns the returned object.
TimeSeriesProperty<int> *createIntegerTSP(int numberOfValues) {
TimeSeriesProperty<int> *log = new TimeSeriesProperty<int>("intProp");
DateAndTime startTime("2007-11-30T16:17:00");
for (int value = 0; value < numberOfValues; ++value) {
DateAndTime time = startTime + value * 10.0;
TS_ASSERT_THROWS_NOTHING(log->addValue(time, value + 1));
}
return log;
}
public:
void setUp() override {
iProp = new TimeSeriesProperty<int>("intProp");
dProp = new TimeSeriesProperty<double>("doubleProp");
sProp = new TimeSeriesProperty<std::string>("stringProp");
}
void tearDown() override {
delete iProp;
delete dProp;
delete sProp;
}
void test_Constructor() {
// Test that all the base class member variables are correctly assigned to
TS_ASSERT(!iProp->name().compare("intProp"));
TS_ASSERT(!iProp->documentation().compare(""));
TS_ASSERT(typeid(std::vector<TimeValueUnit<int>>) == *iProp->type_info());
TS_ASSERT(!iProp->isDefault())
TS_ASSERT(!dProp->name().compare("doubleProp"));
TS_ASSERT(!dProp->documentation().compare(""));
TS_ASSERT(typeid(std::vector<TimeValueUnit<double>>) == *dProp->type_info());
TS_ASSERT(!dProp->isDefault())
TS_ASSERT(!sProp->name().compare("stringProp"));
TS_ASSERT(!sProp->documentation().compare(""));
TS_ASSERT(typeid(std::vector<TimeValueUnit<std::string>>) == *sProp->type_info());
TS_ASSERT(!sProp->isDefault())
TS_ASSERT_EQUALS(sProp->isValid(), "");
}
void test_Constructor_with_values() {
std::vector<DateAndTime> times = {DateAndTime("2019-01-01T00:00:00"), DateAndTime("2019-01-01T00:01:00")};
// Test int TimeSeriesProperty
std::vector<int> iValues = {0, 1};
auto iPropWithValues = std::make_unique<TimeSeriesProperty<int>>("intProp", times, iValues);
TS_ASSERT(!iPropWithValues->name().compare("intProp"));
TS_ASSERT(!iPropWithValues->documentation().compare(""));
TS_ASSERT(typeid(std::vector<TimeValueUnit<int>>) == *iPropWithValues->type_info());
TS_ASSERT(!iPropWithValues->isDefault());
auto iPropTimes = iPropWithValues->timesAsVector();
TS_ASSERT_EQUALS(iPropTimes[0], DateAndTime("2019-01-01T00:00:00"));
TS_ASSERT_EQUALS(iPropTimes[1], DateAndTime("2019-01-01T00:01:00"));
auto iPropValues = iPropWithValues->valuesAsVector();
TS_ASSERT_EQUALS(iPropValues[0], 0);
TS_ASSERT_EQUALS(iPropValues[1], 1);
// Test double TimeSeriesProperty
std::vector<double> dValues = {0.1, 1.2};
auto dPropWithValues = std::make_unique<TimeSeriesProperty<double>>("doubleProp", times, dValues);
TS_ASSERT(!dPropWithValues->name().compare("doubleProp"));
TS_ASSERT(!dPropWithValues->documentation().compare(""));
TS_ASSERT(typeid(std::vector<TimeValueUnit<double>>) == *dPropWithValues->type_info());
TS_ASSERT(!dPropWithValues->isDefault());
auto dPropTimes = dPropWithValues->timesAsVector();
TS_ASSERT_EQUALS(dPropTimes[0], DateAndTime("2019-01-01T00:00:00"));
TS_ASSERT_EQUALS(dPropTimes[1], DateAndTime("2019-01-01T00:01:00"));
auto dPropValues = dPropWithValues->valuesAsVector();
TS_ASSERT_EQUALS(dPropValues[0], 0.1);
TS_ASSERT_EQUALS(dPropValues[1], 1.2);
// Test string TimeSeriesProperty
std::vector<std::string> sValues = {"test", "test2"};
auto sPropWithValues = std::make_unique<TimeSeriesProperty<std::string>>("stringProp", times, sValues);
TS_ASSERT(!sPropWithValues->name().compare("stringProp"));
TS_ASSERT(!sPropWithValues->documentation().compare(""));
TS_ASSERT(typeid(std::vector<TimeValueUnit<std::string>>) == *sPropWithValues->type_info());
TS_ASSERT(!sPropWithValues->isDefault());
auto sPropTimes = sPropWithValues->timesAsVector();
TS_ASSERT_EQUALS(sPropTimes[0], DateAndTime("2019-01-01T00:00:00"));
TS_ASSERT_EQUALS(sPropTimes[1], DateAndTime("2019-01-01T00:01:00"));
auto sPropValues = sPropWithValues->valuesAsVector();
TS_ASSERT_EQUALS(sPropValues[0], "test");
TS_ASSERT_EQUALS(sPropValues[1], "test2");
}
void test_SetValueFromString() {
TS_ASSERT_THROWS(iProp->setValue("1"), const Exception::NotImplementedError &);
TS_ASSERT_THROWS(dProp->setValue("5.5"), const Exception::NotImplementedError &);
TS_ASSERT_THROWS(sProp->setValue("aValue"), const Exception::NotImplementedError &);
}
void test_SetValueFromJson() {
TS_ASSERT_THROWS(iProp->setValueFromJson(Json::Value(1)), const Exception::NotImplementedError &);
TS_ASSERT_THROWS(dProp->setValueFromJson(Json::Value(5.5)), const Exception::NotImplementedError &);
TS_ASSERT_THROWS(sProp->setValueFromJson(Json::Value("aValue")), const Exception::NotImplementedError &);
}
void test_AddValue() {
const std::string tester("2007-11-30T16:17:00");
int sizepre = iProp->size();
TS_ASSERT_THROWS_NOTHING(iProp->addValue(tester, 1));
TS_ASSERT_THROWS_NOTHING(iProp->addValue("2007-11-30T16:17:10", 1));
TS_ASSERT_EQUALS(iProp->size(), sizepre + 2);
sizepre = dProp->size();
TS_ASSERT_THROWS_NOTHING(dProp->addValue("2007-11-30T16:17:00", 9.99));
TS_ASSERT_THROWS_NOTHING(dProp->addValue("2007-11-30T16:17:10", 5.55));
TS_ASSERT_EQUALS(dProp->size(), sizepre + 2);
sizepre = sProp->size();
TS_ASSERT_THROWS_NOTHING(sProp->addValue("2007-11-30T16:17:00", "test"));
TS_ASSERT_THROWS_NOTHING(sProp->addValue("2007-11-30T16:17:10", "test2"));
TS_ASSERT_EQUALS(sProp->size(), sizepre + 2);
// Now try the other overloads
TimeSeriesProperty<int> otherProp("otherProp");
TS_ASSERT_THROWS_NOTHING(otherProp.addValue(static_cast<std::time_t>(123), 1));
TS_ASSERT_THROWS_NOTHING(otherProp.addValue(boost::posix_time::second_clock::local_time(), 1));
const std::string dString = dProp->value();
TS_ASSERT_EQUALS(dString.substr(0, 27), "2007-Nov-30 16:17:00 9.99\n");
const std::string iString = iProp->value();
TS_ASSERT_EQUALS(iString.substr(0, 24), "2007-Nov-30 16:17:00 1\n");
const std::string sString = sProp->value();
TS_ASSERT_EQUALS(sString.substr(0, 27), "2007-Nov-30 16:17:00 test\n");
// Test the internal toggling of the 'sorted' flag works
auto twoVals = dProp->valuesAsVector();
double newVal = 2.22;
dProp->addValue("2007-11-30T16:17:05", newVal);
// Calling this method sorts the vector by time, so long as the internal
// flag says it isn't sorted
auto threeVals = dProp->valuesAsVector();
TS_ASSERT_EQUALS(threeVals.size(), 3);
TS_ASSERT_EQUALS(twoVals[0], threeVals[0]);
TS_ASSERT_EQUALS(twoVals[1], threeVals[2]);
TS_ASSERT_EQUALS(newVal, threeVals[1]);
}
void test_GetDerivative() {
dProp->addValue("2007-11-30T16:17:10", 10);
dProp->addValue("2007-11-30T16:17:12", 12);
dProp->addValue("2007-11-30T16:17:01", 01);
dProp->addValue("2007-11-30T16:17:05", 05);
auto derProp = dProp->getDerivative();
TS_ASSERT(dynamic_cast<TimeSeriesProperty<double> *>(derProp.get()))
TS_ASSERT_EQUALS(derProp->size(), 3);
auto derValues = derProp->valuesAsVector();
TS_ASSERT_EQUALS(derValues[0], 1);
TS_ASSERT_EQUALS(derValues[1], 1);
TS_ASSERT_EQUALS(derValues[2], 1);
TSM_ASSERT_THROWS("derivative undefined for string property", sProp->getDerivative(), const std::runtime_error &);
iProp->addValue("2007-11-30T16:17:10", 10);
TSM_ASSERT_THROWS("derivative undefined for property with less then 2 values", iProp->getDerivative(),
const std::runtime_error &);
iProp->addValue("2007-11-30T16:17:12", 12);
derProp = iProp->getDerivative();
TS_ASSERT_EQUALS(derProp->size(), 1);
derValues = derProp->valuesAsVector();
TS_ASSERT_EQUALS(derValues[0], 1);
}
void test_timesAsVector() {
TimeSeriesProperty<double> *p = new TimeSeriesProperty<double>("doubleProp");
TS_ASSERT_THROWS_NOTHING(p->addValue("2007-11-30T16:17:20", 5.55));
TS_ASSERT_THROWS_NOTHING(p->addValue("2007-11-30T16:17:00", 9.99));
TS_ASSERT_THROWS_NOTHING(p->addValue("2007-11-30T16:17:10", 5.55));
TS_ASSERT_THROWS_NOTHING(p->addValue("2007-11-30T16:17:30", 5.55));
std::vector<double> timeSec;
timeSec = p->timesAsVectorSeconds();
TS_ASSERT_DELTA(timeSec[0], 0.0, 1e-6);
TS_ASSERT_DELTA(timeSec[1], 10.0, 1e-6);
TS_ASSERT_DELTA(timeSec[2], 20.0, 1e-6);
TS_ASSERT_DELTA(timeSec[3], 30.0, 1e-6);
std::vector<DateAndTime> time;
time = p->timesAsVector();
TS_ASSERT_EQUALS(time[0], DateAndTime("2007-11-30T16:17:00"));
TS_ASSERT_EQUALS(time[1], DateAndTime("2007-11-30T16:17:10"));
TS_ASSERT_EQUALS(time[2], DateAndTime("2007-11-30T16:17:20"));
TS_ASSERT_EQUALS(time[3], DateAndTime("2007-11-30T16:17:30"));
delete p;
}
void test_replaceValues() {
// Arrange
size_t num = 1000;
DateAndTime first("2007-11-30T16:17:10");
std::vector<DateAndTime> times;
std::vector<double> values;
std::vector<double> replacementValues;
double offset = 100.0;
for (size_t i = 0; i < num; i++) {
times.emplace_back(first + double(i));
values.emplace_back(double(i));
replacementValues.emplace_back(double(i) + offset);
}
TimeSeriesProperty<double> tsp("test");
tsp.addValues(times, values);
TS_ASSERT_EQUALS(tsp.size(), 1000);
TS_ASSERT_EQUALS(tsp.nthValue(3), 3.0);
// Act
tsp.replaceValues(times, replacementValues);
// Assert
TSM_ASSERT_EQUALS("Should have 1000 entries", tsp.size(), 1000);
TSM_ASSERT_EQUALS("Should be 3 plus the offset of 100", tsp.nthValue(3), 103.0);
}
void test_addValues() {
size_t num = 1000;
DateAndTime first("2007-11-30T16:17:10");
std::vector<DateAndTime> times;
std::vector<double> values;
for (size_t i = 0; i < num; i++) {
times.emplace_back(first + double(i));
values.emplace_back(double(i));
}
TimeSeriesProperty<double> tsp("test");
tsp.addValues(times, values);
TS_ASSERT_EQUALS(tsp.size(), 1000);
TS_ASSERT_EQUALS(tsp.nthValue(3), 3.0);
}
void test_Casting() {
TS_ASSERT_DIFFERS(dynamic_cast<Property *>(iProp), static_cast<Property *>(nullptr));
TS_ASSERT_DIFFERS(dynamic_cast<Property *>(dProp), static_cast<Property *>(nullptr));
TS_ASSERT_DIFFERS(dynamic_cast<Property *>(sProp), static_cast<Property *>(nullptr));
TS_ASSERT_DIFFERS(dynamic_cast<ITimeSeriesProperty *>(iProp), static_cast<ITimeSeriesProperty *>(nullptr));
}
//----------------------------------------------------------------------------
void test_AdditionOperator() {
TimeSeriesProperty<int> *log = new TimeSeriesProperty<int>("MyIntLog");
TS_ASSERT_THROWS_NOTHING(log->addValue("2007-11-30T16:17:00", 1));
TS_ASSERT_THROWS_NOTHING(log->addValue("2007-11-30T16:19:10", 2));
TimeSeriesProperty<int> *log2 = new TimeSeriesProperty<int>("MyIntLog2");
TS_ASSERT_THROWS_NOTHING(log2->addValue("2007-11-30T16:18:00", 3));
TS_ASSERT_THROWS_NOTHING(log2->addValue("2007-11-30T16:18:10", 4));
TS_ASSERT_THROWS_NOTHING(log2->addValue("2007-11-30T16:18:11", 5));
TS_ASSERT_EQUALS(log->size(), 2);
// Concatenate the lists
(*log) += log2;
TS_ASSERT_EQUALS(log->size(), 5);
DateAndTime t0 = log->firstTime();
DateAndTime tf = log->lastTime();
TS_ASSERT_EQUALS(t0, DateAndTime("2007-11-30T16:17:00"));
TS_ASSERT_EQUALS(tf, DateAndTime("2007-11-30T16:19:10"));
delete log;
delete log2;
}
//----------------------------------------------------------------------------
/// Ticket 2097: This caused an infinite loop
void test_AdditionOperatorOnYourself() {
TimeSeriesProperty<int> *log = createIntegerTSP(2);
(*log) += log;
// There is now a check and trying to do this does nothing.
TS_ASSERT_EQUALS(log->size(), 2);
delete log;
}
void test_ComparisonOperator() {
// Setup two logs and two filters so that logs have different sizes but are
// the same size after applying the filter
TimeSeriesProperty<int> *log1 = new TimeSeriesProperty<int>("count_rate");
log1->addValue("2016-03-17T00:00:00", 1);
log1->addValue("2016-03-17T00:30:00", 2);
log1->addValue("2016-03-17T01:00:00", 3);
log1->addValue("2016-03-17T01:30:00", 4);
log1->addValue("2016-03-17T02:00:00", 5);
TimeSeriesProperty<bool> *filter1 = new TimeSeriesProperty<bool>("filter");
filter1->addValue("2016-Mar-17 00:00:00", 1);
filter1->addValue("2016-Mar-17 01:00:00", 0);
log1->filterWith(filter1);
TimeSeriesProperty<int> *log2 = new TimeSeriesProperty<int>("count_rate");
log2->addValue("2016-03-17T03:00:00", 1);
log2->addValue("2016-03-17T04:00:00", 2);
log2->addValue("2016-03-17T05:00:00", 3);
log2->addValue("2016-03-17T06:00:0", 4);
TimeSeriesProperty<bool> *filter2 = new TimeSeriesProperty<bool>("filter");
filter2->addValue("2016-Mar-17 03:00:00", 1);
filter2->addValue("2016-Mar-17 05:00:00", 0);
log2->filterWith(filter2);
TS_ASSERT(!(*log1 == *log2));
delete log1;
delete log2;
delete filter1;
delete filter2;
}
//----------------------------------------------------------------------------
void test_filterByTime() {
TimeSeriesProperty<int> *log = createIntegerTSP(6);
TS_ASSERT_EQUALS(log->realSize(), 6);
DateAndTime start = DateAndTime("2007-11-30T16:17:10");
DateAndTime stop = DateAndTime("2007-11-30T16:17:40");
// Since the filter is < stop, the last one is not counted, so there are 3
// taken out.
log->filterByTime(start, stop);
TS_ASSERT_EQUALS(log->realSize(), 3);
delete log;
}
//-------------------------------------------------------------------------------
void test_filterByTimes1() {
TimeSeriesProperty<int> *log = createIntegerTSP(6);
TS_ASSERT_EQUALS(log->realSize(), 6);
Mantid::Kernel::SplittingInterval interval0(DateAndTime("2007-11-30T16:17:10"), DateAndTime("2007-11-30T16:17:40"),
0);
Mantid::Kernel::TimeSplitterType splitters;
splitters.emplace_back(interval0);
// Since the filter is < stop, the last one is not counted, so there are 3
// taken out.
log->filterByTimes(splitters);
TS_ASSERT_EQUALS(log->realSize(), 3);
delete log;
}
void test_filterByTimesN() {
TimeSeriesProperty<int> *log = createIntegerTSP(10);
TS_ASSERT_EQUALS(log->realSize(), 10);
Mantid::Kernel::SplittingInterval interval0(DateAndTime("2007-11-30T16:17:10"), DateAndTime("2007-11-30T16:17:40"),
0);
Mantid::Kernel::SplittingInterval interval1(DateAndTime("2007-11-30T16:18:05"), DateAndTime("2007-11-30T16:18:25"),
0);
Mantid::Kernel::TimeSplitterType splitters;
splitters.emplace_back(interval0);
splitters.emplace_back(interval1);
// Since the filter is < stop, the last one is not counted, so there are 3
// taken out.
log->filterByTimes(splitters);
TS_ASSERT_EQUALS(log->realSize(), 6);
delete log;
}
//----------------------------------------------------------------------------
/// Ticket #2591
void test_filterByTime_ifOnlyOneValue_assumes_constant_instead() {
TimeSeriesProperty<int> *log = createIntegerTSP(1);
TS_ASSERT_EQUALS(log->realSize(), 1);
DateAndTime start = DateAndTime("2007-11-30T16:17:10");
DateAndTime stop = DateAndTime("2007-11-30T16:17:40");
log->filterByTime(start, stop);
// Still there!
TS_ASSERT_EQUALS(log->realSize(), 1);
delete log;
}
//----------------------------------------------------------------------------
/// Ticket #2591
void test_filterByTime_ifOnlyOneValue_assumes_constant_instead_2() {
TimeSeriesProperty<int> *log = new TimeSeriesProperty<int>("MyIntLog");
TS_ASSERT_THROWS_NOTHING(log->addValue("1990-01-01T00:00:00", 1));
TS_ASSERT_EQUALS(log->realSize(), 1);
DateAndTime start = DateAndTime("2007-11-30T16:17:10");
DateAndTime stop = DateAndTime("2007-11-30T16:17:40");
log->filterByTime(start, stop);
// Still there!
TS_ASSERT_EQUALS(log->realSize(), 1);
delete log;
}
//----------------------------------------------------------------------------
void test_makeFilterByValue() {
TimeSeriesProperty<double> *log = new TimeSeriesProperty<double>("MyIntLog");
TS_ASSERT_THROWS_NOTHING(log->addValue("2007-11-30T16:17:00", 1));
TS_ASSERT_THROWS_NOTHING(log->addValue("2007-11-30T16:17:10", 2));
TS_ASSERT_THROWS_NOTHING(log->addValue("2007-11-30T16:17:20", 3));
TS_ASSERT_THROWS_NOTHING(log->addValue("2007-11-30T16:17:30", 2.0));
TS_ASSERT_THROWS_NOTHING(log->addValue("2007-11-30T16:17:40", 2.01));
TS_ASSERT_THROWS_NOTHING(log->addValue("2007-11-30T16:17:50", 6));
TS_ASSERT_EQUALS(log->realSize(), 6);
// Test centred log value boundaries
TimeSplitterType splitter;
log->makeFilterByValue(splitter, 1.8, 2.2, 1.0, true);
TS_ASSERT_EQUALS(splitter.size(), 2);
SplittingInterval s;
DateAndTime t;
s = splitter[0];
t = DateAndTime("2007-11-30T16:17:09");
TS_ASSERT_DELTA(s.start(), t, 1e-3);
t = DateAndTime("2007-11-30T16:17:11");
TS_ASSERT_DELTA(s.stop(), t, 1e-3);
s = splitter[1];
t = DateAndTime("2007-11-30T16:17:29");
TS_ASSERT_DELTA(s.start(), t, 1e-3);
t = DateAndTime("2007-11-30T16:17:41");
TS_ASSERT_DELTA(s.stop(), t, 1e-3);
// Now test with left-aligned log value boundaries
log->makeFilterByValue(splitter, 1.8, 2.2, 1.0);
TS_ASSERT_EQUALS(splitter.size(), 2);
s = splitter[0];
t = DateAndTime("2007-11-30T16:17:10");
TS_ASSERT_DELTA(s.start(), t, 1e-3);
t = DateAndTime("2007-11-30T16:17:20");
TS_ASSERT_DELTA(s.stop(), t, 1e-3);
s = splitter[1];
t = DateAndTime("2007-11-30T16:17:30");
TS_ASSERT_DELTA(s.start(), t, 1e-3);
t = DateAndTime("2007-11-30T16:17:50");
TS_ASSERT_DELTA(s.stop(), t, 1e-3);
// Check throws if min > max
TS_ASSERT_THROWS(log->makeFilterByValue(splitter, 2.0, 1.0, 0.0, true), const std::invalid_argument &);
delete log;
}
void test_makeFilterByValue_throws_for_string_property() {
TimeSeriesProperty<std::string> log("StringTSP");
TimeSplitterType splitter;
TS_ASSERT_THROWS(log.makeFilterByValue(splitter, 0.0, 0.0, 0.0, true), const Exception::NotImplementedError &);
}
void test_expandFilterToRange() {
TimeSeriesProperty<int> log("MyIntLog");
TS_ASSERT_THROWS_NOTHING(log.addValue("2007-11-30T16:17:00", 1));
TS_ASSERT_THROWS_NOTHING(log.addValue("2007-11-30T16:17:10", 2));
TS_ASSERT_THROWS_NOTHING(log.addValue("2007-11-30T16:17:20", 3));
TS_ASSERT_THROWS_NOTHING(log.addValue("2007-11-30T16:17:30", 4));
TS_ASSERT_THROWS_NOTHING(log.addValue("2007-11-30T16:17:40", 6));
TS_ASSERT_THROWS_NOTHING(log.addValue("2007-11-30T16:17:50", 2));
// Create a TimeInterval that's wider than this log
TimeInterval interval(DateAndTime("2007-11-30T16:16:00"), DateAndTime("2007-11-30T16:18:50"));
TimeSplitterType splitter;
// Test good at both ends
log.makeFilterByValue(splitter, 1.0, 2.2, 1.0, false);
log.expandFilterToRange(splitter, 1.0, 2.2, interval);
TS_ASSERT_EQUALS(splitter.size(), 2);
TS_ASSERT_DELTA(splitter[0].start(), DateAndTime("2007-11-30T16:16:00"), 1e-3);
TS_ASSERT_DELTA(splitter[0].stop(), DateAndTime("2007-11-30T16:17:20"), 1e-3);
TS_ASSERT_DELTA(splitter[1].start(), DateAndTime("2007-11-30T16:17:50"), 1e-3);
TS_ASSERT_DELTA(splitter[1].stop(), DateAndTime("2007-11-30T16:18:50"), 1e-3);
// Test bad at both ends
log.makeFilterByValue(splitter, 2.5, 10.0, 0.0, false);
log.expandFilterToRange(splitter, 2.5, 10.0, interval);
TS_ASSERT_EQUALS(splitter.size(), 1);
TS_ASSERT_DELTA(splitter[0].start(), DateAndTime("2007-11-30T16:17:20"), 1e-3);
TS_ASSERT_DELTA(splitter[0].stop(), DateAndTime("2007-11-30T16:17:50"), 1e-3);
// Test good at start, bad at end
log.makeFilterByValue(splitter, -1.0, 1.5, 0.0, false);
log.expandFilterToRange(splitter, -1.0, 1.5, interval);
TS_ASSERT_EQUALS(splitter.size(), 1);
TS_ASSERT_DELTA(splitter[0].start(), DateAndTime("2007-11-30T16:16:00"), 1e-3);
TS_ASSERT_DELTA(splitter[0].stop(), DateAndTime("2007-11-30T16:17:10"), 1e-3);
// Test good at end, bad at start
log.makeFilterByValue(splitter, 1.99, 2.5, 1.0, false);
log.expandFilterToRange(splitter, 1.99, 2.5, interval);
TS_ASSERT_EQUALS(splitter.size(), 2);
TS_ASSERT_DELTA(splitter[0].start(), DateAndTime("2007-11-30T16:17:10"), 1e-3);
TS_ASSERT_DELTA(splitter[0].stop(), DateAndTime("2007-11-30T16:17:20"), 1e-3);
TS_ASSERT_DELTA(splitter[1].start(), DateAndTime("2007-11-30T16:17:50"), 1e-3);
TS_ASSERT_DELTA(splitter[1].stop(), DateAndTime("2007-11-30T16:18:50"), 1e-3);
// Check throws if min > max
TS_ASSERT_THROWS(log.expandFilterToRange(splitter, 2.0, 1.0, interval), const std::invalid_argument &);
// Test good at both ends, but interval narrower than log range
TimeInterval narrowinterval(DateAndTime("2007-11-30T16:17:15"), DateAndTime("2007-11-30T16:17:41"));
log.makeFilterByValue(splitter, 0.0, 10.0, 0.0, false);
log.expandFilterToRange(splitter, 0.0, 10.0, narrowinterval);
TS_ASSERT_EQUALS(splitter.size(), 1);
TS_ASSERT_DELTA(splitter[0].start(), DateAndTime("2007-11-30T16:17:00"), 1e-3);
TS_ASSERT_DELTA(splitter[0].stop(), DateAndTime("2007-11-30T16:17:50"), 1e-3);
}
void test_expandFilterToRange_throws_for_string_property() {
TimeSeriesProperty<std::string> log("StringTSP");
TimeSplitterType splitter;
TS_ASSERT_THROWS(log.expandFilterToRange(splitter, 0.0, 0.0, TimeInterval()),
const Exception::NotImplementedError &);
}
void test_averageValueInFilter() {
auto dblLog = createDoubleTSP();
auto intLog = createIntegerTSP(5);
// Test a filter that's fully within the range of both properties
TimeSplitterType filter;
filter.emplace_back(SplittingInterval(DateAndTime("2007-11-30T16:17:05"), DateAndTime("2007-11-30T16:17:29")));
TS_ASSERT_DELTA(dblLog->averageValueInFilter(filter), 7.308, 0.001);
TS_ASSERT_DELTA(intLog->averageValueInFilter(filter), 2.167, 0.001);
// Test a filter that starts before the log start time
filter[0] = SplittingInterval(DateAndTime("2007-11-30T16:16:30"), DateAndTime("2007-11-30T16:17:13"));
TS_ASSERT_DELTA(dblLog->averageValueInFilter(filter), 9.820, 0.001);
TS_ASSERT_DELTA(intLog->averageValueInFilter(filter), 1.070, 0.001);
// How about one that's entirely outside the log range (should just take the
// last value)
filter[0] = SplittingInterval(DateAndTime("2013-01-01T00:00:00"), DateAndTime("2013-01-01T01:00:00"));
TS_ASSERT_DELTA(dblLog->averageValueInFilter(filter), 10.55, 0.001);
TS_ASSERT_DELTA(intLog->averageValueInFilter(filter), 5.0, 0.001);
// Test a filter with two separate ranges, one of which goes past the end of
// the log
filter[0] = SplittingInterval(DateAndTime("2007-11-30T16:17:05"), DateAndTime("2007-11-30T16:17:15"));
filter.emplace_back(SplittingInterval(DateAndTime("2007-11-30T16:17:25"), DateAndTime("2007-11-30T16:17:45")));
TS_ASSERT_DELTA(dblLog->averageValueInFilter(filter), 9.123, 0.001);
TS_ASSERT_DELTA(intLog->averageValueInFilter(filter), 3.167, 0.001);
// Test a filter with two out of order ranges (the second one coming before
// the first)
// It should work fine.
filter[0] = filter[1];
filter[0] = SplittingInterval(DateAndTime("2007-11-30T16:17:05"), DateAndTime("2007-11-30T16:17:15"));
TS_ASSERT_DELTA(dblLog->averageValueInFilter(filter), 9.123, 0.001);
TS_ASSERT_DELTA(intLog->averageValueInFilter(filter), 3.167, 0.001);
// What about an overlap between the filters? It's odd, but it's allowed.
filter[0] = SplittingInterval(DateAndTime("2007-11-30T16:17:05"), DateAndTime("2007-11-30T16:17:15"));
filter[1] = SplittingInterval(DateAndTime("2007-11-30T16:17:10"), DateAndTime("2007-11-30T16:17:20"));
TS_ASSERT_DELTA(dblLog->averageValueInFilter(filter), 8.16, 0.001);
TS_ASSERT_DELTA(intLog->averageValueInFilter(filter), 1.75, 0.001);
// Check the correct behaviour of empty of single value logs.
TS_ASSERT(std::isnan(dProp->averageValueInFilter(filter)));
iProp->addValue(DateAndTime("2010-11-30T16:17:25"), 99);
TS_ASSERT_EQUALS(iProp->averageValueInFilter(filter), 99.0);
// Clean up
delete dblLog;
delete intLog;
}
void test_timeAverageValue() {
auto dblLog = createDoubleTSP();
auto intLog = createIntegerTSP(5);
// average values
const double dblMean = dblLog->timeAverageValue();
TS_ASSERT_DELTA(dblMean, 7.6966, .0001);
const double intMean = intLog->timeAverageValue();
TS_ASSERT_DELTA(intMean, 2.5, .0001);
// Clean up
delete dblLog;
delete intLog;
}
void test_averageValueInFilter_throws_for_string_property() {
TimeSplitterType splitter;
TS_ASSERT_THROWS(sProp->averageValueInFilter(splitter), const Exception::NotImplementedError &);
TS_ASSERT_THROWS(sProp->averageAndStdDevInFilter(splitter), const Exception::NotImplementedError &);
}
//----------------------------------------------------------------------------
void test_splitByTime_and_getTotalValue() {
TimeSeriesProperty<int> *log = createIntegerTSP(12);
// Make the outputs
std::vector<Property *> outputs;
for (std::size_t i = 0; i < 5; i++) {
TimeSeriesProperty<int> *newlog = new TimeSeriesProperty<int>("MyIntLog");
outputs.emplace_back(newlog);
}
// Make a splitter
DateAndTime start, stop;
TimeSplitterType splitter;
start = DateAndTime("2007-11-30T16:17:10");
stop = DateAndTime("2007-11-30T16:17:40");
splitter.emplace_back(SplittingInterval(start, stop, 0));
start = DateAndTime("2007-11-30T16:17:55");
stop = DateAndTime("2007-11-30T16:17:56");
splitter.emplace_back(SplittingInterval(start, stop, 1));
start = DateAndTime("2007-11-30T16:17:56");
stop = DateAndTime("2007-11-30T16:18:01");
splitter.emplace_back(SplittingInterval(start, stop, 2)); // just one entry
start = DateAndTime("2007-11-30T16:18:09");
stop = DateAndTime("2007-11-30T16:18:21");
splitter.emplace_back(SplittingInterval(start, stop, 3));
start = DateAndTime("2007-11-30T16:18:45");
stop = DateAndTime("2007-11-30T16:22:50");
splitter.emplace_back(SplittingInterval(start, stop, 4));
log->splitByTime(splitter, outputs, false);
TS_ASSERT_EQUALS(dynamic_cast<TimeSeriesProperty<int> *>(outputs[0])->realSize(), 3);
TS_ASSERT_EQUALS(dynamic_cast<TimeSeriesProperty<int> *>(outputs[1])->realSize(), 1);
TS_ASSERT_EQUALS(dynamic_cast<TimeSeriesProperty<int> *>(outputs[2])->realSize(), 2);
TS_ASSERT_EQUALS(dynamic_cast<TimeSeriesProperty<int> *>(outputs[3])->realSize(), 3);
TS_ASSERT_EQUALS(dynamic_cast<TimeSeriesProperty<int> *>(outputs[4])->realSize(), 2);
delete log;
delete outputs[0];
delete outputs[1];
delete outputs[2];
delete outputs[3];
delete outputs[4];
}
//----------------------------------------------------------------------------
void test_splitByTime_withOverlap() {
TimeSeriesProperty<int> *log = createIntegerTSP(12);
// Make the outputs
std::vector<Property *> outputs;
for (std::size_t i = 0; i < 1; i++) {
TimeSeriesProperty<int> *newlog = new TimeSeriesProperty<int>("MyIntLog");
outputs.emplace_back(newlog);
}
// Make a splitter
DateAndTime start, stop;
TimeSplitterType splitter;
start = DateAndTime("2007-11-30T16:17:10");
stop = DateAndTime("2007-11-30T16:17:40");
splitter.emplace_back(SplittingInterval(start, stop, 0));
start = DateAndTime("2007-11-30T16:17:35");
stop = DateAndTime("2007-11-30T16:17:59");
splitter.emplace_back(SplittingInterval(start, stop, 0));
log->splitByTime(splitter, outputs, false);
TS_ASSERT_EQUALS(dynamic_cast<TimeSeriesProperty<int> *>(outputs[0])->realSize(), 5);
delete log;
delete outputs[0];
}
//----------------------------------------------------------------------------
/**
* otuput 0 has entries: 3
* otuput 1 has entries: 5
* otuput 2 has entries: 2
* otuput 3 has entries: 7
* @brief test_splitByTimeVector
*/
void test_splitByTimeVector() {
// create the splitters
std::vector<DateAndTime> split_time_vec;
split_time_vec.emplace_back(DateAndTime("2007-11-30T16:17:10"));
split_time_vec.emplace_back(DateAndTime("2007-11-30T16:17:40"));
split_time_vec.emplace_back(DateAndTime("2007-11-30T16:17:55"));
split_time_vec.emplace_back(DateAndTime("2007-11-30T16:17:56"));
split_time_vec.emplace_back(DateAndTime("2007-11-30T16:18:09"));
split_time_vec.emplace_back(DateAndTime("2007-11-30T16:18:45"));
split_time_vec.emplace_back(DateAndTime("2007-11-30T16:22:50"));
std::vector<int> split_target_vec;
split_target_vec.emplace_back(1);
split_target_vec.emplace_back(0);
split_target_vec.emplace_back(2);
split_target_vec.emplace_back(0);
split_target_vec.emplace_back(1);
split_target_vec.emplace_back(3);
TimeSeriesProperty<int> log("test log");
log.addValue(DateAndTime("2007-11-30T16:17:00"), 1);
log.addValue(DateAndTime("2007-11-30T16:17:30"), 2);
log.addValue(DateAndTime("2007-11-30T16:18:00"), 3);
log.addValue(DateAndTime("2007-11-30T16:18:30"), 4);
log.addValue(DateAndTime("2007-11-30T16:19:00"), 5);
log.addValue(DateAndTime("2007-11-30T16:19:30"), 6);
log.addValue(DateAndTime("2007-11-30T16:20:00"), 7);
log.addValue(DateAndTime("2007-11-30T16:20:30"), 8);
log.addValue(DateAndTime("2007-11-30T16:21:00"), 9);
log.addValue(DateAndTime("2007-11-30T16:21:30"), 10);
std::vector<TimeSeriesProperty<int> *> outputs;
for (int itarget = 0; itarget < 4; ++itarget) {
TimeSeriesProperty<int> *tsp = new TimeSeriesProperty<int>("target");
outputs.emplace_back(tsp);
}
log.splitByTimeVector(split_time_vec, split_target_vec, outputs);
// Exam the split entries
TimeSeriesProperty<int> *out_0 = outputs[0];
// FIXME - Check whether out_0 is correct!
TS_ASSERT_EQUALS(out_0->size(), 3);
TS_ASSERT_EQUALS(out_0->nthValue(0), 2);
TS_ASSERT_EQUALS(out_0->nthValue(1), 3);
TS_ASSERT_EQUALS(out_0->nthValue(2), 4);
TimeSeriesProperty<int> *out_1 = outputs[1];
TS_ASSERT_EQUALS(out_1->size(), 5);
TS_ASSERT_EQUALS(out_1->nthValue(0), 1);
TS_ASSERT_EQUALS(out_1->nthValue(1), 2);
TS_ASSERT_EQUALS(out_1->nthValue(2), 3);
TS_ASSERT_EQUALS(out_1->nthValue(3), 4);
TS_ASSERT_EQUALS(out_1->nthValue(4), 5);
TimeSeriesProperty<int> *out_2 = outputs[2];
TS_ASSERT_EQUALS(out_2->size(), 2);
TS_ASSERT_EQUALS(out_2->nthValue(0), 2);
TS_ASSERT_EQUALS(out_2->nthValue(1), 3);
TimeSeriesProperty<int> *out_3 = outputs[3];
TS_ASSERT_EQUALS(out_3->size(), 7);
// out[3] should have entries: 4, 5, 6, 7, 8, 9, 10
for (int j = 0; j < out_3->size(); ++j) {
TS_ASSERT_EQUALS(out_3->nthValue(j), j + 4);
}
for (auto outputPtr : outputs) {
delete outputPtr;
}
}
//----------------------------------------------------------------------------
/** last splitter is before first entry
* @brief test_splitByTimeVectorEarlySplitter
*/
void test_splitByTimeVectorEarlySplitter() {
// create the splitters
std::vector<DateAndTime> split_time_vec;
split_time_vec.emplace_back(DateAndTime("2007-11-30T16:00:10"));
split_time_vec.emplace_back(DateAndTime("2007-11-30T16:00:40"));
split_time_vec.emplace_back(DateAndTime("2007-11-30T16:07:55"));
split_time_vec.emplace_back(DateAndTime("2007-11-30T16:07:56"));
split_time_vec.emplace_back(DateAndTime("2007-11-30T16:08:09"));
split_time_vec.emplace_back(DateAndTime("2007-11-30T16:08:45"));
split_time_vec.emplace_back(DateAndTime("2007-11-30T16:12:50"));
std::vector<int> split_target_vec;
split_target_vec.emplace_back(1);
split_target_vec.emplace_back(0);
split_target_vec.emplace_back(2);
split_target_vec.emplace_back(0);
split_target_vec.emplace_back(1);
split_target_vec.emplace_back(3);
TimeSeriesProperty<int> log("test log");
log.addValue(DateAndTime("2007-11-30T16:17:00"), 1);
log.addValue(DateAndTime("2007-11-30T16:17:30"), 2);
log.addValue(DateAndTime("2007-11-30T16:18:00"), 3);
log.addValue(DateAndTime("2007-11-30T16:18:30"), 4);
log.addValue(DateAndTime("2007-11-30T16:19:00"), 5);
log.addValue(DateAndTime("2007-11-30T16:19:30"), 6);
log.addValue(DateAndTime("2007-11-30T16:20:00"), 7);
log.addValue(DateAndTime("2007-11-30T16:20:30"), 8);
log.addValue(DateAndTime("2007-11-30T16:21:00"), 9);
log.addValue(DateAndTime("2007-11-30T16:21:30"), 10);
// Initialze the 4 splitters
std::vector<TimeSeriesProperty<int> *> outputs;
for (int itarget = 0; itarget < 4; ++itarget) {
outputs.emplace_back(new TimeSeriesProperty<int>("target"));
}
log.splitByTimeVector(split_time_vec, split_target_vec, outputs);
// check
for (int i = 0; i < 4; ++i) {
TimeSeriesProperty<int> *out_i = outputs[i];
TS_ASSERT_EQUALS(out_i->size(), 0);
delete out_i;
outputs[i] = nullptr;
}
return;
}
//----------------------------------------------------------------------------
/** first splitter is after last entry
* @brief test_splitByTimeVectorLaterSplitter
*/
void test_splitByTimeVectorLaterSplitter() {
// create the splitters
std::vector<DateAndTime> split_time_vec;
split_time_vec.emplace_back(DateAndTime("2007-12-30T16:00:10"));
split_time_vec.emplace_back(DateAndTime("2007-12-30T16:00:40"));
split_time_vec.emplace_back(DateAndTime("2007-12-30T16:07:55"));
split_time_vec.emplace_back(DateAndTime("2007-12-30T16:07:56"));
split_time_vec.emplace_back(DateAndTime("2007-12-30T16:08:09"));
split_time_vec.emplace_back(DateAndTime("2007-12-30T16:08:45"));
split_time_vec.emplace_back(DateAndTime("2007-12-30T16:12:50"));
std::vector<int> split_target_vec;
split_target_vec.emplace_back(1);
split_target_vec.emplace_back(0);
split_target_vec.emplace_back(2);
split_target_vec.emplace_back(0);
split_target_vec.emplace_back(1);
split_target_vec.emplace_back(3);
// create test log
TimeSeriesProperty<int> log("test log");
log.addValue(DateAndTime("2007-11-30T16:17:00"), 1);
log.addValue(DateAndTime("2007-11-30T16:17:30"), 2);
log.addValue(DateAndTime("2007-11-30T16:18:00"), 3);
log.addValue(DateAndTime("2007-11-30T16:18:30"), 4);
log.addValue(DateAndTime("2007-11-30T16:19:00"), 5);
log.addValue(DateAndTime("2007-11-30T16:19:30"), 6);
log.addValue(DateAndTime("2007-11-30T16:20:00"), 7);
log.addValue(DateAndTime("2007-11-30T16:20:30"), 8);
log.addValue(DateAndTime("2007-11-30T16:21:00"), 9);
log.addValue(DateAndTime("2007-11-30T16:21:30"), 10);
// Initialze the 4 splitters
std::vector<TimeSeriesProperty<int> *> outputs;
for (int itarget = 0; itarget < 4; ++itarget) {
outputs.emplace_back(new TimeSeriesProperty<int>("target"));
}
log.splitByTimeVector(split_time_vec, split_target_vec, outputs);
// check
for (int i = 0; i < 4; ++i) {
TimeSeriesProperty<int> *out_i = outputs[i];
TS_ASSERT_EQUALS(out_i->size(), 1);
delete out_i;
outputs[i] = nullptr;
}
}
//----------------------------------------------------------------------------
/** high-frequency splitters splits a slow change log
* @brief test_splitByTimeVectorFastLogSplitter
*/
void test_splitByTimeVectorFastLogSplitter() {
// create test log
TimeSeriesProperty<int> log("test log");
log.addValue(DateAndTime("2007-11-30T16:17:00"), 1);
log.addValue(DateAndTime("2007-11-30T16:17:30"), 2);
log.addValue(DateAndTime("2007-11-30T16:18:00"), 3);
log.addValue(DateAndTime("2007-11-30T16:18:30"), 4);
log.addValue(DateAndTime("2007-11-30T16:19:00"), 5);
log.addValue(DateAndTime("2007-11-30T16:19:30"), 6);
log.addValue(DateAndTime("2007-11-30T16:20:00"), 7);
log.addValue(DateAndTime("2007-11-30T16:20:30"), 8);
log.addValue(DateAndTime("2007-11-30T16:21:00"), 9);
log.addValue(DateAndTime("2007-11-30T16:21:30"), 10);
// create a high frequency splitter
DateAndTime split_time("2007-11-30T16:17:00");
int64_t dt = 100 * 1000;
std::vector<DateAndTime> vec_split_times;
std::vector<int> vec_split_target;
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 10; ++j) {
vec_split_times.emplace_back(split_time);
split_time += dt;
vec_split_target.emplace_back(j);
}
}
// push back last split-time (split stop)
vec_split_times.emplace_back(split_time);
// Initialze the 10 splitters
std::vector<TimeSeriesProperty<int> *> outputs;
for (int itarget = 0; itarget < 10; ++itarget) {
outputs.emplace_back(new TimeSeriesProperty<int>("target"));
}
// split time series property
log.splitByTimeVector(vec_split_times, vec_split_target, outputs);
// test
for (auto &it : outputs) {
TS_ASSERT_EQUALS(it->size(), 2);
delete it;
it = nullptr;
}
}
//----------------------------------------------------------------------------
/** Extreme case 1: the last entry of time series property is before the first
* splitter. The test case is extracted from issue #21836, in which
* the last entry is before the first splitter
* @brief test_SplitByTimeExtremeCase1.
*/
void test_SplitByTimeExtremeCase1() {
// create test log
TimeSeriesProperty<int> int_log("test int log 21836");
int_log.addValue(DateAndTime("2017-11-10T03:12:06"), 1);
int_log.addValue(DateAndTime("2017-11-10T03:12:31"), 3);
int_log.addValue(DateAndTime("2017-11-10T03:12:40"), 2);
TimeSeriesProperty<double> dbl_log("test double log 21836");
dbl_log.addValue(DateAndTime("2017-11-10T03:12:06"), 1.0);
dbl_log.addValue(DateAndTime("2017-11-10T03:12:31"), 3.0);
dbl_log.addValue(DateAndTime("2017-11-10T03:12:40"), 2.0);
// create the splitters
std::vector<DateAndTime> split_time_vec;
split_time_vec.emplace_back(DateAndTime("2017-11-10T03:13:06.814538624"));
split_time_vec.emplace_back(DateAndTime("2017-11-10T03:14:07.764311936"));
split_time_vec.emplace_back(DateAndTime("2017-11-10T03:15:07.697312000"));
split_time_vec.emplace_back(DateAndTime("2017-11-10T03:16:08.827971840"));
split_time_vec.emplace_back(DateAndTime("2017-11-10T03:17:08.745746688"));
split_time_vec.emplace_back(DateAndTime("2017-11-10T03:20:10.757950208"));
// create the target vector
std::vector<int> split_target_vec(5);
for (size_t i = 0; i < 5; ++i) {
split_target_vec[i] = (i + 1) % 2;