##// END OF EJS Templates
Implements test execute() method...
Alexandre Leroux -
r1205:0c07405da56c
parent child
Show More
@@ -1,153 +1,207
1 #include "FuzzingDefs.h"
1 #include "FuzzingDefs.h"
2 #include "FuzzingOperations.h"
2 #include "FuzzingOperations.h"
3 #include <Network/NetworkController.h>
3 #include <Network/NetworkController.h>
4 #include <SqpApplication.h>
4 #include <SqpApplication.h>
5 #include <Time/TimeController.h>
5 #include <Time/TimeController.h>
6 #include <Variable/VariableController.h>
6 #include <Variable/VariableController.h>
7
7
8 #include <QLoggingCategory>
8 #include <QLoggingCategory>
9 #include <QObject>
9 #include <QObject>
10 #include <QtTest>
10 #include <QtTest>
11
11
12 #include <memory>
12 #include <memory>
13
13
14 Q_LOGGING_CATEGORY(LOG_TestAmdaFuzzing, "TestAmdaFuzzing")
14 Q_LOGGING_CATEGORY(LOG_TestAmdaFuzzing, "TestAmdaFuzzing")
15
15
16 namespace {
16 namespace {
17
17
18 // /////// //
18 // /////// //
19 // Aliases //
19 // Aliases //
20 // /////// //
20 // /////// //
21
21
22 using VariableId = int;
22 using VariableId = int;
23
23
24 using VariableOperation = std::pair<VariableId, std::shared_ptr<IFuzzingOperation> >;
25 using VariablesOperations = std::vector<VariableOperation>;
26
24 using OperationsPool = std::set<std::shared_ptr<IFuzzingOperation> >;
27 using OperationsPool = std::set<std::shared_ptr<IFuzzingOperation> >;
25 using VariablesPool = std::map<VariableId, std::shared_ptr<Variable> >;
28 using VariablesPool = std::map<VariableId, std::shared_ptr<Variable> >;
26
29
27 // ///////// //
30 // ///////// //
28 // Constants //
31 // Constants //
29 // ///////// //
32 // ///////// //
30
33
31 // Defaults values used when the associated properties have not been set for the test
34 // Defaults values used when the associated properties have not been set for the test
32 const auto NB_MAX_OPERATIONS_DEFAULT_VALUE = 100;
35 const auto NB_MAX_OPERATIONS_DEFAULT_VALUE = 100;
33 const auto NB_MAX_VARIABLES_DEFAULT_VALUE = 1;
36 const auto NB_MAX_VARIABLES_DEFAULT_VALUE = 1;
34 const auto AVAILABLE_OPERATIONS_DEFAULT_VALUE
37 const auto AVAILABLE_OPERATIONS_DEFAULT_VALUE
35 = QVariant::fromValue(OperationsTypes{FuzzingOperationType::CREATE});
38 = QVariant::fromValue(OperationsTypes{FuzzingOperationType::CREATE});
39 // /////// //
40 // Methods //
41 // /////// //
42
43 /// Goes through the variables pool and operations pool to determine the set of {variable/operation}
44 /// pairs that are valid (i.e. operation that can be executed on variable)
45 VariablesOperations availableOperations(const VariablesPool &variablesPool,
46 const OperationsPool &operationsPool)
47 {
48 VariablesOperations result{};
49
50 for (const auto &variablesPoolEntry : variablesPool) {
51 auto variableId = variablesPoolEntry.first;
52 auto variable = variablesPoolEntry.second;
53
54 for (const auto &operation : operationsPool) {
55 // A pair is valid if the current operation can be executed on the current variable
56 if (operation->canExecute(variable)) {
57 result.push_back({variableId, operation});
58 }
59 }
60 }
61
62 return result;
63 }
64
36 OperationsPool createOperationsPool(const OperationsTypes &types)
65 OperationsPool createOperationsPool(const OperationsTypes &types)
37 {
66 {
38 OperationsPool result{};
67 OperationsPool result{};
39
68
40 std::transform(types.cbegin(), types.cend(), std::inserter(result, result.end()),
69 std::transform(types.cbegin(), types.cend(), std::inserter(result, result.end()),
41 [](const auto &type) { return FuzzingOperationFactory::create(type); });
70 [](const auto &type) { return FuzzingOperationFactory::create(type); });
42
71
43 return result;
72 return result;
44 }
73 }
45
74
46 /**
75 /**
47 * Class to run random tests
76 * Class to run random tests
48 */
77 */
49 class FuzzingTest {
78 class FuzzingTest {
50 public:
79 public:
51 explicit FuzzingTest(VariableController &variableController, Properties properties)
80 explicit FuzzingTest(VariableController &variableController, Properties properties)
52 : m_VariableController{variableController},
81 : m_VariableController{variableController},
53 m_Properties{std::move(properties)},
82 m_Properties{std::move(properties)},
54 m_VariablesPool{}
83 m_VariablesPool{}
55 {
84 {
56 // Inits variables pool: at init, all variables are null
85 // Inits variables pool: at init, all variables are null
57 for (auto variableId = 0; variableId < nbMaxVariables(); ++variableId) {
86 for (auto variableId = 0; variableId < nbMaxVariables(); ++variableId) {
58 m_VariablesPool[variableId] = nullptr;
87 m_VariablesPool[variableId] = nullptr;
59 }
88 }
60 }
89 }
61
90
62 void execute()
91 void execute()
63 {
92 {
64 /// @todo: complete
65 qCInfo(LOG_TestAmdaFuzzing()) << "Running" << nbMaxOperations() << "operations on"
93 qCInfo(LOG_TestAmdaFuzzing()) << "Running" << nbMaxOperations() << "operations on"
66 << nbMaxVariables() << "variables...";
94 << nbMaxVariables() << "variables...";
67
95
96 auto canExecute = true;
97 for (auto i = 0; i < nbMaxOperations() && canExecute; ++i) {
98 // Retrieves all operations that can be executed in the current context
99 auto variableOperations = availableOperations(m_VariablesPool, operationsPool());
100
101 canExecute = !variableOperations.empty();
102 if (canExecute) {
103 // Of the operations available, chooses a random operation and executes it
104 auto variableOperation
105 = /* TODO: gets a random operation */;
106
107 auto variableId = variableOperation.first;
108 auto variable = m_VariablesPool.at(variableId);
109 auto fuzzingOperation = variableOperation.second;
110
111 fuzzingOperation->execute(variable, m_VariableController, m_Properties);
112
113 // Updates variable pool with the new state of the variable after operation
114 m_VariablesPool[variableId] = variable;
115 }
116 else {
117 qCInfo(LOG_TestAmdaFuzzing())
118 << "No more operations are available, the execution of the test will stop...";
119 }
120 }
121
68 qCInfo(LOG_TestAmdaFuzzing()) << "Execution of the test completed.";
122 qCInfo(LOG_TestAmdaFuzzing()) << "Execution of the test completed.";
69 }
123 }
70
124
71 private:
125 private:
72 int nbMaxOperations() const
126 int nbMaxOperations() const
73 {
127 {
74 static auto result
128 static auto result
75 = m_Properties.value(NB_MAX_OPERATIONS_PROPERTY, NB_MAX_OPERATIONS_DEFAULT_VALUE)
129 = m_Properties.value(NB_MAX_OPERATIONS_PROPERTY, NB_MAX_OPERATIONS_DEFAULT_VALUE)
76 .toInt();
130 .toInt();
77 return result;
131 return result;
78 }
132 }
79
133
80 int nbMaxVariables() const
134 int nbMaxVariables() const
81 {
135 {
82 static auto result
136 static auto result
83 = m_Properties.value(NB_MAX_VARIABLES_PROPERTY, NB_MAX_VARIABLES_DEFAULT_VALUE).toInt();
137 = m_Properties.value(NB_MAX_VARIABLES_PROPERTY, NB_MAX_VARIABLES_DEFAULT_VALUE).toInt();
84 return result;
138 return result;
85 }
139 }
86
140
87 OperationsPool operationsPool() const
141 OperationsPool operationsPool() const
88 {
142 {
89 static auto result = createOperationsPool(
143 static auto result = createOperationsPool(
90 m_Properties.value(AVAILABLE_OPERATIONS_PROPERTY, AVAILABLE_OPERATIONS_DEFAULT_VALUE)
144 m_Properties.value(AVAILABLE_OPERATIONS_PROPERTY, AVAILABLE_OPERATIONS_DEFAULT_VALUE)
91 .value<OperationsTypes>());
145 .value<OperationsTypes>());
92 return result;
146 return result;
93 }
147 }
94
148
95 VariableController &m_VariableController;
149 VariableController &m_VariableController;
96 Properties m_Properties;
150 Properties m_Properties;
97 VariablesPool m_VariablesPool;
151 VariablesPool m_VariablesPool;
98 };
152 };
99
153
100 } // namespace
154 } // namespace
101
155
102 class TestAmdaFuzzing : public QObject {
156 class TestAmdaFuzzing : public QObject {
103 Q_OBJECT
157 Q_OBJECT
104
158
105 private slots:
159 private slots:
106 /// Input data for @sa testFuzzing()
160 /// Input data for @sa testFuzzing()
107 void testFuzzing_data();
161 void testFuzzing_data();
108 void testFuzzing();
162 void testFuzzing();
109 };
163 };
110
164
111 void TestAmdaFuzzing::testFuzzing_data()
165 void TestAmdaFuzzing::testFuzzing_data()
112 {
166 {
113 // ////////////// //
167 // ////////////// //
114 // Test structure //
168 // Test structure //
115 // ////////////// //
169 // ////////////// //
116
170
117 QTest::addColumn<Properties>("properties"); // Properties for random test
171 QTest::addColumn<Properties>("properties"); // Properties for random test
118
172
119 // ////////// //
173 // ////////// //
120 // Test cases //
174 // Test cases //
121 // ////////// //
175 // ////////// //
122
176
123 ///@todo: complete
177 ///@todo: complete
124 }
178 }
125
179
126 void TestAmdaFuzzing::testFuzzing()
180 void TestAmdaFuzzing::testFuzzing()
127 {
181 {
128 QFETCH(Properties, properties);
182 QFETCH(Properties, properties);
129
183
130 auto &variableController = sqpApp->variableController();
184 auto &variableController = sqpApp->variableController();
131 auto &timeController = sqpApp->timeController();
185 auto &timeController = sqpApp->timeController();
132
186
133 FuzzingTest test{variableController, properties};
187 FuzzingTest test{variableController, properties};
134 test.execute();
188 test.execute();
135 }
189 }
136
190
137 int main(int argc, char *argv[])
191 int main(int argc, char *argv[])
138 {
192 {
139 QLoggingCategory::setFilterRules(
193 QLoggingCategory::setFilterRules(
140 "*.warning=false\n"
194 "*.warning=false\n"
141 "*.info=false\n"
195 "*.info=false\n"
142 "*.debug=false\n"
196 "*.debug=false\n"
143 "FuzzingOperations.info=true\n"
197 "FuzzingOperations.info=true\n"
144 "TestAmdaFuzzing.info=true\n");
198 "TestAmdaFuzzing.info=true\n");
145
199
146 SqpApplication app{argc, argv};
200 SqpApplication app{argc, argv};
147 app.setAttribute(Qt::AA_Use96Dpi, true);
201 app.setAttribute(Qt::AA_Use96Dpi, true);
148 TestAmdaFuzzing testObject{};
202 TestAmdaFuzzing testObject{};
149 QTEST_SET_MAIN_SOURCE_PATH
203 QTEST_SET_MAIN_SOURCE_PATH
150 return QTest::qExec(&testObject, argc, argv);
204 return QTest::qExec(&testObject, argc, argv);
151 }
205 }
152
206
153 #include "TestAmdaFuzzing.moc"
207 #include "TestAmdaFuzzing.moc"
General Comments 0
You need to be logged in to leave comments. Login now