[M94 Dev][Tizen] Fix for errors for generating ninja files
[platform/framework/web/chromium-efl.git] / base / rand_util_unittest.cc
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/rand_util.h"
6
7 #include <stddef.h>
8 #include <stdint.h>
9
10 #include <algorithm>
11 #include <cmath>
12 #include <limits>
13 #include <memory>
14 #include <vector>
15
16 #include "base/logging.h"
17 #include "base/time/time.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 namespace base {
21
22 namespace {
23
24 const int kIntMin = std::numeric_limits<int>::min();
25 const int kIntMax = std::numeric_limits<int>::max();
26
27 }  // namespace
28
29 TEST(RandUtilTest, RandInt) {
30   EXPECT_EQ(base::RandInt(0, 0), 0);
31   EXPECT_EQ(base::RandInt(kIntMin, kIntMin), kIntMin);
32   EXPECT_EQ(base::RandInt(kIntMax, kIntMax), kIntMax);
33
34   // Check that the DCHECKS in RandInt() don't fire due to internal overflow.
35   // There was a 50% chance of that happening, so calling it 40 times means
36   // the chances of this passing by accident are tiny (9e-13).
37   for (int i = 0; i < 40; ++i)
38     base::RandInt(kIntMin, kIntMax);
39 }
40
41 TEST(RandUtilTest, RandDouble) {
42   // Force 64-bit precision, making sure we're not in a 80-bit FPU register.
43   volatile double number = base::RandDouble();
44   EXPECT_GT(1.0, number);
45   EXPECT_LE(0.0, number);
46 }
47
48 TEST(RandUtilTest, RandBytes) {
49   const size_t buffer_size = 50;
50   char buffer[buffer_size];
51   memset(buffer, 0, buffer_size);
52   base::RandBytes(buffer, buffer_size);
53   std::sort(buffer, buffer + buffer_size);
54   // Probability of occurrence of less than 25 unique bytes in 50 random bytes
55   // is below 10^-25.
56   EXPECT_GT(std::unique(buffer, buffer + buffer_size) - buffer, 25);
57 }
58
59 // Verify that calling base::RandBytes with an empty buffer doesn't fail.
60 TEST(RandUtilTest, RandBytes0) {
61   base::RandBytes(nullptr, 0);
62 }
63
64 TEST(RandUtilTest, RandBytesAsString) {
65   std::string random_string = base::RandBytesAsString(1);
66   EXPECT_EQ(1U, random_string.size());
67   random_string = base::RandBytesAsString(145);
68   EXPECT_EQ(145U, random_string.size());
69   char accumulator = 0;
70   for (auto i : random_string)
71     accumulator |= i;
72   // In theory this test can fail, but it won't before the universe dies of
73   // heat death.
74   EXPECT_NE(0, accumulator);
75 }
76
77 // Make sure that it is still appropriate to use RandGenerator in conjunction
78 // with std::random_shuffle().
79 TEST(RandUtilTest, RandGeneratorForRandomShuffle) {
80   EXPECT_EQ(base::RandGenerator(1), 0U);
81   EXPECT_LE(std::numeric_limits<ptrdiff_t>::max(),
82             std::numeric_limits<int64_t>::max());
83 }
84
85 TEST(RandUtilTest, RandGeneratorIsUniform) {
86   // Verify that RandGenerator has a uniform distribution. This is a
87   // regression test that consistently failed when RandGenerator was
88   // implemented this way:
89   //
90   //   return base::RandUint64() % max;
91   //
92   // A degenerate case for such an implementation is e.g. a top of
93   // range that is 2/3rds of the way to MAX_UINT64, in which case the
94   // bottom half of the range would be twice as likely to occur as the
95   // top half. A bit of calculus care of jar@ shows that the largest
96   // measurable delta is when the top of the range is 3/4ths of the
97   // way, so that's what we use in the test.
98   constexpr uint64_t kTopOfRange =
99       (std::numeric_limits<uint64_t>::max() / 4ULL) * 3ULL;
100   constexpr double kExpectedAverage = static_cast<double>(kTopOfRange / 2);
101   constexpr double kAllowedVariance = kExpectedAverage / 50.0;  // +/- 2%
102   constexpr int kMinAttempts = 1000;
103   constexpr int kMaxAttempts = 1000000;
104
105   double cumulative_average = 0.0;
106   int count = 0;
107   while (count < kMaxAttempts) {
108     uint64_t value = base::RandGenerator(kTopOfRange);
109     cumulative_average = (count * cumulative_average + value) / (count + 1);
110
111     // Don't quit too quickly for things to start converging, or we may have
112     // a false positive.
113     if (count > kMinAttempts &&
114         kExpectedAverage - kAllowedVariance < cumulative_average &&
115         cumulative_average < kExpectedAverage + kAllowedVariance) {
116       break;
117     }
118
119     ++count;
120   }
121
122   ASSERT_LT(count, kMaxAttempts) << "Expected average was " << kExpectedAverage
123                                  << ", average ended at " << cumulative_average;
124 }
125
126 TEST(RandUtilTest, RandUint64ProducesBothValuesOfAllBits) {
127   // This tests to see that our underlying random generator is good
128   // enough, for some value of good enough.
129   uint64_t kAllZeros = 0ULL;
130   uint64_t kAllOnes = ~kAllZeros;
131   uint64_t found_ones = kAllZeros;
132   uint64_t found_zeros = kAllOnes;
133
134   for (size_t i = 0; i < 1000; ++i) {
135     uint64_t value = base::RandUint64();
136     found_ones |= value;
137     found_zeros &= value;
138
139     if (found_zeros == kAllZeros && found_ones == kAllOnes)
140       return;
141   }
142
143   FAIL() << "Didn't achieve all bit values in maximum number of tries.";
144 }
145
146 TEST(RandUtilTest, RandBytesLonger) {
147   // Fuchsia can only retrieve 256 bytes of entropy at a time, so make sure we
148   // handle longer requests than that.
149   std::string random_string0 = base::RandBytesAsString(255);
150   EXPECT_EQ(255u, random_string0.size());
151   std::string random_string1 = base::RandBytesAsString(1023);
152   EXPECT_EQ(1023u, random_string1.size());
153   std::string random_string2 = base::RandBytesAsString(4097);
154   EXPECT_EQ(4097u, random_string2.size());
155 }
156
157 // Benchmark test for RandBytes().  Disabled since it's intentionally slow and
158 // does not test anything that isn't already tested by the existing RandBytes()
159 // tests.
160 TEST(RandUtilTest, DISABLED_RandBytesPerf) {
161   // Benchmark the performance of |kTestIterations| of RandBytes() using a
162   // buffer size of |kTestBufferSize|.
163   const int kTestIterations = 10;
164   const size_t kTestBufferSize = 1 * 1024 * 1024;
165
166   std::unique_ptr<uint8_t[]> buffer(new uint8_t[kTestBufferSize]);
167   const base::TimeTicks now = base::TimeTicks::Now();
168   for (int i = 0; i < kTestIterations; ++i)
169     base::RandBytes(buffer.get(), kTestBufferSize);
170   const base::TimeTicks end = base::TimeTicks::Now();
171
172   LOG(INFO) << "RandBytes(" << kTestBufferSize
173             << ") took: " << (end - now).InMicroseconds() << "µs";
174 }
175
176 TEST(RandUtilTest, InsecureRandomGeneratorProducesBothValuesOfAllBits) {
177   // This tests to see that our underlying random generator is good
178   // enough, for some value of good enough.
179   uint64_t kAllZeros = 0ULL;
180   uint64_t kAllOnes = ~kAllZeros;
181   uint64_t found_ones = kAllZeros;
182   uint64_t found_zeros = kAllOnes;
183
184   InsecureRandomGenerator generator;
185   generator.Seed();
186
187   for (size_t i = 0; i < 1000; ++i) {
188     uint64_t value = generator.RandUint64();
189     found_ones |= value;
190     found_zeros &= value;
191
192     if (found_zeros == kAllZeros && found_ones == kAllOnes)
193       return;
194   }
195
196   FAIL() << "Didn't achieve all bit values in maximum number of tries.";
197 }
198
199 namespace {
200
201 constexpr double kXp1Percent = -2.33;
202 constexpr double kXp99Percent = 2.33;
203
204 double ChiSquaredCriticalValue(double nu, double x_p) {
205   // From "The Art Of Computer Programming" (TAOCP), Volume 2, Section 3.3.1,
206   // Table 1. This is the asymptotic value for nu > 30, up to O(1 / sqrt(nu)).
207   return nu + sqrt(2. * nu) * x_p + 2. / 3. * (x_p * x_p) - 2. / 3.;
208 }
209
210 int ExtractBits(uint64_t value, int from_bit, int num_bits) {
211   return (value >> from_bit) & ((1 << num_bits) - 1);
212 }
213
214 // Performs a Chi-Squared test on a subset of |num_bits| extracted starting from
215 // |from_bit| in the generated value.
216 //
217 // See TAOCP, Volume 2, Section 3.3.1, and
218 // https://en.wikipedia.org/wiki/Pearson%27s_chi-squared_test for details.
219 //
220 // This is only one of the many, many random number generator test we could do,
221 // but they are cumbersome, as they are typically very slow, and expected to
222 // fail from time to time, due to their probabilistic nature.
223 //
224 // The generator we use has however been vetted with the BigCrush test suite
225 // from Marsaglia, so this should suffice as a smoke test that our
226 // implementation is wrong.
227 bool ChiSquaredTest(InsecureRandomGenerator& gen,
228                     size_t n,
229                     int from_bit,
230                     int num_bits) {
231   const int range = 1 << num_bits;
232   CHECK_EQ(static_cast<int>(n % range), 0) << "Makes computations simpler";
233   std::vector<size_t> samples(range, 0);
234
235   // Count how many samples pf each value are found. All buckets should be
236   // almost equal if the generator is suitably uniformly random.
237   for (size_t i = 0; i < n; i++) {
238     int sample = ExtractBits(gen.RandUint64(), from_bit, num_bits);
239     samples[sample] += 1;
240   }
241
242   // Compute the Chi-Squared statistic, which is:
243   // \Sum_{k=0}^{range-1} \frac{(count - expected)^2}{expected}
244   double chi_squared = 0.;
245   double expected_count = n / range;
246   for (size_t sample_count : samples) {
247     double deviation = sample_count - expected_count;
248     chi_squared += (deviation * deviation) / expected_count;
249   }
250
251   // The generator should produce numbers that are not too far of (chi_squared
252   // lower than a given quantile), but not too close to the ideal distribution
253   // either (chi_squared is too low).
254   //
255   // See The Art Of Computer Programming, Volume 2, Section 3.3.1 for details.
256   return chi_squared > ChiSquaredCriticalValue(range - 1, kXp1Percent) &&
257          chi_squared < ChiSquaredCriticalValue(range - 1, kXp99Percent);
258 }
259
260 }  // namespace
261
262 TEST(RandUtilTest, InsecureRandomGeneratorChiSquared) {
263   constexpr int kIterations = 50;
264
265   // Specifically test the low bits, which are usually weaker in random number
266   // generators. We don't use them for the 32 bit number generation, but let's
267   // make sure they are still suitable.
268   for (int start_bit : {1, 2, 3, 8, 12, 20, 32, 48, 54}) {
269     int pass_count = 0;
270     for (int i = 0; i < kIterations; i++) {
271       size_t samples = 1 << 16;
272       InsecureRandomGenerator gen;
273       // Fix the seed to make the test non-flaky.
274       gen.SeedForTesting(kIterations + 1);
275       bool pass = ChiSquaredTest(gen, samples, start_bit, 8);
276       pass_count += pass;
277     }
278
279     // We exclude 1% on each side, so we expect 98% of tests to pass, meaning 98
280     // * kIterations / 100. However this is asymptotic, so add a bit of leeway.
281     int expected_pass_count = (kIterations * 98) / 100;
282     EXPECT_GE(pass_count, expected_pass_count - ((kIterations * 2) / 100))
283         << "For start_bit = " << start_bit;
284   }
285 }
286
287 TEST(RandUtilTest, InsecureRandomGeneratorRandDouble) {
288   InsecureRandomGenerator gen;
289   gen.Seed();
290
291   for (int i = 0; i < 1000; i++) {
292     volatile double x = gen.RandDouble();
293     EXPECT_GE(x, 0.);
294     EXPECT_LT(x, 1.);
295   }
296 }
297 }  // namespace base