@@ -1,108 +1,109 | |||
|
1 | 1 | #include "FuzzingDefs.h" |
|
2 | 2 | |
|
3 | const QString ACQUISITION_TIMEOUT_PROPERTY = QStringLiteral("acquisitionTimeout"); | |
|
3 | 4 | const QString NB_MAX_OPERATIONS_PROPERTY = QStringLiteral("component"); |
|
4 | 5 | const QString NB_MAX_SYNC_GROUPS_PROPERTY = QStringLiteral("nbSyncGroups"); |
|
5 | 6 | const QString NB_MAX_VARIABLES_PROPERTY = QStringLiteral("nbMaxVariables"); |
|
6 | 7 | const QString AVAILABLE_OPERATIONS_PROPERTY = QStringLiteral("availableOperations"); |
|
7 | 8 | const QString CACHE_TOLERANCE_PROPERTY = QStringLiteral("cacheTolerance"); |
|
8 | 9 | const QString INITIAL_RANGE_PROPERTY = QStringLiteral("initialRange"); |
|
9 | 10 | const QString MAX_RANGE_PROPERTY = QStringLiteral("maxRange"); |
|
10 | 11 | const QString METADATA_POOL_PROPERTY = QStringLiteral("metadataPool"); |
|
11 | 12 | const QString PROVIDER_PROPERTY = QStringLiteral("provider"); |
|
12 | 13 | const QString OPERATION_DELAY_BOUNDS_PROPERTY = QStringLiteral("operationDelays"); |
|
13 | 14 | const QString VALIDATORS_PROPERTY = QStringLiteral("validators"); |
|
14 | 15 | const QString VALIDATION_FREQUENCY_BOUNDS_PROPERTY = QStringLiteral("validationFrequencyBounds"); |
|
15 | 16 | |
|
16 | 17 | // //////////// // |
|
17 | 18 | // FuzzingState // |
|
18 | 19 | // //////////// // |
|
19 | 20 | |
|
20 | 21 | const SyncGroup &FuzzingState::syncGroup(SyncGroupId id) const |
|
21 | 22 | { |
|
22 | 23 | return m_SyncGroupsPool.at(id); |
|
23 | 24 | } |
|
24 | 25 | |
|
25 | 26 | SyncGroup &FuzzingState::syncGroup(SyncGroupId id) |
|
26 | 27 | { |
|
27 | 28 | return m_SyncGroupsPool.at(id); |
|
28 | 29 | } |
|
29 | 30 | |
|
30 | 31 | const VariableState &FuzzingState::variableState(VariableId id) const |
|
31 | 32 | { |
|
32 | 33 | return m_VariablesPool.at(id); |
|
33 | 34 | } |
|
34 | 35 | |
|
35 | 36 | VariableState &FuzzingState::variableState(VariableId id) |
|
36 | 37 | { |
|
37 | 38 | return m_VariablesPool.at(id); |
|
38 | 39 | } |
|
39 | 40 | |
|
40 | 41 | SyncGroupId FuzzingState::syncGroupId(VariableId variableId) const |
|
41 | 42 | { |
|
42 | 43 | auto end = m_SyncGroupsPool.cend(); |
|
43 | 44 | auto it |
|
44 | 45 | = std::find_if(m_SyncGroupsPool.cbegin(), end, [&variableId](const auto &syncGroupEntry) { |
|
45 | 46 | const auto &syncGroup = syncGroupEntry.second; |
|
46 | 47 | return syncGroup.m_Variables.find(variableId) != syncGroup.m_Variables.end(); |
|
47 | 48 | }); |
|
48 | 49 | |
|
49 | 50 | return it != end ? it->first : SyncGroupId{}; |
|
50 | 51 | } |
|
51 | 52 | |
|
52 | 53 | std::vector<SyncGroupId> FuzzingState::syncGroupsIds() const |
|
53 | 54 | { |
|
54 | 55 | std::vector<SyncGroupId> result{}; |
|
55 | 56 | |
|
56 | 57 | for (const auto &entry : m_SyncGroupsPool) { |
|
57 | 58 | result.push_back(entry.first); |
|
58 | 59 | } |
|
59 | 60 | |
|
60 | 61 | return result; |
|
61 | 62 | } |
|
62 | 63 | |
|
63 | 64 | void FuzzingState::synchronizeVariable(VariableId variableId, SyncGroupId syncGroupId) |
|
64 | 65 | { |
|
65 | 66 | if (syncGroupId.isNull()) { |
|
66 | 67 | return; |
|
67 | 68 | } |
|
68 | 69 | |
|
69 | 70 | // Registers variable into sync group: if it's the first variable, sets the variable range as |
|
70 | 71 | // the sync group range |
|
71 | 72 | auto &syncGroup = m_SyncGroupsPool.at(syncGroupId); |
|
72 | 73 | syncGroup.m_Variables.insert(variableId); |
|
73 | 74 | if (syncGroup.m_Variables.size() == 1) { |
|
74 | 75 | auto &variableState = m_VariablesPool.at(variableId); |
|
75 | 76 | syncGroup.m_Range = variableState.m_Range; |
|
76 | 77 | } |
|
77 | 78 | } |
|
78 | 79 | |
|
79 | 80 | void FuzzingState::desynchronizeVariable(VariableId variableId, SyncGroupId syncGroupId) |
|
80 | 81 | { |
|
81 | 82 | if (syncGroupId.isNull()) { |
|
82 | 83 | return; |
|
83 | 84 | } |
|
84 | 85 | |
|
85 | 86 | // Unregisters variable from sync group: if there is no more variable in the group, resets the |
|
86 | 87 | // range |
|
87 | 88 | auto &syncGroup = m_SyncGroupsPool.at(syncGroupId); |
|
88 | 89 | syncGroup.m_Variables.erase(variableId); |
|
89 | 90 | if (syncGroup.m_Variables.empty()) { |
|
90 | 91 | syncGroup.m_Range = INVALID_RANGE; |
|
91 | 92 | } |
|
92 | 93 | } |
|
93 | 94 | |
|
94 | 95 | void FuzzingState::updateRanges(VariableId variableId, const SqpRange &newRange) |
|
95 | 96 | { |
|
96 | 97 | auto syncGroupId = this->syncGroupId(variableId); |
|
97 | 98 | |
|
98 | 99 | // Retrieves the variables to update: |
|
99 | 100 | // - if the variable is synchronized to others, updates all synchronized variables |
|
100 | 101 | // - otherwise, updates only the variable |
|
101 | 102 | auto variablesToUpdate = syncGroupId.isNull() ? std::set<VariableId>{variableId} |
|
102 | 103 | : m_SyncGroupsPool.at(syncGroupId).m_Variables; |
|
103 | 104 | |
|
104 | 105 | // Sets new range |
|
105 | 106 | for (const auto &variableId : variablesToUpdate) { |
|
106 | 107 | m_VariablesPool.at(variableId).m_Range = newRange; |
|
107 | 108 | } |
|
108 | 109 | } |
@@ -1,125 +1,128 | |||
|
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 <QUuid> |
|
8 | 8 | #include <QVariantHash> |
|
9 | 9 | |
|
10 | 10 | #include <memory> |
|
11 | 11 | #include <set> |
|
12 | 12 | |
|
13 | 13 | // /////// // |
|
14 | 14 | // Aliases // |
|
15 | 15 | // /////// // |
|
16 | 16 | |
|
17 | 17 | using MetadataPool = std::vector<QVariantHash>; |
|
18 | 18 | Q_DECLARE_METATYPE(MetadataPool) |
|
19 | 19 | |
|
20 | 20 | using Properties = QVariantHash; |
|
21 | 21 | |
|
22 | 22 | // ///////// // |
|
23 | 23 | // Constants // |
|
24 | 24 | // ///////// // |
|
25 | 25 | |
|
26 | /// Timeout set for data acquisition for an operation (in ms) | |
|
27 | extern const QString ACQUISITION_TIMEOUT_PROPERTY; | |
|
28 | ||
|
26 | 29 | /// Max number of operations to generate |
|
27 | 30 | extern const QString NB_MAX_OPERATIONS_PROPERTY; |
|
28 | 31 | |
|
29 | 32 | /// Max number of sync groups to create through operations |
|
30 | 33 | extern const QString NB_MAX_SYNC_GROUPS_PROPERTY; |
|
31 | 34 | |
|
32 | 35 | /// Max number of variables to manipulate through operations |
|
33 | 36 | extern const QString NB_MAX_VARIABLES_PROPERTY; |
|
34 | 37 | |
|
35 | 38 | /// Set of operations available for the test |
|
36 | 39 | extern const QString AVAILABLE_OPERATIONS_PROPERTY; |
|
37 | 40 | |
|
38 | 41 | /// Tolerance used for variable's cache (in ratio) |
|
39 | 42 | extern const QString CACHE_TOLERANCE_PROPERTY; |
|
40 | 43 | |
|
41 | 44 | /// Range with which the timecontroller is initialized |
|
42 | 45 | extern const QString INITIAL_RANGE_PROPERTY; |
|
43 | 46 | |
|
44 | 47 | /// Max range that an operation can reach |
|
45 | 48 | extern const QString MAX_RANGE_PROPERTY; |
|
46 | 49 | |
|
47 | 50 | /// Set of metadata that can be associated to a variable |
|
48 | 51 | extern const QString METADATA_POOL_PROPERTY; |
|
49 | 52 | |
|
50 | 53 | /// Provider used to retrieve data |
|
51 | 54 | extern const QString PROVIDER_PROPERTY; |
|
52 | 55 | |
|
53 | 56 | /// Min/max times left for an operation to execute |
|
54 | 57 | extern const QString OPERATION_DELAY_BOUNDS_PROPERTY; |
|
55 | 58 | |
|
56 | 59 | /// Validators used to validate an operation |
|
57 | 60 | extern const QString VALIDATORS_PROPERTY; |
|
58 | 61 | |
|
59 | 62 | /// Min/max number of operations to execute before calling validation of the current test's state |
|
60 | 63 | extern const QString VALIDATION_FREQUENCY_BOUNDS_PROPERTY; |
|
61 | 64 | |
|
62 | 65 | // /////// // |
|
63 | 66 | // Structs // |
|
64 | 67 | // /////// // |
|
65 | 68 | |
|
66 | 69 | class Variable; |
|
67 | 70 | struct VariableState { |
|
68 | 71 | std::shared_ptr<Variable> m_Variable{nullptr}; |
|
69 | 72 | SqpRange m_Range{INVALID_RANGE}; |
|
70 | 73 | }; |
|
71 | 74 | |
|
72 | 75 | using VariableId = int; |
|
73 | 76 | using VariablesPool = std::map<VariableId, VariableState>; |
|
74 | 77 | |
|
75 | 78 | /** |
|
76 | 79 | * Defines a synchronization group for a fuzzing state. A group reports the variables synchronized |
|
77 | 80 | * with each other, and the current range of the group (i.e. range of the last synchronized variable |
|
78 | 81 | * that has been moved) |
|
79 | 82 | */ |
|
80 | 83 | struct SyncGroup { |
|
81 | 84 | std::set<VariableId> m_Variables{}; |
|
82 | 85 | SqpRange m_Range{INVALID_RANGE}; |
|
83 | 86 | }; |
|
84 | 87 | |
|
85 | 88 | using SyncGroupId = QUuid; |
|
86 | 89 | using SyncGroupsPool = std::map<SyncGroupId, SyncGroup>; |
|
87 | 90 | |
|
88 | 91 | /** |
|
89 | 92 | * Defines a current state during a fuzzing state. It contains all the variables manipulated during |
|
90 | 93 | * the test, as well as the synchronization status of these variables. |
|
91 | 94 | */ |
|
92 | 95 | struct FuzzingState { |
|
93 | 96 | const SyncGroup &syncGroup(SyncGroupId id) const; |
|
94 | 97 | SyncGroup &syncGroup(SyncGroupId id); |
|
95 | 98 | |
|
96 | 99 | const VariableState &variableState(VariableId id) const; |
|
97 | 100 | VariableState &variableState(VariableId id); |
|
98 | 101 | |
|
99 | 102 | /// @return the identifier of the synchronization group in which the variable passed in |
|
100 | 103 | /// parameter is located. If the variable is not in any group, returns an invalid identifier |
|
101 | 104 | SyncGroupId syncGroupId(VariableId variableId) const; |
|
102 | 105 | |
|
103 | 106 | /// @return the set of synchronization group identifiers |
|
104 | 107 | std::vector<SyncGroupId> syncGroupsIds() const; |
|
105 | 108 | |
|
106 | 109 | /// Updates fuzzing state according to a variable synchronization |
|
107 | 110 | /// @param variableId the variable that is synchronized |
|
108 | 111 | /// @param syncGroupId the synchronization group |
|
109 | 112 | void synchronizeVariable(VariableId variableId, SyncGroupId syncGroupId); |
|
110 | 113 | |
|
111 | 114 | /// Updates fuzzing state according to a variable desynchronization |
|
112 | 115 | /// @param variableId the variable that is desynchronized |
|
113 | 116 | /// @param syncGroupId the synchronization group from which to remove the variable |
|
114 | 117 | void desynchronizeVariable(VariableId variableId, SyncGroupId syncGroupId); |
|
115 | 118 | |
|
116 | 119 | /// Updates the range of a variable and all variables to which it is synchronized |
|
117 | 120 | /// @param the variable for which to affect the range |
|
118 | 121 | /// @param the range to affect |
|
119 | 122 | void updateRanges(VariableId variableId, const SqpRange &newRange); |
|
120 | 123 | |
|
121 | 124 | VariablesPool m_VariablesPool; |
|
122 | 125 | SyncGroupsPool m_SyncGroupsPool; |
|
123 | 126 | }; |
|
124 | 127 | |
|
125 | 128 | #endif // SCIQLOP_FUZZINGDEFS_H |
@@ -1,369 +1,377 | |||
|
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 <Common/SignalWaiter.h> |
|
9 | 9 | #include <Network/NetworkController.h> |
|
10 | 10 | #include <Settings/SqpSettingsDefs.h> |
|
11 | 11 | #include <SqpApplication.h> |
|
12 | 12 | #include <Time/TimeController.h> |
|
13 | 13 | #include <Variable/Variable.h> |
|
14 | 14 | #include <Variable/VariableController.h> |
|
15 | 15 | |
|
16 | 16 | #include <QLoggingCategory> |
|
17 | 17 | #include <QObject> |
|
18 | 18 | #include <QtTest> |
|
19 | 19 | |
|
20 | 20 | #include <memory> |
|
21 | 21 | |
|
22 | 22 | Q_LOGGING_CATEGORY(LOG_TestAmdaFuzzing, "TestAmdaFuzzing") |
|
23 | 23 | |
|
24 | 24 | /** |
|
25 | 25 | * Macro used to generate a getter for a property in @sa FuzzingTest. The macro generates a static |
|
26 | 26 | * attribute that is initialized by searching in properties the property and use a default value if |
|
27 | 27 | * it's not present. Macro arguments are: |
|
28 | 28 | * - GETTER_NAME : name of the getter |
|
29 | 29 | * - PROPERTY_NAME: used to generate constants for property's name ({PROPERTY_NAME}_PROPERTY) and |
|
30 | 30 | * default value ({PROPERTY_NAME}_DEFAULT_VALUE) |
|
31 | 31 | * - TYPE : return type of the getter |
|
32 | 32 | */ |
|
33 | 33 | // clang-format off |
|
34 | 34 | #define DECLARE_PROPERTY_GETTER(GETTER_NAME, PROPERTY_NAME, TYPE) \ |
|
35 | 35 | TYPE GETTER_NAME() const \ |
|
36 | 36 | { \ |
|
37 | 37 | static auto result = m_Properties.value(PROPERTY_NAME##_PROPERTY, PROPERTY_NAME##_DEFAULT_VALUE).value<TYPE>(); \ |
|
38 | 38 | return result; \ |
|
39 | 39 | } \ |
|
40 | 40 | // clang-format on |
|
41 | 41 | |
|
42 | 42 | namespace { |
|
43 | 43 | |
|
44 | 44 | // /////// // |
|
45 | 45 | // Aliases // |
|
46 | 46 | // /////// // |
|
47 | 47 | |
|
48 | 48 | using IntPair = std::pair<int, int>; |
|
49 | 49 | using Weight = double; |
|
50 | 50 | using Weights = std::vector<Weight>; |
|
51 | 51 | |
|
52 | 52 | using VariableOperation = std::pair<VariableId, std::shared_ptr<IFuzzingOperation> >; |
|
53 | 53 | using VariablesOperations = std::vector<VariableOperation>; |
|
54 | 54 | |
|
55 | 55 | using WeightedOperationsPool = std::map<std::shared_ptr<IFuzzingOperation>, Weight>; |
|
56 | 56 | using Validators = std::vector<std::shared_ptr<IFuzzingValidator> >; |
|
57 | 57 | |
|
58 | 58 | // ///////// // |
|
59 | 59 | // Constants // |
|
60 | 60 | // ///////// // |
|
61 | 61 | |
|
62 | 62 | // Defaults values used when the associated properties have not been set for the test |
|
63 | const auto ACQUISITION_TIMEOUT_DEFAULT_VALUE = 30000; | |
|
63 | 64 | const auto NB_MAX_OPERATIONS_DEFAULT_VALUE = 100; |
|
64 | 65 | const auto NB_MAX_SYNC_GROUPS_DEFAULT_VALUE = 1; |
|
65 | 66 | const auto NB_MAX_VARIABLES_DEFAULT_VALUE = 1; |
|
66 | 67 | const auto AVAILABLE_OPERATIONS_DEFAULT_VALUE = QVariant::fromValue(WeightedOperationsTypes{ |
|
67 | 68 | {FuzzingOperationType::CREATE, 1.}, |
|
68 | 69 | {FuzzingOperationType::DELETE, 0.1}, // Delete operation is less frequent |
|
69 | 70 | {FuzzingOperationType::PAN_LEFT, 1.}, |
|
70 | 71 | {FuzzingOperationType::PAN_RIGHT, 1.}, |
|
71 | 72 | {FuzzingOperationType::ZOOM_IN, 1.}, |
|
72 | 73 | {FuzzingOperationType::ZOOM_OUT, 1.}, |
|
73 | 74 | {FuzzingOperationType::SYNCHRONIZE, 0.8}, |
|
74 | 75 | {FuzzingOperationType::DESYNCHRONIZE, 0.4}}); |
|
75 | 76 | const auto CACHE_TOLERANCE_DEFAULT_VALUE = 0.2; |
|
76 | 77 | |
|
77 | 78 | /// Min/max delays between each operation (in ms) |
|
78 | 79 | const auto OPERATION_DELAY_BOUNDS_DEFAULT_VALUE = QVariant::fromValue(std::make_pair(100, 3000)); |
|
79 | 80 | |
|
80 | 81 | /// Validators for the tests (executed in the order in which they're defined) |
|
81 | 82 | const auto VALIDATORS_DEFAULT_VALUE = QVariant::fromValue( |
|
82 | 83 | ValidatorsTypes{{FuzzingValidatorType::RANGE, FuzzingValidatorType::DATA}}); |
|
83 | 84 | |
|
84 | 85 | /// Min/max number of operations to execute before calling validation |
|
85 | 86 | const auto VALIDATION_FREQUENCY_BOUNDS_DEFAULT_VALUE = QVariant::fromValue(std::make_pair(1, 10)); |
|
86 | 87 | |
|
87 | 88 | // /////// // |
|
88 | 89 | // Methods // |
|
89 | 90 | // /////// // |
|
90 | 91 | |
|
91 | 92 | /// Goes through the variables pool and operations pool to determine the set of {variable/operation} |
|
92 | 93 | /// pairs that are valid (i.e. operation that can be executed on variable) |
|
93 | 94 | std::pair<VariablesOperations, Weights> |
|
94 | 95 | availableOperations(const FuzzingState &fuzzingState, const WeightedOperationsPool &operationsPool) |
|
95 | 96 | { |
|
96 | 97 | VariablesOperations result{}; |
|
97 | 98 | Weights weights{}; |
|
98 | 99 | |
|
99 | 100 | for (const auto &variablesPoolEntry : fuzzingState.m_VariablesPool) { |
|
100 | 101 | auto variableId = variablesPoolEntry.first; |
|
101 | 102 | |
|
102 | 103 | for (const auto &operationsPoolEntry : operationsPool) { |
|
103 | 104 | auto operation = operationsPoolEntry.first; |
|
104 | 105 | auto weight = operationsPoolEntry.second; |
|
105 | 106 | |
|
106 | 107 | // A pair is valid if the current operation can be executed on the current variable |
|
107 | 108 | if (operation->canExecute(variableId, fuzzingState)) { |
|
108 | 109 | result.push_back({variableId, operation}); |
|
109 | 110 | weights.push_back(weight); |
|
110 | 111 | } |
|
111 | 112 | } |
|
112 | 113 | } |
|
113 | 114 | |
|
114 | 115 | return {result, weights}; |
|
115 | 116 | } |
|
116 | 117 | |
|
117 | 118 | WeightedOperationsPool createOperationsPool(const WeightedOperationsTypes &types) |
|
118 | 119 | { |
|
119 | 120 | WeightedOperationsPool result{}; |
|
120 | 121 | |
|
121 | 122 | std::transform( |
|
122 | 123 | types.cbegin(), types.cend(), std::inserter(result, result.end()), [](const auto &type) { |
|
123 | 124 | return std::make_pair(FuzzingOperationFactory::create(type.first), type.second); |
|
124 | 125 | }); |
|
125 | 126 | |
|
126 | 127 | return result; |
|
127 | 128 | } |
|
128 | 129 | |
|
129 | 130 | Validators createValidators(const ValidatorsTypes &types) |
|
130 | 131 | { |
|
131 | 132 | Validators result{}; |
|
132 | 133 | |
|
133 | 134 | std::transform(types.cbegin(), types.cend(), std::inserter(result, result.end()), |
|
134 | 135 | [](const auto &type) { return FuzzingValidatorFactory::create(type); }); |
|
135 | 136 | |
|
136 | 137 | return result; |
|
137 | 138 | } |
|
138 | 139 | |
|
139 | 140 | /** |
|
140 | 141 | * Validates all the variables' states passed in parameter, according to a set of validators |
|
141 | 142 | * @param variablesPool the variables' states |
|
142 | 143 | * @param validators the validators used for validation |
|
143 | 144 | */ |
|
144 | 145 | void validate(const VariablesPool &variablesPool, const Validators &validators) |
|
145 | 146 | { |
|
146 | 147 | for (const auto &variablesPoolEntry : variablesPool) { |
|
147 | 148 | auto variableId = variablesPoolEntry.first; |
|
148 | 149 | const auto &variableState = variablesPoolEntry.second; |
|
149 | 150 | |
|
150 | 151 | auto variableMessage = variableState.m_Variable ? variableState.m_Variable->name() |
|
151 | 152 | : QStringLiteral("null variable"); |
|
152 | 153 | qCInfo(LOG_TestAmdaFuzzing()).noquote() << "Validating state of variable at index" |
|
153 | 154 | << variableId << "(" << variableMessage << ")..."; |
|
154 | 155 | |
|
155 | 156 | for (const auto &validator : validators) { |
|
156 | 157 | validator->validate(VariableState{variableState}); |
|
157 | 158 | } |
|
158 | 159 | |
|
159 | 160 | qCInfo(LOG_TestAmdaFuzzing()).noquote() << "Validation completed."; |
|
160 | 161 | } |
|
161 | 162 | } |
|
162 | 163 | |
|
163 | 164 | /** |
|
164 | 165 | * Class to run random tests |
|
165 | 166 | */ |
|
166 | 167 | class FuzzingTest { |
|
167 | 168 | public: |
|
168 | 169 | explicit FuzzingTest(VariableController &variableController, Properties properties) |
|
169 | 170 | : m_VariableController{variableController}, |
|
170 | 171 | m_Properties{std::move(properties)}, |
|
171 | 172 | m_FuzzingState{} |
|
172 | 173 | { |
|
173 | 174 | // Inits variables pool: at init, all variables are null |
|
174 | 175 | for (auto variableId = 0; variableId < nbMaxVariables(); ++variableId) { |
|
175 | 176 | m_FuzzingState.m_VariablesPool[variableId] = VariableState{}; |
|
176 | 177 | } |
|
177 | 178 | |
|
178 | 179 | // Inits sync groups and registers them into the variable controller |
|
179 | 180 | for (auto i = 0; i < nbMaxSyncGroups(); ++i) { |
|
180 | 181 | auto syncGroupId = SyncGroupId::createUuid(); |
|
181 | 182 | variableController.onAddSynchronizationGroupId(syncGroupId); |
|
182 | 183 | m_FuzzingState.m_SyncGroupsPool[syncGroupId] = SyncGroup{}; |
|
183 | 184 | } |
|
184 | 185 | } |
|
185 | 186 | |
|
186 | 187 | void execute() |
|
187 | 188 | { |
|
188 | 189 | qCInfo(LOG_TestAmdaFuzzing()).noquote() << "Running" << nbMaxOperations() << "operations on" |
|
189 | 190 | << nbMaxVariables() << "variable(s)..."; |
|
190 | 191 | |
|
191 | 192 | |
|
192 | 193 | // Inits the count of the number of operations before the next validation |
|
193 | 194 | int nextValidationCounter = 0; |
|
194 | 195 | auto updateValidationCounter = [this, &nextValidationCounter]() { |
|
195 | 196 | nextValidationCounter = RandomGenerator::instance().generateInt( |
|
196 | 197 | validationFrequencies().first, validationFrequencies().second); |
|
197 | 198 | qCInfo(LOG_TestAmdaFuzzing()).noquote() |
|
198 | << "Next validation in " << nextValidationCounter << "operations..."; | |
|
199 | << "Next validation in " << nextValidationCounter << "operation(s)..."; | |
|
199 | 200 | }; |
|
200 | 201 | updateValidationCounter(); |
|
201 | 202 | |
|
202 | 203 | auto canExecute = true; |
|
203 | 204 | for (auto i = 0; i < nbMaxOperations() && canExecute; ++i) { |
|
204 | 205 | // Retrieves all operations that can be executed in the current context |
|
205 | 206 | VariablesOperations variableOperations{}; |
|
206 | 207 | Weights weights{}; |
|
207 | 208 | std::tie(variableOperations, weights) |
|
208 | 209 | = availableOperations(m_FuzzingState, operationsPool()); |
|
209 | 210 | |
|
210 | 211 | canExecute = !variableOperations.empty(); |
|
211 | 212 | if (canExecute) { |
|
212 | 213 | --nextValidationCounter; |
|
213 | 214 | |
|
214 | 215 | // Of the operations available, chooses a random operation and executes it |
|
215 | 216 | auto variableOperation |
|
216 | 217 | = RandomGenerator::instance().randomChoice(variableOperations, weights); |
|
217 | 218 | |
|
218 | 219 | auto variableId = variableOperation.first; |
|
219 | 220 | auto fuzzingOperation = variableOperation.second; |
|
220 | 221 | |
|
222 | auto waitAcquisition = nextValidationCounter == 0; | |
|
223 | ||
|
221 | 224 | fuzzingOperation->execute(variableId, m_FuzzingState, m_VariableController, |
|
222 | 225 | m_Properties); |
|
223 | 226 | |
|
224 | // Delays the next operation with a randomly generated time | |
|
225 | auto delay = RandomGenerator::instance().generateInt(operationDelays().first, | |
|
226 | operationDelays().second); | |
|
227 | qCDebug(LOG_TestAmdaFuzzing()) | |
|
228 | << "Waiting " << delay << "ms before the next operation..."; | |
|
229 | QTest::qWait(delay); | |
|
227 | if (waitAcquisition) { | |
|
228 | qCDebug(LOG_TestAmdaFuzzing()) << "Waiting for acquisition to finish..."; | |
|
229 | SignalWaiter{m_VariableController, SIGNAL(acquisitionFinished())}.wait( | |
|
230 | acquisitionTimeout()); | |
|
230 | 231 | |
|
231 | // Validates variables | |
|
232 | if (nextValidationCounter == 0) { | |
|
232 | // Validates variables | |
|
233 | 233 | validate(m_FuzzingState.m_VariablesPool, validators()); |
|
234 | 234 | updateValidationCounter(); |
|
235 | 235 | } |
|
236 | else { | |
|
237 | // Delays the next operation with a randomly generated time | |
|
238 | auto delay = RandomGenerator::instance().generateInt(operationDelays().first, | |
|
239 | operationDelays().second); | |
|
240 | qCDebug(LOG_TestAmdaFuzzing()) | |
|
241 | << "Waiting " << delay << "ms before the next operation..."; | |
|
242 | QTest::qWait(delay); | |
|
243 | } | |
|
236 | 244 | } |
|
237 | 245 | else { |
|
238 | 246 | qCInfo(LOG_TestAmdaFuzzing()).noquote() |
|
239 | 247 | << "No more operations are available, the execution of the test will stop..."; |
|
240 | 248 | } |
|
241 | 249 | } |
|
242 | 250 | |
|
243 | 251 | qCInfo(LOG_TestAmdaFuzzing()).noquote() << "Execution of the test completed."; |
|
244 | 252 | } |
|
245 | 253 | |
|
246 | 254 | private: |
|
247 | 255 | |
|
248 | 256 | WeightedOperationsPool operationsPool() const |
|
249 | 257 | { |
|
250 | 258 | static auto result = createOperationsPool( |
|
251 | 259 | m_Properties.value(AVAILABLE_OPERATIONS_PROPERTY, AVAILABLE_OPERATIONS_DEFAULT_VALUE) |
|
252 | 260 | .value<WeightedOperationsTypes>()); |
|
253 | 261 | return result; |
|
254 | 262 | } |
|
255 | 263 | |
|
256 | 264 | Validators validators() const |
|
257 | 265 | { |
|
258 | 266 | static auto result |
|
259 | 267 | = createValidators(m_Properties.value(VALIDATORS_PROPERTY, VALIDATORS_DEFAULT_VALUE) |
|
260 | 268 | .value<ValidatorsTypes>()); |
|
261 | 269 | return result; |
|
262 | 270 | } |
|
263 | 271 | |
|
264 | 272 | DECLARE_PROPERTY_GETTER(nbMaxOperations, NB_MAX_OPERATIONS, int) |
|
265 | 273 | DECLARE_PROPERTY_GETTER(nbMaxSyncGroups, NB_MAX_SYNC_GROUPS, int) |
|
266 | 274 | DECLARE_PROPERTY_GETTER(nbMaxVariables, NB_MAX_VARIABLES, int) |
|
267 | 275 | DECLARE_PROPERTY_GETTER(operationDelays, OPERATION_DELAY_BOUNDS, IntPair) |
|
268 | 276 | DECLARE_PROPERTY_GETTER(validationFrequencies, VALIDATION_FREQUENCY_BOUNDS, IntPair) |
|
269 | 277 | DECLARE_PROPERTY_GETTER(acquisitionTimeout, ACQUISITION_TIMEOUT, int) |
|
270 | 278 | |
|
271 | 279 | VariableController &m_VariableController; |
|
272 | 280 | Properties m_Properties; |
|
273 | 281 | FuzzingState m_FuzzingState; |
|
274 | 282 | }; |
|
275 | 283 | |
|
276 | 284 | } // namespace |
|
277 | 285 | |
|
278 | 286 | class TestAmdaFuzzing : public QObject { |
|
279 | 287 | Q_OBJECT |
|
280 | 288 | |
|
281 | 289 | private slots: |
|
282 | 290 | /// Input data for @sa testFuzzing() |
|
283 | 291 | void testFuzzing_data(); |
|
284 | 292 | void testFuzzing(); |
|
285 | 293 | }; |
|
286 | 294 | |
|
287 | 295 | void TestAmdaFuzzing::testFuzzing_data() |
|
288 | 296 | { |
|
289 | 297 | // ////////////// // |
|
290 | 298 | // Test structure // |
|
291 | 299 | // ////////////// // |
|
292 | 300 | |
|
293 | 301 | QTest::addColumn<Properties>("properties"); // Properties for random test |
|
294 | 302 | |
|
295 | 303 | // ////////// // |
|
296 | 304 | // Test cases // |
|
297 | 305 | // ////////// // |
|
298 | 306 | |
|
299 | 307 | auto maxRange = SqpRange::fromDateTime({2017, 1, 1}, {0, 0}, {2017, 1, 5}, {0, 0}); |
|
300 | 308 | MetadataPool metadataPool{{{"dataType", "vector"}, {"xml:id", "imf"}}}; |
|
301 | 309 | |
|
302 | 310 | // Note: we don't use auto here as we want to pass std::shared_ptr<IDataProvider> as is in the |
|
303 | 311 | // QVariant |
|
304 | 312 | std::shared_ptr<IDataProvider> provider = std::make_shared<AmdaProvider>(); |
|
305 | 313 | |
|
306 | 314 | QTest::newRow("fuzzingTest") << Properties{ |
|
307 | 315 | {MAX_RANGE_PROPERTY, QVariant::fromValue(maxRange)}, |
|
308 | 316 | {METADATA_POOL_PROPERTY, QVariant::fromValue(metadataPool)}, |
|
309 | 317 | {PROVIDER_PROPERTY, QVariant::fromValue(provider)}}; |
|
310 | 318 | } |
|
311 | 319 | |
|
312 | 320 | void TestAmdaFuzzing::testFuzzing() |
|
313 | 321 | { |
|
314 | 322 | QFETCH(Properties, properties); |
|
315 | 323 | |
|
316 | 324 | // Sets cache property |
|
317 | 325 | QSettings settings{}; |
|
318 | 326 | auto cacheTolerance = properties.value(CACHE_TOLERANCE_PROPERTY, CACHE_TOLERANCE_DEFAULT_VALUE); |
|
319 | 327 | settings.setValue(GENERAL_TOLERANCE_AT_INIT_KEY, cacheTolerance); |
|
320 | 328 | settings.setValue(GENERAL_TOLERANCE_AT_UPDATE_KEY, cacheTolerance); |
|
321 | 329 | |
|
322 | 330 | auto &variableController = sqpApp->variableController(); |
|
323 | 331 | auto &timeController = sqpApp->timeController(); |
|
324 | 332 | |
|
325 | 333 | // Generates random initial range (bounded to max range) |
|
326 | 334 | auto maxRange = properties.value(MAX_RANGE_PROPERTY, QVariant::fromValue(INVALID_RANGE)) |
|
327 | 335 | .value<SqpRange>(); |
|
328 | 336 | |
|
329 | 337 | QVERIFY(maxRange != INVALID_RANGE); |
|
330 | 338 | |
|
331 | 339 | auto initialRangeStart |
|
332 | 340 | = RandomGenerator::instance().generateDouble(maxRange.m_TStart, maxRange.m_TEnd); |
|
333 | 341 | auto initialRangeEnd |
|
334 | 342 | = RandomGenerator::instance().generateDouble(maxRange.m_TStart, maxRange.m_TEnd); |
|
335 | 343 | if (initialRangeStart > initialRangeEnd) { |
|
336 | 344 | std::swap(initialRangeStart, initialRangeEnd); |
|
337 | 345 | } |
|
338 | 346 | |
|
339 | 347 | // Sets initial range on time controller |
|
340 | 348 | SqpRange initialRange{initialRangeStart, initialRangeEnd}; |
|
341 | 349 | qCInfo(LOG_TestAmdaFuzzing()).noquote() << "Setting initial range to" << initialRange << "..."; |
|
342 | 350 | timeController.onTimeToUpdate(initialRange); |
|
343 | 351 | properties.insert(INITIAL_RANGE_PROPERTY, QVariant::fromValue(initialRange)); |
|
344 | 352 | |
|
345 | 353 | FuzzingTest test{variableController, properties}; |
|
346 | 354 | test.execute(); |
|
347 | 355 | } |
|
348 | 356 | |
|
349 | 357 | int main(int argc, char *argv[]) |
|
350 | 358 | { |
|
351 | 359 | QLoggingCategory::setFilterRules( |
|
352 | 360 | "*.warning=false\n" |
|
353 | 361 | "*.info=false\n" |
|
354 | 362 | "*.debug=false\n" |
|
355 | 363 | "FuzzingOperations.info=true\n" |
|
356 | 364 | "FuzzingValidators.info=true\n" |
|
357 | 365 | "TestAmdaFuzzing.info=true\n"); |
|
358 | 366 | |
|
359 | 367 | SqpApplication app{argc, argv}; |
|
360 | 368 | SqpApplication::setOrganizationName("LPP"); |
|
361 | 369 | SqpApplication::setOrganizationDomain("lpp.fr"); |
|
362 | 370 | SqpApplication::setApplicationName("SciQLop-TestFuzzing"); |
|
363 | 371 | app.setAttribute(Qt::AA_Use96Dpi, true); |
|
364 | 372 | TestAmdaFuzzing testObject{}; |
|
365 | 373 | QTEST_SET_MAIN_SOURCE_PATH |
|
366 | 374 | return QTest::qExec(&testObject, argc, argv); |
|
367 | 375 | } |
|
368 | 376 | |
|
369 | 377 | #include "TestAmdaFuzzing.moc" |
General Comments 0
You need to be logged in to leave comments.
Login now