diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2023-07-28 19:27:43 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-07-28 19:27:43 -0700 |
| commit | bde6e075dfc202fdcfa21ec9c2d90941460b002e (patch) | |
| tree | fa782eaf75c76e5d7ab09ce6af58061c8e688df4 /test | |
| parent | 423c1221f9c020a1047fc14ce8e9003d7e009914 (diff) | |
| parent | 97ae2d2c29f827ebb73abbc0317cd39ac4ca4c9b (diff) | |
| download | zig-bde6e075dfc202fdcfa21ec9c2d90941460b002e.tar.gz zig-bde6e075dfc202fdcfa21ec9c2d90941460b002e.zip | |
Merge pull request #16593 from jacobly0/c-abi
Fix various C ABI issues
Diffstat (limited to 'test')
| -rw-r--r-- | test/behavior/tuple.zig | 15 | ||||
| -rw-r--r-- | test/c_abi/cfuncs.c | 50 | ||||
| -rw-r--r-- | test/c_abi/main.zig | 50 | ||||
| -rw-r--r-- | test/tests.zig | 2 |
4 files changed, 102 insertions, 15 deletions
diff --git a/test/behavior/tuple.zig b/test/behavior/tuple.zig index e9d3fcd0aa..32ebfe89fd 100644 --- a/test/behavior/tuple.zig +++ b/test/behavior/tuple.zig @@ -453,3 +453,18 @@ test "tuple pointer is indexable" { try expectEqual(@as(u32, 100), (&y)[0]); try expectEqual(false, (&y)[1]); } + +test "coerce anon tuple to tuple" { + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; + + var x: u8 = 1; + var y: u16 = 2; + var t = .{ x, y }; + var s: struct { u8, u16 } = t; + try expectEqual(x, s[0]); + try expectEqual(y, s[1]); +} diff --git a/test/c_abi/cfuncs.c b/test/c_abi/cfuncs.c index 262a3a794e..34aab7f934 100644 --- a/test/c_abi/cfuncs.c +++ b/test/c_abi/cfuncs.c @@ -16,8 +16,12 @@ static void assert_or_panic(bool ok) { # define ZIG_PPC32 #endif -#if defined __riscv && defined _ILP32 -# define ZIG_RISCV32 +#ifdef __riscv +# ifdef _ILP32 +# define ZIG_RISCV32 +# else +# define ZIG_RISCV64 +# endif #endif #if defined(__aarch64__) && defined(__linux__) @@ -191,6 +195,15 @@ struct SmallStructInts { void zig_small_struct_ints(struct SmallStructInts); struct SmallStructInts zig_ret_small_struct_ints(); +struct MedStructInts { + int32_t x; + int32_t y; + int32_t z; +}; + +void zig_med_struct_ints(struct MedStructInts); +struct MedStructInts zig_ret_med_struct_ints(); + struct MedStructMixed { uint32_t a; float b; @@ -339,14 +352,22 @@ void run_c_tests(void) { } #endif -#if !defined __i386__ && !defined __arm__ && !defined __mips__ && \ - !defined ZIG_PPC32 && !defined _ARCH_PPC64 +#if !defined __i386__ && !defined __arm__ && !defined __aarch64__ && \ + !defined __mips__ && !defined __powerpc__ && !defined ZIG_RISCV64 { struct SmallStructInts s = {1, 2, 3, 4}; zig_small_struct_ints(s); } #endif +#if !defined __i386__ && !defined __arm__ && !defined __aarch64__ && \ + !defined __mips__ && !defined __powerpc__ && !defined ZIG_RISCV64 + { + struct MedStructInts s = {1, 2, 3}; + zig_med_struct_ints(s); + } +#endif + #ifndef ZIG_NO_I128 { __int128 s = 0; @@ -586,6 +607,27 @@ struct SmallStructInts c_ret_small_struct_ints() { return s; } +void c_med_struct_ints(struct MedStructInts s) { + assert_or_panic(s.x == 1); + assert_or_panic(s.y == 2); + assert_or_panic(s.z == 3); + + struct MedStructInts s2 = zig_ret_med_struct_ints(); + + assert_or_panic(s2.x == 1); + assert_or_panic(s2.y == 2); + assert_or_panic(s2.z == 3); +} + +struct MedStructInts c_ret_med_struct_ints() { + struct MedStructInts s = { + .x = 1, + .y = 2, + .z = 3, + }; + return s; +} + void c_med_struct_mixed(struct MedStructMixed x) { assert_or_panic(x.a == 1234); assert_or_panic(x.b == 100.0f); diff --git a/test/c_abi/main.zig b/test/c_abi/main.zig index 05f7f060ea..1aa306ab14 100644 --- a/test/c_abi/main.zig +++ b/test/c_abi/main.zig @@ -393,6 +393,38 @@ export fn zig_small_struct_ints(x: SmallStructInts) void { expect(x.d == 4) catch @panic("test failure"); } +const MedStructInts = extern struct { + x: i32, + y: i32, + z: i32, +}; +extern fn c_med_struct_ints(MedStructInts) void; +extern fn c_ret_med_struct_ints() MedStructInts; + +test "C ABI medium struct of ints" { + if (builtin.cpu.arch == .x86) return error.SkipZigTest; + if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest; + if (comptime builtin.cpu.arch.isPPC()) return error.SkipZigTest; + if (comptime builtin.cpu.arch.isPPC64()) return error.SkipZigTest; + + var s = MedStructInts{ + .x = 1, + .y = 2, + .z = 3, + }; + c_med_struct_ints(s); + var s2 = c_ret_med_struct_ints(); + try expect(s2.x == 1); + try expect(s2.y == 2); + try expect(s2.z == 3); +} + +export fn zig_med_struct_ints(s: MedStructInts) void { + expect(s.x == 1) catch @panic("test failure"); + expect(s.y == 2) catch @panic("test failure"); + expect(s.z == 3) catch @panic("test failure"); +} + const SmallPackedStruct = packed struct { a: u2, b: u2, @@ -691,6 +723,14 @@ export fn zig_ret_small_struct_ints() SmallStructInts { }; } +export fn zig_ret_med_struct_ints() MedStructInts { + return .{ + .x = 1, + .y = 2, + .z = 3, + }; +} + export fn zig_ret_med_struct_mixed() MedStructMixed { return .{ .a = 1234, @@ -813,11 +853,6 @@ extern fn c_ret_medium_vec() MediumVec; test "medium simd vector" { if (comptime builtin.cpu.arch.isPPC64()) return error.SkipZigTest; - if (builtin.cpu.arch == .x86_64 and builtin.os.tag == .linux) { - // TODO: https://github.com/ziglang/zig/issues/14908 - return error.SkipZigTest; - } - c_medium_vec(.{ 1, 2, 3, 4 }); var x = c_ret_medium_vec(); @@ -837,11 +872,6 @@ test "big simd vector" { if (comptime builtin.cpu.arch.isPPC64()) return error.SkipZigTest; if (builtin.cpu.arch == .x86_64 and builtin.os.tag == .macos and builtin.mode != .Debug) return error.SkipZigTest; - if (builtin.cpu.arch == .x86_64 and builtin.os.tag == .linux) { - // TODO: https://github.com/ziglang/zig/issues/14908 - return error.SkipZigTest; - } - c_big_vec(.{ 1, 2, 3, 4, 5, 6, 7, 8 }); var x = c_ret_big_vec(); diff --git a/test/tests.zig b/test/tests.zig index b76f7baf84..bba1b00142 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -1102,7 +1102,7 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step { pub fn addCAbiTests(b: *std.Build, skip_non_native: bool, skip_release: bool) *Step { const step = b.step("test-c-abi", "Run the C ABI tests"); - const optimize_modes: [2]OptimizeMode = .{ .Debug, .ReleaseFast }; + const optimize_modes: [3]OptimizeMode = .{ .Debug, .ReleaseSafe, .ReleaseFast }; for (optimize_modes) |optimize_mode| { if (optimize_mode != .Debug and skip_release) continue; |
