##// END OF EJS Templates
Completes fuzzing test structure by setting initial range for the time controller
Alexandre Leroux -
r1178:324a3ee21c58
parent child
Show More
@@ -1,58 +1,66
1 #ifndef SCIQLOP_SQPRANGE_H
1 #ifndef SCIQLOP_SQPRANGE_H
2 #define SCIQLOP_SQPRANGE_H
2 #define SCIQLOP_SQPRANGE_H
3
3
4 #include <QObject>
4 #include <QObject>
5
5
6 #include <QDebug>
6 #include <QDebug>
7
7
8 #include <Common/DateUtils.h>
8 #include <Common/DateUtils.h>
9 #include <Common/MetaTypes.h>
9 #include <Common/MetaTypes.h>
10
10
11 #include <cmath>
11 #include <cmath>
12
12
13 /**
13 /**
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)
20 double m_TEnd;
28 double m_TEnd;
21
29
22 bool contains(const SqpRange &dateTime) const noexcept
30 bool contains(const SqpRange &dateTime) const noexcept
23 {
31 {
24 return (m_TStart <= dateTime.m_TStart && m_TEnd >= dateTime.m_TEnd);
32 return (m_TStart <= dateTime.m_TStart && m_TEnd >= dateTime.m_TEnd);
25 }
33 }
26
34
27 bool intersect(const SqpRange &dateTime) const noexcept
35 bool intersect(const SqpRange &dateTime) const noexcept
28 {
36 {
29 return (m_TEnd >= dateTime.m_TStart && m_TStart <= dateTime.m_TEnd);
37 return (m_TEnd >= dateTime.m_TStart && m_TStart <= dateTime.m_TEnd);
30 }
38 }
31
39
32 bool operator==(const SqpRange &other) const
40 bool operator==(const SqpRange &other) const
33 {
41 {
34 auto equals = [](const auto &v1, const auto &v2) {
42 auto equals = [](const auto &v1, const auto &v2) {
35 return (std::isnan(v1) && std::isnan(v2)) || v1 == v2;
43 return (std::isnan(v1) && std::isnan(v2)) || v1 == v2;
36 };
44 };
37
45
38 return equals(m_TStart, other.m_TStart) && equals(m_TEnd, other.m_TEnd);
46 return equals(m_TStart, other.m_TStart) && equals(m_TEnd, other.m_TEnd);
39 }
47 }
40 bool operator!=(const SqpRange &other) const { return !(*this == other); }
48 bool operator!=(const SqpRange &other) const { return !(*this == other); }
41 };
49 };
42
50
43 const auto INVALID_RANGE
51 const auto INVALID_RANGE
44 = SqpRange{std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN()};
52 = SqpRange{std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN()};
45
53
46 inline QDebug operator<<(QDebug d, SqpRange obj)
54 inline QDebug operator<<(QDebug d, SqpRange obj)
47 {
55 {
48 auto tendDateTimeStart = DateUtils::dateTime(obj.m_TStart);
56 auto tendDateTimeStart = DateUtils::dateTime(obj.m_TStart);
49 auto tendDateTimeEnd = DateUtils::dateTime(obj.m_TEnd);
57 auto tendDateTimeEnd = DateUtils::dateTime(obj.m_TEnd);
50
58
51 d << "ts: " << tendDateTimeStart << " te: " << tendDateTimeEnd;
59 d << "ts: " << tendDateTimeStart << " te: " << tendDateTimeEnd;
52 return d;
60 return d;
53 }
61 }
54
62
55 // Required for using shared_ptr in signals/slots
63 // Required for using shared_ptr in signals/slots
56 SCIQLOP_REGISTER_META_TYPE(SQPRANGE_REGISTRY, SqpRange)
64 SCIQLOP_REGISTER_META_TYPE(SQPRANGE_REGISTRY, SqpRange)
57
65
58 #endif // SCIQLOP_SQPRANGE_H
66 #endif // SCIQLOP_SQPRANGE_H
@@ -1,7 +1,8
1 #include "FuzzingDefs.h"
1 #include "FuzzingDefs.h"
2
2
3 const QString NB_MAX_OPERATIONS_PROPERTY = QStringLiteral("component");
3 const QString NB_MAX_OPERATIONS_PROPERTY = QStringLiteral("component");
4 const QString NB_MAX_VARIABLES_PROPERTY = QStringLiteral("nbMaxVariables");
4 const QString NB_MAX_VARIABLES_PROPERTY = QStringLiteral("nbMaxVariables");
5 const QString AVAILABLE_OPERATIONS_PROPERTY = QStringLiteral("availableOperations");
5 const QString AVAILABLE_OPERATIONS_PROPERTY = QStringLiteral("availableOperations");
6 const QString MAX_RANGE_PROPERTY = QStringLiteral("maxRange");
6 const QString METADATA_POOL_PROPERTY = QStringLiteral("metadataPool");
7 const QString METADATA_POOL_PROPERTY = QStringLiteral("metadataPool");
7 const QString PROVIDER_PROPERTY = QStringLiteral("provider");
8 const QString PROVIDER_PROPERTY = QStringLiteral("provider");
@@ -1,35 +1,38
1 #ifndef SCIQLOP_FUZZINGDEFS_H
1 #ifndef SCIQLOP_FUZZINGDEFS_H
2 #define SCIQLOP_FUZZINGDEFS_H
2 #define SCIQLOP_FUZZINGDEFS_H
3
3
4 #include <QString>
4 #include <QString>
5 #include <QVariantHash>
5 #include <QVariantHash>
6
6
7 // /////// //
7 // /////// //
8 // Aliases //
8 // Aliases //
9 // /////// //
9 // /////// //
10
10
11 using MetadataPool = std::vector<QVariantHash>;
11 using MetadataPool = std::vector<QVariantHash>;
12 Q_DECLARE_METATYPE(MetadataPool)
12 Q_DECLARE_METATYPE(MetadataPool)
13
13
14 using Properties = QVariantHash;
14 using Properties = QVariantHash;
15
15
16 // ///////// //
16 // ///////// //
17 // Constants //
17 // Constants //
18 // ///////// //
18 // ///////// //
19
19
20 /// Max number of operations to generate
20 /// Max number of operations to generate
21 extern const QString NB_MAX_OPERATIONS_PROPERTY;
21 extern const QString NB_MAX_OPERATIONS_PROPERTY;
22
22
23 /// Max number of variables to manipulate through operations
23 /// Max number of variables to manipulate through operations
24 extern const QString NB_MAX_VARIABLES_PROPERTY;
24 extern const QString NB_MAX_VARIABLES_PROPERTY;
25
25
26 /// Set of operations available for the test
26 /// Set of operations available for the test
27 extern const QString AVAILABLE_OPERATIONS_PROPERTY;
27 extern const QString AVAILABLE_OPERATIONS_PROPERTY;
28
28
29 /// Max range that an operation can reach
30 extern const QString MAX_RANGE_PROPERTY;
31
29 /// Set of metadata that can be associated to a variable
32 /// Set of metadata that can be associated to a variable
30 extern const QString METADATA_POOL_PROPERTY;
33 extern const QString METADATA_POOL_PROPERTY;
31
34
32 /// Provider used to retrieve data
35 /// Provider used to retrieve data
33 extern const QString PROVIDER_PROPERTY;
36 extern const QString PROVIDER_PROPERTY;
34
37
35 #endif // SCIQLOP_FUZZINGDEFS_H
38 #endif // SCIQLOP_FUZZINGDEFS_H
@@ -1,209 +1,229
1 #include "FuzzingDefs.h"
1 #include "FuzzingDefs.h"
2 #include "FuzzingOperations.h"
2 #include "FuzzingOperations.h"
3 #include "FuzzingUtils.h"
3 #include "FuzzingUtils.h"
4
4
5 #include <Network/NetworkController.h>
5 #include <Network/NetworkController.h>
6 #include <SqpApplication.h>
6 #include <SqpApplication.h>
7 #include <Time/TimeController.h>
7 #include <Time/TimeController.h>
8 #include <Variable/VariableController.h>
8 #include <Variable/VariableController.h>
9
9
10 #include <QLoggingCategory>
10 #include <QLoggingCategory>
11 #include <QObject>
11 #include <QObject>
12 #include <QtTest>
12 #include <QtTest>
13
13
14 #include <memory>
14 #include <memory>
15
15
16 Q_LOGGING_CATEGORY(LOG_TestAmdaFuzzing, "TestAmdaFuzzing")
16 Q_LOGGING_CATEGORY(LOG_TestAmdaFuzzing, "TestAmdaFuzzing")
17
17
18 namespace {
18 namespace {
19
19
20 // /////// //
20 // /////// //
21 // Aliases //
21 // Aliases //
22 // /////// //
22 // /////// //
23
23
24 using VariableId = int;
24 using VariableId = int;
25
25
26 using VariableOperation = std::pair<VariableId, std::shared_ptr<IFuzzingOperation> >;
26 using VariableOperation = std::pair<VariableId, std::shared_ptr<IFuzzingOperation> >;
27 using VariablesOperations = std::vector<VariableOperation>;
27 using VariablesOperations = std::vector<VariableOperation>;
28
28
29 using OperationsPool = std::set<std::shared_ptr<IFuzzingOperation> >;
29 using OperationsPool = std::set<std::shared_ptr<IFuzzingOperation> >;
30 using VariablesPool = std::map<VariableId, std::shared_ptr<Variable> >;
30 using VariablesPool = std::map<VariableId, std::shared_ptr<Variable> >;
31
31
32 // ///////// //
32 // ///////// //
33 // Constants //
33 // Constants //
34 // ///////// //
34 // ///////// //
35
35
36 // Defaults values used when the associated properties have not been set for the test
36 // Defaults values used when the associated properties have not been set for the test
37 const auto NB_MAX_OPERATIONS_DEFAULT_VALUE = 100;
37 const auto NB_MAX_OPERATIONS_DEFAULT_VALUE = 100;
38 const auto NB_MAX_VARIABLES_DEFAULT_VALUE = 1;
38 const auto NB_MAX_VARIABLES_DEFAULT_VALUE = 1;
39 const auto AVAILABLE_OPERATIONS_DEFAULT_VALUE
39 const auto AVAILABLE_OPERATIONS_DEFAULT_VALUE
40 = QVariant::fromValue(OperationsTypes{FuzzingOperationType::CREATE});
40 = QVariant::fromValue(OperationsTypes{FuzzingOperationType::CREATE});
41
41 // /////// //
42 // /////// //
42 // Methods //
43 // Methods //
43 // /////// //
44 // /////// //
44
45
45 /// Goes through the variables pool and operations pool to determine the set of {variable/operation}
46 /// Goes through the variables pool and operations pool to determine the set of {variable/operation}
46 /// pairs that are valid (i.e. operation that can be executed on variable)
47 /// pairs that are valid (i.e. operation that can be executed on variable)
47 VariablesOperations availableOperations(const VariablesPool &variablesPool,
48 VariablesOperations availableOperations(const VariablesPool &variablesPool,
48 const OperationsPool &operationsPool)
49 const OperationsPool &operationsPool)
49 {
50 {
50 VariablesOperations result{};
51 VariablesOperations result{};
51
52
52 for (const auto &variablesPoolEntry : variablesPool) {
53 for (const auto &variablesPoolEntry : variablesPool) {
53 auto variableId = variablesPoolEntry.first;
54 auto variableId = variablesPoolEntry.first;
54 auto variable = variablesPoolEntry.second;
55 auto variable = variablesPoolEntry.second;
55
56
56 for (const auto &operation : operationsPool) {
57 for (const auto &operation : operationsPool) {
57 // A pair is valid if the current operation can be executed on the current variable
58 // A pair is valid if the current operation can be executed on the current variable
58 if (operation->canExecute(variable)) {
59 if (operation->canExecute(variable)) {
59 result.push_back({variableId, operation});
60 result.push_back({variableId, operation});
60 }
61 }
61 }
62 }
62 }
63 }
63
64
64 return result;
65 return result;
65 }
66 }
66
67
67 OperationsPool createOperationsPool(const OperationsTypes &types)
68 OperationsPool createOperationsPool(const OperationsTypes &types)
68 {
69 {
69 OperationsPool result{};
70 OperationsPool result{};
70
71
71 std::transform(types.cbegin(), types.cend(), std::inserter(result, result.end()),
72 std::transform(types.cbegin(), types.cend(), std::inserter(result, result.end()),
72 [](const auto &type) { return FuzzingOperationFactory::create(type); });
73 [](const auto &type) { return FuzzingOperationFactory::create(type); });
73
74
74 return result;
75 return result;
75 }
76 }
76
77
77 /**
78 /**
78 * Class to run random tests
79 * Class to run random tests
79 */
80 */
80 class FuzzingTest {
81 class FuzzingTest {
81 public:
82 public:
82 explicit FuzzingTest(VariableController &variableController, Properties properties)
83 explicit FuzzingTest(VariableController &variableController, Properties properties)
83 : m_VariableController{variableController},
84 : m_VariableController{variableController},
84 m_Properties{std::move(properties)},
85 m_Properties{std::move(properties)},
85 m_VariablesPool{}
86 m_VariablesPool{}
86 {
87 {
87 // Inits variables pool: at init, all variables are null
88 // Inits variables pool: at init, all variables are null
88 for (auto variableId = 0; variableId < nbMaxVariables(); ++variableId) {
89 for (auto variableId = 0; variableId < nbMaxVariables(); ++variableId) {
89 m_VariablesPool[variableId] = nullptr;
90 m_VariablesPool[variableId] = nullptr;
90 }
91 }
91 }
92 }
92
93
93 void execute()
94 void execute()
94 {
95 {
95 qCInfo(LOG_TestAmdaFuzzing()) << "Running" << nbMaxOperations() << "operations on"
96 qCInfo(LOG_TestAmdaFuzzing()) << "Running" << nbMaxOperations() << "operations on"
96 << nbMaxVariables() << "variables...";
97 << nbMaxVariables() << "variables...";
97
98
98 auto canExecute = true;
99 auto canExecute = true;
99 for (auto i = 0; i < nbMaxOperations() && canExecute; ++i) {
100 for (auto i = 0; i < nbMaxOperations() && canExecute; ++i) {
100 // Retrieves all operations that can be executed in the current context
101 // Retrieves all operations that can be executed in the current context
101 auto variableOperations = availableOperations(m_VariablesPool, operationsPool());
102 auto variableOperations = availableOperations(m_VariablesPool, operationsPool());
102
103
103 canExecute = !variableOperations.empty();
104 canExecute = !variableOperations.empty();
104 if (canExecute) {
105 if (canExecute) {
105 // Of the operations available, chooses a random operation and executes it
106 // Of the operations available, chooses a random operation and executes it
106 auto variableOperation
107 auto variableOperation
107 = RandomGenerator::instance().randomChoice(variableOperations);
108 = RandomGenerator::instance().randomChoice(variableOperations);
108
109
109 auto variableId = variableOperation.first;
110 auto variableId = variableOperation.first;
110 auto variable = m_VariablesPool.at(variableId);
111 auto variable = m_VariablesPool.at(variableId);
111 auto fuzzingOperation = variableOperation.second;
112 auto fuzzingOperation = variableOperation.second;
112
113
113 fuzzingOperation->execute(variable, m_VariableController, m_Properties);
114 fuzzingOperation->execute(variable, m_VariableController, m_Properties);
114
115
115 // Updates variable pool with the new state of the variable after operation
116 // Updates variable pool with the new state of the variable after operation
116 m_VariablesPool[variableId] = variable;
117 m_VariablesPool[variableId] = variable;
117 }
118 }
118 else {
119 else {
119 qCInfo(LOG_TestAmdaFuzzing())
120 qCInfo(LOG_TestAmdaFuzzing())
120 << "No more operations are available, the execution of the test will stop...";
121 << "No more operations are available, the execution of the test will stop...";
121 }
122 }
122 }
123 }
123
124
124 qCInfo(LOG_TestAmdaFuzzing()) << "Execution of the test completed.";
125 qCInfo(LOG_TestAmdaFuzzing()) << "Execution of the test completed.";
125 }
126 }
126
127
127 private:
128 private:
128 int nbMaxOperations() const
129 int nbMaxOperations() const
129 {
130 {
130 static auto result
131 static auto result
131 = m_Properties.value(NB_MAX_OPERATIONS_PROPERTY, NB_MAX_OPERATIONS_DEFAULT_VALUE)
132 = m_Properties.value(NB_MAX_OPERATIONS_PROPERTY, NB_MAX_OPERATIONS_DEFAULT_VALUE)
132 .toInt();
133 .toInt();
133 return result;
134 return result;
134 }
135 }
135
136
136 int nbMaxVariables() const
137 int nbMaxVariables() const
137 {
138 {
138 static auto result
139 static auto result
139 = m_Properties.value(NB_MAX_VARIABLES_PROPERTY, NB_MAX_VARIABLES_DEFAULT_VALUE).toInt();
140 = m_Properties.value(NB_MAX_VARIABLES_PROPERTY, NB_MAX_VARIABLES_DEFAULT_VALUE).toInt();
140 return result;
141 return result;
141 }
142 }
142
143
143 OperationsPool operationsPool() const
144 OperationsPool operationsPool() const
144 {
145 {
145 static auto result = createOperationsPool(
146 static auto result = createOperationsPool(
146 m_Properties.value(AVAILABLE_OPERATIONS_PROPERTY, AVAILABLE_OPERATIONS_DEFAULT_VALUE)
147 m_Properties.value(AVAILABLE_OPERATIONS_PROPERTY, AVAILABLE_OPERATIONS_DEFAULT_VALUE)
147 .value<OperationsTypes>());
148 .value<OperationsTypes>());
148 return result;
149 return result;
149 }
150 }
150
151
151 VariableController &m_VariableController;
152 VariableController &m_VariableController;
152 Properties m_Properties;
153 Properties m_Properties;
153 VariablesPool m_VariablesPool;
154 VariablesPool m_VariablesPool;
154 };
155 };
155
156
156 } // namespace
157 } // namespace
157
158
158 class TestAmdaFuzzing : public QObject {
159 class TestAmdaFuzzing : public QObject {
159 Q_OBJECT
160 Q_OBJECT
160
161
161 private slots:
162 private slots:
162 /// Input data for @sa testFuzzing()
163 /// Input data for @sa testFuzzing()
163 void testFuzzing_data();
164 void testFuzzing_data();
164 void testFuzzing();
165 void testFuzzing();
165 };
166 };
166
167
167 void TestAmdaFuzzing::testFuzzing_data()
168 void TestAmdaFuzzing::testFuzzing_data()
168 {
169 {
169 // ////////////// //
170 // ////////////// //
170 // Test structure //
171 // Test structure //
171 // ////////////// //
172 // ////////////// //
172
173
173 QTest::addColumn<Properties>("properties"); // Properties for random test
174 QTest::addColumn<Properties>("properties"); // Properties for random test
174
175
175 // ////////// //
176 // ////////// //
176 // Test cases //
177 // Test cases //
177 // ////////// //
178 // ////////// //
178
179
179 ///@todo: complete
180 ///@todo: complete
180 }
181 }
181
182
182 void TestAmdaFuzzing::testFuzzing()
183 void TestAmdaFuzzing::testFuzzing()
183 {
184 {
184 QFETCH(Properties, properties);
185 QFETCH(Properties, properties);
185
186
186 auto &variableController = sqpApp->variableController();
187 auto &variableController = sqpApp->variableController();
187 auto &timeController = sqpApp->timeController();
188 auto &timeController = sqpApp->timeController();
188
189
190 // Generates random initial range (bounded to max range)
191 auto maxRange = properties.value(MAX_RANGE_PROPERTY, QVariant::fromValue(INVALID_RANGE))
192 .value<SqpRange>();
193
194 QVERIFY(maxRange != INVALID_RANGE);
195
196 auto initialRangeStart
197 = RandomGenerator::instance().generateDouble(maxRange.m_TStart, maxRange.m_TEnd);
198 auto initialRangeEnd
199 = RandomGenerator::instance().generateDouble(maxRange.m_TStart, maxRange.m_TEnd);
200 if (initialRangeStart > initialRangeEnd) {
201 std::swap(initialRangeStart, initialRangeEnd);
202 }
203
204 // Sets initial range on time controller
205 SqpRange initialRange{initialRangeStart, initialRangeEnd};
206 qCInfo(LOG_TestAmdaFuzzing()) << "Setting initial range to" << initialRange << "...";
207 timeController.onTimeToUpdate(initialRange);
208
189 FuzzingTest test{variableController, properties};
209 FuzzingTest test{variableController, properties};
190 test.execute();
210 test.execute();
191 }
211 }
192
212
193 int main(int argc, char *argv[])
213 int main(int argc, char *argv[])
194 {
214 {
195 QLoggingCategory::setFilterRules(
215 QLoggingCategory::setFilterRules(
196 "*.warning=false\n"
216 "*.warning=false\n"
197 "*.info=false\n"
217 "*.info=false\n"
198 "*.debug=false\n"
218 "*.debug=false\n"
199 "FuzzingOperations.info=true\n"
219 "FuzzingOperations.info=true\n"
200 "TestAmdaFuzzing.info=true\n");
220 "TestAmdaFuzzing.info=true\n");
201
221
202 SqpApplication app{argc, argv};
222 SqpApplication app{argc, argv};
203 app.setAttribute(Qt::AA_Use96Dpi, true);
223 app.setAttribute(Qt::AA_Use96Dpi, true);
204 TestAmdaFuzzing testObject{};
224 TestAmdaFuzzing testObject{};
205 QTEST_SET_MAIN_SOURCE_PATH
225 QTEST_SET_MAIN_SOURCE_PATH
206 return QTest::qExec(&testObject, argc, argv);
226 return QTest::qExec(&testObject, argc, argv);
207 }
227 }
208
228
209 #include "TestAmdaFuzzing.moc"
229 #include "TestAmdaFuzzing.moc"
General Comments 0
You need to be logged in to leave comments. Login now