Correctly deduce RNG type in folly::Random
[folly.git] / folly / test / RandomTest.cpp
index 2f3c97d5cc03180e185f986886fb69c5354d91de..69447872efdd0b5e23166025c6b15fda56483941 100644 (file)
@@ -47,6 +47,33 @@ TEST(Random, Simple) {
   }
 }
 
+TEST(Random, FixedSeed) {
+  // clang-format off
+  struct ConstantRNG {
+    typedef uint32_t result_type;
+    result_type operator()() {
+      return 4; // chosen by fair dice roll.
+                // guaranteed to be random.
+    }
+    result_type min() {
+      return std::numeric_limits<result_type>::min();
+    }
+    result_type max() {
+      return std::numeric_limits<result_type>::max();
+    }
+  };
+  // clang-format on
+
+  ConstantRNG gen;
+  // Loop to make sure it really is constant.
+  for (int i = 0; i < 1024; ++i) {
+    auto result = Random::rand32(10, gen);
+    // TODO: This is a little bit brittle; standard library changes could break
+    // it, if it starts implementing distribution types differently.
+    EXPECT_EQ(0, result);
+  }
+}
+
 TEST(Random, MultiThreaded) {
   const int n = 100;
   std::vector<uint32_t> seeds(n);