From 2728fb97fd33a7a67e89ab5363427701963ddf79 2017-12-18 15:23:18 From: Alexandre Leroux Date: 2017-12-18 15:23:18 Subject: [PATCH] Merge branch 'feature/FuzingTests' into develop --- diff --git a/core/include/Data/SqpRange.h b/core/include/Data/SqpRange.h index fd7b344..9fb5cc3 100644 --- a/core/include/Data/SqpRange.h +++ b/core/include/Data/SqpRange.h @@ -14,6 +14,14 @@ * @brief The SqpRange struct holds the information of time parameters */ struct SqpRange { + /// Creates SqpRange from dates and times + static SqpRange fromDateTime(const QDate &startDate, const QTime &startTime, + const QDate &endDate, const QTime &endTime) + { + return {DateUtils::secondsSinceEpoch(QDateTime{startDate, startTime}), + DateUtils::secondsSinceEpoch(QDateTime{endDate, endTime})}; + } + /// Start time (UTC) double m_TStart; /// End time (UTC) diff --git a/plugins/amda/CMakeLists.txt b/plugins/amda/CMakeLists.txt index dcce263..ebcaf32 100644 --- a/plugins/amda/CMakeLists.txt +++ b/plugins/amda/CMakeLists.txt @@ -115,6 +115,7 @@ IF(BUILD_TESTS) ${testDirectory}/*.cpp ${testDirectory}/*.h) LIST (REMOVE_ITEM currentTestSources ${TESTS_SOURCES}) + LIST(APPEND testFilesToFormat ${currentTestSources}) # LIST (REMOVE_ITEM currentTestSources ${TESTS_HEADERS}) ADD_EXECUTABLE(${testName} ${testFile} ${currentTestSources}) diff --git a/plugins/amda/meson.build b/plugins/amda/meson.build index 7728623..52109e4 100644 --- a/plugins/amda/meson.build +++ b/plugins/amda/meson.build @@ -62,7 +62,17 @@ sciqlop_amdaplugin = library('amdaplugin', tests = [ [['tests/TestAmdaParser.cpp'],'test_amda_parser','AMDA parser test'], [['tests/TestAmdaResultParser.cpp'],'test_amda_result_parser','AMDA result parser test'], - [['tests/TestAmdaAcquisition.cpp'],'test_amda_acquisition','AMDA Acquisition test'] + [['tests/TestAmdaAcquisition.cpp'],'test_amda_acquisition','AMDA Acquisition test'], + [['tests/TestAmdaFuzzing.cpp'],'test_amda_fuzzing','AMDA fuzzing test'] +] + +tests_sources = [ + 'tests/FuzzingDefs.h', + 'tests/FuzzingDefs.cpp', + 'tests/FuzzingOperations.h', + 'tests/FuzzingOperations.cpp', + 'tests/FuzzingUtils.h', + 'tests/FuzzingUtils.cpp' ] foreach unit_test : tests @@ -71,6 +81,7 @@ foreach unit_test : tests link_with : [sciqlop_amdaplugin], include_directories : [amdaplugin_inc], cpp_args : ['-DAMDA_TESTS_RESOURCES_DIR="'+meson.current_source_dir()+'/tests-resources"'], + sources : [tests_sources], dependencies : [sciqlop_core, sciqlop_gui, qt5test]) test(unit_test[2], test_exe, args: ['-teamcity', '-o', '@0@.teamcity.txt'.format(unit_test[1])], timeout: 3 * 60) endforeach diff --git a/plugins/amda/tests/FuzzingDefs.cpp b/plugins/amda/tests/FuzzingDefs.cpp new file mode 100644 index 0000000..71e863f --- /dev/null +++ b/plugins/amda/tests/FuzzingDefs.cpp @@ -0,0 +1,8 @@ +#include "FuzzingDefs.h" + +const QString NB_MAX_OPERATIONS_PROPERTY = QStringLiteral("component"); +const QString NB_MAX_VARIABLES_PROPERTY = QStringLiteral("nbMaxVariables"); +const QString AVAILABLE_OPERATIONS_PROPERTY = QStringLiteral("availableOperations"); +const QString MAX_RANGE_PROPERTY = QStringLiteral("maxRange"); +const QString METADATA_POOL_PROPERTY = QStringLiteral("metadataPool"); +const QString PROVIDER_PROPERTY = QStringLiteral("provider"); diff --git a/plugins/amda/tests/FuzzingDefs.h b/plugins/amda/tests/FuzzingDefs.h new file mode 100644 index 0000000..8d6f092 --- /dev/null +++ b/plugins/amda/tests/FuzzingDefs.h @@ -0,0 +1,38 @@ +#ifndef SCIQLOP_FUZZINGDEFS_H +#define SCIQLOP_FUZZINGDEFS_H + +#include +#include + +// /////// // +// Aliases // +// /////// // + +using MetadataPool = std::vector; +Q_DECLARE_METATYPE(MetadataPool) + +using Properties = QVariantHash; + +// ///////// // +// Constants // +// ///////// // + +/// Max number of operations to generate +extern const QString NB_MAX_OPERATIONS_PROPERTY; + +/// Max number of variables to manipulate through operations +extern const QString NB_MAX_VARIABLES_PROPERTY; + +/// Set of operations available for the test +extern const QString AVAILABLE_OPERATIONS_PROPERTY; + +/// Max range that an operation can reach +extern const QString MAX_RANGE_PROPERTY; + +/// Set of metadata that can be associated to a variable +extern const QString METADATA_POOL_PROPERTY; + +/// Provider used to retrieve data +extern const QString PROVIDER_PROPERTY; + +#endif // SCIQLOP_FUZZINGDEFS_H diff --git a/plugins/amda/tests/FuzzingOperations.cpp b/plugins/amda/tests/FuzzingOperations.cpp new file mode 100644 index 0000000..124ae35 --- /dev/null +++ b/plugins/amda/tests/FuzzingOperations.cpp @@ -0,0 +1,74 @@ +#include "FuzzingOperations.h" +#include "FuzzingUtils.h" + +#include + +#include +#include + +#include + +Q_LOGGING_CATEGORY(LOG_FuzzingOperations, "FuzzingOperations") + +namespace { + +struct CreateOperation : public IFuzzingOperation { + bool canExecute(std::shared_ptr variable) const override + { + // A variable can be created only if it doesn't exist yet + return variable == nullptr; + } + + void execute(std::shared_ptr &variable, VariableController &variableController, + const Properties &properties) const override + { + // Retrieves metadata pool from properties, and choose one of the metadata entries to + // associate it with the variable + auto metaDataPool = properties.value(METADATA_POOL_PROPERTY).value(); + auto variableMetadata = RandomGenerator::instance().randomChoice(metaDataPool); + + // Retrieves provider + auto variableProvider + = properties.value(PROVIDER_PROPERTY).value >(); + + auto variableName = QString{"Var_%1"}.arg(QUuid::createUuid().toString()); + qCInfo(LOG_FuzzingOperations()) + << "Creating variable" << variableName << "(metadata:" << variableMetadata << ")"; + + auto newVariable + = variableController.createVariable(variableName, variableMetadata, variableProvider); + std::swap(variable, newVariable); + } +}; + +struct UnknownOperation : public IFuzzingOperation { + bool canExecute(std::shared_ptr variable) const override + { + Q_UNUSED(variable); + return false; + } + + void execute(std::shared_ptr &variable, VariableController &variableController, + const Properties &properties) const override + { + Q_UNUSED(variable); + Q_UNUSED(variableController); + Q_UNUSED(properties); + // Does nothing + } +}; + +} // namespace + +std::unique_ptr FuzzingOperationFactory::create(FuzzingOperationType type) +{ + switch (type) { + case FuzzingOperationType::CREATE: + return std::make_unique(); + default: + // Default case returns unknown operation + break; + } + + return std::make_unique(); +} diff --git a/plugins/amda/tests/FuzzingOperations.h b/plugins/amda/tests/FuzzingOperations.h new file mode 100644 index 0000000..cbc3dc0 --- /dev/null +++ b/plugins/amda/tests/FuzzingOperations.h @@ -0,0 +1,49 @@ +#ifndef SCIQLOP_FUZZINGOPERATIONS_H +#define SCIQLOP_FUZZINGOPERATIONS_H + +#include "FuzzingDefs.h" + +#include +#include + +#include +#include + +Q_DECLARE_LOGGING_CATEGORY(LOG_FuzzingOperations) + +class Variable; +class VariableController; + +/** + * Enumeration of types of existing fuzzing operations + */ +enum class FuzzingOperationType { CREATE }; + +/// Interface that represents an operation that can be executed during a fuzzing test +struct IFuzzingOperation { + virtual ~IFuzzingOperation() noexcept = default; + + /// Checks if the operation can be executed according to the current state of the variable + /// passed in parameter + virtual bool canExecute(std::shared_ptr variable) const = 0; + /// Executes the operation on the variable passed in parameter + /// @param variable the variable on which to execute the operation + /// @param variableController the controller associated to the operation + /// @param properties properties that can be used to configure the operation + /// @remarks variable is passed as a reference because, according to the operation, it can be + /// modified (in/out parameter) + virtual void execute(std::shared_ptr &variable, + VariableController &variableController, + const Properties &properties = {}) const = 0; +}; + +/// Factory of @sa IFuzzingOperation +struct FuzzingOperationFactory { + /// Creates a fuzzing operation from a type + static std::unique_ptr create(FuzzingOperationType type); +}; + +using OperationsTypes = std::set; +Q_DECLARE_METATYPE(OperationsTypes) + +#endif // SCIQLOP_FUZZINGOPERATIONS_H diff --git a/plugins/amda/tests/FuzzingUtils.cpp b/plugins/amda/tests/FuzzingUtils.cpp new file mode 100644 index 0000000..6cf89af --- /dev/null +++ b/plugins/amda/tests/FuzzingUtils.cpp @@ -0,0 +1,25 @@ +#include "FuzzingUtils.h" + +RandomGenerator &RandomGenerator::instance() +{ + static auto instance = RandomGenerator(); + return instance; +} + +double RandomGenerator::generateDouble(double min, double max) +{ + std::uniform_real_distribution dist{min, max}; + return dist(m_Mt); +} + +int RandomGenerator::generateInt(int min, int max) +{ + std::uniform_int_distribution dist{min, max}; + return dist(m_Mt); +} + +RandomGenerator::RandomGenerator() +{ + std::random_device rd{}; + m_Mt = std::mt19937{rd()}; +} diff --git a/plugins/amda/tests/FuzzingUtils.h b/plugins/amda/tests/FuzzingUtils.h new file mode 100644 index 0000000..adfe293 --- /dev/null +++ b/plugins/amda/tests/FuzzingUtils.h @@ -0,0 +1,41 @@ +#ifndef SCIQLOP_FUZZINGUTILS_H +#define SCIQLOP_FUZZINGUTILS_H + +#include + +/** + * Class that proposes random utility methods + */ +class RandomGenerator { +public: + /// @return the unique instance of the random generator + static RandomGenerator &instance(); + + /// Generates a random double between [min, max] + double generateDouble(double min, double max); + /// Generates a random int between [min, max] + int generateInt(int min, int max); + + /// Returns a random element among the elements of a container. If the container is empty, + /// returns an element built by default + template + ValueType randomChoice(const T &container); + +private: + std::mt19937 m_Mt; + + explicit RandomGenerator(); +}; + +template +ValueType RandomGenerator::randomChoice(const T &container) +{ + if (container.empty()) { + return ValueType{}; + } + + auto randomIndex = generateInt(0, container.size() - 1); + return container.at(randomIndex); +} + +#endif // SCIQLOP_FUZZINGUTILS diff --git a/plugins/amda/tests/TestAmdaFuzzing.cpp b/plugins/amda/tests/TestAmdaFuzzing.cpp new file mode 100644 index 0000000..41ace89 --- /dev/null +++ b/plugins/amda/tests/TestAmdaFuzzing.cpp @@ -0,0 +1,241 @@ +#include "FuzzingDefs.h" +#include "FuzzingOperations.h" +#include "FuzzingUtils.h" + +#include "AmdaProvider.h" + +#include +#include +#include