RandomTest.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright 2012-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <folly/Random.h>
  17. #include <algorithm>
  18. #include <random>
  19. #include <thread>
  20. #include <unordered_set>
  21. #include <vector>
  22. #include <glog/logging.h>
  23. #include <folly/portability/GTest.h>
  24. using namespace folly;
  25. TEST(Random, StateSize) {
  26. using namespace folly::detail;
  27. // uint_fast32_t is uint64_t on x86_64, w00t
  28. EXPECT_EQ(
  29. sizeof(uint_fast32_t) / 4 + 3, StateSizeT<std::minstd_rand0>::value);
  30. EXPECT_EQ(624, StateSizeT<std::mt19937>::value);
  31. #if FOLLY_HAVE_EXTRANDOM_SFMT19937
  32. EXPECT_EQ(624, StateSizeT<__gnu_cxx::sfmt19937>::value);
  33. #endif
  34. EXPECT_EQ(24, StateSizeT<std::ranlux24_base>::value);
  35. }
  36. TEST(Random, Simple) {
  37. uint32_t prev = 0, seed = 0;
  38. for (int i = 0; i < 1024; ++i) {
  39. EXPECT_NE(seed = randomNumberSeed(), prev);
  40. prev = seed;
  41. }
  42. }
  43. TEST(Random, FixedSeed) {
  44. // clang-format off
  45. struct ConstantRNG {
  46. typedef uint32_t result_type;
  47. result_type operator()() {
  48. return 4; // chosen by fair dice roll.
  49. // guaranteed to be random.
  50. }
  51. static constexpr result_type min() {
  52. return std::numeric_limits<result_type>::min();
  53. }
  54. static constexpr result_type max() {
  55. return std::numeric_limits<result_type>::max();
  56. }
  57. };
  58. // clang-format on
  59. ConstantRNG gen;
  60. // Pick a constant random number...
  61. auto value = Random::rand32(10, gen);
  62. // Loop to make sure it really is constant.
  63. for (int i = 0; i < 1024; ++i) {
  64. auto result = Random::rand32(10, gen);
  65. EXPECT_EQ(value, result);
  66. }
  67. }
  68. TEST(Random, MultiThreaded) {
  69. const int n = 100;
  70. std::vector<uint32_t> seeds(n);
  71. std::vector<std::thread> threads;
  72. for (int i = 0; i < n; ++i) {
  73. threads.push_back(
  74. std::thread([i, &seeds] { seeds[i] = randomNumberSeed(); }));
  75. }
  76. for (auto& t : threads) {
  77. t.join();
  78. }
  79. std::sort(seeds.begin(), seeds.end());
  80. for (int i = 0; i < n - 1; ++i) {
  81. EXPECT_LT(seeds[i], seeds[i + 1]);
  82. }
  83. }
  84. TEST(Random, sanity) {
  85. // edge cases
  86. EXPECT_EQ(folly::Random::rand32(0), 0);
  87. EXPECT_EQ(folly::Random::rand32(12, 12), 0);
  88. EXPECT_EQ(folly::Random::rand64(0), 0);
  89. EXPECT_EQ(folly::Random::rand64(12, 12), 0);
  90. // 32-bit repeatability, uniqueness
  91. constexpr int kTestSize = 1000;
  92. {
  93. std::vector<uint32_t> vals;
  94. folly::Random::DefaultGenerator rng;
  95. rng.seed(0xdeadbeef);
  96. for (int i = 0; i < kTestSize; ++i) {
  97. vals.push_back(folly::Random::rand32(rng));
  98. }
  99. rng.seed(0xdeadbeef);
  100. for (int i = 0; i < kTestSize; ++i) {
  101. EXPECT_EQ(vals[i], folly::Random::rand32(rng));
  102. }
  103. EXPECT_EQ(
  104. vals.size(),
  105. std::unordered_set<uint32_t>(vals.begin(), vals.end()).size());
  106. }
  107. // 64-bit repeatability, uniqueness
  108. {
  109. std::vector<uint64_t> vals;
  110. folly::Random::DefaultGenerator rng;
  111. rng.seed(0xdeadbeef);
  112. for (int i = 0; i < kTestSize; ++i) {
  113. vals.push_back(folly::Random::rand64(rng));
  114. }
  115. rng.seed(0xdeadbeef);
  116. for (int i = 0; i < kTestSize; ++i) {
  117. EXPECT_EQ(vals[i], folly::Random::rand64(rng));
  118. }
  119. EXPECT_EQ(
  120. vals.size(),
  121. std::unordered_set<uint64_t>(vals.begin(), vals.end()).size());
  122. }
  123. }
  124. #ifndef _WIN32
  125. TEST(Random, SecureFork) {
  126. // Random buffer size is 128, must be less than that.
  127. int retries = 100;
  128. while (true) {
  129. unsigned char buffer = 0;
  130. // Init random buffer
  131. folly::Random::secureRandom(&buffer, 1);
  132. auto pid = fork();
  133. EXPECT_NE(pid, -1);
  134. if (pid) {
  135. // parent
  136. int status = 0;
  137. folly::Random::secureRandom(&buffer, 1);
  138. auto pid2 = wait(&status);
  139. EXPECT_EQ(pid, pid2);
  140. // Since this *is* random data, we could randomly end up with
  141. // the same byte. Try again a few times if so before assuming
  142. // real failure.
  143. if (WEXITSTATUS(status) == buffer && retries-- > 0) {
  144. continue;
  145. }
  146. EXPECT_NE(WEXITSTATUS(status), buffer);
  147. return;
  148. } else {
  149. // child
  150. folly::Random::secureRandom(&buffer, 1);
  151. exit(buffer); // Do not print gtest results
  152. }
  153. }
  154. }
  155. #endif