| author | |
| committer | |
| log | 64061cc1bfb8e925d20f5824aa178b61eb2e5f11 |
| tree | 0a295c4828194b9569600d956f1373de615cdab3 |
| parent | 4a1f0e141893fe56f540709c8fb12b8b8dc22218 |
2 files changed, 75 insertions(+), 0 deletions(-)
test/cases/switch.zig+37| ... | @@ -232,3 +232,40 @@ test "capture value of switch with all unreachable prongs" { | ... | @@ -232,3 +232,40 @@ test "capture value of switch with all unreachable prongs" { |
| 232 | }; | 232 | }; |
| 233 | assert(x == 1); | 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 | } |
test/compile_errors.zig+38| ... | @@ -1,6 +1,44 @@ | ... | @@ -1,6 +1,44 @@ |
| 1 | const tests = @import("tests.zig"); | 1 | const tests = @import("tests.zig"); |
| 2 | 2 | ||
| 3 | pub fn addCases(cases: *tests.CompileErrorContext) void { | 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 | cases.add( | 42 | cases.add( |
| 5 | "reading past end of pointer casted array", | 43 | "reading past end of pointer casted array", |
| 6 | \\comptime { | 44 | \\comptime { |