diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2019-01-01 18:36:12 -0500 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2019-01-01 18:36:12 -0500 |
| commit | 036a49e97d0bc556ece08562c9dd3c75879598cf (patch) | |
| tree | 9707943a5c063fe196bd091b801c5c626f3f0eb8 /test | |
| parent | c0e391e94ab34cb2fcfd85c5c9f93aed8ec1ba79 (diff) | |
| parent | 6df8e4bca73309f2e340dbfa9031f1bb16a73bcc (diff) | |
| download | zig-036a49e97d0bc556ece08562c9dd3c75879598cf.tar.gz zig-036a49e97d0bc556ece08562c9dd3c75879598cf.zip | |
Merge remote-tracking branch 'origin/master' into llvm8
Diffstat (limited to 'test')
| -rw-r--r-- | test/cases/switch.zig | 37 | ||||
| -rw-r--r-- | test/compile_errors.zig | 38 |
2 files changed, 75 insertions, 0 deletions
diff --git a/test/cases/switch.zig b/test/cases/switch.zig index d5258f0bb1..1162fdd4b2 100644 --- a/test/cases/switch.zig +++ b/test/cases/switch.zig @@ -232,3 +232,40 @@ test "capture value of switch with all unreachable prongs" { }; assert(x == 1); } + +test "switching on booleans" { + testSwitchOnBools(); + comptime testSwitchOnBools(); +} + +fn testSwitchOnBools() void { + assert(testSwitchOnBoolsTrueAndFalse(true) == false); + assert(testSwitchOnBoolsTrueAndFalse(false) == true); + + assert(testSwitchOnBoolsTrueWithElse(true) == false); + assert(testSwitchOnBoolsTrueWithElse(false) == true); + + assert(testSwitchOnBoolsFalseWithElse(true) == false); + assert(testSwitchOnBoolsFalseWithElse(false) == true); +} + +fn testSwitchOnBoolsTrueAndFalse(x: bool) bool { + return switch (x) { + true => false, + false => true, + }; +} + +fn testSwitchOnBoolsTrueWithElse(x: bool) bool { + return switch (x) { + true => false, + else => true, + }; +} + +fn testSwitchOnBoolsFalseWithElse(x: bool) bool { + return switch (x) { + false => true, + else => false, + }; +} diff --git a/test/compile_errors.zig b/test/compile_errors.zig index ee3741ee6b..880a96a322 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -2,6 +2,44 @@ const tests = @import("tests.zig"); pub fn addCases(cases: *tests.CompileErrorContext) void { cases.add( + "duplicate boolean switch value", + \\comptime { + \\ const x = switch (true) { + \\ true => false, + \\ false => true, + \\ true => false, + \\ }; + \\} + \\comptime { + \\ const x = switch (true) { + \\ false => true, + \\ true => false, + \\ false => true, + \\ }; + \\} + , + ".tmp_source.zig:5:9: error: duplicate switch value", + ".tmp_source.zig:12:9: error: duplicate switch value", + ); + + cases.add( + "missing boolean switch value", + \\comptime { + \\ const x = switch (true) { + \\ true => false, + \\ }; + \\} + \\comptime { + \\ const x = switch (true) { + \\ false => true, + \\ }; + \\} + , + ".tmp_source.zig:2:15: error: switch must handle all possibilities", + ".tmp_source.zig:7:15: error: switch must handle all possibilities", + ); + + cases.add( "reading past end of pointer casted array", \\comptime { \\ const array = "aoeu"; |
