pool_urbg_test.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "absl/random/internal/pool_urbg.h"
  15. #include <algorithm>
  16. #include <bitset>
  17. #include <cmath>
  18. #include <cstdint>
  19. #include <iterator>
  20. #include "gtest/gtest.h"
  21. #include "absl/meta/type_traits.h"
  22. #include "absl/types/span.h"
  23. using absl::random_internal::PoolURBG;
  24. using absl::random_internal::RandenPool;
  25. namespace {
  26. // is_randen_pool trait is true when parameterized by an RandenPool
  27. template <typename T>
  28. using is_randen_pool = typename absl::disjunction< //
  29. std::is_same<T, RandenPool<uint8_t>>, //
  30. std::is_same<T, RandenPool<uint16_t>>, //
  31. std::is_same<T, RandenPool<uint32_t>>, //
  32. std::is_same<T, RandenPool<uint64_t>>>; //
  33. // MyFill either calls RandenPool::Fill() or std::generate(..., rng)
  34. template <typename T, typename V>
  35. typename absl::enable_if_t<absl::negation<is_randen_pool<T>>::value, void> //
  36. MyFill(T& rng, absl::Span<V> data) { // NOLINT(runtime/references)
  37. std::generate(std::begin(data), std::end(data), rng);
  38. }
  39. template <typename T, typename V>
  40. typename absl::enable_if_t<is_randen_pool<T>::value, void> //
  41. MyFill(T& rng, absl::Span<V> data) { // NOLINT(runtime/references)
  42. rng.Fill(data);
  43. }
  44. template <typename EngineType>
  45. class PoolURBGTypedTest : public ::testing::Test {};
  46. using EngineTypes = ::testing::Types< //
  47. RandenPool<uint8_t>, //
  48. RandenPool<uint16_t>, //
  49. RandenPool<uint32_t>, //
  50. RandenPool<uint64_t>, //
  51. PoolURBG<uint8_t, 2>, //
  52. PoolURBG<uint16_t, 2>, //
  53. PoolURBG<uint32_t, 2>, //
  54. PoolURBG<uint64_t, 2>, //
  55. PoolURBG<unsigned int, 8>, // NOLINT(runtime/int)
  56. PoolURBG<unsigned long, 8>, // NOLINT(runtime/int)
  57. PoolURBG<unsigned long int, 4>, // NOLINT(runtime/int)
  58. PoolURBG<unsigned long long, 4>>; // NOLINT(runtime/int)
  59. TYPED_TEST_SUITE(PoolURBGTypedTest, EngineTypes);
  60. // This test is checks that the engines meet the URBG interface requirements
  61. // defined in [rand.req.urbg].
  62. TYPED_TEST(PoolURBGTypedTest, URBGInterface) {
  63. using E = TypeParam;
  64. using T = typename E::result_type;
  65. static_assert(std::is_copy_constructible<E>::value,
  66. "engine must be copy constructible");
  67. static_assert(absl::is_copy_assignable<E>::value,
  68. "engine must be copy assignable");
  69. E e;
  70. const E x;
  71. e();
  72. static_assert(std::is_same<decltype(e()), T>::value,
  73. "return type of operator() must be result_type");
  74. E u0(x);
  75. u0();
  76. E u1 = e;
  77. u1();
  78. }
  79. // This validates that sequences are independent.
  80. TYPED_TEST(PoolURBGTypedTest, VerifySequences) {
  81. using E = TypeParam;
  82. using result_type = typename E::result_type;
  83. E rng;
  84. (void)rng(); // Discard one value.
  85. constexpr int kNumOutputs = 64;
  86. result_type a[kNumOutputs];
  87. result_type b[kNumOutputs];
  88. std::fill(std::begin(b), std::end(b), 0);
  89. // Fill a using Fill or generate, depending on the engine type.
  90. {
  91. E x = rng;
  92. MyFill(x, absl::MakeSpan(a));
  93. }
  94. // Fill b using std::generate().
  95. {
  96. E x = rng;
  97. std::generate(std::begin(b), std::end(b), x);
  98. }
  99. // Test that generated sequence changed as sequence of bits, i.e. if about
  100. // half of the bites were flipped between two non-correlated values.
  101. size_t changed_bits = 0;
  102. size_t unchanged_bits = 0;
  103. size_t total_set = 0;
  104. size_t total_bits = 0;
  105. size_t equal_count = 0;
  106. for (size_t i = 0; i < kNumOutputs; ++i) {
  107. equal_count += (a[i] == b[i]) ? 1 : 0;
  108. std::bitset<sizeof(result_type) * 8> bitset(a[i] ^ b[i]);
  109. changed_bits += bitset.count();
  110. unchanged_bits += bitset.size() - bitset.count();
  111. std::bitset<sizeof(result_type) * 8> a_set(a[i]);
  112. std::bitset<sizeof(result_type) * 8> b_set(b[i]);
  113. total_set += a_set.count() + b_set.count();
  114. total_bits += 2 * 8 * sizeof(result_type);
  115. }
  116. // On average, half the bits are changed between two calls.
  117. EXPECT_LE(changed_bits, 0.60 * (changed_bits + unchanged_bits));
  118. EXPECT_GE(changed_bits, 0.40 * (changed_bits + unchanged_bits));
  119. // verify using a quick normal-approximation to the binomial.
  120. EXPECT_NEAR(total_set, total_bits * 0.5, 4 * std::sqrt(total_bits))
  121. << "@" << total_set / static_cast<double>(total_bits);
  122. // Also, A[i] == B[i] with probability (1/range) * N.
  123. // Give this a pretty wide latitude, though.
  124. const double kExpected = kNumOutputs / (1.0 * sizeof(result_type) * 8);
  125. EXPECT_LE(equal_count, 1.0 + kExpected);
  126. }
  127. } // namespace
  128. /*
  129. $ nanobenchmarks 1 RandenPool construct
  130. $ nanobenchmarks 1 PoolURBG construct
  131. RandenPool<uint32_t> | 1 | 1000 | 48482.00 ticks | 48.48 ticks | 13.9 ns
  132. RandenPool<uint32_t> | 10 | 2000 | 1028795.00 ticks | 51.44 ticks | 14.7 ns
  133. RandenPool<uint32_t> | 100 | 1000 | 5119968.00 ticks | 51.20 ticks | 14.6 ns
  134. RandenPool<uint32_t> | 1000 | 500 | 25867936.00 ticks | 51.74 ticks | 14.8 ns
  135. RandenPool<uint64_t> | 1 | 1000 | 49921.00 ticks | 49.92 ticks | 14.3 ns
  136. RandenPool<uint64_t> | 10 | 2000 | 1208269.00 ticks | 60.41 ticks | 17.3 ns
  137. RandenPool<uint64_t> | 100 | 1000 | 5844955.00 ticks | 58.45 ticks | 16.7 ns
  138. RandenPool<uint64_t> | 1000 | 500 | 28767404.00 ticks | 57.53 ticks | 16.4 ns
  139. PoolURBG<uint32_t,8> | 1 | 1000 | 86431.00 ticks | 86.43 ticks | 24.7 ns
  140. PoolURBG<uint32_t,8> | 10 | 1000 | 206191.00 ticks | 20.62 ticks | 5.9 ns
  141. PoolURBG<uint32_t,8> | 100 | 1000 | 1516049.00 ticks | 15.16 ticks | 4.3 ns
  142. PoolURBG<uint32_t,8> | 1000 | 500 | 7613936.00 ticks | 15.23 ticks | 4.4 ns
  143. PoolURBG<uint64_t,4> | 1 | 1000 | 96668.00 ticks | 96.67 ticks | 27.6 ns
  144. PoolURBG<uint64_t,4> | 10 | 1000 | 282423.00 ticks | 28.24 ticks | 8.1 ns
  145. PoolURBG<uint64_t,4> | 100 | 1000 | 2609587.00 ticks | 26.10 ticks | 7.5 ns
  146. PoolURBG<uint64_t,4> | 1000 | 500 | 12408757.00 ticks | 24.82 ticks | 7.1 ns
  147. */