v8  3.25.30(node0.11.13)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test-random-number-generator.cc
Go to the documentation of this file.
1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 #include "v8.h"
29 
30 #include "cctest.h"
32 
33 using namespace v8::internal;
34 
35 
36 static const int kMaxRuns = 12345;
37 static const int kRandomSeeds[] = {
38  -1, 1, 42, 100, 1234567890, 987654321
39 };
40 
41 
42 TEST(NextIntWithMaxValue) {
43  for (unsigned n = 0; n < ARRAY_SIZE(kRandomSeeds); ++n) {
44  RandomNumberGenerator rng(kRandomSeeds[n]);
45  for (int max = 1; max <= kMaxRuns; ++max) {
46  int n = rng.NextInt(max);
47  CHECK_LE(0, n);
48  CHECK_LT(n, max);
49  }
50  }
51 }
52 
53 
54 TEST(NextBoolReturnsBooleanValue) {
55  for (unsigned n = 0; n < ARRAY_SIZE(kRandomSeeds); ++n) {
56  RandomNumberGenerator rng(kRandomSeeds[n]);
57  for (int k = 0; k < kMaxRuns; ++k) {
58  bool b = rng.NextBool();
59  CHECK(b == false || b == true);
60  }
61  }
62 }
63 
64 
65 TEST(NextDoubleRange) {
66  for (unsigned n = 0; n < ARRAY_SIZE(kRandomSeeds); ++n) {
67  RandomNumberGenerator rng(kRandomSeeds[n]);
68  for (int k = 0; k < kMaxRuns; ++k) {
69  double d = rng.NextDouble();
70  CHECK_LE(0.0, d);
71  CHECK_LT(d, 1.0);
72  }
73  }
74 }
75 
76 
77 TEST(RandomSeedFlagIsUsed) {
78  for (unsigned n = 0; n < ARRAY_SIZE(kRandomSeeds); ++n) {
79  FLAG_random_seed = kRandomSeeds[n];
80  RandomNumberGenerator rng1;
81  RandomNumberGenerator rng2(kRandomSeeds[n]);
82  for (int k = 1; k <= kMaxRuns; ++k) {
83  int64_t i1, i2;
84  rng1.NextBytes(&i1, sizeof(i1));
85  rng2.NextBytes(&i2, sizeof(i2));
86  CHECK_EQ(i2, i1);
87  CHECK_EQ(rng2.NextInt(), rng1.NextInt());
88  CHECK_EQ(rng2.NextInt(k), rng1.NextInt(k));
89  CHECK_EQ(rng2.NextDouble(), rng1.NextDouble());
90  }
91  }
92 }
#define CHECK_EQ(expected, value)
Definition: checks.h:252
#define CHECK_LT(a, b)
Definition: checks.h:262
#define CHECK(condition)
Definition: checks.h:75
#define CHECK_LE(a, b)
Definition: checks.h:263
#define ARRAY_SIZE(a)
Definition: globals.h:333