authorgravatar for justin.b.alexander1@gmail.comalexander <justin.b.alexander1@gmail.com> 2018-12-27 13:46:32-06:00
committergravatar for justin.b.alexander1@gmail.comalexander <justin.b.alexander1@gmail.com> 2018-12-27 13:46:32-06:00
log64061cc1bfb8e925d20f5824aa178b61eb2e5f11
tree0a295c4828194b9569600d956f1373de615cdab3
parent4a1f0e141893fe56f540709c8fb12b8b8dc22218

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


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" {
232232 };
233233 assert(x == 1);
234234}
235
236test "switching on booleans" {
237 testSwitchOnBools();
238 comptime testSwitchOnBools();
239}
240
241fn 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
252fn testSwitchOnBoolsTrueAndFalse(x: bool) bool {
253 return switch (x) {
254 true => false,
255 false => true,
256 };
257}
258
259fn testSwitchOnBoolsTrueWithElse(x: bool) bool {
260 return switch (x) {
261 true => false,
262 else => true,
263 };
264}
265
266fn 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 @@
11const tests = @import("tests.zig");
22
33pub 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
442 cases.add(
543 "reading past end of pointer casted array",
644 \\comptime {