/*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* limitations under the License.
*/
-#include "folly/Random.h"
-#include "folly/Range.h"
-#include "folly/Benchmark.h"
-#include "folly/Foreach.h"
-
-#include <glog/logging.h>
-#include <gtest/gtest.h>
+#include <folly/Random.h>
#include <algorithm>
+#include <random>
#include <thread>
+#include <unordered_set>
#include <vector>
-#include <random>
+
+#include <glog/logging.h>
+
+#include <folly/portability/GTest.h>
using namespace folly;
using namespace folly::detail;
// uint_fast32_t is uint64_t on x86_64, w00t
- EXPECT_EQ(sizeof(uint_fast32_t) / 4 + 3,
- StateSize<std::minstd_rand0>::value);
- EXPECT_EQ(624, StateSize<std::mt19937>::value);
-#if FOLLY_USE_SIMD_PRNG
- EXPECT_EQ(624, StateSize<__gnu_cxx::sfmt19937>::value);
+ EXPECT_EQ(
+ sizeof(uint_fast32_t) / 4 + 3, StateSizeT<std::minstd_rand0>::value);
+ EXPECT_EQ(624, StateSizeT<std::mt19937>::value);
+#if FOLLY_HAVE_EXTRANDOM_SFMT19937
+ EXPECT_EQ(624, StateSizeT<__gnu_cxx::sfmt19937>::value);
#endif
- EXPECT_EQ(24, StateSize<std::ranlux24_base>::value);
+ EXPECT_EQ(24, StateSizeT<std::ranlux24_base>::value);
}
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.
+ }
+ static constexpr result_type min() {
+ return std::numeric_limits<result_type>::min();
+ }
+ static constexpr result_type max() {
+ return std::numeric_limits<result_type>::max();
+ }
+ };
+ // clang-format on
+
+ ConstantRNG gen;
+
+ // Pick a constant random number...
+ auto value = Random::rand32(10, gen);
+
+ // Loop to make sure it really is constant.
+ for (int i = 0; i < 1024; ++i) {
+ auto result = Random::rand32(10, gen);
+ EXPECT_EQ(value, result);
+ }
+}
+
TEST(Random, MultiThreaded) {
- const int n = 1024;
+ const int n = 100;
std::vector<uint32_t> seeds(n);
std::vector<std::thread> threads;
for (int i = 0; i < n; ++i) {
}
}
-BENCHMARK(minstdrand, n) {
- BenchmarkSuspender braces;
- std::random_device rd;
- std::minstd_rand rng(rd());
-
- braces.dismiss();
-
- FOR_EACH_RANGE (i, 0, n) {
- doNotOptimizeAway(rng());
- }
-}
-
-BENCHMARK(mt19937, n) {
- BenchmarkSuspender braces;
- std::random_device rd;
- std::mt19937 rng(rd());
-
- braces.dismiss();
-
- FOR_EACH_RANGE (i, 0, n) {
- doNotOptimizeAway(rng());
+TEST(Random, sanity) {
+ // edge cases
+ EXPECT_EQ(folly::Random::rand32(0), 0);
+ EXPECT_EQ(folly::Random::rand32(12, 12), 0);
+ EXPECT_EQ(folly::Random::rand64(0), 0);
+ EXPECT_EQ(folly::Random::rand64(12, 12), 0);
+
+ // 32-bit repeatability, uniqueness
+ constexpr int kTestSize = 1000;
+ {
+ std::vector<uint32_t> vals;
+ folly::Random::DefaultGenerator rng;
+ rng.seed(0xdeadbeef);
+ for (int i = 0; i < kTestSize; ++i) {
+ vals.push_back(folly::Random::rand32(rng));
+ }
+ rng.seed(0xdeadbeef);
+ for (int i = 0; i < kTestSize; ++i) {
+ EXPECT_EQ(vals[i], folly::Random::rand32(rng));
+ }
+ EXPECT_EQ(
+ vals.size(),
+ std::unordered_set<uint32_t>(vals.begin(), vals.end()).size());
}
-}
-
-BENCHMARK(threadprng, n) {
- BenchmarkSuspender braces;
- ThreadLocalPRNG tprng;
- tprng();
-
- braces.dismiss();
-
- FOR_EACH_RANGE (i, 0, n) {
- doNotOptimizeAway(tprng());
- }
-}
-
-BENCHMARK(RandomDouble) { doNotOptimizeAway(Random::randDouble01()); }
-BENCHMARK(Random32) { doNotOptimizeAway(Random::rand32()); }
-BENCHMARK(Random32Num) { doNotOptimizeAway(Random::rand32(100)); }
-BENCHMARK(Random64) { doNotOptimizeAway(Random::rand64()); }
-BENCHMARK(Random64Num) { doNotOptimizeAway(Random::rand64(100ul << 32)); }
-BENCHMARK(Random64OneIn) { doNotOptimizeAway(Random::oneIn(100)); }
-
-int main(int argc, char** argv) {
- testing::InitGoogleTest(&argc, argv);
- google::ParseCommandLineFlags(&argc, &argv, true);
- if (FLAGS_benchmark) {
- folly::runBenchmarks();
+ // 64-bit repeatability, uniqueness
+ {
+ std::vector<uint64_t> vals;
+ folly::Random::DefaultGenerator rng;
+ rng.seed(0xdeadbeef);
+ for (int i = 0; i < kTestSize; ++i) {
+ vals.push_back(folly::Random::rand64(rng));
+ }
+ rng.seed(0xdeadbeef);
+ for (int i = 0; i < kTestSize; ++i) {
+ EXPECT_EQ(vals[i], folly::Random::rand64(rng));
+ }
+ EXPECT_EQ(
+ vals.size(),
+ std::unordered_set<uint64_t>(vals.begin(), vals.end()).size());
}
- return RUN_ALL_TESTS();
}