Line data Source code
1 : // Copyright 2014 the V8 project 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 <climits>
6 :
7 : #include "src/base/utils/random-number-generator.h"
8 : #include "testing/gtest/include/gtest/gtest.h"
9 :
10 : namespace v8 {
11 : namespace base {
12 :
13 54 : class RandomNumberGeneratorTest : public ::testing::TestWithParam<int> {};
14 :
15 :
16 : static const int kMaxRuns = 12345;
17 :
18 :
19 15822 : TEST_P(RandomNumberGeneratorTest, NextIntWithMaxValue) {
20 9 : RandomNumberGenerator rng(GetParam());
21 111114 : for (int max = 1; max <= kMaxRuns; ++max) {
22 111105 : int n = rng.NextInt(max);
23 111105 : EXPECT_LE(0, n);
24 111105 : EXPECT_LT(n, max);
25 : }
26 9 : }
27 :
28 :
29 15822 : TEST_P(RandomNumberGeneratorTest, NextBooleanReturnsFalseOrTrue) {
30 9 : RandomNumberGenerator rng(GetParam());
31 111114 : for (int k = 0; k < kMaxRuns; ++k) {
32 : bool b = rng.NextBool();
33 : EXPECT_TRUE(b == false || b == true);
34 : }
35 9 : }
36 :
37 :
38 15822 : TEST_P(RandomNumberGeneratorTest, NextDoubleReturnsValueBetween0And1) {
39 9 : RandomNumberGenerator rng(GetParam());
40 111114 : for (int k = 0; k < kMaxRuns; ++k) {
41 111105 : double d = rng.NextDouble();
42 111105 : EXPECT_LE(0.0, d);
43 111105 : EXPECT_LT(d, 1.0);
44 : }
45 9 : }
46 :
47 :
48 99978 : INSTANTIATE_TEST_CASE_P(RandomSeeds, RandomNumberGeneratorTest,
49 : ::testing::Values(INT_MIN, -1, 0, 1, 42, 100,
50 : 1234567890, 987654321, INT_MAX));
51 :
52 : } // namespace base
53 7893 : } // namespace v8
|