From 9f9ab053f00beec138c41d32a0b8034d2745fa21 2017-12-18 14:55:22 From: Alexandre Leroux Date: 2017-12-18 14:55:22 Subject: [PATCH] Adds utility class to get random values --- diff --git a/plugins/amda/meson.build b/plugins/amda/meson.build index 51775b0..52109e4 100644 --- a/plugins/amda/meson.build +++ b/plugins/amda/meson.build @@ -71,6 +71,8 @@ tests_sources = [ 'tests/FuzzingDefs.cpp', 'tests/FuzzingOperations.h', 'tests/FuzzingOperations.cpp', + 'tests/FuzzingUtils.h', + 'tests/FuzzingUtils.cpp' ] foreach unit_test : tests 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..87ff7cf --- /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 index eba0cb7..b128eda 100644 --- a/plugins/amda/tests/TestAmdaFuzzing.cpp +++ b/plugins/amda/tests/TestAmdaFuzzing.cpp @@ -1,5 +1,7 @@ #include "FuzzingDefs.h" #include "FuzzingOperations.h" +#include "FuzzingUtils.h" + #include #include #include