diff options
author | kcgen <kcgen@users.noreply.github.com> | 2022-08-26 23:01:54 +0300 |
---|---|---|
committer | kcgen <1557255+kcgen@users.noreply.github.com> | 2022-08-28 17:13:34 +0300 |
commit | 5504e4004ae043f844a70450479bcc1d23e2106a (patch) | |
tree | c513173f5ff6afe96298772b427fe06acf3cf150 /tests | |
parent | a8d6927d8502eaf63b670a2fb360ec19647edd89 (diff) |
Let the random number generator support float values
Diffstat (limited to 'tests')
-rw-r--r-- | tests/support_tests.cpp | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/tests/support_tests.cpp b/tests/support_tests.cpp index 7096eaf57..9e3140490 100644 --- a/tests/support_tests.cpp +++ b/tests/support_tests.cpp @@ -156,4 +156,89 @@ TEST(Support_next_uint, UnsignedInvalid) EXPECT_NE(typeid(next_uint_t<uint64_t>), typeid(uint32_t)); } +template <typename T> +void test_randomizer(const T min_value, const T max_value) +{ + // Ensure the range is valid + ASSERT_LT(min_value, max_value); + + // Calculate one quarter of the range to roughly test the distribution + // of the generated values + const auto quarter_range = (max_value - min_value) / 4; + ASSERT_GT(quarter_range, 0); + + // Calculate a value roughly 25% greater than min + const auto near_min = min_value + quarter_range; + ASSERT_GT(near_min, min_value); + + // Calculate a value oughly 25% less than max + const auto near_max = max_value - quarter_range; + ASSERT_LT(near_max, max_value); + + // State trackers of what we've found so far + bool found_near_middle = false; + bool found_near_min = false; + bool found_near_max = false; + + // Create a random value generator + const auto generate_random_value = CreateRandomizer<T>(min_value, max_value); + + constexpr auto max_iterations = 1000; + + // Start generating and testing values + for (auto i = 0; i < max_iterations; ++i) { + + const auto v = generate_random_value(); + EXPECT_GE(v, min_value); + EXPECT_LE(v, max_value); + + const auto values_span_range = found_near_middle && found_near_min && found_near_max; + if (values_span_range) { + break; + } + else if (v > near_min && v < near_max) { + found_near_middle = true; + } + else if (v < near_min) { + found_near_min = true; + } else if (v > near_max) { + found_near_max = true; + } + } + // We can only pass when values have been found near the middle, min, and max before we pass the test + ASSERT_TRUE(found_near_middle); + ASSERT_TRUE(found_near_min); + ASSERT_TRUE(found_near_max); +} + +TEST(CreateRandomizer, RangeOfLetters) +{ + // Ensure we're dealing with the standard ASCII character values + ASSERT_EQ('A', 65); + ASSERT_EQ('z', 122); + + test_randomizer('A', 'z'); +} + +TEST(CreateRandomizer, RangeOfFloats) +{ + // positive range + test_randomizer(1000.0f, 2000.0f); + + // negative range + test_randomizer(-2000.0f, -1000.0f); + + // postitive and negative range + test_randomizer(-32000.0f, 32000.0f); + + // positive percent-as-ratio + test_randomizer(0.0f, 1.0f); + + // negative percent-as-ratio + test_randomizer(-1.0f, 0.0f); + + // postitive and negative percent-as-ratio + test_randomizer(-1.0f, 1.0f); +} + } // namespace |