aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/empty_union.zig
blob: f05feacfafd7e429ff01aa3c65e376acaba63441 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const builtin = @import("builtin");
const std = @import("std");
const expect = std.testing.expect;

test "switch on empty enum" {
    const E = enum {};
    var e: E = undefined;
    _ = &e;
    switch (e) {}
}

test "switch on empty enum with a specified tag type" {
    const E = enum(u8) {};
    var e: E = undefined;
    _ = &e;
    switch (e) {}
}

test "switch on empty auto numbered tagged union" {
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO

    const U = union(enum(u8)) {};
    var u: U = undefined;
    _ = &u;
    switch (u) {}
}

test "switch on empty tagged union" {
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO

    const E = enum {};
    const U = union(E) {};
    var u: U = undefined;
    _ = &u;
    switch (u) {}
}

test "empty union" {
    const U = union {};
    try expect(@sizeOf(U) == 0);
    try expect(@alignOf(U) == 1);
}

test "empty extern union" {
    const U = extern union {};
    try expect(@sizeOf(U) == 0);
    try expect(@alignOf(U) == 1);
}

test "empty union passed as argument" {
    const U = union(enum) {
        fn f(u: @This()) void {
            switch (u) {}
        }
    };
    U.f(@as(U, undefined));
}

test "empty enum passed as argument" {
    const E = enum {
        fn f(e: @This()) void {
            switch (e) {}
        }
    };
    E.f(@as(E, undefined));
}