##// END OF EJS Templates
Setups test (4)...
Alexandre Leroux -
r716:c3e161f3c70a
parent child
Show More
@@ -1,192 +1,241
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 <Variable/Variable.h>
10 #include <Variable/Variable.h>
10 #include <Variable/VariableController.h>
11 #include <Variable/VariableController.h>
11 #include <Variable/VariableModel.h>
12 #include <Variable/VariableModel.h>
12
13
13 namespace {
14 namespace {
14
15
16 /// Delay after each operation on the variable before validating it (in ms)
17 const auto OPERATION_DELAY = 100;
18
15 /**
19 /**
16 * 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
17 * 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)
18 *
22 *
19 * 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
20 * {10,11,12,13,14,15,16,17,18,19,20}
24 * {10,11,12,13,14,15,16,17,18,19,20}
21 */
25 */
22 std::vector<double> values(const SqpRange &range)
26 std::vector<double> values(const SqpRange &range)
23 {
27 {
24 QTime referenceTime{0, 0};
28 QTime referenceTime{0, 0};
25
29
26 std::vector<double> result{};
30 std::vector<double> result{};
27
31
28 for (auto i = range.m_TStart; i <= range.m_TEnd; ++i) {
32 for (auto i = range.m_TStart; i <= range.m_TEnd; ++i) {
29 auto time = DateUtils::dateTime(i).time();
33 auto time = DateUtils::dateTime(i).time();
30 result.push_back(referenceTime.secsTo(time));
34 result.push_back(referenceTime.secsTo(time));
31 }
35 }
32
36
33 return result;
37 return result;
34 }
38 }
35
39
36 /// Provider used for the tests
40 /// Provider used for the tests
37 class TestProvider : public IDataProvider {
41 class TestProvider : public IDataProvider {
38 std::shared_ptr<IDataProvider> clone() const { return std::make_shared<TestProvider>(); }
42 std::shared_ptr<IDataProvider> clone() const { return std::make_shared<TestProvider>(); }
39
43
40 void requestDataLoading(QUuid acqIdentifier, const DataProviderParameters &parameters) override
44 void requestDataLoading(QUuid acqIdentifier, const DataProviderParameters &parameters) override
41 {
45 {
42 const auto &ranges = parameters.m_Times;
46 const auto &ranges = parameters.m_Times;
43
47
44 for (const auto &range : ranges) {
48 for (const auto &range : ranges) {
45 // Generates data series
49 // Generates data series
46 auto valuesData = values(range);
50 auto valuesData = values(range);
47
51
48 std::vector<double> xAxisData{};
52 std::vector<double> xAxisData{};
49 for (auto i = range.m_TStart; i <= range.m_TEnd; ++i) {
53 for (auto i = range.m_TStart; i <= range.m_TEnd; ++i) {
50 xAxisData.push_back(i);
54 xAxisData.push_back(i);
51 }
55 }
52
56
53 auto dataSeries = std::make_shared<ScalarSeries>(
57 auto dataSeries = std::make_shared<ScalarSeries>(
54 std::move(xAxisData), std::move(valuesData), Unit{"t", true}, Unit{});
58 std::move(xAxisData), std::move(valuesData), Unit{"t", true}, Unit{});
55
59
56 emit dataProvided(acqIdentifier, dataSeries, range);
60 emit dataProvided(acqIdentifier, dataSeries, range);
57 }
61 }
58 }
62 }
59
63
60 void requestDataAborting(QUuid acqIdentifier) override
64 void requestDataAborting(QUuid acqIdentifier) override
61 {
65 {
62 // Does nothing
66 // Does nothing
63 }
67 }
64 };
68 };
65
69
66 /**
70 /**
67 * Interface representing an operation performed on a variable controller.
71 * Interface representing an operation performed on a variable controller.
68 * 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
69 * controller after each operation
73 * controller after each operation
70 */
74 */
71 struct IOperation {
75 struct IOperation {
72 virtual ~IOperation() = default;
76 virtual ~IOperation() = default;
73 /// Executes the operation on the variable controller
77 /// Executes the operation on the variable controller
74 virtual void exec(VariableController &variableController) const = 0;
78 virtual void exec(VariableController &variableController) const = 0;
75 };
79 };
76
80
77 /**
81 /**
78 *Variable creation operation in the controller
82 *Variable creation operation in the controller
79 */
83 */
80 struct Create : public IOperation {
84 struct Create : public IOperation {
81 explicit Create(int index) : m_Index{index} {}
85 explicit Create(int index) : m_Index{index} {}
82
86
83 void exec(VariableController &variableController) const override
87 void exec(VariableController &variableController) const override
84 {
88 {
85 auto variable = variableController.createVariable(QString::number(m_Index), {},
89 auto variable = variableController.createVariable(QString::number(m_Index), {},
86 std::make_unique<TestProvider>());
90 std::make_unique<TestProvider>());
87 }
91 }
88
92
89 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
90 };
94 };
91
95
92 /**
96 /**
93 * Variable move/shift operation in the controller
97 * Variable move/shift operation in the controller
94 */
98 */
95 struct Move : public IOperation {
99 struct Move : public IOperation {
96 explicit Move(int index, const SqpRange &newRange, bool shift = false)
100 explicit Move(int index, const SqpRange &newRange, bool shift = false)
97 : m_Index{index}, m_NewRange{newRange}, m_Shift{shift}
101 : m_Index{index}, m_NewRange{newRange}, m_Shift{shift}
98 {
102 {
99 }
103 }
100
104
101 void exec(VariableController &variableController) const override
105 void exec(VariableController &variableController) const override
102 {
106 {
103 if (auto variable = variableController.variableModel()->variable(m_Index)) {
107 if (auto variable = variableController.variableModel()->variable(m_Index)) {
104 variableController.onRequestDataLoading({variable}, m_NewRange, variable->range(),
108 variableController.onRequestDataLoading({variable}, m_NewRange, variable->range(),
105 !m_Shift);
109 !m_Shift);
106 }
110 }
107 }
111 }
108
112
109 int m_Index; ///< The index of the variable to move
113 int m_Index; ///< The index of the variable to move
110 SqpRange m_NewRange; ///< The new range of the variable
114 SqpRange m_NewRange; ///< The new range of the variable
111 bool m_Shift; ///< Performs a shift (
115 bool m_Shift; ///< Performs a shift (
112 };
116 };
113
117
114 /**
118 /**
115 * Variable synchronization/desynchronization operation in the controller
119 * Variable synchronization/desynchronization operation in the controller
116 */
120 */
117 struct Synchronize : public IOperation {
121 struct Synchronize : public IOperation {
118 explicit Synchronize(int index, QUuid syncId, bool synchronize = true)
122 explicit Synchronize(int index, QUuid syncId, bool synchronize = true)
119 : m_Index{index}, m_SyncId{syncId}, m_Synchronize{synchronize}
123 : m_Index{index}, m_SyncId{syncId}, m_Synchronize{synchronize}
120 {
124 {
121 }
125 }
122
126
123 void exec(VariableController &variableController) const override
127 void exec(VariableController &variableController) const override
124 {
128 {
125 if (auto variable = variableController.variableModel()->variable(m_Index)) {
129 if (auto variable = variableController.variableModel()->variable(m_Index)) {
126 if (m_Synchronize) {
130 if (m_Synchronize) {
127 variableController.onAddSynchronized(variable, m_SyncId);
131 variableController.onAddSynchronized(variable, m_SyncId);
128 }
132 }
129 else {
133 else {
130 variableController.desynchronize(variable, m_SyncId);
134 variableController.desynchronize(variable, m_SyncId);
131 }
135 }
132 }
136 }
133 }
137 }
134
138
135 int m_Index; ///< The index of the variable to sync/desync
139 int m_Index; ///< The index of the variable to sync/desync
136 QUuid m_SyncId; ///< The synchronization group of the variable
140 QUuid m_SyncId; ///< The synchronization group of the variable
137 bool m_Synchronize; ///< Performs sync or desync operation
141 bool m_Synchronize; ///< Performs sync or desync operation
138 };
142 };
139
143
140 /**
144 /**
141 * Test Iteration
145 * Test Iteration
142 *
146 *
143 * A test iteration includes an operation to be performed, and a set of expected ranges after each
147 * A test iteration includes an operation to be performed, and a set of expected ranges after each
144 * operation. Each range is tested after the operation to ensure that:
148 * operation. Each range is tested after the operation to ensure that:
145 * - the range of the variable is the expected range
149 * - the range of the variable is the expected range
146 * - the data of the variable are those generated for the expected range
150 * - the data of the variable are those generated for the expected range
147 */
151 */
148 struct Iteration {
152 struct Iteration {
149 std::shared_ptr<IOperation> m_Operation; ///< Operation to perform
153 std::shared_ptr<IOperation> m_Operation; ///< Operation to perform
150 std::map<int, SqpRange> m_ExpectedRanges; ///< Expected ranges (by variable index)
154 std::map<int, SqpRange> m_ExpectedRanges; ///< Expected ranges (by variable index)
151 };
155 };
152
156
153 using Iterations = std::vector<Iteration>;
157 using Iterations = std::vector<Iteration>;
154
158
155 } // namespace
159 } // namespace
156
160
157 Q_DECLARE_METATYPE(Iterations)
161 Q_DECLARE_METATYPE(Iterations)
158
162
159 class TestVariableSync : public QObject {
163 class TestVariableSync : public QObject {
160 Q_OBJECT
164 Q_OBJECT
161
165
162 private slots:
166 private slots:
163 /// Input data for @sa testSync()
167 /// Input data for @sa testSync()
164 void testSync_data();
168 void testSync_data();
165
169
166 /// Tests synchronization between variables through several operations
170 /// Tests synchronization between variables through several operations
167 void testSync();
171 void testSync();
168 };
172 };
169
173
170 void TestVariableSync::testSync_data()
174 void TestVariableSync::testSync_data()
171 {
175 {
172 // ////////////// //
176 // ////////////// //
173 // Test structure //
177 // Test structure //
174 // ////////////// //
178 // ////////////// //
175
179
176 /// @todo
180 QTest::addColumn<QUuid>("syncId");
181 QTest::addColumn<SqpRange>("initialRange");
182 QTest::addColumn<Iterations>("iterations");
177
183
178 // ////////// //
184 // ////////// //
179 // Test cases //
185 // Test cases //
180 // ////////// //
186 // ////////// //
181
187
182 /// @todo
188 /// @todo
183 }
189 }
184
190
185 void TestVariableSync::testSync()
191 void TestVariableSync::testSync()
186 {
192 {
187 /// @todo
193 // Inits controllers
194 TimeController timeController{};
195 VariableController variableController{};
196 variableController.setTimeController(&timeController);
197
198 QFETCH(QUuid, syncId);
199 QFETCH(SqpRange, initialRange);
200 timeController.onTimeToUpdate(initialRange);
201
202 // Synchronization group used
203 variableController.onAddSynchronizationGroupId(syncId);
204
205 // For each iteration:
206 // - execute operation
207 // - compare the variables' state to the expected states
208 QFETCH(Iterations, iterations);
209 for (const auto &iteration : iterations) {
210 iteration.m_Operation->exec(variableController);
211 QTest::qWait(OPERATION_DELAY);
212
213 for (const auto &expectedRangeEntry : iteration.m_ExpectedRanges) {
214 auto variableIndex = expectedRangeEntry.first;
215 auto expectedRange = expectedRangeEntry.second;
216
217 // Gets the variable in the controller
218 auto variable = variableController.variableModel()->variable(variableIndex);
219
220 // Compares variable's range to the expected range
221 QVERIFY(variable != nullptr);
222 auto range = variable->range();
223 QCOMPARE(range, expectedRange);
224
225 // Compares variable's data with values expected for its range
226 auto dataSeries = variable->dataSeries();
227 QVERIFY(dataSeries != nullptr);
228
229 auto it = dataSeries->xAxisRange(range.m_TStart, range.m_TEnd);
230 auto expectedValues = values(range);
231 QVERIFY(std::equal(it.first, it.second, expectedValues.cbegin(), expectedValues.cend(),
232 [](const auto &dataSeriesIt, const auto &expectedValue) {
233 return dataSeriesIt.value() == expectedValue;
234 }));
235 }
236 }
188 }
237 }
189
238
190 QTEST_MAIN(TestVariableSync)
239 QTEST_MAIN(TestVariableSync)
191
240
192 #include "TestVariableSync.moc"
241 #include "TestVariableSync.moc"
General Comments 0
You need to be logged in to leave comments. Login now