aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/globals.zig
blob: 982ebc41f839ecbf7a28289be2fe38ab3c474acd (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const std = @import("std");
const builtin = @import("builtin");
const expect = std.testing.expect;

var pos = [2]f32{ 0.0, 0.0 };
test "store to global array" {
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;

    try expect(pos[1] == 0.0);
    pos = [2]f32{ 0.0, 1.0 };
    try expect(pos[1] == 1.0);
}

var vpos = @Vector(2, f32){ 0.0, 0.0 };
test "store to global vector" {
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;

    try expect(vpos[1] == 0.0);
    vpos = @Vector(2, f32){ 0.0, 1.0 };
    try expect(vpos[1] == 1.0);
}

test "slices pointing at the same address as global array." {
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;

    const S = struct {
        const a = [_]u8{ 1, 2, 3 };

        fn checkAddress(s: []const u8) !void {
            for (s, 0..) |*i, j| {
                try expect(i == &a[j]);
            }
        }
    };

    try S.checkAddress(&S.a);
    try comptime S.checkAddress(&S.a);
}

test "global loads can affect liveness" {
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;

    const S = struct {
        const ByRef = struct {
            a: u32,
        };

        var global_ptr: *ByRef = undefined;

        fn f() void {
            global_ptr.* = .{ .a = 42 };
        }
    };

    var x: S.ByRef = .{ .a = 1 };
    S.global_ptr = &x;
    const y = x;
    S.f();
    try std.testing.expect(y.a == 1);
}

test "global const can be self-referential" {
    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;

    const S = struct {
        self: *const @This(),
        x: u32,

        const foo: @This() = .{ .self = &foo, .x = 123 };
    };

    try std.testing.expect(S.foo.x == 123);
    try std.testing.expect(S.foo.self.x == 123);
    try std.testing.expect(S.foo.self.self.x == 123);
    try std.testing.expect(S.foo.self == &S.foo);
    try std.testing.expect(S.foo.self.self == &S.foo);
}

test "global var can be self-referential" {
    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;

    const S = struct {
        self: *@This(),
        x: u32,

        var foo: @This() = .{ .self = &foo, .x = undefined };
    };

    S.foo.x = 123;

    try std.testing.expect(S.foo.x == 123);
    try std.testing.expect(S.foo.self.x == 123);
    try std.testing.expect(S.foo.self == &S.foo);

    S.foo.self.x = 456;

    try std.testing.expect(S.foo.x == 456);
    try std.testing.expect(S.foo.self.x == 456);
    try std.testing.expect(S.foo.self == &S.foo);

    S.foo.self.self.x = 789;

    try std.testing.expect(S.foo.x == 789);
    try std.testing.expect(S.foo.self.x == 789);
    try std.testing.expect(S.foo.self == &S.foo);
}

test "global const can be indirectly self-referential" {
    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;

    const S = struct {
        other: *const @This(),
        x: u32,

        const foo: @This() = .{ .other = &bar, .x = 123 };
        const bar: @This() = .{ .other = &foo, .x = 456 };
    };

    try std.testing.expect(S.foo.x == 123);
    try std.testing.expect(S.foo.other.x == 456);
    try std.testing.expect(S.foo.other.other.x == 123);
    try std.testing.expect(S.foo.other.other.other.x == 456);
    try std.testing.expect(S.foo.other == &S.bar);
    try std.testing.expect(S.foo.other.other == &S.foo);

    try std.testing.expect(S.bar.x == 456);
    try std.testing.expect(S.bar.other.x == 123);
    try std.testing.expect(S.bar.other.other.x == 456);
    try std.testing.expect(S.bar.other.other.other.x == 123);
    try std.testing.expect(S.bar.other == &S.foo);
    try std.testing.expect(S.bar.other.other == &S.bar);
}

test "global var can be indirectly self-referential" {
    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;

    const S = struct {
        other: *@This(),
        x: u32,

        var foo: @This() = .{ .other = &bar, .x = undefined };
        var bar: @This() = .{ .other = &foo, .x = undefined };
    };

    S.foo.other.x = 123; // bar.x
    S.foo.other.other.x = 456; // foo.x

    try std.testing.expect(S.foo.x == 456);
    try std.testing.expect(S.foo.other.x == 123);
    try std.testing.expect(S.foo.other.other.x == 456);
    try std.testing.expect(S.foo.other.other.other.x == 123);
    try std.testing.expect(S.foo.other == &S.bar);
    try std.testing.expect(S.foo.other.other == &S.foo);

    S.bar.other.x = 111; // foo.x
    S.bar.other.other.x = 222; // bar.x

    try std.testing.expect(S.bar.x == 222);
    try std.testing.expect(S.bar.other.x == 111);
    try std.testing.expect(S.bar.other.other.x == 222);
    try std.testing.expect(S.bar.other.other.other.x == 111);
    try std.testing.expect(S.bar.other == &S.foo);
    try std.testing.expect(S.bar.other.other == &S.bar);
}

pub const Callbacks = extern struct {
    key_callback: *const fn (key: i32) callconv(.c) i32,
};

var callbacks: Callbacks = undefined;
var callbacks_loaded: bool = false;

test "function pointer field call on global extern struct, conditional on global" {
    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;

    if (callbacks_loaded) {
        try std.testing.expectEqual(42, callbacks.key_callback(42));
    }
}

test "function pointer field call on global extern struct" {
    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;

    const S = struct {
        fn keyCallback(key: i32) callconv(.c) i32 {
            return key;
        }
    };

    callbacks = Callbacks{ .key_callback = S.keyCallback };
    try std.testing.expectEqual(42, callbacks.key_callback(42));
}