forked from coverslide/node-alea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
144 lines (101 loc) · 3.14 KB
/
test.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
var test = require('tape')
var Alea = require('./alea')
'use strict'
test("make sure two seeded values are the same", function(t){
var prng1 = Alea(1)
var prng2 = Alea(3)
var prng3 = Alea(1)
var a = prng1()
var b = prng2()
var c = prng3()
t.equal(a, c, 'return values of the same seed')
t.notEqual(a, b, 'return values of different seed')
// test return values directly
t.equal(prng1(), prng3(), 'same seed called again')
t.notEqual(prng1(), prng2(), 'different seed again')
t.notEqual(prng1(), prng3(), 'prng1 called more times than prng3')
t.notEqual(prng2(), prng3(), 'prng3 called again')
t.equal(prng1(), prng3(), 'call counts equal again')
//not sure why explicit end() is needed here
t.end()
})
test("Known values test", function(t){
var prng1 = Alea(12345)
//predefined numbers
var values = [
0.27138191112317145,
0.19615925149992108,
0.6810678059700876
]
t.equal(prng1(), values[0], 'check value 1')
t.equal(prng1(), values[1], 'check value 2')
t.equal(prng1(), values[2], 'check value 3')
t.end()
})
test("Uint32 test", function(t){
var prng1 = Alea(12345)
//predefined numbers
var values = [
1165576433,
842497570,
2925163953
]
t.equal(prng1.uint32(), values[0], 'check value 1')
t.equal(prng1.uint32(), values[1], 'check value 2')
t.equal(prng1.uint32(), values[2], 'check value 3')
t.end()
})
test("Fract53 test", function(t){
var prng1 = Alea(12345)
//predefined numbers
var values = [
0.27138191116884325,
0.6810678062004586,
0.3407802057882554
]
t.equal(prng1.fract53(), values[0], 'check value 1')
t.equal(prng1.fract53(), values[1], 'check value 2')
t.equal(prng1.fract53(), values[2], 'check value 3')
t.end()
})
test("Import with Alea.importState()", function(t){
var prng1 = Alea(200)
// generate a few numbers
prng1()
prng1()
prng1()
var e = prng1.exportState()
var prng4 = Alea.importState(e)
t.equal(prng1(), prng4(), 'synced prngs, call 1')
t.equal(prng1(), prng4(), 'synced prngs, call 2')
t.equal(prng1(), prng4(), 'synced prngs, call 3')
t.end()
})
test("Resync two differring prngs with prng.importState()", function(t){
var prng1 = Alea(200000)
var prng2 = Alea(9000)
// generate a few numbers
t.notEqual(prng1(), prng2(), 'just generating randomness, call 1')
t.notEqual(prng1(), prng2(), 'just generating randomness, call 2')
t.notEqual(prng1(), prng2(), 'just generating randomness, call 3')
// sync prng2 to prng1
prng2.importState(prng1.exportState())
t.equal(prng1(), prng2(), 'imported prng, call 1')
t.equal(prng1(), prng2(), 'imported prng, call 2')
t.equal(prng1(), prng2(), 'imported prng, call 3')
// let's test they still sync up if called non-sequentially
prng1()
prng1()
var a1 = prng1()
var b1 = prng1()
var c1 = prng1()
prng2()
prng2()
var a2 = prng2()
var b2 = prng2()
var c2 = prng2()
t.equal(a1, a2, 'return values should sync based on number of calls, call 1')
t.equal(b1, b2, 'return values should sync based on number of calls, call 2')
t.equal(c1, c2, 'return values should sync based on number of calls, call 3')
t.end()
})