TestAmdaFuzzing.cpp
305 lines
| 10.1 KiB
| text/x-c
|
CppLexer
|
r1170 | #include "FuzzingDefs.h" | ||
|
r1171 | #include "FuzzingOperations.h" | ||
|
r1175 | #include "FuzzingUtils.h" | ||
|
r1195 | #include "FuzzingValidators.h" | ||
|
r1175 | |||
|
r1179 | #include "AmdaProvider.h" | ||
|
r1168 | #include <Network/NetworkController.h> | ||
|
r1191 | #include <Settings/SqpSettingsDefs.h> | ||
|
r1168 | #include <SqpApplication.h> | ||
#include <Time/TimeController.h> | ||||
|
r1189 | #include <Variable/Variable.h> | ||
|
r1168 | #include <Variable/VariableController.h> | ||
#include <QLoggingCategory> | ||||
#include <QObject> | ||||
#include <QtTest> | ||||
#include <memory> | ||||
Q_LOGGING_CATEGORY(LOG_TestAmdaFuzzing, "TestAmdaFuzzing") | ||||
namespace { | ||||
|
r1171 | // /////// // | ||
// Aliases // | ||||
// /////// // | ||||
|
r1172 | using VariableId = int; | ||
|
r1183 | using Weight = double; | ||
using Weights = std::vector<Weight>; | ||||
|
r1172 | |||
|
r1174 | using VariableOperation = std::pair<VariableId, std::shared_ptr<IFuzzingOperation> >; | ||
using VariablesOperations = std::vector<VariableOperation>; | ||||
|
r1183 | using WeightedOperationsPool = std::map<std::shared_ptr<IFuzzingOperation>, Weight>; | ||
|
r1189 | using VariablesPool = std::map<VariableId, VariableState>; | ||
|
r1195 | using Validators = std::vector<std::shared_ptr<IFuzzingValidator> >; | ||
|
r1172 | |||
|
r1170 | // ///////// // | ||
// 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; | ||||
|
r1192 | const auto AVAILABLE_OPERATIONS_DEFAULT_VALUE = QVariant::fromValue(WeightedOperationsTypes{ | ||
{FuzzingOperationType::CREATE, 1.}, | ||||
{FuzzingOperationType::DELETE, 0.1}, // Delete operation is less frequent | ||||
{FuzzingOperationType::PAN_LEFT, 1.}, | ||||
{FuzzingOperationType::PAN_RIGHT, 1.}, | ||||
{FuzzingOperationType::ZOOM_IN, 1.}, | ||||
{FuzzingOperationType::ZOOM_OUT, 1.}}); | ||||
|
r1191 | const auto CACHE_TOLERANCE_DEFAULT_VALUE = 0.2; | ||
|
r1186 | |||
/// Delay between each operation (in ms) | ||||
const auto OPERATION_DELAY_DEFAULT_VALUE = 3000; | ||||
|
r1178 | |||
|
r1195 | /// Validators for the tests (executed in the order in which they're defined) | ||
const auto VALIDATORS_DEFAULT_VALUE = QVariant::fromValue( | ||||
ValidatorsTypes{{FuzzingValidatorType::RANGE, FuzzingValidatorType::DATA}}); | ||||
|
r1174 | // /////// // | ||
// 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) | ||||
|
r1183 | std::pair<VariablesOperations, Weights> | ||
availableOperations(const VariablesPool &variablesPool, | ||||
const WeightedOperationsPool &operationsPool) | ||||
|
r1174 | { | ||
VariablesOperations result{}; | ||||
|
r1183 | Weights weights{}; | ||
|
r1174 | |||
for (const auto &variablesPoolEntry : variablesPool) { | ||||
auto variableId = variablesPoolEntry.first; | ||||
|
r1189 | const auto &variableState = variablesPoolEntry.second; | ||
|
r1174 | |||
|
r1183 | for (const auto &operationsPoolEntry : operationsPool) { | ||
auto operation = operationsPoolEntry.first; | ||||
auto weight = operationsPoolEntry.second; | ||||
|
r1174 | // A pair is valid if the current operation can be executed on the current variable | ||
|
r1189 | if (operation->canExecute(variableState)) { | ||
|
r1174 | result.push_back({variableId, operation}); | ||
|
r1183 | weights.push_back(weight); | ||
|
r1174 | } | ||
} | ||||
} | ||||
|
r1183 | return {result, weights}; | ||
|
r1174 | } | ||
|
r1183 | WeightedOperationsPool createOperationsPool(const WeightedOperationsTypes &types) | ||
|
r1173 | { | ||
|
r1183 | WeightedOperationsPool result{}; | ||
|
r1173 | |||
|
r1183 | std::transform( | ||
types.cbegin(), types.cend(), std::inserter(result, result.end()), [](const auto &type) { | ||||
return std::make_pair(FuzzingOperationFactory::create(type.first), type.second); | ||||
}); | ||||
|
r1173 | |||
return result; | ||||
} | ||||
|
r1195 | Validators createValidators(const ValidatorsTypes &types) | ||
{ | ||||
Validators result{}; | ||||
std::transform(types.cbegin(), types.cend(), std::inserter(result, result.end()), | ||||
[](const auto &type) { return FuzzingValidatorFactory::create(type); }); | ||||
return result; | ||||
} | ||||
|
r1168 | /** | ||
* Class to run random tests | ||||
*/ | ||||
class FuzzingTest { | ||||
public: | ||||
|
r1169 | explicit FuzzingTest(VariableController &variableController, Properties properties) | ||
: m_VariableController{variableController}, | ||||
m_Properties{std::move(properties)}, | ||||
|
r1172 | m_VariablesPool{} | ||
|
r1169 | { | ||
|
r1172 | // Inits variables pool: at init, all variables are null | ||
for (auto variableId = 0; variableId < nbMaxVariables(); ++variableId) { | ||||
|
r1189 | m_VariablesPool[variableId] = VariableState{}; | ||
|
r1172 | } | ||
|
r1169 | } | ||
|
r1168 | void execute() | ||
{ | ||||
|
r1190 | qCInfo(LOG_TestAmdaFuzzing()).noquote() << "Running" << nbMaxOperations() << "operations on" | ||
<< nbMaxVariables() << "variable(s)..."; | ||||
|
r1170 | |||
|
r1174 | auto canExecute = true; | ||
for (auto i = 0; i < nbMaxOperations() && canExecute; ++i) { | ||||
// Retrieves all operations that can be executed in the current context | ||||
|
r1183 | VariablesOperations variableOperations{}; | ||
Weights weights{}; | ||||
std::tie(variableOperations, weights) | ||||
= availableOperations(m_VariablesPool, operationsPool()); | ||||
|
r1174 | |||
canExecute = !variableOperations.empty(); | ||||
if (canExecute) { | ||||
// Of the operations available, chooses a random operation and executes it | ||||
auto variableOperation | ||||
|
r1183 | = RandomGenerator::instance().randomChoice(variableOperations, weights); | ||
|
r1174 | |||
auto variableId = variableOperation.first; | ||||
|
r1189 | auto &variableState = m_VariablesPool.at(variableId); | ||
|
r1174 | auto fuzzingOperation = variableOperation.second; | ||
|
r1189 | fuzzingOperation->execute(variableState, m_VariableController, m_Properties); | ||
|
r1186 | QTest::qWait(operationDelay()); | ||
|
r1174 | } | ||
else { | ||||
|
r1190 | qCInfo(LOG_TestAmdaFuzzing()).noquote() | ||
|
r1174 | << "No more operations are available, the execution of the test will stop..."; | ||
} | ||||
} | ||||
|
r1190 | qCInfo(LOG_TestAmdaFuzzing()).noquote() << "Execution of the test completed."; | ||
|
r1168 | } | ||
|
r1169 | |||
private: | ||||
|
r1170 | 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; | ||||
} | ||||
|
r1186 | int operationDelay() const | ||
{ | ||||
static auto result | ||||
= m_Properties.value(OPERATION_DELAY_PROPERTY, OPERATION_DELAY_DEFAULT_VALUE).toInt(); | ||||
return result; | ||||
} | ||||
|
r1183 | WeightedOperationsPool operationsPool() const | ||
|
r1173 | { | ||
static auto result = createOperationsPool( | ||||
m_Properties.value(AVAILABLE_OPERATIONS_PROPERTY, AVAILABLE_OPERATIONS_DEFAULT_VALUE) | ||||
|
r1183 | .value<WeightedOperationsTypes>()); | ||
|
r1173 | return result; | ||
} | ||||
|
r1195 | Validators validators() const | ||
{ | ||||
static auto result | ||||
= createValidators(m_Properties.value(VALIDATORS_PROPERTY, VALIDATORS_DEFAULT_VALUE) | ||||
.value<ValidatorsTypes>()); | ||||
return result; | ||||
} | ||||
|
r1169 | VariableController &m_VariableController; | ||
Properties m_Properties; | ||||
|
r1172 | VariablesPool m_VariablesPool; | ||
|
r1168 | }; | ||
} // 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 // | ||||
// ////////////// // | ||||
|
r1169 | QTest::addColumn<Properties>("properties"); // Properties for random test | ||
|
r1168 | |||
// ////////// // | ||||
// Test cases // | ||||
// ////////// // | ||||
|
r1179 | 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)}}; | ||||
|
r1168 | } | ||
void TestAmdaFuzzing::testFuzzing() | ||||
{ | ||||
|
r1169 | QFETCH(Properties, properties); | ||
|
r1191 | // Sets cache property | ||
QSettings settings{}; | ||||
auto cacheTolerance = properties.value(CACHE_TOLERANCE_PROPERTY, CACHE_TOLERANCE_DEFAULT_VALUE); | ||||
settings.setValue(GENERAL_TOLERANCE_AT_INIT_KEY, cacheTolerance); | ||||
settings.setValue(GENERAL_TOLERANCE_AT_UPDATE_KEY, cacheTolerance); | ||||
|
r1168 | auto &variableController = sqpApp->variableController(); | ||
auto &timeController = sqpApp->timeController(); | ||||
|
r1178 | // 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}; | ||||
|
r1190 | qCInfo(LOG_TestAmdaFuzzing()).noquote() << "Setting initial range to" << initialRange << "..."; | ||
|
r1178 | timeController.onTimeToUpdate(initialRange); | ||
|
r1190 | properties.insert(INITIAL_RANGE_PROPERTY, QVariant::fromValue(initialRange)); | ||
|
r1178 | |||
|
r1169 | FuzzingTest test{variableController, properties}; | ||
|
r1168 | test.execute(); | ||
} | ||||
int main(int argc, char *argv[]) | ||||
{ | ||||
QLoggingCategory::setFilterRules( | ||||
"*.warning=false\n" | ||||
"*.info=false\n" | ||||
"*.debug=false\n" | ||||
"FuzzingOperations.info=true\n" | ||||
|
r1195 | "FuzzingValidators.info=true\n" | ||
|
r1168 | "TestAmdaFuzzing.info=true\n"); | ||
SqpApplication app{argc, argv}; | ||||
|
r1191 | SqpApplication::setOrganizationName("LPP"); | ||
SqpApplication::setOrganizationDomain("lpp.fr"); | ||||
SqpApplication::setApplicationName("SciQLop-TestFuzzing"); | ||||
|
r1168 | app.setAttribute(Qt::AA_Use96Dpi, true); | ||
TestAmdaFuzzing testObject{}; | ||||
QTEST_SET_MAIN_SOURCE_PATH | ||||
return QTest::qExec(&testObject, argc, argv); | ||||
} | ||||
#include "TestAmdaFuzzing.moc" | ||||