-
Notifications
You must be signed in to change notification settings - Fork 1
/
random_tests.js
66 lines (59 loc) · 2.1 KB
/
random_tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
var Random = require("./random");
var prepareTest = function (test) {
test.isTrue = test.ok;
test.isFalse = function (v, m) {
return test.ok(!v, m);
};
};
exports['test random'] = function (test) {
prepareTest(test);
// Deterministic with a specified seed, which should generate the
// same sequence in all environments.
//
// For repeatable unit test failures using deterministic random
// number sequences it's fine if a new Meteor release changes the
// algorithm being used and it starts generating a different
// sequence for a seed, as long as the sequence is consistent for
// a particular release.
var random = Random.createWithSeeds(0);
test.equal(random.id(), "cp9hWvhg8GSvuZ9os");
test.equal(random.id(), "3f3k6Xo7rrHCifQhR");
test.equal(random.id(), "shxDnjWWmnKPEoLhM");
test.equal(random.id(), "6QTjB8C5SEqhmz4ni");
};
// node crypto and window.crypto.getRandomValues() don't let us specify a seed,
// but at least test that the output is in the right format.
exports['test random - format'] = function (test) {
prepareTest(test);
var idLen = 17;
test.equal(Random.id().length, idLen);
test.equal(Random.id(29).length, 29);
var numDigits = 9;
var hexStr = Random.hexString(numDigits);
test.equal(hexStr.length, numDigits);
parseInt(hexStr, 16); // should not throw
var frac = Random.fraction();
test.isTrue(frac < 1.0);
test.isTrue(frac >= 0.0);
test.equal(Random.secret().length, 43);
test.equal(Random.secret(13).length, 13);
};
exports['test random - Alea is last resort'] = function (test) {
prepareTest(test);
if (process) {
test.isTrue(Random.alea === undefined);
}
if (typeof window !== "undefined") {
var useGetRandomValues = !!(typeof window !== "undefined" &&
window.crypto && window.crypto.getRandomValues);
test.equal(Random.alea === undefined, useGetRandomValues);
}
};
exports['test random - createWithSeeds requires parameters'] = function (test) {
prepareTest(test);
test.throws(function () {
Random.createWithSeeds();
});
};
console.log(exports);
if (module == require.main) require('test').run(exports);