-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
73 lines (63 loc) · 1.42 KB
/
index.js
File metadata and controls
73 lines (63 loc) · 1.42 KB
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
import test from 'tape';
import loopBreaker from '../src/';
testBrokenLoop('while with body', (c) => {
while (c()) {
void(0);
}
});
testBrokenLoop('while without body', (c) => {
while (c()) void(0);
});
testBrokenLoop('for with body', (c) => {
for (let i = 0; c(); i++) {
void(0);
}
});
testBrokenLoop('for without body', (c) => {
for (let i = 0; c(); i++) void(0);
});
testBrokenLoop('nested infinite for loops', (c) => {
for (let i = 0; c(); i++) {
for (let j = 0; c(); j++) {
void(0);
}
}
});
testBrokenLoop('inner infinite for loop', (c) => {
for (let i = 0; i < 10; i++) {
for (let j = 0; c(); j++) {
void(0);
}
}
});
testBrokenLoop('outer infinite for loop', (c) => {
for (let i = 0; c(); i++) {
for (let j = 0; j < 10; j++) {
void(0);
}
}
});
testBrokenLoop('do-while with body', (c) => {
do {
void(0);
} while (c());
});
testBrokenLoop('non-infinite slow code execution', () => {
const testStartTime = Date.now() + 2000;
function checkDate() {
return Date.now() - testStartTime < 2000;
}
while (checkDate()) void(0);
});
function testBrokenLoop(message, fn) {
test(message, (assert) => {
const startTime = Date.now();
const fnSource = loopBreaker(`(${fn})`);
const modifiedFn = eval(fnSource);
assert.throws(
() => modifiedFn(() => Date.now() - startTime < 5000),
/loop broken/i
);
assert.end();
});
}