authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-12 21:13:07-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-12 21:13:07-07:00
logc349191b75811f8a21e26f8b175483449fae1638
tree70e4b8e77f3b7746110b7ed0a1b391c189b3f81d
parenta005ac9d3c884c6254074ec150fe536881fe31b5

organize behavior tests

moving towards disabling failing tests on an individual basis

15 files changed, 2111 insertions(+), 1938 deletions(-)

test/behavior.zig-7
......@@ -46,7 +46,6 @@ test {
4646
4747 if (builtin.zig_backend != .stage2_arm and builtin.zig_backend != .stage2_x86_64) {
4848 // Tests that pass (partly) for stage1, llvm backend, C backend, wasm backend.
49 _ = @import("behavior/array_llvm.zig");
5049 _ = @import("behavior/bitcast.zig");
5150 _ = @import("behavior/bugs/624.zig");
5251 _ = @import("behavior/bugs/704.zig");
......@@ -61,7 +60,6 @@ test {
6160 _ = @import("behavior/bugs/4954.zig");
6261 _ = @import("behavior/byval_arg_var.zig");
6362 _ = @import("behavior/call.zig");
64 _ = @import("behavior/cast_llvm.zig");
6563 _ = @import("behavior/defer.zig");
6664 _ = @import("behavior/enum.zig");
6765 _ = @import("behavior/error.zig");
......@@ -98,7 +96,6 @@ test {
9896 // Tests that pass for stage1 and the llvm backend.
9997 _ = @import("behavior/atomics.zig");
10098 _ = @import("behavior/bugs/9584.zig");
101 _ = @import("behavior/error_llvm.zig");
10299 _ = @import("behavior/eval.zig");
103100 _ = @import("behavior/floatop.zig");
104101 _ = @import("behavior/math.zig");
......@@ -107,7 +104,6 @@ test {
107104 _ = @import("behavior/popcount.zig");
108105 _ = @import("behavior/saturating_arithmetic.zig");
109106 _ = @import("behavior/sizeof_and_typeof.zig");
110 _ = @import("behavior/struct_llvm.zig");
111107 _ = @import("behavior/switch.zig");
112108 _ = @import("behavior/widening.zig");
113109
......@@ -156,14 +152,11 @@ test {
156152 _ = @import("behavior/reflection.zig");
157153 _ = @import("behavior/select.zig");
158154 _ = @import("behavior/shuffle.zig");
159 _ = @import("behavior/sizeof_and_typeof_stage1.zig");
160155 _ = @import("behavior/struct_contains_null_ptr_itself.zig");
161156 _ = @import("behavior/struct_contains_slice_of_itself.zig");
162157 _ = @import("behavior/switch_prong_err_enum.zig");
163158 _ = @import("behavior/switch_prong_implicit_cast.zig");
164 _ = @import("behavior/truncate_stage1.zig");
165159 _ = @import("behavior/tuple.zig");
166 _ = @import("behavior/type_stage1.zig");
167160 _ = @import("behavior/typename.zig");
168161 _ = @import("behavior/union_with_members.zig");
169162 _ = @import("behavior/var_args.zig");
test/behavior/array.zig+350
......@@ -222,3 +222,353 @@ test "anonymous list literal syntax" {
222222 try S.doTheTest();
223223 comptime try S.doTheTest();
224224}
225
226var s_array: [8]Sub = undefined;
227const Sub = struct { b: u8 };
228const Str = struct { a: []Sub };
229test "set global var array via slice embedded in struct" {
230 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
231 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
232 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
233
234 var s = Str{ .a = s_array[0..] };
235
236 s.a[0].b = 1;
237 s.a[1].b = 2;
238 s.a[2].b = 3;
239
240 try expect(s_array[0].b == 1);
241 try expect(s_array[1].b == 2);
242 try expect(s_array[2].b == 3);
243}
244
245test "read/write through global variable array of struct fields initialized via array mult" {
246 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
247 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
248 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
249
250 const S = struct {
251 fn doTheTest() !void {
252 try expect(storage[0].term == 1);
253 storage[0] = MyStruct{ .term = 123 };
254 try expect(storage[0].term == 123);
255 }
256
257 pub const MyStruct = struct {
258 term: usize,
259 };
260
261 var storage: [1]MyStruct = [_]MyStruct{MyStruct{ .term = 1 }} ** 1;
262 };
263 try S.doTheTest();
264}
265
266test "implicit cast single-item pointer" {
267 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
268 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
269 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
270
271 try testImplicitCastSingleItemPtr();
272 comptime try testImplicitCastSingleItemPtr();
273}
274
275fn testImplicitCastSingleItemPtr() !void {
276 var byte: u8 = 100;
277 const slice = @as(*[1]u8, &byte)[0..];
278 slice[0] += 1;
279 try expect(byte == 101);
280}
281
282fn testArrayByValAtComptime(b: [2]u8) u8 {
283 return b[0];
284}
285
286test "comptime evaluating function that takes array by value" {
287 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
288 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
289 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
290
291 const arr = [_]u8{ 1, 2 };
292 const x = comptime testArrayByValAtComptime(arr);
293 const y = comptime testArrayByValAtComptime(arr);
294 try expect(x == 1);
295 try expect(y == 1);
296}
297
298test "runtime initialize array elem and then implicit cast to slice" {
299 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
300 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
301 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
302
303 var two: i32 = 2;
304 const x: []const i32 = &[_]i32{two};
305 try expect(x[0] == 2);
306}
307
308test "array literal as argument to function" {
309 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
310 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
311 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
312
313 const S = struct {
314 fn entry(two: i32) !void {
315 try foo(&[_]i32{ 1, 2, 3 });
316 try foo(&[_]i32{ 1, two, 3 });
317 try foo2(true, &[_]i32{ 1, 2, 3 });
318 try foo2(true, &[_]i32{ 1, two, 3 });
319 }
320 fn foo(x: []const i32) !void {
321 try expect(x[0] == 1);
322 try expect(x[1] == 2);
323 try expect(x[2] == 3);
324 }
325 fn foo2(trash: bool, x: []const i32) !void {
326 try expect(trash);
327 try expect(x[0] == 1);
328 try expect(x[1] == 2);
329 try expect(x[2] == 3);
330 }
331 };
332 try S.entry(2);
333 comptime try S.entry(2);
334}
335
336test "double nested array to const slice cast in array literal" {
337 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
338 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
339 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
340
341 const S = struct {
342 fn entry(two: i32) !void {
343 const cases = [_][]const []const i32{
344 &[_][]const i32{&[_]i32{1}},
345 &[_][]const i32{&[_]i32{ 2, 3 }},
346 &[_][]const i32{
347 &[_]i32{4},
348 &[_]i32{ 5, 6, 7 },
349 },
350 };
351 try check(&cases);
352
353 const cases2 = [_][]const i32{
354 &[_]i32{1},
355 &[_]i32{ two, 3 },
356 };
357 try expect(cases2.len == 2);
358 try expect(cases2[0].len == 1);
359 try expect(cases2[0][0] == 1);
360 try expect(cases2[1].len == 2);
361 try expect(cases2[1][0] == 2);
362 try expect(cases2[1][1] == 3);
363
364 const cases3 = [_][]const []const i32{
365 &[_][]const i32{&[_]i32{1}},
366 &[_][]const i32{&[_]i32{ two, 3 }},
367 &[_][]const i32{
368 &[_]i32{4},
369 &[_]i32{ 5, 6, 7 },
370 },
371 };
372 try check(&cases3);
373 }
374
375 fn check(cases: []const []const []const i32) !void {
376 try expect(cases.len == 3);
377 try expect(cases[0].len == 1);
378 try expect(cases[0][0].len == 1);
379 try expect(cases[0][0][0] == 1);
380 try expect(cases[1].len == 1);
381 try expect(cases[1][0].len == 2);
382 try expect(cases[1][0][0] == 2);
383 try expect(cases[1][0][1] == 3);
384 try expect(cases[2].len == 2);
385 try expect(cases[2][0].len == 1);
386 try expect(cases[2][0][0] == 4);
387 try expect(cases[2][1].len == 3);
388 try expect(cases[2][1][0] == 5);
389 try expect(cases[2][1][1] == 6);
390 try expect(cases[2][1][2] == 7);
391 }
392 };
393 try S.entry(2);
394 comptime try S.entry(2);
395}
396
397test "anonymous literal in array" {
398 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
399 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
400 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
401
402 const S = struct {
403 const Foo = struct {
404 a: usize = 2,
405 b: usize = 4,
406 };
407 fn doTheTest() !void {
408 var array: [2]Foo = .{
409 .{ .a = 3 },
410 .{ .b = 3 },
411 };
412 try expect(array[0].a == 3);
413 try expect(array[0].b == 4);
414 try expect(array[1].a == 2);
415 try expect(array[1].b == 3);
416 }
417 };
418 try S.doTheTest();
419 comptime try S.doTheTest();
420}
421
422test "access the null element of a null terminated array" {
423 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
424 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
425 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
426
427 const S = struct {
428 fn doTheTest() !void {
429 var array: [4:0]u8 = .{ 'a', 'o', 'e', 'u' };
430 try expect(array[4] == 0);
431 var len: usize = 4;
432 try expect(array[len] == 0);
433 }
434 };
435 try S.doTheTest();
436 comptime try S.doTheTest();
437}
438
439test "type deduction for array subscript expression" {
440 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
441 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
442 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
443
444 const S = struct {
445 fn doTheTest() !void {
446 var array = [_]u8{ 0x55, 0xAA };
447 var v0 = true;
448 try expect(@as(u8, 0xAA) == array[if (v0) 1 else 0]);
449 var v1 = false;
450 try expect(@as(u8, 0x55) == array[if (v1) 1 else 0]);
451 }
452 };
453 try S.doTheTest();
454 comptime try S.doTheTest();
455}
456
457test "sentinel element count towards the ABI size calculation" {
458 if (builtin.zig_backend == .stage2_llvm) return error.SkipZigTest; // TODO
459 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
460 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
461 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
462 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
463
464 const S = struct {
465 fn doTheTest() !void {
466 const T = packed struct {
467 fill_pre: u8 = 0x55,
468 data: [0:0]u8 = undefined,
469 fill_post: u8 = 0xAA,
470 };
471 var x = T{};
472 var as_slice = mem.asBytes(&x);
473 try expect(@as(usize, 3) == as_slice.len);
474 try expect(@as(u8, 0x55) == as_slice[0]);
475 try expect(@as(u8, 0xAA) == as_slice[2]);
476 }
477 };
478
479 try S.doTheTest();
480 comptime try S.doTheTest();
481}
482
483test "zero-sized array with recursive type definition" {
484 if (builtin.zig_backend == .stage2_llvm) return error.SkipZigTest; // TODO
485 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
486 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
487 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
488 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
489
490 const U = struct {
491 fn foo(comptime T: type, comptime n: usize) type {
492 return struct {
493 s: [n]T,
494 x: usize = n,
495 };
496 }
497 };
498
499 const S = struct {
500 list: U.foo(@This(), 0),
501 };
502
503 var t: S = .{ .list = .{ .s = undefined } };
504 try expect(@as(usize, 0) == t.list.x);
505}
506
507test "type coercion of anon struct literal to array" {
508 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
509 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
510 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
511
512 const S = struct {
513 const U = union {
514 a: u32,
515 b: bool,
516 c: []const u8,
517 };
518
519 fn doTheTest() !void {
520 var x1: u8 = 42;
521 const t1 = .{ x1, 56, 54 };
522 var arr1: [3]u8 = t1;
523 try expect(arr1[0] == 42);
524 try expect(arr1[1] == 56);
525 try expect(arr1[2] == 54);
526
527 if (@import("builtin").zig_backend == .stage2_llvm) return error.SkipZigTest; // TODO
528 if (@import("builtin").zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
529
530 var x2: U = .{ .a = 42 };
531 const t2 = .{ x2, .{ .b = true }, .{ .c = "hello" } };
532 var arr2: [3]U = t2;
533 try expect(arr2[0].a == 42);
534 try expect(arr2[1].b == true);
535 try expect(mem.eql(u8, arr2[2].c, "hello"));
536 }
537 };
538 try S.doTheTest();
539 comptime try S.doTheTest();
540}
541
542test "type coercion of pointer to anon struct literal to pointer to array" {
543 if (builtin.zig_backend == .stage2_llvm) return error.SkipZigTest; // TODO
544 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
545 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
546 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
547 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
548
549 const S = struct {
550 const U = union {
551 a: u32,
552 b: bool,
553 c: []const u8,
554 };
555
556 fn doTheTest() !void {
557 var x1: u8 = 42;
558 const t1 = &.{ x1, 56, 54 };
559 var arr1: *const [3]u8 = t1;
560 try expect(arr1[0] == 42);
561 try expect(arr1[1] == 56);
562 try expect(arr1[2] == 54);
563
564 var x2: U = .{ .a = 42 };
565 const t2 = &.{ x2, .{ .b = true }, .{ .c = "hello" } };
566 var arr2: *const [3]U = t2;
567 try expect(arr2[0].a == 42);
568 try expect(arr2[1].b == true);
569 try expect(mem.eql(u8, arr2[2].c, "hello"));
570 }
571 };
572 try S.doTheTest();
573 comptime try S.doTheTest();
574}
test/behavior/array_llvm.zig deleted-315
......@@ -1,315 +0,0 @@
1const std = @import("std");
2const testing = std.testing;
3const expect = testing.expect;
4const mem = std.mem;
5
6var s_array: [8]Sub = undefined;
7const Sub = struct { b: u8 };
8const Str = struct { a: []Sub };
9test "set global var array via slice embedded in struct" {
10 if (@import("builtin").zig_backend == .stage2_c) return error.SkipZigTest; // TODO
11 var s = Str{ .a = s_array[0..] };
12
13 s.a[0].b = 1;
14 s.a[1].b = 2;
15 s.a[2].b = 3;
16
17 try expect(s_array[0].b == 1);
18 try expect(s_array[1].b == 2);
19 try expect(s_array[2].b == 3);
20}
21
22test "read/write through global variable array of struct fields initialized via array mult" {
23 if (@import("builtin").zig_backend == .stage2_c) return error.SkipZigTest; // TODO
24 const S = struct {
25 fn doTheTest() !void {
26 try expect(storage[0].term == 1);
27 storage[0] = MyStruct{ .term = 123 };
28 try expect(storage[0].term == 123);
29 }
30
31 pub const MyStruct = struct {
32 term: usize,
33 };
34
35 var storage: [1]MyStruct = [_]MyStruct{MyStruct{ .term = 1 }} ** 1;
36 };
37 try S.doTheTest();
38}
39
40test "implicit cast single-item pointer" {
41 if (@import("builtin").zig_backend == .stage2_c) return error.SkipZigTest; // TODO
42 try testImplicitCastSingleItemPtr();
43 comptime try testImplicitCastSingleItemPtr();
44}
45
46fn testImplicitCastSingleItemPtr() !void {
47 var byte: u8 = 100;
48 const slice = @as(*[1]u8, &byte)[0..];
49 slice[0] += 1;
50 try expect(byte == 101);
51}
52
53fn testArrayByValAtComptime(b: [2]u8) u8 {
54 return b[0];
55}
56
57test "comptime evaluating function that takes array by value" {
58 if (@import("builtin").zig_backend == .stage2_c) return error.SkipZigTest; // TODO
59 const arr = [_]u8{ 1, 2 };
60 const x = comptime testArrayByValAtComptime(arr);
61 const y = comptime testArrayByValAtComptime(arr);
62 try expect(x == 1);
63 try expect(y == 1);
64}
65
66test "runtime initialize array elem and then implicit cast to slice" {
67 if (@import("builtin").zig_backend == .stage2_c) return error.SkipZigTest; // TODO
68 var two: i32 = 2;
69 const x: []const i32 = &[_]i32{two};
70 try expect(x[0] == 2);
71}
72
73test "array literal as argument to function" {
74 if (@import("builtin").zig_backend == .stage2_c) return error.SkipZigTest; // TODO
75 const S = struct {
76 fn entry(two: i32) !void {
77 try foo(&[_]i32{ 1, 2, 3 });
78 try foo(&[_]i32{ 1, two, 3 });
79 try foo2(true, &[_]i32{ 1, 2, 3 });
80 try foo2(true, &[_]i32{ 1, two, 3 });
81 }
82 fn foo(x: []const i32) !void {
83 try expect(x[0] == 1);
84 try expect(x[1] == 2);
85 try expect(x[2] == 3);
86 }
87 fn foo2(trash: bool, x: []const i32) !void {
88 try expect(trash);
89 try expect(x[0] == 1);
90 try expect(x[1] == 2);
91 try expect(x[2] == 3);
92 }
93 };
94 try S.entry(2);
95 comptime try S.entry(2);
96}
97
98test "double nested array to const slice cast in array literal" {
99 if (@import("builtin").zig_backend == .stage2_c) return error.SkipZigTest; // TODO
100 const S = struct {
101 fn entry(two: i32) !void {
102 const cases = [_][]const []const i32{
103 &[_][]const i32{&[_]i32{1}},
104 &[_][]const i32{&[_]i32{ 2, 3 }},
105 &[_][]const i32{
106 &[_]i32{4},
107 &[_]i32{ 5, 6, 7 },
108 },
109 };
110 try check(&cases);
111
112 const cases2 = [_][]const i32{
113 &[_]i32{1},
114 &[_]i32{ two, 3 },
115 };
116 try expect(cases2.len == 2);
117 try expect(cases2[0].len == 1);
118 try expect(cases2[0][0] == 1);
119 try expect(cases2[1].len == 2);
120 try expect(cases2[1][0] == 2);
121 try expect(cases2[1][1] == 3);
122
123 const cases3 = [_][]const []const i32{
124 &[_][]const i32{&[_]i32{1}},
125 &[_][]const i32{&[_]i32{ two, 3 }},
126 &[_][]const i32{
127 &[_]i32{4},
128 &[_]i32{ 5, 6, 7 },
129 },
130 };
131 try check(&cases3);
132 }
133
134 fn check(cases: []const []const []const i32) !void {
135 try expect(cases.len == 3);
136 try expect(cases[0].len == 1);
137 try expect(cases[0][0].len == 1);
138 try expect(cases[0][0][0] == 1);
139 try expect(cases[1].len == 1);
140 try expect(cases[1][0].len == 2);
141 try expect(cases[1][0][0] == 2);
142 try expect(cases[1][0][1] == 3);
143 try expect(cases[2].len == 2);
144 try expect(cases[2][0].len == 1);
145 try expect(cases[2][0][0] == 4);
146 try expect(cases[2][1].len == 3);
147 try expect(cases[2][1][0] == 5);
148 try expect(cases[2][1][1] == 6);
149 try expect(cases[2][1][2] == 7);
150 }
151 };
152 try S.entry(2);
153 comptime try S.entry(2);
154}
155
156test "anonymous literal in array" {
157 if (@import("builtin").zig_backend == .stage2_c) return error.SkipZigTest; // TODO
158 const S = struct {
159 const Foo = struct {
160 a: usize = 2,
161 b: usize = 4,
162 };
163 fn doTheTest() !void {
164 var array: [2]Foo = .{
165 .{ .a = 3 },
166 .{ .b = 3 },
167 };
168 try expect(array[0].a == 3);
169 try expect(array[0].b == 4);
170 try expect(array[1].a == 2);
171 try expect(array[1].b == 3);
172 }
173 };
174 try S.doTheTest();
175 comptime try S.doTheTest();
176}
177
178test "access the null element of a null terminated array" {
179 if (@import("builtin").zig_backend == .stage2_c) return error.SkipZigTest; // TODO
180 const S = struct {
181 fn doTheTest() !void {
182 var array: [4:0]u8 = .{ 'a', 'o', 'e', 'u' };
183 try expect(array[4] == 0);
184 var len: usize = 4;
185 try expect(array[len] == 0);
186 }
187 };
188 try S.doTheTest();
189 comptime try S.doTheTest();
190}
191
192test "type deduction for array subscript expression" {
193 if (@import("builtin").zig_backend == .stage2_c) return error.SkipZigTest; // TODO
194 const S = struct {
195 fn doTheTest() !void {
196 var array = [_]u8{ 0x55, 0xAA };
197 var v0 = true;
198 try expect(@as(u8, 0xAA) == array[if (v0) 1 else 0]);
199 var v1 = false;
200 try expect(@as(u8, 0x55) == array[if (v1) 1 else 0]);
201 }
202 };
203 try S.doTheTest();
204 comptime try S.doTheTest();
205}
206
207test "sentinel element count towards the ABI size calculation" {
208 if (@import("builtin").zig_backend == .stage2_llvm) return error.SkipZigTest; // TODO
209 if (@import("builtin").zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
210 if (@import("builtin").zig_backend == .stage2_c) return error.SkipZigTest; // TODO
211
212 const S = struct {
213 fn doTheTest() !void {
214 const T = packed struct {
215 fill_pre: u8 = 0x55,
216 data: [0:0]u8 = undefined,
217 fill_post: u8 = 0xAA,
218 };
219 var x = T{};
220 var as_slice = mem.asBytes(&x);
221 try expect(@as(usize, 3) == as_slice.len);
222 try expect(@as(u8, 0x55) == as_slice[0]);
223 try expect(@as(u8, 0xAA) == as_slice[2]);
224 }
225 };
226
227 try S.doTheTest();
228 comptime try S.doTheTest();
229}
230
231test "zero-sized array with recursive type definition" {
232 if (@import("builtin").zig_backend == .stage2_llvm) return error.SkipZigTest; // TODO
233 if (@import("builtin").zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
234 if (@import("builtin").zig_backend == .stage2_c) return error.SkipZigTest; // TODO
235
236 const U = struct {
237 fn foo(comptime T: type, comptime n: usize) type {
238 return struct {
239 s: [n]T,
240 x: usize = n,
241 };
242 }
243 };
244
245 const S = struct {
246 list: U.foo(@This(), 0),
247 };
248
249 var t: S = .{ .list = .{ .s = undefined } };
250 try expect(@as(usize, 0) == t.list.x);
251}
252
253test "type coercion of anon struct literal to array" {
254 if (@import("builtin").zig_backend == .stage2_c) return error.SkipZigTest; // TODO
255 const S = struct {
256 const U = union {
257 a: u32,
258 b: bool,
259 c: []const u8,
260 };
261
262 fn doTheTest() !void {
263 var x1: u8 = 42;
264 const t1 = .{ x1, 56, 54 };
265 var arr1: [3]u8 = t1;
266 try expect(arr1[0] == 42);
267 try expect(arr1[1] == 56);
268 try expect(arr1[2] == 54);
269
270 if (@import("builtin").zig_backend == .stage2_llvm) return error.SkipZigTest; // TODO
271 if (@import("builtin").zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
272
273 var x2: U = .{ .a = 42 };
274 const t2 = .{ x2, .{ .b = true }, .{ .c = "hello" } };
275 var arr2: [3]U = t2;
276 try expect(arr2[0].a == 42);
277 try expect(arr2[1].b == true);
278 try expect(mem.eql(u8, arr2[2].c, "hello"));
279 }
280 };
281 try S.doTheTest();
282 comptime try S.doTheTest();
283}
284
285test "type coercion of pointer to anon struct literal to pointer to array" {
286 if (@import("builtin").zig_backend == .stage2_llvm) return error.SkipZigTest; // TODO
287 if (@import("builtin").zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
288 if (@import("builtin").zig_backend == .stage2_c) return error.SkipZigTest; // TODO
289
290 const S = struct {
291 const U = union {
292 a: u32,
293 b: bool,
294 c: []const u8,
295 };
296
297 fn doTheTest() !void {
298 var x1: u8 = 42;
299 const t1 = &.{ x1, 56, 54 };
300 var arr1: *const [3]u8 = t1;
301 try expect(arr1[0] == 42);
302 try expect(arr1[1] == 56);
303 try expect(arr1[2] == 54);
304
305 var x2: U = .{ .a = 42 };
306 const t2 = &.{ x2, .{ .b = true }, .{ .c = "hello" } };
307 var arr2: *const [3]U = t2;
308 try expect(arr2[0].a == 42);
309 try expect(arr2[1].b == true);
310 try expect(mem.eql(u8, arr2[2].c, "hello"));
311 }
312 };
313 try S.doTheTest();
314 comptime try S.doTheTest();
315}
test/behavior/cast.zig+338-2
......@@ -1,8 +1,9 @@
1const builtin = @import("builtin");
12const std = @import("std");
23const expect = std.testing.expect;
34const mem = std.mem;
45const maxInt = std.math.maxInt;
5const builtin = @import("builtin");
6const native_endian = builtin.target.cpu.arch.endian();
67
78test "int to ptr cast" {
89 const x = @as(usize, 13);
......@@ -93,7 +94,8 @@ test "comptime_int @intToFloat" {
9394}
9495
9596test "@floatToInt" {
96 if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
97 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
98 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
9799
98100 try testFloatToInts();
99101 comptime try testFloatToInts();
......@@ -802,3 +804,337 @@ test "comptime float casts" {
802804 try expectFloatToInt(comptime_int, 1234, i16, 1234);
803805 try expectFloatToInt(comptime_float, 12.3, comptime_int, 12);
804806}
807
808test "pointer reinterpret const float to int" {
809 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
810 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
811 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
812
813 // The hex representation is 0x3fe3333333333303.
814 const float: f64 = 5.99999999999994648725e-01;
815 const float_ptr = &float;
816 const int_ptr = @ptrCast(*const i32, float_ptr);
817 const int_val = int_ptr.*;
818 if (native_endian == .Little)
819 try expect(int_val == 0x33333303)
820 else
821 try expect(int_val == 0x3fe33333);
822}
823
824test "implicit cast from [*]T to ?*anyopaque" {
825 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
826 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
827 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
828
829 var a = [_]u8{ 3, 2, 1 };
830 var runtime_zero: usize = 0;
831 incrementVoidPtrArray(a[runtime_zero..].ptr, 3);
832 try expect(std.mem.eql(u8, &a, &[_]u8{ 4, 3, 2 }));
833}
834
835fn incrementVoidPtrArray(array: ?*anyopaque, len: usize) void {
836 var n: usize = 0;
837 while (n < len) : (n += 1) {
838 @ptrCast([*]u8, array.?)[n] += 1;
839 }
840}
841
842test "compile time int to ptr of function" {
843 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
844 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) return error.SkipZigTest; // TODO
845 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
846 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
847 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
848
849 try foobar(FUNCTION_CONSTANT);
850}
851
852pub const FUNCTION_CONSTANT = @intToPtr(PFN_void, maxInt(usize));
853pub const PFN_void = *const fn (*anyopaque) callconv(.C) void;
854
855fn foobar(func: PFN_void) !void {
856 try std.testing.expect(@ptrToInt(func) == maxInt(usize));
857}
858
859test "implicit ptr to *anyopaque" {
860 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
861 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
862 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
863
864 var a: u32 = 1;
865 var ptr: *align(@alignOf(u32)) anyopaque = &a;
866 var b: *u32 = @ptrCast(*u32, ptr);
867 try expect(b.* == 1);
868 var ptr2: ?*align(@alignOf(u32)) anyopaque = &a;
869 var c: *u32 = @ptrCast(*u32, ptr2.?);
870 try expect(c.* == 1);
871}
872
873test "return null from fn() anyerror!?&T" {
874 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
875 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
876
877 const a = returnNullFromOptionalTypeErrorRef();
878 const b = returnNullLitFromOptionalTypeErrorRef();
879 try expect((try a) == null and (try b) == null);
880}
881fn returnNullFromOptionalTypeErrorRef() anyerror!?*A {
882 const a: ?*A = null;
883 return a;
884}
885fn returnNullLitFromOptionalTypeErrorRef() anyerror!?*A {
886 return null;
887}
888
889test "peer type resolution: [0]u8 and []const u8" {
890 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
891 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
892 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
893
894 try expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);
895 try expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);
896 comptime {
897 try expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);
898 try expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);
899 }
900}
901fn peerTypeEmptyArrayAndSlice(a: bool, slice: []const u8) []const u8 {
902 if (a) {
903 return &[_]u8{};
904 }
905
906 return slice[0..1];
907}
908
909test "implicitly cast from [N]T to ?[]const T" {
910 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
911 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
912 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
913
914 try expect(mem.eql(u8, castToOptionalSlice().?, "hi"));
915 comptime try expect(mem.eql(u8, castToOptionalSlice().?, "hi"));
916}
917
918fn castToOptionalSlice() ?[]const u8 {
919 return "hi";
920}
921
922test "cast u128 to f128 and back" {
923 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
924 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
925 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
926
927 comptime try testCast128();
928 try testCast128();
929}
930
931fn testCast128() !void {
932 try expect(cast128Int(cast128Float(0x7fff0000000000000000000000000000)) == 0x7fff0000000000000000000000000000);
933}
934
935fn cast128Int(x: f128) u128 {
936 return @bitCast(u128, x);
937}
938
939fn cast128Float(x: u128) f128 {
940 return @bitCast(f128, x);
941}
942
943test "implicit cast from *[N]T to ?[*]T" {
944 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
945 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
946 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
947
948 var x: ?[*]u16 = null;
949 var y: [4]u16 = [4]u16{ 0, 1, 2, 3 };
950
951 x = &y;
952 try expect(std.mem.eql(u16, x.?[0..4], y[0..4]));
953 x.?[0] = 8;
954 y[3] = 6;
955 try expect(std.mem.eql(u16, x.?[0..4], y[0..4]));
956}
957
958test "implicit cast from *T to ?*anyopaque" {
959 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
960 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
961 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
962
963 var a: u8 = 1;
964 incrementVoidPtrValue(&a);
965 try std.testing.expect(a == 2);
966}
967
968fn incrementVoidPtrValue(value: ?*anyopaque) void {
969 @ptrCast(*u8, value.?).* += 1;
970}
971
972test "implicit cast *[0]T to E![]const u8" {
973 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
974 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
975 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
976
977 var x = @as(anyerror![]const u8, &[0]u8{});
978 try expect((x catch unreachable).len == 0);
979}
980
981var global_array: [4]u8 = undefined;
982test "cast from array reference to fn: comptime fn ptr" {
983 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
984 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
985
986 const f = @ptrCast(*const fn () callconv(.C) void, &global_array);
987 try expect(@ptrToInt(f) == @ptrToInt(&global_array));
988}
989test "cast from array reference to fn: runtime fn ptr" {
990 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
991 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
992 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
993
994 var f = @ptrCast(*const fn () callconv(.C) void, &global_array);
995 try expect(@ptrToInt(f) == @ptrToInt(&global_array));
996}
997
998test "*const [N]null u8 to ?[]const u8" {
999 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
1000 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
1001 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
1002
1003 const S = struct {
1004 fn doTheTest() !void {
1005 var a = "Hello";
1006 var b: ?[]const u8 = a;
1007 try expect(mem.eql(u8, b.?, "Hello"));
1008 }
1009 };
1010 try S.doTheTest();
1011 comptime try S.doTheTest();
1012}
1013
1014test "cast between [*c]T and ?[*:0]T on fn parameter" {
1015 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
1016 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
1017
1018 const S = struct {
1019 const Handler = ?fn ([*c]const u8) callconv(.C) void;
1020 fn addCallback(handler: Handler) void {
1021 _ = handler;
1022 }
1023
1024 fn myCallback(cstr: ?[*:0]const u8) callconv(.C) void {
1025 _ = cstr;
1026 }
1027
1028 fn doTheTest() void {
1029 addCallback(myCallback);
1030 }
1031 };
1032 S.doTheTest();
1033}
1034
1035var global_struct: struct { f0: usize } = undefined;
1036test "assignment to optional pointer result loc" {
1037 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
1038 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
1039 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
1040
1041 var foo: struct { ptr: ?*anyopaque } = .{ .ptr = &global_struct };
1042 try expect(foo.ptr.? == @ptrCast(*anyopaque, &global_struct));
1043}
1044
1045test "cast between *[N]void and []void" {
1046 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
1047 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
1048
1049 var a: [4]void = undefined;
1050 var b: []void = &a;
1051 try expect(b.len == 4);
1052}
1053
1054test "peer resolve arrays of different size to const slice" {
1055 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
1056 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
1057
1058 try expect(mem.eql(u8, boolToStr(true), "true"));
1059 try expect(mem.eql(u8, boolToStr(false), "false"));
1060 comptime try expect(mem.eql(u8, boolToStr(true), "true"));
1061 comptime try expect(mem.eql(u8, boolToStr(false), "false"));
1062}
1063fn boolToStr(b: bool) []const u8 {
1064 return if (b) "true" else "false";
1065}
1066
1067test "cast f16 to wider types" {
1068 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
1069 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
1070 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
1071 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
1072
1073 const S = struct {
1074 fn doTheTest() !void {
1075 var x: f16 = 1234.0;
1076 try expect(@as(f32, 1234.0) == x);
1077 try expect(@as(f64, 1234.0) == x);
1078 try expect(@as(f128, 1234.0) == x);
1079 }
1080 };
1081 try S.doTheTest();
1082 comptime try S.doTheTest();
1083}
1084
1085test "cast f128 to narrower types" {
1086 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
1087 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
1088 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
1089 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
1090
1091 const S = struct {
1092 fn doTheTest() !void {
1093 var x: f128 = 1234.0;
1094 try expect(@as(f16, 1234.0) == @floatCast(f16, x));
1095 try expect(@as(f32, 1234.0) == @floatCast(f32, x));
1096 try expect(@as(f64, 1234.0) == @floatCast(f64, x));
1097 }
1098 };
1099 try S.doTheTest();
1100 comptime try S.doTheTest();
1101}
1102
1103test "peer type resolution: unreachable, null, slice" {
1104 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
1105 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
1106 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
1107
1108 const S = struct {
1109 fn doTheTest(num: usize, word: []const u8) !void {
1110 const result = switch (num) {
1111 0 => null,
1112 1 => word,
1113 else => unreachable,
1114 };
1115 try expect(mem.eql(u8, result.?, "hi"));
1116 }
1117 };
1118 try S.doTheTest(1, "hi");
1119}
1120
1121test "cast i8 fn call peers to i32 result" {
1122 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
1123 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
1124
1125 const S = struct {
1126 fn doTheTest() !void {
1127 var cond = true;
1128 const value: i32 = if (cond) smallBoi() else bigBoi();
1129 try expect(value == 123);
1130 }
1131 fn smallBoi() i8 {
1132 return 123;
1133 }
1134 fn bigBoi() i16 {
1135 return 1234;
1136 }
1137 };
1138 try S.doTheTest();
1139 comptime try S.doTheTest();
1140}
test/behavior/cast_llvm.zig deleted-299
......@@ -1,299 +0,0 @@
1const builtin = @import("builtin");
2const std = @import("std");
3const expect = std.testing.expect;
4const mem = std.mem;
5const maxInt = std.math.maxInt;
6const native_endian = builtin.target.cpu.arch.endian();
7
8test "pointer reinterpret const float to int" {
9 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
10
11 // The hex representation is 0x3fe3333333333303.
12 const float: f64 = 5.99999999999994648725e-01;
13 const float_ptr = &float;
14 const int_ptr = @ptrCast(*const i32, float_ptr);
15 const int_val = int_ptr.*;
16 if (native_endian == .Little)
17 try expect(int_val == 0x33333303)
18 else
19 try expect(int_val == 0x3fe33333);
20}
21
22test "@floatToInt" {
23 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
24
25 try testFloatToInts();
26 comptime try testFloatToInts();
27}
28
29fn testFloatToInts() !void {
30 try expectFloatToInt(f16, 255.1, u8, 255);
31 try expectFloatToInt(f16, 127.2, i8, 127);
32 try expectFloatToInt(f16, -128.2, i8, -128);
33}
34
35fn expectFloatToInt(comptime F: type, f: F, comptime I: type, i: I) !void {
36 try expect(@floatToInt(I, f) == i);
37}
38
39test "implicit cast from [*]T to ?*anyopaque" {
40 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
41
42 var a = [_]u8{ 3, 2, 1 };
43 var runtime_zero: usize = 0;
44 incrementVoidPtrArray(a[runtime_zero..].ptr, 3);
45 try expect(std.mem.eql(u8, &a, &[_]u8{ 4, 3, 2 }));
46}
47
48fn incrementVoidPtrArray(array: ?*anyopaque, len: usize) void {
49 var n: usize = 0;
50 while (n < len) : (n += 1) {
51 @ptrCast([*]u8, array.?)[n] += 1;
52 }
53}
54
55test "compile time int to ptr of function" {
56 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
57 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) return error.SkipZigTest; // TODO
58 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
59
60 try foobar(FUNCTION_CONSTANT);
61}
62
63pub const FUNCTION_CONSTANT = @intToPtr(PFN_void, maxInt(usize));
64pub const PFN_void = *const fn (*anyopaque) callconv(.C) void;
65
66fn foobar(func: PFN_void) !void {
67 try std.testing.expect(@ptrToInt(func) == maxInt(usize));
68}
69
70test "implicit ptr to *anyopaque" {
71 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
72
73 var a: u32 = 1;
74 var ptr: *align(@alignOf(u32)) anyopaque = &a;
75 var b: *u32 = @ptrCast(*u32, ptr);
76 try expect(b.* == 1);
77 var ptr2: ?*align(@alignOf(u32)) anyopaque = &a;
78 var c: *u32 = @ptrCast(*u32, ptr2.?);
79 try expect(c.* == 1);
80}
81
82const A = struct {
83 a: i32,
84};
85test "return null from fn() anyerror!?&T" {
86 const a = returnNullFromOptionalTypeErrorRef();
87 const b = returnNullLitFromOptionalTypeErrorRef();
88 try expect((try a) == null and (try b) == null);
89}
90fn returnNullFromOptionalTypeErrorRef() anyerror!?*A {
91 const a: ?*A = null;
92 return a;
93}
94fn returnNullLitFromOptionalTypeErrorRef() anyerror!?*A {
95 return null;
96}
97
98test "peer type resolution: [0]u8 and []const u8" {
99 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
100 try expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);
101 try expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);
102 comptime {
103 try expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);
104 try expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);
105 }
106}
107fn peerTypeEmptyArrayAndSlice(a: bool, slice: []const u8) []const u8 {
108 if (a) {
109 return &[_]u8{};
110 }
111
112 return slice[0..1];
113}
114
115test "implicitly cast from [N]T to ?[]const T" {
116 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
117 try expect(mem.eql(u8, castToOptionalSlice().?, "hi"));
118 comptime try expect(mem.eql(u8, castToOptionalSlice().?, "hi"));
119}
120
121fn castToOptionalSlice() ?[]const u8 {
122 return "hi";
123}
124
125test "cast u128 to f128 and back" {
126 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
127 comptime try testCast128();
128 try testCast128();
129}
130
131fn testCast128() !void {
132 try expect(cast128Int(cast128Float(0x7fff0000000000000000000000000000)) == 0x7fff0000000000000000000000000000);
133}
134
135fn cast128Int(x: f128) u128 {
136 return @bitCast(u128, x);
137}
138
139fn cast128Float(x: u128) f128 {
140 return @bitCast(f128, x);
141}
142
143test "implicit cast from *[N]T to ?[*]T" {
144 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
145 var x: ?[*]u16 = null;
146 var y: [4]u16 = [4]u16{ 0, 1, 2, 3 };
147
148 x = &y;
149 try expect(std.mem.eql(u16, x.?[0..4], y[0..4]));
150 x.?[0] = 8;
151 y[3] = 6;
152 try expect(std.mem.eql(u16, x.?[0..4], y[0..4]));
153}
154
155test "implicit cast from *T to ?*anyopaque" {
156 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
157 var a: u8 = 1;
158 incrementVoidPtrValue(&a);
159 try std.testing.expect(a == 2);
160}
161
162fn incrementVoidPtrValue(value: ?*anyopaque) void {
163 @ptrCast(*u8, value.?).* += 1;
164}
165
166test "implicit cast *[0]T to E![]const u8" {
167 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
168 var x = @as(anyerror![]const u8, &[0]u8{});
169 try expect((x catch unreachable).len == 0);
170}
171
172var global_array: [4]u8 = undefined;
173test "cast from array reference to fn: comptime fn ptr" {
174 const f = @ptrCast(*const fn () callconv(.C) void, &global_array);
175 try expect(@ptrToInt(f) == @ptrToInt(&global_array));
176}
177test "cast from array reference to fn: runtime fn ptr" {
178 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
179 var f = @ptrCast(*const fn () callconv(.C) void, &global_array);
180 try expect(@ptrToInt(f) == @ptrToInt(&global_array));
181}
182
183test "*const [N]null u8 to ?[]const u8" {
184 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
185 const S = struct {
186 fn doTheTest() !void {
187 var a = "Hello";
188 var b: ?[]const u8 = a;
189 try expect(mem.eql(u8, b.?, "Hello"));
190 }
191 };
192 try S.doTheTest();
193 comptime try S.doTheTest();
194}
195
196test "cast between [*c]T and ?[*:0]T on fn parameter" {
197 const S = struct {
198 const Handler = ?fn ([*c]const u8) callconv(.C) void;
199 fn addCallback(handler: Handler) void {
200 _ = handler;
201 }
202
203 fn myCallback(cstr: ?[*:0]const u8) callconv(.C) void {
204 _ = cstr;
205 }
206
207 fn doTheTest() void {
208 addCallback(myCallback);
209 }
210 };
211 S.doTheTest();
212}
213
214var global_struct: struct { f0: usize } = undefined;
215test "assignment to optional pointer result loc" {
216 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
217 var foo: struct { ptr: ?*anyopaque } = .{ .ptr = &global_struct };
218 try expect(foo.ptr.? == @ptrCast(*anyopaque, &global_struct));
219}
220
221test "cast between *[N]void and []void" {
222 var a: [4]void = undefined;
223 var b: []void = &a;
224 try expect(b.len == 4);
225}
226
227test "peer resolve arrays of different size to const slice" {
228 try expect(mem.eql(u8, boolToStr(true), "true"));
229 try expect(mem.eql(u8, boolToStr(false), "false"));
230 comptime try expect(mem.eql(u8, boolToStr(true), "true"));
231 comptime try expect(mem.eql(u8, boolToStr(false), "false"));
232}
233fn boolToStr(b: bool) []const u8 {
234 return if (b) "true" else "false";
235}
236
237test "cast f16 to wider types" {
238 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
239 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
240 const S = struct {
241 fn doTheTest() !void {
242 var x: f16 = 1234.0;
243 try expect(@as(f32, 1234.0) == x);
244 try expect(@as(f64, 1234.0) == x);
245 try expect(@as(f128, 1234.0) == x);
246 }
247 };
248 try S.doTheTest();
249 comptime try S.doTheTest();
250}
251
252test "cast f128 to narrower types" {
253 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
254 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
255
256 const S = struct {
257 fn doTheTest() !void {
258 var x: f128 = 1234.0;
259 try expect(@as(f16, 1234.0) == @floatCast(f16, x));
260 try expect(@as(f32, 1234.0) == @floatCast(f32, x));
261 try expect(@as(f64, 1234.0) == @floatCast(f64, x));
262 }
263 };
264 try S.doTheTest();
265 comptime try S.doTheTest();
266}
267
268test "peer type resolution: unreachable, null, slice" {
269 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
270 const S = struct {
271 fn doTheTest(num: usize, word: []const u8) !void {
272 const result = switch (num) {
273 0 => null,
274 1 => word,
275 else => unreachable,
276 };
277 try expect(mem.eql(u8, result.?, "hi"));
278 }
279 };
280 try S.doTheTest(1, "hi");
281}
282
283test "cast i8 fn call peers to i32 result" {
284 const S = struct {
285 fn doTheTest() !void {
286 var cond = true;
287 const value: i32 = if (cond) smallBoi() else bigBoi();
288 try expect(value == 123);
289 }
290 fn smallBoi() i8 {
291 return 123;
292 }
293 fn bigBoi() i16 {
294 return 1234;
295 }
296 };
297 try S.doTheTest();
298 comptime try S.doTheTest();
299}
test/behavior/error.zig+26
......@@ -478,3 +478,29 @@ test "error union comptime caching" {
478478 S.quux(@as(anyerror!void, {}));
479479 S.quux(@as(anyerror!void, {}));
480480}
481
482test "@errorName" {
483 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
484 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
485
486 try expect(mem.eql(u8, @errorName(error.AnError), "AnError"));
487 try expect(mem.eql(u8, @errorName(error.ALongerErrorName), "ALongerErrorName"));
488 try expect(mem.eql(u8, @errorName(gimmeItBroke()), "ItBroke"));
489}
490fn gimmeItBroke() anyerror {
491 return error.ItBroke;
492}
493
494test "@errorName sentinel length matches slice length" {
495 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
496 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
497
498 const name = testBuiltinErrorName(error.FooBar);
499 const length: usize = 6;
500 try expect(length == std.mem.indexOfSentinel(u8, 0, name.ptr));
501 try expect(length == name.len);
502}
503
504pub fn testBuiltinErrorName(err: anyerror) [:0]const u8 {
505 return @errorName(err);
506}
test/behavior/error_llvm.zig deleted-24
......@@ -1,24 +0,0 @@
1const std = @import("std");
2const expect = std.testing.expect;
3const mem = std.mem;
4
5fn gimmeItBroke() anyerror {
6 return error.ItBroke;
7}
8
9test "@errorName" {
10 try expect(mem.eql(u8, @errorName(error.AnError), "AnError"));
11 try expect(mem.eql(u8, @errorName(error.ALongerErrorName), "ALongerErrorName"));
12 try expect(mem.eql(u8, @errorName(gimmeItBroke()), "ItBroke"));
13}
14
15test "@errorName sentinel length matches slice length" {
16 const name = testBuiltinErrorName(error.FooBar);
17 const length: usize = 6;
18 try expect(length == std.mem.indexOfSentinel(u8, 0, name.ptr));
19 try expect(length == name.len);
20}
21
22pub fn testBuiltinErrorName(err: anyerror) [:0]const u8 {
23 return @errorName(err);
24}
test/behavior/sizeof_and_typeof.zig+113
......@@ -1,3 +1,4 @@
1const builtin = @import("builtin");
12const std = @import("std");
23const expect = std.testing.expect;
34const expectEqual = std.testing.expectEqual;
......@@ -160,3 +161,115 @@ test "@bitOffsetOf" {
160161 try expect(@offsetOf(A, "f") * 8 == @bitOffsetOf(A, "f"));
161162 try expect(@offsetOf(A, "g") * 8 == @bitOffsetOf(A, "g"));
162163}
164
165test "@sizeOf(T) == 0 doesn't force resolving struct size" {
166 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
167
168 const S = struct {
169 const Foo = struct {
170 y: if (@sizeOf(Foo) == 0) u64 else u32,
171 };
172 const Bar = struct {
173 x: i32,
174 y: if (0 == @sizeOf(Bar)) u64 else u32,
175 };
176 };
177
178 try expect(@sizeOf(S.Foo) == 4);
179 try expect(@sizeOf(S.Bar) == 8);
180}
181
182test "@TypeOf() has no runtime side effects" {
183 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
184
185 const S = struct {
186 fn foo(comptime T: type, ptr: *T) T {
187 ptr.* += 1;
188 return ptr.*;
189 }
190 };
191 var data: i32 = 0;
192 const T = @TypeOf(S.foo(i32, &data));
193 comptime try expect(T == i32);
194 try expect(data == 0);
195}
196
197test "branching logic inside @TypeOf" {
198 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
199
200 const S = struct {
201 var data: i32 = 0;
202 fn foo() anyerror!i32 {
203 data += 1;
204 return undefined;
205 }
206 };
207 const T = @TypeOf(S.foo() catch undefined);
208 comptime try expect(T == i32);
209 try expect(S.data == 0);
210}
211
212test "@bitSizeOf" {
213 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
214
215 try expect(@bitSizeOf(u2) == 2);
216 try expect(@bitSizeOf(u8) == @sizeOf(u8) * 8);
217 try expect(@bitSizeOf(struct {
218 a: u2,
219 }) == 8);
220 try expect(@bitSizeOf(packed struct {
221 a: u2,
222 }) == 2);
223}
224
225test "@sizeOf comparison against zero" {
226 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
227
228 const S0 = struct {
229 f: *@This(),
230 };
231 const U0 = union {
232 f: *@This(),
233 };
234 const S1 = struct {
235 fn H(comptime T: type) type {
236 return struct {
237 x: T,
238 };
239 }
240 f0: H(*@This()),
241 f1: H(**@This()),
242 f2: H(***@This()),
243 };
244 const U1 = union {
245 fn H(comptime T: type) type {
246 return struct {
247 x: T,
248 };
249 }
250 f0: H(*@This()),
251 f1: H(**@This()),
252 f2: H(***@This()),
253 };
254 const S = struct {
255 fn doTheTest(comptime T: type, comptime result: bool) !void {
256 try expectEqual(result, @sizeOf(T) > 0);
257 }
258 };
259 // Zero-sized type
260 try S.doTheTest(u0, false);
261 try S.doTheTest(*u0, false);
262 // Non byte-sized type
263 try S.doTheTest(u1, true);
264 try S.doTheTest(*u1, true);
265 // Regular type
266 try S.doTheTest(u8, true);
267 try S.doTheTest(*u8, true);
268 try S.doTheTest(f32, true);
269 try S.doTheTest(*f32, true);
270 // Container with ptr pointing to themselves
271 try S.doTheTest(S0, true);
272 try S.doTheTest(U0, true);
273 try S.doTheTest(S1, true);
274 try S.doTheTest(U1, true);
275}
test/behavior/sizeof_and_typeof_stage1.zig deleted-105
......@@ -1,105 +0,0 @@
1const std = @import("std");
2const expect = std.testing.expect;
3const expectEqual = std.testing.expectEqual;
4
5test "@sizeOf(T) == 0 doesn't force resolving struct size" {
6 const S = struct {
7 const Foo = struct {
8 y: if (@sizeOf(Foo) == 0) u64 else u32,
9 };
10 const Bar = struct {
11 x: i32,
12 y: if (0 == @sizeOf(Bar)) u64 else u32,
13 };
14 };
15
16 try expect(@sizeOf(S.Foo) == 4);
17 try expect(@sizeOf(S.Bar) == 8);
18}
19
20test "@TypeOf() has no runtime side effects" {
21 const S = struct {
22 fn foo(comptime T: type, ptr: *T) T {
23 ptr.* += 1;
24 return ptr.*;
25 }
26 };
27 var data: i32 = 0;
28 const T = @TypeOf(S.foo(i32, &data));
29 comptime try expect(T == i32);
30 try expect(data == 0);
31}
32
33test "branching logic inside @TypeOf" {
34 const S = struct {
35 var data: i32 = 0;
36 fn foo() anyerror!i32 {
37 data += 1;
38 return undefined;
39 }
40 };
41 const T = @TypeOf(S.foo() catch undefined);
42 comptime try expect(T == i32);
43 try expect(S.data == 0);
44}
45
46test "@bitSizeOf" {
47 try expect(@bitSizeOf(u2) == 2);
48 try expect(@bitSizeOf(u8) == @sizeOf(u8) * 8);
49 try expect(@bitSizeOf(struct {
50 a: u2,
51 }) == 8);
52 try expect(@bitSizeOf(packed struct {
53 a: u2,
54 }) == 2);
55}
56
57test "@sizeOf comparison against zero" {
58 const S0 = struct {
59 f: *@This(),
60 };
61 const U0 = union {
62 f: *@This(),
63 };
64 const S1 = struct {
65 fn H(comptime T: type) type {
66 return struct {
67 x: T,
68 };
69 }
70 f0: H(*@This()),
71 f1: H(**@This()),
72 f2: H(***@This()),
73 };
74 const U1 = union {
75 fn H(comptime T: type) type {
76 return struct {
77 x: T,
78 };
79 }
80 f0: H(*@This()),
81 f1: H(**@This()),
82 f2: H(***@This()),
83 };
84 const S = struct {
85 fn doTheTest(comptime T: type, comptime result: bool) !void {
86 try expectEqual(result, @sizeOf(T) > 0);
87 }
88 };
89 // Zero-sized type
90 try S.doTheTest(u0, false);
91 try S.doTheTest(*u0, false);
92 // Non byte-sized type
93 try S.doTheTest(u1, true);
94 try S.doTheTest(*u1, true);
95 // Regular type
96 try S.doTheTest(u8, true);
97 try S.doTheTest(*u8, true);
98 try S.doTheTest(f32, true);
99 try S.doTheTest(*f32, true);
100 // Container with ptr pointing to themselves
101 try S.doTheTest(S0, true);
102 try S.doTheTest(U0, true);
103 try S.doTheTest(S1, true);
104 try S.doTheTest(U1, true);
105}
test/behavior/struct.zig+885-9
......@@ -133,15 +133,6 @@ fn returnEmptyStructInstance() StructWithNoFields {
133133 return empty_global_instance;
134134}
135135
136const Node = struct {
137 val: Val,
138 next: *Node,
139};
140
141const Val = struct {
142 x: i32,
143};
144
145136test "fn call of struct field" {
146137 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
147138
......@@ -294,3 +285,888 @@ const blah: packed struct {
294285test "bit field alignment" {
295286 try expect(@TypeOf(&blah.b) == *align(1:3:1) const u3);
296287}
288
289const Node = struct {
290 val: Val,
291 next: *Node,
292};
293
294const Val = struct {
295 x: i32,
296};
297
298test "struct point to self" {
299 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
300 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
301 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
302 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
303
304 var root: Node = undefined;
305 root.val.x = 1;
306
307 var node: Node = undefined;
308 node.next = &root;
309 node.val.x = 2;
310
311 root.next = &node;
312
313 try expect(node.next.next.next.val.x == 1);
314}
315
316test "void struct fields" {
317 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
318 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
319 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
320 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
321
322 const foo = VoidStructFieldsFoo{
323 .a = void{},
324 .b = 1,
325 .c = void{},
326 };
327 try expect(foo.b == 1);
328 try expect(@sizeOf(VoidStructFieldsFoo) == 4);
329}
330const VoidStructFieldsFoo = struct {
331 a: void,
332 b: i32,
333 c: void,
334};
335
336test "return empty struct from fn" {
337 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
338 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
339 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
340 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
341
342 _ = testReturnEmptyStructFromFn();
343}
344const EmptyStruct2 = struct {};
345fn testReturnEmptyStructFromFn() EmptyStruct2 {
346 return EmptyStruct2{};
347}
348
349test "pass slice of empty struct to fn" {
350 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
351 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
352 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
353 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
354
355 try expect(testPassSliceOfEmptyStructToFn(&[_]EmptyStruct2{EmptyStruct2{}}) == 1);
356}
357fn testPassSliceOfEmptyStructToFn(slice: []const EmptyStruct2) usize {
358 return slice.len;
359}
360
361test "self-referencing struct via array member" {
362 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
363 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
364 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
365 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
366
367 const T = struct {
368 children: [1]*@This(),
369 };
370 var x: T = undefined;
371 x = T{ .children = .{&x} };
372 try expect(x.children[0] == &x);
373}
374
375test "empty struct method call" {
376 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
377 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
378 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
379 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
380
381 const es = EmptyStruct{};
382 try expect(es.method() == 1234);
383}
384const EmptyStruct = struct {
385 fn method(es: *const EmptyStruct) i32 {
386 _ = es;
387 return 1234;
388 }
389};
390
391test "align 1 field before self referential align 8 field as slice return type" {
392 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
393 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
394 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
395 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
396
397 const result = alloc(Expr);
398 try expect(result.len == 0);
399}
400
401const Expr = union(enum) {
402 Literal: u8,
403 Question: *Expr,
404};
405
406fn alloc(comptime T: type) []T {
407 return &[_]T{};
408}
409
410const APackedStruct = packed struct {
411 x: u8,
412 y: u8,
413};
414
415test "packed struct" {
416 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
417 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
418 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
419 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
420
421 var foo = APackedStruct{
422 .x = 1,
423 .y = 2,
424 };
425 foo.y += 1;
426 const four = foo.x + foo.y;
427 try expect(four == 4);
428}
429
430const Foo24Bits = packed struct {
431 field: u24,
432};
433const Foo96Bits = packed struct {
434 a: u24,
435 b: u24,
436 c: u24,
437 d: u24,
438};
439
440test "packed struct 24bits" {
441 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
442 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
443 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
444 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
445
446 comptime {
447 try expect(@sizeOf(Foo24Bits) == 4);
448 if (@sizeOf(usize) == 4) {
449 try expect(@sizeOf(Foo96Bits) == 12);
450 } else {
451 try expect(@sizeOf(Foo96Bits) == 16);
452 }
453 }
454
455 var value = Foo96Bits{
456 .a = 0,
457 .b = 0,
458 .c = 0,
459 .d = 0,
460 };
461 value.a += 1;
462 try expect(value.a == 1);
463 try expect(value.b == 0);
464 try expect(value.c == 0);
465 try expect(value.d == 0);
466
467 value.b += 1;
468 try expect(value.a == 1);
469 try expect(value.b == 1);
470 try expect(value.c == 0);
471 try expect(value.d == 0);
472
473 value.c += 1;
474 try expect(value.a == 1);
475 try expect(value.b == 1);
476 try expect(value.c == 1);
477 try expect(value.d == 0);
478
479 value.d += 1;
480 try expect(value.a == 1);
481 try expect(value.b == 1);
482 try expect(value.c == 1);
483 try expect(value.d == 1);
484}
485
486test "runtime struct initialization of bitfield" {
487 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
488 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
489 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
490 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
491
492 const s1 = Nibbles{
493 .x = x1,
494 .y = x1,
495 };
496 const s2 = Nibbles{
497 .x = @intCast(u4, x2),
498 .y = @intCast(u4, x2),
499 };
500
501 try expect(s1.x == x1);
502 try expect(s1.y == x1);
503 try expect(s2.x == @intCast(u4, x2));
504 try expect(s2.y == @intCast(u4, x2));
505}
506
507var x1 = @as(u4, 1);
508var x2 = @as(u8, 2);
509
510const Nibbles = packed struct {
511 x: u4,
512 y: u4,
513};
514
515const Bitfields = packed struct {
516 f1: u16,
517 f2: u16,
518 f3: u8,
519 f4: u8,
520 f5: u4,
521 f6: u4,
522 f7: u8,
523};
524
525test "native bit field understands endianness" {
526 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
527 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
528 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
529 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
530
531 var all: u64 = if (native_endian != .Little)
532 0x1111222233445677
533 else
534 0x7765443322221111;
535 var bytes: [8]u8 = undefined;
536 @memcpy(&bytes, @ptrCast([*]u8, &all), 8);
537 var bitfields = @ptrCast(*Bitfields, &bytes).*;
538
539 try expect(bitfields.f1 == 0x1111);
540 try expect(bitfields.f2 == 0x2222);
541 try expect(bitfields.f3 == 0x33);
542 try expect(bitfields.f4 == 0x44);
543 try expect(bitfields.f5 == 0x5);
544 try expect(bitfields.f6 == 0x6);
545 try expect(bitfields.f7 == 0x77);
546}
547
548test "implicit cast packed struct field to const ptr" {
549 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
550 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
551 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
552 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
553
554 const LevelUpMove = packed struct {
555 move_id: u9,
556 level: u7,
557
558 fn toInt(value: u7) u7 {
559 return value;
560 }
561 };
562
563 var lup: LevelUpMove = undefined;
564 lup.level = 12;
565 const res = LevelUpMove.toInt(lup.level);
566 try expect(res == 12);
567}
568
569test "zero-bit field in packed struct" {
570 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
571 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
572 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
573 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
574
575 const S = packed struct {
576 x: u10,
577 y: void,
578 };
579 var x: S = undefined;
580 _ = x;
581}
582
583test "packed struct with non-ABI-aligned field" {
584 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
585 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
586 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
587 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
588
589 const S = packed struct {
590 x: u9,
591 y: u183,
592 };
593 var s: S = undefined;
594 s.x = 1;
595 s.y = 42;
596 try expect(s.x == 1);
597 try expect(s.y == 42);
598}
599
600const BitField1 = packed struct {
601 a: u3,
602 b: u3,
603 c: u2,
604};
605
606const bit_field_1 = BitField1{
607 .a = 1,
608 .b = 2,
609 .c = 3,
610};
611
612test "bit field access" {
613 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
614 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
615 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
616 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
617
618 var data = bit_field_1;
619 try expect(getA(&data) == 1);
620 try expect(getB(&data) == 2);
621 try expect(getC(&data) == 3);
622 comptime try expect(@sizeOf(BitField1) == 1);
623
624 data.b += 1;
625 try expect(data.b == 3);
626
627 data.a += 1;
628 try expect(data.a == 2);
629 try expect(data.b == 3);
630}
631
632fn getA(data: *const BitField1) u3 {
633 return data.a;
634}
635
636fn getB(data: *const BitField1) u3 {
637 return data.b;
638}
639
640fn getC(data: *const BitField1) u2 {
641 return data.c;
642}
643
644test "default struct initialization fields" {
645 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
646 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
647 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
648 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
649
650 const S = struct {
651 a: i32 = 1234,
652 b: i32,
653 };
654 const x = S{
655 .b = 5,
656 };
657 var five: i32 = 5;
658 const y = S{
659 .b = five,
660 };
661 if (x.a + x.b != 1239) {
662 @compileError("it should be comptime known");
663 }
664 try expect(y.a == x.a);
665 try expect(y.b == x.b);
666 try expect(1239 == x.a + x.b);
667}
668
669// TODO revisit this test when doing https://github.com/ziglang/zig/issues/1512
670test "packed array 24bits" {
671 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
672
673 comptime {
674 try expect(@sizeOf([9]Foo32Bits) == 9 * 4);
675 try expect(@sizeOf(FooArray24Bits) == 2 + 2 * 4 + 2);
676 }
677
678 var bytes = [_]u8{0} ** (@sizeOf(FooArray24Bits) + 1);
679 bytes[bytes.len - 1] = 0xaa;
680 const ptr = &std.mem.bytesAsSlice(FooArray24Bits, bytes[0 .. bytes.len - 1])[0];
681 try expect(ptr.a == 0);
682 try expect(ptr.b[0].field == 0);
683 try expect(ptr.b[1].field == 0);
684 try expect(ptr.c == 0);
685
686 ptr.a = maxInt(u16);
687 try expect(ptr.a == maxInt(u16));
688 try expect(ptr.b[0].field == 0);
689 try expect(ptr.b[1].field == 0);
690 try expect(ptr.c == 0);
691
692 ptr.b[0].field = maxInt(u24);
693 try expect(ptr.a == maxInt(u16));
694 try expect(ptr.b[0].field == maxInt(u24));
695 try expect(ptr.b[1].field == 0);
696 try expect(ptr.c == 0);
697
698 ptr.b[1].field = maxInt(u24);
699 try expect(ptr.a == maxInt(u16));
700 try expect(ptr.b[0].field == maxInt(u24));
701 try expect(ptr.b[1].field == maxInt(u24));
702 try expect(ptr.c == 0);
703
704 ptr.c = maxInt(u16);
705 try expect(ptr.a == maxInt(u16));
706 try expect(ptr.b[0].field == maxInt(u24));
707 try expect(ptr.b[1].field == maxInt(u24));
708 try expect(ptr.c == maxInt(u16));
709
710 try expect(bytes[bytes.len - 1] == 0xaa);
711}
712
713const Foo32Bits = packed struct {
714 field: u24,
715 pad: u8,
716};
717
718const FooArray24Bits = packed struct {
719 a: u16,
720 b: [2]Foo32Bits,
721 c: u16,
722};
723
724test "aligned array of packed struct" {
725 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
726
727 comptime {
728 try expect(@sizeOf(FooStructAligned) == 2);
729 try expect(@sizeOf(FooArrayOfAligned) == 2 * 2);
730 }
731
732 var bytes = [_]u8{0xbb} ** @sizeOf(FooArrayOfAligned);
733 const ptr = &std.mem.bytesAsSlice(FooArrayOfAligned, bytes[0..])[0];
734
735 try expect(ptr.a[0].a == 0xbb);
736 try expect(ptr.a[0].b == 0xbb);
737 try expect(ptr.a[1].a == 0xbb);
738 try expect(ptr.a[1].b == 0xbb);
739}
740
741const FooStructAligned = packed struct {
742 a: u8,
743 b: u8,
744};
745
746const FooArrayOfAligned = packed struct {
747 a: [2]FooStructAligned,
748};
749
750test "pointer to packed struct member in a stack variable" {
751 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
752
753 const S = packed struct {
754 a: u2,
755 b: u2,
756 };
757
758 var s = S{ .a = 2, .b = 0 };
759 var b_ptr = &s.b;
760 try expect(s.b == 0);
761 b_ptr.* = 2;
762 try expect(s.b == 2);
763}
764
765test "non-byte-aligned array inside packed struct" {
766 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
767
768 const Foo = packed struct {
769 a: bool,
770 b: [0x16]u8,
771 };
772 const S = struct {
773 fn bar(slice: []const u8) !void {
774 try expectEqualSlices(u8, slice, "abcdefghijklmnopqurstu");
775 }
776 fn doTheTest() !void {
777 var foo = Foo{
778 .a = true,
779 .b = "abcdefghijklmnopqurstu".*,
780 };
781 const value = foo.b;
782 try bar(&value);
783 }
784 };
785 try S.doTheTest();
786 comptime try S.doTheTest();
787}
788
789test "packed struct with u0 field access" {
790 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
791
792 const S = packed struct {
793 f0: u0,
794 };
795 var s = S{ .f0 = 0 };
796 comptime try expect(s.f0 == 0);
797}
798
799test "access to global struct fields" {
800 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
801
802 g_foo.bar.value = 42;
803 try expect(g_foo.bar.value == 42);
804}
805
806const S0 = struct {
807 bar: S1,
808
809 pub const S1 = struct {
810 value: u8,
811 };
812
813 fn init() @This() {
814 return S0{ .bar = S1{ .value = 123 } };
815 }
816};
817
818var g_foo: S0 = S0.init();
819
820test "packed struct with fp fields" {
821 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
822
823 const S = packed struct {
824 data: [3]f32,
825
826 pub fn frob(self: *@This()) void {
827 self.data[0] += self.data[1] + self.data[2];
828 self.data[1] += self.data[0] + self.data[2];
829 self.data[2] += self.data[0] + self.data[1];
830 }
831 };
832
833 var s: S = undefined;
834 s.data[0] = 1.0;
835 s.data[1] = 2.0;
836 s.data[2] = 3.0;
837 s.frob();
838 try expectEqual(@as(f32, 6.0), s.data[0]);
839 try expectEqual(@as(f32, 11.0), s.data[1]);
840 try expectEqual(@as(f32, 20.0), s.data[2]);
841}
842
843test "fn with C calling convention returns struct by value" {
844 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
845
846 const S = struct {
847 fn entry() !void {
848 var x = makeBar(10);
849 try expectEqual(@as(i32, 10), x.handle);
850 }
851
852 const ExternBar = extern struct {
853 handle: i32,
854 };
855
856 fn makeBar(t: i32) callconv(.C) ExternBar {
857 return ExternBar{
858 .handle = t,
859 };
860 }
861 };
862 try S.entry();
863 comptime try S.entry();
864}
865
866test "non-packed struct with u128 entry in union" {
867 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
868
869 const U = union(enum) {
870 Num: u128,
871 Void,
872 };
873
874 const S = struct {
875 f1: U,
876 f2: U,
877 };
878
879 var sx: S = undefined;
880 var s = &sx;
881 try std.testing.expect(@ptrToInt(&s.f2) - @ptrToInt(&s.f1) == @offsetOf(S, "f2"));
882 var v2 = U{ .Num = 123 };
883 s.f2 = v2;
884 try std.testing.expect(s.f2.Num == 123);
885}
886
887test "packed struct field passed to generic function" {
888 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
889
890 const S = struct {
891 const P = packed struct {
892 b: u5,
893 g: u5,
894 r: u5,
895 a: u1,
896 };
897
898 fn genericReadPackedField(ptr: anytype) u5 {
899 return ptr.*;
900 }
901 };
902
903 var p: S.P = undefined;
904 p.b = 29;
905 var loaded = S.genericReadPackedField(&p.b);
906 try expect(loaded == 29);
907}
908
909test "anonymous struct literal syntax" {
910 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
911 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
912 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
913 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
914
915 const S = struct {
916 const Point = struct {
917 x: i32,
918 y: i32,
919 };
920
921 fn doTheTest() !void {
922 var p: Point = .{
923 .x = 1,
924 .y = 2,
925 };
926 try expect(p.x == 1);
927 try expect(p.y == 2);
928 }
929 };
930 try S.doTheTest();
931 comptime try S.doTheTest();
932}
933
934test "fully anonymous struct" {
935 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
936
937 const S = struct {
938 fn doTheTest() !void {
939 try dump(.{
940 .int = @as(u32, 1234),
941 .float = @as(f64, 12.34),
942 .b = true,
943 .s = "hi",
944 });
945 }
946 fn dump(args: anytype) !void {
947 try expect(args.int == 1234);
948 try expect(args.float == 12.34);
949 try expect(args.b);
950 try expect(args.s[0] == 'h');
951 try expect(args.s[1] == 'i');
952 }
953 };
954 try S.doTheTest();
955 comptime try S.doTheTest();
956}
957
958test "fully anonymous list literal" {
959 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
960
961 const S = struct {
962 fn doTheTest() !void {
963 try dump(.{ @as(u32, 1234), @as(f64, 12.34), true, "hi" });
964 }
965 fn dump(args: anytype) !void {
966 try expect(args.@"0" == 1234);
967 try expect(args.@"1" == 12.34);
968 try expect(args.@"2");
969 try expect(args.@"3"[0] == 'h');
970 try expect(args.@"3"[1] == 'i');
971 }
972 };
973 try S.doTheTest();
974 comptime try S.doTheTest();
975}
976
977test "anonymous struct literal assigned to variable" {
978 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
979
980 var vec = .{ @as(i32, 22), @as(i32, 55), @as(i32, 99) };
981 try expect(vec.@"0" == 22);
982 try expect(vec.@"1" == 55);
983 try expect(vec.@"2" == 99);
984}
985
986test "comptime struct field" {
987 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
988
989 const T = struct {
990 a: i32,
991 comptime b: i32 = 1234,
992 };
993
994 var foo: T = undefined;
995 comptime try expect(foo.b == 1234);
996}
997
998test "anon struct literal field value initialized with fn call" {
999 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
1000
1001 const S = struct {
1002 fn doTheTest() !void {
1003 var x = .{foo()};
1004 try expectEqualSlices(u8, x[0], "hi");
1005 }
1006 fn foo() []const u8 {
1007 return "hi";
1008 }
1009 };
1010 try S.doTheTest();
1011 comptime try S.doTheTest();
1012}
1013
1014test "struct with union field" {
1015 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
1016
1017 const Value = struct {
1018 ref: u32 = 2,
1019 kind: union(enum) {
1020 None: usize,
1021 Bool: bool,
1022 },
1023 };
1024
1025 var True = Value{
1026 .kind = .{ .Bool = true },
1027 };
1028 try expectEqual(@as(u32, 2), True.ref);
1029 try expectEqual(true, True.kind.Bool);
1030}
1031
1032test "type coercion of anon struct literal to struct" {
1033 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
1034
1035 const S = struct {
1036 const S2 = struct {
1037 A: u32,
1038 B: []const u8,
1039 C: void,
1040 D: Foo = .{},
1041 };
1042
1043 const Foo = struct {
1044 field: i32 = 1234,
1045 };
1046
1047 fn doTheTest() !void {
1048 var y: u32 = 42;
1049 const t0 = .{ .A = 123, .B = "foo", .C = {} };
1050 const t1 = .{ .A = y, .B = "foo", .C = {} };
1051 const y0: S2 = t0;
1052 var y1: S2 = t1;
1053 try expect(y0.A == 123);
1054 try expect(std.mem.eql(u8, y0.B, "foo"));
1055 try expect(y0.C == {});
1056 try expect(y0.D.field == 1234);
1057 try expect(y1.A == y);
1058 try expect(std.mem.eql(u8, y1.B, "foo"));
1059 try expect(y1.C == {});
1060 try expect(y1.D.field == 1234);
1061 }
1062 };
1063 try S.doTheTest();
1064 comptime try S.doTheTest();
1065}
1066
1067test "type coercion of pointer to anon struct literal to pointer to struct" {
1068 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
1069
1070 const S = struct {
1071 const S2 = struct {
1072 A: u32,
1073 B: []const u8,
1074 C: void,
1075 D: Foo = .{},
1076 };
1077
1078 const Foo = struct {
1079 field: i32 = 1234,
1080 };
1081
1082 fn doTheTest() !void {
1083 var y: u32 = 42;
1084 const t0 = &.{ .A = 123, .B = "foo", .C = {} };
1085 const t1 = &.{ .A = y, .B = "foo", .C = {} };
1086 const y0: *const S2 = t0;
1087 var y1: *const S2 = t1;
1088 try expect(y0.A == 123);
1089 try expect(std.mem.eql(u8, y0.B, "foo"));
1090 try expect(y0.C == {});
1091 try expect(y0.D.field == 1234);
1092 try expect(y1.A == y);
1093 try expect(std.mem.eql(u8, y1.B, "foo"));
1094 try expect(y1.C == {});
1095 try expect(y1.D.field == 1234);
1096 }
1097 };
1098 try S.doTheTest();
1099 comptime try S.doTheTest();
1100}
1101
1102test "packed struct with undefined initializers" {
1103 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
1104 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
1105 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
1106 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
1107
1108 const S = struct {
1109 const P = packed struct {
1110 a: u3,
1111 _a: u3 = undefined,
1112 b: u3,
1113 _b: u3 = undefined,
1114 c: u3,
1115 _c: u3 = undefined,
1116 };
1117
1118 fn doTheTest() !void {
1119 var p: P = undefined;
1120 p = P{ .a = 2, .b = 4, .c = 6 };
1121 // Make sure the compiler doesn't touch the unprefixed fields.
1122 // Use expect since i386-linux doesn't like expectEqual
1123 try expect(p.a == 2);
1124 try expect(p.b == 4);
1125 try expect(p.c == 6);
1126 }
1127 };
1128
1129 try S.doTheTest();
1130 comptime try S.doTheTest();
1131}
1132
1133test "for loop over pointers to struct, getting field from struct pointer" {
1134 // When enabling this test, be careful. I have observed it to pass when compiling
1135 // stage2 alone, but when using stage1 with -fno-stage1 -fLLVM it fails.
1136 // Maybe eyeball the LLVM that it generates and run in valgrind, both the compiler
1137 // and the generated test at runtime.
1138 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
1139
1140 const S = struct {
1141 const Foo = struct {
1142 name: []const u8,
1143 };
1144
1145 var ok = true;
1146
1147 fn eql(a: []const u8) bool {
1148 _ = a;
1149 return true;
1150 }
1151
1152 const ArrayList = struct {
1153 fn toSlice(self: *ArrayList) []*Foo {
1154 _ = self;
1155 return @as([*]*Foo, undefined)[0..0];
1156 }
1157 };
1158
1159 fn doTheTest() !void {
1160 var objects: ArrayList = undefined;
1161
1162 for (objects.toSlice()) |obj| {
1163 if (eql(obj.name)) {
1164 ok = false;
1165 }
1166 }
1167
1168 try expect(ok);
1169 }
1170 };
1171 try S.doTheTest();
1172}
test/behavior/struct_llvm.zig deleted-802
......@@ -1,802 +0,0 @@
1const std = @import("std");
2const builtin = @import("builtin");
3const native_endian = builtin.target.cpu.arch.endian();
4const expect = std.testing.expect;
5const expectEqual = std.testing.expectEqual;
6const expectEqualSlices = std.testing.expectEqualSlices;
7const maxInt = std.math.maxInt;
8
9const Node = struct {
10 val: Val,
11 next: *Node,
12};
13
14const Val = struct {
15 x: i32,
16};
17
18test "struct point to self" {
19 var root: Node = undefined;
20 root.val.x = 1;
21
22 var node: Node = undefined;
23 node.next = &root;
24 node.val.x = 2;
25
26 root.next = &node;
27
28 try expect(node.next.next.next.val.x == 1);
29}
30
31test "void struct fields" {
32 const foo = VoidStructFieldsFoo{
33 .a = void{},
34 .b = 1,
35 .c = void{},
36 };
37 try expect(foo.b == 1);
38 try expect(@sizeOf(VoidStructFieldsFoo) == 4);
39}
40const VoidStructFieldsFoo = struct {
41 a: void,
42 b: i32,
43 c: void,
44};
45
46test "return empty struct from fn" {
47 _ = testReturnEmptyStructFromFn();
48}
49const EmptyStruct2 = struct {};
50fn testReturnEmptyStructFromFn() EmptyStruct2 {
51 return EmptyStruct2{};
52}
53
54test "pass slice of empty struct to fn" {
55 try expect(testPassSliceOfEmptyStructToFn(&[_]EmptyStruct2{EmptyStruct2{}}) == 1);
56}
57fn testPassSliceOfEmptyStructToFn(slice: []const EmptyStruct2) usize {
58 return slice.len;
59}
60
61test "self-referencing struct via array member" {
62 const T = struct {
63 children: [1]*@This(),
64 };
65 var x: T = undefined;
66 x = T{ .children = .{&x} };
67 try expect(x.children[0] == &x);
68}
69
70test "empty struct method call" {
71 const es = EmptyStruct{};
72 try expect(es.method() == 1234);
73}
74const EmptyStruct = struct {
75 fn method(es: *const EmptyStruct) i32 {
76 _ = es;
77 return 1234;
78 }
79};
80
81test "align 1 field before self referential align 8 field as slice return type" {
82 const result = alloc(Expr);
83 try expect(result.len == 0);
84}
85
86const Expr = union(enum) {
87 Literal: u8,
88 Question: *Expr,
89};
90
91fn alloc(comptime T: type) []T {
92 return &[_]T{};
93}
94
95const APackedStruct = packed struct {
96 x: u8,
97 y: u8,
98};
99
100test "packed struct" {
101 var foo = APackedStruct{
102 .x = 1,
103 .y = 2,
104 };
105 foo.y += 1;
106 const four = foo.x + foo.y;
107 try expect(four == 4);
108}
109
110const Foo24Bits = packed struct {
111 field: u24,
112};
113const Foo96Bits = packed struct {
114 a: u24,
115 b: u24,
116 c: u24,
117 d: u24,
118};
119
120test "packed struct 24bits" {
121 comptime {
122 try expect(@sizeOf(Foo24Bits) == 4);
123 if (@sizeOf(usize) == 4) {
124 try expect(@sizeOf(Foo96Bits) == 12);
125 } else {
126 try expect(@sizeOf(Foo96Bits) == 16);
127 }
128 }
129
130 var value = Foo96Bits{
131 .a = 0,
132 .b = 0,
133 .c = 0,
134 .d = 0,
135 };
136 value.a += 1;
137 try expect(value.a == 1);
138 try expect(value.b == 0);
139 try expect(value.c == 0);
140 try expect(value.d == 0);
141
142 value.b += 1;
143 try expect(value.a == 1);
144 try expect(value.b == 1);
145 try expect(value.c == 0);
146 try expect(value.d == 0);
147
148 value.c += 1;
149 try expect(value.a == 1);
150 try expect(value.b == 1);
151 try expect(value.c == 1);
152 try expect(value.d == 0);
153
154 value.d += 1;
155 try expect(value.a == 1);
156 try expect(value.b == 1);
157 try expect(value.c == 1);
158 try expect(value.d == 1);
159}
160
161test "runtime struct initialization of bitfield" {
162 const s1 = Nibbles{
163 .x = x1,
164 .y = x1,
165 };
166 const s2 = Nibbles{
167 .x = @intCast(u4, x2),
168 .y = @intCast(u4, x2),
169 };
170
171 try expect(s1.x == x1);
172 try expect(s1.y == x1);
173 try expect(s2.x == @intCast(u4, x2));
174 try expect(s2.y == @intCast(u4, x2));
175}
176
177var x1 = @as(u4, 1);
178var x2 = @as(u8, 2);
179
180const Nibbles = packed struct {
181 x: u4,
182 y: u4,
183};
184
185const Bitfields = packed struct {
186 f1: u16,
187 f2: u16,
188 f3: u8,
189 f4: u8,
190 f5: u4,
191 f6: u4,
192 f7: u8,
193};
194
195test "native bit field understands endianness" {
196 var all: u64 = if (native_endian != .Little)
197 0x1111222233445677
198 else
199 0x7765443322221111;
200 var bytes: [8]u8 = undefined;
201 @memcpy(&bytes, @ptrCast([*]u8, &all), 8);
202 var bitfields = @ptrCast(*Bitfields, &bytes).*;
203
204 try expect(bitfields.f1 == 0x1111);
205 try expect(bitfields.f2 == 0x2222);
206 try expect(bitfields.f3 == 0x33);
207 try expect(bitfields.f4 == 0x44);
208 try expect(bitfields.f5 == 0x5);
209 try expect(bitfields.f6 == 0x6);
210 try expect(bitfields.f7 == 0x77);
211}
212
213test "implicit cast packed struct field to const ptr" {
214 const LevelUpMove = packed struct {
215 move_id: u9,
216 level: u7,
217
218 fn toInt(value: u7) u7 {
219 return value;
220 }
221 };
222
223 var lup: LevelUpMove = undefined;
224 lup.level = 12;
225 const res = LevelUpMove.toInt(lup.level);
226 try expect(res == 12);
227}
228
229test "zero-bit field in packed struct" {
230 const S = packed struct {
231 x: u10,
232 y: void,
233 };
234 var x: S = undefined;
235 _ = x;
236}
237
238test "packed struct with non-ABI-aligned field" {
239 const S = packed struct {
240 x: u9,
241 y: u183,
242 };
243 var s: S = undefined;
244 s.x = 1;
245 s.y = 42;
246 try expect(s.x == 1);
247 try expect(s.y == 42);
248}
249
250const BitField1 = packed struct {
251 a: u3,
252 b: u3,
253 c: u2,
254};
255
256const bit_field_1 = BitField1{
257 .a = 1,
258 .b = 2,
259 .c = 3,
260};
261
262test "bit field access" {
263 var data = bit_field_1;
264 try expect(getA(&data) == 1);
265 try expect(getB(&data) == 2);
266 try expect(getC(&data) == 3);
267 comptime try expect(@sizeOf(BitField1) == 1);
268
269 data.b += 1;
270 try expect(data.b == 3);
271
272 data.a += 1;
273 try expect(data.a == 2);
274 try expect(data.b == 3);
275}
276
277fn getA(data: *const BitField1) u3 {
278 return data.a;
279}
280
281fn getB(data: *const BitField1) u3 {
282 return data.b;
283}
284
285fn getC(data: *const BitField1) u2 {
286 return data.c;
287}
288
289test "default struct initialization fields" {
290 const S = struct {
291 a: i32 = 1234,
292 b: i32,
293 };
294 const x = S{
295 .b = 5,
296 };
297 var five: i32 = 5;
298 const y = S{
299 .b = five,
300 };
301 if (x.a + x.b != 1239) {
302 @compileError("it should be comptime known");
303 }
304 try expect(y.a == x.a);
305 try expect(y.b == x.b);
306 try expect(1239 == x.a + x.b);
307}
308
309// TODO revisit this test when doing https://github.com/ziglang/zig/issues/1512
310test "packed array 24bits" {
311 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
312
313 comptime {
314 try expect(@sizeOf([9]Foo32Bits) == 9 * 4);
315 try expect(@sizeOf(FooArray24Bits) == 2 + 2 * 4 + 2);
316 }
317
318 var bytes = [_]u8{0} ** (@sizeOf(FooArray24Bits) + 1);
319 bytes[bytes.len - 1] = 0xaa;
320 const ptr = &std.mem.bytesAsSlice(FooArray24Bits, bytes[0 .. bytes.len - 1])[0];
321 try expect(ptr.a == 0);
322 try expect(ptr.b[0].field == 0);
323 try expect(ptr.b[1].field == 0);
324 try expect(ptr.c == 0);
325
326 ptr.a = maxInt(u16);
327 try expect(ptr.a == maxInt(u16));
328 try expect(ptr.b[0].field == 0);
329 try expect(ptr.b[1].field == 0);
330 try expect(ptr.c == 0);
331
332 ptr.b[0].field = maxInt(u24);
333 try expect(ptr.a == maxInt(u16));
334 try expect(ptr.b[0].field == maxInt(u24));
335 try expect(ptr.b[1].field == 0);
336 try expect(ptr.c == 0);
337
338 ptr.b[1].field = maxInt(u24);
339 try expect(ptr.a == maxInt(u16));
340 try expect(ptr.b[0].field == maxInt(u24));
341 try expect(ptr.b[1].field == maxInt(u24));
342 try expect(ptr.c == 0);
343
344 ptr.c = maxInt(u16);
345 try expect(ptr.a == maxInt(u16));
346 try expect(ptr.b[0].field == maxInt(u24));
347 try expect(ptr.b[1].field == maxInt(u24));
348 try expect(ptr.c == maxInt(u16));
349
350 try expect(bytes[bytes.len - 1] == 0xaa);
351}
352
353const Foo32Bits = packed struct {
354 field: u24,
355 pad: u8,
356};
357
358const FooArray24Bits = packed struct {
359 a: u16,
360 b: [2]Foo32Bits,
361 c: u16,
362};
363
364test "aligned array of packed struct" {
365 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
366
367 comptime {
368 try expect(@sizeOf(FooStructAligned) == 2);
369 try expect(@sizeOf(FooArrayOfAligned) == 2 * 2);
370 }
371
372 var bytes = [_]u8{0xbb} ** @sizeOf(FooArrayOfAligned);
373 const ptr = &std.mem.bytesAsSlice(FooArrayOfAligned, bytes[0..])[0];
374
375 try expect(ptr.a[0].a == 0xbb);
376 try expect(ptr.a[0].b == 0xbb);
377 try expect(ptr.a[1].a == 0xbb);
378 try expect(ptr.a[1].b == 0xbb);
379}
380
381const FooStructAligned = packed struct {
382 a: u8,
383 b: u8,
384};
385
386const FooArrayOfAligned = packed struct {
387 a: [2]FooStructAligned,
388};
389
390test "pointer to packed struct member in a stack variable" {
391 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
392
393 const S = packed struct {
394 a: u2,
395 b: u2,
396 };
397
398 var s = S{ .a = 2, .b = 0 };
399 var b_ptr = &s.b;
400 try expect(s.b == 0);
401 b_ptr.* = 2;
402 try expect(s.b == 2);
403}
404
405test "non-byte-aligned array inside packed struct" {
406 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
407
408 const Foo = packed struct {
409 a: bool,
410 b: [0x16]u8,
411 };
412 const S = struct {
413 fn bar(slice: []const u8) !void {
414 try expectEqualSlices(u8, slice, "abcdefghijklmnopqurstu");
415 }
416 fn doTheTest() !void {
417 var foo = Foo{
418 .a = true,
419 .b = "abcdefghijklmnopqurstu".*,
420 };
421 const value = foo.b;
422 try bar(&value);
423 }
424 };
425 try S.doTheTest();
426 comptime try S.doTheTest();
427}
428
429test "packed struct with u0 field access" {
430 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
431
432 const S = packed struct {
433 f0: u0,
434 };
435 var s = S{ .f0 = 0 };
436 comptime try expect(s.f0 == 0);
437}
438
439test "access to global struct fields" {
440 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
441
442 g_foo.bar.value = 42;
443 try expect(g_foo.bar.value == 42);
444}
445
446const S0 = struct {
447 bar: S1,
448
449 pub const S1 = struct {
450 value: u8,
451 };
452
453 fn init() @This() {
454 return S0{ .bar = S1{ .value = 123 } };
455 }
456};
457
458var g_foo: S0 = S0.init();
459
460test "packed struct with fp fields" {
461 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
462
463 const S = packed struct {
464 data: [3]f32,
465
466 pub fn frob(self: *@This()) void {
467 self.data[0] += self.data[1] + self.data[2];
468 self.data[1] += self.data[0] + self.data[2];
469 self.data[2] += self.data[0] + self.data[1];
470 }
471 };
472
473 var s: S = undefined;
474 s.data[0] = 1.0;
475 s.data[1] = 2.0;
476 s.data[2] = 3.0;
477 s.frob();
478 try expectEqual(@as(f32, 6.0), s.data[0]);
479 try expectEqual(@as(f32, 11.0), s.data[1]);
480 try expectEqual(@as(f32, 20.0), s.data[2]);
481}
482
483test "fn with C calling convention returns struct by value" {
484 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
485
486 const S = struct {
487 fn entry() !void {
488 var x = makeBar(10);
489 try expectEqual(@as(i32, 10), x.handle);
490 }
491
492 const ExternBar = extern struct {
493 handle: i32,
494 };
495
496 fn makeBar(t: i32) callconv(.C) ExternBar {
497 return ExternBar{
498 .handle = t,
499 };
500 }
501 };
502 try S.entry();
503 comptime try S.entry();
504}
505
506test "non-packed struct with u128 entry in union" {
507 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
508
509 const U = union(enum) {
510 Num: u128,
511 Void,
512 };
513
514 const S = struct {
515 f1: U,
516 f2: U,
517 };
518
519 var sx: S = undefined;
520 var s = &sx;
521 try std.testing.expect(@ptrToInt(&s.f2) - @ptrToInt(&s.f1) == @offsetOf(S, "f2"));
522 var v2 = U{ .Num = 123 };
523 s.f2 = v2;
524 try std.testing.expect(s.f2.Num == 123);
525}
526
527test "packed struct field passed to generic function" {
528 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
529
530 const S = struct {
531 const P = packed struct {
532 b: u5,
533 g: u5,
534 r: u5,
535 a: u1,
536 };
537
538 fn genericReadPackedField(ptr: anytype) u5 {
539 return ptr.*;
540 }
541 };
542
543 var p: S.P = undefined;
544 p.b = 29;
545 var loaded = S.genericReadPackedField(&p.b);
546 try expect(loaded == 29);
547}
548
549test "anonymous struct literal syntax" {
550 const S = struct {
551 const Point = struct {
552 x: i32,
553 y: i32,
554 };
555
556 fn doTheTest() !void {
557 var p: Point = .{
558 .x = 1,
559 .y = 2,
560 };
561 try expect(p.x == 1);
562 try expect(p.y == 2);
563 }
564 };
565 try S.doTheTest();
566 comptime try S.doTheTest();
567}
568
569test "fully anonymous struct" {
570 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
571
572 const S = struct {
573 fn doTheTest() !void {
574 try dump(.{
575 .int = @as(u32, 1234),
576 .float = @as(f64, 12.34),
577 .b = true,
578 .s = "hi",
579 });
580 }
581 fn dump(args: anytype) !void {
582 try expect(args.int == 1234);
583 try expect(args.float == 12.34);
584 try expect(args.b);
585 try expect(args.s[0] == 'h');
586 try expect(args.s[1] == 'i');
587 }
588 };
589 try S.doTheTest();
590 comptime try S.doTheTest();
591}
592
593test "fully anonymous list literal" {
594 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
595
596 const S = struct {
597 fn doTheTest() !void {
598 try dump(.{ @as(u32, 1234), @as(f64, 12.34), true, "hi" });
599 }
600 fn dump(args: anytype) !void {
601 try expect(args.@"0" == 1234);
602 try expect(args.@"1" == 12.34);
603 try expect(args.@"2");
604 try expect(args.@"3"[0] == 'h');
605 try expect(args.@"3"[1] == 'i');
606 }
607 };
608 try S.doTheTest();
609 comptime try S.doTheTest();
610}
611
612test "anonymous struct literal assigned to variable" {
613 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
614
615 var vec = .{ @as(i32, 22), @as(i32, 55), @as(i32, 99) };
616 try expect(vec.@"0" == 22);
617 try expect(vec.@"1" == 55);
618 try expect(vec.@"2" == 99);
619}
620
621test "comptime struct field" {
622 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
623
624 const T = struct {
625 a: i32,
626 comptime b: i32 = 1234,
627 };
628
629 var foo: T = undefined;
630 comptime try expect(foo.b == 1234);
631}
632
633test "anon struct literal field value initialized with fn call" {
634 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
635
636 const S = struct {
637 fn doTheTest() !void {
638 var x = .{foo()};
639 try expectEqualSlices(u8, x[0], "hi");
640 }
641 fn foo() []const u8 {
642 return "hi";
643 }
644 };
645 try S.doTheTest();
646 comptime try S.doTheTest();
647}
648
649test "struct with union field" {
650 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
651
652 const Value = struct {
653 ref: u32 = 2,
654 kind: union(enum) {
655 None: usize,
656 Bool: bool,
657 },
658 };
659
660 var True = Value{
661 .kind = .{ .Bool = true },
662 };
663 try expectEqual(@as(u32, 2), True.ref);
664 try expectEqual(true, True.kind.Bool);
665}
666
667test "type coercion of anon struct literal to struct" {
668 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
669
670 const S = struct {
671 const S2 = struct {
672 A: u32,
673 B: []const u8,
674 C: void,
675 D: Foo = .{},
676 };
677
678 const Foo = struct {
679 field: i32 = 1234,
680 };
681
682 fn doTheTest() !void {
683 var y: u32 = 42;
684 const t0 = .{ .A = 123, .B = "foo", .C = {} };
685 const t1 = .{ .A = y, .B = "foo", .C = {} };
686 const y0: S2 = t0;
687 var y1: S2 = t1;
688 try expect(y0.A == 123);
689 try expect(std.mem.eql(u8, y0.B, "foo"));
690 try expect(y0.C == {});
691 try expect(y0.D.field == 1234);
692 try expect(y1.A == y);
693 try expect(std.mem.eql(u8, y1.B, "foo"));
694 try expect(y1.C == {});
695 try expect(y1.D.field == 1234);
696 }
697 };
698 try S.doTheTest();
699 comptime try S.doTheTest();
700}
701
702test "type coercion of pointer to anon struct literal to pointer to struct" {
703 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
704
705 const S = struct {
706 const S2 = struct {
707 A: u32,
708 B: []const u8,
709 C: void,
710 D: Foo = .{},
711 };
712
713 const Foo = struct {
714 field: i32 = 1234,
715 };
716
717 fn doTheTest() !void {
718 var y: u32 = 42;
719 const t0 = &.{ .A = 123, .B = "foo", .C = {} };
720 const t1 = &.{ .A = y, .B = "foo", .C = {} };
721 const y0: *const S2 = t0;
722 var y1: *const S2 = t1;
723 try expect(y0.A == 123);
724 try expect(std.mem.eql(u8, y0.B, "foo"));
725 try expect(y0.C == {});
726 try expect(y0.D.field == 1234);
727 try expect(y1.A == y);
728 try expect(std.mem.eql(u8, y1.B, "foo"));
729 try expect(y1.C == {});
730 try expect(y1.D.field == 1234);
731 }
732 };
733 try S.doTheTest();
734 comptime try S.doTheTest();
735}
736
737test "packed struct with undefined initializers" {
738 const S = struct {
739 const P = packed struct {
740 a: u3,
741 _a: u3 = undefined,
742 b: u3,
743 _b: u3 = undefined,
744 c: u3,
745 _c: u3 = undefined,
746 };
747
748 fn doTheTest() !void {
749 var p: P = undefined;
750 p = P{ .a = 2, .b = 4, .c = 6 };
751 // Make sure the compiler doesn't touch the unprefixed fields.
752 // Use expect since i386-linux doesn't like expectEqual
753 try expect(p.a == 2);
754 try expect(p.b == 4);
755 try expect(p.c == 6);
756 }
757 };
758
759 try S.doTheTest();
760 comptime try S.doTheTest();
761}
762
763test "for loop over pointers to struct, getting field from struct pointer" {
764 // When enabling this test, be careful. I have observed it to pass when compiling
765 // stage2 alone, but when using stage1 with -fno-stage1 -fLLVM it fails.
766 // Maybe eyeball the LLVM that it generates and run in valgrind, both the compiler
767 // and the generated test at runtime.
768 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
769
770 const S = struct {
771 const Foo = struct {
772 name: []const u8,
773 };
774
775 var ok = true;
776
777 fn eql(a: []const u8) bool {
778 _ = a;
779 return true;
780 }
781
782 const ArrayList = struct {
783 fn toSlice(self: *ArrayList) []*Foo {
784 _ = self;
785 return @as([*]*Foo, undefined)[0..0];
786 }
787 };
788
789 fn doTheTest() !void {
790 var objects: ArrayList = undefined;
791
792 for (objects.toSlice()) |obj| {
793 if (eql(obj.name)) {
794 ok = false;
795 }
796 }
797
798 try expect(ok);
799 }
800 };
801 try S.doTheTest();
802}
test/behavior/truncate.zig+13
......@@ -76,3 +76,16 @@ test "truncate on comptime integer" {
7676 var w = @truncate(u1, 1 << 100);
7777 try expect(w == 0);
7878}
79
80test "truncate on vectors" {
81 if (builtin.zig_backend != .stage1) return error.SkipZigTest;
82
83 const S = struct {
84 fn doTheTest() !void {
85 var v1: @Vector(4, u16) = .{ 0xaabb, 0xccdd, 0xeeff, 0x1122 };
86 var v2 = @truncate(u8, v1);
87 try expect(std.mem.eql(u8, &@as([4]u8, v2), &[4]u8{ 0xbb, 0xdd, 0xff, 0x22 }));
88 }
89 };
90 try S.doTheTest();
91}
test/behavior/truncate_stage1.zig deleted-13
......@@ -1,13 +0,0 @@
1const std = @import("std");
2const expect = std.testing.expect;
3
4test "truncate on vectors" {
5 const S = struct {
6 fn doTheTest() !void {
7 var v1: @Vector(4, u16) = .{ 0xaabb, 0xccdd, 0xeeff, 0x1122 };
8 var v2 = @truncate(u8, v1);
9 try expect(std.mem.eql(u8, &@as([4]u8, v2), &[4]u8{ 0xbb, 0xdd, 0xff, 0x22 }));
10 }
11 };
12 try S.doTheTest();
13}
test/behavior/type.zig+386
......@@ -102,3 +102,389 @@ test "Type.Pointer" {
102102 [*c]align(8) volatile u8, [*c]align(8) const volatile u8,
103103 });
104104}
105
106test "Type.Float" {
107 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
108
109 try testing.expect(f16 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 16 } }));
110 try testing.expect(f32 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 32 } }));
111 try testing.expect(f64 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 64 } }));
112 try testing.expect(f80 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 80 } }));
113 try testing.expect(f128 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 128 } }));
114 try testTypes(&[_]type{ f16, f32, f64, f80, f128 });
115}
116
117test "Type.Array" {
118 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
119
120 try testing.expect([123]u8 == @Type(TypeInfo{
121 .Array = TypeInfo.Array{
122 .len = 123,
123 .child = u8,
124 .sentinel = null,
125 },
126 }));
127 try testing.expect([2]u32 == @Type(TypeInfo{
128 .Array = TypeInfo.Array{
129 .len = 2,
130 .child = u32,
131 .sentinel = null,
132 },
133 }));
134 try testing.expect([2:0]u32 == @Type(TypeInfo{
135 .Array = TypeInfo.Array{
136 .len = 2,
137 .child = u32,
138 .sentinel = &@as(u32, 0),
139 },
140 }));
141 try testTypes(&[_]type{ [1]u8, [30]usize, [7]bool });
142}
143
144test "@Type create slice with null sentinel" {
145 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
146
147 const Slice = @Type(TypeInfo{
148 .Pointer = .{
149 .size = .Slice,
150 .is_const = true,
151 .is_volatile = false,
152 .is_allowzero = false,
153 .alignment = 8,
154 .address_space = .generic,
155 .child = *i32,
156 .sentinel = null,
157 },
158 });
159 try testing.expect(Slice == []align(8) const *i32);
160}
161
162test "@Type picks up the sentinel value from TypeInfo" {
163 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
164
165 try testTypes(&[_]type{
166 [11:0]u8, [4:10]u8,
167 [*:0]u8, [*:0]const u8,
168 [*:0]volatile u8, [*:0]const volatile u8,
169 [*:0]align(4) u8, [*:0]align(4) const u8,
170 [*:0]align(4) volatile u8, [*:0]align(4) const volatile u8,
171 [*:0]align(8) u8, [*:0]align(8) const u8,
172 [*:0]align(8) volatile u8, [*:0]align(8) const volatile u8,
173 [*:0]allowzero u8, [*:0]allowzero const u8,
174 [*:0]allowzero volatile u8, [*:0]allowzero const volatile u8,
175 [*:0]allowzero align(4) u8, [*:0]allowzero align(4) const u8,
176 [*:0]allowzero align(4) volatile u8, [*:0]allowzero align(4) const volatile u8,
177 [*:5]allowzero align(4) volatile u8, [*:5]allowzero align(4) const volatile u8,
178 [:0]u8, [:0]const u8,
179 [:0]volatile u8, [:0]const volatile u8,
180 [:0]align(4) u8, [:0]align(4) const u8,
181 [:0]align(4) volatile u8, [:0]align(4) const volatile u8,
182 [:0]align(8) u8, [:0]align(8) const u8,
183 [:0]align(8) volatile u8, [:0]align(8) const volatile u8,
184 [:0]allowzero u8, [:0]allowzero const u8,
185 [:0]allowzero volatile u8, [:0]allowzero const volatile u8,
186 [:0]allowzero align(4) u8, [:0]allowzero align(4) const u8,
187 [:0]allowzero align(4) volatile u8, [:0]allowzero align(4) const volatile u8,
188 [:4]allowzero align(4) volatile u8, [:4]allowzero align(4) const volatile u8,
189 });
190}
191
192test "Type.Optional" {
193 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
194
195 try testTypes(&[_]type{
196 ?u8,
197 ?*u8,
198 ?[]u8,
199 ?[*]u8,
200 ?[*c]u8,
201 });
202}
203
204test "Type.ErrorUnion" {
205 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
206
207 try testTypes(&[_]type{
208 error{}!void,
209 error{Error}!void,
210 });
211}
212
213test "Type.Opaque" {
214 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
215
216 const Opaque = @Type(.{
217 .Opaque = .{
218 .decls = &[_]TypeInfo.Declaration{},
219 },
220 });
221 try testing.expect(Opaque != opaque {});
222 try testing.expectEqualSlices(
223 TypeInfo.Declaration,
224 &[_]TypeInfo.Declaration{},
225 @typeInfo(Opaque).Opaque.decls,
226 );
227}
228
229test "Type.Vector" {
230 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
231
232 try testTypes(&[_]type{
233 @Vector(0, u8),
234 @Vector(4, u8),
235 @Vector(8, *u8),
236 std.meta.Vector(0, u8),
237 std.meta.Vector(4, u8),
238 std.meta.Vector(8, *u8),
239 });
240}
241
242test "Type.AnyFrame" {
243 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
244
245 try testTypes(&[_]type{
246 anyframe,
247 anyframe->u8,
248 anyframe->anyframe->u8,
249 });
250}
251
252fn add(a: i32, b: i32) i32 {
253 return a + b;
254}
255
256test "Type.ErrorSet" {
257 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
258
259 // error sets don't compare equal so just check if they compile
260 _ = @Type(@typeInfo(error{}));
261 _ = @Type(@typeInfo(error{A}));
262 _ = @Type(@typeInfo(error{ A, B, C }));
263}
264
265test "Type.Struct" {
266 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
267
268 const A = @Type(@typeInfo(struct { x: u8, y: u32 }));
269 const infoA = @typeInfo(A).Struct;
270 try testing.expectEqual(TypeInfo.ContainerLayout.Auto, infoA.layout);
271 try testing.expectEqualSlices(u8, "x", infoA.fields[0].name);
272 try testing.expectEqual(u8, infoA.fields[0].field_type);
273 try testing.expectEqual(@as(?*const anyopaque, null), infoA.fields[0].default_value);
274 try testing.expectEqualSlices(u8, "y", infoA.fields[1].name);
275 try testing.expectEqual(u32, infoA.fields[1].field_type);
276 try testing.expectEqual(@as(?*const anyopaque, null), infoA.fields[1].default_value);
277 try testing.expectEqualSlices(TypeInfo.Declaration, &[_]TypeInfo.Declaration{}, infoA.decls);
278 try testing.expectEqual(@as(bool, false), infoA.is_tuple);
279
280 var a = A{ .x = 0, .y = 1 };
281 try testing.expectEqual(@as(u8, 0), a.x);
282 try testing.expectEqual(@as(u32, 1), a.y);
283 a.y += 1;
284 try testing.expectEqual(@as(u32, 2), a.y);
285
286 const B = @Type(@typeInfo(extern struct { x: u8, y: u32 = 5 }));
287 const infoB = @typeInfo(B).Struct;
288 try testing.expectEqual(TypeInfo.ContainerLayout.Extern, infoB.layout);
289 try testing.expectEqualSlices(u8, "x", infoB.fields[0].name);
290 try testing.expectEqual(u8, infoB.fields[0].field_type);
291 try testing.expectEqual(@as(?*const anyopaque, null), infoB.fields[0].default_value);
292 try testing.expectEqualSlices(u8, "y", infoB.fields[1].name);
293 try testing.expectEqual(u32, infoB.fields[1].field_type);
294 try testing.expectEqual(@as(u32, 5), @ptrCast(*const u32, infoB.fields[1].default_value.?).*);
295 try testing.expectEqual(@as(usize, 0), infoB.decls.len);
296 try testing.expectEqual(@as(bool, false), infoB.is_tuple);
297
298 const C = @Type(@typeInfo(packed struct { x: u8 = 3, y: u32 = 5 }));
299 const infoC = @typeInfo(C).Struct;
300 try testing.expectEqual(TypeInfo.ContainerLayout.Packed, infoC.layout);
301 try testing.expectEqualSlices(u8, "x", infoC.fields[0].name);
302 try testing.expectEqual(u8, infoC.fields[0].field_type);
303 try testing.expectEqual(@as(u8, 3), @ptrCast(*const u8, infoC.fields[0].default_value.?).*);
304 try testing.expectEqualSlices(u8, "y", infoC.fields[1].name);
305 try testing.expectEqual(u32, infoC.fields[1].field_type);
306 try testing.expectEqual(@as(u32, 5), @ptrCast(*const u32, infoC.fields[1].default_value.?).*);
307 try testing.expectEqual(@as(usize, 0), infoC.decls.len);
308 try testing.expectEqual(@as(bool, false), infoC.is_tuple);
309}
310
311test "Type.Enum" {
312 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
313
314 const Foo = @Type(.{
315 .Enum = .{
316 .layout = .Auto,
317 .tag_type = u8,
318 .fields = &[_]TypeInfo.EnumField{
319 .{ .name = "a", .value = 1 },
320 .{ .name = "b", .value = 5 },
321 },
322 .decls = &[_]TypeInfo.Declaration{},
323 .is_exhaustive = true,
324 },
325 });
326 try testing.expectEqual(true, @typeInfo(Foo).Enum.is_exhaustive);
327 try testing.expectEqual(@as(u8, 1), @enumToInt(Foo.a));
328 try testing.expectEqual(@as(u8, 5), @enumToInt(Foo.b));
329 const Bar = @Type(.{
330 .Enum = .{
331 .layout = .Extern,
332 .tag_type = u32,
333 .fields = &[_]TypeInfo.EnumField{
334 .{ .name = "a", .value = 1 },
335 .{ .name = "b", .value = 5 },
336 },
337 .decls = &[_]TypeInfo.Declaration{},
338 .is_exhaustive = false,
339 },
340 });
341 try testing.expectEqual(false, @typeInfo(Bar).Enum.is_exhaustive);
342 try testing.expectEqual(@as(u32, 1), @enumToInt(Bar.a));
343 try testing.expectEqual(@as(u32, 5), @enumToInt(Bar.b));
344 try testing.expectEqual(@as(u32, 6), @enumToInt(@intToEnum(Bar, 6)));
345}
346
347test "Type.Union" {
348 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
349
350 const Untagged = @Type(.{
351 .Union = .{
352 .layout = .Auto,
353 .tag_type = null,
354 .fields = &[_]TypeInfo.UnionField{
355 .{ .name = "int", .field_type = i32, .alignment = @alignOf(f32) },
356 .{ .name = "float", .field_type = f32, .alignment = @alignOf(f32) },
357 },
358 .decls = &[_]TypeInfo.Declaration{},
359 },
360 });
361 var untagged = Untagged{ .int = 1 };
362 untagged.float = 2.0;
363 untagged.int = 3;
364 try testing.expectEqual(@as(i32, 3), untagged.int);
365
366 const PackedUntagged = @Type(.{
367 .Union = .{
368 .layout = .Packed,
369 .tag_type = null,
370 .fields = &[_]TypeInfo.UnionField{
371 .{ .name = "signed", .field_type = i32, .alignment = @alignOf(i32) },
372 .{ .name = "unsigned", .field_type = u32, .alignment = @alignOf(u32) },
373 },
374 .decls = &[_]TypeInfo.Declaration{},
375 },
376 });
377 var packed_untagged = PackedUntagged{ .signed = -1 };
378 try testing.expectEqual(@as(i32, -1), packed_untagged.signed);
379 try testing.expectEqual(~@as(u32, 0), packed_untagged.unsigned);
380
381 const Tag = @Type(.{
382 .Enum = .{
383 .layout = .Auto,
384 .tag_type = u1,
385 .fields = &[_]TypeInfo.EnumField{
386 .{ .name = "signed", .value = 0 },
387 .{ .name = "unsigned", .value = 1 },
388 },
389 .decls = &[_]TypeInfo.Declaration{},
390 .is_exhaustive = true,
391 },
392 });
393 const Tagged = @Type(.{
394 .Union = .{
395 .layout = .Auto,
396 .tag_type = Tag,
397 .fields = &[_]TypeInfo.UnionField{
398 .{ .name = "signed", .field_type = i32, .alignment = @alignOf(i32) },
399 .{ .name = "unsigned", .field_type = u32, .alignment = @alignOf(u32) },
400 },
401 .decls = &[_]TypeInfo.Declaration{},
402 },
403 });
404 var tagged = Tagged{ .signed = -1 };
405 try testing.expectEqual(Tag.signed, tagged);
406 tagged = .{ .unsigned = 1 };
407 try testing.expectEqual(Tag.unsigned, tagged);
408}
409
410test "Type.Union from Type.Enum" {
411 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
412
413 const Tag = @Type(.{
414 .Enum = .{
415 .layout = .Auto,
416 .tag_type = u0,
417 .fields = &[_]TypeInfo.EnumField{
418 .{ .name = "working_as_expected", .value = 0 },
419 },
420 .decls = &[_]TypeInfo.Declaration{},
421 .is_exhaustive = true,
422 },
423 });
424 const T = @Type(.{
425 .Union = .{
426 .layout = .Auto,
427 .tag_type = Tag,
428 .fields = &[_]TypeInfo.UnionField{
429 .{ .name = "working_as_expected", .field_type = u32, .alignment = @alignOf(u32) },
430 },
431 .decls = &[_]TypeInfo.Declaration{},
432 },
433 });
434 _ = T;
435 _ = @typeInfo(T).Union;
436}
437
438test "Type.Union from regular enum" {
439 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
440
441 const E = enum { working_as_expected };
442 const T = @Type(.{
443 .Union = .{
444 .layout = .Auto,
445 .tag_type = E,
446 .fields = &[_]TypeInfo.UnionField{
447 .{ .name = "working_as_expected", .field_type = u32, .alignment = @alignOf(u32) },
448 },
449 .decls = &[_]TypeInfo.Declaration{},
450 },
451 });
452 _ = T;
453 _ = @typeInfo(T).Union;
454}
455
456test "Type.Fn" {
457 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
458
459 // wasm doesn't support align attributes on functions
460 if (builtin.target.cpu.arch == .wasm32 or builtin.target.cpu.arch == .wasm64) return error.SkipZigTest;
461
462 const foo = struct {
463 fn func(a: usize, b: bool) align(4) callconv(.C) usize {
464 _ = a;
465 _ = b;
466 return 0;
467 }
468 }.func;
469 const Foo = @Type(@typeInfo(@TypeOf(foo)));
470 const foo_2: Foo = foo;
471 _ = foo_2;
472}
473
474test "Type.BoundFn" {
475 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
476
477 // wasm doesn't support align attributes on functions
478 if (builtin.target.cpu.arch == .wasm32 or builtin.target.cpu.arch == .wasm64) return error.SkipZigTest;
479
480 const TestStruct = packed struct {
481 pub fn foo(self: *const @This()) align(4) callconv(.Unspecified) void {
482 _ = self;
483 }
484 };
485 const test_instance: TestStruct = undefined;
486 try testing.expect(std.meta.eql(
487 @typeName(@TypeOf(test_instance.foo)),
488 @typeName(@Type(@typeInfo(@TypeOf(test_instance.foo)))),
489 ));
490}
test/behavior/type_stage1.zig deleted-362
......@@ -1,362 +0,0 @@
1const std = @import("std");
2const builtin = @import("builtin");
3const TypeInfo = std.builtin.TypeInfo;
4const testing = std.testing;
5
6fn testTypes(comptime types: []const type) !void {
7 inline for (types) |testType| {
8 try testing.expect(testType == @Type(@typeInfo(testType)));
9 }
10}
11
12test "Type.Float" {
13 try testing.expect(f16 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 16 } }));
14 try testing.expect(f32 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 32 } }));
15 try testing.expect(f64 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 64 } }));
16 try testing.expect(f80 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 80 } }));
17 try testing.expect(f128 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 128 } }));
18 try testTypes(&[_]type{ f16, f32, f64, f80, f128 });
19}
20
21test "Type.Array" {
22 try testing.expect([123]u8 == @Type(TypeInfo{
23 .Array = TypeInfo.Array{
24 .len = 123,
25 .child = u8,
26 .sentinel = null,
27 },
28 }));
29 try testing.expect([2]u32 == @Type(TypeInfo{
30 .Array = TypeInfo.Array{
31 .len = 2,
32 .child = u32,
33 .sentinel = null,
34 },
35 }));
36 try testing.expect([2:0]u32 == @Type(TypeInfo{
37 .Array = TypeInfo.Array{
38 .len = 2,
39 .child = u32,
40 .sentinel = &@as(u32, 0),
41 },
42 }));
43 try testTypes(&[_]type{ [1]u8, [30]usize, [7]bool });
44}
45
46test "@Type create slice with null sentinel" {
47 const Slice = @Type(TypeInfo{
48 .Pointer = .{
49 .size = .Slice,
50 .is_const = true,
51 .is_volatile = false,
52 .is_allowzero = false,
53 .alignment = 8,
54 .address_space = .generic,
55 .child = *i32,
56 .sentinel = null,
57 },
58 });
59 try testing.expect(Slice == []align(8) const *i32);
60}
61
62test "@Type picks up the sentinel value from TypeInfo" {
63 try testTypes(&[_]type{
64 [11:0]u8, [4:10]u8,
65 [*:0]u8, [*:0]const u8,
66 [*:0]volatile u8, [*:0]const volatile u8,
67 [*:0]align(4) u8, [*:0]align(4) const u8,
68 [*:0]align(4) volatile u8, [*:0]align(4) const volatile u8,
69 [*:0]align(8) u8, [*:0]align(8) const u8,
70 [*:0]align(8) volatile u8, [*:0]align(8) const volatile u8,
71 [*:0]allowzero u8, [*:0]allowzero const u8,
72 [*:0]allowzero volatile u8, [*:0]allowzero const volatile u8,
73 [*:0]allowzero align(4) u8, [*:0]allowzero align(4) const u8,
74 [*:0]allowzero align(4) volatile u8, [*:0]allowzero align(4) const volatile u8,
75 [*:5]allowzero align(4) volatile u8, [*:5]allowzero align(4) const volatile u8,
76 [:0]u8, [:0]const u8,
77 [:0]volatile u8, [:0]const volatile u8,
78 [:0]align(4) u8, [:0]align(4) const u8,
79 [:0]align(4) volatile u8, [:0]align(4) const volatile u8,
80 [:0]align(8) u8, [:0]align(8) const u8,
81 [:0]align(8) volatile u8, [:0]align(8) const volatile u8,
82 [:0]allowzero u8, [:0]allowzero const u8,
83 [:0]allowzero volatile u8, [:0]allowzero const volatile u8,
84 [:0]allowzero align(4) u8, [:0]allowzero align(4) const u8,
85 [:0]allowzero align(4) volatile u8, [:0]allowzero align(4) const volatile u8,
86 [:4]allowzero align(4) volatile u8, [:4]allowzero align(4) const volatile u8,
87 });
88}
89
90test "Type.Optional" {
91 try testTypes(&[_]type{
92 ?u8,
93 ?*u8,
94 ?[]u8,
95 ?[*]u8,
96 ?[*c]u8,
97 });
98}
99
100test "Type.ErrorUnion" {
101 try testTypes(&[_]type{
102 error{}!void,
103 error{Error}!void,
104 });
105}
106
107test "Type.Opaque" {
108 const Opaque = @Type(.{
109 .Opaque = .{
110 .decls = &[_]TypeInfo.Declaration{},
111 },
112 });
113 try testing.expect(Opaque != opaque {});
114 try testing.expectEqualSlices(
115 TypeInfo.Declaration,
116 &[_]TypeInfo.Declaration{},
117 @typeInfo(Opaque).Opaque.decls,
118 );
119}
120
121test "Type.Vector" {
122 try testTypes(&[_]type{
123 @Vector(0, u8),
124 @Vector(4, u8),
125 @Vector(8, *u8),
126 std.meta.Vector(0, u8),
127 std.meta.Vector(4, u8),
128 std.meta.Vector(8, *u8),
129 });
130}
131
132test "Type.AnyFrame" {
133 try testTypes(&[_]type{
134 anyframe,
135 anyframe->u8,
136 anyframe->anyframe->u8,
137 });
138}
139
140fn add(a: i32, b: i32) i32 {
141 return a + b;
142}
143
144test "Type.ErrorSet" {
145 // error sets don't compare equal so just check if they compile
146 _ = @Type(@typeInfo(error{}));
147 _ = @Type(@typeInfo(error{A}));
148 _ = @Type(@typeInfo(error{ A, B, C }));
149}
150
151test "Type.Struct" {
152 const A = @Type(@typeInfo(struct { x: u8, y: u32 }));
153 const infoA = @typeInfo(A).Struct;
154 try testing.expectEqual(TypeInfo.ContainerLayout.Auto, infoA.layout);
155 try testing.expectEqualSlices(u8, "x", infoA.fields[0].name);
156 try testing.expectEqual(u8, infoA.fields[0].field_type);
157 try testing.expectEqual(@as(?*const anyopaque, null), infoA.fields[0].default_value);
158 try testing.expectEqualSlices(u8, "y", infoA.fields[1].name);
159 try testing.expectEqual(u32, infoA.fields[1].field_type);
160 try testing.expectEqual(@as(?*const anyopaque, null), infoA.fields[1].default_value);
161 try testing.expectEqualSlices(TypeInfo.Declaration, &[_]TypeInfo.Declaration{}, infoA.decls);
162 try testing.expectEqual(@as(bool, false), infoA.is_tuple);
163
164 var a = A{ .x = 0, .y = 1 };
165 try testing.expectEqual(@as(u8, 0), a.x);
166 try testing.expectEqual(@as(u32, 1), a.y);
167 a.y += 1;
168 try testing.expectEqual(@as(u32, 2), a.y);
169
170 const B = @Type(@typeInfo(extern struct { x: u8, y: u32 = 5 }));
171 const infoB = @typeInfo(B).Struct;
172 try testing.expectEqual(TypeInfo.ContainerLayout.Extern, infoB.layout);
173 try testing.expectEqualSlices(u8, "x", infoB.fields[0].name);
174 try testing.expectEqual(u8, infoB.fields[0].field_type);
175 try testing.expectEqual(@as(?*const anyopaque, null), infoB.fields[0].default_value);
176 try testing.expectEqualSlices(u8, "y", infoB.fields[1].name);
177 try testing.expectEqual(u32, infoB.fields[1].field_type);
178 try testing.expectEqual(@as(u32, 5), @ptrCast(*const u32, infoB.fields[1].default_value.?).*);
179 try testing.expectEqual(@as(usize, 0), infoB.decls.len);
180 try testing.expectEqual(@as(bool, false), infoB.is_tuple);
181
182 const C = @Type(@typeInfo(packed struct { x: u8 = 3, y: u32 = 5 }));
183 const infoC = @typeInfo(C).Struct;
184 try testing.expectEqual(TypeInfo.ContainerLayout.Packed, infoC.layout);
185 try testing.expectEqualSlices(u8, "x", infoC.fields[0].name);
186 try testing.expectEqual(u8, infoC.fields[0].field_type);
187 try testing.expectEqual(@as(u8, 3), @ptrCast(*const u8, infoC.fields[0].default_value.?).*);
188 try testing.expectEqualSlices(u8, "y", infoC.fields[1].name);
189 try testing.expectEqual(u32, infoC.fields[1].field_type);
190 try testing.expectEqual(@as(u32, 5), @ptrCast(*const u32, infoC.fields[1].default_value.?).*);
191 try testing.expectEqual(@as(usize, 0), infoC.decls.len);
192 try testing.expectEqual(@as(bool, false), infoC.is_tuple);
193}
194
195test "Type.Enum" {
196 const Foo = @Type(.{
197 .Enum = .{
198 .layout = .Auto,
199 .tag_type = u8,
200 .fields = &[_]TypeInfo.EnumField{
201 .{ .name = "a", .value = 1 },
202 .{ .name = "b", .value = 5 },
203 },
204 .decls = &[_]TypeInfo.Declaration{},
205 .is_exhaustive = true,
206 },
207 });
208 try testing.expectEqual(true, @typeInfo(Foo).Enum.is_exhaustive);
209 try testing.expectEqual(@as(u8, 1), @enumToInt(Foo.a));
210 try testing.expectEqual(@as(u8, 5), @enumToInt(Foo.b));
211 const Bar = @Type(.{
212 .Enum = .{
213 .layout = .Extern,
214 .tag_type = u32,
215 .fields = &[_]TypeInfo.EnumField{
216 .{ .name = "a", .value = 1 },
217 .{ .name = "b", .value = 5 },
218 },
219 .decls = &[_]TypeInfo.Declaration{},
220 .is_exhaustive = false,
221 },
222 });
223 try testing.expectEqual(false, @typeInfo(Bar).Enum.is_exhaustive);
224 try testing.expectEqual(@as(u32, 1), @enumToInt(Bar.a));
225 try testing.expectEqual(@as(u32, 5), @enumToInt(Bar.b));
226 try testing.expectEqual(@as(u32, 6), @enumToInt(@intToEnum(Bar, 6)));
227}
228
229test "Type.Union" {
230 const Untagged = @Type(.{
231 .Union = .{
232 .layout = .Auto,
233 .tag_type = null,
234 .fields = &[_]TypeInfo.UnionField{
235 .{ .name = "int", .field_type = i32, .alignment = @alignOf(f32) },
236 .{ .name = "float", .field_type = f32, .alignment = @alignOf(f32) },
237 },
238 .decls = &[_]TypeInfo.Declaration{},
239 },
240 });
241 var untagged = Untagged{ .int = 1 };
242 untagged.float = 2.0;
243 untagged.int = 3;
244 try testing.expectEqual(@as(i32, 3), untagged.int);
245
246 const PackedUntagged = @Type(.{
247 .Union = .{
248 .layout = .Packed,
249 .tag_type = null,
250 .fields = &[_]TypeInfo.UnionField{
251 .{ .name = "signed", .field_type = i32, .alignment = @alignOf(i32) },
252 .{ .name = "unsigned", .field_type = u32, .alignment = @alignOf(u32) },
253 },
254 .decls = &[_]TypeInfo.Declaration{},
255 },
256 });
257 var packed_untagged = PackedUntagged{ .signed = -1 };
258 try testing.expectEqual(@as(i32, -1), packed_untagged.signed);
259 try testing.expectEqual(~@as(u32, 0), packed_untagged.unsigned);
260
261 const Tag = @Type(.{
262 .Enum = .{
263 .layout = .Auto,
264 .tag_type = u1,
265 .fields = &[_]TypeInfo.EnumField{
266 .{ .name = "signed", .value = 0 },
267 .{ .name = "unsigned", .value = 1 },
268 },
269 .decls = &[_]TypeInfo.Declaration{},
270 .is_exhaustive = true,
271 },
272 });
273 const Tagged = @Type(.{
274 .Union = .{
275 .layout = .Auto,
276 .tag_type = Tag,
277 .fields = &[_]TypeInfo.UnionField{
278 .{ .name = "signed", .field_type = i32, .alignment = @alignOf(i32) },
279 .{ .name = "unsigned", .field_type = u32, .alignment = @alignOf(u32) },
280 },
281 .decls = &[_]TypeInfo.Declaration{},
282 },
283 });
284 var tagged = Tagged{ .signed = -1 };
285 try testing.expectEqual(Tag.signed, tagged);
286 tagged = .{ .unsigned = 1 };
287 try testing.expectEqual(Tag.unsigned, tagged);
288}
289
290test "Type.Union from Type.Enum" {
291 const Tag = @Type(.{
292 .Enum = .{
293 .layout = .Auto,
294 .tag_type = u0,
295 .fields = &[_]TypeInfo.EnumField{
296 .{ .name = "working_as_expected", .value = 0 },
297 },
298 .decls = &[_]TypeInfo.Declaration{},
299 .is_exhaustive = true,
300 },
301 });
302 const T = @Type(.{
303 .Union = .{
304 .layout = .Auto,
305 .tag_type = Tag,
306 .fields = &[_]TypeInfo.UnionField{
307 .{ .name = "working_as_expected", .field_type = u32, .alignment = @alignOf(u32) },
308 },
309 .decls = &[_]TypeInfo.Declaration{},
310 },
311 });
312 _ = T;
313 _ = @typeInfo(T).Union;
314}
315
316test "Type.Union from regular enum" {
317 const E = enum { working_as_expected };
318 const T = @Type(.{
319 .Union = .{
320 .layout = .Auto,
321 .tag_type = E,
322 .fields = &[_]TypeInfo.UnionField{
323 .{ .name = "working_as_expected", .field_type = u32, .alignment = @alignOf(u32) },
324 },
325 .decls = &[_]TypeInfo.Declaration{},
326 },
327 });
328 _ = T;
329 _ = @typeInfo(T).Union;
330}
331
332test "Type.Fn" {
333 // wasm doesn't support align attributes on functions
334 if (builtin.target.cpu.arch == .wasm32 or builtin.target.cpu.arch == .wasm64) return error.SkipZigTest;
335
336 const foo = struct {
337 fn func(a: usize, b: bool) align(4) callconv(.C) usize {
338 _ = a;
339 _ = b;
340 return 0;
341 }
342 }.func;
343 const Foo = @Type(@typeInfo(@TypeOf(foo)));
344 const foo_2: Foo = foo;
345 _ = foo_2;
346}
347
348test "Type.BoundFn" {
349 // wasm doesn't support align attributes on functions
350 if (builtin.target.cpu.arch == .wasm32 or builtin.target.cpu.arch == .wasm64) return error.SkipZigTest;
351
352 const TestStruct = packed struct {
353 pub fn foo(self: *const @This()) align(4) callconv(.Unspecified) void {
354 _ = self;
355 }
356 };
357 const test_instance: TestStruct = undefined;
358 try testing.expect(std.meta.eql(
359 @typeName(@TypeOf(test_instance.foo)),
360 @typeName(@Type(@typeInfo(@TypeOf(test_instance.foo)))),
361 ));
362}