##// END OF EJS Templates
Adds utility class to get random values
Adds utility class to get random values

File last commit:

r1175:9f9ab053f00b
r1175:9f9ab053f00b
Show More
TestAmdaFuzzing.cpp
209 lines | 6.1 KiB | text/x-c | CppLexer
/ plugins / amda / tests / TestAmdaFuzzing.cpp
Alexandre Leroux
Adds "number of operations" and "number of variables" properties for the tests
r1170 #include "FuzzingDefs.h"
Alexandre Leroux
Defines fuzzing operations...
r1171 #include "FuzzingOperations.h"
Alexandre Leroux
Adds utility class to get random values
r1175 #include "FuzzingUtils.h"
Alexandre Leroux
Inits test structure
r1168 #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
Defines fuzzing operations...
r1171 // /////// //
// Aliases //
// /////// //
Alexandre Leroux
Defines variable pool...
r1172 using VariableId = int;
Alexandre Leroux
Implements test execute() method...
r1174 using VariableOperation = std::pair<VariableId, std::shared_ptr<IFuzzingOperation> >;
using VariablesOperations = std::vector<VariableOperation>;
Alexandre Leroux
Defines fuzzing operations...
r1171 using OperationsPool = std::set<std::shared_ptr<IFuzzingOperation> >;
Alexandre Leroux
Defines variable pool...
r1172 using VariablesPool = std::map<VariableId, std::shared_ptr<Variable> >;
Alexandre Leroux
Adds "number of operations" and "number of variables" properties for the tests
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;
Alexandre Leroux
Defines operations pool...
r1173 const auto AVAILABLE_OPERATIONS_DEFAULT_VALUE
= QVariant::fromValue(OperationsTypes{FuzzingOperationType::CREATE});
Alexandre Leroux
Implements test execute() method...
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)
VariablesOperations availableOperations(const VariablesPool &variablesPool,
const OperationsPool &operationsPool)
{
VariablesOperations result{};
for (const auto &variablesPoolEntry : variablesPool) {
auto variableId = variablesPoolEntry.first;
auto variable = variablesPoolEntry.second;
for (const auto &operation : operationsPool) {
// A pair is valid if the current operation can be executed on the current variable
if (operation->canExecute(variable)) {
result.push_back({variableId, operation});
}
}
}
return result;
}
Alexandre Leroux
Defines operations pool...
r1173 OperationsPool createOperationsPool(const OperationsTypes &types)
{
OperationsPool result{};
std::transform(types.cbegin(), types.cend(), std::inserter(result, result.end()),
[](const auto &type) { return FuzzingOperationFactory::create(type); });
return result;
}
Alexandre Leroux
Inits test structure
r1168 /**
* Class to run random tests
*/
class FuzzingTest {
public:
Alexandre Leroux
Adds variable controller and properties to test structure...
r1169 explicit FuzzingTest(VariableController &variableController, Properties properties)
: m_VariableController{variableController},
m_Properties{std::move(properties)},
Alexandre Leroux
Defines variable pool...
r1172 m_VariablesPool{}
Alexandre Leroux
Adds variable controller and properties to test structure...
r1169 {
Alexandre Leroux
Defines variable pool...
r1172 // Inits variables pool: at init, all variables are null
for (auto variableId = 0; variableId < nbMaxVariables(); ++variableId) {
m_VariablesPool[variableId] = nullptr;
}
Alexandre Leroux
Adds variable controller and properties to test structure...
r1169 }
Alexandre Leroux
Inits test structure
r1168 void execute()
{
Alexandre Leroux
Adds "number of operations" and "number of variables" properties for the tests
r1170 qCInfo(LOG_TestAmdaFuzzing()) << "Running" << nbMaxOperations() << "operations on"
<< nbMaxVariables() << "variables...";
Alexandre Leroux
Implements test execute() method...
r1174 auto canExecute = true;
for (auto i = 0; i < nbMaxOperations() && canExecute; ++i) {
// Retrieves all operations that can be executed in the current context
auto variableOperations = availableOperations(m_VariablesPool, operationsPool());
canExecute = !variableOperations.empty();
if (canExecute) {
// Of the operations available, chooses a random operation and executes it
auto variableOperation
Alexandre Leroux
Adds utility class to get random values
r1175 = RandomGenerator::instance().randomChoice(variableOperations);
Alexandre Leroux
Implements test execute() method...
r1174
auto variableId = variableOperation.first;
auto variable = m_VariablesPool.at(variableId);
auto fuzzingOperation = variableOperation.second;
fuzzingOperation->execute(variable, m_VariableController, m_Properties);
// 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
Inits test structure
r1168 qCInfo(LOG_TestAmdaFuzzing()) << "Execution of the test completed.";
}
Alexandre Leroux
Adds variable controller and properties to test structure...
r1169
private:
Alexandre Leroux
Adds "number of operations" and "number of variables" properties for the tests
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;
}
Alexandre Leroux
Defines operations pool...
r1173 OperationsPool operationsPool() const
{
static auto result = createOperationsPool(
m_Properties.value(AVAILABLE_OPERATIONS_PROPERTY, AVAILABLE_OPERATIONS_DEFAULT_VALUE)
.value<OperationsTypes>());
return result;
}
Alexandre Leroux
Adds variable controller and properties to test structure...
r1169 VariableController &m_VariableController;
Properties m_Properties;
Alexandre Leroux
Defines variable pool...
r1172 VariablesPool m_VariablesPool;
Alexandre Leroux
Inits test structure
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 //
// ////////////// //
Alexandre Leroux
Adds variable controller and properties to test structure...
r1169 QTest::addColumn<Properties>("properties"); // Properties for random test
Alexandre Leroux
Inits test structure
r1168
// ////////// //
// Test cases //
// ////////// //
///@todo: complete
}
void TestAmdaFuzzing::testFuzzing()
{
Alexandre Leroux
Adds variable controller and properties to test structure...
r1169 QFETCH(Properties, properties);
Alexandre Leroux
Inits test structure
r1168 auto &variableController = sqpApp->variableController();
auto &timeController = sqpApp->timeController();
Alexandre Leroux
Adds variable controller and properties to test structure...
r1169 FuzzingTest test{variableController, properties};
Alexandre Leroux
Inits test structure
r1168 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"