| author | |
| committer | |
| log | 7e946bc790c245242af3716199878cf99c369d48 |
| tree | 8b102f5d9c350d91b409d1acf31ed1d925673667 |
| parent | 51491186cb045520ace001e005164a97a8695504 |
2 files changed, 60 insertions(+), 2 deletions(-)
test/c_abi/cfuncs.c+39| ... | ... | @@ -12,6 +12,15 @@ static void assert_or_panic(bool ok) { |
| 12 | 12 | } |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | #ifdef __i386__ | |
| 16 | # define ZIG_NO_I128 | |
| 17 | #endif | |
| 18 | ||
| 19 | #ifdef __i386__ | |
| 20 | # define ZIG_NO_COMPLEX | |
| 21 | #endif | |
| 22 | ||
| 23 | #ifndef ZIG_NO_I128 | |
| 15 | 24 | struct i128 { |
| 16 | 25 | __int128 value; |
| 17 | 26 | }; |
| ... | ... | @@ -19,17 +28,22 @@ struct i128 { |
| 19 | 28 | struct u128 { |
| 20 | 29 | unsigned __int128 value; |
| 21 | 30 | }; |
| 31 | #endif | |
| 22 | 32 | |
| 23 | 33 | void zig_u8(uint8_t); |
| 24 | 34 | void zig_u16(uint16_t); |
| 25 | 35 | void zig_u32(uint32_t); |
| 26 | 36 | void zig_u64(uint64_t); |
| 37 | #ifndef ZIG_NO_I128 | |
| 27 | 38 | void zig_struct_u128(struct u128); |
| 39 | #endif | |
| 28 | 40 | void zig_i8(int8_t); |
| 29 | 41 | void zig_i16(int16_t); |
| 30 | 42 | void zig_i32(int32_t); |
| 31 | 43 | void zig_i64(int64_t); |
| 44 | #ifndef ZIG_NO_I128 | |
| 32 | 45 | void zig_struct_i128(struct i128); |
| 46 | #endif | |
| 33 | 47 | void zig_five_integers(int32_t, int32_t, int32_t, int32_t, int32_t); |
| 34 | 48 | |
| 35 | 49 | void zig_f32(float); |
| ... | ... | @@ -95,7 +109,9 @@ void zig_med_struct_mixed(struct MedStructMixed); |
| 95 | 109 | struct MedStructMixed zig_ret_med_struct_mixed(); |
| 96 | 110 | |
| 97 | 111 | void zig_small_packed_struct(uint8_t); |
| 112 | #ifndef ZIG_NO_I128 | |
| 98 | 113 | void zig_big_packed_struct(__int128); |
| 114 | #endif | |
| 99 | 115 | |
| 100 | 116 | struct SplitStructInts { |
| 101 | 117 | uint64_t a; |
| ... | ... | @@ -151,19 +167,26 @@ void run_c_tests(void) { |
| 151 | 167 | zig_u16(0xfffe); |
| 152 | 168 | zig_u32(0xfffffffd); |
| 153 | 169 | zig_u64(0xfffffffffffffffc); |
| 170 | ||
| 171 | #ifndef ZIG_NO_I128 | |
| 154 | 172 | { |
| 155 | 173 | struct u128 s = {0xfffffffffffffffc}; |
| 156 | 174 | zig_struct_u128(s); |
| 157 | 175 | } |
| 176 | #endif | |
| 158 | 177 | |
| 159 | 178 | zig_i8(-1); |
| 160 | 179 | zig_i16(-2); |
| 161 | 180 | zig_i32(-3); |
| 162 | 181 | zig_i64(-4); |
| 182 | ||
| 183 | #ifndef ZIG_NO_I128 | |
| 163 | 184 | { |
| 164 | 185 | struct i128 s = {-6}; |
| 165 | 186 | zig_struct_i128(s); |
| 166 | 187 | } |
| 188 | #endif | |
| 189 | ||
| 167 | 190 | zig_five_integers(12, 34, 56, 78, 90); |
| 168 | 191 | |
| 169 | 192 | zig_f32(12.34f); |
| ... | ... | @@ -175,6 +198,7 @@ void run_c_tests(void) { |
| 175 | 198 | |
| 176 | 199 | zig_bool(true); |
| 177 | 200 | |
| 201 | #ifndef ZIG_NO_COMPLEX | |
| 178 | 202 | // TODO: Resolve https://github.com/ziglang/zig/issues/8465 |
| 179 | 203 | //{ |
| 180 | 204 | // float complex a = 1.25f + I * 2.6f; |
| ... | ... | @@ -211,23 +235,28 @@ void run_c_tests(void) { |
| 211 | 235 | assert_or_panic(creal(z) == 1.5); |
| 212 | 236 | assert_or_panic(cimag(z) == 13.5); |
| 213 | 237 | } |
| 238 | #endif | |
| 214 | 239 | |
| 215 | 240 | { |
| 216 | 241 | struct BigStruct s = {1, 2, 3, 4, 5}; |
| 217 | 242 | zig_big_struct(s); |
| 218 | 243 | } |
| 219 | 244 | |
| 245 | #ifndef __i386__ | |
| 220 | 246 | { |
| 221 | 247 | struct SmallStructInts s = {1, 2, 3, 4}; |
| 222 | 248 | zig_small_struct_ints(s); |
| 223 | 249 | } |
| 250 | #endif | |
| 224 | 251 | |
| 252 | #ifndef ZIG_NO_I128 | |
| 225 | 253 | { |
| 226 | 254 | __int128 s = 0; |
| 227 | 255 | s |= 1 << 0; |
| 228 | 256 | s |= (__int128)2 << 64; |
| 229 | 257 | zig_big_packed_struct(s); |
| 230 | 258 | } |
| 259 | #endif | |
| 231 | 260 | |
| 232 | 261 | { |
| 233 | 262 | uint8_t s = 0; |
| ... | ... | @@ -238,20 +267,24 @@ void run_c_tests(void) { |
| 238 | 267 | zig_small_packed_struct(s); |
| 239 | 268 | } |
| 240 | 269 | |
| 270 | #ifndef __i386__ | |
| 241 | 271 | { |
| 242 | 272 | struct SplitStructInts s = {1234, 100, 1337}; |
| 243 | 273 | zig_split_struct_ints(s); |
| 244 | 274 | } |
| 275 | #endif | |
| 245 | 276 | |
| 246 | 277 | { |
| 247 | 278 | struct MedStructMixed s = {1234, 100.0f, 1337.0f}; |
| 248 | 279 | zig_med_struct_mixed(s); |
| 249 | 280 | } |
| 250 | 281 | |
| 282 | #ifndef __i386__ | |
| 251 | 283 | { |
| 252 | 284 | struct SplitStructMixed s = {1234, 100, 1337.0f}; |
| 253 | 285 | zig_split_struct_mixed(s); |
| 254 | 286 | } |
| 287 | #endif | |
| 255 | 288 | |
| 256 | 289 | { |
| 257 | 290 | struct BigStruct s = {30, 31, 32, 33, 34}; |
| ... | ... | @@ -306,9 +339,11 @@ void c_u64(uint64_t x) { |
| 306 | 339 | assert_or_panic(x == 0xfffffffffffffffcULL); |
| 307 | 340 | } |
| 308 | 341 | |
| 342 | #ifndef ZIG_NO_I128 | |
| 309 | 343 | void c_struct_u128(struct u128 x) { |
| 310 | 344 | assert_or_panic(x.value == 0xfffffffffffffffcULL); |
| 311 | 345 | } |
| 346 | #endif | |
| 312 | 347 | |
| 313 | 348 | void c_i8(int8_t x) { |
| 314 | 349 | assert_or_panic(x == -1); |
| ... | ... | @@ -326,9 +361,11 @@ void c_i64(int64_t x) { |
| 326 | 361 | assert_or_panic(x == -4); |
| 327 | 362 | } |
| 328 | 363 | |
| 364 | #ifndef ZIG_NO_I128 | |
| 329 | 365 | void c_struct_i128(struct i128 x) { |
| 330 | 366 | assert_or_panic(x.value == -6); |
| 331 | 367 | } |
| 368 | #endif | |
| 332 | 369 | |
| 333 | 370 | void c_f32(float x) { |
| 334 | 371 | assert_or_panic(x == 12.34f); |
| ... | ... | @@ -495,6 +532,7 @@ void c_small_packed_struct(uint8_t x) { |
| 495 | 532 | assert_or_panic(((x >> 6) & 0x3) == 3); |
| 496 | 533 | } |
| 497 | 534 | |
| 535 | #ifndef ZIG_NO_I128 | |
| 498 | 536 | __int128 c_ret_big_packed_struct() { |
| 499 | 537 | __int128 s = 0; |
| 500 | 538 | s |= 1 << 0; |
| ... | ... | @@ -506,6 +544,7 @@ void c_big_packed_struct(__int128 x) { |
| 506 | 544 | assert_or_panic(((x >> 0) & 0xFFFFFFFFFFFFFFFF) == 1); |
| 507 | 545 | assert_or_panic(((x >> 64) & 0xFFFFFFFFFFFFFFFF) == 2); |
| 508 | 546 | } |
| 547 | #endif | |
| 509 | 548 | |
| 510 | 549 | struct SplitStructMixed c_ret_split_struct_mixed() { |
| 511 | 550 | struct SplitStructMixed s = { |
test/c_abi/main.zig+21-2| ... | ... | @@ -2,6 +2,7 @@ const std = @import("std"); |
| 2 | 2 | const builtin = @import("builtin"); |
| 3 | 3 | const print = std.debug.print; |
| 4 | 4 | const expect = std.testing.expect; |
| 5 | const has_i128 = builtin.cpu.arch != .i386; | |
| 5 | 6 | |
| 6 | 7 | extern fn run_c_tests() void; |
| 7 | 8 | |
| ... | ... | @@ -40,13 +41,13 @@ test "C ABI integers" { |
| 40 | 41 | c_u16(0xfffe); |
| 41 | 42 | c_u32(0xfffffffd); |
| 42 | 43 | c_u64(0xfffffffffffffffc); |
| 43 | c_struct_u128(.{ .value = 0xfffffffffffffffc }); | |
| 44 | if (has_i128) c_struct_u128(.{ .value = 0xfffffffffffffffc }); | |
| 44 | 45 | |
| 45 | 46 | c_i8(-1); |
| 46 | 47 | c_i16(-2); |
| 47 | 48 | c_i32(-3); |
| 48 | 49 | c_i64(-4); |
| 49 | c_struct_i128(.{ .value = -6 }); | |
| 50 | if (has_i128) c_struct_i128(.{ .value = -6 }); | |
| 50 | 51 | c_five_integers(12, 34, 56, 78, 90); |
| 51 | 52 | } |
| 52 | 53 | |
| ... | ... | @@ -178,6 +179,8 @@ test "C ABI complex float" { |
| 178 | 179 | } |
| 179 | 180 | |
| 180 | 181 | test "C ABI complex float by component" { |
| 182 | if (builtin.cpu.arch == .i386) return error.SkipZigTest; | |
| 183 | ||
| 181 | 184 | const a = ComplexFloat{ .real = 1.25, .imag = 2.6 }; |
| 182 | 185 | const b = ComplexFloat{ .real = 11.3, .imag = -1.5 }; |
| 183 | 186 | |
| ... | ... | @@ -187,6 +190,8 @@ test "C ABI complex float by component" { |
| 187 | 190 | } |
| 188 | 191 | |
| 189 | 192 | test "C ABI complex double" { |
| 193 | if (builtin.cpu.arch == .i386) return error.SkipZigTest; | |
| 194 | ||
| 190 | 195 | const a = ComplexDouble{ .real = 1.25, .imag = 2.6 }; |
| 191 | 196 | const b = ComplexDouble{ .real = 11.3, .imag = -1.5 }; |
| 192 | 197 | |
| ... | ... | @@ -196,6 +201,8 @@ test "C ABI complex double" { |
| 196 | 201 | } |
| 197 | 202 | |
| 198 | 203 | test "C ABI complex double by component" { |
| 204 | if (builtin.cpu.arch == .i386) return error.SkipZigTest; | |
| 205 | ||
| 199 | 206 | const a = ComplexDouble{ .real = 1.25, .imag = 2.6 }; |
| 200 | 207 | const b = ComplexDouble{ .real = 11.3, .imag = -1.5 }; |
| 201 | 208 | |
| ... | ... | @@ -304,6 +311,8 @@ extern fn c_med_struct_mixed(MedStructMixed) void; |
| 304 | 311 | extern fn c_ret_med_struct_mixed() MedStructMixed; |
| 305 | 312 | |
| 306 | 313 | test "C ABI medium struct of ints and floats" { |
| 314 | if (builtin.cpu.arch == .i386) return error.SkipZigTest; | |
| 315 | ||
| 307 | 316 | var s = MedStructMixed{ |
| 308 | 317 | .a = 1234, |
| 309 | 318 | .b = 100.0, |
| ... | ... | @@ -332,6 +341,8 @@ extern fn c_small_struct_ints(SmallStructInts) void; |
| 332 | 341 | extern fn c_ret_small_struct_ints() SmallStructInts; |
| 333 | 342 | |
| 334 | 343 | test "C ABI small struct of ints" { |
| 344 | if (builtin.cpu.arch == .i386) return error.SkipZigTest; | |
| 345 | ||
| 335 | 346 | var s = SmallStructInts{ |
| 336 | 347 | .a = 1, |
| 337 | 348 | .b = 2, |
| ... | ... | @@ -392,6 +403,8 @@ export fn zig_big_packed_struct(x: BigPackedStruct) void { |
| 392 | 403 | } |
| 393 | 404 | |
| 394 | 405 | test "C ABI big packed struct" { |
| 406 | if (!has_i128) return error.SkipZigTest; | |
| 407 | ||
| 395 | 408 | var s = BigPackedStruct{ .a = 1, .b = 2 }; |
| 396 | 409 | c_big_packed_struct(s); |
| 397 | 410 | var s2 = c_ret_big_packed_struct(); |
| ... | ... | @@ -407,6 +420,8 @@ const SplitStructInt = extern struct { |
| 407 | 420 | extern fn c_split_struct_ints(SplitStructInt) void; |
| 408 | 421 | |
| 409 | 422 | test "C ABI split struct of ints" { |
| 423 | if (builtin.cpu.arch == .i386) return error.SkipZigTest; | |
| 424 | ||
| 410 | 425 | var s = SplitStructInt{ |
| 411 | 426 | .a = 1234, |
| 412 | 427 | .b = 100, |
| ... | ... | @@ -430,6 +445,8 @@ extern fn c_split_struct_mixed(SplitStructMixed) void; |
| 430 | 445 | extern fn c_ret_split_struct_mixed() SplitStructMixed; |
| 431 | 446 | |
| 432 | 447 | test "C ABI split struct of ints and floats" { |
| 448 | if (builtin.cpu.arch == .i386) return error.SkipZigTest; | |
| 449 | ||
| 433 | 450 | var s = SplitStructMixed{ |
| 434 | 451 | .a = 1234, |
| 435 | 452 | .b = 100, |
| ... | ... | @@ -675,6 +692,8 @@ extern fn c_struct_with_array(StructWithArray) void; |
| 675 | 692 | extern fn c_ret_struct_with_array() StructWithArray; |
| 676 | 693 | |
| 677 | 694 | test "Struct with array as padding." { |
| 695 | if (builtin.cpu.arch == .i386) return error.SkipZigTest; | |
| 696 | ||
| 678 | 697 | c_struct_with_array(.{ .a = 1, .padding = undefined, .b = 2 }); |
| 679 | 698 | |
| 680 | 699 | var x = c_ret_struct_with_array(); |