diff options
author | Simon Warta <[email protected]> | 2015-07-24 00:23:02 +0200 |
---|---|---|
committer | Simon Warta <[email protected]> | 2015-07-24 10:36:57 +0200 |
commit | 81411c5b69a27f308c4921acdb52c25541ef2c73 (patch) | |
tree | 5d9b63d81164536917834b063abb5f7dcc78ec82 /src/tests | |
parent | e5e8635ab5e21991e890b229c9c563ffeec2db31 (diff) |
Fix BigInt random_integer() distribution issue.
Fixes #108
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/catchy/test_bigint.cpp | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/src/tests/catchy/test_bigint.cpp b/src/tests/catchy/test_bigint.cpp index 5f4c960cf..9bd99e919 100644 --- a/src/tests/catchy/test_bigint.cpp +++ b/src/tests/catchy/test_bigint.cpp @@ -71,4 +71,101 @@ TEST_CASE("Bigint basics", "[bigint]") } } +TEST_CASE("Bigint random_integer", "[bigint]") + { + RandomNumberGenerator *rng = RandomNumberGenerator::make_rng(); + + SECTION("min is 0") + { + // 0–9 + const size_t MIN = 0; + const size_t MAX = 10; // excluded + const int ITERATIONS = 10000; + + std::vector<int> counts(MAX, 0); + std::vector<double> ratios(MAX, 1.0); + + for (size_t i = 0; i < ITERATIONS; i++) + { + BigInt b = BigInt::random_integer(*rng, MIN, MAX); + size_t x = b.to_u32bit(); + counts[x]++; + } + + std::stringstream debug; + for (size_t d = MIN; d < MAX; ++d) + { + auto ratio = static_cast<double>(counts[d]) / ITERATIONS; + ratios[d] = ratio; + + if (!debug.str().empty()) + { + debug << ", "; + } + debug << d << ": " << std::setprecision(3) << ratio; + } + + INFO( debug.str() ) + + // Have ~ 10 % on each digit from 0-9 + CHECK(( 0.09 < ratios[0] )); CHECK(( ratios[0] < 0.11 )); + CHECK(( 0.09 < ratios[1] )); CHECK(( ratios[1] < 0.11 )); + CHECK(( 0.09 < ratios[2] )); CHECK(( ratios[2] < 0.11 )); + CHECK(( 0.09 < ratios[3] )); CHECK(( ratios[3] < 0.11 )); + CHECK(( 0.09 < ratios[4] )); CHECK(( ratios[4] < 0.11 )); + CHECK(( 0.09 < ratios[5] )); CHECK(( ratios[5] < 0.11 )); + CHECK(( 0.09 < ratios[6] )); CHECK(( ratios[6] < 0.11 )); + CHECK(( 0.09 < ratios[7] )); CHECK(( ratios[7] < 0.11 )); + CHECK(( 0.09 < ratios[8] )); CHECK(( ratios[8] < 0.11 )); + CHECK(( 0.09 < ratios[9] )); CHECK(( ratios[9] < 0.11 )); + //CHECK( false ); + } + + SECTION("min is 10") + { + // 10–19 + const size_t MIN = 10; + const size_t MAX = 20; // excluded + const size_t ITERATIONS = 10000; + + std::vector<int> counts(MAX, 0); + std::vector<double> ratios(MAX, 1.0); + + for (size_t i = 0; i < ITERATIONS; i++) + { + BigInt b = BigInt::random_integer(*rng, MIN, MAX); + size_t x = b.to_u32bit(); + counts[x]++; + } + + std::stringstream debug; + for (size_t d = MIN; d < MAX; ++d) + { + auto ratio = static_cast<double>(counts[d]) / ITERATIONS; + ratios[d] = ratio; + + if (!debug.str().empty()) + { + debug << ", "; + } + debug << d << ": " << std::setprecision(3) << ratio; + } + + INFO( debug.str() ) + + // Have ~ 10 % on each digit from 10-19 + CHECK(( 0.09 < ratios[10] )); CHECK(( ratios[10] < 0.11 )); + CHECK(( 0.09 < ratios[11] )); CHECK(( ratios[11] < 0.11 )); + CHECK(( 0.09 < ratios[12] )); CHECK(( ratios[12] < 0.11 )); + CHECK(( 0.09 < ratios[13] )); CHECK(( ratios[13] < 0.11 )); + CHECK(( 0.09 < ratios[14] )); CHECK(( ratios[14] < 0.11 )); + CHECK(( 0.09 < ratios[15] )); CHECK(( ratios[15] < 0.11 )); + CHECK(( 0.09 < ratios[16] )); CHECK(( ratios[16] < 0.11 )); + CHECK(( 0.09 < ratios[17] )); CHECK(( ratios[17] < 0.11 )); + CHECK(( 0.09 < ratios[18] )); CHECK(( ratios[18] < 0.11 )); + CHECK(( 0.09 < ratios[19] )); CHECK(( ratios[19] < 0.11 )); + //CHECK( false ); + } + } + #endif |