OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 10 matching lines...) Expand all Loading... |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 #include "src/v8.h" | 28 #include "src/v8.h" |
29 #include "test/cctest/cctest.h" | 29 #include "test/cctest/cctest.h" |
30 | 30 |
| 31 #include "src/base/utils/random-number-generator.h" |
31 #include "src/isolate-inl.h" | 32 #include "src/isolate-inl.h" |
32 #include "src/utils/random-number-generator.h" | |
33 | 33 |
34 using namespace v8::internal; | 34 using namespace v8::internal; |
35 | 35 |
36 | 36 |
37 static const int kMaxRuns = 12345; | 37 static const int kMaxRuns = 12345; |
38 static const int kRandomSeeds[] = { | 38 static const int kRandomSeeds[] = { |
39 -1, 1, 42, 100, 1234567890, 987654321 | 39 -1, 1, 42, 100, 1234567890, 987654321 |
40 }; | 40 }; |
41 | 41 |
42 | 42 |
43 TEST(NextIntWithMaxValue) { | 43 TEST(NextIntWithMaxValue) { |
44 for (unsigned n = 0; n < ARRAY_SIZE(kRandomSeeds); ++n) { | 44 for (unsigned n = 0; n < ARRAY_SIZE(kRandomSeeds); ++n) { |
45 RandomNumberGenerator rng(kRandomSeeds[n]); | 45 v8::base::RandomNumberGenerator rng(kRandomSeeds[n]); |
46 for (int max = 1; max <= kMaxRuns; ++max) { | 46 for (int max = 1; max <= kMaxRuns; ++max) { |
47 int n = rng.NextInt(max); | 47 int n = rng.NextInt(max); |
48 CHECK_LE(0, n); | 48 CHECK_LE(0, n); |
49 CHECK_LT(n, max); | 49 CHECK_LT(n, max); |
50 } | 50 } |
51 } | 51 } |
52 } | 52 } |
53 | 53 |
54 | 54 |
55 TEST(NextBoolReturnsBooleanValue) { | 55 TEST(NextBoolReturnsBooleanValue) { |
56 for (unsigned n = 0; n < ARRAY_SIZE(kRandomSeeds); ++n) { | 56 for (unsigned n = 0; n < ARRAY_SIZE(kRandomSeeds); ++n) { |
57 RandomNumberGenerator rng(kRandomSeeds[n]); | 57 v8::base::RandomNumberGenerator rng(kRandomSeeds[n]); |
58 for (int k = 0; k < kMaxRuns; ++k) { | 58 for (int k = 0; k < kMaxRuns; ++k) { |
59 bool b = rng.NextBool(); | 59 bool b = rng.NextBool(); |
60 CHECK(b == false || b == true); | 60 CHECK(b == false || b == true); |
61 } | 61 } |
62 } | 62 } |
63 } | 63 } |
64 | 64 |
65 | 65 |
66 TEST(NextDoubleRange) { | 66 TEST(NextDoubleRange) { |
67 for (unsigned n = 0; n < ARRAY_SIZE(kRandomSeeds); ++n) { | 67 for (unsigned n = 0; n < ARRAY_SIZE(kRandomSeeds); ++n) { |
68 RandomNumberGenerator rng(kRandomSeeds[n]); | 68 v8::base::RandomNumberGenerator rng(kRandomSeeds[n]); |
69 for (int k = 0; k < kMaxRuns; ++k) { | 69 for (int k = 0; k < kMaxRuns; ++k) { |
70 double d = rng.NextDouble(); | 70 double d = rng.NextDouble(); |
71 CHECK_LE(0.0, d); | 71 CHECK_LE(0.0, d); |
72 CHECK_LT(d, 1.0); | 72 CHECK_LT(d, 1.0); |
73 } | 73 } |
74 } | 74 } |
75 } | 75 } |
76 | 76 |
77 | 77 |
78 TEST(RandomSeedFlagIsUsed) { | 78 TEST(RandomSeedFlagIsUsed) { |
79 for (unsigned n = 0; n < ARRAY_SIZE(kRandomSeeds); ++n) { | 79 for (unsigned n = 0; n < ARRAY_SIZE(kRandomSeeds); ++n) { |
80 FLAG_random_seed = kRandomSeeds[n]; | 80 FLAG_random_seed = kRandomSeeds[n]; |
81 v8::Isolate* i = v8::Isolate::New(); | 81 v8::Isolate* i = v8::Isolate::New(); |
82 RandomNumberGenerator& rng1 = | 82 v8::base::RandomNumberGenerator& rng1 = |
83 *reinterpret_cast<Isolate*>(i)->random_number_generator(); | 83 *reinterpret_cast<Isolate*>(i)->random_number_generator(); |
84 RandomNumberGenerator rng2(kRandomSeeds[n]); | 84 v8::base::RandomNumberGenerator rng2(kRandomSeeds[n]); |
85 for (int k = 1; k <= kMaxRuns; ++k) { | 85 for (int k = 1; k <= kMaxRuns; ++k) { |
86 int64_t i1, i2; | 86 int64_t i1, i2; |
87 rng1.NextBytes(&i1, sizeof(i1)); | 87 rng1.NextBytes(&i1, sizeof(i1)); |
88 rng2.NextBytes(&i2, sizeof(i2)); | 88 rng2.NextBytes(&i2, sizeof(i2)); |
89 CHECK_EQ(i2, i1); | 89 CHECK_EQ(i2, i1); |
90 CHECK_EQ(rng2.NextInt(), rng1.NextInt()); | 90 CHECK_EQ(rng2.NextInt(), rng1.NextInt()); |
91 CHECK_EQ(rng2.NextInt(k), rng1.NextInt(k)); | 91 CHECK_EQ(rng2.NextInt(k), rng1.NextInt(k)); |
92 CHECK_EQ(rng2.NextDouble(), rng1.NextDouble()); | 92 CHECK_EQ(rng2.NextDouble(), rng1.NextDouble()); |
93 } | 93 } |
94 i->Dispose(); | 94 i->Dispose(); |
95 } | 95 } |
96 } | 96 } |
OLD | NEW |