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