|
15 | 15 | #include <string> |
16 | 16 | #include <deque> |
17 | 17 | #include <algorithm> |
| 18 | +#include <limits> |
| 19 | +#include <numeric> |
18 | 20 | #include <climits> |
19 | 21 | #include <unordered_set> |
20 | 22 |
|
@@ -1160,6 +1162,57 @@ TEST_CASE("Test random", "[tools]") |
1160 | 1162 | REQUIRE(mean_value < max_threshold); |
1161 | 1163 | } |
1162 | 1164 |
|
| 1165 | + // Test GetUInt() |
| 1166 | + emp::vector<uint32_t> uint32_draws; |
| 1167 | + total = 0.0; |
| 1168 | + for (size_t i = 0; i < num_tests; i++) { |
| 1169 | + const uint32_t cur_value = rng.GetUInt(); |
| 1170 | + total += cur_value; |
| 1171 | + uint32_draws.push_back(cur_value); |
| 1172 | + } |
| 1173 | + |
| 1174 | + { |
| 1175 | + const double expected_mean = ((double)std::numeric_limits<uint32_t>::max())/2.0; |
| 1176 | + const double min_threshold = (expected_mean*0.995); |
| 1177 | + const double max_threshold = (expected_mean*1.005); |
| 1178 | + double mean_value = total/(double) num_tests; |
| 1179 | + |
| 1180 | + REQUIRE(mean_value > min_threshold); |
| 1181 | + REQUIRE(mean_value < max_threshold); |
| 1182 | + // ensure that all bits are set at least once and unset at least once |
| 1183 | + REQUIRE(std::numeric_limits<uint32_t>::max() == std::accumulate(uint32_draws.begin(),uint32_draws.end(),(uint32_t)0, |
| 1184 | + [](uint32_t accumulator, uint32_t val){ return accumulator | val; }) |
| 1185 | + ); |
| 1186 | + REQUIRE(std::numeric_limits<uint32_t>::max() == std::accumulate(uint32_draws.begin(),uint32_draws.end(),(uint32_t)0, |
| 1187 | + [](uint32_t accumulator, uint32_t val){ return accumulator | (~val); }) |
| 1188 | + ); |
| 1189 | + } |
| 1190 | + // Test GetUInt64 |
| 1191 | + emp::vector<uint64_t> uint64_draws; |
| 1192 | + total = 0.0; |
| 1193 | + for (size_t i = 0; i < num_tests; i++) { |
| 1194 | + const uint64_t cur_value = rng.GetUInt64(); |
| 1195 | + total += cur_value/(double)num_tests; |
| 1196 | + uint64_draws.push_back(cur_value); |
| 1197 | + } |
| 1198 | + |
| 1199 | + { |
| 1200 | + const double expected_mean = ((double)std::numeric_limits<uint64_t>::max())/2.0; |
| 1201 | + const double min_threshold = (expected_mean*0.995); |
| 1202 | + const double max_threshold = (expected_mean*1.005); |
| 1203 | + double mean_value = total; // values were divided by num_tests when added |
| 1204 | + |
| 1205 | + REQUIRE(mean_value > min_threshold); |
| 1206 | + REQUIRE(mean_value < max_threshold); |
| 1207 | + // ensure that all bits are set at least once and unset at least once |
| 1208 | + REQUIRE(std::numeric_limits<uint64_t>::max() == std::accumulate(uint64_draws.begin(),uint64_draws.end(),(uint64_t)0, |
| 1209 | + [](uint64_t accumulator, uint64_t val){ return accumulator | val; }) |
| 1210 | + ); |
| 1211 | + REQUIRE(std::numeric_limits<uint64_t>::max() == std::accumulate(uint64_draws.begin(),uint64_draws.end(),(uint64_t)0, |
| 1212 | + [](uint64_t accumulator, uint64_t val){ return accumulator | (~val); }) |
| 1213 | + ); |
| 1214 | + } |
| 1215 | + |
1163 | 1216 | // Test P |
1164 | 1217 | double flip_prob = 0.56789; |
1165 | 1218 | int hit_count = 0; |
|
0 commit comments