Skip to content

Commit 25b9f84

Browse files
committed
coverage: Branch coverage tests for lazy boolean operators
1 parent 40cfc2d commit 25b9f84

File tree

3 files changed

+370
-0
lines changed

3 files changed

+370
-0
lines changed
+177
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
Function name: lazy_boolean::branch_and
2+
Raw bytes (42): 0x[01, 01, 03, 09, 0a, 05, 09, 05, 09, 06, 01, 13, 01, 01, 10, 03, 04, 09, 00, 0a, 05, 00, 0d, 00, 0e, 20, 09, 0a, 00, 0d, 00, 0e, 09, 00, 12, 00, 13, 03, 01, 05, 01, 02]
3+
Number of files: 1
4+
- file 0 => global file 1
5+
Number of expressions: 3
6+
- expression 0 operands: lhs = Counter(2), rhs = Expression(2, Sub)
7+
- expression 1 operands: lhs = Counter(1), rhs = Counter(2)
8+
- expression 2 operands: lhs = Counter(1), rhs = Counter(2)
9+
Number of file 0 mappings: 6
10+
- Code(Counter(0)) at (prev + 19, 1) to (start + 1, 16)
11+
- Code(Expression(0, Add)) at (prev + 4, 9) to (start + 0, 10)
12+
= (c2 + (c1 - c2))
13+
- Code(Counter(1)) at (prev + 0, 13) to (start + 0, 14)
14+
- Branch { true: Counter(2), false: Expression(2, Sub) } at (prev + 0, 13) to (start + 0, 14)
15+
true = c2
16+
false = (c1 - c2)
17+
- Code(Counter(2)) at (prev + 0, 18) to (start + 0, 19)
18+
- Code(Expression(0, Add)) at (prev + 1, 5) to (start + 1, 2)
19+
= (c2 + (c1 - c2))
20+
21+
Function name: lazy_boolean::branch_or
22+
Raw bytes (44): 0x[01, 01, 04, 09, 0e, 05, 09, 05, 09, 05, 09, 06, 01, 1b, 01, 01, 10, 03, 04, 09, 00, 0a, 05, 00, 0d, 00, 0e, 20, 09, 0e, 00, 0d, 00, 0e, 0e, 00, 12, 00, 13, 03, 01, 05, 01, 02]
23+
Number of files: 1
24+
- file 0 => global file 1
25+
Number of expressions: 4
26+
- expression 0 operands: lhs = Counter(2), rhs = Expression(3, Sub)
27+
- expression 1 operands: lhs = Counter(1), rhs = Counter(2)
28+
- expression 2 operands: lhs = Counter(1), rhs = Counter(2)
29+
- expression 3 operands: lhs = Counter(1), rhs = Counter(2)
30+
Number of file 0 mappings: 6
31+
- Code(Counter(0)) at (prev + 27, 1) to (start + 1, 16)
32+
- Code(Expression(0, Add)) at (prev + 4, 9) to (start + 0, 10)
33+
= (c2 + (c1 - c2))
34+
- Code(Counter(1)) at (prev + 0, 13) to (start + 0, 14)
35+
- Branch { true: Counter(2), false: Expression(3, Sub) } at (prev + 0, 13) to (start + 0, 14)
36+
true = c2
37+
false = (c1 - c2)
38+
- Code(Expression(3, Sub)) at (prev + 0, 18) to (start + 0, 19)
39+
= (c1 - c2)
40+
- Code(Expression(0, Add)) at (prev + 1, 5) to (start + 1, 2)
41+
= (c2 + (c1 - c2))
42+
43+
Function name: lazy_boolean::chain
44+
Raw bytes (149): 0x[01, 01, 13, 11, 07, 0b, 16, 15, 1a, 09, 0d, 05, 09, 05, 09, 09, 0d, 47, 25, 4b, 21, 19, 1d, 03, 19, 03, 19, 3e, 1d, 03, 19, 3e, 1d, 03, 19, 47, 25, 4b, 21, 19, 1d, 13, 01, 24, 01, 01, 10, 03, 04, 09, 00, 0a, 05, 00, 0d, 00, 12, 20, 09, 16, 00, 0d, 00, 12, 09, 00, 16, 00, 1b, 20, 0d, 1a, 00, 16, 00, 1b, 0d, 00, 1f, 00, 24, 20, 11, 15, 00, 1f, 00, 24, 11, 00, 28, 00, 2d, 03, 01, 05, 00, 11, 43, 03, 09, 00, 0a, 03, 00, 0d, 00, 12, 20, 19, 3e, 00, 0d, 00, 12, 3e, 00, 16, 00, 1b, 20, 1d, 3a, 00, 16, 00, 1b, 3a, 00, 1f, 00, 24, 20, 21, 25, 00, 1f, 00, 24, 25, 00, 28, 00, 2d, 43, 01, 05, 01, 02]
45+
Number of files: 1
46+
- file 0 => global file 1
47+
Number of expressions: 19
48+
- expression 0 operands: lhs = Counter(4), rhs = Expression(1, Add)
49+
- expression 1 operands: lhs = Expression(2, Add), rhs = Expression(5, Sub)
50+
- expression 2 operands: lhs = Counter(5), rhs = Expression(6, Sub)
51+
- expression 3 operands: lhs = Counter(2), rhs = Counter(3)
52+
- expression 4 operands: lhs = Counter(1), rhs = Counter(2)
53+
- expression 5 operands: lhs = Counter(1), rhs = Counter(2)
54+
- expression 6 operands: lhs = Counter(2), rhs = Counter(3)
55+
- expression 7 operands: lhs = Expression(17, Add), rhs = Counter(9)
56+
- expression 8 operands: lhs = Expression(18, Add), rhs = Counter(8)
57+
- expression 9 operands: lhs = Counter(6), rhs = Counter(7)
58+
- expression 10 operands: lhs = Expression(0, Add), rhs = Counter(6)
59+
- expression 11 operands: lhs = Expression(0, Add), rhs = Counter(6)
60+
- expression 12 operands: lhs = Expression(15, Sub), rhs = Counter(7)
61+
- expression 13 operands: lhs = Expression(0, Add), rhs = Counter(6)
62+
- expression 14 operands: lhs = Expression(15, Sub), rhs = Counter(7)
63+
- expression 15 operands: lhs = Expression(0, Add), rhs = Counter(6)
64+
- expression 16 operands: lhs = Expression(17, Add), rhs = Counter(9)
65+
- expression 17 operands: lhs = Expression(18, Add), rhs = Counter(8)
66+
- expression 18 operands: lhs = Counter(6), rhs = Counter(7)
67+
Number of file 0 mappings: 19
68+
- Code(Counter(0)) at (prev + 36, 1) to (start + 1, 16)
69+
- Code(Expression(0, Add)) at (prev + 4, 9) to (start + 0, 10)
70+
= (c4 + ((c5 + (c2 - c3)) + (c1 - c2)))
71+
- Code(Counter(1)) at (prev + 0, 13) to (start + 0, 18)
72+
- Branch { true: Counter(2), false: Expression(5, Sub) } at (prev + 0, 13) to (start + 0, 18)
73+
true = c2
74+
false = (c1 - c2)
75+
- Code(Counter(2)) at (prev + 0, 22) to (start + 0, 27)
76+
- Branch { true: Counter(3), false: Expression(6, Sub) } at (prev + 0, 22) to (start + 0, 27)
77+
true = c3
78+
false = (c2 - c3)
79+
- Code(Counter(3)) at (prev + 0, 31) to (start + 0, 36)
80+
- Branch { true: Counter(4), false: Counter(5) } at (prev + 0, 31) to (start + 0, 36)
81+
true = c4
82+
false = c5
83+
- Code(Counter(4)) at (prev + 0, 40) to (start + 0, 45)
84+
- Code(Expression(0, Add)) at (prev + 1, 5) to (start + 0, 17)
85+
= (c4 + ((c5 + (c2 - c3)) + (c1 - c2)))
86+
- Code(Expression(16, Add)) at (prev + 3, 9) to (start + 0, 10)
87+
= (((c6 + c7) + c8) + c9)
88+
- Code(Expression(0, Add)) at (prev + 0, 13) to (start + 0, 18)
89+
= (c4 + ((c5 + (c2 - c3)) + (c1 - c2)))
90+
- Branch { true: Counter(6), false: Expression(15, Sub) } at (prev + 0, 13) to (start + 0, 18)
91+
true = c6
92+
false = ((c4 + ((c5 + (c2 - c3)) + (c1 - c2))) - c6)
93+
- Code(Expression(15, Sub)) at (prev + 0, 22) to (start + 0, 27)
94+
= ((c4 + ((c5 + (c2 - c3)) + (c1 - c2))) - c6)
95+
- Branch { true: Counter(7), false: Expression(14, Sub) } at (prev + 0, 22) to (start + 0, 27)
96+
true = c7
97+
false = (((c4 + ((c5 + (c2 - c3)) + (c1 - c2))) - c6) - c7)
98+
- Code(Expression(14, Sub)) at (prev + 0, 31) to (start + 0, 36)
99+
= (((c4 + ((c5 + (c2 - c3)) + (c1 - c2))) - c6) - c7)
100+
- Branch { true: Counter(8), false: Counter(9) } at (prev + 0, 31) to (start + 0, 36)
101+
true = c8
102+
false = c9
103+
- Code(Counter(9)) at (prev + 0, 40) to (start + 0, 45)
104+
- Code(Expression(16, Add)) at (prev + 1, 5) to (start + 1, 2)
105+
= (((c6 + c7) + c8) + c9)
106+
107+
Function name: lazy_boolean::nested_mixed
108+
Raw bytes (159): 0x[01, 01, 18, 07, 22, 11, 36, 3b, 11, 09, 0d, 26, 0d, 05, 09, 05, 09, 05, 09, 26, 0d, 05, 09, 09, 0d, 3b, 11, 09, 0d, 3b, 11, 09, 0d, 19, 5f, 1d, 21, 03, 15, 15, 19, 52, 56, 15, 19, 03, 15, 19, 5f, 1d, 21, 13, 01, 31, 01, 01, 10, 03, 04, 09, 00, 0a, 05, 00, 0e, 00, 13, 20, 09, 26, 00, 0e, 00, 13, 26, 00, 17, 00, 1d, 20, 0d, 22, 00, 17, 00, 1d, 3b, 00, 23, 00, 28, 20, 11, 36, 00, 23, 00, 28, 36, 00, 2c, 00, 33, 03, 01, 05, 00, 11, 5b, 03, 09, 00, 0a, 03, 00, 0e, 00, 13, 20, 15, 56, 00, 0e, 00, 13, 15, 00, 17, 00, 1c, 20, 19, 52, 00, 17, 00, 1c, 4f, 00, 22, 00, 28, 20, 1d, 21, 00, 22, 00, 28, 1d, 00, 2c, 00, 33, 5b, 01, 05, 01, 02]
109+
Number of files: 1
110+
- file 0 => global file 1
111+
Number of expressions: 24
112+
- expression 0 operands: lhs = Expression(1, Add), rhs = Expression(8, Sub)
113+
- expression 1 operands: lhs = Counter(4), rhs = Expression(13, Sub)
114+
- expression 2 operands: lhs = Expression(14, Add), rhs = Counter(4)
115+
- expression 3 operands: lhs = Counter(2), rhs = Counter(3)
116+
- expression 4 operands: lhs = Expression(9, Sub), rhs = Counter(3)
117+
- expression 5 operands: lhs = Counter(1), rhs = Counter(2)
118+
- expression 6 operands: lhs = Counter(1), rhs = Counter(2)
119+
- expression 7 operands: lhs = Counter(1), rhs = Counter(2)
120+
- expression 8 operands: lhs = Expression(9, Sub), rhs = Counter(3)
121+
- expression 9 operands: lhs = Counter(1), rhs = Counter(2)
122+
- expression 10 operands: lhs = Counter(2), rhs = Counter(3)
123+
- expression 11 operands: lhs = Expression(14, Add), rhs = Counter(4)
124+
- expression 12 operands: lhs = Counter(2), rhs = Counter(3)
125+
- expression 13 operands: lhs = Expression(14, Add), rhs = Counter(4)
126+
- expression 14 operands: lhs = Counter(2), rhs = Counter(3)
127+
- expression 15 operands: lhs = Counter(6), rhs = Expression(23, Add)
128+
- expression 16 operands: lhs = Counter(7), rhs = Counter(8)
129+
- expression 17 operands: lhs = Expression(0, Add), rhs = Counter(5)
130+
- expression 18 operands: lhs = Counter(5), rhs = Counter(6)
131+
- expression 19 operands: lhs = Expression(20, Sub), rhs = Expression(21, Sub)
132+
- expression 20 operands: lhs = Counter(5), rhs = Counter(6)
133+
- expression 21 operands: lhs = Expression(0, Add), rhs = Counter(5)
134+
- expression 22 operands: lhs = Counter(6), rhs = Expression(23, Add)
135+
- expression 23 operands: lhs = Counter(7), rhs = Counter(8)
136+
Number of file 0 mappings: 19
137+
- Code(Counter(0)) at (prev + 49, 1) to (start + 1, 16)
138+
- Code(Expression(0, Add)) at (prev + 4, 9) to (start + 0, 10)
139+
= ((c4 + ((c2 + c3) - c4)) + ((c1 - c2) - c3))
140+
- Code(Counter(1)) at (prev + 0, 14) to (start + 0, 19)
141+
- Branch { true: Counter(2), false: Expression(9, Sub) } at (prev + 0, 14) to (start + 0, 19)
142+
true = c2
143+
false = (c1 - c2)
144+
- Code(Expression(9, Sub)) at (prev + 0, 23) to (start + 0, 29)
145+
= (c1 - c2)
146+
- Branch { true: Counter(3), false: Expression(8, Sub) } at (prev + 0, 23) to (start + 0, 29)
147+
true = c3
148+
false = ((c1 - c2) - c3)
149+
- Code(Expression(14, Add)) at (prev + 0, 35) to (start + 0, 40)
150+
= (c2 + c3)
151+
- Branch { true: Counter(4), false: Expression(13, Sub) } at (prev + 0, 35) to (start + 0, 40)
152+
true = c4
153+
false = ((c2 + c3) - c4)
154+
- Code(Expression(13, Sub)) at (prev + 0, 44) to (start + 0, 51)
155+
= ((c2 + c3) - c4)
156+
- Code(Expression(0, Add)) at (prev + 1, 5) to (start + 0, 17)
157+
= ((c4 + ((c2 + c3) - c4)) + ((c1 - c2) - c3))
158+
- Code(Expression(22, Add)) at (prev + 3, 9) to (start + 0, 10)
159+
= (c6 + (c7 + c8))
160+
- Code(Expression(0, Add)) at (prev + 0, 14) to (start + 0, 19)
161+
= ((c4 + ((c2 + c3) - c4)) + ((c1 - c2) - c3))
162+
- Branch { true: Counter(5), false: Expression(21, Sub) } at (prev + 0, 14) to (start + 0, 19)
163+
true = c5
164+
false = (((c4 + ((c2 + c3) - c4)) + ((c1 - c2) - c3)) - c5)
165+
- Code(Counter(5)) at (prev + 0, 23) to (start + 0, 28)
166+
- Branch { true: Counter(6), false: Expression(20, Sub) } at (prev + 0, 23) to (start + 0, 28)
167+
true = c6
168+
false = (c5 - c6)
169+
- Code(Expression(19, Add)) at (prev + 0, 34) to (start + 0, 40)
170+
= ((c5 - c6) + (((c4 + ((c2 + c3) - c4)) + ((c1 - c2) - c3)) - c5))
171+
- Branch { true: Counter(7), false: Counter(8) } at (prev + 0, 34) to (start + 0, 40)
172+
true = c7
173+
false = c8
174+
- Code(Counter(7)) at (prev + 0, 44) to (start + 0, 51)
175+
- Code(Expression(22, Add)) at (prev + 1, 5) to (start + 1, 2)
176+
= (c6 + (c7 + c8))
177+
+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
LL| |#![feature(coverage_attribute)]
2+
LL| |//@ edition: 2021
3+
LL| |//@ compile-flags: -Zcoverage-options=branch
4+
LL| |//@ llvm-cov-flags: --show-branches=count
5+
LL| |
6+
LL| |// Tests for branch coverage of the lazy boolean operators `&&` and `||`,
7+
LL| |// as ordinary expressions that aren't part of an `if` condition or similar.
8+
LL| |
9+
LL| |use core::hint::black_box;
10+
LL| |
11+
LL| |// Helper macro to prevent start-of-function spans from being merged into
12+
LL| |// spans on the lines we care about.
13+
LL| |macro_rules! no_merge {
14+
LL| | () => {
15+
LL| | for _ in 0..1 {}
16+
LL| | };
17+
LL| |}
18+
LL| |
19+
LL| 15|fn branch_and(a: bool, b: bool) {
20+
LL| 15| no_merge!();
21+
LL| |
22+
LL| | // |13 |18 (no branch)
23+
LL| 15| let c = a && b;
24+
^12
25+
------------------
26+
| Branch (LL:13): [True: 12, False: 3]
27+
------------------
28+
LL| 15| black_box(c);
29+
LL| 15|}
30+
LL| |
31+
LL| 15|fn branch_or(a: bool, b: bool) {
32+
LL| 15| no_merge!();
33+
LL| |
34+
LL| | // |13 |18 (no branch)
35+
LL| 15| let c = a || b;
36+
^3
37+
------------------
38+
| Branch (LL:13): [True: 12, False: 3]
39+
------------------
40+
LL| 15| black_box(c);
41+
LL| 15|}
42+
LL| |
43+
LL| |// Test for chaining one operator several times.
44+
LL| 16|fn chain(x: u32) {
45+
LL| 16| no_merge!();
46+
LL| |
47+
LL| | // |13 |22 |31 |40 (no branch)
48+
LL| 16| let c = x > 1 && x > 2 && x > 4 && x > 8;
49+
^14 ^13 ^11
50+
------------------
51+
| Branch (LL:13): [True: 14, False: 2]
52+
| Branch (LL:22): [True: 13, False: 1]
53+
| Branch (LL:31): [True: 11, False: 2]
54+
------------------
55+
LL| 16| black_box(c);
56+
LL| |
57+
LL| | // |13 |22 |31 |40 (no branch)
58+
LL| 16| let d = x < 1 || x < 2 || x < 4 || x < 8;
59+
^15 ^14 ^12
60+
------------------
61+
| Branch (LL:13): [True: 1, False: 15]
62+
| Branch (LL:22): [True: 1, False: 14]
63+
| Branch (LL:31): [True: 2, False: 12]
64+
------------------
65+
LL| 16| black_box(d);
66+
LL| 16|}
67+
LL| |
68+
LL| |// Test for nested combinations of different operators.
69+
LL| 16|fn nested_mixed(x: u32) {
70+
LL| 16| no_merge!();
71+
LL| |
72+
LL| | // |14 |23 |35 |44 (no branch)
73+
LL| 16| let c = (x < 4 || x >= 9) && (x < 2 || x >= 10);
74+
^12 ^11 ^9
75+
------------------
76+
| Branch (LL:14): [True: 4, False: 12]
77+
| Branch (LL:23): [True: 7, False: 5]
78+
| Branch (LL:35): [True: 2, False: 9]
79+
------------------
80+
LL| 16| black_box(c);
81+
LL| |
82+
LL| | // |14 |23 |34 |44 (no branch)
83+
LL| 16| let d = (x < 4 && x < 1) || (x >= 8 && x >= 10);
84+
^4 ^15 ^8
85+
------------------
86+
| Branch (LL:14): [True: 4, False: 12]
87+
| Branch (LL:23): [True: 1, False: 3]
88+
| Branch (LL:34): [True: 8, False: 7]
89+
------------------
90+
LL| 16| black_box(d);
91+
LL| 16|}
92+
LL| |
93+
LL| |#[coverage(off)]
94+
LL| |fn main() {
95+
LL| | // Use each set of arguments (2^n) times, so that each combination has a
96+
LL| | // unique sum, and we can use those sums to verify expected control flow.
97+
LL| | // 1x (false, false)
98+
LL| | // 2x (false, true)
99+
LL| | // 4x (true, false)
100+
LL| | // 8x (true, true)
101+
LL| | for a in [false, true, true, true, true] {
102+
LL| | for b in [false, true, true] {
103+
LL| | branch_and(a, b);
104+
LL| | branch_or(a, b);
105+
LL| | }
106+
LL| | }
107+
LL| |
108+
LL| | for x in 0..16 {
109+
LL| | chain(x);
110+
LL| | nested_mixed(x);
111+
LL| | }
112+
LL| |}
113+

tests/coverage/branch/lazy-boolean.rs

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#![feature(coverage_attribute)]
2+
//@ edition: 2021
3+
//@ compile-flags: -Zcoverage-options=branch
4+
//@ llvm-cov-flags: --show-branches=count
5+
6+
// Tests for branch coverage of the lazy boolean operators `&&` and `||`,
7+
// as ordinary expressions that aren't part of an `if` condition or similar.
8+
9+
use core::hint::black_box;
10+
11+
// Helper macro to prevent start-of-function spans from being merged into
12+
// spans on the lines we care about.
13+
macro_rules! no_merge {
14+
() => {
15+
for _ in 0..1 {}
16+
};
17+
}
18+
19+
fn branch_and(a: bool, b: bool) {
20+
no_merge!();
21+
22+
// |13 |18 (no branch)
23+
let c = a && b;
24+
black_box(c);
25+
}
26+
27+
fn branch_or(a: bool, b: bool) {
28+
no_merge!();
29+
30+
// |13 |18 (no branch)
31+
let c = a || b;
32+
black_box(c);
33+
}
34+
35+
// Test for chaining one operator several times.
36+
fn chain(x: u32) {
37+
no_merge!();
38+
39+
// |13 |22 |31 |40 (no branch)
40+
let c = x > 1 && x > 2 && x > 4 && x > 8;
41+
black_box(c);
42+
43+
// |13 |22 |31 |40 (no branch)
44+
let d = x < 1 || x < 2 || x < 4 || x < 8;
45+
black_box(d);
46+
}
47+
48+
// Test for nested combinations of different operators.
49+
fn nested_mixed(x: u32) {
50+
no_merge!();
51+
52+
// |14 |23 |35 |44 (no branch)
53+
let c = (x < 4 || x >= 9) && (x < 2 || x >= 10);
54+
black_box(c);
55+
56+
// |14 |23 |34 |44 (no branch)
57+
let d = (x < 4 && x < 1) || (x >= 8 && x >= 10);
58+
black_box(d);
59+
}
60+
61+
#[coverage(off)]
62+
fn main() {
63+
// Use each set of arguments (2^n) times, so that each combination has a
64+
// unique sum, and we can use those sums to verify expected control flow.
65+
// 1x (false, false)
66+
// 2x (false, true)
67+
// 4x (true, false)
68+
// 8x (true, true)
69+
for a in [false, true, true, true, true] {
70+
for b in [false, true, true] {
71+
branch_and(a, b);
72+
branch_or(a, b);
73+
}
74+
}
75+
76+
for x in 0..16 {
77+
chain(x);
78+
nested_mixed(x);
79+
}
80+
}

0 commit comments

Comments
 (0)