##// END OF EJS Templates
Add test for one variable acquisition in TestVariableSync
perrinel -
r833:650754855dfd
parent child
Show More
@@ -37,6 +37,36 std::vector<double> values(const SqpRange &range)
37 37 return result;
38 38 }
39 39
40 void validateRanges(VariableController &variableController,
41 const std::map<int, SqpRange> &expectedRanges)
42 {
43 for (const auto &expectedRangeEntry : expectedRanges) {
44 auto variableIndex = expectedRangeEntry.first;
45 auto expectedRange = expectedRangeEntry.second;
46
47 // Gets the variable in the controller
48 auto variable = variableController.variableModel()->variable(variableIndex);
49
50 // Compares variable's range to the expected range
51 QVERIFY(variable != nullptr);
52 auto range = variable->range();
53 qInfo() << "range vs expected range" << range << expectedRange;
54 QCOMPARE(range, expectedRange);
55
56 // Compares variable's data with values expected for its range
57 auto dataSeries = variable->dataSeries();
58 QVERIFY(dataSeries != nullptr);
59
60 auto it = dataSeries->xAxisRange(range.m_TStart, range.m_TEnd);
61 auto expectedValues = values(range);
62 qInfo() << std::distance(it.first, it.second) << expectedValues.size();
63 QVERIFY(std::equal(it.first, it.second, expectedValues.cbegin(), expectedValues.cend(),
64 [](const auto &dataSeriesIt, const auto &expectedValue) {
65 return dataSeriesIt.value() == expectedValue;
66 }));
67 }
68 }
69
40 70 /// Provider used for the tests
41 71 class TestProvider : public IDataProvider {
42 72 std::shared_ptr<IDataProvider> clone() const { return std::make_shared<TestProvider>(); }
@@ -97,8 +127,8 struct Create : public IOperation {
97 127 * Variable move/shift operation in the controller
98 128 */
99 129 struct Move : public IOperation {
100 explicit Move(int index, const SqpRange &newRange, bool shift = false)
101 : m_Index{index}, m_NewRange{newRange}, m_Shift{shift}
130 explicit Move(int index, const SqpRange &newRange, bool shift = false, int delayMS = 10)
131 : m_Index{index}, m_NewRange{newRange}, m_Shift{shift}, m_DelayMs{delayMS}
102 132 {
103 133 }
104 134
@@ -106,12 +136,14 struct Move : public IOperation {
106 136 {
107 137 if (auto variable = variableController.variableModel()->variable(m_Index)) {
108 138 variableController.onRequestDataLoading({variable}, m_NewRange, !m_Shift);
139 QTest::qWait(m_DelayMs);
109 140 }
110 141 }
111 142
112 143 int m_Index; ///< The index of the variable to move
113 144 SqpRange m_NewRange; ///< The new range of the variable
114 145 bool m_Shift; ///< Performs a shift (
146 int m_DelayMs; ///< wait the delay after running the request (
115 147 };
116 148
117 149 /**
@@ -169,11 +201,17 private slots:
169 201 /// Input data for @sa testSyncWithAborting()
170 202 void testSyncWithAborting_data();
171 203
172 /// Tests synchronization between variables through several operations with aborting
204 /// Input data for @sa testSyncOneVar()
205 void testSyncOneVar_data();
206
207 /// Tests synchronization between variables through several operations with automatic aborting
173 208 void testSyncWithAborting();
174 209
175 210 /// Tests synchronization between variables through several operations
176 211 void testSync();
212
213 /// Tests synchronization between variables through several operations
214 void testSyncOneVar();
177 215 };
178 216
179 217 namespace {
@@ -312,12 +350,6 void testSyncCase1WithAborting()
312 350 {std::make_shared<Move>(0, var0NewRange), {{0, var0NewRange}, {1, var1ExpectedRange}}});
313 351 };
314 352
315 // auto oldRange = newRange;
316 // newRange = range({14, 30}, {15, 30});
317 // auto expectedRange = oldRange;
318 // iterations.push_back(
319 // {std::make_shared<Move>(0, newRange), {{0, oldRange}, {1, expectedRange}}});
320
321 353 // Pan left
322 354 moveVar0(range({14, 30}, {15, 30}), range({14, 30}, {15, 30}));
323 355 // Pan right
@@ -389,6 +421,64 void testSyncCase2()
389 421 QTest::newRow("sync2") << syncId << initialRange << iterations << 4000;
390 422 // QTest::newRow("sync3") << syncId << initialRange << iterations << 5000;
391 423 }
424
425 void testSyncOnVarCase1()
426 {
427 // Id used to synchronize variables in the controller
428 auto syncId = QUuid::createUuid();
429
430 /// Generates a range according to a start time and a end time (the date is the same)
431 auto range = [](const QTime &startTime, const QTime &endTime) {
432 return SqpRange{DateUtils::secondsSinceEpoch(QDateTime{{2017, 1, 1}, startTime, Qt::UTC}),
433 DateUtils::secondsSinceEpoch(QDateTime{{2017, 1, 1}, endTime, Qt::UTC})};
434 };
435
436 auto initialRange = range({12, 0}, {13, 0});
437
438 Iterations creations{};
439 // Creates variables var0, var1 and var2
440 creations.push_back({std::make_shared<Create>(0), {{0, initialRange}}});
441
442 Iterations synchronization{};
443 // Adds variables into the sync group (ranges don't need to be tested here)
444 synchronization.push_back({std::make_shared<Synchronize>(0, syncId)});
445
446 Iterations iterations{};
447
448 // Moves var0 through several operations
449 auto moveOp = [&iterations](const auto &requestedRange, const auto &expectedRange, auto delay) {
450 iterations.push_back(
451 {std::make_shared<Move>(0, requestedRange, true, delay), {{0, expectedRange}}});
452 };
453
454 // we assume here 300 ms is enough to finsh a operation
455 int delayToFinish = 300;
456 // jump to right, let's the operation time to finish
457 moveOp(range({14, 30}, {15, 30}), range({14, 30}, {15, 30}), delayToFinish);
458 // pan to right, let's the operation time to finish
459 moveOp(range({14, 45}, {15, 45}), range({14, 45}, {15, 45}), delayToFinish);
460 // jump to left, let's the operation time to finish
461 moveOp(range({03, 30}, {04, 30}), range({03, 30}, {04, 30}), delayToFinish);
462 // Pan to left, let's the operation time to finish
463 moveOp(range({03, 10}, {04, 10}), range({03, 10}, {04, 10}), delayToFinish);
464 // Zoom in, let's the operation time to finish
465 moveOp(range({03, 30}, {04, 00}), range({03, 30}, {04, 00}), delayToFinish);
466 // Zoom out left, let's the operation time to finish
467 moveOp(range({01, 10}, {18, 10}), range({01, 10}, {18, 10}), delayToFinish);
468 // Go back to initial range
469 moveOp(initialRange, initialRange, delayToFinish);
470
471
472 // jump to right, let's the operation time to finish
473 // moveOp(range({14, 30}, {15, 30}), initialRange, delayToFinish);
474 // Zoom out left, let's the operation time to finish
475 moveOp(range({01, 10}, {18, 10}), initialRange, delayToFinish);
476 // Go back to initial range
477 moveOp(initialRange, initialRange, 300);
478
479 QTest::newRow("syncOnVarCase1") << syncId << initialRange << std::move(creations)
480 << std::move(iterations);
481 }
392 482 }
393 483
394 484 void TestVariableSync::testSync_data()
@@ -429,10 +519,26 void TestVariableSync::testSyncWithAborting_data()
429 519 testSyncCase1WithAborting();
430 520 }
431 521
432 void TestVariableSync::testSync()
522 void TestVariableSync::testSyncOneVar_data()
433 523 {
434 return;
524 // ////////////// //
525 // Test structure //
526 // ////////////// //
527
528 QTest::addColumn<QUuid>("syncId");
529 QTest::addColumn<SqpRange>("initialRange");
530 QTest::addColumn<Iterations>("creations");
531 QTest::addColumn<Iterations>("iterations");
435 532
533 // ////////// //
534 // Test cases //
535 // ////////// //
536
537 testSyncOnVarCase1();
538 }
539
540 void TestVariableSync::testSync()
541 {
436 542 // Inits controllers
437 543 TimeController timeController{};
438 544 VariableController variableController{};
@@ -445,33 +551,6 void TestVariableSync::testSync()
445 551 // Synchronization group used
446 552 variableController.onAddSynchronizationGroupId(syncId);
447 553
448 auto validateRanges = [&variableController](const auto &expectedRanges) {
449 for (const auto &expectedRangeEntry : expectedRanges) {
450 auto variableIndex = expectedRangeEntry.first;
451 auto expectedRange = expectedRangeEntry.second;
452
453 // Gets the variable in the controller
454 auto variable = variableController.variableModel()->variable(variableIndex);
455
456 // Compares variable's range to the expected range
457 QVERIFY(variable != nullptr);
458 auto range = variable->range();
459 QCOMPARE(range, expectedRange);
460
461 // Compares variable's data with values expected for its range
462 auto dataSeries = variable->dataSeries();
463 QVERIFY(dataSeries != nullptr);
464
465 auto it = dataSeries->xAxisRange(range.m_TStart, range.m_TEnd);
466 auto expectedValues = values(range);
467 qInfo() << std::distance(it.first, it.second) << expectedValues.size();
468 QVERIFY(std::equal(it.first, it.second, expectedValues.cbegin(), expectedValues.cend(),
469 [](const auto &dataSeriesIt, const auto &expectedValue) {
470 return dataSeriesIt.value() == expectedValue;
471 }));
472 }
473 };
474
475 554 // For each iteration:
476 555 // - execute operation
477 556 // - compare the variables' state to the expected states
@@ -481,7 +560,7 void TestVariableSync::testSync()
481 560 iteration.m_Operation->exec(variableController);
482 561 QTest::qWait(operationDelay);
483 562
484 validateRanges(iteration.m_ExpectedRanges);
563 validateRanges(variableController, iteration.m_ExpectedRanges);
485 564 }
486 565 }
487 566
@@ -499,34 +578,6 void TestVariableSync::testSyncWithAborting()
499 578 // Synchronization group used
500 579 variableController.onAddSynchronizationGroupId(syncId);
501 580
502 auto validateRanges = [&variableController](const auto &expectedRanges) {
503 for (const auto &expectedRangeEntry : expectedRanges) {
504 auto variableIndex = expectedRangeEntry.first;
505 auto expectedRange = expectedRangeEntry.second;
506
507 // Gets the variable in the controller
508 auto variable = variableController.variableModel()->variable(variableIndex);
509
510 // Compares variable's range to the expected range
511 QVERIFY(variable != nullptr);
512 auto range = variable->range();
513 qInfo() << "range vs expected range" << range << variable->range();
514 QCOMPARE(range, expectedRange);
515
516 // Compares variable's data with values expected for its range
517 auto dataSeries = variable->dataSeries();
518 QVERIFY(dataSeries != nullptr);
519
520 auto it = dataSeries->xAxisRange(range.m_TStart, range.m_TEnd);
521 auto expectedValues = values(range);
522 qInfo() << std::distance(it.first, it.second) << expectedValues.size();
523 QVERIFY(std::equal(it.first, it.second, expectedValues.cbegin(), expectedValues.cend(),
524 [](const auto &dataSeriesIt, const auto &expectedValue) {
525 return dataSeriesIt.value() == expectedValue;
526 }));
527 }
528 };
529
530 581 // For each iteration:
531 582 // - execute operation
532 583 // - compare the variables' state to the expected states
@@ -544,9 +595,42 void TestVariableSync::testSyncWithAborting()
544 595 }
545 596
546 597 QTest::qWait(operationDelay);
547 validateRanges(iterations.back().m_ExpectedRanges);
598 validateRanges(variableController, iterations.back().m_ExpectedRanges);
548 599 }
549 600
601 void TestVariableSync::testSyncOneVar()
602 {
603 // Inits controllers
604 TimeController timeController{};
605 VariableController variableController{};
606 variableController.setTimeController(&timeController);
607
608 QFETCH(QUuid, syncId);
609 QFETCH(SqpRange, initialRange);
610 timeController.onTimeToUpdate(initialRange);
611
612 // Synchronization group used
613 variableController.onAddSynchronizationGroupId(syncId);
614
615 // For each iteration:
616 // - execute operation
617 // - compare the variables' state to the expected states
618 QFETCH(Iterations, iterations);
619 QFETCH(Iterations, creations);
620
621 for (const auto &creation : creations) {
622 creation.m_Operation->exec(variableController);
623 QTest::qWait(300);
624 }
625
626 for (const auto &iteration : iterations) {
627 iteration.m_Operation->exec(variableController);
628 }
629
630 if (!iterations.empty()) {
631 validateRanges(variableController, iterations.back().m_ExpectedRanges);
632 }
633 }
550 634
551 635 QTEST_MAIN(TestVariableSync)
552 636
General Comments 0
You need to be logged in to leave comments. Login now