random_test.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. //
  6. // Copyright (c) 2012 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. #include <cstring>
  10. #include <vector>
  11. #include "test_util/testharness.h"
  12. #include "util/random.h"
  13. using ROCKSDB_NAMESPACE::Random;
  14. TEST(RandomTest, Uniform) {
  15. const int average = 20;
  16. for (uint32_t seed : {0, 1, 2, 37, 4096}) {
  17. Random r(seed);
  18. for (int range : {1, 2, 8, 12, 100}) {
  19. std::vector<int> counts(range, 0);
  20. for (int i = 0; i < range * average; ++i) {
  21. ++counts.at(r.Uniform(range));
  22. }
  23. int max_variance = static_cast<int>(std::sqrt(range) * 2 + 4);
  24. for (int i = 0; i < range; ++i) {
  25. EXPECT_GE(counts[i], std::max(1, average - max_variance));
  26. EXPECT_LE(counts[i], average + max_variance + 1);
  27. }
  28. }
  29. }
  30. }
  31. TEST(RandomTest, OneIn) {
  32. Random r(42);
  33. for (int range : {1, 2, 8, 12, 100, 1234}) {
  34. const int average = 100;
  35. int count = 0;
  36. for (int i = 0; i < average * range; ++i) {
  37. if (r.OneIn(range)) {
  38. ++count;
  39. }
  40. }
  41. if (range == 1) {
  42. EXPECT_EQ(count, average);
  43. } else {
  44. int max_variance = static_cast<int>(std::sqrt(average) * 1.5);
  45. EXPECT_GE(count, average - max_variance);
  46. EXPECT_LE(count, average + max_variance);
  47. }
  48. }
  49. }
  50. TEST(RandomTest, OneInOpt) {
  51. Random r(42);
  52. for (int range : {-12, 0, 1, 2, 8, 12, 100, 1234}) {
  53. const int average = 100;
  54. int count = 0;
  55. for (int i = 0; i < average * range; ++i) {
  56. if (r.OneInOpt(range)) {
  57. ++count;
  58. }
  59. }
  60. if (range < 1) {
  61. EXPECT_EQ(count, 0);
  62. } else if (range == 1) {
  63. EXPECT_EQ(count, average);
  64. } else {
  65. int max_variance = static_cast<int>(std::sqrt(average) * 1.5);
  66. EXPECT_GE(count, average - max_variance);
  67. EXPECT_LE(count, average + max_variance);
  68. }
  69. }
  70. }
  71. TEST(RandomTest, PercentTrue) {
  72. Random r(42);
  73. for (int pct : {-12, 0, 1, 2, 10, 50, 90, 98, 99, 100, 1234}) {
  74. const int samples = 10000;
  75. int count = 0;
  76. for (int i = 0; i < samples; ++i) {
  77. if (r.PercentTrue(pct)) {
  78. ++count;
  79. }
  80. }
  81. if (pct <= 0) {
  82. EXPECT_EQ(count, 0);
  83. } else if (pct >= 100) {
  84. EXPECT_EQ(count, samples);
  85. } else {
  86. int est = (count * 100 + (samples / 2)) / samples;
  87. EXPECT_EQ(est, pct);
  88. }
  89. }
  90. }
  91. int main(int argc, char** argv) {
  92. ::testing::InitGoogleTest(&argc, argv);
  93. return RUN_ALL_TESTS();
  94. }