-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathrandom.cpp
More file actions
61 lines (50 loc) · 1.76 KB
/
Copy pathrandom.cpp
File metadata and controls
61 lines (50 loc) · 1.76 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
#include <rsl/random.hpp>
#include <catch2/catch_approx.hpp>
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_string.hpp>
#include <thread>
namespace {
auto const* const rng = &rsl::rng({0, 1});
}
TEST_CASE("rsl::rng") {
SECTION("Repeated calls in thread yield same object") {
CHECK(rng == &rsl::rng());
CHECK(rng == &rsl::rng());
CHECK(rng == &rsl::rng());
}
SECTION("Calls from separate threads yield separate objects") {
// Test using randomly generated seed
auto thread1 = std::thread([] { CHECK(rng != &rsl::rng()); });
// Test that custom seed in separate thread does not throw
auto thread2 = std::thread([] { CHECK(rng != &rsl::rng({2, 3})); });
thread1.join();
thread2.join();
}
SECTION("Throw if seeded after first call") {
CHECK_NOTHROW(rsl::rng({}));
CHECK_THROWS_WITH(rsl::rng({1, 2, 3, 4}), Catch::Matchers::ContainsSubstring(
"rng cannot be re-seeded on this thread"));
}
}
TEST_CASE("rsl::uniform_real") {
constexpr auto lower = -100.;
constexpr auto upper = 100.;
for (int i = 0; i < 1'000; ++i) {
auto const value = rsl::uniform_real(lower, upper);
CHECK(value >= lower);
CHECK(value < upper);
}
}
TEST_CASE("rsl::uniform_int") {
constexpr auto lower = -100;
constexpr auto upper = 100;
for (int i = 0; i < 1'000; ++i) {
auto const value = rsl::uniform_int(lower, upper);
CHECK(value >= lower);
CHECK(value <= upper);
}
}
TEST_CASE("rsl::random_unit_quaternion") {
for (int i = 0; i < 1'000; ++i)
CHECK(rsl::random_unit_quaternion().norm() == Catch::Approx(1.).epsilon(0).margin(1e-6));
}