##// END OF EJS Templates
More VC2 tests refactoring...
jeandet -
r5:865d00547f1a
parent child
Show More
@@ -0,0 +1,1
1 #include "TestProviders.h"
@@ -0,0 +1,94
1 #ifndef TESTPROVIDER_H
2 #define TESTPROVIDER_H
3
4 #include <memory>
5 #include <cmath>
6 #include <algorithm>
7 #include <numeric>
8
9 #include <QUuid>
10 #include <QtGlobal>
11 #include <QtTest>
12
13 #include <Data/IDataProvider.h>
14 #include <Data/DataProviderParameters.h>
15 #include <Data/DataSeries.h>
16 #include <Data/ScalarSeries.h>
17 #include <Variable/Variable.h>
18
19
20 template<int slope>
21 class SimpleRange: public IDataProvider
22 {
23 public:
24 SimpleRange() = default;
25
26 std::shared_ptr<IDataProvider> clone() const override{ return std::make_shared<SimpleRange>(); }
27
28 IDataSeries* getData(const DataProviderParameters &parameters) override
29 {
30 auto tstart = parameters.m_Times[0].m_TStart;
31 auto tend = parameters.m_Times[0].m_TEnd;
32 std::vector<double> x;
33 std::vector<double> y;
34 for(double i = ceil(tstart);i<=floor(tend);i+=1.) //1 seconde data resolution
35 {
36 x.push_back(i);
37 y.push_back(i*slope);
38 }
39 auto serie = new ScalarSeries(std::move(x),std::move(y),Unit("Secondes",true),Unit("Volts",false));
40 return serie;
41 }
42
43
44
45 void requestDataLoading(QUuid acqIdentifier, const DataProviderParameters &parameters) override
46 {
47 Q_UNUSED(acqIdentifier)
48 Q_UNUSED(parameters)
49 }
50
51 void requestDataAborting(QUuid acqIdentifier) override
52 {
53 Q_UNUSED(acqIdentifier)
54 }
55
56 };
57
58
59 template <class T>
60 auto sumdiff(T begin, T end)
61 {
62 std::vector<double> diff_vect(end-begin-1);
63 auto diff = [](auto next,auto item)
64 {
65 return next.value() - item.value();
66 };
67 std::transform (begin+1, end, begin, diff_vect.begin(),diff);
68 return std::accumulate(diff_vect.cbegin(), diff_vect.cend(), 0);
69 }
70
71 template <int slope=1>
72 struct RangeType
73 {
74 static void check_properties(std::shared_ptr<Variable> v, DateTimeRange r)
75 {
76 auto bounds = v->dataSeries()->valuesBounds(r.m_TStart, r.m_TEnd);
77 auto s = sumdiff(bounds.first, bounds.second) / slope;
78 auto nbpoints = bounds.second - bounds.first+1.;
79 QCOMPARE(nbpoints, int(s)+2);//<- @TODO weird has to be investigated why +2?
80 QCOMPARE(bounds.first->x(), bounds.first->value()/slope);
81 }
82 };
83
84 template <class T>
85 void check_variable_state(std::shared_ptr<Variable> v, DateTimeRange r)
86 {
87 auto bounds = v->dataSeries()->valuesBounds(r.m_TStart, r.m_TEnd);
88 //generated data has to be inside range
89 QVERIFY(bounds.first->x() >= r.m_TStart);
90 QVERIFY(bounds.second->x() <= r.m_TEnd);
91 T::check_properties(v,r);
92 }
93
94 #endif
@@ -1,39 +1,46
1 1 add_definitions(-DCORE_TESTS_RESOURCES_DIR="${CMAKE_CURRENT_LIST_DIR}/../tests-resources")
2 2
3 FILE (GLOB_RECURSE TestUtilsSources
4 TestUtils/TestProviders.h
5 TestUtils/TestProviders.cpp
6 )
7
8 add_library(TestUtils ${TestUtilsSources})
9 target_link_libraries(TestUtils sciqlopcore Qt5::Test)
3 10
4 11 declare_test(TestStringUtils TestStringUtils Common/TestStringUtils.cpp "sciqlopcore;Qt5::Test")
5 12
6 13 declare_test(TestContainers TestContainers Common/TestContainers.cpp "sciqlopcore;Qt5::Test")
7 14 declare_test(TestSyncGroup TestSyncGroup Variable/TestSyncGroup.cpp "sciqlopcore;Qt5::Test")
8 15
9 16
10 17 declare_test(TestDataSeriesUtils TestDataSeriesUtils Data/TestDataSeriesUtils.cpp "sciqlopcore;Qt5::Test")
11 18 declare_test(TestOptionalAxis TestOptionalAxis Data/TestOptionalAxis.cpp "sciqlopcore;Qt5::Test")
12 19 declare_test(TestSpectrogramSeries TestSpectrogramSeries
13 20 "Data/TestSpectrogramSeries.cpp;Data/DataSeriesBuilders.h;Data/DataSeriesBuilders.cpp;Data/DataSeriesTestsUtils.h;Data/DataSeriesTestsUtils.cpp"
14 21 "sciqlopcore;Qt5::Test")
15 22 declare_test(TestOneDimArrayData TestOneDimArrayData Data/TestOneDimArrayData.cpp "sciqlopcore;Qt5::Test")
16 23 declare_test(TestScalarSeries TestScalarSeries
17 24 "Data/TestScalarSeries.cpp;Data/DataSeriesBuilders.h;Data/DataSeriesBuilders.cpp;Data/DataSeriesTestsUtils.h;Data/DataSeriesTestsUtils.cpp"
18 25 "sciqlopcore;Qt5::Test")
19 26 declare_test(TestTwoDimArrayData TestTwoDimArrayData Data/TestTwoDimArrayData.cpp "sciqlopcore;Qt5::Test")
20 27 declare_test(TestVectorSeries TestVectorSeries
21 28 "Data/TestVectorSeries.cpp;Data/DataSeriesBuilders.h;Data/DataSeriesBuilders.cpp;Data/DataSeriesTestsUtils.h;Data/DataSeriesTestsUtils.cpp"
22 29 "sciqlopcore;Qt5::Test")
23 30
24 31 declare_test(TestDataSourceController TestDataSourceController
25 32 "DataSource/TestDataSourceController.cpp;DataSource/DataSourceItemBuilder.cpp"
26 33 "sciqlopcore;Qt5::Test")
27 34 declare_test(TestDataSourceItem TestDataSourceItem
28 35 "DataSource/TestDataSourceItem.cpp;DataSource/DataSourceItemBuilder.cpp"
29 36 "sciqlopcore;Qt5::Test")
30 37
31 38 declare_test(TestVariable TestVariable Variable/TestVariable.cpp "sciqlopcore;Qt5::Test")
32 39 declare_test(TestVariableCacheController TestVariableCacheController Variable/TestVariableCacheController.cpp "sciqlopcore;Qt5::Test")
33 40 declare_test(TestVariableController TestVariableController Variable/TestVariableController.cpp "sciqlopcore;Qt5::Test")
34 41 declare_test(TestVariableSync TestVariableSync Variable/TestVariableSync.cpp "sciqlopcore;Qt5::Test")
35 42
36 43 declare_test(TestDownloader TestDownloader Network/TestDownloader.cpp "sciqlopcore;Qt5::Test")
37 44
38 45
39 declare_test(TestVariableController2 TestVariableController2 Variable/TestVariableController2.cpp "sciqlopcore;Qt5::Test")
46 declare_test(TestVariableController2 TestVariableController2 Variable/TestVariableController2.cpp "sciqlopcore;TestUtils;Qt5::Test")
@@ -1,170 +1,113
1 1 #include <cmath>
2 2 #include <algorithm>
3 3 #include <numeric>
4 4 #include <QtTest>
5 5 #include <QObject>
6 6 #include <Variable/VariableController2.h>
7 7 #include <Data/DateTimeRange.h>
8 8 #include <Data/IDataProvider.h>
9 9 #include <Data/ScalarSeries.h>
10 10 #include <Data/DataProviderParameters.h>
11 11 #include <Common/containers.h>
12 12
13 template<int slope>
14 class SimpleRange: public IDataProvider
15 {
16 public:
17 SimpleRange() = default;
18
19 std::shared_ptr<IDataProvider> clone() const override{ return std::make_shared<SimpleRange>(); }
13 #include <TestUtils/TestProviders.h>
20 14
21 IDataSeries* getData(const DataProviderParameters &parameters) override
22 {
23 auto tstart = parameters.m_Times[0].m_TStart;
24 auto tend = parameters.m_Times[0].m_TEnd;
25 std::vector<double> x;
26 std::vector<double> y;
27 for(auto i = tstart;i<tend;i+=1.) //1 seconde data resolution
28 {
29 x.push_back(i);
30 y.push_back(i*slope);
31 }
32 auto serie = new ScalarSeries(std::move(x),std::move(y),Unit("Secondes",true),Unit("Volts",false));
33 return serie;
34 }
15 #define TEST_VC2_FIXTURE(slope) \
16 VariableController2 vc; \
17 auto provider = std::make_shared<SimpleRange<slope>>();\
35 18
19 #define TEST_VC2_CREATE_DEFAULT_VAR(name)\
20 auto range = DateTimeRange::fromDateTime(QDate(2018,8,7),QTime(14,00),\
21 QDate(2018,8,7),QTime(16,00));\
22 auto name = vc.createVariable("name", {}, provider, range);\
36 23
37 24
38 void requestDataLoading(QUuid acqIdentifier, const DataProviderParameters &parameters) override
39 {
40 Q_UNUSED(acqIdentifier)
41 Q_UNUSED(parameters)
42 }
43
44 void requestDataAborting(QUuid acqIdentifier) override
45 {
46 Q_UNUSED(acqIdentifier)
47 }
48
49 };
50
51
52 template <class T>
53 auto sumdiff(T begin, T end)
54 {
55 std::vector<double> diff_vect(end-begin-1);
56 auto diff = [](auto next,auto item)
57 {
58 return next.value() - item.value();
59 };
60 std::transform (begin+1, end, begin, diff_vect.begin(),diff);
61 return std::accumulate(diff_vect.cbegin(), diff_vect.cend(), 0);
62 }
63
64 template <int slope=1>
65 struct RangeType
66 {
67 static void check_properties(std::shared_ptr<Variable> v, DateTimeRange r)
68 {
69 auto bounds = v->dataSeries()->valuesBounds(r.m_TStart, r.m_TEnd);
70 auto s = sumdiff(bounds.first, bounds.second) / slope;
71 auto nbpoints = bounds.second - bounds.first+1.;
72 QCOMPARE(nbpoints, int(s)+2);//<- @TODO weird has to be investigated why +2?
73 QCOMPARE(r.m_TStart, bounds.first->value()/slope);
74 }
75 };
76
77 template <class T>
78 void check_variable_state(std::shared_ptr<Variable> v, DateTimeRange r)
79 {
80 auto bounds = v->dataSeries()->valuesBounds(r.m_TStart, r.m_TEnd);
81 auto nbpoints = bounds.second - bounds.first+1.;
82 QCOMPARE(nbpoints, int(static_cast<double>(r.delta())));
83 T::check_properties(v,r);
84 }
85
86 25 class TestVariableController2 : public QObject
87 26 {
88 27 Q_OBJECT
89 28 public:
90 29 explicit TestVariableController2(QObject *parent = nullptr) : QObject(parent){}
91 30 signals:
92 31
93 32 private slots:
94 33 void initTestCase(){}
95 34 void cleanupTestCase(){}
96 35
97 36 void testCreateVariable()
98 37 {
99 VariableController2 vc;
38 TEST_VC2_FIXTURE(2);
100 39 bool callbackCalled = false;
101 40 connect(&vc,&VariableController2::variableAdded, [&callbackCalled](std::shared_ptr<Variable>){callbackCalled=true;});
102 auto provider = std::make_shared<SimpleRange<1>>();
103 41 QVERIFY(!callbackCalled);
104 42 auto var1 = vc.createVariable("var1",{},provider,DateTimeRange());
105 43 QVERIFY(SciQLop::containers::contains(vc.variables(), var1));
106 44 QVERIFY(callbackCalled);
107 45 }
108 46
109 47 void testDeleteVariable()
110 48 {
111 VariableController2 vc;
49 TEST_VC2_FIXTURE(1);
112 50 bool callbackCalled = false;
113 51 connect(&vc,&VariableController2::variableDeleted, [&callbackCalled](std::shared_ptr<Variable>){callbackCalled=true;});
114 auto provider = std::make_shared<SimpleRange<1>>();
115 52 auto var1 = vc.createVariable("var1",{},provider,DateTimeRange());
116 53 QVERIFY(SciQLop::containers::contains(vc.variables(), var1));
117 54 QVERIFY(!callbackCalled);
118 55 vc.deleteVariable(var1);
119 56 QVERIFY(!SciQLop::containers::contains(vc.variables(), var1));
120 57 QVERIFY(callbackCalled);
121 58 }
122 59
123 60 void testGetData()
124 61 {
125 VariableController2 vc;
126 auto provider = std::make_shared<SimpleRange<10>>();
127 auto range = DateTimeRange::fromDateTime(QDate(2018,8,7),QTime(14,00),
128 QDate(2018,8,7),QTime(16,00));
129
130 auto var1 = vc.createVariable("var1", {}, provider, range);
62 TEST_VC2_FIXTURE(10);
63 TEST_VC2_CREATE_DEFAULT_VAR(var1);
131 64 check_variable_state<RangeType<10>>(var1, range);
132 65 }
133 66
134 void testZoomOut()
67 void testZoom_data()
135 68 {
136 VariableController2 vc;
137 auto provider = std::make_shared<SimpleRange<10>>();
138 auto range = DateTimeRange::fromDateTime(QDate(2018,8,7),QTime(14,00),
139 QDate(2018,8,7),QTime(16,00));
140
141 auto var1 = vc.createVariable("var1", {}, provider, range);
142 check_variable_state<RangeType<10>>(var1, range);
69 QTest::addColumn<double>("zoom");
70 QTest::newRow("Zoom IN 10x") << .1;
71 QTest::newRow("Zoom OUT 10x") << 10.;
72 QTest::newRow("Zoom IN 1x") << 1.;
73 }
74 void testZoom()
75 {
76 TEST_VC2_FIXTURE(100);
77 TEST_VC2_CREATE_DEFAULT_VAR(var1);
78 check_variable_state<RangeType<100>>(var1, range);
143 79
144 range *=2.;
80 QFETCH(double, zoom);
81 range *=zoom;
145 82 vc.changeRange(var1, range);
146 check_variable_state<RangeType<10>>(var1, range);
83 check_variable_state<RangeType<100>>(var1, range);
147 84 }
148 85
149 void testPanRight()
86 void testPan_data()
150 87 {
151 VariableController2 vc;
152 auto provider = std::make_shared<SimpleRange<10>>();
153 auto range = DateTimeRange::fromDateTime(QDate(2018,8,7),QTime(14,00),
154 QDate(2018,8,7),QTime(16,00));
155
156 auto var1 = vc.createVariable("var1", {}, provider, range);
88 QTest::addColumn<double>("pan");
89 QTest::newRow("Right 1000 seconds") << 1000.;
90 QTest::newRow("Left 1000 seconds") << -1000.;
91 QTest::newRow("Right 0.1 seconds") << .1;
92 QTest::newRow("Left 0.1 seconds") << -.1;
93 }
94 void testPan()
95 {
96 TEST_VC2_FIXTURE(10);
97 TEST_VC2_CREATE_DEFAULT_VAR(var1);
157 98 check_variable_state<RangeType<10>>(var1, range);
158 99
159 range += Seconds<double>{1000.};
100 QFETCH(double, pan);
101
102 range += Seconds<double>{pan};
160 103 vc.changeRange(var1, range);
161 104 check_variable_state<RangeType<10>>(var1, range);
162 105 }
163 106
164 107 };
165 108
166 109
167 110 QTEST_MAIN(TestVariableController2)
168 111
169 112 #include "TestVariableController2.moc"
170 113
General Comments 0
You need to be logged in to leave comments. Login now