@@ -1,138 +1,192 | |||
|
1 | 1 | #include <QObject> |
|
2 | 2 | #include <QtTest> |
|
3 | 3 | |
|
4 | 4 | #include <memory> |
|
5 | 5 | |
|
6 | #include <Data/DataProviderParameters.h> | |
|
7 | #include <Data/IDataProvider.h> | |
|
8 | #include <Data/ScalarSeries.h> | |
|
6 | 9 | #include <Variable/Variable.h> |
|
7 | 10 | #include <Variable/VariableController.h> |
|
8 | 11 | #include <Variable/VariableModel.h> |
|
9 | 12 | |
|
10 | 13 | namespace { |
|
11 | 14 | |
|
12 | 15 | /** |
|
16 | * 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) | |
|
18 | * | |
|
19 | * 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} | |
|
21 | */ | |
|
22 | std::vector<double> values(const SqpRange &range) | |
|
23 | { | |
|
24 | QTime referenceTime{0, 0}; | |
|
25 | ||
|
26 | std::vector<double> result{}; | |
|
27 | ||
|
28 | for (auto i = range.m_TStart; i <= range.m_TEnd; ++i) { | |
|
29 | auto time = DateUtils::dateTime(i).time(); | |
|
30 | result.push_back(referenceTime.secsTo(time)); | |
|
31 | } | |
|
32 | ||
|
33 | return result; | |
|
34 | } | |
|
35 | ||
|
36 | /// Provider used for the tests | |
|
37 | class TestProvider : public IDataProvider { | |
|
38 | std::shared_ptr<IDataProvider> clone() const { return std::make_shared<TestProvider>(); } | |
|
39 | ||
|
40 | void requestDataLoading(QUuid acqIdentifier, const DataProviderParameters ¶meters) override | |
|
41 | { | |
|
42 | const auto &ranges = parameters.m_Times; | |
|
43 | ||
|
44 | for (const auto &range : ranges) { | |
|
45 | // Generates data series | |
|
46 | auto valuesData = values(range); | |
|
47 | ||
|
48 | std::vector<double> xAxisData{}; | |
|
49 | for (auto i = range.m_TStart; i <= range.m_TEnd; ++i) { | |
|
50 | xAxisData.push_back(i); | |
|
51 | } | |
|
52 | ||
|
53 | auto dataSeries = std::make_shared<ScalarSeries>( | |
|
54 | std::move(xAxisData), std::move(valuesData), Unit{"t", true}, Unit{}); | |
|
55 | ||
|
56 | emit dataProvided(acqIdentifier, dataSeries, range); | |
|
57 | } | |
|
58 | } | |
|
59 | ||
|
60 | void requestDataAborting(QUuid acqIdentifier) override | |
|
61 | { | |
|
62 | // Does nothing | |
|
63 | } | |
|
64 | }; | |
|
65 | ||
|
66 | /** | |
|
13 | 67 | * Interface representing an operation performed on a variable controller. |
|
14 | 68 | * This interface is used in tests to apply a set of operations and check the status of the |
|
15 | 69 | * controller after each operation |
|
16 | 70 | */ |
|
17 | 71 | struct IOperation { |
|
18 | 72 | virtual ~IOperation() = default; |
|
19 | 73 | /// Executes the operation on the variable controller |
|
20 | 74 | virtual void exec(VariableController &variableController) const = 0; |
|
21 | 75 | }; |
|
22 | 76 | |
|
23 | 77 | /** |
|
24 | 78 | *Variable creation operation in the controller |
|
25 | 79 | */ |
|
26 | 80 | struct Create : public IOperation { |
|
27 | 81 | explicit Create(int index) : m_Index{index} {} |
|
28 | 82 | |
|
29 | 83 | void exec(VariableController &variableController) const override |
|
30 | 84 | { |
|
31 | 85 | auto variable = variableController.createVariable(QString::number(m_Index), {}, |
|
32 | 86 | std::make_unique<TestProvider>()); |
|
33 | 87 | } |
|
34 | 88 | |
|
35 | 89 | int m_Index; ///< The index of the variable to create in the controller |
|
36 | 90 | }; |
|
37 | 91 | |
|
38 | 92 | /** |
|
39 | 93 | * Variable move/shift operation in the controller |
|
40 | 94 | */ |
|
41 | 95 | struct Move : public IOperation { |
|
42 | 96 | explicit Move(int index, const SqpRange &newRange, bool shift = false) |
|
43 | 97 | : m_Index{index}, m_NewRange{newRange}, m_Shift{shift} |
|
44 | 98 | { |
|
45 | 99 | } |
|
46 | 100 | |
|
47 | 101 | void exec(VariableController &variableController) const override |
|
48 | 102 | { |
|
49 | 103 | if (auto variable = variableController.variableModel()->variable(m_Index)) { |
|
50 | 104 | variableController.onRequestDataLoading({variable}, m_NewRange, variable->range(), |
|
51 | 105 | !m_Shift); |
|
52 | 106 | } |
|
53 | 107 | } |
|
54 | 108 | |
|
55 | 109 | int m_Index; ///< The index of the variable to move |
|
56 | 110 | SqpRange m_NewRange; ///< The new range of the variable |
|
57 | 111 | bool m_Shift; ///< Performs a shift ( |
|
58 | 112 | }; |
|
59 | 113 | |
|
60 | 114 | /** |
|
61 | 115 | * Variable synchronization/desynchronization operation in the controller |
|
62 | 116 | */ |
|
63 | 117 | struct Synchronize : public IOperation { |
|
64 | 118 | explicit Synchronize(int index, QUuid syncId, bool synchronize = true) |
|
65 | 119 | : m_Index{index}, m_SyncId{syncId}, m_Synchronize{synchronize} |
|
66 | 120 | { |
|
67 | 121 | } |
|
68 | 122 | |
|
69 | 123 | void exec(VariableController &variableController) const override |
|
70 | 124 | { |
|
71 | 125 | if (auto variable = variableController.variableModel()->variable(m_Index)) { |
|
72 | 126 | if (m_Synchronize) { |
|
73 | 127 | variableController.onAddSynchronized(variable, m_SyncId); |
|
74 | 128 | } |
|
75 | 129 | else { |
|
76 | 130 | variableController.desynchronize(variable, m_SyncId); |
|
77 | 131 | } |
|
78 | 132 | } |
|
79 | 133 | } |
|
80 | 134 | |
|
81 | 135 | int m_Index; ///< The index of the variable to sync/desync |
|
82 | 136 | QUuid m_SyncId; ///< The synchronization group of the variable |
|
83 | 137 | bool m_Synchronize; ///< Performs sync or desync operation |
|
84 | 138 | }; |
|
85 | 139 | |
|
86 | 140 | /** |
|
87 | 141 | * Test Iteration |
|
88 | 142 | * |
|
89 | 143 | * A test iteration includes an operation to be performed, and a set of expected ranges after each |
|
90 | 144 | * operation. Each range is tested after the operation to ensure that: |
|
91 | 145 | * - the range of the variable is the expected range |
|
92 | 146 | * - the data of the variable are those generated for the expected range |
|
93 | 147 | */ |
|
94 | 148 | struct Iteration { |
|
95 | 149 | std::shared_ptr<IOperation> m_Operation; ///< Operation to perform |
|
96 | 150 | std::map<int, SqpRange> m_ExpectedRanges; ///< Expected ranges (by variable index) |
|
97 | 151 | }; |
|
98 | 152 | |
|
99 | 153 | using Iterations = std::vector<Iteration>; |
|
100 | 154 | |
|
101 | 155 | } // namespace |
|
102 | 156 | |
|
103 | 157 | Q_DECLARE_METATYPE(Iterations) |
|
104 | 158 | |
|
105 | 159 | class TestVariableSync : public QObject { |
|
106 | 160 | Q_OBJECT |
|
107 | 161 | |
|
108 | 162 | private slots: |
|
109 | 163 | /// Input data for @sa testSync() |
|
110 | 164 | void testSync_data(); |
|
111 | 165 | |
|
112 | 166 | /// Tests synchronization between variables through several operations |
|
113 | 167 | void testSync(); |
|
114 | 168 | }; |
|
115 | 169 | |
|
116 | 170 | void TestVariableSync::testSync_data() |
|
117 | 171 | { |
|
118 | 172 | // ////////////// // |
|
119 | 173 | // Test structure // |
|
120 | 174 | // ////////////// // |
|
121 | 175 | |
|
122 | 176 | /// @todo |
|
123 | 177 | |
|
124 | 178 | // ////////// // |
|
125 | 179 | // Test cases // |
|
126 | 180 | // ////////// // |
|
127 | 181 | |
|
128 | 182 | /// @todo |
|
129 | 183 | } |
|
130 | 184 | |
|
131 | 185 | void TestVariableSync::testSync() |
|
132 | 186 | { |
|
133 | 187 | /// @todo |
|
134 | 188 | } |
|
135 | 189 | |
|
136 | 190 | QTEST_MAIN(TestVariableSync) |
|
137 | 191 | |
|
138 | 192 | #include "TestVariableSync.moc" |
General Comments 0
You need to be logged in to leave comments.
Login now