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