-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
test.js
122 lines (94 loc) · 3.49 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
import test from 'ava';
import assertInRange from './assert-in-range.js';
import {consecutiveUniqueRandom, exhaustiveUniqueRandom} from './index.js';
function testConsecutiveUniqueness(t, uniqueRandom) {
const random = uniqueRandom(1, 10);
let previousValue;
for (let count = 0; count < 1000; count++) {
const currentValue = random();
assertInRange(t, currentValue, {start: 1, end: 10});
if (previousValue !== undefined) {
t.not(currentValue, previousValue);
}
previousValue = currentValue;
}
}
test('consecutiveUniqueRandom - main', t => {
testConsecutiveUniqueness(t, consecutiveUniqueRandom);
});
test('consecutiveUniqueRandom - iterator', t => {
t.plan(3); // In case the for-of loop doesn't run
const random = consecutiveUniqueRandom(1, 10);
for (const number of random) { // eslint-disable-line no-unreachable-loop
assertInRange(t, number, {start: 1, end: 10});
break;
}
const {value, done} = random[Symbol.iterator]().next();
assertInRange(t, value, {start: 1, end: 10});
t.false(done);
});
test('exhaustiveUniqueRandom - main', t => {
const random = exhaustiveUniqueRandom(1, 5);
const seenValuesCount = new Map(Array.from({length: 5}, (_, index) => [index + 1, 0]));
for (let count = 1; count <= 10; count++) {
const value = random();
assertInRange(t, value, {start: 1, end: 5});
t.true(seenValuesCount.get(value) < 2, 'Value should only appear twice');
seenValuesCount.set(value, seenValuesCount.get(value) + 1);
}
});
test('exhaustiveUniqueRandom - consecutive uniqueness', t => {
testConsecutiveUniqueness(t, exhaustiveUniqueRandom);
});
test('exhaustiveUniqueRandom - iterator', t => {
t.plan(3); // In case the for-of loop doesn't run
const random = exhaustiveUniqueRandom(1, 10);
for (const number of random) { // eslint-disable-line no-unreachable-loop
assertInRange(t, number, {start: 1, end: 10});
break;
}
const {value, done} = random[Symbol.iterator]().next();
assertInRange(t, value, {start: 1, end: 10});
t.false(done);
});
test('consecutiveUniqueRandom - ensures maximum value can appear', t => {
const minimum = 1;
const maximum = 2; // Reduced range to increase the chance of seeing both values quickly.
const random = consecutiveUniqueRandom(minimum, maximum);
const seenValues = new Set();
for (let count = 0; count < 1000; count++) {
const value = random();
seenValues.add(value);
if (seenValues.size === (maximum - minimum + 1)) {
break;
} // Exit early if all possible values have been seen.
}
t.is(seenValues.size, maximum - minimum + 1, 'Should be able to produce all values in the range');
});
test('consecutiveUniqueRandom - edge case minimum equals maximum', t => {
const random = consecutiveUniqueRandom(5, 5);
for (let count = 0; count < 100; count++) {
t.is(random(), 5);
}
});
test('exhaustiveUniqueRandom - edge case minimum equals maximum', t => {
const random = exhaustiveUniqueRandom(5, 5);
for (let count = 0; count < 100; count++) {
t.is(random(), 5);
}
});
test('exhaustiveUniqueRandom - resets after full cycle', t => {
const rangeSize = 5;
const random = exhaustiveUniqueRandom(1, rangeSize);
const seenValues = new Set();
for (let count = 0; count < rangeSize * 2; count++) {
const value = random();
assertInRange(t, value, {start: 1, end: rangeSize});
if (seenValues.has(value)) {
// We expect a reset to happen here, clearing the seen values
seenValues.clear();
}
seenValues.add(value);
}
t.is(seenValues.size, rangeSize, 'Generator did not properly reset after exhausting unique values.');
});