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
10namespace v8 {
11namespace base {
12
13class RandomNumberGeneratorTest : public ::testing::TestWithParam<int> {};
14
15
16static const int kMaxRuns = 12345;
17
18
19TEST_P(RandomNumberGeneratorTest, NextIntWithMaxValue) {
20  RandomNumberGenerator rng(GetParam());
21  for (int max = 1; max <= kMaxRuns; ++max) {
22    int n = rng.NextInt(max);
23    EXPECT_LE(0, n);
24    EXPECT_LT(n, max);
25  }
26}
27
28
29TEST_P(RandomNumberGeneratorTest, NextBooleanReturnsFalseOrTrue) {
30  RandomNumberGenerator rng(GetParam());
31  for (int k = 0; k < kMaxRuns; ++k) {
32    bool b = rng.NextBool();
33    EXPECT_TRUE(b == false || b == true);
34  }
35}
36
37
38TEST_P(RandomNumberGeneratorTest, NextDoubleReturnsValueBetween0And1) {
39  RandomNumberGenerator rng(GetParam());
40  for (int k = 0; k < kMaxRuns; ++k) {
41    double d = rng.NextDouble();
42    EXPECT_LE(0.0, d);
43    EXPECT_LT(d, 1.0);
44  }
45}
46
47
48INSTANTIATE_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}  // namespace v8
54