nodetest.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. var assert = require("assert");
  2. var seedrandom = require("../seedrandom");
  3. var requirejs = require("requirejs");
  4. // Stub out requirejs if in the browser via browserify.
  5. if (!requirejs.config) {
  6. requirejs = require;
  7. } else {
  8. requirejs.config({
  9. baseUrl: __dirname
  10. });
  11. }
  12. describe("Nodejs API Test", function() {
  13. it('should pass basic tests.', function() {
  14. var original = Math.random,
  15. result, r, xprng, obj, as2, as3, autoseed1, myrng,
  16. firstprng, secondprng, thirdprng, rng;
  17. result = Math.seedrandom('hello.');
  18. firstprng = Math.random;
  19. assert.ok(original !== firstprng, "Should change Math.random.");
  20. assert.equal(result, "hello.", "Should return short seed.");
  21. r = Math.random();
  22. assert.equal(r, 0.9282578795792454, "Should be 'hello.'#1");
  23. r = Math.random();
  24. assert.equal(r, 0.3752569768646784, "Should be 'hello.'#2");
  25. // should be able to autoseed
  26. result = Math.seedrandom();
  27. secondprng = Math.random;
  28. assert.ok(original !== secondprng, "Should change Math.random.");
  29. assert.ok(firstprng !== secondprng, "Should change Math.random.");
  30. assert.equal(result.length, 256, "Should return short seed.");
  31. r = Math.random();
  32. assert.ok(r > 0, "Should be posititive.");
  33. assert.ok(r < 1, "Should be less than 1.");
  34. assert.ok(r != 0.9282578795792454, "Should not be 'hello.'#1");
  35. assert.ok(r != 0.3752569768646784, "Should not be 'hello.'#2");
  36. assert.ok(r != 0.7316977468919549, "Should not be 'hello.'#3");
  37. autoseed1 = r;
  38. // should be able to add entropy.
  39. result = Math.seedrandom('added entropy.', { entropy:true });
  40. assert.equal(result.length, 256, "Should return short seed.");
  41. thirdprng = Math.random;
  42. assert.ok(thirdprng !== secondprng, "Should change Math.random.");
  43. r = Math.random();
  44. assert.ok(r != 0.597067214994467, "Should not be 'added entropy.'#1");
  45. // Reset to original Math.random.
  46. Math.random = original;
  47. // should be able to use new Math.seedrandom('hello.')
  48. 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.equal(r, 0.9282578795792454, "Should be 'hello.'#1");
  53. // should be able to use seedrandom('hello.')"
  54. rng = seedrandom('hello.');
  55. assert.equal(typeof(rng), 'function', "Should return a function.");
  56. r = rng();
  57. assert.equal(r, 0.9282578795792454, "Should be 'hello.'#1");
  58. assert.ok(original === Math.random, "Should not change Math.random.");
  59. assert.ok(original !== rng, "PRNG should not be Math.random.");
  60. // Global PRNG: set Math.random.
  61. // should be able to use seedrandom('hello.', { global: true })
  62. result = seedrandom('hello.', { global: true });
  63. assert.equal(result, 'hello.', "Should return short seed.");
  64. assert.ok(original != Math.random, "Should change Math.random.");
  65. r = Math.random();
  66. assert.equal(r, 0.9282578795792454, "Should be 'hello.'#1");
  67. // Autoseeded non-global
  68. Math.random = original;
  69. // should be able to use seedrandom()
  70. result = seedrandom();
  71. assert.equal(typeof(result), 'function', "Should return function.");
  72. assert.ok(original === Math.random, "Should not change Math.random.");
  73. r = result();
  74. // got " + r);
  75. assert.ok(r != autoseed1, "Should not repeat previous autoseed.");
  76. assert.ok(r != 0.9282578795792454, "Should not be 'hello.'#1");
  77. assert.ok(r != 0.7316977468919549, "Should not be 'hello.'#3");
  78. // Mixing accumulated entropy.
  79. // should be able to use seedrandom('added entropy.', { entropy: true })
  80. rng = seedrandom('added entropy.', { entropy: true });
  81. r = result();
  82. // got " + r);
  83. assert.ok(r != autoseed1, "Should not repeat previous autoseed.");
  84. assert.ok(r != 0.597067214994467, "Should not be 'added entropy.'#1");
  85. // Legacy calling convention for mixing accumulated entropy.
  86. // should be able to use seedrandom('added entropy.', true)
  87. rng = seedrandom('added entropy.', true);
  88. r = result();
  89. // got " + r);
  90. assert.ok(r != autoseed1, "Should not repeat previous autoseed.");
  91. assert.ok(r != 0.597067214994467, "Should not be 'added entropy.'#1");
  92. // The pass option
  93. // should be able to use Math.seedrandom(null, { pass: ...
  94. obj = Math.seedrandom(null, { pass: function(prng, seed) {
  95. return { random: prng, seed: seed };
  96. }});
  97. assert.ok(original === Math.random, "Should not change Math.random.");
  98. assert.ok(original !== obj.random, "Should be different from Math.random.");
  99. assert.equal(typeof(obj.random), 'function', "Should return a PRNG function.");
  100. assert.equal(typeof(obj.seed), 'string', "Should return a seed.");
  101. as2 = obj.random();
  102. assert.ok(as2 != 0.9282578795792454, "Should not be 'hello.'#1");
  103. rng = seedrandom(obj.seed);
  104. as3 = rng();
  105. assert.equal(as2, as3, "Should be reproducible when using the seed.");
  106. // Exercise pass again, with explicit seed and global
  107. // should be able to use Math.seedrandom('hello.', { pass: ...
  108. result = Math.seedrandom('hello.', {
  109. global: 'abc',
  110. pass: function(prng, seed, global) {
  111. assert.equal(typeof(prng), 'function', "Callback arg #1 assert");
  112. assert.equal(seed, 'hello.', "Callback arg #2 assert");
  113. assert.equal(global, 'abc', "Callback arg #3 passed through.");
  114. assert.equal(prng(), 0.9282578795792454, "Should be 'hello.'#1");
  115. return 'def';
  116. }});
  117. assert.equal(result, 'def', "Should return value from callback.");
  118. assert.ok(original === Math.random, "Should not change Math.random.");
  119. // Legacy third argument callback argument:
  120. // should be able to use Math.seedrandom('hello.', { global: 50 }, callback)
  121. result = Math.seedrandom('hello.', { global: 50 },
  122. function(prng, seed, global) {
  123. assert.equal(typeof(prng), 'function', "Callback arg #1 assert");
  124. assert.equal(seed, 'hello.', "Callback arg #2 assert");
  125. assert.equal(global, 50, "Callback arg #3 assert");
  126. assert.equal(prng(), 0.9282578795792454, "Should be 'hello.'#1");
  127. return 'zzz';
  128. });
  129. assert.equal(result, 'zzz', "Should return value from callback.");
  130. assert.ok(original === Math.random, "Should not change Math.random.");
  131. // Global: false.
  132. // should be able to use new Math.seedrandom('hello.', {global: false})
  133. myrng = new Math.seedrandom('hello.', {global:false});
  134. assert.equal(typeof(myrng), 'function', "Should return a PRNG funciton.");
  135. assert.ok(original === Math.random, "Should not change Math.random.");
  136. assert.ok(original !== myrng, "PRNG should not be Math.random.");
  137. r = myrng();
  138. assert.equal(r, 0.9282578795792454, "Should be 'hello.'#1");
  139. // options = {} when a method of Math.
  140. // should be able to use Math.seedrandom('hello.', {})
  141. result = Math.seedrandom('hello.');
  142. xprng = Math.random;
  143. assert.ok(original !== xprng, "Should change Math.random.");
  144. assert.equal(result, "hello.", "Should return short seed.");
  145. r = Math.random();
  146. assert.equal(r, 0.9282578795792454, "Should be 'hello.'#1");
  147. r = Math.random();
  148. assert.equal(r, 0.3752569768646784, "Should be 'hello.'#2");
  149. Math.random = original;
  150. // options = {} when not a method of Math
  151. // should be able to use seedrandom('hello.', {})
  152. rng = seedrandom('hello.', {});
  153. assert.equal(typeof(rng), 'function', "Should return a function.");
  154. r = rng();
  155. assert.equal(r, 0.9282578795792454, "Should be 'hello.'#1");
  156. assert.ok(original === Math.random, "Should not change Math.random.");
  157. assert.ok(original !== rng, "PRNG should not be Math.random.");
  158. });
  159. it('should support state api.', function() {
  160. // Verify that there is no state method
  161. var dummy = seedrandom('hello');
  162. var unexpected = -1;
  163. var expected = -1;
  164. try {
  165. unexpected = dummy.state();
  166. } catch(e) {
  167. expected = 1;
  168. }
  169. assert.equal(unexpected, -1);
  170. assert.equal(expected, 1);
  171. var count = 0;
  172. for (var x in dummy) {
  173. if (x == 'state') count += 1;
  174. }
  175. assert.equal(count, 0);
  176. // Verify that a state method can be added
  177. var saveable = seedrandom("secret-seed", {state: true});
  178. var ordinary = seedrandom("secret-seed");
  179. for (var j = 0; j < 1e2; ++j) {
  180. assert.equal(ordinary(), saveable());
  181. }
  182. var virgin = seedrandom("secret-seed");
  183. var saved = saveable.state();
  184. var replica = seedrandom("", {state: saved});
  185. for (var j = 0; j < 1e2; ++j) {
  186. var r = replica();
  187. assert.equal(r, saveable());
  188. assert.equal(r, ordinary());
  189. assert.ok(r != virgin());
  190. }
  191. });
  192. it('should support requirejs in node.', function() {
  193. var original = Math.random;
  194. var rsr = requirejs('../seedrandom');
  195. var rng = rsr('hello.');
  196. assert.equal(typeof(rng), 'function', "Should return a function.");
  197. var r = rng();
  198. assert.equal(r, 0.9282578795792454, "Should be 'hello.'#1");
  199. assert.ok(original === Math.random, "Should not change Math.random.");
  200. assert.ok(original !== rng, "PRNG should not be Math.random.");
  201. });
  202. // End of test.
  203. });