Skip to content

Commit 2b589d7

Browse files
committed
stage2: move some tests which are now passing
1 parent cb24889 commit 2b589d7

9 files changed

+275
-275
lines changed

test/behavior.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ test {
5858
_ = @import("behavior/floatop.zig");
5959
_ = @import("behavior/fn.zig");
6060
_ = @import("behavior/for.zig");
61+
_ = @import("behavior/generics_llvm.zig");
6162
_ = @import("behavior/math.zig");
6263
_ = @import("behavior/maximum_minimum.zig");
6364
_ = @import("behavior/null_llvm.zig");
@@ -145,7 +146,6 @@ test {
145146
_ = @import("behavior/fn_delegation.zig");
146147
_ = @import("behavior/fn_in_struct_in_comptime.zig");
147148
_ = @import("behavior/for_stage1.zig");
148-
_ = @import("behavior/generics_stage1.zig");
149149
_ = @import("behavior/if_stage1.zig");
150150
_ = @import("behavior/import.zig");
151151
_ = @import("behavior/incomplete_struct_param_tld.zig");

test/behavior/cast.zig

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,32 @@ test "array coersion to undefined at runtime" {
266266
array = undefined;
267267
try expect(std.mem.eql(u8, &array, &undefined_val));
268268
}
269+
270+
test "implicitly cast from int to anyerror!?T" {
271+
implicitIntLitToOptional();
272+
comptime implicitIntLitToOptional();
273+
}
274+
fn implicitIntLitToOptional() void {
275+
const f: ?i32 = 1;
276+
_ = f;
277+
const g: anyerror!?i32 = 1;
278+
_ = g catch {};
279+
}
280+
281+
test "return u8 coercing into ?u32 return type" {
282+
const S = struct {
283+
fn doTheTest() !void {
284+
try expect(foo(123).? == 123);
285+
}
286+
fn foo(arg: u8) ?u32 {
287+
return arg;
288+
}
289+
};
290+
try S.doTheTest();
291+
comptime try S.doTheTest();
292+
}
293+
294+
test "cast from ?[*]T to ??[*]T" {
295+
const a: ??[*]u8 = @as(?[*]u8, null);
296+
try expect(a != null and a.? == null);
297+
}

test/behavior/cast_llvm.zig

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,135 @@ test "implicit ptr to *c_void" {
6565
var c: *u32 = @ptrCast(*u32, ptr2.?);
6666
try expect(c.* == 1);
6767
}
68+
69+
const A = struct {
70+
a: i32,
71+
};
72+
test "return null from fn() anyerror!?&T" {
73+
const a = returnNullFromOptionalTypeErrorRef();
74+
const b = returnNullLitFromOptionalTypeErrorRef();
75+
try expect((try a) == null and (try b) == null);
76+
}
77+
fn returnNullFromOptionalTypeErrorRef() anyerror!?*A {
78+
const a: ?*A = null;
79+
return a;
80+
}
81+
fn returnNullLitFromOptionalTypeErrorRef() anyerror!?*A {
82+
return null;
83+
}
84+
85+
test "peer type resolution: [0]u8 and []const u8" {
86+
try expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);
87+
try expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);
88+
comptime {
89+
try expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);
90+
try expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);
91+
}
92+
}
93+
fn peerTypeEmptyArrayAndSlice(a: bool, slice: []const u8) []const u8 {
94+
if (a) {
95+
return &[_]u8{};
96+
}
97+
98+
return slice[0..1];
99+
}
100+
101+
test "implicitly cast from [N]T to ?[]const T" {
102+
try expect(mem.eql(u8, castToOptionalSlice().?, "hi"));
103+
comptime try expect(mem.eql(u8, castToOptionalSlice().?, "hi"));
104+
}
105+
106+
fn castToOptionalSlice() ?[]const u8 {
107+
return "hi";
108+
}
109+
110+
test "cast u128 to f128 and back" {
111+
comptime try testCast128();
112+
try testCast128();
113+
}
114+
115+
fn testCast128() !void {
116+
try expect(cast128Int(cast128Float(0x7fff0000000000000000000000000000)) == 0x7fff0000000000000000000000000000);
117+
}
118+
119+
fn cast128Int(x: f128) u128 {
120+
return @bitCast(u128, x);
121+
}
122+
123+
fn cast128Float(x: u128) f128 {
124+
return @bitCast(f128, x);
125+
}
126+
127+
test "implicit cast from *[N]T to ?[*]T" {
128+
var x: ?[*]u16 = null;
129+
var y: [4]u16 = [4]u16{ 0, 1, 2, 3 };
130+
131+
x = &y;
132+
try expect(std.mem.eql(u16, x.?[0..4], y[0..4]));
133+
x.?[0] = 8;
134+
y[3] = 6;
135+
try expect(std.mem.eql(u16, x.?[0..4], y[0..4]));
136+
}
137+
138+
test "implicit cast from *T to ?*c_void" {
139+
var a: u8 = 1;
140+
incrementVoidPtrValue(&a);
141+
try std.testing.expect(a == 2);
142+
}
143+
144+
fn incrementVoidPtrValue(value: ?*c_void) void {
145+
@ptrCast(*u8, value.?).* += 1;
146+
}
147+
148+
test "implicit cast *[0]T to E![]const u8" {
149+
var x = @as(anyerror![]const u8, &[0]u8{});
150+
try expect((x catch unreachable).len == 0);
151+
}
152+
153+
var global_array: [4]u8 = undefined;
154+
test "cast from array reference to fn" {
155+
const f = @ptrCast(fn () callconv(.C) void, &global_array);
156+
try expect(@ptrToInt(f) == @ptrToInt(&global_array));
157+
}
158+
159+
test "*const [N]null u8 to ?[]const u8" {
160+
const S = struct {
161+
fn doTheTest() !void {
162+
var a = "Hello";
163+
var b: ?[]const u8 = a;
164+
try expect(mem.eql(u8, b.?, "Hello"));
165+
}
166+
};
167+
try S.doTheTest();
168+
comptime try S.doTheTest();
169+
}
170+
171+
test "cast between [*c]T and ?[*:0]T on fn parameter" {
172+
const S = struct {
173+
const Handler = ?fn ([*c]const u8) callconv(.C) void;
174+
fn addCallback(handler: Handler) void {
175+
_ = handler;
176+
}
177+
178+
fn myCallback(cstr: ?[*:0]const u8) callconv(.C) void {
179+
_ = cstr;
180+
}
181+
182+
fn doTheTest() void {
183+
addCallback(myCallback);
184+
}
185+
};
186+
S.doTheTest();
187+
}
188+
189+
var global_struct: struct { f0: usize } = undefined;
190+
test "assignment to optional pointer result loc" {
191+
var foo: struct { ptr: ?*c_void } = .{ .ptr = &global_struct };
192+
try expect(foo.ptr.? == @ptrCast(*c_void, &global_struct));
193+
}
194+
195+
test "cast between *[N]void and []void" {
196+
var a: [4]void = undefined;
197+
var b: []void = &a;
198+
try expect(b.len == 4);
199+
}

0 commit comments

Comments
 (0)