Skip to content

Commit 58c1d98

Browse files
Vexuandrewrk
authored andcommitted
add tests for fixed stage1 bugs
Closes #4144 Closes #4255 Closes #4372 Closes #4375 Closes #4380 Closes #4417 Closes #4423 Closes #4476 Closes #4528 Closes #4562 Closes #4572 Closes #4597 Closes #4639 Closes #4672 Closes #4782 Closes #4955 Closes #4984 Closes #4997 Closes #5010 Closes #5114 Closes #5166 Closes #5173 Closes #5276
1 parent 1c711b0 commit 58c1d98

14 files changed

+283
-6
lines changed

lib/std/fmt.zig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2773,3 +2773,21 @@ test "runtime precision specifier" {
27732773
try expectFmt("3.14e+00", "{:1.[1]}", .{ number, precision });
27742774
try expectFmt("3.14e+00", "{:1.[precision]}", .{ .number = number, .precision = precision });
27752775
}
2776+
2777+
test "recursive format function" {
2778+
const R = union(enum) {
2779+
const R = @This();
2780+
Leaf: i32,
2781+
Branch: struct { left: *const R, right: *const R },
2782+
2783+
pub fn format(self: R, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
2784+
return switch (self) {
2785+
.Leaf => |n| std.fmt.format(writer, "Leaf({})", .{n}),
2786+
.Branch => |b| std.fmt.format(writer, "Branch({}, {})", .{ b.left, b.right }),
2787+
};
2788+
}
2789+
};
2790+
2791+
var r = R{ .Leaf = 1 };
2792+
try expectFmt("Leaf(1)\n", "{}\n", .{&r});
2793+
}

lib/std/io/fixed_buffer_stream.zig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,17 @@ test "FixedBufferStream output" {
132132
try testing.expectEqualSlices(u8, "HelloWorld!", fbs.getWritten());
133133
}
134134

135+
test "FixedBufferStream output at comptime" {
136+
comptime {
137+
var buf: [255]u8 = undefined;
138+
var fbs = fixedBufferStream(&buf);
139+
const stream = fbs.writer();
140+
141+
try stream.print("{s}{s}!", .{ "Hello", "World" });
142+
try testing.expectEqualSlices(u8, "HelloWorld!", fbs.getWritten());
143+
}
144+
}
145+
135146
test "FixedBufferStream output 2" {
136147
var buffer: [10]u8 = undefined;
137148
var fbs = fixedBufferStream(&buffer);

test/behavior/array.zig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,3 +654,11 @@ test "array init of container level array variable" {
654654
S.bar(5, 6);
655655
try expectEqual([2]usize{ 5, 6 }, S.pair);
656656
}
657+
658+
test "runtime initialized sentinel-terminated array literal" {
659+
var c: u16 = 300;
660+
const f = &[_:0x9999]u16{c};
661+
const g = @ptrCast(*[4]u8, f);
662+
try std.testing.expect(g[2] == 0x99);
663+
try std.testing.expect(g[3] == 0x99);
664+
}

test/behavior/call.zig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,3 +394,20 @@ test "recursive inline call with comptime known argument" {
394394

395395
try expect(S.foo(4) == 20);
396396
}
397+
398+
test "inline while with @call" {
399+
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
400+
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
401+
402+
const S = struct {
403+
fn inc(a: *u32) void {
404+
a.* += 1;
405+
}
406+
};
407+
var a: u32 = 0;
408+
comptime var i = 0;
409+
inline while (i < 10) : (i += 1) {
410+
@call(.auto, S.inc, .{&a});
411+
}
412+
try expect(a == 10);
413+
}

test/behavior/cast.zig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,3 +1528,19 @@ test "optional pointer coerced to optional allowzero pointer" {
15281528
q = p;
15291529
try expect(@ptrToInt(q.?) == 4);
15301530
}
1531+
1532+
test "ptrToInt on const inside comptime block" {
1533+
var a = comptime blk: {
1534+
const b: u8 = 1;
1535+
const c = @ptrToInt(&b);
1536+
break :blk c;
1537+
};
1538+
try expect(@intToPtr(*const u8, a).* == 1);
1539+
}
1540+
1541+
test "single item pointer to pointer to array to slice" {
1542+
var x: i32 = 1234;
1543+
try expect(@as([]const i32, @as(*[1]i32, &x))[0] == 1234);
1544+
const z1 = @as([]const i32, @as(*[1]i32, &x));
1545+
try expect(z1[0] == 1234);
1546+
}

test/behavior/eval.zig

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,3 +1621,38 @@ test "inline for loop of functions returning error unions" {
16211621
}
16221622
try expect(a == 3);
16231623
}
1624+
1625+
test "if inside a switch" {
1626+
var condition = true;
1627+
var wave_type: u32 = 0;
1628+
var sample: i32 = switch (wave_type) {
1629+
0 => if (condition) 2 else 3,
1630+
1 => 100,
1631+
2 => 200,
1632+
3 => 300,
1633+
else => unreachable,
1634+
};
1635+
try expect(sample == 2);
1636+
}
1637+
1638+
test "function has correct return type when previous return is casted to smaller type" {
1639+
const S = struct {
1640+
fn foo(b: bool) u16 {
1641+
if (b) return @as(u8, 0xFF);
1642+
return 0xFFFF;
1643+
}
1644+
};
1645+
try expect(S.foo(true) == 0xFF);
1646+
}
1647+
1648+
test "early exit in container level const" {
1649+
const S = struct {
1650+
const value = blk: {
1651+
if (true) {
1652+
break :blk @as(u32, 1);
1653+
}
1654+
break :blk @as(u32, 0);
1655+
};
1656+
};
1657+
try expect(S.value == 1);
1658+
}

test/behavior/fn.zig

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,3 +484,36 @@ test "using @ptrCast on function pointers" {
484484
// https://github.com/ziglang/zig/issues/2626
485485
// try comptime S.run();
486486
}
487+
488+
test "function returns function returning type" {
489+
const S = struct {
490+
fn a() fn () type {
491+
return (struct {
492+
fn b() type {
493+
return u32;
494+
}
495+
}).b;
496+
}
497+
};
498+
try expect(S.a()() == u32);
499+
}
500+
501+
test "peer type resolution of inferred error set with non-void payload" {
502+
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
503+
504+
const S = struct {
505+
fn openDataFile(mode: enum { read, write }) !u32 {
506+
return switch (mode) {
507+
.read => foo(),
508+
.write => bar(),
509+
};
510+
}
511+
fn foo() error{ a, b }!u32 {
512+
return 1;
513+
}
514+
fn bar() error{ c, d }!u32 {
515+
return 2;
516+
}
517+
};
518+
try expect(try S.openDataFile(.read) == 1);
519+
}

test/behavior/packed-struct.zig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,3 +588,14 @@ test "overaligned pointer to packed struct" {
588588
},
589589
}
590590
}
591+
592+
test "packed struct initialized in bitcast" {
593+
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
594+
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
595+
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
596+
597+
const T = packed struct { val: u8 };
598+
var val: u8 = 123;
599+
const t = @bitCast(u8, T{ .val = val });
600+
try expect(t == val);
601+
}

test/behavior/slice.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,3 +737,13 @@ test "empty slice ptr is non null" {
737737
const t = @ptrCast([*]i8, p);
738738
try expect(@ptrToInt(t) == @ptrToInt(empty_slice.ptr));
739739
}
740+
741+
test "slice decays to many pointer" {
742+
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
743+
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
744+
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
745+
746+
var buf: [8]u8 = "abcdefg\x00".*;
747+
const p: [*:0]const u8 = buf[0..7 :0];
748+
try expectEqualStrings(buf[0..7], std.mem.span(p));
749+
}

test/behavior/struct.zig

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,3 +1495,63 @@ test "function pointer in struct returns the struct" {
14951495
var a = A.f();
14961496
try expect(a.f == A.f);
14971497
}
1498+
1499+
test "no dependency loop on optional field wrapped in generic function" {
1500+
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
1501+
1502+
const S = struct {
1503+
fn Atomic(comptime T: type) type {
1504+
return T;
1505+
}
1506+
const A = struct { b: Atomic(?*B) };
1507+
const B = struct { a: ?*A };
1508+
};
1509+
var a: S.A = .{ .b = null };
1510+
var b: S.B = .{ .a = &a };
1511+
a.b = &b;
1512+
1513+
try expect(a.b == &b);
1514+
try expect(b.a == &a);
1515+
}
1516+
1517+
test "optional field init with tuple" {
1518+
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
1519+
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
1520+
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
1521+
1522+
const S = struct {
1523+
a: ?struct { b: u32 },
1524+
};
1525+
var a: u32 = 0;
1526+
var b = S{
1527+
.a = .{ .b = a },
1528+
};
1529+
try expect(b.a.?.b == a);
1530+
}
1531+
1532+
test "if inside struct init inside if" {
1533+
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
1534+
1535+
const MyStruct = struct { x: u32 };
1536+
const b: u32 = 5;
1537+
var i: u32 = 1;
1538+
var my_var = if (i < 5)
1539+
MyStruct{
1540+
.x = 1 + if (i > 0) b else 0,
1541+
}
1542+
else
1543+
MyStruct{
1544+
.x = 1 + if (i > 0) b else 0,
1545+
};
1546+
try expect(my_var.x == 6);
1547+
}
1548+
1549+
test "optional generic function label struct field" {
1550+
const Options = struct {
1551+
isFoo: ?fn (type) u8 = defaultIsFoo,
1552+
fn defaultIsFoo(comptime _: type) u8 {
1553+
return 123;
1554+
}
1555+
};
1556+
try expect((Options{}).isFoo.?(u8) == 123);
1557+
}

test/behavior/translate_c_macros.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,7 @@ test "Macro that uses remainder operator. Issue #13346" {
221221
),
222222
);
223223
}
224+
225+
test "@typeInfo on @cImport result" {
226+
try expect(@typeInfo(h).Struct.decls.len > 1);
227+
}

test/behavior/tuple.zig

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const std = @import("std");
33
const testing = std.testing;
44
const expect = testing.expect;
55
const expectEqualStrings = std.testing.expectEqualStrings;
6+
const expectEqual = std.testing.expectEqual;
67

78
test "tuple concatenation" {
89
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
@@ -233,31 +234,31 @@ test "fieldParentPtr of anon struct" {
233234
test "offsetOf tuple" {
234235
var x: u32 = 0;
235236
const T = @TypeOf(.{ x, x });
236-
_ = @offsetOf(T, "1");
237+
try expect(@offsetOf(T, "1") == @sizeOf(u32));
237238
}
238239

239240
test "offsetOf anon struct" {
240241
var x: u32 = 0;
241242
const T = @TypeOf(.{ .foo = x, .bar = x });
242-
_ = @offsetOf(T, "bar");
243+
try expect(@offsetOf(T, "bar") == @sizeOf(u32));
243244
}
244245

245246
test "initializing tuple with mixed comptime-runtime fields" {
246-
if (true) return error.SkipZigTest; // TODO
247+
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
247248

248249
var x: u32 = 15;
249250
const T = @TypeOf(.{ @as(i32, -1234), @as(u32, 5678), x });
250251
var a: T = .{ -1234, 5678, x + 1 };
251-
_ = a;
252+
try expect(a[2] == 16);
252253
}
253254

254255
test "initializing anon struct with mixed comptime-runtime fields" {
255-
if (true) return error.SkipZigTest; // TODO
256+
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
256257

257258
var x: u32 = 15;
258259
const T = @TypeOf(.{ .foo = @as(i32, -1234), .bar = x });
259260
var a: T = .{ .foo = -1234, .bar = x + 1 };
260-
_ = a;
261+
try expect(a.bar == 16);
261262
}
262263

263264
test "tuple in tuple passed to generic function" {
@@ -366,3 +367,33 @@ test "tuple initialized with a runtime known value" {
366367
const w = .{W{ .w = e }};
367368
try expectEqualStrings(w[0].w.e, "test");
368369
}
370+
371+
test "tuple of struct concatenation and coercion to array" {
372+
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
373+
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
374+
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
375+
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
376+
377+
const StructWithDefault = struct { value: f32 = 42 };
378+
const SomeStruct = struct { array: [4]StructWithDefault };
379+
380+
const value1 = SomeStruct{ .array = .{StructWithDefault{}} ++ [_]StructWithDefault{.{}} ** 3 };
381+
const value2 = SomeStruct{ .array = .{.{}} ++ [_]StructWithDefault{.{}} ** 3 };
382+
383+
try expectEqual(value1, value2);
384+
}
385+
386+
test "nested runtime conditionals in tuple initializer" {
387+
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
388+
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
389+
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
390+
391+
var data: u8 = 0;
392+
const x = .{
393+
if (data != 0) "" else switch (@truncate(u1, data)) {
394+
0 => "up",
395+
1 => "down",
396+
},
397+
};
398+
try expectEqualStrings("up", x[0]);
399+
}

test/behavior/union.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,6 +1482,8 @@ test "no dependency loop when function pointer in union returns the union" {
14821482
b: *const fn (x: U) void,
14831483
c: *const fn (x: U) U,
14841484
d: *const fn (x: u8) U,
1485+
e: *const fn (x: *U) void,
1486+
f: *const fn (x: *U) U,
14851487
fn foo(x: u8) U {
14861488
return .{ .a = x };
14871489
}

test/cases/compile_errors/comptime_slice-sentinel_does_not_match_target-sentinel.zig

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@ export fn foo_slice() void {
5353
_ = slice;
5454
}
5555
}
56+
export fn undefined_slice() void {
57+
const arr: [100]u16 = undefined;
58+
const slice = arr[0..12 :0];
59+
_ = slice;
60+
}
61+
export fn string_slice() void {
62+
const str = "abcdefg";
63+
const slice = str[0..1 :12];
64+
_ = slice;
65+
}
66+
export fn typeName_slice() void {
67+
const arr = @typeName(usize);
68+
const slice = arr[0..2 :0];
69+
_ = slice;
70+
}
5671

5772
// error
5873
// backend=stage2
@@ -72,3 +87,9 @@ export fn foo_slice() void {
7287
// :44:29: note: expected '255', found '0'
7388
// :52:29: error: value in memory does not match slice sentinel
7489
// :52:29: note: expected '255', found '0'
90+
// :58:22: error: value in memory does not match slice sentinel
91+
// :58:22: note: expected '0', found 'undefined'
92+
// :63:22: error: value in memory does not match slice sentinel
93+
// :63:22: note: expected '12', found '98'
94+
// :68:22: error: value in memory does not match slice sentinel
95+
// :68:22: note: expected '0', found '105'

0 commit comments

Comments
 (0)