forked from citizenfx/fivem
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestUtils.cpp
More file actions
59 lines (52 loc) · 1.41 KB
/
TestUtils.cpp
File metadata and controls
59 lines (52 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "TestUtils.h"
#include <random>
#include <catch_amalgamated.hpp>
uint64_t fx::TestUtils::u64Random(const uint64_t range)
{
std::uniform_int_distribution<uint64_t> dist(0, range);
return dist(getRandomEngine());
}
std::string fx::TestUtils::asciiRandom(const uint64_t count)
{
std::string val;
val.resize(count);
std::uniform_int_distribution<uint64_t> dist(0, 61);
for (uint64_t i = 0; i < count; ++i)
{
const uint64_t generatedValue = dist(getRandomEngine());
// 'a' - 'z'
if (generatedValue < 26)
{
val[i] = static_cast<char>('a' + generatedValue);
continue;
}
// 'A' - 'Z'
if (generatedValue < 52)
{
val[i] = static_cast<char>('A' + generatedValue - 26);
continue;
}
// '0' - '9'
if (generatedValue < 62)
{
val[i] = static_cast<char>('0' + generatedValue - 52);
}
}
return val;
}
void fx::TestUtils::fillVectorU8Random(std::vector<uint8_t>& data)
{
std::uniform_int_distribution<uint64_t> dist(0, 255);
for (uint64_t i = 0, length = data.size(); i < length; ++i)
{
data[i] = static_cast<uint8_t>(dist(getRandomEngine()));
}
}
std::default_random_engine& fx::TestUtils::getRandomEngine()
{
// seed sequence needs to be generated after catch is initialized and started the first test
static std::seed_seq ssq{Catch::getSeed()};
// the random engine is reused after it got created with the catch seed sequence
static std::default_random_engine eng{ssq};
return eng;
}