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