| 1 |
// Copyright 2009 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.testing.PseudoRandomTest'); |
| 16 |
goog.setTestOnly('goog.testing.PseudoRandomTest'); |
| 17 |
|
| 18 |
goog.require('goog.testing.PseudoRandom'); |
| 19 |
goog.require('goog.testing.jsunit'); |
| 20 |
|
| 21 |
function runFairnessTest(sides, rolls, chiSquareLimit) { |
| 22 |
// Initialize the count table for dice rolls. |
| 23 |
var counts = []; |
| 24 |
for (var i = 0; i < sides; ++i) { |
| 25 |
counts[i] = 0; |
| 26 |
} |
| 27 |
// Roll the dice many times and count the results. |
| 28 |
for (var i = 0; i < rolls; ++i) { |
| 29 |
++counts[Math.floor(Math.random() * sides)]; |
| 30 |
} |
| 31 |
// If the dice is fair, we expect a uniform distribution. |
| 32 |
var expected = rolls / sides; |
| 33 |
// Pearson's chi-square test for a distribution fit. |
| 34 |
var chiSquare = 0; |
| 35 |
for (var i = 0; i < sides; ++i) { |
| 36 |
chiSquare += (counts[i] - expected) * (counts[i] - expected) / expected; |
| 37 |
} |
| 38 |
assert( |
| 39 |
'Chi-square test for a distribution fit failed', |
| 40 |
chiSquare < chiSquareLimit); |
| 41 |
} |
| 42 |
|
| 43 |
function testInstall() { |
| 44 |
var random = new goog.testing.PseudoRandom(); |
| 45 |
var originalRandom = Math.random; |
| 46 |
|
| 47 |
assertFalse(!!random.installed_); |
| 48 |
|
| 49 |
random.install(); |
| 50 |
assertTrue(random.installed_); |
| 51 |
assertNotEquals(Math.random, originalRandom); |
| 52 |
|
| 53 |
random.uninstall(); |
| 54 |
assertFalse(random.installed_); |
| 55 |
assertEquals(originalRandom, Math.random); |
| 56 |
} |
| 57 |
|
| 58 |
function testBounds() { |
| 59 |
var random = new goog.testing.PseudoRandom(); |
| 60 |
random.install(); |
| 61 |
|
| 62 |
for (var i = 0; i < 100000; ++i) { |
| 63 |
var value = Math.random(); |
| 64 |
assert('Random value out of bounds', value >= 0 && value < 1); |
| 65 |
} |
| 66 |
|
| 67 |
random.uninstall(); |
| 68 |
} |
| 69 |
|
| 70 |
function testFairness() { |
| 71 |
var random = new goog.testing.PseudoRandom(0, true); |
| 72 |
|
| 73 |
// Chi-square statistics: p-value = 0.05, df = 5, limit = 11.07. |
| 74 |
runFairnessTest(6, 100000, 11.07); |
| 75 |
// Chi-square statistics: p-value = 0.05, df = 100, limit = 124.34. |
| 76 |
runFairnessTest(101, 100000, 124.34); |
| 77 |
|
| 78 |
random.uninstall(); |
| 79 |
} |
| 80 |
|
| 81 |
function testReseed() { |
| 82 |
var random = new goog.testing.PseudoRandom(100, true); |
| 83 |
|
| 84 |
var sequence = []; |
| 85 |
for (var i = 0; i < 64000; ++i) { |
| 86 |
sequence.push(Math.random()); |
| 87 |
} |
| 88 |
|
| 89 |
random.seed(100); |
| 90 |
for (var i = 0; i < sequence.length; ++i) { |
| 91 |
assertEquals(sequence[i], Math.random()); |
| 92 |
} |
| 93 |
|
| 94 |
random.uninstall(); |
| 95 |
} |