aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-07-27 22:04:00 -0700
committerGitHub <noreply@github.com>2022-07-27 22:04:00 -0700
commitc650ccfca719b695fe7752f9126e8dbcc2ab4d6d (patch)
treedc112b2472f53e44e377dee68d30db02caed8c03 /test
parentdfc7493dcb049788b92137ca09b8bd47cee23865 (diff)
parent3ccb6a0cd4d4f436a6009ed614436bb3e2e27a7c (diff)
downloadzig-c650ccfca719b695fe7752f9126e8dbcc2ab4d6d.tar.gz
zig-c650ccfca719b695fe7752f9126e8dbcc2ab4d6d.zip
Merge pull request #12265 from ziglang/stage3-run-translated-c
CI: test-run-translated-c with stage3
Diffstat (limited to 'test')
-rw-r--r--test/behavior/fn.zig14
-rw-r--r--test/behavior/packed-struct.zig22
-rw-r--r--test/behavior/union.zig30
-rw-r--r--test/run_translated_c.zig110
4 files changed, 124 insertions, 52 deletions
diff --git a/test/behavior/fn.zig b/test/behavior/fn.zig
index 383d553781..044e4ff049 100644
--- a/test/behavior/fn.zig
+++ b/test/behavior/fn.zig
@@ -408,3 +408,17 @@ test "function with inferred error set but returning no error" {
const return_ty = @typeInfo(@TypeOf(S.foo)).Fn.return_type.?;
try expectEqual(0, @typeInfo(@typeInfo(return_ty).ErrorUnion.error_set).ErrorSet.?.len);
}
+
+test "import passed byref to function in return type" {
+ if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
+
+ const S = struct {
+ fn get() @import("std").ArrayListUnmanaged(i32) {
+ var x: @import("std").ArrayListUnmanaged(i32) = .{};
+ return x;
+ }
+ };
+ var list = S.get();
+ try expect(list.items.len == 0);
+}
diff --git a/test/behavior/packed-struct.zig b/test/behavior/packed-struct.zig
index 52083a492d..2dea485bf5 100644
--- a/test/behavior/packed-struct.zig
+++ b/test/behavior/packed-struct.zig
@@ -436,3 +436,25 @@ test "load pointer from packed struct" {
try expect(i == 123);
}
}
+
+test "@ptrToInt on a packed struct field" {
+ if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
+
+ const S = struct {
+ const P = packed struct {
+ x: u8,
+ y: u8,
+ z: u32,
+ };
+ var p0: P = P{
+ .x = 1,
+ .y = 2,
+ .z = 0,
+ };
+ };
+ try expect(@ptrToInt(&S.p0.z) - @ptrToInt(&S.p0.x) == 2);
+}
diff --git a/test/behavior/union.zig b/test/behavior/union.zig
index 8e4b262565..2f6fa78f0c 100644
--- a/test/behavior/union.zig
+++ b/test/behavior/union.zig
@@ -1226,3 +1226,33 @@ test "extern union most-aligned field is smaller" {
var a: ?U = .{ .un = [_]u8{0} ** 110 };
try expect(a != null);
}
+
+test "return an extern union from C calling convention" {
+ if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
+
+ const namespace = struct {
+ const S = extern struct {
+ x: c_int,
+ };
+ const U = extern union {
+ l: c_long,
+ d: f64,
+ s: S,
+ };
+
+ fn bar(arg_u: U) callconv(.C) U {
+ var u = arg_u;
+ return u;
+ }
+ };
+
+ var u: namespace.U = namespace.U{
+ .l = @as(c_long, 42),
+ };
+ u = namespace.bar(namespace.U{
+ .d = 4.0,
+ });
+ try expect(u.d == 4.0);
+}
diff --git a/test/run_translated_c.zig b/test/run_translated_c.zig
index cfd7f2d15e..871ec98fbc 100644
--- a/test/run_translated_c.zig
+++ b/test/run_translated_c.zig
@@ -891,39 +891,42 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
\\}
, "");
- cases.add("Obscure ways of calling functions; issue #4124",
- \\#include <stdlib.h>
- \\static int add(int a, int b) {
- \\ return a + b;
- \\}
- \\typedef int (*adder)(int, int);
- \\typedef void (*funcptr)(void);
- \\int main() {
- \\ if ((add)(1, 2) != 3) abort();
- \\ if ((&add)(1, 2) != 3) abort();
- \\ if (add(3, 1) != 4) abort();
- \\ if ((*add)(2, 3) != 5) abort();
- \\ if ((**add)(7, -1) != 6) abort();
- \\ if ((***add)(-2, 9) != 7) abort();
- \\
- \\ int (*ptr)(int a, int b);
- \\ ptr = add;
- \\
- \\ if (ptr(1, 2) != 3) abort();
- \\ if ((*ptr)(3, 1) != 4) abort();
- \\ if ((**ptr)(2, 3) != 5) abort();
- \\ if ((***ptr)(7, -1) != 6) abort();
- \\ if ((****ptr)(-2, 9) != 7) abort();
- \\
- \\ funcptr addr1 = (funcptr)(add);
- \\ funcptr addr2 = (funcptr)(&add);
- \\
- \\ if (addr1 != addr2) abort();
- \\ if (((int(*)(int, int))addr1)(1, 2) != 3) abort();
- \\ if (((adder)addr2)(1, 2) != 3) abort();
- \\ return 0;
- \\}
- , "");
+ if (@import("builtin").zig_backend == .stage1) {
+ // https://github.com/ziglang/zig/issues/12263
+ cases.add("Obscure ways of calling functions; issue #4124",
+ \\#include <stdlib.h>
+ \\static int add(int a, int b) {
+ \\ return a + b;
+ \\}
+ \\typedef int (*adder)(int, int);
+ \\typedef void (*funcptr)(void);
+ \\int main() {
+ \\ if ((add)(1, 2) != 3) abort();
+ \\ if ((&add)(1, 2) != 3) abort();
+ \\ if (add(3, 1) != 4) abort();
+ \\ if ((*add)(2, 3) != 5) abort();
+ \\ if ((**add)(7, -1) != 6) abort();
+ \\ if ((***add)(-2, 9) != 7) abort();
+ \\
+ \\ int (*ptr)(int a, int b);
+ \\ ptr = add;
+ \\
+ \\ if (ptr(1, 2) != 3) abort();
+ \\ if ((*ptr)(3, 1) != 4) abort();
+ \\ if ((**ptr)(2, 3) != 5) abort();
+ \\ if ((***ptr)(7, -1) != 6) abort();
+ \\ if ((****ptr)(-2, 9) != 7) abort();
+ \\
+ \\ funcptr addr1 = (funcptr)(add);
+ \\ funcptr addr2 = (funcptr)(&add);
+ \\
+ \\ if (addr1 != addr2) abort();
+ \\ if (((int(*)(int, int))addr1)(1, 2) != 3) abort();
+ \\ if (((adder)addr2)(1, 2) != 3) abort();
+ \\ return 0;
+ \\}
+ , "");
+ }
cases.add("Return boolean expression as int; issue #6215",
\\#include <stdlib.h>
@@ -1319,25 +1322,28 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
\\}
, "");
- cases.add("basic vector expressions",
- \\#include <stdlib.h>
- \\#include <stdint.h>
- \\typedef int16_t __v8hi __attribute__((__vector_size__(16)));
- \\int main(int argc, char**argv) {
- \\ __v8hi uninitialized;
- \\ __v8hi empty_init = {};
- \\ __v8hi partial_init = {0, 1, 2, 3};
- \\
- \\ __v8hi a = {0, 1, 2, 3, 4, 5, 6, 7};
- \\ __v8hi b = (__v8hi) {100, 200, 300, 400, 500, 600, 700, 800};
- \\
- \\ __v8hi sum = a + b;
- \\ for (int i = 0; i < 8; i++) {
- \\ if (sum[i] != a[i] + b[i]) abort();
- \\ }
- \\ return 0;
- \\}
- , "");
+ if (@import("builtin").zig_backend == .stage1) {
+ // https://github.com/ziglang/zig/issues/12264
+ cases.add("basic vector expressions",
+ \\#include <stdlib.h>
+ \\#include <stdint.h>
+ \\typedef int16_t __v8hi __attribute__((__vector_size__(16)));
+ \\int main(int argc, char**argv) {
+ \\ __v8hi uninitialized;
+ \\ __v8hi empty_init = {};
+ \\ __v8hi partial_init = {0, 1, 2, 3};
+ \\
+ \\ __v8hi a = {0, 1, 2, 3, 4, 5, 6, 7};
+ \\ __v8hi b = (__v8hi) {100, 200, 300, 400, 500, 600, 700, 800};
+ \\
+ \\ __v8hi sum = a + b;
+ \\ for (int i = 0; i < 8; i++) {
+ \\ if (sum[i] != a[i] + b[i]) abort();
+ \\ }
+ \\ return 0;
+ \\}
+ , "");
+ }
cases.add("__builtin_shufflevector",
\\#include <stdlib.h>