proxygen
RandomTest.cpp File Reference
#include <folly/Random.h>
#include <algorithm>
#include <random>
#include <thread>
#include <unordered_set>
#include <vector>
#include <glog/logging.h>
#include <folly/portability/GTest.h>

Go to the source code of this file.

Functions

 TEST (Random, StateSize)
 
 TEST (Random, Simple)
 
 TEST (Random, FixedSeed)
 
 TEST (Random, MultiThreaded)
 
 TEST (Random, sanity)
 
 TEST (Random, SecureFork)
 

Function Documentation

TEST ( Random  ,
StateSize   
)

Definition at line 31 of file RandomTest.cpp.

References EXPECT_EQ.

31  {
32  using namespace folly::detail;
33 
34  // uint_fast32_t is uint64_t on x86_64, w00t
35  EXPECT_EQ(
36  sizeof(uint_fast32_t) / 4 + 3, StateSizeT<std::minstd_rand0>::value);
38 #if FOLLY_HAVE_EXTRANDOM_SFMT19937
40 #endif
42 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
_t< StateSize< RNG >> StateSizeT
Definition: Random-inl.h:58
TEST ( Random  ,
Simple   
)

Definition at line 44 of file RandomTest.cpp.

References EXPECT_NE, i, folly::randomNumberSeed(), seed, and uint32_t.

44  {
45  uint32_t prev = 0, seed = 0;
46  for (int i = 0; i < 1024; ++i) {
47  EXPECT_NE(seed = randomNumberSeed(), prev);
48  prev = seed;
49  }
50 }
static const int seed
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
uint32_t randomNumberSeed()
Definition: Random.h:367
TEST ( Random  ,
FixedSeed   
)

Definition at line 52 of file RandomTest.cpp.

References EXPECT_EQ, i, max, min, folly::Random::rand32(), uint32_t, and folly::value().

52  {
53  // clang-format off
54  struct ConstantRNG {
55  typedef uint32_t result_type;
56  result_type operator()() {
57  return 4; // chosen by fair dice roll.
58  // guaranteed to be random.
59  }
60  static constexpr result_type min() {
62  }
63  static constexpr result_type max() {
65  }
66  };
67  // clang-format on
68 
69  ConstantRNG gen;
70 
71  // Pick a constant random number...
72  auto value = Random::rand32(10, gen);
73 
74  // Loop to make sure it really is constant.
75  for (int i = 0; i < 1024; ++i) {
76  auto result = Random::rand32(10, gen);
77  EXPECT_EQ(value, result);
78  }
79 }
LogLevel max
Definition: LogLevel.cpp:31
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
LogLevel min
Definition: LogLevel.cpp:30
static const char *const value
Definition: Conv.cpp:50
TEST ( Random  ,
MultiThreaded   
)

Definition at line 81 of file RandomTest.cpp.

References EXPECT_LT, i, folly::randomNumberSeed(), folly::pushmi::detail::t, and threads.

81  {
82  const int n = 100;
83  std::vector<uint32_t> seeds(n);
84  std::vector<std::thread> threads;
85  for (int i = 0; i < n; ++i) {
86  threads.push_back(
87  std::thread([i, &seeds] { seeds[i] = randomNumberSeed(); }));
88  }
89  for (auto& t : threads) {
90  t.join();
91  }
92  std::sort(seeds.begin(), seeds.end());
93  for (int i = 0; i < n - 1; ++i) {
94  EXPECT_LT(seeds[i], seeds[i + 1]);
95  }
96 }
std::vector< std::thread::id > threads
#define EXPECT_LT(val1, val2)
Definition: gtest.h:1930
uint32_t randomNumberSeed()
Definition: Random.h:367
TEST ( Random  ,
sanity   
)

Definition at line 98 of file RandomTest.cpp.

References EXPECT_EQ, i, folly::Random::rand32(), folly::Random::rand64(), rng, and folly::size().

98  {
99  // edge cases
101  EXPECT_EQ(folly::Random::rand32(12, 12), 0);
103  EXPECT_EQ(folly::Random::rand64(12, 12), 0);
104 
105  // 32-bit repeatability, uniqueness
106  constexpr int kTestSize = 1000;
107  {
108  std::vector<uint32_t> vals;
110  rng.seed(0xdeadbeef);
111  for (int i = 0; i < kTestSize; ++i) {
112  vals.push_back(folly::Random::rand32(rng));
113  }
114  rng.seed(0xdeadbeef);
115  for (int i = 0; i < kTestSize; ++i) {
116  EXPECT_EQ(vals[i], folly::Random::rand32(rng));
117  }
118  EXPECT_EQ(
119  vals.size(),
120  std::unordered_set<uint32_t>(vals.begin(), vals.end()).size());
121  }
122 
123  // 64-bit repeatability, uniqueness
124  {
125  std::vector<uint64_t> vals;
127  rng.seed(0xdeadbeef);
128  for (int i = 0; i < kTestSize; ++i) {
129  vals.push_back(folly::Random::rand64(rng));
130  }
131  rng.seed(0xdeadbeef);
132  for (int i = 0; i < kTestSize; ++i) {
133  EXPECT_EQ(vals[i], folly::Random::rand64(rng));
134  }
135  EXPECT_EQ(
136  vals.size(),
137  std::unordered_set<uint64_t>(vals.begin(), vals.end()).size());
138  }
139 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
auto rng
Definition: CollectTest.cpp:31
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
std::mt19937 DefaultGenerator
Definition: Random.h:97
static uint32_t rand32()
Definition: Random.h:213
static uint64_t rand64()
Definition: Random.h:263
TEST ( Random  ,
SecureFork   
)

Definition at line 142 of file RandomTest.cpp.

References buffer(), EXPECT_EQ, EXPECT_NE, folly::Random::secureRandom(), and folly::detail::distributed_mutex::wait().

142  {
143  // Random buffer size is 128, must be less than that.
144  int retries = 100;
145  while (true) {
146  unsigned char buffer = 0;
147  // Init random buffer
148  folly::Random::secureRandom(&buffer, 1);
149 
150  auto pid = fork();
151  EXPECT_NE(pid, -1);
152  if (pid) {
153  // parent
154  int status = 0;
155  folly::Random::secureRandom(&buffer, 1);
156  auto pid2 = wait(&status);
157  EXPECT_EQ(pid, pid2);
158  // Since this *is* random data, we could randomly end up with
159  // the same byte. Try again a few times if so before assuming
160  // real failure.
161  if (WEXITSTATUS(status) == buffer && retries-- > 0) {
162  continue;
163  }
164  EXPECT_NE(WEXITSTATUS(status), buffer);
165  return;
166  } else {
167  // child
168  folly::Random::secureRandom(&buffer, 1);
169  exit(buffer); // Do not print gtest results
170  }
171  }
172 }
std::vector< uint8_t > buffer(kBufferSize+16)
static std::enable_if< std::is_integral< T >::value &&!std::is_same< T, bool >::value, T >::type secureRandom()
Definition: Random.h:112
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
bool wait(Waiter *waiter, bool shouldSleep, Waiter *&next)
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926