@@ -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 ¶meters) 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 ¶meters) 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,5 +1,12 | |||||
1 | add_definitions(-DCORE_TESTS_RESOURCES_DIR="${CMAKE_CURRENT_LIST_DIR}/../tests-resources") |
|
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 | declare_test(TestStringUtils TestStringUtils Common/TestStringUtils.cpp "sciqlopcore;Qt5::Test") |
|
11 | declare_test(TestStringUtils TestStringUtils Common/TestStringUtils.cpp "sciqlopcore;Qt5::Test") | |
5 |
|
12 | |||
@@ -36,4 +43,4 declare_test(TestVariableSync TestVariableSync Variable/TestVariableSync.cpp "sc | |||||
36 | declare_test(TestDownloader TestDownloader Network/TestDownloader.cpp "sciqlopcore;Qt5::Test") |
|
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") |
@@ -10,79 +10,18 | |||||
10 | #include <Data/DataProviderParameters.h> |
|
10 | #include <Data/DataProviderParameters.h> | |
11 | #include <Common/containers.h> |
|
11 | #include <Common/containers.h> | |
12 |
|
12 | |||
13 | template<int slope> |
|
13 | #include <TestUtils/TestProviders.h> | |
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>(); } |
|
|||
20 |
|
14 | |||
21 | IDataSeries* getData(const DataProviderParameters ¶meters) override |
|
15 | #define TEST_VC2_FIXTURE(slope) \ | |
22 | { |
|
16 | VariableController2 vc; \ | |
23 | auto tstart = parameters.m_Times[0].m_TStart; |
|
17 | auto provider = std::make_shared<SimpleRange<slope>>();\ | |
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 | } |
|
|||
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 ¶meters) 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 | class TestVariableController2 : public QObject |
|
25 | class TestVariableController2 : public QObject | |
87 | { |
|
26 | { | |
88 | Q_OBJECT |
|
27 | Q_OBJECT | |
@@ -96,10 +35,9 private slots: | |||||
96 |
|
35 | |||
97 | void testCreateVariable() |
|
36 | void testCreateVariable() | |
98 | { |
|
37 | { | |
99 | VariableController2 vc; |
|
38 | TEST_VC2_FIXTURE(2); | |
100 | bool callbackCalled = false; |
|
39 | bool callbackCalled = false; | |
101 | connect(&vc,&VariableController2::variableAdded, [&callbackCalled](std::shared_ptr<Variable>){callbackCalled=true;}); |
|
40 | connect(&vc,&VariableController2::variableAdded, [&callbackCalled](std::shared_ptr<Variable>){callbackCalled=true;}); | |
102 | auto provider = std::make_shared<SimpleRange<1>>(); |
|
|||
103 | QVERIFY(!callbackCalled); |
|
41 | QVERIFY(!callbackCalled); | |
104 | auto var1 = vc.createVariable("var1",{},provider,DateTimeRange()); |
|
42 | auto var1 = vc.createVariable("var1",{},provider,DateTimeRange()); | |
105 | QVERIFY(SciQLop::containers::contains(vc.variables(), var1)); |
|
43 | QVERIFY(SciQLop::containers::contains(vc.variables(), var1)); | |
@@ -108,10 +46,9 private slots: | |||||
108 |
|
46 | |||
109 | void testDeleteVariable() |
|
47 | void testDeleteVariable() | |
110 | { |
|
48 | { | |
111 | VariableController2 vc; |
|
49 | TEST_VC2_FIXTURE(1); | |
112 | bool callbackCalled = false; |
|
50 | bool callbackCalled = false; | |
113 | connect(&vc,&VariableController2::variableDeleted, [&callbackCalled](std::shared_ptr<Variable>){callbackCalled=true;}); |
|
51 | connect(&vc,&VariableController2::variableDeleted, [&callbackCalled](std::shared_ptr<Variable>){callbackCalled=true;}); | |
114 | auto provider = std::make_shared<SimpleRange<1>>(); |
|
|||
115 | auto var1 = vc.createVariable("var1",{},provider,DateTimeRange()); |
|
52 | auto var1 = vc.createVariable("var1",{},provider,DateTimeRange()); | |
116 | QVERIFY(SciQLop::containers::contains(vc.variables(), var1)); |
|
53 | QVERIFY(SciQLop::containers::contains(vc.variables(), var1)); | |
117 | QVERIFY(!callbackCalled); |
|
54 | QVERIFY(!callbackCalled); | |
@@ -122,41 +59,47 private slots: | |||||
122 |
|
59 | |||
123 | void testGetData() |
|
60 | void testGetData() | |
124 | { |
|
61 | { | |
125 | VariableController2 vc; |
|
62 | TEST_VC2_FIXTURE(10); | |
126 | auto provider = std::make_shared<SimpleRange<10>>(); |
|
63 | TEST_VC2_CREATE_DEFAULT_VAR(var1); | |
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); |
|
|||
131 | check_variable_state<RangeType<10>>(var1, range); |
|
64 | check_variable_state<RangeType<10>>(var1, range); | |
132 | } |
|
65 | } | |
133 |
|
66 | |||
134 |
void testZoom |
|
67 | void testZoom_data() | |
135 | { |
|
68 | { | |
136 | VariableController2 vc; |
|
69 | QTest::addColumn<double>("zoom"); | |
137 | auto provider = std::make_shared<SimpleRange<10>>(); |
|
70 | QTest::newRow("Zoom IN 10x") << .1; | |
138 | auto range = DateTimeRange::fromDateTime(QDate(2018,8,7),QTime(14,00), |
|
71 | QTest::newRow("Zoom OUT 10x") << 10.; | |
139 | QDate(2018,8,7),QTime(16,00)); |
|
72 | QTest::newRow("Zoom IN 1x") << 1.; | |
140 |
|
73 | } | ||
141 | auto var1 = vc.createVariable("var1", {}, provider, range); |
|
74 | void testZoom() | |
142 | check_variable_state<RangeType<10>>(var1, range); |
|
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 | vc.changeRange(var1, range); |
|
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 testPan |
|
86 | void testPan_data() | |
150 | { |
|
87 | { | |
151 | VariableController2 vc; |
|
88 | QTest::addColumn<double>("pan"); | |
152 | auto provider = std::make_shared<SimpleRange<10>>(); |
|
89 | QTest::newRow("Right 1000 seconds") << 1000.; | |
153 | auto range = DateTimeRange::fromDateTime(QDate(2018,8,7),QTime(14,00), |
|
90 | QTest::newRow("Left 1000 seconds") << -1000.; | |
154 | QDate(2018,8,7),QTime(16,00)); |
|
91 | QTest::newRow("Right 0.1 seconds") << .1; | |
155 |
|
92 | QTest::newRow("Left 0.1 seconds") << -.1; | ||
156 | auto var1 = vc.createVariable("var1", {}, provider, range); |
|
93 | } | |
|
94 | void testPan() | |||
|
95 | { | |||
|
96 | TEST_VC2_FIXTURE(10); | |||
|
97 | TEST_VC2_CREATE_DEFAULT_VAR(var1); | |||
157 | check_variable_state<RangeType<10>>(var1, range); |
|
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 | vc.changeRange(var1, range); |
|
103 | vc.changeRange(var1, range); | |
161 | check_variable_state<RangeType<10>>(var1, range); |
|
104 | check_variable_state<RangeType<10>>(var1, range); | |
162 | } |
|
105 | } |
General Comments 0
You need to be logged in to leave comments.
Login now