-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickcheck.js
More file actions
78 lines (68 loc) · 1.77 KB
/
quickcheck.js
File metadata and controls
78 lines (68 loc) · 1.77 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
74
75
76
77
78
/*
* Random boilerplate, and jsQuickCheck-stuff
*/
var propContains = {args: [randomString], prop: function (rs) { return true; }};
function randomString(s) {
// map (toChar . (const $ randomInt 0 255) ) range(0, s)
return range(0, s)
.map(function (x) { return randomInt(0, 255); })
.map(String.fromCharCode);
}
/*
* Returns the list of values between min, max using step
*/
function range(min, max, step) {
var r = [];
for (var i = min; i <= max; i += step) { r.push(i); }
return r;
}
// what properties can we deduce about range?
// var propRange = {args: [randomInt, randomInt, randomInt], prop: function (min, max, step) {
/*
* Return a random integer between 0 and s, it may not be randomly
* distributed.
*/
function randomIntImprecise(s) {
return Math.round(Math.random()*s);
}
/*
* Return a random integer between min and max. The distribution
* should be even.
*/
function randomInt(min, max) {
var r = Math.random();
var i = max - min + 0.5;
var q = r * i + (min - 0.5);
return Math.round(q);
}
/*
* Some properties on randomInt
*/
var propRandomInt = [
{
args: [randomIntImprecise, randomIntImprecise],
prop: function (min, max) {
return min <= randomInt(min, max) && max >= randomInt(min, max);
}
},
{
args: [randomIntImprecise] ,
prop: function (x) {
return x = randomInt(x, x);
}
}
];
// test: randomInt faktiskt retunerar värden mellan min och max
// test: randomInt returnerar min
// test: eller max ibland (ganska otroligt
// test: randomInt(x,x) = x (borde funka)
/*
randomInt(0, 4)
i = (4 - 0 + 0.5) = 4.5
q = r * 4.5 + (0 - 0.5) = 4.5r - 0.5 = [-0.5, 4]
round(q) = [0, 4]
randomInt(1, 5)
i = (5 - 1 + 0.5) = 4.5
q = r * 4.5 + (1 - 0.5) = 4.5r + 0.5 = [0.5, 5]
round(q) = [0.5, 5]
*/