-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_random.cpp
More file actions
144 lines (112 loc) · 4.41 KB
/
test_random.cpp
File metadata and controls
144 lines (112 loc) · 4.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "test_settings.hpp"
#ifdef ENABLE_TESTS
#include "../utils_lib/external/doctest.hpp"
#include "../utils_lib/utils_random.hpp"
#include "../utils_lib/utils_string.hpp"
#include "../utils_lib/utils_algorithm.hpp"
#include <algorithm>
#include <vector>
TEST_CASE("Test utils::random::pick_x_from") {
const std::vector<int> vec{1,2,3,4,5,6,7,8,9,10};
const std::vector<int> empty{};
auto s1 = utils::random::pick_x_from(10, empty);
CHECK(s1.size() == 0);
s1 = utils::random::pick_x_from(0, vec);
CHECK(s1.size() == 0);
s1 = utils::random::pick_x_from(10, vec);
CHECK(s1.size() == 10);
CHECK(utils::algorithm::within(1, 10, s1));
s1 = utils::random::sample(5, vec);
CHECK(s1.size() == 5);
CHECK(utils::algorithm::within(1, 10, s1));
s1 = utils::random::pick_x_from(3, vec);
CHECK(s1.size() == 3);
CHECK(utils::algorithm::within(1, 10, s1));
}
TEST_CASE("Test utils::random::sample") {
const std::vector<int> vec{1,2,3,4,5,6,7,8,9,10};
const std::vector<int> empty{};
auto s1 = utils::random::sample(10, empty);
CHECK(s1.size() == 0);
s1 = utils::random::sample(0, vec);
CHECK(s1.size() == 0);
s1 = utils::random::sample(10, vec);
CHECK(vec == s1);
s1 = utils::random::sample(5, vec);
CHECK(s1.size() == 5);
CHECK(utils::algorithm::within(1, 10, s1));
CHECK(utils::algorithm::is_ascending(s1));
s1 = utils::random::sample(3, vec);
CHECK(s1.size() == 3);
CHECK(utils::algorithm::within(1, 10, s1));
CHECK(utils::algorithm::is_ascending(s1));
}
TEST_CASE("Test utils::random::generate_x") {
auto cont = utils::random::generate_x<int>(0);
CHECK(cont.size() == 0);
cont = utils::random::generate_x<int>(10);
CHECK(cont.size() == 10);
CHECK(utils::algorithm::within(std::numeric_limits<int>::min(), std::numeric_limits<int>::max(), cont));
cont = utils::random::generate_x<int>(10, -100, 100);
CHECK(cont.size() == 10);
CHECK(utils::algorithm::within(-100, 100, cont));
cont = utils::random::generate_x<int>(10, 100, -100);
CHECK(cont.size() == 10);
CHECK(utils::algorithm::within(-100, 100, cont));
auto cont2 = utils::random::generate_x<bool>(10);
CHECK(cont2.size() == 10);
CHECK(utils::algorithm::within(false, true, cont2));
auto cont3 = utils::random::generate_x<uint8_t>(10);
CHECK(cont3.size() == 10);
CHECK(utils::algorithm::within(0x00, 0xFF, cont3));
auto cont4 = utils::random::generate_x<float>(10, -5.221f, 85.0144f);
CHECK(cont4.size() == 10);
CHECK(utils::algorithm::within(-5.221f, 85.0144f, cont4));
}
TEST_CASE("Test utils::random::generate_string") {
auto str = utils::random::generate_string<char>(0);
CHECK(str.size() == 0);
str = utils::random::generate_string<char>(100);
CHECK(str.size() == 100);
CHECK(utils::algorithm::within(std::numeric_limits<char>::min(), std::numeric_limits<char>::max(), str));
}
TEST_CASE("Test utils::random::generate_safe_string") {
auto str = utils::random::generate_safe_string<char>(0);
CHECK(str.size() == 0);
str = utils::random::generate_safe_string(100);
CHECK(str.size() == 100);
CHECK(std::all_of(str.begin(), str.end(), [](const char c){
return utils::string::contains(utils::string::_base64_chars, c);
}));
}
TEST_CASE("Test utils::random::generate_bool") {
auto cont = utils::random::generate_bool(0);
CHECK(cont.size() == 0);
cont = utils::random::generate_bool(10, 0.0);
CHECK(cont.size() == 10);
CHECK(std::all_of(cont.begin(), cont.end(), [](const bool x){
return x == false;
}));
cont = utils::random::generate_bool(10, 1.0);
CHECK(cont.size() == 10);
CHECK(std::all_of(cont.begin(), cont.end(), [](const bool x){
return x == true;
}));
cont = utils::random::generate_bool(10);
CHECK(cont.size() == 10);
CHECK(utils::algorithm::contains(cont, false).has_value());
CHECK(utils::algorithm::contains(cont, true).has_value());
}
TEST_CASE("Test utils::random::generate_uuid") {
const std::string guid = utils::random::generate_uuid();
REQUIRE(guid.size() == 36);
REQUIRE(guid != utils::random::generate_uuid());
const auto parts = utils::string::split(guid, '-');
REQUIRE(parts.size() == 5);
CHECK(parts[0].size() == 8);
CHECK(parts[1].size() == 4);
CHECK(parts[2].size() == 4);
CHECK(parts[3].size() == 4);
CHECK(parts[4].size() == 12);
}
#endif