@@ -1,206 +1,229 | |||
|
1 | 1 | #include "FuzzingOperations.h" |
|
2 | 2 | #include "FuzzingUtils.h" |
|
3 | 3 | |
|
4 | 4 | #include <Data/IDataProvider.h> |
|
5 | 5 | |
|
6 | 6 | #include <Variable/Variable.h> |
|
7 | 7 | #include <Variable/VariableController.h> |
|
8 | 8 | |
|
9 | 9 | #include <QUuid> |
|
10 | 10 | |
|
11 | 11 | #include <functional> |
|
12 | 12 | |
|
13 | 13 | Q_LOGGING_CATEGORY(LOG_FuzzingOperations, "FuzzingOperations") |
|
14 | 14 | |
|
15 | 15 | namespace { |
|
16 | 16 | |
|
17 | 17 | struct CreateOperation : public IFuzzingOperation { |
|
18 | 18 | bool canExecute(VariableId variableId, const FuzzingState &fuzzingState) const override |
|
19 | 19 | { |
|
20 | 20 | // A variable can be created only if it doesn't exist yet |
|
21 | 21 | return fuzzingState.variableState(variableId).m_Variable == nullptr; |
|
22 | 22 | } |
|
23 | 23 | |
|
24 | 24 | void execute(VariableId variableId, FuzzingState &fuzzingState, |
|
25 | 25 | VariableController &variableController, |
|
26 | 26 | const Properties &properties) const override |
|
27 | 27 | { |
|
28 | 28 | // Retrieves metadata pool from properties, and choose one of the metadata entries to |
|
29 | 29 | // associate it with the variable |
|
30 | 30 | auto metaDataPool = properties.value(METADATA_POOL_PROPERTY).value<MetadataPool>(); |
|
31 | 31 | auto variableMetadata = RandomGenerator::instance().randomChoice(metaDataPool); |
|
32 | 32 | |
|
33 | 33 | // Retrieves provider |
|
34 | 34 | auto variableProvider |
|
35 | 35 | = properties.value(PROVIDER_PROPERTY).value<std::shared_ptr<IDataProvider> >(); |
|
36 | 36 | |
|
37 | 37 | auto variableName = QString{"Var_%1"}.arg(QUuid::createUuid().toString()); |
|
38 | 38 | qCInfo(LOG_FuzzingOperations()).noquote() |
|
39 | 39 | << "Creating variable" << variableName << "(metadata:" << variableMetadata << ")..."; |
|
40 | 40 | |
|
41 | 41 | auto newVariable |
|
42 | 42 | = variableController.createVariable(variableName, variableMetadata, variableProvider); |
|
43 | 43 | |
|
44 | 44 | // Updates variable's state |
|
45 | 45 | auto &variableState = fuzzingState.variableState(variableId); |
|
46 | 46 | variableState.m_Range = properties.value(INITIAL_RANGE_PROPERTY).value<SqpRange>(); |
|
47 | 47 | std::swap(variableState.m_Variable, newVariable); |
|
48 | 48 | } |
|
49 | 49 | }; |
|
50 | 50 | |
|
51 | 51 | struct DeleteOperation : public IFuzzingOperation { |
|
52 | 52 | bool canExecute(VariableId variableId, const FuzzingState &fuzzingState) const override |
|
53 | 53 | { |
|
54 | 54 | // A variable can be delete only if it exists |
|
55 | 55 | return fuzzingState.variableState(variableId).m_Variable != nullptr; |
|
56 | 56 | } |
|
57 | 57 | |
|
58 | 58 | void execute(VariableId variableId, FuzzingState &fuzzingState, |
|
59 | 59 | VariableController &variableController, const Properties &) const override |
|
60 | 60 | { |
|
61 | 61 | auto &variableState = fuzzingState.variableState(variableId); |
|
62 | 62 | |
|
63 | 63 | qCInfo(LOG_FuzzingOperations()).noquote() |
|
64 | 64 | << "Deleting variable" << variableState.m_Variable->name() << "..."; |
|
65 | 65 | variableController.deleteVariable(variableState.m_Variable); |
|
66 | 66 | |
|
67 | 67 | // Updates variable's state |
|
68 | 68 | variableState.m_Range = INVALID_RANGE; |
|
69 | 69 | variableState.m_Variable = nullptr; |
|
70 | 70 | } |
|
71 | 71 | }; |
|
72 | 72 | |
|
73 | 73 | /** |
|
74 | 74 | * Defines a move operation through a range. |
|
75 | 75 | * |
|
76 | 76 | * A move operation is determined by three functions: |
|
77 | 77 | * - Two 'move' functions, used to indicate in which direction the beginning and the end of a range |
|
78 | 78 | * are going during the operation. These functions will be: |
|
79 | 79 | * -- {<- / <-} for pan left |
|
80 | 80 | * -- {-> / ->} for pan right |
|
81 | 81 | * -- {-> / <-} for zoom in |
|
82 | 82 | * -- {<- / ->} for zoom out |
|
83 | 83 | * - One 'max move' functions, used to compute the max delta at which the operation can move a |
|
84 | 84 | * range, according to a max range. For exemple, for a range of {1, 5} and a max range of {0, 10}, |
|
85 | 85 | * max deltas will be: |
|
86 | 86 | * -- {0, 4} for pan left |
|
87 | 87 | * -- {6, 10} for pan right |
|
88 | 88 | * -- {3, 3} for zoom in |
|
89 | 89 | * -- {0, 6} for zoom out (same spacing left and right) |
|
90 | 90 | */ |
|
91 | 91 | struct MoveOperation : public IFuzzingOperation { |
|
92 | 92 | using MoveFunction = std::function<double(double currentValue, double maxValue)>; |
|
93 | 93 | using MaxMoveFunction = std::function<double(const SqpRange &range, const SqpRange &maxRange)>; |
|
94 | 94 | |
|
95 | 95 | explicit MoveOperation(MoveFunction rangeStartMoveFun, MoveFunction rangeEndMoveFun, |
|
96 | 96 | MaxMoveFunction maxMoveFun, |
|
97 | 97 | const QString &label = QStringLiteral("Move operation")) |
|
98 | 98 | : m_RangeStartMoveFun{std::move(rangeStartMoveFun)}, |
|
99 | 99 | m_RangeEndMoveFun{std::move(rangeEndMoveFun)}, |
|
100 | 100 | m_MaxMoveFun{std::move(maxMoveFun)}, |
|
101 | 101 | m_Label{label} |
|
102 | 102 | { |
|
103 | 103 | } |
|
104 | 104 | |
|
105 | 105 | bool canExecute(VariableId variableId, const FuzzingState &fuzzingState) const override |
|
106 | 106 | { |
|
107 | 107 | return fuzzingState.variableState(variableId).m_Variable != nullptr; |
|
108 | 108 | } |
|
109 | 109 | |
|
110 | 110 | void execute(VariableId variableId, FuzzingState &fuzzingState, |
|
111 | 111 | VariableController &variableController, |
|
112 | 112 | const Properties &properties) const override |
|
113 | 113 | { |
|
114 | 114 | auto &variableState = fuzzingState.variableState(variableId); |
|
115 | 115 | auto variable = variableState.m_Variable; |
|
116 | 116 | |
|
117 | 117 | // Gets the max range defined |
|
118 | 118 | auto maxRange = properties.value(MAX_RANGE_PROPERTY, QVariant::fromValue(INVALID_RANGE)) |
|
119 | 119 | .value<SqpRange>(); |
|
120 | 120 | auto variableRange = variable->range(); |
|
121 | 121 | |
|
122 | 122 | if (maxRange == INVALID_RANGE || variableRange.m_TStart < maxRange.m_TStart |
|
123 | 123 | || variableRange.m_TEnd > maxRange.m_TEnd) { |
|
124 | 124 | qCWarning(LOG_FuzzingOperations()) << "Can't execute operation: invalid max range"; |
|
125 | 125 | return; |
|
126 | 126 | } |
|
127 | 127 | |
|
128 | 128 | // Computes the max delta at which the variable can move, up to the limits of the max range |
|
129 | 129 | auto deltaMax = m_MaxMoveFun(variable->range(), maxRange); |
|
130 | 130 | |
|
131 | 131 | // Generates random delta that will be used to move variable |
|
132 | 132 | auto delta = RandomGenerator::instance().generateDouble(0, deltaMax); |
|
133 | 133 | |
|
134 | 134 | // Moves variable to its new range |
|
135 | 135 | auto newVariableRange = SqpRange{m_RangeStartMoveFun(variableRange.m_TStart, delta), |
|
136 | 136 | m_RangeEndMoveFun(variableRange.m_TEnd, delta)}; |
|
137 | 137 | qCInfo(LOG_FuzzingOperations()).noquote() |
|
138 | 138 | << "Performing" << m_Label << "on" << variable->name() << "(from" << variableRange |
|
139 | 139 | << "to" << newVariableRange << ")..."; |
|
140 | 140 | variableController.onRequestDataLoading({variable}, newVariableRange, false); |
|
141 | 141 | |
|
142 | 142 | // Updates variable's state |
|
143 | 143 | variableState.m_Range = newVariableRange; |
|
144 | 144 | } |
|
145 | 145 | |
|
146 | 146 | MoveFunction m_RangeStartMoveFun; |
|
147 | 147 | MoveFunction m_RangeEndMoveFun; |
|
148 | 148 | MaxMoveFunction m_MaxMoveFun; |
|
149 | 149 | QString m_Label; |
|
150 | 150 | }; |
|
151 | 151 | |
|
152 | struct SynchronizeOperation : public IFuzzingOperation { | |
|
153 | bool canExecute(VariableId variableId, const FuzzingState &fuzzingState) const override | |
|
154 | { | |
|
155 | } | |
|
156 | ||
|
157 | void execute(VariableId variableId, FuzzingState &fuzzingState, | |
|
158 | VariableController &variableController, const Properties &) const override | |
|
159 | { | |
|
160 | } | |
|
161 | }; | |
|
162 | ||
|
163 | struct DesynchronizeOperation : public IFuzzingOperation { | |
|
164 | bool canExecute(VariableId variableId, const FuzzingState &fuzzingState) const override | |
|
165 | { | |
|
166 | } | |
|
167 | ||
|
168 | void execute(VariableId variableId, FuzzingState &fuzzingState, | |
|
169 | VariableController &variableController, const Properties &) const override | |
|
170 | { | |
|
152 | 171 | struct UnknownOperation : public IFuzzingOperation { |
|
153 | 172 | bool canExecute(VariableId, const FuzzingState &) const override { return false; } |
|
154 | 173 | |
|
155 | 174 | void execute(VariableId, FuzzingState &, VariableController &, |
|
156 | 175 | const Properties &) const override |
|
157 | 176 | { |
|
158 | 177 | } |
|
159 | 178 | }; |
|
160 | 179 | |
|
161 | 180 | } // namespace |
|
162 | 181 | |
|
163 | 182 | std::unique_ptr<IFuzzingOperation> FuzzingOperationFactory::create(FuzzingOperationType type) |
|
164 | 183 | { |
|
165 | 184 | switch (type) { |
|
166 | 185 | case FuzzingOperationType::CREATE: |
|
167 | 186 | return std::make_unique<CreateOperation>(); |
|
168 | 187 | case FuzzingOperationType::DELETE: |
|
169 | 188 | return std::make_unique<DeleteOperation>(); |
|
170 | 189 | case FuzzingOperationType::PAN_LEFT: |
|
171 | 190 | return std::make_unique<MoveOperation>( |
|
172 | 191 | std::minus<double>(), std::minus<double>(), |
|
173 | 192 | [](const SqpRange &range, const SqpRange &maxRange) { |
|
174 | 193 | return range.m_TStart - maxRange.m_TStart; |
|
175 | 194 | }, |
|
176 | 195 | QStringLiteral("Pan left operation")); |
|
177 | 196 | case FuzzingOperationType::PAN_RIGHT: |
|
178 | 197 | return std::make_unique<MoveOperation>( |
|
179 | 198 | std::plus<double>(), std::plus<double>(), |
|
180 | 199 | [](const SqpRange &range, const SqpRange &maxRange) { |
|
181 | 200 | return maxRange.m_TEnd - range.m_TEnd; |
|
182 | 201 | }, |
|
183 | 202 | QStringLiteral("Pan right operation")); |
|
184 | 203 | case FuzzingOperationType::ZOOM_IN: |
|
185 | 204 | return std::make_unique<MoveOperation>( |
|
186 | 205 | std::plus<double>(), std::minus<double>(), |
|
187 | 206 | [](const SqpRange &range, const SqpRange &maxRange) { |
|
188 | 207 | Q_UNUSED(maxRange) |
|
189 | 208 | return range.m_TEnd - (range.m_TStart + range.m_TEnd) / 2.; |
|
190 | 209 | }, |
|
191 | 210 | QStringLiteral("Zoom in operation")); |
|
192 | 211 | case FuzzingOperationType::ZOOM_OUT: |
|
193 | 212 | return std::make_unique<MoveOperation>( |
|
194 | 213 | std::minus<double>(), std::plus<double>(), |
|
195 | 214 | [](const SqpRange &range, const SqpRange &maxRange) { |
|
196 | 215 | return std::min(range.m_TStart - maxRange.m_TStart, |
|
197 | 216 | maxRange.m_TEnd - range.m_TEnd); |
|
198 | 217 | }, |
|
199 | 218 | QStringLiteral("Zoom out operation")); |
|
219 | case FuzzingOperationType::SYNCHRONIZE: | |
|
220 | return std::make_unique<SynchronizeOperation>(); | |
|
221 | case FuzzingOperationType::DESYNCHRONIZE: | |
|
222 | return std::make_unique<DesynchronizeOperation>(); | |
|
200 | 223 | default: |
|
201 | 224 | // Default case returns unknown operation |
|
202 | 225 | break; |
|
203 | 226 | } |
|
204 | 227 | |
|
205 | 228 | return std::make_unique<UnknownOperation>(); |
|
206 | 229 | } |
@@ -1,50 +1,59 | |||
|
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 VariableController; |
|
15 | 15 | |
|
16 | 16 | /** |
|
17 | 17 | * Enumeration of types of existing fuzzing operations |
|
18 | 18 | */ |
|
19 | enum class FuzzingOperationType { CREATE, DELETE, PAN_LEFT, PAN_RIGHT, ZOOM_IN, ZOOM_OUT }; | |
|
19 | enum class FuzzingOperationType { | |
|
20 | CREATE, | |
|
21 | DELETE, | |
|
22 | PAN_LEFT, | |
|
23 | PAN_RIGHT, | |
|
24 | ZOOM_IN, | |
|
25 | ZOOM_OUT, | |
|
26 | SYNCHRONIZE, | |
|
27 | DESYNCHRONIZE | |
|
28 | }; | |
|
20 | 29 | |
|
21 | 30 | /// Interface that represents an operation that can be executed during a fuzzing test |
|
22 | 31 | struct IFuzzingOperation { |
|
23 | 32 | virtual ~IFuzzingOperation() noexcept = default; |
|
24 | 33 | |
|
25 | 34 | /// Checks if the operation can be executed according to the current test's state for the |
|
26 | 35 | /// variable passed in parameter |
|
27 | 36 | virtual bool canExecute(VariableId variableId, const FuzzingState &fuzzingState) const = 0; |
|
28 | 37 | /// Executes the operation on the variable for which its identifier is passed in parameter |
|
29 | 38 | /// @param variableId the variable identifier |
|
30 | 39 | /// @param fuzzingState the current test's state on which to find the variable and execute the |
|
31 | 40 | /// operation |
|
32 | 41 | /// @param variableController the controller associated to the operation |
|
33 | 42 | /// @param properties properties that can be used to configure the operation |
|
34 | 43 | /// @remarks fuzzingState is passed as a reference because, according to the operation, it can |
|
35 | 44 | /// be modified (in/out parameter) |
|
36 | 45 | virtual void execute(VariableId variableId, FuzzingState &fuzzingState, |
|
37 | 46 | VariableController &variableController, |
|
38 | 47 | const Properties &properties = {}) const = 0; |
|
39 | 48 | }; |
|
40 | 49 | |
|
41 | 50 | /// Factory of @sa IFuzzingOperation |
|
42 | 51 | struct FuzzingOperationFactory { |
|
43 | 52 | /// Creates a fuzzing operation from a type |
|
44 | 53 | static std::unique_ptr<IFuzzingOperation> create(FuzzingOperationType type); |
|
45 | 54 | }; |
|
46 | 55 | |
|
47 | 56 | using WeightedOperationsTypes = std::map<FuzzingOperationType, double>; |
|
48 | 57 | Q_DECLARE_METATYPE(WeightedOperationsTypes) |
|
49 | 58 | |
|
50 | 59 | #endif // SCIQLOP_FUZZINGOPERATIONS_H |
@@ -1,344 +1,346 | |||
|
1 | 1 | #include "FuzzingDefs.h" |
|
2 | 2 | #include "FuzzingOperations.h" |
|
3 | 3 | #include "FuzzingUtils.h" |
|
4 | 4 | #include "FuzzingValidators.h" |
|
5 | 5 | |
|
6 | 6 | #include "AmdaProvider.h" |
|
7 | 7 | |
|
8 | 8 | #include <Network/NetworkController.h> |
|
9 | 9 | #include <Settings/SqpSettingsDefs.h> |
|
10 | 10 | #include <SqpApplication.h> |
|
11 | 11 | #include <Time/TimeController.h> |
|
12 | 12 | #include <Variable/Variable.h> |
|
13 | 13 | #include <Variable/VariableController.h> |
|
14 | 14 | |
|
15 | 15 | #include <QLoggingCategory> |
|
16 | 16 | #include <QObject> |
|
17 | 17 | #include <QtTest> |
|
18 | 18 | |
|
19 | 19 | #include <memory> |
|
20 | 20 | |
|
21 | 21 | Q_LOGGING_CATEGORY(LOG_TestAmdaFuzzing, "TestAmdaFuzzing") |
|
22 | 22 | |
|
23 | 23 | namespace { |
|
24 | 24 | |
|
25 | 25 | // /////// // |
|
26 | 26 | // Aliases // |
|
27 | 27 | // /////// // |
|
28 | 28 | |
|
29 | 29 | using Weight = double; |
|
30 | 30 | using Weights = std::vector<Weight>; |
|
31 | 31 | |
|
32 | 32 | using VariableOperation = std::pair<VariableId, std::shared_ptr<IFuzzingOperation> >; |
|
33 | 33 | using VariablesOperations = std::vector<VariableOperation>; |
|
34 | 34 | |
|
35 | 35 | using WeightedOperationsPool = std::map<std::shared_ptr<IFuzzingOperation>, Weight>; |
|
36 | 36 | using Validators = std::vector<std::shared_ptr<IFuzzingValidator> >; |
|
37 | 37 | |
|
38 | 38 | // ///////// // |
|
39 | 39 | // Constants // |
|
40 | 40 | // ///////// // |
|
41 | 41 | |
|
42 | 42 | // Defaults values used when the associated properties have not been set for the test |
|
43 | 43 | const auto NB_MAX_OPERATIONS_DEFAULT_VALUE = 100; |
|
44 | 44 | const auto NB_MAX_SYNC_GROUPS_DEFAULT_VALUE = 1; |
|
45 | 45 | const auto NB_MAX_VARIABLES_DEFAULT_VALUE = 1; |
|
46 | 46 | const auto AVAILABLE_OPERATIONS_DEFAULT_VALUE = QVariant::fromValue(WeightedOperationsTypes{ |
|
47 | 47 | {FuzzingOperationType::CREATE, 1.}, |
|
48 | 48 | {FuzzingOperationType::DELETE, 0.1}, // Delete operation is less frequent |
|
49 | 49 | {FuzzingOperationType::PAN_LEFT, 1.}, |
|
50 | 50 | {FuzzingOperationType::PAN_RIGHT, 1.}, |
|
51 | 51 | {FuzzingOperationType::ZOOM_IN, 1.}, |
|
52 |
{FuzzingOperationType::ZOOM_OUT, 1.} |
|
|
52 | {FuzzingOperationType::ZOOM_OUT, 1.}, | |
|
53 | {FuzzingOperationType::SYNCHRONIZE, 0.8}, | |
|
54 | {FuzzingOperationType::DESYNCHRONIZE, 0.4}}); | |
|
53 | 55 | const auto CACHE_TOLERANCE_DEFAULT_VALUE = 0.2; |
|
54 | 56 | |
|
55 | 57 | /// Delay between each operation (in ms) |
|
56 | 58 | const auto OPERATION_DELAY_DEFAULT_VALUE = 3000; |
|
57 | 59 | |
|
58 | 60 | /// Validators for the tests (executed in the order in which they're defined) |
|
59 | 61 | const auto VALIDATORS_DEFAULT_VALUE = QVariant::fromValue( |
|
60 | 62 | ValidatorsTypes{{FuzzingValidatorType::RANGE, FuzzingValidatorType::DATA}}); |
|
61 | 63 | |
|
62 | 64 | // /////// // |
|
63 | 65 | // Methods // |
|
64 | 66 | // /////// // |
|
65 | 67 | |
|
66 | 68 | /// Goes through the variables pool and operations pool to determine the set of {variable/operation} |
|
67 | 69 | /// pairs that are valid (i.e. operation that can be executed on variable) |
|
68 | 70 | std::pair<VariablesOperations, Weights> |
|
69 | 71 | availableOperations(const FuzzingState &fuzzingState, const WeightedOperationsPool &operationsPool) |
|
70 | 72 | { |
|
71 | 73 | VariablesOperations result{}; |
|
72 | 74 | Weights weights{}; |
|
73 | 75 | |
|
74 | 76 | for (const auto &variablesPoolEntry : fuzzingState.m_VariablesPool) { |
|
75 | 77 | auto variableId = variablesPoolEntry.first; |
|
76 | 78 | |
|
77 | 79 | for (const auto &operationsPoolEntry : operationsPool) { |
|
78 | 80 | auto operation = operationsPoolEntry.first; |
|
79 | 81 | auto weight = operationsPoolEntry.second; |
|
80 | 82 | |
|
81 | 83 | // A pair is valid if the current operation can be executed on the current variable |
|
82 | 84 | if (operation->canExecute(variableId, fuzzingState)) { |
|
83 | 85 | result.push_back({variableId, operation}); |
|
84 | 86 | weights.push_back(weight); |
|
85 | 87 | } |
|
86 | 88 | } |
|
87 | 89 | } |
|
88 | 90 | |
|
89 | 91 | return {result, weights}; |
|
90 | 92 | } |
|
91 | 93 | |
|
92 | 94 | WeightedOperationsPool createOperationsPool(const WeightedOperationsTypes &types) |
|
93 | 95 | { |
|
94 | 96 | WeightedOperationsPool result{}; |
|
95 | 97 | |
|
96 | 98 | std::transform( |
|
97 | 99 | types.cbegin(), types.cend(), std::inserter(result, result.end()), [](const auto &type) { |
|
98 | 100 | return std::make_pair(FuzzingOperationFactory::create(type.first), type.second); |
|
99 | 101 | }); |
|
100 | 102 | |
|
101 | 103 | return result; |
|
102 | 104 | } |
|
103 | 105 | |
|
104 | 106 | Validators createValidators(const ValidatorsTypes &types) |
|
105 | 107 | { |
|
106 | 108 | Validators result{}; |
|
107 | 109 | |
|
108 | 110 | std::transform(types.cbegin(), types.cend(), std::inserter(result, result.end()), |
|
109 | 111 | [](const auto &type) { return FuzzingValidatorFactory::create(type); }); |
|
110 | 112 | |
|
111 | 113 | return result; |
|
112 | 114 | } |
|
113 | 115 | |
|
114 | 116 | /** |
|
115 | 117 | * Validates all the variables' states passed in parameter, according to a set of validators |
|
116 | 118 | * @param variablesPool the variables' states |
|
117 | 119 | * @param validators the validators used for validation |
|
118 | 120 | */ |
|
119 | 121 | void validate(const VariablesPool &variablesPool, const Validators &validators) |
|
120 | 122 | { |
|
121 | 123 | for (const auto &variablesPoolEntry : variablesPool) { |
|
122 | 124 | auto variableId = variablesPoolEntry.first; |
|
123 | 125 | const auto &variableState = variablesPoolEntry.second; |
|
124 | 126 | |
|
125 | 127 | auto variableMessage = variableState.m_Variable ? variableState.m_Variable->name() |
|
126 | 128 | : QStringLiteral("null variable"); |
|
127 | 129 | qCInfo(LOG_TestAmdaFuzzing()).noquote() << "Validating state of variable at index" |
|
128 | 130 | << variableId << "(" << variableMessage << ")..."; |
|
129 | 131 | |
|
130 | 132 | for (const auto &validator : validators) { |
|
131 | 133 | validator->validate(VariableState{variableState}); |
|
132 | 134 | } |
|
133 | 135 | |
|
134 | 136 | qCInfo(LOG_TestAmdaFuzzing()).noquote() << "Validation completed."; |
|
135 | 137 | } |
|
136 | 138 | } |
|
137 | 139 | |
|
138 | 140 | /** |
|
139 | 141 | * Class to run random tests |
|
140 | 142 | */ |
|
141 | 143 | class FuzzingTest { |
|
142 | 144 | public: |
|
143 | 145 | explicit FuzzingTest(VariableController &variableController, Properties properties) |
|
144 | 146 | : m_VariableController{variableController}, |
|
145 | 147 | m_Properties{std::move(properties)}, |
|
146 | 148 | m_FuzzingState{} |
|
147 | 149 | { |
|
148 | 150 | // Inits variables pool: at init, all variables are null |
|
149 | 151 | for (auto variableId = 0; variableId < nbMaxVariables(); ++variableId) { |
|
150 | 152 | m_FuzzingState.m_VariablesPool[variableId] = VariableState{}; |
|
151 | 153 | } |
|
152 | 154 | |
|
153 | 155 | // Inits sync groups and registers them into the variable controller |
|
154 | 156 | for (auto i = 0; i < nbMaxSyncGroups(); ++i) { |
|
155 | 157 | auto syncGroupId = SyncGroupId::createUuid(); |
|
156 | 158 | variableController.onAddSynchronizationGroupId(syncGroupId); |
|
157 | 159 | m_FuzzingState.m_SyncGroupsPool[syncGroupId] = SyncGroup{}; |
|
158 | 160 | } |
|
159 | 161 | } |
|
160 | 162 | |
|
161 | 163 | void execute() |
|
162 | 164 | { |
|
163 | 165 | qCInfo(LOG_TestAmdaFuzzing()).noquote() << "Running" << nbMaxOperations() << "operations on" |
|
164 | 166 | << nbMaxVariables() << "variable(s)..."; |
|
165 | 167 | |
|
166 | 168 | auto canExecute = true; |
|
167 | 169 | for (auto i = 0; i < nbMaxOperations() && canExecute; ++i) { |
|
168 | 170 | // Retrieves all operations that can be executed in the current context |
|
169 | 171 | VariablesOperations variableOperations{}; |
|
170 | 172 | Weights weights{}; |
|
171 | 173 | std::tie(variableOperations, weights) |
|
172 | 174 | = availableOperations(m_FuzzingState, operationsPool()); |
|
173 | 175 | |
|
174 | 176 | canExecute = !variableOperations.empty(); |
|
175 | 177 | if (canExecute) { |
|
176 | 178 | // Of the operations available, chooses a random operation and executes it |
|
177 | 179 | auto variableOperation |
|
178 | 180 | = RandomGenerator::instance().randomChoice(variableOperations, weights); |
|
179 | 181 | |
|
180 | 182 | auto variableId = variableOperation.first; |
|
181 | 183 | auto fuzzingOperation = variableOperation.second; |
|
182 | 184 | |
|
183 | 185 | fuzzingOperation->execute(variableId, m_FuzzingState, m_VariableController, |
|
184 | 186 | m_Properties); |
|
185 | 187 | QTest::qWait(operationDelay()); |
|
186 | 188 | |
|
187 | 189 | // Validates variables |
|
188 | 190 | validate(m_FuzzingState.m_VariablesPool, validators()); |
|
189 | 191 | } |
|
190 | 192 | else { |
|
191 | 193 | qCInfo(LOG_TestAmdaFuzzing()).noquote() |
|
192 | 194 | << "No more operations are available, the execution of the test will stop..."; |
|
193 | 195 | } |
|
194 | 196 | } |
|
195 | 197 | |
|
196 | 198 | qCInfo(LOG_TestAmdaFuzzing()).noquote() << "Execution of the test completed."; |
|
197 | 199 | } |
|
198 | 200 | |
|
199 | 201 | private: |
|
200 | 202 | int nbMaxOperations() const |
|
201 | 203 | { |
|
202 | 204 | static auto result |
|
203 | 205 | = m_Properties.value(NB_MAX_OPERATIONS_PROPERTY, NB_MAX_OPERATIONS_DEFAULT_VALUE) |
|
204 | 206 | .toInt(); |
|
205 | 207 | return result; |
|
206 | 208 | } |
|
207 | 209 | |
|
208 | 210 | int nbMaxSyncGroups() const |
|
209 | 211 | { |
|
210 | 212 | static auto result |
|
211 | 213 | = m_Properties.value(NB_MAX_SYNC_GROUPS_PROPERTY, NB_MAX_SYNC_GROUPS_DEFAULT_VALUE) |
|
212 | 214 | .toInt(); |
|
213 | 215 | return result; |
|
214 | 216 | } |
|
215 | 217 | |
|
216 | 218 | int nbMaxVariables() const |
|
217 | 219 | { |
|
218 | 220 | static auto result |
|
219 | 221 | = m_Properties.value(NB_MAX_VARIABLES_PROPERTY, NB_MAX_VARIABLES_DEFAULT_VALUE).toInt(); |
|
220 | 222 | return result; |
|
221 | 223 | } |
|
222 | 224 | |
|
223 | 225 | int operationDelay() const |
|
224 | 226 | { |
|
225 | 227 | static auto result |
|
226 | 228 | = m_Properties.value(OPERATION_DELAY_PROPERTY, OPERATION_DELAY_DEFAULT_VALUE).toInt(); |
|
227 | 229 | return result; |
|
228 | 230 | } |
|
229 | 231 | |
|
230 | 232 | WeightedOperationsPool operationsPool() const |
|
231 | 233 | { |
|
232 | 234 | static auto result = createOperationsPool( |
|
233 | 235 | m_Properties.value(AVAILABLE_OPERATIONS_PROPERTY, AVAILABLE_OPERATIONS_DEFAULT_VALUE) |
|
234 | 236 | .value<WeightedOperationsTypes>()); |
|
235 | 237 | return result; |
|
236 | 238 | } |
|
237 | 239 | |
|
238 | 240 | Validators validators() const |
|
239 | 241 | { |
|
240 | 242 | static auto result |
|
241 | 243 | = createValidators(m_Properties.value(VALIDATORS_PROPERTY, VALIDATORS_DEFAULT_VALUE) |
|
242 | 244 | .value<ValidatorsTypes>()); |
|
243 | 245 | return result; |
|
244 | 246 | } |
|
245 | 247 | |
|
246 | 248 | VariableController &m_VariableController; |
|
247 | 249 | Properties m_Properties; |
|
248 | 250 | FuzzingState m_FuzzingState; |
|
249 | 251 | }; |
|
250 | 252 | |
|
251 | 253 | } // namespace |
|
252 | 254 | |
|
253 | 255 | class TestAmdaFuzzing : public QObject { |
|
254 | 256 | Q_OBJECT |
|
255 | 257 | |
|
256 | 258 | private slots: |
|
257 | 259 | /// Input data for @sa testFuzzing() |
|
258 | 260 | void testFuzzing_data(); |
|
259 | 261 | void testFuzzing(); |
|
260 | 262 | }; |
|
261 | 263 | |
|
262 | 264 | void TestAmdaFuzzing::testFuzzing_data() |
|
263 | 265 | { |
|
264 | 266 | // ////////////// // |
|
265 | 267 | // Test structure // |
|
266 | 268 | // ////////////// // |
|
267 | 269 | |
|
268 | 270 | QTest::addColumn<Properties>("properties"); // Properties for random test |
|
269 | 271 | |
|
270 | 272 | // ////////// // |
|
271 | 273 | // Test cases // |
|
272 | 274 | // ////////// // |
|
273 | 275 | |
|
274 | 276 | auto maxRange = SqpRange::fromDateTime({2017, 1, 1}, {0, 0}, {2017, 1, 5}, {0, 0}); |
|
275 | 277 | MetadataPool metadataPool{{{"dataType", "vector"}, {"xml:id", "imf"}}}; |
|
276 | 278 | |
|
277 | 279 | // Note: we don't use auto here as we want to pass std::shared_ptr<IDataProvider> as is in the |
|
278 | 280 | // QVariant |
|
279 | 281 | std::shared_ptr<IDataProvider> provider = std::make_shared<AmdaProvider>(); |
|
280 | 282 | |
|
281 | 283 | QTest::newRow("fuzzingTest") << Properties{ |
|
282 | 284 | {MAX_RANGE_PROPERTY, QVariant::fromValue(maxRange)}, |
|
283 | 285 | {METADATA_POOL_PROPERTY, QVariant::fromValue(metadataPool)}, |
|
284 | 286 | {PROVIDER_PROPERTY, QVariant::fromValue(provider)}}; |
|
285 | 287 | } |
|
286 | 288 | |
|
287 | 289 | void TestAmdaFuzzing::testFuzzing() |
|
288 | 290 | { |
|
289 | 291 | QFETCH(Properties, properties); |
|
290 | 292 | |
|
291 | 293 | // Sets cache property |
|
292 | 294 | QSettings settings{}; |
|
293 | 295 | auto cacheTolerance = properties.value(CACHE_TOLERANCE_PROPERTY, CACHE_TOLERANCE_DEFAULT_VALUE); |
|
294 | 296 | settings.setValue(GENERAL_TOLERANCE_AT_INIT_KEY, cacheTolerance); |
|
295 | 297 | settings.setValue(GENERAL_TOLERANCE_AT_UPDATE_KEY, cacheTolerance); |
|
296 | 298 | |
|
297 | 299 | auto &variableController = sqpApp->variableController(); |
|
298 | 300 | auto &timeController = sqpApp->timeController(); |
|
299 | 301 | |
|
300 | 302 | // Generates random initial range (bounded to max range) |
|
301 | 303 | auto maxRange = properties.value(MAX_RANGE_PROPERTY, QVariant::fromValue(INVALID_RANGE)) |
|
302 | 304 | .value<SqpRange>(); |
|
303 | 305 | |
|
304 | 306 | QVERIFY(maxRange != INVALID_RANGE); |
|
305 | 307 | |
|
306 | 308 | auto initialRangeStart |
|
307 | 309 | = RandomGenerator::instance().generateDouble(maxRange.m_TStart, maxRange.m_TEnd); |
|
308 | 310 | auto initialRangeEnd |
|
309 | 311 | = RandomGenerator::instance().generateDouble(maxRange.m_TStart, maxRange.m_TEnd); |
|
310 | 312 | if (initialRangeStart > initialRangeEnd) { |
|
311 | 313 | std::swap(initialRangeStart, initialRangeEnd); |
|
312 | 314 | } |
|
313 | 315 | |
|
314 | 316 | // Sets initial range on time controller |
|
315 | 317 | SqpRange initialRange{initialRangeStart, initialRangeEnd}; |
|
316 | 318 | qCInfo(LOG_TestAmdaFuzzing()).noquote() << "Setting initial range to" << initialRange << "..."; |
|
317 | 319 | timeController.onTimeToUpdate(initialRange); |
|
318 | 320 | properties.insert(INITIAL_RANGE_PROPERTY, QVariant::fromValue(initialRange)); |
|
319 | 321 | |
|
320 | 322 | FuzzingTest test{variableController, properties}; |
|
321 | 323 | test.execute(); |
|
322 | 324 | } |
|
323 | 325 | |
|
324 | 326 | int main(int argc, char *argv[]) |
|
325 | 327 | { |
|
326 | 328 | QLoggingCategory::setFilterRules( |
|
327 | 329 | "*.warning=false\n" |
|
328 | 330 | "*.info=false\n" |
|
329 | 331 | "*.debug=false\n" |
|
330 | 332 | "FuzzingOperations.info=true\n" |
|
331 | 333 | "FuzzingValidators.info=true\n" |
|
332 | 334 | "TestAmdaFuzzing.info=true\n"); |
|
333 | 335 | |
|
334 | 336 | SqpApplication app{argc, argv}; |
|
335 | 337 | SqpApplication::setOrganizationName("LPP"); |
|
336 | 338 | SqpApplication::setOrganizationDomain("lpp.fr"); |
|
337 | 339 | SqpApplication::setApplicationName("SciQLop-TestFuzzing"); |
|
338 | 340 | app.setAttribute(Qt::AA_Use96Dpi, true); |
|
339 | 341 | TestAmdaFuzzing testObject{}; |
|
340 | 342 | QTEST_SET_MAIN_SOURCE_PATH |
|
341 | 343 | return QTest::qExec(&testObject, argc, argv); |
|
342 | 344 | } |
|
343 | 345 | |
|
344 | 346 | #include "TestAmdaFuzzing.moc" |
General Comments 0
You need to be logged in to leave comments.
Login now