TestAmdaFuzzing.cpp
269 lines
| 8.7 KiB
| text/x-c
|
CppLexer
Alexandre Leroux
|
r1201 | #include "FuzzingDefs.h" | ||
Alexandre Leroux
|
r1202 | #include "FuzzingOperations.h" | ||
Alexandre Leroux
|
r1206 | #include "FuzzingUtils.h" | ||
Alexandre Leroux
|
r1210 | #include "AmdaProvider.h" | ||
Alexandre Leroux
|
r1199 | #include <Network/NetworkController.h> | ||
#include <SqpApplication.h> | ||||
#include <Time/TimeController.h> | ||||
#include <Variable/VariableController.h> | ||||
#include <QLoggingCategory> | ||||
#include <QObject> | ||||
#include <QtTest> | ||||
#include <memory> | ||||
Q_LOGGING_CATEGORY(LOG_TestAmdaFuzzing, "TestAmdaFuzzing") | ||||
namespace { | ||||
Alexandre Leroux
|
r1202 | // /////// // | ||
// Aliases // | ||||
// /////// // | ||||
Alexandre Leroux
|
r1203 | using VariableId = int; | ||
Alexandre Leroux
|
r1216 | using Weight = double; | ||
using Weights = std::vector<Weight>; | ||||
Alexandre Leroux
|
r1203 | |||
Alexandre Leroux
|
r1205 | using VariableOperation = std::pair<VariableId, std::shared_ptr<IFuzzingOperation> >; | ||
using VariablesOperations = std::vector<VariableOperation>; | ||||
Alexandre Leroux
|
r1216 | using WeightedOperationsPool = std::map<std::shared_ptr<IFuzzingOperation>, Weight>; | ||
Alexandre Leroux
|
r1203 | using VariablesPool = std::map<VariableId, std::shared_ptr<Variable> >; | ||
Alexandre Leroux
|
r1201 | // ///////// // | ||
// Constants // | ||||
// ///////// // | ||||
// Defaults values used when the associated properties have not been set for the test | ||||
const auto NB_MAX_OPERATIONS_DEFAULT_VALUE = 100; | ||||
const auto NB_MAX_VARIABLES_DEFAULT_VALUE = 1; | ||||
Alexandre Leroux
|
r1204 | const auto AVAILABLE_OPERATIONS_DEFAULT_VALUE | ||
Alexandre Leroux
|
r1219 | = QVariant::fromValue(WeightedOperationsTypes{{FuzzingOperationType::CREATE, 1.}, | ||
{FuzzingOperationType::PAN_LEFT, 1.}, | ||||
{FuzzingOperationType::PAN_RIGHT, 1.}, | ||||
{FuzzingOperationType::ZOOM_IN, 1.}, | ||||
{FuzzingOperationType::ZOOM_OUT, 1.}}); | ||||
/// Delay between each operation (in ms) | ||||
const auto OPERATION_DELAY_DEFAULT_VALUE = 3000; | ||||
Alexandre Leroux
|
r1209 | |||
Alexandre Leroux
|
r1205 | // /////// // | ||
// Methods // | ||||
// /////// // | ||||
/// Goes through the variables pool and operations pool to determine the set of {variable/operation} | ||||
/// pairs that are valid (i.e. operation that can be executed on variable) | ||||
Alexandre Leroux
|
r1216 | std::pair<VariablesOperations, Weights> | ||
availableOperations(const VariablesPool &variablesPool, | ||||
const WeightedOperationsPool &operationsPool) | ||||
Alexandre Leroux
|
r1205 | { | ||
VariablesOperations result{}; | ||||
Alexandre Leroux
|
r1216 | Weights weights{}; | ||
Alexandre Leroux
|
r1205 | |||
for (const auto &variablesPoolEntry : variablesPool) { | ||||
auto variableId = variablesPoolEntry.first; | ||||
auto variable = variablesPoolEntry.second; | ||||
Alexandre Leroux
|
r1216 | for (const auto &operationsPoolEntry : operationsPool) { | ||
auto operation = operationsPoolEntry.first; | ||||
auto weight = operationsPoolEntry.second; | ||||
Alexandre Leroux
|
r1205 | // A pair is valid if the current operation can be executed on the current variable | ||
if (operation->canExecute(variable)) { | ||||
result.push_back({variableId, operation}); | ||||
Alexandre Leroux
|
r1216 | weights.push_back(weight); | ||
Alexandre Leroux
|
r1205 | } | ||
} | ||||
} | ||||
Alexandre Leroux
|
r1216 | return {result, weights}; | ||
Alexandre Leroux
|
r1205 | } | ||
Alexandre Leroux
|
r1216 | WeightedOperationsPool createOperationsPool(const WeightedOperationsTypes &types) | ||
Alexandre Leroux
|
r1204 | { | ||
Alexandre Leroux
|
r1216 | WeightedOperationsPool result{}; | ||
Alexandre Leroux
|
r1204 | |||
Alexandre Leroux
|
r1216 | std::transform( | ||
types.cbegin(), types.cend(), std::inserter(result, result.end()), [](const auto &type) { | ||||
return std::make_pair(FuzzingOperationFactory::create(type.first), type.second); | ||||
}); | ||||
Alexandre Leroux
|
r1204 | |||
return result; | ||||
} | ||||
Alexandre Leroux
|
r1199 | /** | ||
* Class to run random tests | ||||
*/ | ||||
class FuzzingTest { | ||||
public: | ||||
Alexandre Leroux
|
r1200 | explicit FuzzingTest(VariableController &variableController, Properties properties) | ||
: m_VariableController{variableController}, | ||||
m_Properties{std::move(properties)}, | ||||
Alexandre Leroux
|
r1203 | m_VariablesPool{} | ||
Alexandre Leroux
|
r1200 | { | ||
Alexandre Leroux
|
r1203 | // Inits variables pool: at init, all variables are null | ||
for (auto variableId = 0; variableId < nbMaxVariables(); ++variableId) { | ||||
m_VariablesPool[variableId] = nullptr; | ||||
} | ||||
Alexandre Leroux
|
r1200 | } | ||
Alexandre Leroux
|
r1199 | void execute() | ||
{ | ||||
Alexandre Leroux
|
r1201 | qCInfo(LOG_TestAmdaFuzzing()) << "Running" << nbMaxOperations() << "operations on" | ||
Alexandre Leroux
|
r1210 | << nbMaxVariables() << "variable(s)..."; | ||
Alexandre Leroux
|
r1201 | |||
Alexandre Leroux
|
r1205 | auto canExecute = true; | ||
for (auto i = 0; i < nbMaxOperations() && canExecute; ++i) { | ||||
// Retrieves all operations that can be executed in the current context | ||||
Alexandre Leroux
|
r1216 | VariablesOperations variableOperations{}; | ||
Weights weights{}; | ||||
std::tie(variableOperations, weights) | ||||
= availableOperations(m_VariablesPool, operationsPool()); | ||||
Alexandre Leroux
|
r1205 | |||
canExecute = !variableOperations.empty(); | ||||
if (canExecute) { | ||||
// Of the operations available, chooses a random operation and executes it | ||||
auto variableOperation | ||||
Alexandre Leroux
|
r1216 | = RandomGenerator::instance().randomChoice(variableOperations, weights); | ||
Alexandre Leroux
|
r1205 | |||
auto variableId = variableOperation.first; | ||||
auto variable = m_VariablesPool.at(variableId); | ||||
auto fuzzingOperation = variableOperation.second; | ||||
fuzzingOperation->execute(variable, m_VariableController, m_Properties); | ||||
Alexandre Leroux
|
r1219 | QTest::qWait(operationDelay()); | ||
Alexandre Leroux
|
r1205 | |||
// Updates variable pool with the new state of the variable after operation | ||||
m_VariablesPool[variableId] = variable; | ||||
} | ||||
else { | ||||
qCInfo(LOG_TestAmdaFuzzing()) | ||||
<< "No more operations are available, the execution of the test will stop..."; | ||||
} | ||||
} | ||||
Alexandre Leroux
|
r1199 | qCInfo(LOG_TestAmdaFuzzing()) << "Execution of the test completed."; | ||
} | ||||
Alexandre Leroux
|
r1200 | |||
private: | ||||
Alexandre Leroux
|
r1201 | int nbMaxOperations() const | ||
{ | ||||
static auto result | ||||
= m_Properties.value(NB_MAX_OPERATIONS_PROPERTY, NB_MAX_OPERATIONS_DEFAULT_VALUE) | ||||
.toInt(); | ||||
return result; | ||||
} | ||||
int nbMaxVariables() const | ||||
{ | ||||
static auto result | ||||
= m_Properties.value(NB_MAX_VARIABLES_PROPERTY, NB_MAX_VARIABLES_DEFAULT_VALUE).toInt(); | ||||
return result; | ||||
} | ||||
Alexandre Leroux
|
r1219 | int operationDelay() const | ||
{ | ||||
static auto result | ||||
= m_Properties.value(OPERATION_DELAY_PROPERTY, OPERATION_DELAY_DEFAULT_VALUE).toInt(); | ||||
return result; | ||||
} | ||||
Alexandre Leroux
|
r1216 | WeightedOperationsPool operationsPool() const | ||
Alexandre Leroux
|
r1204 | { | ||
static auto result = createOperationsPool( | ||||
m_Properties.value(AVAILABLE_OPERATIONS_PROPERTY, AVAILABLE_OPERATIONS_DEFAULT_VALUE) | ||||
Alexandre Leroux
|
r1216 | .value<WeightedOperationsTypes>()); | ||
Alexandre Leroux
|
r1204 | return result; | ||
} | ||||
Alexandre Leroux
|
r1200 | VariableController &m_VariableController; | ||
Properties m_Properties; | ||||
Alexandre Leroux
|
r1203 | VariablesPool m_VariablesPool; | ||
Alexandre Leroux
|
r1199 | }; | ||
} // namespace | ||||
class TestAmdaFuzzing : public QObject { | ||||
Q_OBJECT | ||||
private slots: | ||||
/// Input data for @sa testFuzzing() | ||||
void testFuzzing_data(); | ||||
void testFuzzing(); | ||||
}; | ||||
void TestAmdaFuzzing::testFuzzing_data() | ||||
{ | ||||
// ////////////// // | ||||
// Test structure // | ||||
// ////////////// // | ||||
Alexandre Leroux
|
r1200 | QTest::addColumn<Properties>("properties"); // Properties for random test | ||
Alexandre Leroux
|
r1199 | |||
// ////////// // | ||||
// Test cases // | ||||
// ////////// // | ||||
Alexandre Leroux
|
r1210 | auto maxRange = SqpRange::fromDateTime({2017, 1, 1}, {0, 0}, {2017, 1, 5}, {0, 0}); | ||
MetadataPool metadataPool{{{"dataType", "vector"}, {"xml:id", "imf"}}}; | ||||
// Note: we don't use auto here as we want to pass std::shared_ptr<IDataProvider> as is in the | ||||
// QVariant | ||||
std::shared_ptr<IDataProvider> provider = std::make_shared<AmdaProvider>(); | ||||
QTest::newRow("fuzzingTest") << Properties{ | ||||
{MAX_RANGE_PROPERTY, QVariant::fromValue(maxRange)}, | ||||
{METADATA_POOL_PROPERTY, QVariant::fromValue(metadataPool)}, | ||||
{PROVIDER_PROPERTY, QVariant::fromValue(provider)}}; | ||||
Alexandre Leroux
|
r1199 | } | ||
void TestAmdaFuzzing::testFuzzing() | ||||
{ | ||||
Alexandre Leroux
|
r1200 | QFETCH(Properties, properties); | ||
Alexandre Leroux
|
r1199 | auto &variableController = sqpApp->variableController(); | ||
auto &timeController = sqpApp->timeController(); | ||||
Alexandre Leroux
|
r1209 | // Generates random initial range (bounded to max range) | ||
auto maxRange = properties.value(MAX_RANGE_PROPERTY, QVariant::fromValue(INVALID_RANGE)) | ||||
.value<SqpRange>(); | ||||
QVERIFY(maxRange != INVALID_RANGE); | ||||
auto initialRangeStart | ||||
= RandomGenerator::instance().generateDouble(maxRange.m_TStart, maxRange.m_TEnd); | ||||
auto initialRangeEnd | ||||
= RandomGenerator::instance().generateDouble(maxRange.m_TStart, maxRange.m_TEnd); | ||||
if (initialRangeStart > initialRangeEnd) { | ||||
std::swap(initialRangeStart, initialRangeEnd); | ||||
} | ||||
// Sets initial range on time controller | ||||
SqpRange initialRange{initialRangeStart, initialRangeEnd}; | ||||
qCInfo(LOG_TestAmdaFuzzing()) << "Setting initial range to" << initialRange << "..."; | ||||
timeController.onTimeToUpdate(initialRange); | ||||
Alexandre Leroux
|
r1200 | FuzzingTest test{variableController, properties}; | ||
Alexandre Leroux
|
r1199 | test.execute(); | ||
} | ||||
int main(int argc, char *argv[]) | ||||
{ | ||||
QLoggingCategory::setFilterRules( | ||||
"*.warning=false\n" | ||||
"*.info=false\n" | ||||
"*.debug=false\n" | ||||
"FuzzingOperations.info=true\n" | ||||
"TestAmdaFuzzing.info=true\n"); | ||||
SqpApplication app{argc, argv}; | ||||
app.setAttribute(Qt::AA_Use96Dpi, true); | ||||
TestAmdaFuzzing testObject{}; | ||||
QTEST_SET_MAIN_SOURCE_PATH | ||||
return QTest::qExec(&testObject, argc, argv); | ||||
} | ||||
#include "TestAmdaFuzzing.moc" | ||||