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