-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
173 lines (159 loc) · 4.24 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
const paph = require('./index');
const add = (store, start, end, weight = 1) => {
store.add({
start,
end,
weight,
transition: (a) => a += ` ${start}${end}`,
});
};
test('rejects bad inputs', () => {
const p = paph();
expect(() => p.add({
start: 0,
}))
.toThrow(/start.*string.*0/);
expect(() => p.add({
start: 'start',
end: 1,
}))
.toThrow(/end.*string.*1/);
expect(() => p.add({
start: 'start',
end: 'end',
weight: true,
}))
.toThrow(/weight.*number.*true/);
expect(() => p.add({
start: 'start',
end: 'end',
weight: 1,
transition: null,
}))
.toThrow(/transition.*function.*null/);
expect(() => p.query(0))
.toThrow(/initial.*string.*0/);
expect(() => p.query('initial', 1))
.toThrow(/final.*string.*1/);
});
test('uses a default weight of 1', () => {
const p = paph();
p.add({
start: 'a',
end: 'b',
transition: () => true,
});
p.add({
start: 'a',
end: 'b',
weight: 1.000001,
transition: () => 1,
});
expect(p.query('a', 'b')())
.toBe(true);
p.add({
start: 'a',
end: 'b',
weight: 0.999999,
transition: () => 0,
});
expect(p.query('a', 'b')())
.toBe(0);
});
test('finds a path when it exists', () => {
const p = paph();
add(p, '0', '1');
add(p, '0', '4');
add(p, '1', '2');
add(p, '2', '3');
expect(p.query('0', '3')(''))
.toBe(' 01 12 23');
expect(p.query('0', '2')(''))
.toBe(' 01 12');
});
test('gracefully fails when no path is found', () => {
const p = paph();
add(p, '0', '1');
add(p, '1', '2');
add(p, '2', '3');
expect(() => p.query('1', '0')(''))
.toThrow(/1[^]*0/);
expect(() => p.query('a', 'b')(''))
.toThrow(/a[^]*b/);
});
test('finds the shortest path, regardless of order', () => {
const p = paph();
add(p, '0', '1');
add(p, '1', '2');
add(p, '0', '2');
add(p, '2', '3');
expect(p.query('0', '3')(''))
.toBe(' 02 23');
add(p, '0', '3');
expect(p.query('0', '3')(''))
.toBe(' 03');
});
test('finds the path with the lowest weight', () => {
const p = paph();
add(p, '0', '1', 2);
add(p, '1', '2', 2);
add(p, '0', '2', 5);
add(p, '2', '3', 1);
expect(p.query('0', '3')(''))
.toBe(' 01 12 23');
expect(p.query('0', '3')(''))
.toBe(' 01 12 23');
});
test('picks the first added transitions when weights are equal', () => {
const p = paph();
add(p, '0', '1', 1);
add(p, '1', '2', 1);
add(p, '0', '2', 2);
expect(p.query('0', '2')(''))
.toBe(' 01 12');
});
test('doesn\'t allow negative weights', () => {
const p = paph();
expect(() => p.add({
start: 'a',
end: 'b',
weight: -1,
transition: () => {},
}))
.toThrow(/negative/);
});
test('doesn\'t get stuck in cycles', () => {
const p = paph();
add(p, '0', '1');
add(p, '1', '0');
expect(() => p.query('0', 'a')(''))
.toThrow(Error);
add(p, '2', '2');
expect(() => p.query('2', 'a')(''))
.toThrow(Error);
});
test('results should be partailly memoized', () => {
let p = paph();
const cap = 1000;
for (let i = 0; i <= cap; ++i) {
let final = Math.min(i+1+Math.floor(Math.random()*4), cap);
let weight = 1+Math.floor(Math.random()*4);
p.add({
start: String(i),
end: String(final),
weight,
transition: (a) => a += String(i),
});
}
const timeColdStart = process.hrtime();
const cold = p.query('0', String(cap))('');
const timeCold = process.hrtime(timeColdStart)[0] * 1000 + process.hrtime(timeColdStart)[1] / 1000000;
const timeHotStart = process.hrtime();
const hot = p.query('0', String(cap))('');
const timeHot = process.hrtime(timeHotStart)[0] * 1000 + process.hrtime(timeHotStart)[1] / 1000000;
expect(cold === hot)
.toBe(true);
console.log(`hot was ${(timeCold/timeHot).toFixed(5)}x faster (${timeCold}ms vs. ${timeHot}ms)`);
expect(timeHot < timeCold)
.toBe(true);
});