pseudorandom_test.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. goog.provide('goog.testing.PseudoRandomTest');
  15. goog.setTestOnly('goog.testing.PseudoRandomTest');
  16. goog.require('goog.testing.PseudoRandom');
  17. goog.require('goog.testing.jsunit');
  18. function runFairnessTest(sides, rolls, chiSquareLimit) {
  19. // Initialize the count table for dice rolls.
  20. var counts = [];
  21. for (var i = 0; i < sides; ++i) {
  22. counts[i] = 0;
  23. }
  24. // Roll the dice many times and count the results.
  25. for (var i = 0; i < rolls; ++i) {
  26. ++counts[Math.floor(Math.random() * sides)];
  27. }
  28. // If the dice is fair, we expect a uniform distribution.
  29. var expected = rolls / sides;
  30. // Pearson's chi-square test for a distribution fit.
  31. var chiSquare = 0;
  32. for (var i = 0; i < sides; ++i) {
  33. chiSquare += (counts[i] - expected) * (counts[i] - expected) / expected;
  34. }
  35. assert(
  36. 'Chi-square test for a distribution fit failed',
  37. chiSquare < chiSquareLimit);
  38. }
  39. function testInstall() {
  40. var random = new goog.testing.PseudoRandom();
  41. var originalRandom = Math.random;
  42. assertFalse(!!random.installed_);
  43. random.install();
  44. assertTrue(random.installed_);
  45. assertNotEquals(Math.random, originalRandom);
  46. random.uninstall();
  47. assertFalse(random.installed_);
  48. assertEquals(originalRandom, Math.random);
  49. }
  50. function testBounds() {
  51. var random = new goog.testing.PseudoRandom();
  52. random.install();
  53. for (var i = 0; i < 100000; ++i) {
  54. var value = Math.random();
  55. assert('Random value out of bounds', value >= 0 && value < 1);
  56. }
  57. random.uninstall();
  58. }
  59. function testFairness() {
  60. var random = new goog.testing.PseudoRandom(0, true);
  61. // Chi-square statistics: p-value = 0.05, df = 5, limit = 11.07.
  62. runFairnessTest(6, 100000, 11.07);
  63. // Chi-square statistics: p-value = 0.05, df = 100, limit = 124.34.
  64. runFairnessTest(101, 100000, 124.34);
  65. random.uninstall();
  66. }
  67. function testReseed() {
  68. var random = new goog.testing.PseudoRandom(100, true);
  69. var sequence = [];
  70. for (var i = 0; i < 64000; ++i) {
  71. sequence.push(Math.random());
  72. }
  73. random.seed(100);
  74. for (var i = 0; i < sequence.length; ++i) {
  75. assertEquals(sequence[i], Math.random());
  76. }
  77. random.uninstall();
  78. }