-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.js
157 lines (144 loc) · 3.71 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
145
146
147
148
149
150
151
152
153
154
155
156
157
var test = require('tape')
var Random = require('random-js')
var Engine = require('./')
var utils = require('./utils')
var lodash = require('lodash')
// var crypto = require('crypto-js')
var crypto = require('crypto')
//create random
// var seed = Random.generateEntropyArray()
// var engine = Random.engines.mt19937().seedWithArray(seed)
// var rand = new Random(engine)
test('provable',function(t){
var engine = null
var random = null
var options = {
count:100000,
seed:'test seed',
publicSeed:'another seed',
}
t.test('init',function(t){
engine = Engine(options)
t.ok(engine)
t.end()
})
t.test('hashes',function(t){
var hashes = engine.hashes()
t.equal(hashes.length,options.count)
t.end()
})
t.test('hashes slice',function(t){
var hashes = engine.hashes(0,10)
t.equal(hashes.length,10)
t.end()
})
t.test('stats',function(t){
var stats = {count:0,min:Math.pow(2,32),max:0,hist:{}}
var val = engine()
do{
stats.min = stats.min > val ? val: stats.min
stats.max = stats.max < val ? val: stats.max
stats.count++
// if(stats.hist[val] == null) stats.hist[val] = 0
// stats.hist[val]++
try{
val = engine()
// console.log(val)
}catch(e){
val = null
}
}while(val)
t.ok(stats.min > 0)
t.ok(stats.max < Math.pow(2,32))
t.equal(stats.count,options.count)
console.log(stats)
t.end()
})
t.test('state',function(t){
var state = engine.state(options)
t.equal(state.index,options.count)
t.equal(state.seed,options.seed)
state = Engine().state()
t.ok(state.seed.length)
t.notEqual(state.seed,options.seed)
t.end()
})
t.test('random',function(t){
engine = Engine(options)
random = Random(engine)
t.ok(random)
t.end()
})
t.test('next',function(t){
engine = Engine({count:10})
var hash = engine.next()
t.ok(hash.length)
var nextHash = engine.peek()
var scrambled = engine.next('clientseed')
t.notEqual(nextHash,scrambled)
t.end()
})
t.test('last',function(t){
var next = engine.next()
var last = engine.last()
var curr = engine.peek()
t.equal(next,last)
t.notEqual(curr,last)
t.end()
})
t.test('random stats',function(t){
var val = random.bool()
var stats = {count:0,'true':0,'false':0}
do{
stats[val]++
stats.count++
try{
val = random.bool()
}catch(e){
val = null
}
}while(val !== null)
var prob = stats.true/options.count
console.log(stats,prob)
t.equal(stats.count,options.count)
t.ok(lodash.inRange(prob,.49,.51))
t.end()
})
t.test('resume',function(t){
engine = Engine(options)
lodash.times(100,engine)
var state = engine.state()
var next = engine.peek()
engine = Engine(state)
var hash = engine.next()
t.equal(hash,next)
t.end()
})
t.test('generate',function(t){
var series = Engine.generate(10,'test')
t.equal(series.length,10)
series = Engine.generate(10,'seed')
t.equal(series.length,10)
t.end()
})
t.test('proving stuff',function(t){
var hash = 'bea3d13023a3f99f16553c6bc9e02f78b09de773d2d226b93afffe16022be98f'
var provable = Engine({
seed:hash,
count:10,
})
var hashes = provable.hashes(null,null,true,true)
var original = lodash.clone(provable.hashes())
t.deepEqual(original,hashes.slice(1).reverse())
t.equal(hashes[0],hash)
t.deepEqual(original,provable.hashes())
t.end()
})
t.test('rands',function(t){
var hash = 'bea3d13023a3f99f16553c6bc9e02f78b09de773d2d226b93afffe16022be98f'
t.ok(Engine.toFloat(hash))
t.ok(Engine.toInt(hash))
t.ok(Engine.toBool(hash))
t.end()
})
})