##// END OF EJS Templates
Improve TestVariable test
perrinel -
r824:5c93d38c12c1
parent child
Show More
@@ -1,390 +1,551
1 #include <QObject>
1 #include <QObject>
2 #include <QtTest>
2 #include <QtTest>
3
3
4 #include <memory>
4 #include <memory>
5
5
6 #include <Data/DataProviderParameters.h>
6 #include <Data/DataProviderParameters.h>
7 #include <Data/IDataProvider.h>
7 #include <Data/IDataProvider.h>
8 #include <Data/ScalarSeries.h>
8 #include <Data/ScalarSeries.h>
9 #include <Time/TimeController.h>
9 #include <Time/TimeController.h>
10 #include <Variable/Variable.h>
10 #include <Variable/Variable.h>
11 #include <Variable/VariableController.h>
11 #include <Variable/VariableController.h>
12 #include <Variable/VariableModel.h>
12 #include <Variable/VariableModel.h>
13
13
14 namespace {
14 namespace {
15
15
16 /// Delay after each operation on the variable before validating it (in ms)
16 /// Delay after each operation on the variable before validating it (in ms)
17 const auto OPERATION_DELAY = 100;
17 const auto OPERATION_DELAY = 100;
18
18
19 /**
19 /**
20 * Generates values according to a range. The value generated for a time t is the number of seconds
20 * Generates values according to a range. The value generated for a time t is the number of seconds
21 * of difference between t and a reference value (which is midnight -> 00:00:00)
21 * of difference between t and a reference value (which is midnight -> 00:00:00)
22 *
22 *
23 * Example: For a range between 00:00:10 and 00:00:20, the generated values are
23 * Example: For a range between 00:00:10 and 00:00:20, the generated values are
24 * {10,11,12,13,14,15,16,17,18,19,20}
24 * {10,11,12,13,14,15,16,17,18,19,20}
25 */
25 */
26 std::vector<double> values(const SqpRange &range)
26 std::vector<double> values(const SqpRange &range)
27 {
27 {
28 QTime referenceTime{0, 0};
28 QTime referenceTime{0, 0};
29
29
30 std::vector<double> result{};
30 std::vector<double> result{};
31
31
32 for (auto i = range.m_TStart; i <= range.m_TEnd; ++i) {
32 for (auto i = range.m_TStart; i <= range.m_TEnd; ++i) {
33 auto time = DateUtils::dateTime(i).time();
33 auto time = DateUtils::dateTime(i).time();
34 result.push_back(referenceTime.secsTo(time));
34 result.push_back(referenceTime.secsTo(time));
35 }
35 }
36
36
37 return result;
37 return result;
38 }
38 }
39
39
40 /// Provider used for the tests
40 /// Provider used for the tests
41 class TestProvider : public IDataProvider {
41 class TestProvider : public IDataProvider {
42 std::shared_ptr<IDataProvider> clone() const { return std::make_shared<TestProvider>(); }
42 std::shared_ptr<IDataProvider> clone() const { return std::make_shared<TestProvider>(); }
43
43
44 void requestDataLoading(QUuid acqIdentifier, const DataProviderParameters &parameters) override
44 void requestDataLoading(QUuid acqIdentifier, const DataProviderParameters &parameters) override
45 {
45 {
46 const auto &ranges = parameters.m_Times;
46 const auto &ranges = parameters.m_Times;
47
47
48 for (const auto &range : ranges) {
48 for (const auto &range : ranges) {
49 // Generates data series
49 // Generates data series
50 auto valuesData = values(range);
50 auto valuesData = values(range);
51
51
52 std::vector<double> xAxisData{};
52 std::vector<double> xAxisData{};
53 for (auto i = range.m_TStart; i <= range.m_TEnd; ++i) {
53 for (auto i = range.m_TStart; i <= range.m_TEnd; ++i) {
54 xAxisData.push_back(i);
54 xAxisData.push_back(i);
55 }
55 }
56
56
57 auto dataSeries = std::make_shared<ScalarSeries>(
57 auto dataSeries = std::make_shared<ScalarSeries>(
58 std::move(xAxisData), std::move(valuesData), Unit{"t", true}, Unit{});
58 std::move(xAxisData), std::move(valuesData), Unit{"t", true}, Unit{});
59
59
60 emit dataProvided(acqIdentifier, dataSeries, range);
60 emit dataProvided(acqIdentifier, dataSeries, range);
61 }
61 }
62 }
62 }
63
63
64 void requestDataAborting(QUuid acqIdentifier) override
64 void requestDataAborting(QUuid acqIdentifier) override
65 {
65 {
66 // Does nothing
66 // Does nothing
67 }
67 }
68 };
68 };
69
69
70 /**
70 /**
71 * Interface representing an operation performed on a variable controller.
71 * Interface representing an operation performed on a variable controller.
72 * This interface is used in tests to apply a set of operations and check the status of the
72 * This interface is used in tests to apply a set of operations and check the status of the
73 * controller after each operation
73 * controller after each operation
74 */
74 */
75 struct IOperation {
75 struct IOperation {
76 virtual ~IOperation() = default;
76 virtual ~IOperation() = default;
77 /// Executes the operation on the variable controller
77 /// Executes the operation on the variable controller
78 virtual void exec(VariableController &variableController) const = 0;
78 virtual void exec(VariableController &variableController) const = 0;
79 };
79 };
80
80
81 /**
81 /**
82 *Variable creation operation in the controller
82 *Variable creation operation in the controller
83 */
83 */
84 struct Create : public IOperation {
84 struct Create : public IOperation {
85 explicit Create(int index) : m_Index{index} {}
85 explicit Create(int index) : m_Index{index} {}
86
86
87 void exec(VariableController &variableController) const override
87 void exec(VariableController &variableController) const override
88 {
88 {
89 auto variable = variableController.createVariable(QString::number(m_Index), {},
89 auto variable = variableController.createVariable(QString::number(m_Index), {},
90 std::make_unique<TestProvider>());
90 std::make_unique<TestProvider>());
91 }
91 }
92
92
93 int m_Index; ///< The index of the variable to create in the controller
93 int m_Index; ///< The index of the variable to create in the controller
94 };
94 };
95
95
96 /**
96 /**
97 * Variable move/shift operation in the controller
97 * Variable move/shift operation in the controller
98 */
98 */
99 struct Move : public IOperation {
99 struct Move : public IOperation {
100 explicit Move(int index, const SqpRange &newRange, bool shift = false)
100 explicit Move(int index, const SqpRange &newRange, bool shift = false)
101 : m_Index{index}, m_NewRange{newRange}, m_Shift{shift}
101 : m_Index{index}, m_NewRange{newRange}, m_Shift{shift}
102 {
102 {
103 }
103 }
104
104
105 void exec(VariableController &variableController) const override
105 void exec(VariableController &variableController) const override
106 {
106 {
107 if (auto variable = variableController.variableModel()->variable(m_Index)) {
107 if (auto variable = variableController.variableModel()->variable(m_Index)) {
108 variableController.onRequestDataLoading({variable}, m_NewRange, !m_Shift);
108 variableController.onRequestDataLoading({variable}, m_NewRange, !m_Shift);
109 }
109 }
110 }
110 }
111
111
112 int m_Index; ///< The index of the variable to move
112 int m_Index; ///< The index of the variable to move
113 SqpRange m_NewRange; ///< The new range of the variable
113 SqpRange m_NewRange; ///< The new range of the variable
114 bool m_Shift; ///< Performs a shift (
114 bool m_Shift; ///< Performs a shift (
115 };
115 };
116
116
117 /**
117 /**
118 * Variable synchronization/desynchronization operation in the controller
118 * Variable synchronization/desynchronization operation in the controller
119 */
119 */
120 struct Synchronize : public IOperation {
120 struct Synchronize : public IOperation {
121 explicit Synchronize(int index, QUuid syncId, bool synchronize = true)
121 explicit Synchronize(int index, QUuid syncId, bool synchronize = true)
122 : m_Index{index}, m_SyncId{syncId}, m_Synchronize{synchronize}
122 : m_Index{index}, m_SyncId{syncId}, m_Synchronize{synchronize}
123 {
123 {
124 }
124 }
125
125
126 void exec(VariableController &variableController) const override
126 void exec(VariableController &variableController) const override
127 {
127 {
128 if (auto variable = variableController.variableModel()->variable(m_Index)) {
128 if (auto variable = variableController.variableModel()->variable(m_Index)) {
129 if (m_Synchronize) {
129 if (m_Synchronize) {
130 variableController.onAddSynchronized(variable, m_SyncId);
130 variableController.onAddSynchronized(variable, m_SyncId);
131 }
131 }
132 else {
132 else {
133 variableController.desynchronize(variable, m_SyncId);
133 variableController.desynchronize(variable, m_SyncId);
134 }
134 }
135 }
135 }
136 }
136 }
137
137
138 int m_Index; ///< The index of the variable to sync/desync
138 int m_Index; ///< The index of the variable to sync/desync
139 QUuid m_SyncId; ///< The synchronization group of the variable
139 QUuid m_SyncId; ///< The synchronization group of the variable
140 bool m_Synchronize; ///< Performs sync or desync operation
140 bool m_Synchronize; ///< Performs sync or desync operation
141 };
141 };
142
142
143 /**
143 /**
144 * Test Iteration
144 * Test Iteration
145 *
145 *
146 * A test iteration includes an operation to be performed, and a set of expected ranges after each
146 * A test iteration includes an operation to be performed, and a set of expected ranges after each
147 * operation. Each range is tested after the operation to ensure that:
147 * operation. Each range is tested after the operation to ensure that:
148 * - the range of the variable is the expected range
148 * - the range of the variable is the expected range
149 * - the data of the variable are those generated for the expected range
149 * - the data of the variable are those generated for the expected range
150 */
150 */
151 struct Iteration {
151 struct Iteration {
152 std::shared_ptr<IOperation> m_Operation; ///< Operation to perform
152 std::shared_ptr<IOperation> m_Operation; ///< Operation to perform
153 std::map<int, SqpRange> m_ExpectedRanges; ///< Expected ranges (by variable index)
153 std::map<int, SqpRange> m_ExpectedRanges; ///< Expected ranges (by variable index)
154 };
154 };
155
155
156 using Iterations = std::vector<Iteration>;
156 using Iterations = std::vector<Iteration>;
157
157
158 } // namespace
158 } // namespace
159
159
160 Q_DECLARE_METATYPE(Iterations)
160 Q_DECLARE_METATYPE(Iterations)
161
161
162 class TestVariableSync : public QObject {
162 class TestVariableSync : public QObject {
163 Q_OBJECT
163 Q_OBJECT
164
164
165 private slots:
165 private slots:
166 /// Input data for @sa testSync()
166 /// Input data for @sa testSync()
167 void testSync_data();
167 void testSync_data();
168
168
169 /// Input data for @sa testSyncWithAborting()
170 void testSyncWithAborting_data();
171
172 /// Tests synchronization between variables through several operations with aborting
173 void testSyncWithAborting();
174
169 /// Tests synchronization between variables through several operations
175 /// Tests synchronization between variables through several operations
170 void testSync();
176 void testSync();
171 };
177 };
172
178
173 namespace {
179 namespace {
174
180
175 void testSyncCase1()
181 void testSyncCase1()
176 {
182 {
177 // Id used to synchronize variables in the controller
183 // Id used to synchronize variables in the controller
178 auto syncId = QUuid::createUuid();
184 auto syncId = QUuid::createUuid();
179
185
180 /// Generates a range according to a start time and a end time (the date is the same)
186 /// Generates a range according to a start time and a end time (the date is the same)
181 auto range = [](const QTime &startTime, const QTime &endTime) {
187 auto range = [](const QTime &startTime, const QTime &endTime) {
182 return SqpRange{DateUtils::secondsSinceEpoch(QDateTime{{2017, 1, 1}, startTime, Qt::UTC}),
188 return SqpRange{DateUtils::secondsSinceEpoch(QDateTime{{2017, 1, 1}, startTime, Qt::UTC}),
183 DateUtils::secondsSinceEpoch(QDateTime{{2017, 1, 1}, endTime, Qt::UTC})};
189 DateUtils::secondsSinceEpoch(QDateTime{{2017, 1, 1}, endTime, Qt::UTC})};
184 };
190 };
185
191
186 auto initialRange = range({12, 0}, {13, 0});
192 auto initialRange = range({12, 0}, {13, 0});
187
193
188 Iterations iterations{};
194 Iterations iterations{};
189 // Creates variables var0, var1 and var2
195 // Creates variables var0, var1 and var2
190 iterations.push_back({std::make_shared<Create>(0), {{0, initialRange}}});
196 iterations.push_back({std::make_shared<Create>(0), {{0, initialRange}}});
191 iterations.push_back({std::make_shared<Create>(1), {{0, initialRange}, {1, initialRange}}});
197 iterations.push_back({std::make_shared<Create>(1), {{0, initialRange}, {1, initialRange}}});
192 iterations.push_back(
198 iterations.push_back(
193 {std::make_shared<Create>(2), {{0, initialRange}, {1, initialRange}, {2, initialRange}}});
199 {std::make_shared<Create>(2), {{0, initialRange}, {1, initialRange}, {2, initialRange}}});
194
200
195 // Adds variables into the sync group (ranges don't need to be tested here)
201 // Adds variables into the sync group (ranges don't need to be tested here)
196 iterations.push_back({std::make_shared<Synchronize>(0, syncId)});
202 iterations.push_back({std::make_shared<Synchronize>(0, syncId)});
197 iterations.push_back({std::make_shared<Synchronize>(1, syncId)});
203 iterations.push_back({std::make_shared<Synchronize>(1, syncId)});
198 iterations.push_back({std::make_shared<Synchronize>(2, syncId)});
204 iterations.push_back({std::make_shared<Synchronize>(2, syncId)});
199
205
200 // Moves var0: ranges of var0, var1 and var2 change
206 // Moves var0: ranges of var0, var1 and var2 change
201 auto newRange = range({12, 30}, {13, 30});
207 auto newRange = range({12, 30}, {13, 30});
202 iterations.push_back(
208 iterations.push_back(
203 {std::make_shared<Move>(0, newRange), {{0, newRange}, {1, newRange}, {2, newRange}}});
209 {std::make_shared<Move>(0, newRange), {{0, newRange}, {1, newRange}, {2, newRange}}});
204
210
205 // Moves var1: ranges of var0, var1 and var2 change
211 // Moves var1: ranges of var0, var1 and var2 change
206 newRange = range({13, 0}, {14, 0});
212 newRange = range({13, 0}, {14, 0});
207 iterations.push_back(
213 iterations.push_back(
208 {std::make_shared<Move>(0, newRange), {{0, newRange}, {1, newRange}, {2, newRange}}});
214 {std::make_shared<Move>(0, newRange), {{0, newRange}, {1, newRange}, {2, newRange}}});
209
215
210 // Moves var2: ranges of var0, var1 and var2 change
216 // Moves var2: ranges of var0, var1 and var2 change
211 newRange = range({13, 30}, {14, 30});
217 newRange = range({13, 30}, {14, 30});
212 iterations.push_back(
218 iterations.push_back(
213 {std::make_shared<Move>(0, newRange), {{0, newRange}, {1, newRange}, {2, newRange}}});
219 {std::make_shared<Move>(0, newRange), {{0, newRange}, {1, newRange}, {2, newRange}}});
214
220
215 // Desyncs var2 and moves var0:
221 // Desyncs var2 and moves var0:
216 // - ranges of var0 and var1 change
222 // - ranges of var0 and var1 change
217 // - range of var2 doesn't change anymore
223 // - range of var2 doesn't change anymore
218 auto var2Range = newRange;
224 auto var2Range = newRange;
219 newRange = range({13, 45}, {14, 45});
225 newRange = range({13, 45}, {14, 45});
220 iterations.push_back({std::make_shared<Synchronize>(2, syncId, false)});
226 iterations.push_back({std::make_shared<Synchronize>(2, syncId, false)});
221 iterations.push_back(
227 iterations.push_back(
222 {std::make_shared<Move>(0, newRange), {{0, newRange}, {1, newRange}, {2, var2Range}}});
228 {std::make_shared<Move>(0, newRange), {{0, newRange}, {1, newRange}, {2, var2Range}}});
223
229
224 // Shifts var0: although var1 is synchronized with var0, its range doesn't change
230 // Shifts var0: although var1 is synchronized with var0, its range doesn't change
225 auto var1Range = newRange;
231 auto var1Range = newRange;
226 newRange = range({14, 45}, {15, 45});
232 newRange = range({14, 45}, {15, 45});
227 iterations.push_back({std::make_shared<Move>(0, newRange, true),
233 iterations.push_back({std::make_shared<Move>(0, newRange, true),
228 {{0, newRange}, {1, var1Range}, {2, var2Range}}});
234 {{0, newRange}, {1, var1Range}, {2, var2Range}}});
229
235
230 // Moves var0 through several operations:
236 // Moves var0 through several operations:
231 // - range of var0 changes
237 // - range of var0 changes
232 // - range or var1 changes according to the previous shift (one hour)
238 // - range or var1 changes according to the previous shift (one hour)
233 auto moveVar0 = [&iterations](const auto &var0NewRange, const auto &var1ExpectedRange) {
239 auto moveVar0 = [&iterations](const auto &var0NewRange, const auto &var1ExpectedRange) {
234 iterations.push_back(
240 iterations.push_back(
235 {std::make_shared<Move>(0, var0NewRange), {{0, var0NewRange}, {1, var1ExpectedRange}}});
241 {std::make_shared<Move>(0, var0NewRange), {{0, var0NewRange}, {1, var1ExpectedRange}}});
236 };
242 };
243
237 // Pan left
244 // Pan left
238 moveVar0(range({14, 30}, {15, 30}), range({13, 30}, {14, 30}));
245 moveVar0(range({14, 30}, {15, 30}), range({13, 30}, {14, 30}));
239 // Pan right
246 // Pan right
240 moveVar0(range({16, 0}, {17, 0}), range({15, 0}, {16, 0}));
247 moveVar0(range({16, 0}, {17, 0}), range({15, 0}, {16, 0}));
241 // Zoom in
248 // Zoom in
242 moveVar0(range({16, 30}, {16, 45}), range({15, 30}, {15, 45}));
249 moveVar0(range({16, 30}, {16, 45}), range({15, 30}, {15, 45}));
243 // Zoom out
250 // Zoom out
244 moveVar0(range({16, 15}, {17, 0}), range({15, 15}, {16, 0}));
251 moveVar0(range({16, 15}, {17, 0}), range({15, 15}, {16, 0}));
245
252
246 QTest::newRow("sync1") << syncId << initialRange << std::move(iterations) << 200;
253 QTest::newRow("sync1") << syncId << initialRange << std::move(iterations) << 200;
247 }
254 }
248
255
256 void testSyncCase1WithAborting()
257 {
258 // Id used to synchronize variables in the controller
259 auto syncId = QUuid::createUuid();
260
261 /// Generates a range according to a start time and a end time (the date is the same)
262 auto range = [](const QTime &startTime, const QTime &endTime) {
263 return SqpRange{DateUtils::secondsSinceEpoch(QDateTime{{2017, 1, 1}, startTime, Qt::UTC}),
264 DateUtils::secondsSinceEpoch(QDateTime{{2017, 1, 1}, endTime, Qt::UTC})};
265 };
266
267 auto initialRange = range({12, 0}, {13, 0});
268
269 Iterations creations{};
270 // Creates variables var0, var1 and var2
271 creations.push_back({std::make_shared<Create>(0), {{0, initialRange}}});
272 creations.push_back({std::make_shared<Create>(1), {{0, initialRange}, {1, initialRange}}});
273
274 // Adds variables into the sync group (ranges don't need to be tested here)
275 Iterations iterations{};
276 iterations.push_back({std::make_shared<Synchronize>(0, syncId)});
277 iterations.push_back({std::make_shared<Synchronize>(1, syncId)});
278
279 // Moves var0: ranges of var0, var1 and var2 change
280 auto newRange = range({12, 30}, {13, 30});
281 iterations.push_back({std::make_shared<Move>(0, newRange), {{0, newRange}, {1, newRange}}});
282
283 // Moves var1: ranges of var0, var1 and var2 change
284 newRange = range({13, 0}, {14, 0});
285 iterations.push_back({std::make_shared<Move>(0, newRange), {{0, newRange}, {1, newRange}}});
286
287 // Moves var2: ranges of var0, var1 and var2 change
288 newRange = range({13, 30}, {14, 30});
289 iterations.push_back(
290 {std::make_shared<Move>(0, newRange), {{0, newRange}, {1, newRange}, {2, newRange}}});
291
292 // Desyncs var2 and moves var0:
293 // - ranges of var0 and var1 change
294 // - range of var2 doesn't change anymore
295 auto var2Range = newRange;
296 newRange = range({13, 45}, {14, 45});
297 iterations.push_back({std::make_shared<Synchronize>(2, syncId, false)});
298 iterations.push_back({std::make_shared<Move>(0, newRange), {{0, newRange}, {1, newRange}}});
299
300 // Shifts var0: although var1 is synchronized with var0, its range doesn't change
301 auto var1Range = newRange;
302 newRange = range({14, 45}, {15, 45});
303 iterations.push_back(
304 {std::make_shared<Move>(0, newRange, true), {{0, newRange}, {1, var1Range}}});
305
306 // Moves var0 through several operations:
307 // - range of var0 changes
308 // - range or var1 changes according to the previous shift (one hour)
309 auto moveVar0 = [&iterations](const auto &var0NewRange, const auto &var1ExpectedRange) {
310 iterations.push_back(
311 {std::make_shared<Move>(0, var0NewRange), {{0, var0NewRange}, {1, var1ExpectedRange}}});
312 };
313
314 // auto oldRange = newRange;
315 // newRange = range({14, 30}, {15, 30});
316 // auto expectedRange = oldRange;
317 // iterations.push_back(
318 // {std::make_shared<Move>(0, newRange), {{0, oldRange}, {1, expectedRange}}});
319
320 // Pan left
321 moveVar0(range({14, 30}, {15, 30}), range({13, 30}, {14, 30}));
322 // Pan right
323 moveVar0(range({16, 0}, {17, 0}), range({15, 0}, {16, 0}));
324 // Zoom in
325 moveVar0(range({16, 30}, {16, 45}), range({15, 30}, {15, 45}));
326 // Zoom out
327 moveVar0(range({16, 15}, {17, 0}), range({15, 15}, {16, 0}));
328
329 QTest::newRow("syncWithAborting1") << syncId << initialRange << std::move(creations)
330 << std::move(iterations) << 200;
331 }
332
249 void testSyncCase2()
333 void testSyncCase2()
250 {
334 {
251 // Id used to synchronize variables in the controller
335 // Id used to synchronize variables in the controller
252 auto syncId = QUuid::createUuid();
336 auto syncId = QUuid::createUuid();
253
337
254 /// Generates a range according to a start time and a end time (the date is the same)
338 /// Generates a range according to a start time and a end time (the date is the same)
255 auto dateTime = [](int year, int month, int day, int hours, int minutes, int seconds) {
339 auto dateTime = [](int year, int month, int day, int hours, int minutes, int seconds) {
256 return DateUtils::secondsSinceEpoch(
340 return DateUtils::secondsSinceEpoch(
257 QDateTime{{year, month, day}, QTime{hours, minutes, seconds}, Qt::UTC});
341 QDateTime{{year, month, day}, QTime{hours, minutes, seconds}, Qt::UTC});
258 };
342 };
259
343
260 auto initialRange = SqpRange{dateTime(2017, 1, 1, 12, 0, 0), dateTime(2017, 1, 1, 13, 0, 0)};
344 auto initialRange = SqpRange{dateTime(2017, 1, 1, 12, 0, 0), dateTime(2017, 1, 1, 13, 0, 0)};
261
345
262 Iterations iterations{};
346 Iterations iterations{};
263 // Creates variables var0 and var1
347 // Creates variables var0 and var1
264 iterations.push_back({std::make_shared<Create>(0), {{0, initialRange}}});
348 iterations.push_back({std::make_shared<Create>(0), {{0, initialRange}}});
265 iterations.push_back({std::make_shared<Create>(1), {{0, initialRange}, {1, initialRange}}});
349 iterations.push_back({std::make_shared<Create>(1), {{0, initialRange}, {1, initialRange}}});
266
350
267 // Adds variables into the sync group (ranges don't need to be tested here)
351 // Adds variables into the sync group (ranges don't need to be tested here)
268 iterations.push_back({std::make_shared<Synchronize>(0, syncId)});
352 iterations.push_back({std::make_shared<Synchronize>(0, syncId)});
269 iterations.push_back({std::make_shared<Synchronize>(1, syncId)});
353 iterations.push_back({std::make_shared<Synchronize>(1, syncId)});
270
354
271
355
272 // Moves var0 through several operations:
356 // Moves var0 through several operations:
273 // - range of var0 changes
357 // - range of var0 changes
274 // - range or var1 changes according to the previous shift (one hour)
358 // - range or var1 changes according to the previous shift (one hour)
275 auto moveVar0 = [&iterations](const auto &var0NewRange) {
359 auto moveVar0 = [&iterations](const auto &var0NewRange) {
276 iterations.push_back(
360 iterations.push_back(
277 {std::make_shared<Move>(0, var0NewRange), {{0, var0NewRange}, {1, var0NewRange}}});
361 {std::make_shared<Move>(0, var0NewRange), {{0, var0NewRange}, {1, var0NewRange}}});
278 };
362 };
279 moveVar0(SqpRange{dateTime(2017, 1, 1, 12, 0, 0), dateTime(2017, 1, 1, 13, 0, 0)});
363 moveVar0(SqpRange{dateTime(2017, 1, 1, 12, 0, 0), dateTime(2017, 1, 1, 13, 0, 0)});
280 moveVar0(SqpRange{dateTime(2017, 1, 1, 14, 0, 0), dateTime(2017, 1, 1, 15, 0, 0)});
364 moveVar0(SqpRange{dateTime(2017, 1, 1, 14, 0, 0), dateTime(2017, 1, 1, 15, 0, 0)});
281 moveVar0(SqpRange{dateTime(2017, 1, 1, 8, 0, 0), dateTime(2017, 1, 1, 9, 0, 0)});
365 moveVar0(SqpRange{dateTime(2017, 1, 1, 8, 0, 0), dateTime(2017, 1, 1, 9, 0, 0)});
282 // moveVar0(SqpRange{dateTime(2017, 1, 1, 7, 30, 0), dateTime(2017, 1, 1, 9, 30, 0)});
366 // moveVar0(SqpRange{dateTime(2017, 1, 1, 7, 30, 0), dateTime(2017, 1, 1, 9, 30, 0)});
283 moveVar0(SqpRange{dateTime(2017, 1, 1, 2, 0, 0), dateTime(2017, 1, 1, 4, 0, 0)});
367 moveVar0(SqpRange{dateTime(2017, 1, 1, 2, 0, 0), dateTime(2017, 1, 1, 4, 0, 0)});
284 moveVar0(SqpRange{dateTime(2017, 1, 1, 6, 0, 0), dateTime(2017, 1, 1, 8, 0, 0)});
368 moveVar0(SqpRange{dateTime(2017, 1, 1, 6, 0, 0), dateTime(2017, 1, 1, 8, 0, 0)});
285
369
286 moveVar0(SqpRange{dateTime(2017, 1, 10, 6, 0, 0), dateTime(2017, 1, 15, 8, 0, 0)});
370 moveVar0(SqpRange{dateTime(2017, 1, 10, 6, 0, 0), dateTime(2017, 1, 15, 8, 0, 0)});
287 moveVar0(SqpRange{dateTime(2017, 1, 17, 6, 0, 0), dateTime(2017, 1, 25, 8, 0, 0)});
371 moveVar0(SqpRange{dateTime(2017, 1, 17, 6, 0, 0), dateTime(2017, 1, 25, 8, 0, 0)});
288 moveVar0(SqpRange{dateTime(2017, 1, 2, 6, 0, 0), dateTime(2017, 1, 8, 8, 0, 0)});
372 moveVar0(SqpRange{dateTime(2017, 1, 2, 6, 0, 0), dateTime(2017, 1, 8, 8, 0, 0)});
289
373
290 moveVar0(SqpRange{dateTime(2017, 4, 10, 6, 0, 0), dateTime(2017, 6, 15, 8, 0, 0)});
374 moveVar0(SqpRange{dateTime(2017, 4, 10, 6, 0, 0), dateTime(2017, 6, 15, 8, 0, 0)});
291 moveVar0(SqpRange{dateTime(2017, 1, 17, 6, 0, 0), dateTime(2017, 2, 25, 8, 0, 0)});
375 moveVar0(SqpRange{dateTime(2017, 1, 17, 6, 0, 0), dateTime(2017, 2, 25, 8, 0, 0)});
292 moveVar0(SqpRange{dateTime(2017, 7, 2, 6, 0, 0), dateTime(2017, 10, 8, 8, 0, 0)});
376 moveVar0(SqpRange{dateTime(2017, 7, 2, 6, 0, 0), dateTime(2017, 10, 8, 8, 0, 0)});
293 moveVar0(SqpRange{dateTime(2017, 4, 10, 6, 0, 0), dateTime(2017, 6, 15, 8, 0, 0)});
377 moveVar0(SqpRange{dateTime(2017, 4, 10, 6, 0, 0), dateTime(2017, 6, 15, 8, 0, 0)});
294 moveVar0(SqpRange{dateTime(2017, 1, 17, 6, 0, 0), dateTime(2017, 2, 25, 8, 0, 0)});
378 moveVar0(SqpRange{dateTime(2017, 1, 17, 6, 0, 0), dateTime(2017, 2, 25, 8, 0, 0)});
295 moveVar0(SqpRange{dateTime(2017, 7, 2, 6, 0, 0), dateTime(2017, 10, 8, 8, 0, 0)});
379 moveVar0(SqpRange{dateTime(2017, 7, 2, 6, 0, 0), dateTime(2017, 10, 8, 8, 0, 0)});
296 moveVar0(SqpRange{dateTime(2017, 4, 10, 6, 0, 0), dateTime(2017, 6, 15, 8, 0, 0)});
380 moveVar0(SqpRange{dateTime(2017, 4, 10, 6, 0, 0), dateTime(2017, 6, 15, 8, 0, 0)});
297 moveVar0(SqpRange{dateTime(2017, 1, 17, 6, 0, 0), dateTime(2017, 2, 25, 8, 0, 0)});
381 moveVar0(SqpRange{dateTime(2017, 1, 17, 6, 0, 0), dateTime(2017, 2, 25, 8, 0, 0)});
298 moveVar0(SqpRange{dateTime(2017, 7, 2, 6, 0, 0), dateTime(2017, 10, 8, 8, 0, 0)});
382 moveVar0(SqpRange{dateTime(2017, 7, 2, 6, 0, 0), dateTime(2017, 10, 8, 8, 0, 0)});
299 moveVar0(SqpRange{dateTime(2017, 4, 10, 6, 0, 0), dateTime(2017, 6, 15, 8, 0, 0)});
383 moveVar0(SqpRange{dateTime(2017, 4, 10, 6, 0, 0), dateTime(2017, 6, 15, 8, 0, 0)});
300 moveVar0(SqpRange{dateTime(2017, 1, 17, 6, 0, 0), dateTime(2017, 2, 25, 8, 0, 0)});
384 moveVar0(SqpRange{dateTime(2017, 1, 17, 6, 0, 0), dateTime(2017, 2, 25, 8, 0, 0)});
301 moveVar0(SqpRange{dateTime(2017, 7, 2, 6, 0, 0), dateTime(2017, 10, 8, 8, 0, 0)});
385 moveVar0(SqpRange{dateTime(2017, 7, 2, 6, 0, 0), dateTime(2017, 10, 8, 8, 0, 0)});
302
386
303
387
304 QTest::newRow("sync2") << syncId << initialRange << iterations << 4000;
388 QTest::newRow("sync2") << syncId << initialRange << iterations << 4000;
305 // QTest::newRow("sync3") << syncId << initialRange << iterations << 5000;
389 // QTest::newRow("sync3") << syncId << initialRange << iterations << 5000;
306 }
390 }
307 }
391 }
308
392
309 void TestVariableSync::testSync_data()
393 void TestVariableSync::testSync_data()
310 {
394 {
311 // ////////////// //
395 // ////////////// //
312 // Test structure //
396 // Test structure //
313 // ////////////// //
397 // ////////////// //
314
398
315 QTest::addColumn<QUuid>("syncId");
399 QTest::addColumn<QUuid>("syncId");
316 QTest::addColumn<SqpRange>("initialRange");
400 QTest::addColumn<SqpRange>("initialRange");
317 QTest::addColumn<Iterations>("iterations");
401 QTest::addColumn<Iterations>("iterations");
318 QTest::addColumn<int>("operationDelay");
402 QTest::addColumn<int>("operationDelay");
319
403
320 // ////////// //
404 // ////////// //
321 // Test cases //
405 // Test cases //
322 // ////////// //
406 // ////////// //
323
407
324 testSyncCase1();
408 testSyncCase1();
325 // testSyncCase2();
409 testSyncCase2();
410 }
411
412 void TestVariableSync::testSyncWithAborting_data()
413 {
414 // ////////////// //
415 // Test structure //
416 // ////////////// //
417
418 QTest::addColumn<QUuid>("syncId");
419 QTest::addColumn<SqpRange>("initialRange");
420 QTest::addColumn<Iterations>("creations");
421 QTest::addColumn<Iterations>("iterations");
422 QTest::addColumn<int>("operationDelay");
423
424 // ////////// //
425 // Test cases //
426 // ////////// //
427
428 testSyncCase1WithAborting();
326 }
429 }
327
430
328 void TestVariableSync::testSync()
431 void TestVariableSync::testSync()
329 {
432 {
433 return;
434
330 // Inits controllers
435 // Inits controllers
331 TimeController timeController{};
436 TimeController timeController{};
332 VariableController variableController{};
437 VariableController variableController{};
333 variableController.setTimeController(&timeController);
438 variableController.setTimeController(&timeController);
334
439
335 QFETCH(QUuid, syncId);
440 QFETCH(QUuid, syncId);
336 QFETCH(SqpRange, initialRange);
441 QFETCH(SqpRange, initialRange);
337 timeController.onTimeToUpdate(initialRange);
442 timeController.onTimeToUpdate(initialRange);
338
443
339 // Synchronization group used
444 // Synchronization group used
340 variableController.onAddSynchronizationGroupId(syncId);
445 variableController.onAddSynchronizationGroupId(syncId);
341
446
342 auto validateRanges = [&variableController](const auto &expectedRanges) {
447 auto validateRanges = [&variableController](const auto &expectedRanges) {
343 for (const auto &expectedRangeEntry : expectedRanges) {
448 for (const auto &expectedRangeEntry : expectedRanges) {
344 auto variableIndex = expectedRangeEntry.first;
449 auto variableIndex = expectedRangeEntry.first;
345 auto expectedRange = expectedRangeEntry.second;
450 auto expectedRange = expectedRangeEntry.second;
346
451
347 // Gets the variable in the controller
452 // Gets the variable in the controller
348 auto variable = variableController.variableModel()->variable(variableIndex);
453 auto variable = variableController.variableModel()->variable(variableIndex);
349
454
350 // Compares variable's range to the expected range
455 // Compares variable's range to the expected range
351 QVERIFY(variable != nullptr);
456 QVERIFY(variable != nullptr);
352 auto range = variable->range();
457 auto range = variable->range();
353 QCOMPARE(range, expectedRange);
458 QCOMPARE(range, expectedRange);
354
459
355 // Compares variable's data with values expected for its range
460 // Compares variable's data with values expected for its range
356 auto dataSeries = variable->dataSeries();
461 auto dataSeries = variable->dataSeries();
357 QVERIFY(dataSeries != nullptr);
462 QVERIFY(dataSeries != nullptr);
358
463
359 auto it = dataSeries->xAxisRange(range.m_TStart, range.m_TEnd);
464 auto it = dataSeries->xAxisRange(range.m_TStart, range.m_TEnd);
360 auto expectedValues = values(range);
465 auto expectedValues = values(range);
361 qInfo() << std::distance(it.first, it.second) << expectedValues.size();
466 qInfo() << std::distance(it.first, it.second) << expectedValues.size();
362 QVERIFY(std::equal(it.first, it.second, expectedValues.cbegin(), expectedValues.cend(),
467 QVERIFY(std::equal(it.first, it.second, expectedValues.cbegin(), expectedValues.cend(),
363 [](const auto &dataSeriesIt, const auto &expectedValue) {
468 [](const auto &dataSeriesIt, const auto &expectedValue) {
364 return dataSeriesIt.value() == expectedValue;
469 return dataSeriesIt.value() == expectedValue;
365 }));
470 }));
366 }
471 }
367 };
472 };
368
473
369 // For each iteration:
474 // For each iteration:
370 // - execute operation
475 // - execute operation
371 // - compare the variables' state to the expected states
476 // - compare the variables' state to the expected states
372 QFETCH(Iterations, iterations);
477 QFETCH(Iterations, iterations);
373 QFETCH(int, operationDelay);
478 QFETCH(int, operationDelay);
374 for (const auto &iteration : iterations) {
479 for (const auto &iteration : iterations) {
375 iteration.m_Operation->exec(variableController);
480 iteration.m_Operation->exec(variableController);
376 QTest::qWait(operationDelay);
481 QTest::qWait(operationDelay);
377
482
378 validateRanges(iteration.m_ExpectedRanges);
483 validateRanges(iteration.m_ExpectedRanges);
379 }
484 }
485 }
486
487 void TestVariableSync::testSyncWithAborting()
488 {
489 // Inits controllers
490 TimeController timeController{};
491 VariableController variableController{};
492 variableController.setTimeController(&timeController);
493
494 QFETCH(QUuid, syncId);
495 QFETCH(SqpRange, initialRange);
496 timeController.onTimeToUpdate(initialRange);
497
498 // Synchronization group used
499 variableController.onAddSynchronizationGroupId(syncId);
500
501 auto validateRanges = [&variableController](const auto &expectedRanges) {
502 for (const auto &expectedRangeEntry : expectedRanges) {
503 auto variableIndex = expectedRangeEntry.first;
504 auto expectedRange = expectedRangeEntry.second;
505
506 // Gets the variable in the controller
507 auto variable = variableController.variableModel()->variable(variableIndex);
508
509 // Compares variable's range to the expected range
510 QVERIFY(variable != nullptr);
511 auto range = variable->range();
512 QCOMPARE(range, expectedRange);
513
514 // Compares variable's data with values expected for its range
515 auto dataSeries = variable->dataSeries();
516 QVERIFY(dataSeries != nullptr);
517
518 auto it = dataSeries->xAxisRange(range.m_TStart, range.m_TEnd);
519 auto expectedValues = values(range);
520 qInfo() << std::distance(it.first, it.second) << expectedValues.size();
521 QVERIFY(std::equal(it.first, it.second, expectedValues.cbegin(), expectedValues.cend(),
522 [](const auto &dataSeriesIt, const auto &expectedValue) {
523 return dataSeriesIt.value() == expectedValue;
524 }));
525 }
526 };
527
528 // For each iteration:
529 // - execute operation
530 // - compare the variables' state to the expected states
531 QFETCH(Iterations, iterations);
532 QFETCH(Iterations, creations);
533 QFETCH(int, operationDelay);
534
535 for (const auto &creation : creations) {
536 creation.m_Operation->exec(variableController);
537 QTest::qWait(operationDelay);
538 }
380
539
381 for (const auto &iteration : iterations) {
540 for (const auto &iteration : iterations) {
382 iteration.m_Operation->exec(variableController);
541 iteration.m_Operation->exec(variableController);
383 }
542 }
543
384 QTest::qWait(operationDelay);
544 QTest::qWait(operationDelay);
385 validateRanges(iterations.back().m_ExpectedRanges);
545 validateRanges(iterations.back().m_ExpectedRanges);
386 }
546 }
387
547
548
388 QTEST_MAIN(TestVariableSync)
549 QTEST_MAIN(TestVariableSync)
389
550
390 #include "TestVariableSync.moc"
551 #include "TestVariableSync.moc"
General Comments 0
You need to be logged in to leave comments. Login now