@@ -1,229 +1,241 | |||
|
1 | 1 | #include "FuzzingDefs.h" |
|
2 | 2 | #include "FuzzingOperations.h" |
|
3 | 3 | #include "FuzzingUtils.h" |
|
4 | 4 | |
|
5 | #include "AmdaProvider.h" | |
|
6 | ||
|
5 | 7 | #include <Network/NetworkController.h> |
|
6 | 8 | #include <SqpApplication.h> |
|
7 | 9 | #include <Time/TimeController.h> |
|
8 | 10 | #include <Variable/VariableController.h> |
|
9 | 11 | |
|
10 | 12 | #include <QLoggingCategory> |
|
11 | 13 | #include <QObject> |
|
12 | 14 | #include <QtTest> |
|
13 | 15 | |
|
14 | 16 | #include <memory> |
|
15 | 17 | |
|
16 | 18 | Q_LOGGING_CATEGORY(LOG_TestAmdaFuzzing, "TestAmdaFuzzing") |
|
17 | 19 | |
|
18 | 20 | namespace { |
|
19 | 21 | |
|
20 | 22 | // /////// // |
|
21 | 23 | // Aliases // |
|
22 | 24 | // /////// // |
|
23 | 25 | |
|
24 | 26 | using VariableId = int; |
|
25 | 27 | |
|
26 | 28 | using VariableOperation = std::pair<VariableId, std::shared_ptr<IFuzzingOperation> >; |
|
27 | 29 | using VariablesOperations = std::vector<VariableOperation>; |
|
28 | 30 | |
|
29 | 31 | using OperationsPool = std::set<std::shared_ptr<IFuzzingOperation> >; |
|
30 | 32 | using VariablesPool = std::map<VariableId, std::shared_ptr<Variable> >; |
|
31 | 33 | |
|
32 | 34 | // ///////// // |
|
33 | 35 | // Constants // |
|
34 | 36 | // ///////// // |
|
35 | 37 | |
|
36 | 38 | // Defaults values used when the associated properties have not been set for the test |
|
37 | 39 | const auto NB_MAX_OPERATIONS_DEFAULT_VALUE = 100; |
|
38 | 40 | const auto NB_MAX_VARIABLES_DEFAULT_VALUE = 1; |
|
39 | 41 | const auto AVAILABLE_OPERATIONS_DEFAULT_VALUE |
|
40 | 42 | = QVariant::fromValue(OperationsTypes{FuzzingOperationType::CREATE}); |
|
41 | 43 | |
|
42 | 44 | // /////// // |
|
43 | 45 | // Methods // |
|
44 | 46 | // /////// // |
|
45 | 47 | |
|
46 | 48 | /// Goes through the variables pool and operations pool to determine the set of {variable/operation} |
|
47 | 49 | /// pairs that are valid (i.e. operation that can be executed on variable) |
|
48 | 50 | VariablesOperations availableOperations(const VariablesPool &variablesPool, |
|
49 | 51 | const OperationsPool &operationsPool) |
|
50 | 52 | { |
|
51 | 53 | VariablesOperations result{}; |
|
52 | 54 | |
|
53 | 55 | for (const auto &variablesPoolEntry : variablesPool) { |
|
54 | 56 | auto variableId = variablesPoolEntry.first; |
|
55 | 57 | auto variable = variablesPoolEntry.second; |
|
56 | 58 | |
|
57 | 59 | for (const auto &operation : operationsPool) { |
|
58 | 60 | // A pair is valid if the current operation can be executed on the current variable |
|
59 | 61 | if (operation->canExecute(variable)) { |
|
60 | 62 | result.push_back({variableId, operation}); |
|
61 | 63 | } |
|
62 | 64 | } |
|
63 | 65 | } |
|
64 | 66 | |
|
65 | 67 | return result; |
|
66 | 68 | } |
|
67 | 69 | |
|
68 | 70 | OperationsPool createOperationsPool(const OperationsTypes &types) |
|
69 | 71 | { |
|
70 | 72 | OperationsPool result{}; |
|
71 | 73 | |
|
72 | 74 | std::transform(types.cbegin(), types.cend(), std::inserter(result, result.end()), |
|
73 | 75 | [](const auto &type) { return FuzzingOperationFactory::create(type); }); |
|
74 | 76 | |
|
75 | 77 | return result; |
|
76 | 78 | } |
|
77 | 79 | |
|
78 | 80 | /** |
|
79 | 81 | * Class to run random tests |
|
80 | 82 | */ |
|
81 | 83 | class FuzzingTest { |
|
82 | 84 | public: |
|
83 | 85 | explicit FuzzingTest(VariableController &variableController, Properties properties) |
|
84 | 86 | : m_VariableController{variableController}, |
|
85 | 87 | m_Properties{std::move(properties)}, |
|
86 | 88 | m_VariablesPool{} |
|
87 | 89 | { |
|
88 | 90 | // Inits variables pool: at init, all variables are null |
|
89 | 91 | for (auto variableId = 0; variableId < nbMaxVariables(); ++variableId) { |
|
90 | 92 | m_VariablesPool[variableId] = nullptr; |
|
91 | 93 | } |
|
92 | 94 | } |
|
93 | 95 | |
|
94 | 96 | void execute() |
|
95 | 97 | { |
|
96 | 98 | qCInfo(LOG_TestAmdaFuzzing()) << "Running" << nbMaxOperations() << "operations on" |
|
97 | << nbMaxVariables() << "variables..."; | |
|
99 | << nbMaxVariables() << "variable(s)..."; | |
|
98 | 100 | |
|
99 | 101 | auto canExecute = true; |
|
100 | 102 | for (auto i = 0; i < nbMaxOperations() && canExecute; ++i) { |
|
101 | 103 | // Retrieves all operations that can be executed in the current context |
|
102 | 104 | auto variableOperations = availableOperations(m_VariablesPool, operationsPool()); |
|
103 | 105 | |
|
104 | 106 | canExecute = !variableOperations.empty(); |
|
105 | 107 | if (canExecute) { |
|
106 | 108 | // Of the operations available, chooses a random operation and executes it |
|
107 | 109 | auto variableOperation |
|
108 | 110 | = RandomGenerator::instance().randomChoice(variableOperations); |
|
109 | 111 | |
|
110 | 112 | auto variableId = variableOperation.first; |
|
111 | 113 | auto variable = m_VariablesPool.at(variableId); |
|
112 | 114 | auto fuzzingOperation = variableOperation.second; |
|
113 | 115 | |
|
114 | 116 | fuzzingOperation->execute(variable, m_VariableController, m_Properties); |
|
115 | 117 | |
|
116 | 118 | // Updates variable pool with the new state of the variable after operation |
|
117 | 119 | m_VariablesPool[variableId] = variable; |
|
118 | 120 | } |
|
119 | 121 | else { |
|
120 | 122 | qCInfo(LOG_TestAmdaFuzzing()) |
|
121 | 123 | << "No more operations are available, the execution of the test will stop..."; |
|
122 | 124 | } |
|
123 | 125 | } |
|
124 | 126 | |
|
125 | 127 | qCInfo(LOG_TestAmdaFuzzing()) << "Execution of the test completed."; |
|
126 | 128 | } |
|
127 | 129 | |
|
128 | 130 | private: |
|
129 | 131 | int nbMaxOperations() const |
|
130 | 132 | { |
|
131 | 133 | static auto result |
|
132 | 134 | = m_Properties.value(NB_MAX_OPERATIONS_PROPERTY, NB_MAX_OPERATIONS_DEFAULT_VALUE) |
|
133 | 135 | .toInt(); |
|
134 | 136 | return result; |
|
135 | 137 | } |
|
136 | 138 | |
|
137 | 139 | int nbMaxVariables() const |
|
138 | 140 | { |
|
139 | 141 | static auto result |
|
140 | 142 | = m_Properties.value(NB_MAX_VARIABLES_PROPERTY, NB_MAX_VARIABLES_DEFAULT_VALUE).toInt(); |
|
141 | 143 | return result; |
|
142 | 144 | } |
|
143 | 145 | |
|
144 | 146 | OperationsPool operationsPool() const |
|
145 | 147 | { |
|
146 | 148 | static auto result = createOperationsPool( |
|
147 | 149 | m_Properties.value(AVAILABLE_OPERATIONS_PROPERTY, AVAILABLE_OPERATIONS_DEFAULT_VALUE) |
|
148 | 150 | .value<OperationsTypes>()); |
|
149 | 151 | return result; |
|
150 | 152 | } |
|
151 | 153 | |
|
152 | 154 | VariableController &m_VariableController; |
|
153 | 155 | Properties m_Properties; |
|
154 | 156 | VariablesPool m_VariablesPool; |
|
155 | 157 | }; |
|
156 | 158 | |
|
157 | 159 | } // namespace |
|
158 | 160 | |
|
159 | 161 | class TestAmdaFuzzing : public QObject { |
|
160 | 162 | Q_OBJECT |
|
161 | 163 | |
|
162 | 164 | private slots: |
|
163 | 165 | /// Input data for @sa testFuzzing() |
|
164 | 166 | void testFuzzing_data(); |
|
165 | 167 | void testFuzzing(); |
|
166 | 168 | }; |
|
167 | 169 | |
|
168 | 170 | void TestAmdaFuzzing::testFuzzing_data() |
|
169 | 171 | { |
|
170 | 172 | // ////////////// // |
|
171 | 173 | // Test structure // |
|
172 | 174 | // ////////////// // |
|
173 | 175 | |
|
174 | 176 | QTest::addColumn<Properties>("properties"); // Properties for random test |
|
175 | 177 | |
|
176 | 178 | // ////////// // |
|
177 | 179 | // Test cases // |
|
178 | 180 | // ////////// // |
|
179 | 181 | |
|
180 | ///@todo: complete | |
|
182 | auto maxRange = SqpRange::fromDateTime({2017, 1, 1}, {0, 0}, {2017, 1, 5}, {0, 0}); | |
|
183 | MetadataPool metadataPool{{{"dataType", "vector"}, {"xml:id", "imf"}}}; | |
|
184 | ||
|
185 | // Note: we don't use auto here as we want to pass std::shared_ptr<IDataProvider> as is in the | |
|
186 | // QVariant | |
|
187 | std::shared_ptr<IDataProvider> provider = std::make_shared<AmdaProvider>(); | |
|
188 | ||
|
189 | QTest::newRow("fuzzingTest") << Properties{ | |
|
190 | {MAX_RANGE_PROPERTY, QVariant::fromValue(maxRange)}, | |
|
191 | {METADATA_POOL_PROPERTY, QVariant::fromValue(metadataPool)}, | |
|
192 | {PROVIDER_PROPERTY, QVariant::fromValue(provider)}}; | |
|
181 | 193 | } |
|
182 | 194 | |
|
183 | 195 | void TestAmdaFuzzing::testFuzzing() |
|
184 | 196 | { |
|
185 | 197 | QFETCH(Properties, properties); |
|
186 | 198 | |
|
187 | 199 | auto &variableController = sqpApp->variableController(); |
|
188 | 200 | auto &timeController = sqpApp->timeController(); |
|
189 | 201 | |
|
190 | 202 | // Generates random initial range (bounded to max range) |
|
191 | 203 | auto maxRange = properties.value(MAX_RANGE_PROPERTY, QVariant::fromValue(INVALID_RANGE)) |
|
192 | 204 | .value<SqpRange>(); |
|
193 | 205 | |
|
194 | 206 | QVERIFY(maxRange != INVALID_RANGE); |
|
195 | 207 | |
|
196 | 208 | auto initialRangeStart |
|
197 | 209 | = RandomGenerator::instance().generateDouble(maxRange.m_TStart, maxRange.m_TEnd); |
|
198 | 210 | auto initialRangeEnd |
|
199 | 211 | = RandomGenerator::instance().generateDouble(maxRange.m_TStart, maxRange.m_TEnd); |
|
200 | 212 | if (initialRangeStart > initialRangeEnd) { |
|
201 | 213 | std::swap(initialRangeStart, initialRangeEnd); |
|
202 | 214 | } |
|
203 | 215 | |
|
204 | 216 | // Sets initial range on time controller |
|
205 | 217 | SqpRange initialRange{initialRangeStart, initialRangeEnd}; |
|
206 | 218 | qCInfo(LOG_TestAmdaFuzzing()) << "Setting initial range to" << initialRange << "..."; |
|
207 | 219 | timeController.onTimeToUpdate(initialRange); |
|
208 | 220 | |
|
209 | 221 | FuzzingTest test{variableController, properties}; |
|
210 | 222 | test.execute(); |
|
211 | 223 | } |
|
212 | 224 | |
|
213 | 225 | int main(int argc, char *argv[]) |
|
214 | 226 | { |
|
215 | 227 | QLoggingCategory::setFilterRules( |
|
216 | 228 | "*.warning=false\n" |
|
217 | 229 | "*.info=false\n" |
|
218 | 230 | "*.debug=false\n" |
|
219 | 231 | "FuzzingOperations.info=true\n" |
|
220 | 232 | "TestAmdaFuzzing.info=true\n"); |
|
221 | 233 | |
|
222 | 234 | SqpApplication app{argc, argv}; |
|
223 | 235 | app.setAttribute(Qt::AA_Use96Dpi, true); |
|
224 | 236 | TestAmdaFuzzing testObject{}; |
|
225 | 237 | QTEST_SET_MAIN_SOURCE_PATH |
|
226 | 238 | return QTest::qExec(&testObject, argc, argv); |
|
227 | 239 | } |
|
228 | 240 | |
|
229 | 241 | #include "TestAmdaFuzzing.moc" |
General Comments 0
You need to be logged in to leave comments.
Login now