aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/field_parent_ptr.zig
blob: 6e3eda9032cad54cc3ddf931aac848f352972339 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const expect = @import("std").testing.expect;
const builtin = @import("builtin");

test "@fieldParentPtr non-first field" {
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;

    try testParentFieldPtr(&foo.c);
    try comptime testParentFieldPtr(&foo.c);
}

test "@fieldParentPtr first field" {
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;

    try testParentFieldPtrFirst(&foo.a);
    try comptime testParentFieldPtrFirst(&foo.a);
}

const Foo = struct {
    a: bool,
    b: f32,
    c: i32,
    d: i32,
};

const foo = Foo{
    .a = true,
    .b = 0.123,
    .c = 1234,
    .d = -10,
};

fn testParentFieldPtr(c: *const i32) !void {
    try expect(c == &foo.c);

    const base = @fieldParentPtr(Foo, "c", c);
    try expect(base == &foo);
    try expect(&base.c == c);
}

fn testParentFieldPtrFirst(a: *const bool) !void {
    try expect(a == &foo.a);

    const base = @fieldParentPtr(Foo, "a", a);
    try expect(base == &foo);
    try expect(&base.a == a);
}

test "@fieldParentPtr untagged union" {
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;

    try testFieldParentPtrUnion(&bar.c);
    try comptime testFieldParentPtrUnion(&bar.c);
}

const Bar = union(enum) {
    a: bool,
    b: f32,
    c: i32,
    d: i32,
};

const bar = Bar{ .c = 42 };

fn testFieldParentPtrUnion(c: *const i32) !void {
    try expect(c == &bar.c);

    const base = @fieldParentPtr(Bar, "c", c);
    try expect(base == &bar);
    try expect(&base.c == c);
}

test "@fieldParentPtr tagged union" {
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;

    try testFieldParentPtrTaggedUnion(&bar_tagged.c);
    try comptime testFieldParentPtrTaggedUnion(&bar_tagged.c);
}

const BarTagged = union(enum) {
    a: bool,
    b: f32,
    c: i32,
    d: i32,
};

const bar_tagged = BarTagged{ .c = 42 };

fn testFieldParentPtrTaggedUnion(c: *const i32) !void {
    try expect(c == &bar_tagged.c);

    const base = @fieldParentPtr(BarTagged, "c", c);
    try expect(base == &bar_tagged);
    try expect(&base.c == c);
}

test "@fieldParentPtr extern union" {
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;

    try testFieldParentPtrExternUnion(&bar_extern.c);
    try comptime testFieldParentPtrExternUnion(&bar_extern.c);
}

const BarExtern = extern union {
    a: bool,
    b: f32,
    c: i32,
    d: i32,
};

const bar_extern = BarExtern{ .c = 42 };

fn testFieldParentPtrExternUnion(c: *const i32) !void {
    try expect(c == &bar_extern.c);

    const base = @fieldParentPtr(BarExtern, "c", c);
    try expect(base == &bar_extern);
    try expect(&base.c == c);
}