##// END OF EJS Templates
r1253:c62577560c5b merge
parent child
Show More
@@ -0,0 +1,8
1 #include "FuzzingDefs.h"
2
3 const QString NB_MAX_OPERATIONS_PROPERTY = QStringLiteral("component");
4 const QString NB_MAX_VARIABLES_PROPERTY = QStringLiteral("nbMaxVariables");
5 const QString AVAILABLE_OPERATIONS_PROPERTY = QStringLiteral("availableOperations");
6 const QString MAX_RANGE_PROPERTY = QStringLiteral("maxRange");
7 const QString METADATA_POOL_PROPERTY = QStringLiteral("metadataPool");
8 const QString PROVIDER_PROPERTY = QStringLiteral("provider");
@@ -0,0 +1,38
1 #ifndef SCIQLOP_FUZZINGDEFS_H
2 #define SCIQLOP_FUZZINGDEFS_H
3
4 #include <QString>
5 #include <QVariantHash>
6
7 // /////// //
8 // Aliases //
9 // /////// //
10
11 using MetadataPool = std::vector<QVariantHash>;
12 Q_DECLARE_METATYPE(MetadataPool)
13
14 using Properties = QVariantHash;
15
16 // ///////// //
17 // Constants //
18 // ///////// //
19
20 /// Max number of operations to generate
21 extern const QString NB_MAX_OPERATIONS_PROPERTY;
22
23 /// Max number of variables to manipulate through operations
24 extern const QString NB_MAX_VARIABLES_PROPERTY;
25
26 /// Set of operations available for the test
27 extern const QString AVAILABLE_OPERATIONS_PROPERTY;
28
29 /// Max range that an operation can reach
30 extern const QString MAX_RANGE_PROPERTY;
31
32 /// Set of metadata that can be associated to a variable
33 extern const QString METADATA_POOL_PROPERTY;
34
35 /// Provider used to retrieve data
36 extern const QString PROVIDER_PROPERTY;
37
38 #endif // SCIQLOP_FUZZINGDEFS_H
@@ -0,0 +1,74
1 #include "FuzzingOperations.h"
2 #include "FuzzingUtils.h"
3
4 #include <Data/IDataProvider.h>
5
6 #include <Variable/Variable.h>
7 #include <Variable/VariableController.h>
8
9 #include <QUuid>
10
11 Q_LOGGING_CATEGORY(LOG_FuzzingOperations, "FuzzingOperations")
12
13 namespace {
14
15 struct CreateOperation : public IFuzzingOperation {
16 bool canExecute(std::shared_ptr<Variable> variable) const override
17 {
18 // A variable can be created only if it doesn't exist yet
19 return variable == nullptr;
20 }
21
22 void execute(std::shared_ptr<Variable> &variable, VariableController &variableController,
23 const Properties &properties) const override
24 {
25 // Retrieves metadata pool from properties, and choose one of the metadata entries to
26 // associate it with the variable
27 auto metaDataPool = properties.value(METADATA_POOL_PROPERTY).value<MetadataPool>();
28 auto variableMetadata = RandomGenerator::instance().randomChoice(metaDataPool);
29
30 // Retrieves provider
31 auto variableProvider
32 = properties.value(PROVIDER_PROPERTY).value<std::shared_ptr<IDataProvider> >();
33
34 auto variableName = QString{"Var_%1"}.arg(QUuid::createUuid().toString());
35 qCInfo(LOG_FuzzingOperations())
36 << "Creating variable" << variableName << "(metadata:" << variableMetadata << ")";
37
38 auto newVariable
39 = variableController.createVariable(variableName, variableMetadata, variableProvider);
40 std::swap(variable, newVariable);
41 }
42 };
43
44 struct UnknownOperation : public IFuzzingOperation {
45 bool canExecute(std::shared_ptr<Variable> variable) const override
46 {
47 Q_UNUSED(variable);
48 return false;
49 }
50
51 void execute(std::shared_ptr<Variable> &variable, VariableController &variableController,
52 const Properties &properties) const override
53 {
54 Q_UNUSED(variable);
55 Q_UNUSED(variableController);
56 Q_UNUSED(properties);
57 // Does nothing
58 }
59 };
60
61 } // namespace
62
63 std::unique_ptr<IFuzzingOperation> FuzzingOperationFactory::create(FuzzingOperationType type)
64 {
65 switch (type) {
66 case FuzzingOperationType::CREATE:
67 return std::make_unique<CreateOperation>();
68 default:
69 // Default case returns unknown operation
70 break;
71 }
72
73 return std::make_unique<UnknownOperation>();
74 }
@@ -0,0 +1,49
1 #ifndef SCIQLOP_FUZZINGOPERATIONS_H
2 #define SCIQLOP_FUZZINGOPERATIONS_H
3
4 #include "FuzzingDefs.h"
5
6 #include <memory>
7 #include <set>
8
9 #include <QLoggingCategory>
10 #include <QMetaType>
11
12 Q_DECLARE_LOGGING_CATEGORY(LOG_FuzzingOperations)
13
14 class Variable;
15 class VariableController;
16
17 /**
18 * Enumeration of types of existing fuzzing operations
19 */
20 enum class FuzzingOperationType { CREATE };
21
22 /// Interface that represents an operation that can be executed during a fuzzing test
23 struct IFuzzingOperation {
24 virtual ~IFuzzingOperation() noexcept = default;
25
26 /// Checks if the operation can be executed according to the current state of the variable
27 /// passed in parameter
28 virtual bool canExecute(std::shared_ptr<Variable> variable) const = 0;
29 /// Executes the operation on the variable passed in parameter
30 /// @param variable the variable on which to execute the operation
31 /// @param variableController the controller associated to the operation
32 /// @param properties properties that can be used to configure the operation
33 /// @remarks variable is passed as a reference because, according to the operation, it can be
34 /// modified (in/out parameter)
35 virtual void execute(std::shared_ptr<Variable> &variable,
36 VariableController &variableController,
37 const Properties &properties = {}) const = 0;
38 };
39
40 /// Factory of @sa IFuzzingOperation
41 struct FuzzingOperationFactory {
42 /// Creates a fuzzing operation from a type
43 static std::unique_ptr<IFuzzingOperation> create(FuzzingOperationType type);
44 };
45
46 using OperationsTypes = std::set<FuzzingOperationType>;
47 Q_DECLARE_METATYPE(OperationsTypes)
48
49 #endif // SCIQLOP_FUZZINGOPERATIONS_H
@@ -0,0 +1,25
1 #include "FuzzingUtils.h"
2
3 RandomGenerator &RandomGenerator::instance()
4 {
5 static auto instance = RandomGenerator();
6 return instance;
7 }
8
9 double RandomGenerator::generateDouble(double min, double max)
10 {
11 std::uniform_real_distribution<double> dist{min, max};
12 return dist(m_Mt);
13 }
14
15 int RandomGenerator::generateInt(int min, int max)
16 {
17 std::uniform_int_distribution<int> dist{min, max};
18 return dist(m_Mt);
19 }
20
21 RandomGenerator::RandomGenerator()
22 {
23 std::random_device rd{};
24 m_Mt = std::mt19937{rd()};
25 }
@@ -0,0 +1,41
1 #ifndef SCIQLOP_FUZZINGUTILS_H
2 #define SCIQLOP_FUZZINGUTILS_H
3
4 #include <random>
5
6 /**
7 * Class that proposes random utility methods
8 */
9 class RandomGenerator {
10 public:
11 /// @return the unique instance of the random generator
12 static RandomGenerator &instance();
13
14 /// Generates a random double between [min, max]
15 double generateDouble(double min, double max);
16 /// Generates a random int between [min, max]
17 int generateInt(int min, int max);
18
19 /// Returns a random element among the elements of a container. If the container is empty,
20 /// returns an element built by default
21 template <typename T, typename ValueType = typename T::value_type>
22 ValueType randomChoice(const T &container);
23
24 private:
25 std::mt19937 m_Mt;
26
27 explicit RandomGenerator();
28 };
29
30 template <typename T, typename ValueType>
31 ValueType RandomGenerator::randomChoice(const T &container)
32 {
33 if (container.empty()) {
34 return ValueType{};
35 }
36
37 auto randomIndex = generateInt(0, container.size() - 1);
38 return container.at(randomIndex);
39 }
40
41 #endif // SCIQLOP_FUZZINGUTILS
@@ -0,0 +1,241
1 #include "FuzzingDefs.h"
2 #include "FuzzingOperations.h"
3 #include "FuzzingUtils.h"
4
5 #include "AmdaProvider.h"
6
7 #include <Network/NetworkController.h>
8 #include <SqpApplication.h>
9 #include <Time/TimeController.h>
10 #include <Variable/VariableController.h>
11
12 #include <QLoggingCategory>
13 #include <QObject>
14 #include <QtTest>
15
16 #include <memory>
17
18 Q_LOGGING_CATEGORY(LOG_TestAmdaFuzzing, "TestAmdaFuzzing")
19
20 namespace {
21
22 // /////// //
23 // Aliases //
24 // /////// //
25
26 using VariableId = int;
27
28 using VariableOperation = std::pair<VariableId, std::shared_ptr<IFuzzingOperation> >;
29 using VariablesOperations = std::vector<VariableOperation>;
30
31 using OperationsPool = std::set<std::shared_ptr<IFuzzingOperation> >;
32 using VariablesPool = std::map<VariableId, std::shared_ptr<Variable> >;
33
34 // ///////// //
35 // Constants //
36 // ///////// //
37
38 // Defaults values used when the associated properties have not been set for the test
39 const auto NB_MAX_OPERATIONS_DEFAULT_VALUE = 100;
40 const auto NB_MAX_VARIABLES_DEFAULT_VALUE = 1;
41 const auto AVAILABLE_OPERATIONS_DEFAULT_VALUE
42 = QVariant::fromValue(OperationsTypes{FuzzingOperationType::CREATE});
43
44 // /////// //
45 // Methods //
46 // /////// //
47
48 /// Goes through the variables pool and operations pool to determine the set of {variable/operation}
49 /// pairs that are valid (i.e. operation that can be executed on variable)
50 VariablesOperations availableOperations(const VariablesPool &variablesPool,
51 const OperationsPool &operationsPool)
52 {
53 VariablesOperations result{};
54
55 for (const auto &variablesPoolEntry : variablesPool) {
56 auto variableId = variablesPoolEntry.first;
57 auto variable = variablesPoolEntry.second;
58
59 for (const auto &operation : operationsPool) {
60 // A pair is valid if the current operation can be executed on the current variable
61 if (operation->canExecute(variable)) {
62 result.push_back({variableId, operation});
63 }
64 }
65 }
66
67 return result;
68 }
69
70 OperationsPool createOperationsPool(const OperationsTypes &types)
71 {
72 OperationsPool result{};
73
74 std::transform(types.cbegin(), types.cend(), std::inserter(result, result.end()),
75 [](const auto &type) { return FuzzingOperationFactory::create(type); });
76
77 return result;
78 }
79
80 /**
81 * Class to run random tests
82 */
83 class FuzzingTest {
84 public:
85 explicit FuzzingTest(VariableController &variableController, Properties properties)
86 : m_VariableController{variableController},
87 m_Properties{std::move(properties)},
88 m_VariablesPool{}
89 {
90 // Inits variables pool: at init, all variables are null
91 for (auto variableId = 0; variableId < nbMaxVariables(); ++variableId) {
92 m_VariablesPool[variableId] = nullptr;
93 }
94 }
95
96 void execute()
97 {
98 qCInfo(LOG_TestAmdaFuzzing()) << "Running" << nbMaxOperations() << "operations on"
99 << nbMaxVariables() << "variable(s)...";
100
101 auto canExecute = true;
102 for (auto i = 0; i < nbMaxOperations() && canExecute; ++i) {
103 // Retrieves all operations that can be executed in the current context
104 auto variableOperations = availableOperations(m_VariablesPool, operationsPool());
105
106 canExecute = !variableOperations.empty();
107 if (canExecute) {
108 // Of the operations available, chooses a random operation and executes it
109 auto variableOperation
110 = RandomGenerator::instance().randomChoice(variableOperations);
111
112 auto variableId = variableOperation.first;
113 auto variable = m_VariablesPool.at(variableId);
114 auto fuzzingOperation = variableOperation.second;
115
116 fuzzingOperation->execute(variable, m_VariableController, m_Properties);
117
118 // Updates variable pool with the new state of the variable after operation
119 m_VariablesPool[variableId] = variable;
120 }
121 else {
122 qCInfo(LOG_TestAmdaFuzzing())
123 << "No more operations are available, the execution of the test will stop...";
124 }
125 }
126
127 qCInfo(LOG_TestAmdaFuzzing()) << "Execution of the test completed.";
128 }
129
130 private:
131 int nbMaxOperations() const
132 {
133 static auto result
134 = m_Properties.value(NB_MAX_OPERATIONS_PROPERTY, NB_MAX_OPERATIONS_DEFAULT_VALUE)
135 .toInt();
136 return result;
137 }
138
139 int nbMaxVariables() const
140 {
141 static auto result
142 = m_Properties.value(NB_MAX_VARIABLES_PROPERTY, NB_MAX_VARIABLES_DEFAULT_VALUE).toInt();
143 return result;
144 }
145
146 OperationsPool operationsPool() const
147 {
148 static auto result = createOperationsPool(
149 m_Properties.value(AVAILABLE_OPERATIONS_PROPERTY, AVAILABLE_OPERATIONS_DEFAULT_VALUE)
150 .value<OperationsTypes>());
151 return result;
152 }
153
154 VariableController &m_VariableController;
155 Properties m_Properties;
156 VariablesPool m_VariablesPool;
157 };
158
159 } // namespace
160
161 class TestAmdaFuzzing : public QObject {
162 Q_OBJECT
163
164 private slots:
165 /// Input data for @sa testFuzzing()
166 void testFuzzing_data();
167 void testFuzzing();
168 };
169
170 void TestAmdaFuzzing::testFuzzing_data()
171 {
172 // ////////////// //
173 // Test structure //
174 // ////////////// //
175
176 QTest::addColumn<Properties>("properties"); // Properties for random test
177
178 // ////////// //
179 // Test cases //
180 // ////////// //
181
182 auto maxRange = SqpRange::fromDateTime({2017, 1, 1}, {0, 0}, {2017, 1, 5}, {0, 0});
183 MetadataPool metadataPool{{{"dataType", "vector"}, {"xml:id", "imf"}}};
184
185 // Note: we don't use auto here as we want to pass std::shared_ptr<IDataProvider> as is in the
186 // QVariant
187 std::shared_ptr<IDataProvider> provider = std::make_shared<AmdaProvider>();
188
189 QTest::newRow("fuzzingTest") << Properties{
190 {MAX_RANGE_PROPERTY, QVariant::fromValue(maxRange)},
191 {METADATA_POOL_PROPERTY, QVariant::fromValue(metadataPool)},
192 {PROVIDER_PROPERTY, QVariant::fromValue(provider)}};
193 }
194
195 void TestAmdaFuzzing::testFuzzing()
196 {
197 QFETCH(Properties, properties);
198
199 auto &variableController = sqpApp->variableController();
200 auto &timeController = sqpApp->timeController();
201
202 // Generates random initial range (bounded to max range)
203 auto maxRange = properties.value(MAX_RANGE_PROPERTY, QVariant::fromValue(INVALID_RANGE))
204 .value<SqpRange>();
205
206 QVERIFY(maxRange != INVALID_RANGE);
207
208 auto initialRangeStart
209 = RandomGenerator::instance().generateDouble(maxRange.m_TStart, maxRange.m_TEnd);
210 auto initialRangeEnd
211 = RandomGenerator::instance().generateDouble(maxRange.m_TStart, maxRange.m_TEnd);
212 if (initialRangeStart > initialRangeEnd) {
213 std::swap(initialRangeStart, initialRangeEnd);
214 }
215
216 // Sets initial range on time controller
217 SqpRange initialRange{initialRangeStart, initialRangeEnd};
218 qCInfo(LOG_TestAmdaFuzzing()) << "Setting initial range to" << initialRange << "...";
219 timeController.onTimeToUpdate(initialRange);
220
221 FuzzingTest test{variableController, properties};
222 test.execute();
223 }
224
225 int main(int argc, char *argv[])
226 {
227 QLoggingCategory::setFilterRules(
228 "*.warning=false\n"
229 "*.info=false\n"
230 "*.debug=false\n"
231 "FuzzingOperations.info=true\n"
232 "TestAmdaFuzzing.info=true\n");
233
234 SqpApplication app{argc, argv};
235 app.setAttribute(Qt::AA_Use96Dpi, true);
236 TestAmdaFuzzing testObject{};
237 QTEST_SET_MAIN_SOURCE_PATH
238 return QTest::qExec(&testObject, argc, argv);
239 }
240
241 #include "TestAmdaFuzzing.moc"
@@ -14,6 +14,14
14 * @brief The SqpRange struct holds the information of time parameters
14 * @brief The SqpRange struct holds the information of time parameters
15 */
15 */
16 struct SqpRange {
16 struct SqpRange {
17 /// Creates SqpRange from dates and times
18 static SqpRange fromDateTime(const QDate &startDate, const QTime &startTime,
19 const QDate &endDate, const QTime &endTime)
20 {
21 return {DateUtils::secondsSinceEpoch(QDateTime{startDate, startTime}),
22 DateUtils::secondsSinceEpoch(QDateTime{endDate, endTime})};
23 }
24
17 /// Start time (UTC)
25 /// Start time (UTC)
18 double m_TStart;
26 double m_TStart;
19 /// End time (UTC)
27 /// End time (UTC)
@@ -115,6 +115,7 IF(BUILD_TESTS)
115 ${testDirectory}/*.cpp
115 ${testDirectory}/*.cpp
116 ${testDirectory}/*.h)
116 ${testDirectory}/*.h)
117 LIST (REMOVE_ITEM currentTestSources ${TESTS_SOURCES})
117 LIST (REMOVE_ITEM currentTestSources ${TESTS_SOURCES})
118 LIST(APPEND testFilesToFormat ${currentTestSources})
118 # LIST (REMOVE_ITEM currentTestSources ${TESTS_HEADERS})
119 # LIST (REMOVE_ITEM currentTestSources ${TESTS_HEADERS})
119
120
120 ADD_EXECUTABLE(${testName} ${testFile} ${currentTestSources})
121 ADD_EXECUTABLE(${testName} ${testFile} ${currentTestSources})
@@ -62,7 +62,17 sciqlop_amdaplugin = library('amdaplugin',
62 tests = [
62 tests = [
63 [['tests/TestAmdaParser.cpp'],'test_amda_parser','AMDA parser test'],
63 [['tests/TestAmdaParser.cpp'],'test_amda_parser','AMDA parser test'],
64 [['tests/TestAmdaResultParser.cpp'],'test_amda_result_parser','AMDA result parser test'],
64 [['tests/TestAmdaResultParser.cpp'],'test_amda_result_parser','AMDA result parser test'],
65 [['tests/TestAmdaAcquisition.cpp'],'test_amda_acquisition','AMDA Acquisition test']
65 [['tests/TestAmdaAcquisition.cpp'],'test_amda_acquisition','AMDA Acquisition test'],
66 [['tests/TestAmdaFuzzing.cpp'],'test_amda_fuzzing','AMDA fuzzing test']
67 ]
68
69 tests_sources = [
70 'tests/FuzzingDefs.h',
71 'tests/FuzzingDefs.cpp',
72 'tests/FuzzingOperations.h',
73 'tests/FuzzingOperations.cpp',
74 'tests/FuzzingUtils.h',
75 'tests/FuzzingUtils.cpp'
66 ]
76 ]
67
77
68 foreach unit_test : tests
78 foreach unit_test : tests
@@ -71,6 +81,7 foreach unit_test : tests
71 link_with : [sciqlop_amdaplugin],
81 link_with : [sciqlop_amdaplugin],
72 include_directories : [amdaplugin_inc],
82 include_directories : [amdaplugin_inc],
73 cpp_args : ['-DAMDA_TESTS_RESOURCES_DIR="'+meson.current_source_dir()+'/tests-resources"'],
83 cpp_args : ['-DAMDA_TESTS_RESOURCES_DIR="'+meson.current_source_dir()+'/tests-resources"'],
84 sources : [tests_sources],
74 dependencies : [sciqlop_core, sciqlop_gui, qt5test])
85 dependencies : [sciqlop_core, sciqlop_gui, qt5test])
75 test(unit_test[2], test_exe, args: ['-teamcity', '-o', '@0@.teamcity.txt'.format(unit_test[1])], timeout: 3 * 60)
86 test(unit_test[2], test_exe, args: ['-teamcity', '-o', '@0@.teamcity.txt'.format(unit_test[1])], timeout: 3 * 60)
76 endforeach
87 endforeach
General Comments 0
You need to be logged in to leave comments. Login now