From ea04bef9c90c1ebebb92e233f9ff9f2b3a300bbd 2017-12-19 14:14:48 From: Alexandre Leroux Date: 2017-12-19 14:14:48 Subject: [PATCH] Passes FuzzingState and variable id instead VariableState to the methods of operations --- diff --git a/plugins/amda/tests/FuzzingOperations.cpp b/plugins/amda/tests/FuzzingOperations.cpp index 66de737..7f74f91 100644 --- a/plugins/amda/tests/FuzzingOperations.cpp +++ b/plugins/amda/tests/FuzzingOperations.cpp @@ -15,13 +15,14 @@ Q_LOGGING_CATEGORY(LOG_FuzzingOperations, "FuzzingOperations") namespace { struct CreateOperation : public IFuzzingOperation { - bool canExecute(const VariableState &variableState) const override + bool canExecute(VariableId variableId, const FuzzingState &fuzzingState) const override { // A variable can be created only if it doesn't exist yet - return variableState.m_Variable == nullptr; + return fuzzingState.variableState(variableId).m_Variable == nullptr; } - void execute(VariableState &variableState, VariableController &variableController, + void execute(VariableId variableId, FuzzingState &fuzzingState, + VariableController &variableController, const Properties &properties) const override { // Retrieves metadata pool from properties, and choose one of the metadata entries to @@ -41,22 +42,23 @@ struct CreateOperation : public IFuzzingOperation { = variableController.createVariable(variableName, variableMetadata, variableProvider); // Updates variable's state + auto &variableState = fuzzingState.variableState(variableId); variableState.m_Range = properties.value(INITIAL_RANGE_PROPERTY).value(); std::swap(variableState.m_Variable, newVariable); } }; struct DeleteOperation : public IFuzzingOperation { - bool canExecute(const VariableState &variableState) const override + bool canExecute(VariableId variableId, const FuzzingState &fuzzingState) const override { // A variable can be delete only if it exists - return variableState.m_Variable != nullptr; + return fuzzingState.variableState(variableId).m_Variable != nullptr; } - void execute(VariableState &variableState, VariableController &variableController, - const Properties &properties) const override + void execute(VariableId variableId, FuzzingState &fuzzingState, + VariableController &variableController, const Properties &) const override { - Q_UNUSED(properties); + auto &variableState = fuzzingState.variableState(variableId); qCInfo(LOG_FuzzingOperations()).noquote() << "Deleting variable" << variableState.m_Variable->name() << "..."; @@ -100,14 +102,16 @@ struct MoveOperation : public IFuzzingOperation { { } - bool canExecute(const VariableState &variableState) const override + bool canExecute(VariableId variableId, const FuzzingState &fuzzingState) const override { - return variableState.m_Variable != nullptr; + return fuzzingState.variableState(variableId).m_Variable != nullptr; } - void execute(VariableState &variableState, VariableController &variableController, + void execute(VariableId variableId, FuzzingState &fuzzingState, + VariableController &variableController, const Properties &properties) const override { + auto &variableState = fuzzingState.variableState(variableId); auto variable = variableState.m_Variable; // Gets the max range defined @@ -146,19 +150,11 @@ struct MoveOperation : public IFuzzingOperation { }; struct UnknownOperation : public IFuzzingOperation { - bool canExecute(const VariableState &variableState) const override - { - Q_UNUSED(variableState); - return false; - } + bool canExecute(VariableId, const FuzzingState &) const override { return false; } - void execute(VariableState &variableState, VariableController &variableController, - const Properties &properties) const override + void execute(VariableId, FuzzingState &, VariableController &, + const Properties &) const override { - Q_UNUSED(variableState); - Q_UNUSED(variableController); - Q_UNUSED(properties); - // Does nothing } }; diff --git a/plugins/amda/tests/FuzzingOperations.h b/plugins/amda/tests/FuzzingOperations.h index cc472ff..c0ee0f4 100644 --- a/plugins/amda/tests/FuzzingOperations.h +++ b/plugins/amda/tests/FuzzingOperations.h @@ -22,17 +22,19 @@ enum class FuzzingOperationType { CREATE, DELETE, PAN_LEFT, PAN_RIGHT, ZOOM_IN, struct IFuzzingOperation { virtual ~IFuzzingOperation() noexcept = default; - /// Checks if the operation can be executed according to the current variable state passed in - /// parameter - virtual bool canExecute(const VariableState &variableState) const = 0; - /// Executes the operation on the variable state passed in parameter - /// @param variableState the variable state on which to execute the operation + /// Checks if the operation can be executed according to the current test's state for the + /// variable passed in parameter + virtual bool canExecute(VariableId variableId, const FuzzingState &fuzzingState) const = 0; + /// Executes the operation on the variable for which its identifier is passed in parameter + /// @param variableId the variable identifier + /// @param fuzzingState the current test's state on which to find the variable and execute the + /// operation /// @param variableController the controller associated to the operation /// @param properties properties that can be used to configure the operation - /// @remarks variableState is passed as a reference because, according to the operation, it can - /// be - /// modified (in/out parameter) - virtual void execute(VariableState &variableState, VariableController &variableController, + /// @remarks fuzzingState is passed as a reference because, according to the operation, it can + /// be modified (in/out parameter) + virtual void execute(VariableId variableId, FuzzingState &fuzzingState, + VariableController &variableController, const Properties &properties = {}) const = 0; }; diff --git a/plugins/amda/tests/TestAmdaFuzzing.cpp b/plugins/amda/tests/TestAmdaFuzzing.cpp index cab88a4..4e751da 100644 --- a/plugins/amda/tests/TestAmdaFuzzing.cpp +++ b/plugins/amda/tests/TestAmdaFuzzing.cpp @@ -26,7 +26,6 @@ namespace { // Aliases // // /////// // -using VariableId = int; using Weight = double; using Weights = std::vector; @@ -34,7 +33,6 @@ using VariableOperation = std::pair; using WeightedOperationsPool = std::map, Weight>; -using VariablesPool = std::map; using Validators = std::vector >; // ///////// // @@ -67,22 +65,20 @@ const auto VALIDATORS_DEFAULT_VALUE = QVariant::fromValue( /// 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) std::pair -availableOperations(const VariablesPool &variablesPool, - const WeightedOperationsPool &operationsPool) +availableOperations(const FuzzingState &fuzzingState, const WeightedOperationsPool &operationsPool) { VariablesOperations result{}; Weights weights{}; - for (const auto &variablesPoolEntry : variablesPool) { + for (const auto &variablesPoolEntry : fuzzingState.m_VariablesPool) { auto variableId = variablesPoolEntry.first; - const auto &variableState = variablesPoolEntry.second; for (const auto &operationsPoolEntry : operationsPool) { auto operation = operationsPoolEntry.first; auto weight = operationsPoolEntry.second; // A pair is valid if the current operation can be executed on the current variable - if (operation->canExecute(variableState)) { + if (operation->canExecute(variableId, fuzzingState)) { result.push_back({variableId, operation}); weights.push_back(weight); } @@ -146,11 +142,11 @@ public: explicit FuzzingTest(VariableController &variableController, Properties properties) : m_VariableController{variableController}, m_Properties{std::move(properties)}, - m_VariablesPool{} + m_FuzzingState{} { // Inits variables pool: at init, all variables are null for (auto variableId = 0; variableId < nbMaxVariables(); ++variableId) { - m_VariablesPool[variableId] = VariableState{}; + m_FuzzingState.m_VariablesPool[variableId] = VariableState{}; } } @@ -165,7 +161,7 @@ public: VariablesOperations variableOperations{}; Weights weights{}; std::tie(variableOperations, weights) - = availableOperations(m_VariablesPool, operationsPool()); + = availableOperations(m_FuzzingState, operationsPool()); canExecute = !variableOperations.empty(); if (canExecute) { @@ -174,14 +170,14 @@ public: = RandomGenerator::instance().randomChoice(variableOperations, weights); auto variableId = variableOperation.first; - auto &variableState = m_VariablesPool.at(variableId); auto fuzzingOperation = variableOperation.second; - fuzzingOperation->execute(variableState, m_VariableController, m_Properties); + fuzzingOperation->execute(variableId, m_FuzzingState, m_VariableController, + m_Properties); QTest::qWait(operationDelay()); // Validates variables - validate(m_VariablesPool, validators()); + validate(m_FuzzingState.m_VariablesPool, validators()); } else { qCInfo(LOG_TestAmdaFuzzing()).noquote() @@ -233,7 +229,7 @@ private: VariableController &m_VariableController; Properties m_Properties; - VariablesPool m_VariablesPool; + FuzzingState m_FuzzingState; }; } // namespace