File tree 2 files changed +75
-0
lines changed
2 files changed +75
-0
lines changed Original file line number Diff line number Diff line change @@ -232,3 +232,40 @@ test "capture value of switch with all unreachable prongs" {
232
232
};
233
233
assert (x == 1 );
234
234
}
235
+
236
+ test "switching on booleans" {
237
+ testSwitchOnBools ();
238
+ comptime testSwitchOnBools ();
239
+ }
240
+
241
+ fn testSwitchOnBools () void {
242
+ assert (testSwitchOnBoolsTrueAndFalse (true ) == false );
243
+ assert (testSwitchOnBoolsTrueAndFalse (false ) == true );
244
+
245
+ assert (testSwitchOnBoolsTrueWithElse (true ) == false );
246
+ assert (testSwitchOnBoolsTrueWithElse (false ) == true );
247
+
248
+ assert (testSwitchOnBoolsFalseWithElse (true ) == false );
249
+ assert (testSwitchOnBoolsFalseWithElse (false ) == true );
250
+ }
251
+
252
+ fn testSwitchOnBoolsTrueAndFalse (x : bool ) bool {
253
+ return switch (x ) {
254
+ true = > false ,
255
+ false = > true ,
256
+ };
257
+ }
258
+
259
+ fn testSwitchOnBoolsTrueWithElse (x : bool ) bool {
260
+ return switch (x ) {
261
+ true = > false ,
262
+ else = > true ,
263
+ };
264
+ }
265
+
266
+ fn testSwitchOnBoolsFalseWithElse (x : bool ) bool {
267
+ return switch (x ) {
268
+ false = > true ,
269
+ else = > false ,
270
+ };
271
+ }
Original file line number Diff line number Diff line change 1
1
const tests = @import ("tests.zig" );
2
2
3
3
pub fn addCases (cases : * tests.CompileErrorContext ) void {
4
+ cases .add (
5
+ "duplicate boolean switch value" ,
6
+ \\comptime {
7
+ \\ const x = switch (true) {
8
+ \\ true => false,
9
+ \\ false => true,
10
+ \\ true => false,
11
+ \\ };
12
+ \\}
13
+ \\comptime {
14
+ \\ const x = switch (true) {
15
+ \\ false => true,
16
+ \\ true => false,
17
+ \\ false => true,
18
+ \\ };
19
+ \\}
20
+ ,
21
+ ".tmp_source.zig:5:9: error: duplicate switch value" ,
22
+ ".tmp_source.zig:12:9: error: duplicate switch value" ,
23
+ );
24
+
25
+ cases .add (
26
+ "missing boolean switch value" ,
27
+ \\comptime {
28
+ \\ const x = switch (true) {
29
+ \\ true => false,
30
+ \\ };
31
+ \\}
32
+ \\comptime {
33
+ \\ const x = switch (true) {
34
+ \\ false => true,
35
+ \\ };
36
+ \\}
37
+ ,
38
+ ".tmp_source.zig:2:15: error: switch must handle all possibilities" ,
39
+ ".tmp_source.zig:7:15: error: switch must handle all possibilities" ,
40
+ );
41
+
4
42
cases .add (
5
43
"reading past end of pointer casted array" ,
6
44
\\comptime {
You can’t perform that action at this time.
0 commit comments