-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.js
191 lines (152 loc) · 3.99 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import test from 'ava';
import is from '@sindresorhus/is';
import Randoma from './index.js';
const MAX_INT32 = 2_147_483_647;
const ITERATIONS = 1000;
function assertInteger(t, seed) {
const random = new Randoma({seed});
for (let index = 0; index < ITERATIONS; index++) {
const result = random.integer();
t.true(is.integer(result));
t.true(result <= MAX_INT32);
}
}
function assertIntegerInRange(t, seed) {
const random = new Randoma({seed});
const min = 33;
const max = 2242;
for (let index = 0; index < ITERATIONS; index++) {
const result = random.integerInRange(min, max);
t.true(is.integer(result));
t.true(result >= min);
t.true(result <= max);
}
}
function assertFloat(t, seed) {
const random = new Randoma({seed});
for (let index = 0; index < ITERATIONS; index++) {
const result = random.float();
t.false(is.integer(result));
t.true(result <= MAX_INT32);
}
}
function assertFloatInRange(t, seed) {
const random = new Randoma({seed});
const min = 0.33;
const max = 0.522_42;
for (let index = 0; index < ITERATIONS; index++) {
const result = random.floatInRange(min, max);
t.false(is.integer(result));
t.true(result >= min);
t.true(result <= max);
}
}
function assertBoolean(t, seed) {
const random = new Randoma({seed});
let average = 0;
for (let index = 0; index < ITERATIONS; index++) {
const result = random.boolean();
t.true(is.boolean(result));
average += result ? 1 : -1;
}
t.true(average < 10_000);
}
function assertArrayItem(t, seed) {
const random = new Randoma({seed});
const fixture = [1, 2, 3, 4, 5];
const set = new Set();
for (let index = 0; index < ITERATIONS; index++) {
const result = random.arrayItem(fixture);
t.true(is.number(result));
set.add(result);
}
t.deepEqual([...set].sort(), fixture);
}
function assertDate(t, seed) {
const random = new Randoma({seed});
for (let index = 0; index < ITERATIONS; index++) {
const result = random.date();
t.true(is.date(result));
t.true(is.function(result.getTime));
}
}
function assertDateInRange(t, seed) {
const random = new Randoma({seed});
const startDate = new Date('2009');
const endDate = new Date('2010');
for (let index = 0; index < ITERATIONS; index++) {
const result = random.dateInRange(startDate, endDate);
t.true(is.date(result));
t.true(result >= startDate);
t.true(result <= endDate);
}
}
function runFn(fn) {
const random = new Randoma({seed: 33});
const values = [];
for (let index = 0; index < ITERATIONS; index++) {
values.push(random[fn]());
}
return values;
}
function runAsserts(t, fn, assertFn) {
const seeds = [
0,
1,
10,
-10,
Number.MIN_SAFE_INTEGER,
Number.MAX_SAFE_INTEGER,
];
for (const seed of seeds) {
assertFn(t, seed);
}
if (['integer', 'float', 'boolean'].includes(fn)) {
// Ensure it generates numbers deterministically
t.deepEqual(runFn(fn), runFn(fn));
t.deepEqual(runFn(fn), runFn(fn));
}
}
test('.integer()', t => {
runAsserts(t, 'integer', assertInteger);
});
test('.integerInRange()', t => {
runAsserts(t, 'integerInRange', assertIntegerInRange);
});
test('.float()', t => {
runAsserts(t, 'float', assertFloat);
});
test('.floatInRange()', t => {
runAsserts(t, 'floatInRange', assertFloatInRange);
});
test('.boolean()', t => {
runAsserts(t, 'boolean', assertBoolean);
});
test('.arrayItem()', t => {
runAsserts(t, 'arrayItem', assertArrayItem);
});
test('.date()', t => {
runAsserts(t, 'date', assertDate);
});
test('.dateInRange()', t => {
runAsserts(t, 'dateInRange', assertDateInRange);
});
test('.color()', t => {
const random = new Randoma({seed: 1});
t.is(random.color(0.5).hex().toString(), '#799CF2');
});
test('string seed', t => {
const seed = '🦄';
const random = new Randoma({seed});
t.not(random.integer(), random.integer());
t.is(
(new Randoma({seed})).integer(),
(new Randoma({seed})).integer(),
);
});
test('Randoma.seed()', t => {
const seed = Randoma.seed();
t.true(Number.isInteger(seed));
const random = new Randoma({seed});
t.not(random.integer(), random.integer());
});