##// END OF EJS Templates
Adds utility class to get random values
Alexandre Leroux -
r1206:9f9ab053f00b
parent child
Show More
@@ -0,0 +1,25
1 #include "FuzzingUtils.h"
2
3 RandomGenerator &RandomGenerator::instance()
4 {
5 static auto instance = RandomGenerator();
6 return instance;
7 }
8
9 double RandomGenerator::generateDouble(double min, double max)
10 {
11 std::uniform_real_distribution<double> dist{min, max};
12 return dist(m_Mt);
13 }
14
15 int RandomGenerator::generateInt(int min, int max)
16 {
17 std::uniform_int_distribution<int> dist{min, max};
18 return dist(m_Mt);
19 }
20
21 RandomGenerator::RandomGenerator()
22 {
23 std::random_device rd{};
24 m_Mt = std::mt19937{rd()};
25 }
@@ -0,0 +1,41
1 #ifndef SCIQLOP_FUZZINGUTILS_H
2 #define SCIQLOP_FUZZINGUTILS_H
3
4 #include <random>
5
6 /**
7 * Class that proposes random utility methods
8 */
9 class RandomGenerator {
10 public:
11 /// @return the unique instance of the random generator
12 static RandomGenerator &instance();
13
14 /// Generates a random double between [min, max]
15 double generateDouble(double min, double max);
16 /// Generates a random int between [min, max]
17 int generateInt(int min, int max);
18
19 /// Returns a random element among the elements of a container. If the container is empty, returns an element built by default
20 template <typename T, typename ValueType = typename T::value_type>
21 ValueType randomChoice(const T& container);
22
23 private:
24 std::mt19937 m_Mt;
25
26 explicit RandomGenerator();
27 };
28
29 template<typename T, typename ValueType>
30 ValueType RandomGenerator::randomChoice(const T &container)
31 {
32 if(container.empty()){
33 return ValueType{};
34 }
35
36 auto randomIndex = generateInt(0, container.size() - 1);
37 return container.at(randomIndex);
38 }
39
40 #endif // SCIQLOP_FUZZINGUTILS
41
@@ -71,6 +71,8 tests_sources = [
71 'tests/FuzzingDefs.cpp',
71 'tests/FuzzingDefs.cpp',
72 'tests/FuzzingOperations.h',
72 'tests/FuzzingOperations.h',
73 'tests/FuzzingOperations.cpp',
73 'tests/FuzzingOperations.cpp',
74 'tests/FuzzingUtils.h',
75 'tests/FuzzingUtils.cpp'
74 ]
76 ]
75
77
76 foreach unit_test : tests
78 foreach unit_test : tests
@@ -1,5 +1,7
1 #include "FuzzingDefs.h"
1 #include "FuzzingDefs.h"
2 #include "FuzzingOperations.h"
2 #include "FuzzingOperations.h"
3 #include "FuzzingUtils.h"
4
3 #include <Network/NetworkController.h>
5 #include <Network/NetworkController.h>
4 #include <SqpApplication.h>
6 #include <SqpApplication.h>
5 #include <Time/TimeController.h>
7 #include <Time/TimeController.h>
@@ -102,7 +104,7 public:
102 if (canExecute) {
104 if (canExecute) {
103 // Of the operations available, chooses a random operation and executes it
105 // Of the operations available, chooses a random operation and executes it
104 auto variableOperation
106 auto variableOperation
105 = /* TODO: gets a random operation */;
107 = RandomGenerator::instance().randomChoice(variableOperations);
106
108
107 auto variableId = variableOperation.first;
109 auto variableId = variableOperation.first;
108 auto variable = m_VariablesPool.at(variableId);
110 auto variable = m_VariablesPool.at(variableId);
General Comments 0
You need to be logged in to leave comments. Login now