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