diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2022-01-25 23:29:02 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2022-01-26 00:36:12 -0700 |
| commit | 2c9a5e791ba17987061b057083c99158e85f17d1 (patch) | |
| tree | 5aaf5166a35a6ed909eecf0a916bacf69cad5ea9 /test/behavior/enum.zig | |
| parent | 178cd60a5ef590c3b94d607690fa773cf13b93ce (diff) | |
| download | zig-2c9a5e791ba17987061b057083c99158e85f17d1.tar.gz zig-2c9a5e791ba17987061b057083c99158e85f17d1.zip | |
organize behavior tests
Every test that is moved in this commit has been checked to see if it is
now passing.
Diffstat (limited to 'test/behavior/enum.zig')
| -rw-r--r-- | test/behavior/enum.zig | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/test/behavior/enum.zig b/test/behavior/enum.zig index d044619edf..0a0d7e5a15 100644 --- a/test/behavior/enum.zig +++ b/test/behavior/enum.zig @@ -1,3 +1,4 @@ +const builtin = @import("builtin"); const std = @import("std"); const expect = std.testing.expect; const mem = std.mem; @@ -870,3 +871,104 @@ test "method call on an enum" { try S.doTheTest(); comptime try S.doTheTest(); } + +test "enum value allocation" { + if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO + + const LargeEnum = enum(u32) { + A0 = 0x80000000, + A1, + A2, + }; + + try expect(@enumToInt(LargeEnum.A0) == 0x80000000); + try expect(@enumToInt(LargeEnum.A1) == 0x80000001); + try expect(@enumToInt(LargeEnum.A2) == 0x80000002); +} + +test "enum literal casting to tagged union" { + const Arch = union(enum) { + x86_64, + arm: Arm32, + + const Arm32 = enum { + v8_5a, + v8_4a, + }; + }; + + var t = true; + var x: Arch = .x86_64; + var y = if (t) x else .x86_64; + switch (y) { + .x86_64 => {}, + else => @panic("fail"), + } +} + +const Bar = enum { A, B, C, D }; + +test "enum literal casting to error union with payload enum" { + if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO + + var bar: error{B}!Bar = undefined; + bar = .B; // should never cast to the error set + + try expect((try bar) == Bar.B); +} + +test "exporting enum type and value" { + if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO + + const S = struct { + const E = enum(c_int) { one, two }; + comptime { + @export(E, .{ .name = "E" }); + } + const e: E = .two; + comptime { + @export(e, .{ .name = "e" }); + } + }; + try expect(S.e == .two); +} + +test "constant enum initialization with differing sizes" { + if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO + + try test3_1(test3_foo); + try test3_2(test3_bar); +} +const Test3Foo = union(enum) { + One: void, + Two: f32, + Three: Test3Point, +}; +const Test3Point = struct { + x: i32, + y: i32, +}; +const test3_foo = Test3Foo{ + .Three = Test3Point{ + .x = 3, + .y = 4, + }, +}; +const test3_bar = Test3Foo{ .Two = 13 }; +fn test3_1(f: Test3Foo) !void { + switch (f) { + Test3Foo.Three => |pt| { + try expect(pt.x == 3); + try expect(pt.y == 4); + }, + else => unreachable, + } +} +fn test3_2(f: Test3Foo) !void { + switch (f) { + Test3Foo.Two => |x| { + try expect(x == 13); + }, + else => unreachable, + } +} |
