zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 64061cc1bfb8e925d20f5824aa178b61eb2e5f11 (tree)
parent 4a1f0e141893fe56f540709c8fb12b8b8dc22218
Author: alexander <justin.b.alexander1@gmail.com>
Date:   Thu, 27 Dec 2018 13:46:32 -0600

Test cases for compiler error and working behavior for switching on booleans

Diffstat:
Mtest/cases/switch.zig | 37+++++++++++++++++++++++++++++++++++++
Mtest/compile_errors.zig | 38++++++++++++++++++++++++++++++++++++++
2 files changed, 75 insertions(+), 0 deletions(-)

diff --git 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 @@ -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";