-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
67 lines (65 loc) · 2.14 KB
/
index.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
// Try to break https://alf.nu/ReturnTrue examples
// using Property based testing
const fc = require('fast-check');
const settings = {num_runs: 100000};
const wrapIt = function(fn) {
return (...args) => {
try { return !fn(...args); }
catch (err) { return true; }
};
};
const constantFunction = fc.anything().map(v => {
return eval(`_ => ${JSON.stringify(v)}`);
});
describe('Breaking \'return true to win\' using Property based Testing', () => {
// Those examples have been extracted from
// the website: https://alf.nu/ReturnTrue
it('id', () => {
function id(x) {
return x;
}
fc.assert(fc.property(fc.anything(), x => wrapIt(id)(x)), settings);
});
it('reflexive', () => {
function reflexive(x) {
return x != x;
}
fc.assert(fc.property(fc.anything(), x => wrapIt(reflexive)(x)), settings);
});
it('infinity', () => {
function infinity(x, y) {
return x === y && 1/x < 1/y
}
fc.assert(fc.property(fc.anything(), fc.anything(), (x, y) => wrapIt(infinity)(x, y)), settings);
});
it('transitive', () => {
function transitive(x,y,z) {
return x && x == y && y == z && x != z;
}
fc.assert(fc.property(fc.anything(), fc.anything(), fc.anything(), (x, y, z) => wrapIt(transitive)(x, y, z)), settings);
});
it('peano', () => {
function peano(x) {
return (x++ !== x) && (x++ === x);
}
fc.assert(fc.property(fc.anything(), x => wrapIt(peano)(x)), settings);
});
it('ouroborobj', () => {
function ouroborobj(x) {
return x in x;
}
fc.assert(fc.property(fc.anything(), x => wrapIt(ouroborobj)(x)), settings);
});
it('evil1', () => {
function evil1(x) {
return eval(x+'(x)') && !eval(x)(x);
}
fc.assert(fc.property(constantFunction, x => wrapIt(evil1)(x)), settings);
});
it('closure', () => {
function closure(x) {
return x[x] == x;
}
fc.assert(fc.property(fc.anything(), x => wrapIt(closure)(x)), settings);
});
});