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