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