/
coverage-many-branches.js
115 lines (109 loc) Β· 3.21 KB
/
coverage-many-branches.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
'use strict';
// Exercise coverage of a class. Note, this logic is silly and exists solely
// to generate branch coverage code paths:
class CoveredClass {
constructor(x, y, opts) {
this.x = x;
this.y = y;
// Exercise coverage of nullish coalescing:
this.opts = opts ?? (Math.random() > 0.5 ? {} : undefined);
}
add() {
return this.x + this.y;
}
addSpecial() {
// Exercise coverage of optional chains:
if (this.opts?.special && this.opts?.special?.x && this.opts?.special?.y) {
return this.opts.special.x + this.opts.special.y;
}
return add();
}
mult() {
return this.x * this.y;
}
multSpecial() {
if (this.opts?.special && this.opts?.special?.x && this.opts?.special?.y) {
return this.opts.special.x * this.opts.special.y;
}
return mult();
}
}
// Excercise coverage of functions:
function add(x, y) {
const mt = new CoveredClass(x, y);
return mt.add();
}
function addSpecial(x, y) {
let mt;
if (Math.random() > 0.5) {
mt = new CoveredClass(x, y);
} else {
mt = new CoveredClass(x, y, {
special: {
x: Math.random() * x,
y: Math.random() * y
}
});
}
return mt.addSpecial();
}
function mult(x, y) {
const mt = new CoveredClass(x, y);
return mt.mult();
}
function multSpecial(x, y) {
let mt;
if (Math.random() > 0.5) {
mt = new CoveredClass(x, y);
} else {
mt = new CoveredClass(x, y, {
special: {
x: Math.random() * x,
y: Math.random() * y
}
});
}
return mt.multSpecial();
}
for (let i = 0; i < parseInt(process.env.N); i++) {
const operations = ['add', 'addSpecial', 'mult', 'multSpecial'];
for (const operation of operations) {
// Exercise coverage of switch statements:
switch (operation) {
case 'add':
if (add(Math.random() * 10, Math.random() * 10) > 10) {
// Exercise coverage of ternary operations:
let r = addSpecial(Math.random() * 10, Math.random() * 10) > 10 ?
mult(Math.random() * 10, Math.random() * 10) :
add(Math.random() * 10, Math.random() * 10);
// Exercise && and ||
if (r && Math.random() > 0.5 || Math.random() < 0.5) r++;
}
break;
case 'addSpecial':
if (addSpecial(Math.random() * 10, Math.random() * 10) > 10 &&
add(Math.random() * 10, Math.random() * 10) > 10) {
let r = mult(Math.random() * 10, Math.random() * 10) > 10 ?
add(Math.random() * 10, Math.random() * 10) > 10 :
mult(Math.random() * 10, Math.random() * 10);
if (r && Math.random() > 0.5 || Math.random() < 0.5) r++;
}
break;
case 'mult':
if (mult(Math.random() * 10, Math.random() * 10) > 10) {
let r = multSpecial(Math.random() * 10, Math.random() * 10) > 10 ?
add(Math.random() * 10, Math.random() * 10) :
mult(Math.random() * 10, Math.random() * 10);
if (r && Math.random() > 0.5 || Math.random() < 0.5) r++;
}
break;
case 'multSpecial':
while (multSpecial(Math.random() * 10, Math.random() * 10) < 10) {
mult(Math.random() * 10, Math.random() * 10);
}
break;
default:
break;
}
}
}