Skip to content

Commit 3957f30

Browse files
committed
Add void_cast test
1 parent b474ede commit 3957f30

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

tests/unit/void_cast.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <cassert>
2+
3+
void unused_param(int x) { (void)x; }
4+
5+
int side_effect_counter = 0;
6+
int bump_and_return() {
7+
++side_effect_counter;
8+
return side_effect_counter;
9+
}
10+
11+
struct Holder {
12+
int field;
13+
};
14+
15+
int main() {
16+
unused_param(42);
17+
18+
int y = 5;
19+
(void)y;
20+
21+
int z = ((void)y, 7);
22+
assert(z == 7);
23+
24+
int counter = 0;
25+
int w = ((void)counter, counter = 3, counter);
26+
assert(w == 3);
27+
assert(counter == 3);
28+
29+
(void)bump_and_return();
30+
assert(side_effect_counter == 1);
31+
32+
int v = ((void)bump_and_return(), 99);
33+
assert(side_effect_counter == 2);
34+
assert(v == 99);
35+
36+
(void)0;
37+
(void)(0);
38+
39+
(void)(y);
40+
41+
((void)0);
42+
((void)(y));
43+
44+
int err = 0;
45+
((void)(err = 42));
46+
assert(err == 42);
47+
48+
int chosen = ((void)(err = 7), 123);
49+
assert(err == 7);
50+
assert(chosen == 123);
51+
52+
(void)bump_and_return;
53+
assert(side_effect_counter == 2);
54+
55+
(void)(&bump_and_return);
56+
assert(side_effect_counter == 2);
57+
58+
(void)(static_cast<int (*)()>(&bump_and_return));
59+
assert(side_effect_counter == 2);
60+
61+
int storage = 11;
62+
int *p = &storage;
63+
(void)(*p);
64+
65+
int arr[] = {1, 2, 3};
66+
(void)(arr[1]);
67+
68+
Holder h{17};
69+
(void)(h.field);
70+
Holder *hp = &h;
71+
(void)(hp->field);
72+
73+
return 0;
74+
}

0 commit comments

Comments
 (0)