options.html 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <html>
  2. <head>
  3. <link rel="stylesheet" href="lib/qunit.css">
  4. </head>
  5. <body>
  6. <div id="qunit"></div>
  7. <div id="qunit-fixture"></div>
  8. <script src="../seedrandom.min.js"></script>
  9. <script src="lib/qunit.js"></script>
  10. <script>
  11. QUnit.module("Options API Test");
  12. QUnit.test("Verify that we can use documented options", function(assert) {
  13. assert.ok(true, "Seeded random created with new:");
  14. var original = Math.random;
  15. assert.ok(true, "Using Math.seedrandom('hello.')");
  16. var result = Math.seedrandom('hello.');
  17. var firstprng = Math.random;
  18. assert.ok(original !== firstprng, "Should change Math.random.");
  19. assert.equal(result, "hello.", "Should return short seed.");
  20. var r = Math.random();
  21. assert.ok(true, "Got " + r);
  22. assert.equal(r, 0.9282578795792454, "Should be 'hello.'#1");
  23. r = Math.random();
  24. assert.ok(true, "Got " + r);
  25. assert.equal(r, 0.3752569768646784, "Should be 'hello.'#2");
  26. assert.ok(true, "Using Math.seedrandom()");
  27. result = Math.seedrandom();
  28. var secondprng = Math.random;
  29. assert.ok(original !== secondprng, "Should change Math.random.");
  30. assert.ok(firstprng !== secondprng, "Should change Math.random.");
  31. assert.equal(result.length, 256, "Should return short seed.");
  32. r = Math.random();
  33. assert.ok(true, "Got " + r);
  34. assert.ok(r != 0.9282578795792454, "Should not be 'hello.'#1");
  35. assert.ok(r != 0.7316977468919549, "Should not be 'hello.'#3");
  36. var autoseed1 = r;
  37. assert.ok(true, "Using Math.seedrandom('added entropy.', { entropy:true })");
  38. result = Math.seedrandom('added entropy.', { entropy:true });
  39. assert.equal(result.length, 256, "Should return short seed.");
  40. var thirdprng = Math.random;
  41. assert.ok(thirdprng !== secondprng, "Should change Math.random.");
  42. r = Math.random();
  43. assert.ok(true, "Got " + r);
  44. assert.ok(r != 0.597067214994467, "Should not be 'added entropy.'#1");
  45. // Reset to original Math.random.
  46. Math.random = original;
  47. assert.ok(true, "Using new Math.seedrandom('hello.')");
  48. var myrng = new Math.seedrandom('hello.');
  49. assert.ok(original === Math.random, "Should not change Math.random.");
  50. assert.ok(original !== myrng, "PRNG should not be Math.random.");
  51. r = myrng();
  52. assert.ok(true, "Got " + r);
  53. assert.equal(r, 0.9282578795792454, "Should be 'hello.'#1");
  54. // Use "quick" to get only 32 bits of randomness in a float.
  55. assert.equal(myrng.quick(), 0.3752569768112153, "Should be quick #1.1");
  56. // Use "int32" to get a 32 bit (signed) integer
  57. assert.equal(myrng.int32(), 986220731, "Should be int32 #1.2");
  58. // As if brought in by node.js
  59. var seedrandom = Math.seedrandom;
  60. assert.ok(true, "Using seedrandom('hello.')");
  61. var rng = seedrandom('hello.');
  62. assert.equal(typeof(rng), 'function', "Should return a function.");
  63. r = rng();
  64. assert.ok(true, "Got " + r);
  65. assert.equal(r, 0.9282578795792454, "Should be 'hello.'#1");
  66. assert.ok(original === Math.random, "Should not change Math.random.");
  67. assert.ok(original !== rng, "PRNG should not be Math.random.");
  68. // Global PRNG: set Math.random.
  69. assert.ok(true, "Using seedrandom('hello.', { global: true })");
  70. result = seedrandom('hello.', { global: true });
  71. assert.equal(result, 'hello.', "Should return short seed.");
  72. assert.ok(original != Math.random, "Should change Math.random.");
  73. r = Math.random();
  74. assert.ok(true, "Got " + r);
  75. assert.equal(r, 0.9282578795792454, "Should be 'hello.'#1");
  76. // Autoseeded non-global
  77. Math.random = original;
  78. assert.ok(true, "Using seedrandom()");
  79. result = seedrandom();
  80. assert.equal(typeof(result), 'function', "Should return function.");
  81. assert.ok(original === Math.random, "Should not change Math.random.");
  82. r = result();
  83. assert.ok(true, "Got " + r);
  84. assert.ok(r != autoseed1, "Should not repeat previous autoseed.");
  85. assert.ok(r != 0.9282578795792454, "Should not be 'hello.'#1");
  86. assert.ok(r != 0.7316977468919549, "Should not be 'hello.'#3");
  87. // Mixing accumulated entropy.
  88. assert.ok(true, "Using seedrandom('added entropy.', { entropy: true })");
  89. rng = seedrandom('added entropy.', { entropy: true });
  90. r = result();
  91. assert.ok(true, "Got " + r);
  92. assert.ok(r != autoseed1, "Should not repeat previous autoseed.");
  93. assert.ok(r != 0.597067214994467, "Should not be 'added entropy.'#1");
  94. // Legacy calling convention for mixing accumulated entropy.
  95. assert.ok(true, "Using seedrandom('added entropy.', true)");
  96. rng = seedrandom('added entropy.', true);
  97. r = result();
  98. assert.ok(true, "Got " + r);
  99. assert.ok(r != autoseed1, "Should not repeat previous autoseed.");
  100. assert.ok(r != 0.597067214994467, "Should not be 'added entropy.'#1");
  101. // The pass option
  102. assert.ok(true, "Using Math.seedrandom(null, { pass: ...");
  103. var obj = Math.seedrandom(null, { pass: function(prng, seed) {
  104. return { random: prng, seed: seed };
  105. }});
  106. assert.ok(original === Math.random, "Should not change Math.random.");
  107. assert.ok(original !== obj.random, "Should be different from Math.random.");
  108. assert.equal(typeof(obj.random), 'function', "Should return a PRNG function.");
  109. assert.equal(typeof(obj.seed), 'string', "Should return a seed.");
  110. var as2 = obj.random();
  111. assert.ok(as2 != 0.9282578795792454, "Should not be 'hello.'#1");
  112. rng = seedrandom(obj.seed);
  113. var as3 = rng();
  114. assert.equal(as2, as3, "Should be reproducible when using the seed.");
  115. // Exercise pass again, with explicit seed and global
  116. assert.ok(true, "Using Math.seedrandom('hello.', { pass: ...");
  117. result = Math.seedrandom('hello.', {
  118. global: 'abc',
  119. pass: function(prng, seed, global) {
  120. assert.equal(typeof(prng), 'function', "Callback arg #1 assert.ok");
  121. assert.equal(seed, 'hello.', "Callback arg #2 assert.ok");
  122. assert.equal(global, 'abc', "Callback arg #3 passed through.");
  123. assert.equal(prng(), 0.9282578795792454, "Should be 'hello.'#1");
  124. return 'def';
  125. }});
  126. assert.equal(result, 'def', "Should return value from callback.");
  127. assert.ok(original === Math.random, "Should not change Math.random.");
  128. // Legacy third argument callback argument:
  129. assert.ok(true, "Using Math.seedrandom('hello.', { global: 50 }, callback)");
  130. result = Math.seedrandom('hello.', { global: 50 },
  131. function(prng, seed, global) {
  132. assert.equal(typeof(prng), 'function', "Callback arg #1 assert.ok");
  133. assert.equal(seed, 'hello.', "Callback arg #2 assert.ok");
  134. assert.equal(global, 50, "Callback arg #3 assert.ok");
  135. assert.equal(prng(), 0.9282578795792454, "Should be 'hello.'#1");
  136. return 'zzz';
  137. });
  138. assert.equal(result, 'zzz', "Should return value from callback.");
  139. assert.ok(original === Math.random, "Should not change Math.random.");
  140. // Global: false.
  141. assert.ok(true, "Using new Math.seedrandom('hello.', {global: false})");
  142. myrng = new Math.seedrandom('hello.', {global:false});
  143. assert.equal(typeof(myrng), 'function', "Should return a PRNG funciton.");
  144. assert.ok(original === Math.random, "Should not change Math.random.");
  145. assert.ok(original !== myrng, "PRNG should not be Math.random.");
  146. r = myrng();
  147. assert.ok(true, "Got " + r);
  148. assert.equal(r, 0.9282578795792454, "Should be 'hello.'#1");
  149. // options = {} when a method of Math.
  150. assert.ok(true, "Using Math.seedrandom('hello.', {})");
  151. var result = Math.seedrandom('hello.');
  152. var xprng = Math.random;
  153. assert.ok(original !== xprng, "Should change Math.random.");
  154. assert.equal(result, "hello.", "Should return short seed.");
  155. var r = Math.random();
  156. assert.ok(true, "Got " + r);
  157. assert.equal(r, 0.9282578795792454, "Should be 'hello.'#1");
  158. r = Math.random();
  159. assert.ok(true, "Got " + r);
  160. assert.equal(r, 0.3752569768646784, "Should be 'hello.'#2");
  161. Math.random = original;
  162. // options = {} when not a method of Math
  163. assert.ok(true, "Using seedrandom('hello.', {})");
  164. rng = seedrandom('hello.', {});
  165. assert.equal(typeof(rng), 'function', "Should return a function.");
  166. r = rng();
  167. assert.ok(true, "Got " + r);
  168. assert.equal(r, 0.9282578795792454, "Should be 'hello.'#1");
  169. assert.ok(original === Math.random, "Should not change Math.random.");
  170. assert.ok(original !== rng, "PRNG should not be Math.random.");
  171. });
  172. </script>
  173. </body>
  174. </html>