authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-27 12:41:24-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-27 12:45:04-07:00
log24d9438bccde57a838df861aba394077b0c2a5cc
tree7d42c376274932f2d8e975d8b1afb5a44ac2d50c
parent9f0fd72321bf04654042a2191274cb31ba8f9110

split export behavior test into export_keyword and export_builtin


4 files changed, 128 insertions(+), 96 deletions(-)

test/behavior.zig+2-1
......@@ -161,6 +161,7 @@ test {
161161 _ = @import("behavior/enum.zig");
162162 _ = @import("behavior/error.zig");
163163 _ = @import("behavior/eval.zig");
164 _ = @import("behavior/export_builtin.zig");
164165 _ = @import("behavior/export_self_referential_type_info.zig");
165166 _ = @import("behavior/field_parent_ptr.zig");
166167 _ = @import("behavior/floatop.zig");
......@@ -254,6 +255,6 @@ test {
254255 builtin.zig_backend != .stage2_c and
255256 builtin.zig_backend != .stage2_spirv64)
256257 {
257 _ = @import("behavior/export.zig");
258 _ = @import("behavior/export_keyword.zig");
258259 }
259260}
test/behavior/export.zig deleted-95
......@@ -1,95 +0,0 @@
1const std = @import("std");
2const expect = std.testing.expect;
3const expectEqualSlices = std.testing.expectEqualSlices;
4const expectEqualStrings = std.testing.expectEqualStrings;
5const mem = std.mem;
6const builtin = @import("builtin");
7
8// can't really run this test but we can make sure it has no compile error
9// and generates code
10const vram = @as([*]volatile u8, @ptrFromInt(0x20000000))[0..0x8000];
11export fn writeToVRam() void {
12 vram[0] = 'X';
13}
14
15const PackedStruct = packed struct {
16 a: u8,
17 b: u8,
18};
19const PackedUnion = packed union {
20 a: u8,
21 b: u32,
22};
23
24test "packed struct, enum, union parameters in extern function" {
25 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
26
27 testPackedStuff(&(PackedStruct{
28 .a = 1,
29 .b = 2,
30 }), &(PackedUnion{ .a = 1 }));
31}
32
33export fn testPackedStuff(a: *const PackedStruct, b: *const PackedUnion) void {
34 if (false) {
35 a;
36 b;
37 }
38}
39
40test "exporting enum type and value" {
41 const S = struct {
42 const E = enum(c_int) { one, two };
43 const e: E = .two;
44 comptime {
45 @export(e, .{ .name = "e" });
46 }
47 };
48 try expect(S.e == .two);
49}
50
51test "exporting with internal linkage" {
52 const S = struct {
53 fn foo() callconv(.C) void {}
54 comptime {
55 @export(foo, .{ .name = "exporting_with_internal_linkage_foo", .linkage = .Internal });
56 }
57 };
58 S.foo();
59}
60
61test "exporting using field access" {
62 const S = struct {
63 const Inner = struct {
64 const x: u32 = 5;
65 };
66 comptime {
67 @export(Inner.x, .{ .name = "foo", .linkage = .Internal });
68 }
69 };
70
71 _ = S.Inner.x;
72}
73
74test "exporting comptime-known value" {
75 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
76
77 const x: u32 = 10;
78 @export(x, .{ .name = "exporting_comptime_known_value_foo" });
79 const S = struct {
80 extern const exporting_comptime_known_value_foo: u32;
81 };
82 try expect(S.exporting_comptime_known_value_foo == 10);
83}
84
85test "exporting comptime var" {
86 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
87
88 comptime var x: u32 = 5;
89 @export(x, .{ .name = "exporting_comptime_var_foo" });
90 x = 7; // modifying this now shouldn't change anything
91 const S = struct {
92 extern const exporting_comptime_var_foo: u32;
93 };
94 try expect(S.exporting_comptime_var_foo == 5);
95}
test/behavior/export_builtin.zig created+88
......@@ -0,0 +1,88 @@
1const builtin = @import("builtin");
2const std = @import("std");
3const expect = std.testing.expect;
4
5test "exporting enum type and value" {
6 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
7 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
8 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
9 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
10 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
11
12 const S = struct {
13 const E = enum(c_int) { one, two };
14 const e: E = .two;
15 comptime {
16 @export(e, .{ .name = "e" });
17 }
18 };
19 try expect(S.e == .two);
20}
21
22test "exporting with internal linkage" {
23 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
24 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
25 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
26 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
27 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
28
29 const S = struct {
30 fn foo() callconv(.C) void {}
31 comptime {
32 @export(foo, .{ .name = "exporting_with_internal_linkage_foo", .linkage = .Internal });
33 }
34 };
35 S.foo();
36}
37
38test "exporting using field access" {
39 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
40 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
41 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
42 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
43 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
44
45 const S = struct {
46 const Inner = struct {
47 const x: u32 = 5;
48 };
49 comptime {
50 @export(Inner.x, .{ .name = "foo", .linkage = .Internal });
51 }
52 };
53
54 _ = S.Inner.x;
55}
56
57test "exporting comptime-known value" {
58 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
59 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
60 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
61 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
62 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
63 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
64
65 const x: u32 = 10;
66 @export(x, .{ .name = "exporting_comptime_known_value_foo" });
67 const S = struct {
68 extern const exporting_comptime_known_value_foo: u32;
69 };
70 try expect(S.exporting_comptime_known_value_foo == 10);
71}
72
73test "exporting comptime var" {
74 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
75 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
76 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
77 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
78 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
79 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
80
81 comptime var x: u32 = 5;
82 @export(x, .{ .name = "exporting_comptime_var_foo" });
83 x = 7; // modifying this now shouldn't change anything
84 const S = struct {
85 extern const exporting_comptime_var_foo: u32;
86 };
87 try expect(S.exporting_comptime_var_foo == 5);
88}
test/behavior/export_keyword.zig created+38
......@@ -0,0 +1,38 @@
1const std = @import("std");
2const expect = std.testing.expect;
3const expectEqualSlices = std.testing.expectEqualSlices;
4const expectEqualStrings = std.testing.expectEqualStrings;
5const mem = std.mem;
6const builtin = @import("builtin");
7
8// can't really run this test but we can make sure it has no compile error
9// and generates code
10const vram = @as([*]volatile u8, @ptrFromInt(0x20000000))[0..0x8000];
11export fn writeToVRam() void {
12 vram[0] = 'X';
13}
14
15const PackedStruct = packed struct {
16 a: u8,
17 b: u8,
18};
19const PackedUnion = packed union {
20 a: u8,
21 b: u32,
22};
23
24test "packed struct, enum, union parameters in extern function" {
25 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
26
27 testPackedStuff(&(PackedStruct{
28 .a = 1,
29 .b = 2,
30 }), &(PackedUnion{ .a = 1 }));
31}
32
33export fn testPackedStuff(a: *const PackedStruct, b: *const PackedUnion) void {
34 if (false) {
35 a;
36 b;
37 }
38}