Commit 0a38f61d58
Changed files (125)
test
stage1
behavior
bugs
c_abi
standalone
test/stage1/behavior/bugs/1025.zig
@@ -8,5 +8,5 @@ fn getA() A {
test "bug 1025" {
const a = getA();
- @import("std").testing.expect(a.B == u8);
+ try @import("std").testing.expect(a.B == u8);
}
test/stage1/behavior/bugs/1076.zig
@@ -3,21 +3,21 @@ const mem = std.mem;
const expect = std.testing.expect;
test "comptime code should not modify constant data" {
- testCastPtrOfArrayToSliceAndPtr();
- comptime testCastPtrOfArrayToSliceAndPtr();
+ try testCastPtrOfArrayToSliceAndPtr();
+ comptime try testCastPtrOfArrayToSliceAndPtr();
}
-fn testCastPtrOfArrayToSliceAndPtr() void {
+fn testCastPtrOfArrayToSliceAndPtr() !void {
{
var array = "aoeu".*;
const x: [*]u8 = &array;
x[0] += 1;
- expect(mem.eql(u8, array[0..], "boeu"));
+ try expect(mem.eql(u8, array[0..], "boeu"));
}
{
var array: [4]u8 = "aoeu".*;
const x: [*]u8 = &array;
x[0] += 1;
- expect(mem.eql(u8, array[0..], "boeu"));
+ try expect(mem.eql(u8, array[0..], "boeu"));
}
}
test/stage1/behavior/bugs/1120.zig
@@ -19,5 +19,5 @@ test "bug 1120" {
1 => &b.a,
else => unreachable,
};
- expect(ptr.* == 2);
+ try expect(ptr.* == 2);
}
test/stage1/behavior/bugs/1277.zig
@@ -11,5 +11,5 @@ fn f() i32 {
}
test "don't emit an LLVM global for a const function when it's in an optional in a struct" {
- std.testing.expect(s.f.?() == 1234);
+ try std.testing.expect(s.f.?() == 1234);
}
test/stage1/behavior/bugs/1310.zig
@@ -20,5 +20,5 @@ fn agent_callback(_vm: [*]VM, options: [*]u8) callconv(.C) i32 {
}
test "fixed" {
- expect(agent_callback(undefined, undefined) == 11);
+ try expect(agent_callback(undefined, undefined) == 11);
}
test/stage1/behavior/bugs/1322.zig
@@ -13,7 +13,7 @@ const C = struct {};
test "tagged union with all void fields but a meaningful tag" {
var a: A = A{ .b = B{ .c = C{} } };
- std.testing.expect(@as(std.meta.Tag(B), a.b) == std.meta.Tag(B).c);
+ try std.testing.expect(@as(std.meta.Tag(B), a.b) == std.meta.Tag(B).c);
a = A{ .b = B.None };
- std.testing.expect(@as(std.meta.Tag(B), a.b) == std.meta.Tag(B).None);
+ try std.testing.expect(@as(std.meta.Tag(B), a.b) == std.meta.Tag(B).None);
}
test/stage1/behavior/bugs/1381.zig
@@ -17,5 +17,5 @@ test "union that needs padding bytes inside an array" {
};
const a = as[0].B;
- std.testing.expect(a.D == 1);
+ try std.testing.expect(a.D == 1);
}
test/stage1/behavior/bugs/1421.zig
@@ -10,5 +10,5 @@ const S = struct {
test "functions with return type required to be comptime are generic" {
const ti = S.method();
- expect(@as(builtin.TypeId, ti) == builtin.TypeId.Struct);
+ try expect(@as(builtin.TypeId, ti) == builtin.TypeId.Struct);
}
test/stage1/behavior/bugs/1442.zig
@@ -7,5 +7,5 @@ const Union = union(enum) {
test "const error union field alignment" {
var union_or_err: anyerror!Union = Union{ .Color = 1234 };
- std.testing.expect((union_or_err catch unreachable).Color == 1234);
+ try std.testing.expect((union_or_err catch unreachable).Color == 1234);
}
test/stage1/behavior/bugs/1486.zig
@@ -5,6 +5,6 @@ var global: u64 = 123;
test "constant pointer to global variable causes runtime load" {
global = 1234;
- expect(&global == ptr);
- expect(ptr.* == 1234);
+ try expect(&global == ptr);
+ try expect(ptr.* == 1234);
}
test/stage1/behavior/bugs/1607.zig
@@ -3,13 +3,13 @@ const testing = std.testing;
const a = [_]u8{ 1, 2, 3 };
-fn checkAddress(s: []const u8) void {
+fn checkAddress(s: []const u8) !void {
for (s) |*i, j| {
- testing.expect(i == &a[j]);
+ try testing.expect(i == &a[j]);
}
}
test "slices pointing at the same address as global array." {
- checkAddress(&a);
- comptime checkAddress(&a);
+ try checkAddress(&a);
+ comptime try checkAddress(&a);
}
test/stage1/behavior/bugs/1735.zig
@@ -42,5 +42,5 @@ const a = struct {
test "intialization" {
var t = a.init();
- std.testing.expect(t.foo.len == 0);
+ try std.testing.expect(t.foo.len == 0);
}
test/stage1/behavior/bugs/1741.zig
@@ -2,5 +2,5 @@ const std = @import("std");
test "fixed" {
const x: f32 align(128) = 12.34;
- std.testing.expect(@ptrToInt(&x) % 128 == 0);
+ try std.testing.expect(@ptrToInt(&x) % 128 == 0);
}
test/stage1/behavior/bugs/1851.zig
@@ -2,25 +2,25 @@ const std = @import("std");
const expect = std.testing.expect;
test "allocation and looping over 3-byte integer" {
- expect(@sizeOf(u24) == 4);
- expect(@sizeOf([1]u24) == 4);
- expect(@alignOf(u24) == 4);
- expect(@alignOf([1]u24) == 4);
+ try expect(@sizeOf(u24) == 4);
+ try expect(@sizeOf([1]u24) == 4);
+ try expect(@alignOf(u24) == 4);
+ try expect(@alignOf([1]u24) == 4);
var x = try std.testing.allocator.alloc(u24, 2);
defer std.testing.allocator.free(x);
- expect(x.len == 2);
+ try expect(x.len == 2);
x[0] = 0xFFFFFF;
x[1] = 0xFFFFFF;
const bytes = std.mem.sliceAsBytes(x);
- expect(@TypeOf(bytes) == []align(4) u8);
- expect(bytes.len == 8);
+ try expect(@TypeOf(bytes) == []align(4) u8);
+ try expect(bytes.len == 8);
for (bytes) |*b| {
b.* = 0x00;
}
- expect(x[0] == 0x00);
- expect(x[1] == 0x00);
+ try expect(x[0] == 0x00);
+ try expect(x[1] == 0x00);
}
test/stage1/behavior/bugs/2006.zig
@@ -7,6 +7,6 @@ const S = struct {
test "bug 2006" {
var a: S = undefined;
a = S{ .p = undefined };
- expect(@sizeOf(S) != 0);
- expect(@sizeOf(*void) == 0);
+ try expect(@sizeOf(S) != 0);
+ try expect(@sizeOf(*void) == 0);
}
test/stage1/behavior/bugs/2114.zig
@@ -7,13 +7,13 @@ fn ctz(x: anytype) usize {
}
test "fixed" {
- testClz();
- comptime testClz();
+ try testClz();
+ comptime try testClz();
}
-fn testClz() void {
- expect(ctz(@as(u128, 0x40000000000000000000000000000000)) == 126);
- expect(math.rotl(u128, @as(u128, 0x40000000000000000000000000000000), @as(u8, 1)) == @as(u128, 0x80000000000000000000000000000000));
- expect(ctz(@as(u128, 0x80000000000000000000000000000000)) == 127);
- expect(ctz(math.rotl(u128, @as(u128, 0x40000000000000000000000000000000), @as(u8, 1))) == 127);
+fn testClz() !void {
+ try expect(ctz(@as(u128, 0x40000000000000000000000000000000)) == 126);
+ try expect(math.rotl(u128, @as(u128, 0x40000000000000000000000000000000), @as(u8, 1)) == @as(u128, 0x80000000000000000000000000000000));
+ try expect(ctz(@as(u128, 0x80000000000000000000000000000000)) == 127);
+ try expect(ctz(math.rotl(u128, @as(u128, 0x40000000000000000000000000000000), @as(u8, 1))) == 127);
}
test/stage1/behavior/bugs/2889.zig
@@ -27,5 +27,5 @@ fn parseNote() ?i32 {
test "fixed" {
const result = parseNote();
- std.testing.expect(result.? == 9);
+ try std.testing.expect(result.? == 9);
}
test/stage1/behavior/bugs/3007.zig
@@ -19,5 +19,5 @@ fn get_foo() Foo.FooError!*Foo {
test "fixed" {
default_foo = get_foo() catch null; // This Line
- std.testing.expect(!default_foo.?.free);
+ try std.testing.expect(!default_foo.?.free);
}
test/stage1/behavior/bugs/3046.zig
@@ -15,5 +15,5 @@ test "fixed" {
some_struct = SomeStruct{
.field = couldFail() catch |_| @as(i32, 0),
};
- expect(some_struct.field == 1);
+ try expect(some_struct.field == 1);
}
test/stage1/behavior/bugs/3112.zig
@@ -7,7 +7,7 @@ const State = struct {
};
fn prev(p: ?State) void {
- expect(p == null);
+ expect(p == null) catch @panic("test failure");
}
test "zig test crash" {
test/stage1/behavior/bugs/3384.zig
@@ -2,10 +2,10 @@ const std = @import("std");
const expect = std.testing.expect;
test "resolve array slice using builtin" {
- expect(@hasDecl(@This(), "std") == true);
- expect(@hasDecl(@This(), "std"[0..0]) == false);
- expect(@hasDecl(@This(), "std"[0..1]) == false);
- expect(@hasDecl(@This(), "std"[0..2]) == false);
- expect(@hasDecl(@This(), "std"[0..3]) == true);
- expect(@hasDecl(@This(), "std"[0..]) == true);
+ try expect(@hasDecl(@This(), "std") == true);
+ try expect(@hasDecl(@This(), "std"[0..0]) == false);
+ try expect(@hasDecl(@This(), "std"[0..1]) == false);
+ try expect(@hasDecl(@This(), "std"[0..2]) == false);
+ try expect(@hasDecl(@This(), "std"[0..3]) == true);
+ try expect(@hasDecl(@This(), "std"[0..]) == true);
}
test/stage1/behavior/bugs/394.zig
@@ -14,5 +14,5 @@ test "bug 394 fixed" {
.x = 3,
.y = E{ .B = 1 },
};
- expect(x.x == 3);
+ try expect(x.x == 3);
}
test/stage1/behavior/bugs/421.zig
@@ -1,12 +1,12 @@
const expect = @import("std").testing.expect;
test "bitCast to array" {
- comptime testBitCastArray();
- testBitCastArray();
+ comptime try testBitCastArray();
+ try testBitCastArray();
}
-fn testBitCastArray() void {
- expect(extractOne64(0x0123456789abcdef0123456789abcdef) == 0x0123456789abcdef);
+fn testBitCastArray() !void {
+ try expect(extractOne64(0x0123456789abcdef0123456789abcdef) == 0x0123456789abcdef);
}
fn extractOne64(a: u128) u64 {
test/stage1/behavior/bugs/4328.zig
@@ -25,14 +25,14 @@ test "Extern function calls in @TypeOf" {
return 1;
}
- fn doTheTest() void {
- expectEqual(c_int, @TypeOf(test_fn_1(0, 42)));
- expectEqual(c_short, @TypeOf(test_fn_2(0)));
+ fn doTheTest() !void {
+ try expectEqual(c_int, @TypeOf(test_fn_1(0, 42)));
+ try expectEqual(c_short, @TypeOf(test_fn_2(0)));
}
};
- Test.doTheTest();
- comptime Test.doTheTest();
+ try Test.doTheTest();
+ comptime try Test.doTheTest();
}
test "Peer resolution of extern function calls in @TypeOf" {
@@ -41,13 +41,13 @@ test "Peer resolution of extern function calls in @TypeOf" {
return 0;
}
- fn doTheTest() void {
- expectEqual(c_long, @TypeOf(test_fn()));
+ fn doTheTest() !void {
+ try expectEqual(c_long, @TypeOf(test_fn()));
}
};
- Test.doTheTest();
- comptime Test.doTheTest();
+ try Test.doTheTest();
+ comptime try Test.doTheTest();
}
test "Extern function calls, dereferences and field access in @TypeOf" {
@@ -60,12 +60,12 @@ test "Extern function calls, dereferences and field access in @TypeOf" {
return 255;
}
- fn doTheTest() void {
- expectEqual(FILE, @TypeOf(test_fn_1(0)));
- expectEqual(u8, @TypeOf(test_fn_2(0)));
+ fn doTheTest() !void {
+ try expectEqual(FILE, @TypeOf(test_fn_1(0)));
+ try expectEqual(u8, @TypeOf(test_fn_2(0)));
}
};
- Test.doTheTest();
- comptime Test.doTheTest();
+ try Test.doTheTest();
+ comptime try Test.doTheTest();
}
test/stage1/behavior/bugs/4560.zig
@@ -8,9 +8,9 @@ test "fixed" {
.max_distance_from_start_index = 456,
},
};
- std.testing.expect(s.a == 1);
- std.testing.expect(s.b.size == 123);
- std.testing.expect(s.b.max_distance_from_start_index == 456);
+ try std.testing.expect(s.a == 1);
+ try std.testing.expect(s.b.size == 123);
+ try std.testing.expect(s.b.max_distance_from_start_index == 456);
}
const S = struct {
test/stage1/behavior/bugs/4769_a.zig
@@ -1,1 +1,1 @@
-//
\ No newline at end of file
+//
test/stage1/behavior/bugs/4769_b.zig
@@ -1,1 +1,1 @@
-//!
\ No newline at end of file
+//!
test/stage1/behavior/bugs/5398.zig
@@ -25,7 +25,7 @@ test "assignment of field with padding" {
.emits_shadows = false,
},
};
- testing.expectEqual(false, renderable.material.transparent);
- testing.expectEqual(false, renderable.material.emits_shadows);
- testing.expectEqual(true, renderable.material.render_color);
+ try testing.expectEqual(false, renderable.material.transparent);
+ try testing.expectEqual(false, renderable.material.emits_shadows);
+ try testing.expectEqual(true, renderable.material.render_color);
}
test/stage1/behavior/bugs/5413.zig
@@ -1,6 +1,6 @@
const expect = @import("std").testing.expect;
test "Peer type resolution with string literals and unknown length u8 pointers" {
- expect(@TypeOf("", "a", @as([*:0]const u8, "")) == [*:0]const u8);
- expect(@TypeOf(@as([*:0]const u8, "baz"), "foo", "bar") == [*:0]const u8);
+ try expect(@TypeOf("", "a", @as([*:0]const u8, "")) == [*:0]const u8);
+ try expect(@TypeOf(@as([*:0]const u8, "baz"), "foo", "bar") == [*:0]const u8);
}
test/stage1/behavior/bugs/5474.zig
@@ -25,33 +25,33 @@ const Box2 = struct {
};
};
-fn doTest() void {
+fn doTest() !void {
// var
{
var box0: Box0 = .{ .items = undefined };
- std.testing.expect(@typeInfo(@TypeOf(box0.items[0..])).Pointer.is_const == false);
+ try std.testing.expect(@typeInfo(@TypeOf(box0.items[0..])).Pointer.is_const == false);
var box1: Box1 = .{ .items = undefined };
- std.testing.expect(@typeInfo(@TypeOf(box1.items[0..])).Pointer.is_const == false);
+ try std.testing.expect(@typeInfo(@TypeOf(box1.items[0..])).Pointer.is_const == false);
var box2: Box2 = .{ .items = undefined };
- std.testing.expect(@typeInfo(@TypeOf(box2.items[0..])).Pointer.is_const == false);
+ try std.testing.expect(@typeInfo(@TypeOf(box2.items[0..])).Pointer.is_const == false);
}
// const
{
const box0: Box0 = .{ .items = undefined };
- std.testing.expect(@typeInfo(@TypeOf(box0.items[0..])).Pointer.is_const == true);
+ try std.testing.expect(@typeInfo(@TypeOf(box0.items[0..])).Pointer.is_const == true);
const box1: Box1 = .{ .items = undefined };
- std.testing.expect(@typeInfo(@TypeOf(box1.items[0..])).Pointer.is_const == true);
+ try std.testing.expect(@typeInfo(@TypeOf(box1.items[0..])).Pointer.is_const == true);
const box2: Box2 = .{ .items = undefined };
- std.testing.expect(@typeInfo(@TypeOf(box2.items[0..])).Pointer.is_const == true);
+ try std.testing.expect(@typeInfo(@TypeOf(box2.items[0..])).Pointer.is_const == true);
}
}
test "pointer-to-array constness for zero-size elements" {
- doTest();
- comptime doTest();
+ try doTest();
+ comptime try doTest();
}
test/stage1/behavior/bugs/624.zig
@@ -19,5 +19,5 @@ fn MemoryPool(comptime T: type) type {
test "foo" {
var allocator = ContextAllocator{ .n = 10 };
- expect(allocator.n == 10);
+ try expect(allocator.n == 10);
}
test/stage1/behavior/bugs/6456.zig
@@ -35,9 +35,9 @@ test "issue 6456" {
});
const gen_fields = @typeInfo(T).Struct.fields;
- testing.expectEqual(3, gen_fields.len);
- testing.expectEqualStrings("f1", gen_fields[0].name);
- testing.expectEqualStrings("f2", gen_fields[1].name);
- testing.expectEqualStrings("f3", gen_fields[2].name);
+ try testing.expectEqual(3, gen_fields.len);
+ try testing.expectEqualStrings("f1", gen_fields[0].name);
+ try testing.expectEqualStrings("f2", gen_fields[1].name);
+ try testing.expectEqualStrings("f3", gen_fields[2].name);
}
}
test/stage1/behavior/bugs/655.zig
@@ -3,10 +3,10 @@ const other_file = @import("655_other_file.zig");
test "function with *const parameter with type dereferenced by namespace" {
const x: other_file.Integer = 1234;
- comptime std.testing.expect(@TypeOf(&x) == *const other_file.Integer);
- foo(&x);
+ comptime try std.testing.expect(@TypeOf(&x) == *const other_file.Integer);
+ try foo(&x);
}
-fn foo(x: *const other_file.Integer) void {
- std.testing.expect(x.* == 1234);
+fn foo(x: *const other_file.Integer) !void {
+ try std.testing.expect(x.* == 1234);
}
test/stage1/behavior/bugs/656.zig
@@ -10,10 +10,10 @@ const Value = struct {
};
test "optional if after an if in a switch prong of a switch with 2 prongs in an else" {
- foo(false, true);
+ try foo(false, true);
}
-fn foo(a: bool, b: bool) void {
+fn foo(a: bool, b: bool) !void {
var prefix_op = PrefixOp{
.AddrOf = Value{ .align_expr = 1234 },
};
@@ -22,7 +22,7 @@ fn foo(a: bool, b: bool) void {
PrefixOp.AddrOf => |addr_of_info| {
if (b) {}
if (addr_of_info.align_expr) |align_expr| {
- expect(align_expr == 1234);
+ try expect(align_expr == 1234);
}
},
PrefixOp.Return => {},
test/stage1/behavior/bugs/679.zig
@@ -13,5 +13,5 @@ const Element = struct {
test "false dependency loop in struct definition" {
const listType = ElementList;
var x: listType = 42;
- expect(x == 42);
+ try expect(x == 42);
}
test/stage1/behavior/bugs/6850.zig
@@ -4,7 +4,7 @@ test "lazy sizeof comparison with zero" {
const Empty = struct {};
const T = *Empty;
- std.testing.expect(hasNoBits(T));
+ try std.testing.expect(hasNoBits(T));
}
fn hasNoBits(comptime T: type) bool {
test/stage1/behavior/bugs/7047.zig
@@ -15,8 +15,8 @@ fn S(comptime query: U) type {
test "compiler doesn't consider equal unions with different 'type' payload" {
const s1 = S(U{ .T = u32 }).tag();
- std.testing.expectEqual(u32, s1);
+ try std.testing.expectEqual(u32, s1);
const s2 = S(U{ .T = u64 }).tag();
- std.testing.expectEqual(u64, s2);
+ try std.testing.expectEqual(u64, s2);
}
test/stage1/behavior/bugs/718.zig
@@ -10,8 +10,8 @@ const Keys = struct {
var keys: Keys = undefined;
test "zero keys with @memset" {
@memset(@ptrCast([*]u8, &keys), 0, @sizeOf(@TypeOf(keys)));
- expect(!keys.up);
- expect(!keys.down);
- expect(!keys.left);
- expect(!keys.right);
+ try expect(!keys.up);
+ try expect(!keys.down);
+ try expect(!keys.left);
+ try expect(!keys.right);
}
test/stage1/behavior/bugs/726.zig
@@ -3,7 +3,7 @@ const expect = @import("std").testing.expect;
test "@ptrCast from const to nullable" {
const c: u8 = 4;
var x: ?*const u8 = @ptrCast(?*const u8, &c);
- expect(x.?.* == 4);
+ try expect(x.?.* == 4);
}
test "@ptrCast from var in empty struct to nullable" {
@@ -11,5 +11,5 @@ test "@ptrCast from var in empty struct to nullable" {
var c: u8 = 4;
};
var x: ?*const u8 = @ptrCast(?*const u8, &container.c);
- expect(x.?.* == 4);
+ try expect(x.?.* == 4);
}
test/stage1/behavior/bugs/920.zig
@@ -60,6 +60,6 @@ test "bug 920 fixed" {
};
for (NormalDist1.f) |_, i| {
- std.testing.expectEqual(NormalDist1.f[i], NormalDist.f[i]);
+ try std.testing.expectEqual(NormalDist1.f[i], NormalDist.f[i]);
}
}
test/stage1/behavior/align.zig
@@ -5,16 +5,16 @@ const builtin = @import("builtin");
var foo: u8 align(4) = 100;
test "global variable alignment" {
- comptime expect(@typeInfo(@TypeOf(&foo)).Pointer.alignment == 4);
- comptime expect(@TypeOf(&foo) == *align(4) u8);
+ comptime try expect(@typeInfo(@TypeOf(&foo)).Pointer.alignment == 4);
+ comptime try expect(@TypeOf(&foo) == *align(4) u8);
{
const slice = @as(*[1]u8, &foo)[0..];
- comptime expect(@TypeOf(slice) == *align(4) [1]u8);
+ comptime try expect(@TypeOf(slice) == *align(4) [1]u8);
}
{
var runtime_zero: usize = 0;
const slice = @as(*[1]u8, &foo)[runtime_zero..];
- comptime expect(@TypeOf(slice) == []align(4) u8);
+ comptime try expect(@TypeOf(slice) == []align(4) u8);
}
}
@@ -28,9 +28,9 @@ test "function alignment" {
// function alignment is a compile error on wasm32/wasm64
if (builtin.arch == .wasm32 or builtin.arch == .wasm64) return error.SkipZigTest;
- expect(derp() == 1234);
- expect(@TypeOf(noop1) == fn () align(1) void);
- expect(@TypeOf(noop4) == fn () align(4) void);
+ try expect(derp() == 1234);
+ try expect(@TypeOf(noop1) == fn () align(1) void);
+ try expect(@TypeOf(noop4) == fn () align(4) void);
noop1();
noop4();
}
@@ -41,7 +41,7 @@ var baz: packed struct {
} = undefined;
test "packed struct alignment" {
- expect(@TypeOf(&baz.b) == *align(1) u32);
+ try expect(@TypeOf(&baz.b) == *align(1) u32);
}
const blah: packed struct {
@@ -51,17 +51,17 @@ const blah: packed struct {
} = undefined;
test "bit field alignment" {
- expect(@TypeOf(&blah.b) == *align(1:3:1) const u3);
+ try expect(@TypeOf(&blah.b) == *align(1:3:1) const u3);
}
test "default alignment allows unspecified in type syntax" {
- expect(*u32 == *align(@alignOf(u32)) u32);
+ try expect(*u32 == *align(@alignOf(u32)) u32);
}
test "implicitly decreasing pointer alignment" {
const a: u32 align(4) = 3;
const b: u32 align(8) = 4;
- expect(addUnaligned(&a, &b) == 7);
+ try expect(addUnaligned(&a, &b) == 7);
}
fn addUnaligned(a: *align(1) const u32, b: *align(1) const u32) u32 {
@@ -71,16 +71,16 @@ fn addUnaligned(a: *align(1) const u32, b: *align(1) const u32) u32 {
test "implicitly decreasing slice alignment" {
const a: u32 align(4) = 3;
const b: u32 align(8) = 4;
- expect(addUnalignedSlice(@as(*const [1]u32, &a)[0..], @as(*const [1]u32, &b)[0..]) == 7);
+ try expect(addUnalignedSlice(@as(*const [1]u32, &a)[0..], @as(*const [1]u32, &b)[0..]) == 7);
}
fn addUnalignedSlice(a: []align(1) const u32, b: []align(1) const u32) u32 {
return a[0] + b[0];
}
test "specifying alignment allows pointer cast" {
- testBytesAlign(0x33);
+ try testBytesAlign(0x33);
}
-fn testBytesAlign(b: u8) void {
+fn testBytesAlign(b: u8) !void {
var bytes align(4) = [_]u8{
b,
b,
@@ -88,13 +88,13 @@ fn testBytesAlign(b: u8) void {
b,
};
const ptr = @ptrCast(*u32, &bytes[0]);
- expect(ptr.* == 0x33333333);
+ try expect(ptr.* == 0x33333333);
}
test "@alignCast pointers" {
var x: u32 align(4) = 1;
expectsOnly1(&x);
- expect(x == 2);
+ try expect(x == 2);
}
fn expectsOnly1(x: *align(1) u32) void {
expects4(@alignCast(4, x));
@@ -110,7 +110,7 @@ test "@alignCast slices" {
};
const slice = array[0..];
sliceExpectsOnly1(slice);
- expect(slice[0] == 2);
+ try expect(slice[0] == 2);
}
fn sliceExpectsOnly1(slice: []align(1) u32) void {
sliceExpects4(@alignCast(4, slice));
@@ -123,12 +123,12 @@ test "implicitly decreasing fn alignment" {
// function alignment is a compile error on wasm32/wasm64
if (builtin.arch == .wasm32 or builtin.arch == .wasm64) return error.SkipZigTest;
- testImplicitlyDecreaseFnAlign(alignedSmall, 1234);
- testImplicitlyDecreaseFnAlign(alignedBig, 5678);
+ try testImplicitlyDecreaseFnAlign(alignedSmall, 1234);
+ try testImplicitlyDecreaseFnAlign(alignedBig, 5678);
}
-fn testImplicitlyDecreaseFnAlign(ptr: fn () align(1) i32, answer: i32) void {
- expect(ptr() == answer);
+fn testImplicitlyDecreaseFnAlign(ptr: fn () align(1) i32, answer: i32) !void {
+ try expect(ptr() == answer);
}
fn alignedSmall() align(8) i32 {
@@ -143,7 +143,7 @@ test "@alignCast functions" {
if (builtin.arch == .wasm32 or builtin.arch == .wasm64) return error.SkipZigTest;
if (builtin.arch == .thumb) return error.SkipZigTest;
- expect(fnExpectsOnly1(simple4) == 0x19);
+ try expect(fnExpectsOnly1(simple4) == 0x19);
}
fn fnExpectsOnly1(ptr: fn () align(1) i32) i32 {
return fnExpects4(@alignCast(4, ptr));
@@ -160,9 +160,9 @@ test "generic function with align param" {
if (builtin.arch == .wasm32 or builtin.arch == .wasm64) return error.SkipZigTest;
if (builtin.arch == .thumb) return error.SkipZigTest;
- expect(whyWouldYouEverDoThis(1) == 0x1);
- expect(whyWouldYouEverDoThis(4) == 0x1);
- expect(whyWouldYouEverDoThis(8) == 0x1);
+ try expect(whyWouldYouEverDoThis(1) == 0x1);
+ try expect(whyWouldYouEverDoThis(4) == 0x1);
+ try expect(whyWouldYouEverDoThis(8) == 0x1);
}
fn whyWouldYouEverDoThis(comptime align_bytes: u8) align(align_bytes) u8 {
@@ -172,49 +172,49 @@ fn whyWouldYouEverDoThis(comptime align_bytes: u8) align(align_bytes) u8 {
test "@ptrCast preserves alignment of bigger source" {
var x: u32 align(16) = 1234;
const ptr = @ptrCast(*u8, &x);
- expect(@TypeOf(ptr) == *align(16) u8);
+ try expect(@TypeOf(ptr) == *align(16) u8);
}
test "runtime known array index has best alignment possible" {
// take full advantage of over-alignment
var array align(4) = [_]u8{ 1, 2, 3, 4 };
- expect(@TypeOf(&array[0]) == *align(4) u8);
- expect(@TypeOf(&array[1]) == *u8);
- expect(@TypeOf(&array[2]) == *align(2) u8);
- expect(@TypeOf(&array[3]) == *u8);
+ try expect(@TypeOf(&array[0]) == *align(4) u8);
+ try expect(@TypeOf(&array[1]) == *u8);
+ try expect(@TypeOf(&array[2]) == *align(2) u8);
+ try expect(@TypeOf(&array[3]) == *u8);
// because align is too small but we still figure out to use 2
var bigger align(2) = [_]u64{ 1, 2, 3, 4 };
- expect(@TypeOf(&bigger[0]) == *align(2) u64);
- expect(@TypeOf(&bigger[1]) == *align(2) u64);
- expect(@TypeOf(&bigger[2]) == *align(2) u64);
- expect(@TypeOf(&bigger[3]) == *align(2) u64);
+ try expect(@TypeOf(&bigger[0]) == *align(2) u64);
+ try expect(@TypeOf(&bigger[1]) == *align(2) u64);
+ try expect(@TypeOf(&bigger[2]) == *align(2) u64);
+ try expect(@TypeOf(&bigger[3]) == *align(2) u64);
// because pointer is align 2 and u32 align % 2 == 0 we can assume align 2
var smaller align(2) = [_]u32{ 1, 2, 3, 4 };
var runtime_zero: usize = 0;
- comptime expect(@TypeOf(smaller[runtime_zero..]) == []align(2) u32);
- comptime expect(@TypeOf(smaller[runtime_zero..].ptr) == [*]align(2) u32);
- testIndex(smaller[runtime_zero..].ptr, 0, *align(2) u32);
- testIndex(smaller[runtime_zero..].ptr, 1, *align(2) u32);
- testIndex(smaller[runtime_zero..].ptr, 2, *align(2) u32);
- testIndex(smaller[runtime_zero..].ptr, 3, *align(2) u32);
+ comptime try expect(@TypeOf(smaller[runtime_zero..]) == []align(2) u32);
+ comptime try expect(@TypeOf(smaller[runtime_zero..].ptr) == [*]align(2) u32);
+ try testIndex(smaller[runtime_zero..].ptr, 0, *align(2) u32);
+ try testIndex(smaller[runtime_zero..].ptr, 1, *align(2) u32);
+ try testIndex(smaller[runtime_zero..].ptr, 2, *align(2) u32);
+ try testIndex(smaller[runtime_zero..].ptr, 3, *align(2) u32);
// has to use ABI alignment because index known at runtime only
- testIndex2(array[runtime_zero..].ptr, 0, *u8);
- testIndex2(array[runtime_zero..].ptr, 1, *u8);
- testIndex2(array[runtime_zero..].ptr, 2, *u8);
- testIndex2(array[runtime_zero..].ptr, 3, *u8);
+ try testIndex2(array[runtime_zero..].ptr, 0, *u8);
+ try testIndex2(array[runtime_zero..].ptr, 1, *u8);
+ try testIndex2(array[runtime_zero..].ptr, 2, *u8);
+ try testIndex2(array[runtime_zero..].ptr, 3, *u8);
}
-fn testIndex(smaller: [*]align(2) u32, index: usize, comptime T: type) void {
- comptime expect(@TypeOf(&smaller[index]) == T);
+fn testIndex(smaller: [*]align(2) u32, index: usize, comptime T: type) !void {
+ comptime try expect(@TypeOf(&smaller[index]) == T);
}
-fn testIndex2(ptr: [*]align(4) u8, index: usize, comptime T: type) void {
- comptime expect(@TypeOf(&ptr[index]) == T);
+fn testIndex2(ptr: [*]align(4) u8, index: usize, comptime T: type) !void {
+ comptime try expect(@TypeOf(&ptr[index]) == T);
}
test "alignstack" {
- expect(fnWithAlignedStack() == 1234);
+ try expect(fnWithAlignedStack() == 1234);
}
fn fnWithAlignedStack() i32 {
@@ -223,7 +223,7 @@ fn fnWithAlignedStack() i32 {
}
test "alignment of structs" {
- expect(@alignOf(struct {
+ try expect(@alignOf(struct {
a: i32,
b: *i32,
}) == @alignOf(usize));
@@ -239,37 +239,37 @@ test "alignment of function with c calling convention" {
fn nothing() callconv(.C) void {}
test "return error union with 128-bit integer" {
- expect(3 == try give());
+ try expect(3 == try give());
}
fn give() anyerror!u128 {
return 3;
}
test "alignment of >= 128-bit integer type" {
- expect(@alignOf(u128) == 16);
- expect(@alignOf(u129) == 16);
+ try expect(@alignOf(u128) == 16);
+ try expect(@alignOf(u129) == 16);
}
test "alignment of struct with 128-bit field" {
- expect(@alignOf(struct {
+ try expect(@alignOf(struct {
x: u128,
}) == 16);
comptime {
- expect(@alignOf(struct {
+ try expect(@alignOf(struct {
x: u128,
}) == 16);
}
}
test "size of extern struct with 128-bit field" {
- expect(@sizeOf(extern struct {
+ try expect(@sizeOf(extern struct {
x: u128,
y: u8,
}) == 32);
comptime {
- expect(@sizeOf(extern struct {
+ try expect(@sizeOf(extern struct {
x: u128,
y: u8,
}) == 32);
@@ -286,8 +286,8 @@ test "read 128-bit field from default aligned struct in stack memory" {
.nevermind = 1,
.badguy = 12,
};
- expect((@ptrToInt(&default_aligned.badguy) % 16) == 0);
- expect(12 == default_aligned.badguy);
+ try expect((@ptrToInt(&default_aligned.badguy) % 16) == 0);
+ try expect(12 == default_aligned.badguy);
}
var default_aligned_global = DefaultAligned{
@@ -296,8 +296,8 @@ var default_aligned_global = DefaultAligned{
};
test "read 128-bit field from default aligned struct in global memory" {
- expect((@ptrToInt(&default_aligned_global.badguy) % 16) == 0);
- expect(12 == default_aligned_global.badguy);
+ try expect((@ptrToInt(&default_aligned_global.badguy) % 16) == 0);
+ try expect(12 == default_aligned_global.badguy);
}
test "struct field explicit alignment" {
@@ -310,9 +310,9 @@ test "struct field explicit alignment" {
var node: S.Node = undefined;
node.massive_byte = 100;
- expect(node.massive_byte == 100);
- comptime expect(@TypeOf(&node.massive_byte) == *align(64) u8);
- expect(@ptrToInt(&node.massive_byte) % 64 == 0);
+ try expect(node.massive_byte == 100);
+ comptime try expect(@TypeOf(&node.massive_byte) == *align(64) u8);
+ try expect(@ptrToInt(&node.massive_byte) % 64 == 0);
}
test "align(@alignOf(T)) T does not force resolution of T" {
@@ -334,7 +334,7 @@ test "align(@alignOf(T)) T does not force resolution of T" {
var ok = false;
};
_ = async S.doTheTest();
- expect(S.ok);
+ try expect(S.ok);
}
test "align(N) on functions" {
@@ -342,7 +342,7 @@ test "align(N) on functions" {
if (builtin.arch == .wasm32 or builtin.arch == .wasm64) return error.SkipZigTest;
if (builtin.arch == .thumb) return error.SkipZigTest;
- expect((@ptrToInt(overaligned_fn) & (0x1000 - 1)) == 0);
+ try expect((@ptrToInt(overaligned_fn) & (0x1000 - 1)) == 0);
}
fn overaligned_fn() align(0x1000) i32 {
return 42;
test/stage1/behavior/alignof.zig
@@ -10,29 +10,29 @@ const Foo = struct {
};
test "@alignOf(T) before referencing T" {
- comptime expect(@alignOf(Foo) != maxInt(usize));
+ comptime try expect(@alignOf(Foo) != maxInt(usize));
if (builtin.arch == builtin.Arch.x86_64) {
- comptime expect(@alignOf(Foo) == 4);
+ comptime try expect(@alignOf(Foo) == 4);
}
}
test "comparison of @alignOf(T) against zero" {
{
const T = struct { x: u32 };
- expect(!(@alignOf(T) == 0));
- expect(@alignOf(T) != 0);
- expect(!(@alignOf(T) < 0));
- expect(!(@alignOf(T) <= 0));
- expect(@alignOf(T) > 0);
- expect(@alignOf(T) >= 0);
+ try expect(!(@alignOf(T) == 0));
+ try expect(@alignOf(T) != 0);
+ try expect(!(@alignOf(T) < 0));
+ try expect(!(@alignOf(T) <= 0));
+ try expect(@alignOf(T) > 0);
+ try expect(@alignOf(T) >= 0);
}
{
const T = struct {};
- expect(@alignOf(T) == 0);
- expect(!(@alignOf(T) != 0));
- expect(!(@alignOf(T) < 0));
- expect(@alignOf(T) <= 0);
- expect(!(@alignOf(T) > 0));
- expect(@alignOf(T) >= 0);
+ try expect(@alignOf(T) == 0);
+ try expect(!(@alignOf(T) != 0));
+ try expect(!(@alignOf(T) < 0));
+ try expect(@alignOf(T) <= 0);
+ try expect(!(@alignOf(T) > 0));
+ try expect(@alignOf(T) >= 0);
}
}
test/stage1/behavior/array.zig
@@ -21,8 +21,8 @@ test "arrays" {
i += 1;
}
- expect(accumulator == 15);
- expect(getArrayLen(&array) == 5);
+ try expect(accumulator == 15);
+ try expect(getArrayLen(&array) == 5);
}
fn getArrayLen(a: []const u32) usize {
return a.len;
@@ -30,37 +30,37 @@ fn getArrayLen(a: []const u32) usize {
test "array with sentinels" {
const S = struct {
- fn doTheTest(is_ct: bool) void {
+ fn doTheTest(is_ct: bool) !void {
if (is_ct) {
var zero_sized: [0:0xde]u8 = [_:0xde]u8{};
// Disabled at runtime because of
// https://github.com/ziglang/zig/issues/4372
- expectEqual(@as(u8, 0xde), zero_sized[0]);
+ try expectEqual(@as(u8, 0xde), zero_sized[0]);
var reinterpreted = @ptrCast(*[1]u8, &zero_sized);
- expectEqual(@as(u8, 0xde), reinterpreted[0]);
+ try expectEqual(@as(u8, 0xde), reinterpreted[0]);
}
var arr: [3:0x55]u8 = undefined;
// Make sure the sentinel pointer is pointing after the last element
if (!is_ct) {
const sentinel_ptr = @ptrToInt(&arr[3]);
const last_elem_ptr = @ptrToInt(&arr[2]);
- expectEqual(@as(usize, 1), sentinel_ptr - last_elem_ptr);
+ try expectEqual(@as(usize, 1), sentinel_ptr - last_elem_ptr);
}
// Make sure the sentinel is writeable
arr[3] = 0x55;
}
};
- S.doTheTest(false);
- comptime S.doTheTest(true);
+ try S.doTheTest(false);
+ comptime try S.doTheTest(true);
}
test "void arrays" {
var array: [4]void = undefined;
array[0] = void{};
array[1] = array[2];
- expect(@sizeOf(@TypeOf(array)) == 0);
- expect(array.len == 4);
+ try expect(@sizeOf(@TypeOf(array)) == 0);
+ try expect(array.len == 4);
}
test "array literal" {
@@ -71,12 +71,12 @@ test "array literal" {
1,
};
- expect(hex_mult.len == 4);
- expect(hex_mult[1] == 256);
+ try expect(hex_mult.len == 4);
+ try expect(hex_mult[1] == 256);
}
test "array dot len const expr" {
- expect(comptime x: {
+ try expect(comptime x: {
break :x some_array.len == 4;
});
}
@@ -100,11 +100,11 @@ test "nested arrays" {
"thing",
};
for (array_of_strings) |s, i| {
- if (i == 0) expect(mem.eql(u8, s, "hello"));
- if (i == 1) expect(mem.eql(u8, s, "this"));
- if (i == 2) expect(mem.eql(u8, s, "is"));
- if (i == 3) expect(mem.eql(u8, s, "my"));
- if (i == 4) expect(mem.eql(u8, s, "thing"));
+ if (i == 0) try expect(mem.eql(u8, s, "hello"));
+ if (i == 1) try expect(mem.eql(u8, s, "this"));
+ if (i == 2) try expect(mem.eql(u8, s, "is"));
+ if (i == 3) try expect(mem.eql(u8, s, "my"));
+ if (i == 4) try expect(mem.eql(u8, s, "thing"));
}
}
@@ -122,9 +122,9 @@ test "set global var array via slice embedded in struct" {
s.a[1].b = 2;
s.a[2].b = 3;
- expect(s_array[0].b == 1);
- expect(s_array[1].b == 2);
- expect(s_array[2].b == 3);
+ try expect(s_array[0].b == 1);
+ try expect(s_array[1].b == 2);
+ try expect(s_array[2].b == 3);
}
test "array literal with specified size" {
@@ -132,34 +132,34 @@ test "array literal with specified size" {
1,
2,
};
- expect(array[0] == 1);
- expect(array[1] == 2);
+ try expect(array[0] == 1);
+ try expect(array[1] == 2);
}
test "array len field" {
var arr = [4]u8{ 0, 0, 0, 0 };
var ptr = &arr;
- expect(arr.len == 4);
- comptime expect(arr.len == 4);
- expect(ptr.len == 4);
- comptime expect(ptr.len == 4);
+ try expect(arr.len == 4);
+ comptime try expect(arr.len == 4);
+ try expect(ptr.len == 4);
+ comptime try expect(ptr.len == 4);
}
test "single-item pointer to array indexing and slicing" {
- testSingleItemPtrArrayIndexSlice();
- comptime testSingleItemPtrArrayIndexSlice();
+ try testSingleItemPtrArrayIndexSlice();
+ comptime try testSingleItemPtrArrayIndexSlice();
}
-fn testSingleItemPtrArrayIndexSlice() void {
+fn testSingleItemPtrArrayIndexSlice() !void {
{
var array: [4]u8 = "aaaa".*;
doSomeMangling(&array);
- expect(mem.eql(u8, "azya", &array));
+ try expect(mem.eql(u8, "azya", &array));
}
{
var array = "aaaa".*;
doSomeMangling(&array);
- expect(mem.eql(u8, "azya", &array));
+ try expect(mem.eql(u8, "azya", &array));
}
}
@@ -169,15 +169,15 @@ fn doSomeMangling(array: *[4]u8) void {
}
test "implicit cast single-item pointer" {
- testImplicitCastSingleItemPtr();
- comptime testImplicitCastSingleItemPtr();
+ try testImplicitCastSingleItemPtr();
+ comptime try testImplicitCastSingleItemPtr();
}
-fn testImplicitCastSingleItemPtr() void {
+fn testImplicitCastSingleItemPtr() !void {
var byte: u8 = 100;
const slice = @as(*[1]u8, &byte)[0..];
slice[0] += 1;
- expect(byte == 101);
+ try expect(byte == 101);
}
fn testArrayByValAtComptime(b: [2]u8) u8 {
@@ -192,7 +192,7 @@ test "comptime evalutating function that takes array by value" {
test "implicit comptime in array type size" {
var arr: [plusOne(10)]bool = undefined;
- expect(arr.len == 11);
+ try expect(arr.len == 11);
}
fn plusOne(x: u32) u32 {
@@ -202,52 +202,52 @@ fn plusOne(x: u32) u32 {
test "runtime initialize array elem and then implicit cast to slice" {
var two: i32 = 2;
const x: []const i32 = &[_]i32{two};
- expect(x[0] == 2);
+ try expect(x[0] == 2);
}
test "array literal as argument to function" {
const S = struct {
- fn entry(two: i32) void {
- foo(&[_]i32{
+ fn entry(two: i32) !void {
+ try foo(&[_]i32{
1,
2,
3,
});
- foo(&[_]i32{
+ try foo(&[_]i32{
1,
two,
3,
});
- foo2(true, &[_]i32{
+ try foo2(true, &[_]i32{
1,
2,
3,
});
- foo2(true, &[_]i32{
+ try foo2(true, &[_]i32{
1,
two,
3,
});
}
- fn foo(x: []const i32) void {
- expect(x[0] == 1);
- expect(x[1] == 2);
- expect(x[2] == 3);
+ fn foo(x: []const i32) !void {
+ try expect(x[0] == 1);
+ try expect(x[1] == 2);
+ try expect(x[2] == 3);
}
- fn foo2(trash: bool, x: []const i32) void {
- expect(trash);
- expect(x[0] == 1);
- expect(x[1] == 2);
- expect(x[2] == 3);
+ fn foo2(trash: bool, x: []const i32) !void {
+ try expect(trash);
+ try expect(x[0] == 1);
+ try expect(x[1] == 2);
+ try expect(x[2] == 3);
}
};
- S.entry(2);
- comptime S.entry(2);
+ try S.entry(2);
+ comptime try S.entry(2);
}
test "double nested array to const slice cast in array literal" {
const S = struct {
- fn entry(two: i32) void {
+ fn entry(two: i32) !void {
const cases = [_][]const []const i32{
&[_][]const i32{&[_]i32{1}},
&[_][]const i32{&[_]i32{ 2, 3 }},
@@ -256,18 +256,18 @@ test "double nested array to const slice cast in array literal" {
&[_]i32{ 5, 6, 7 },
},
};
- check(&cases);
+ try check(&cases);
const cases2 = [_][]const i32{
&[_]i32{1},
&[_]i32{ two, 3 },
};
- expect(cases2.len == 2);
- expect(cases2[0].len == 1);
- expect(cases2[0][0] == 1);
- expect(cases2[1].len == 2);
- expect(cases2[1][0] == 2);
- expect(cases2[1][1] == 3);
+ try expect(cases2.len == 2);
+ try expect(cases2[0].len == 1);
+ try expect(cases2[0][0] == 1);
+ try expect(cases2[1].len == 2);
+ try expect(cases2[1][0] == 2);
+ try expect(cases2[1][1] == 3);
const cases3 = [_][]const []const i32{
&[_][]const i32{&[_]i32{1}},
@@ -277,37 +277,37 @@ test "double nested array to const slice cast in array literal" {
&[_]i32{ 5, 6, 7 },
},
};
- check(&cases3);
+ try check(&cases3);
}
- fn check(cases: []const []const []const i32) void {
- expect(cases.len == 3);
- expect(cases[0].len == 1);
- expect(cases[0][0].len == 1);
- expect(cases[0][0][0] == 1);
- expect(cases[1].len == 1);
- expect(cases[1][0].len == 2);
- expect(cases[1][0][0] == 2);
- expect(cases[1][0][1] == 3);
- expect(cases[2].len == 2);
- expect(cases[2][0].len == 1);
- expect(cases[2][0][0] == 4);
- expect(cases[2][1].len == 3);
- expect(cases[2][1][0] == 5);
- expect(cases[2][1][1] == 6);
- expect(cases[2][1][2] == 7);
+ fn check(cases: []const []const []const i32) !void {
+ try expect(cases.len == 3);
+ try expect(cases[0].len == 1);
+ try expect(cases[0][0].len == 1);
+ try expect(cases[0][0][0] == 1);
+ try expect(cases[1].len == 1);
+ try expect(cases[1][0].len == 2);
+ try expect(cases[1][0][0] == 2);
+ try expect(cases[1][0][1] == 3);
+ try expect(cases[2].len == 2);
+ try expect(cases[2][0].len == 1);
+ try expect(cases[2][0][0] == 4);
+ try expect(cases[2][1].len == 3);
+ try expect(cases[2][1][0] == 5);
+ try expect(cases[2][1][1] == 6);
+ try expect(cases[2][1][2] == 7);
}
};
- S.entry(2);
- comptime S.entry(2);
+ try S.entry(2);
+ comptime try S.entry(2);
}
test "read/write through global variable array of struct fields initialized via array mult" {
const S = struct {
- fn doTheTest() void {
- expect(storage[0].term == 1);
+ fn doTheTest() !void {
+ try expect(storage[0].term == 1);
storage[0] = MyStruct{ .term = 123 };
- expect(storage[0].term == 123);
+ try expect(storage[0].term == 123);
}
pub const MyStruct = struct {
@@ -316,34 +316,34 @@ test "read/write through global variable array of struct fields initialized via
var storage: [1]MyStruct = [_]MyStruct{MyStruct{ .term = 1 }} ** 1;
};
- S.doTheTest();
+ try S.doTheTest();
}
test "implicit cast zero sized array ptr to slice" {
{
var b = "".*;
const c: []const u8 = &b;
- expect(c.len == 0);
+ try expect(c.len == 0);
}
{
var b: [0]u8 = "".*;
const c: []const u8 = &b;
- expect(c.len == 0);
+ try expect(c.len == 0);
}
}
test "anonymous list literal syntax" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var array: [4]u8 = .{ 1, 2, 3, 4 };
- expect(array[0] == 1);
- expect(array[1] == 2);
- expect(array[2] == 3);
- expect(array[3] == 4);
+ try expect(array[0] == 1);
+ try expect(array[1] == 2);
+ try expect(array[2] == 3);
+ try expect(array[3] == 4);
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "anonymous literal in array" {
@@ -352,51 +352,51 @@ test "anonymous literal in array" {
a: usize = 2,
b: usize = 4,
};
- fn doTheTest() void {
+ fn doTheTest() !void {
var array: [2]Foo = .{
.{ .a = 3 },
.{ .b = 3 },
};
- expect(array[0].a == 3);
- expect(array[0].b == 4);
- expect(array[1].a == 2);
- expect(array[1].b == 3);
+ try expect(array[0].a == 3);
+ try expect(array[0].b == 4);
+ try expect(array[1].a == 2);
+ try expect(array[1].b == 3);
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "access the null element of a null terminated array" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var array: [4:0]u8 = .{ 'a', 'o', 'e', 'u' };
- expect(array[4] == 0);
+ try expect(array[4] == 0);
var len: usize = 4;
- expect(array[len] == 0);
+ try expect(array[len] == 0);
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "type deduction for array subscript expression" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var array = [_]u8{ 0x55, 0xAA };
var v0 = true;
- expectEqual(@as(u8, 0xAA), array[if (v0) 1 else 0]);
+ try expectEqual(@as(u8, 0xAA), array[if (v0) 1 else 0]);
var v1 = false;
- expectEqual(@as(u8, 0x55), array[if (v1) 1 else 0]);
+ try expectEqual(@as(u8, 0x55), array[if (v1) 1 else 0]);
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "sentinel element count towards the ABI size calculation" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
const T = packed struct {
fill_pre: u8 = 0x55,
data: [0:0]u8 = undefined,
@@ -404,14 +404,14 @@ test "sentinel element count towards the ABI size calculation" {
};
var x = T{};
var as_slice = mem.asBytes(&x);
- expectEqual(@as(usize, 3), as_slice.len);
- expectEqual(@as(u8, 0x55), as_slice[0]);
- expectEqual(@as(u8, 0xAA), as_slice[2]);
+ try expectEqual(@as(usize, 3), as_slice.len);
+ try expectEqual(@as(u8, 0x55), as_slice[0]);
+ try expectEqual(@as(u8, 0xAA), as_slice[2]);
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "zero-sized array with recursive type definition" {
@@ -429,61 +429,61 @@ test "zero-sized array with recursive type definition" {
};
var t: S = .{ .list = .{ .s = undefined } };
- expectEqual(@as(usize, 0), t.list.x);
+ try expectEqual(@as(usize, 0), t.list.x);
}
test "type coercion of anon struct literal to array" {
const S = struct {
- const U = union{
+ const U = union {
a: u32,
b: bool,
c: []const u8,
};
- fn doTheTest() void {
+ fn doTheTest() !void {
var x1: u8 = 42;
const t1 = .{ x1, 56, 54 };
var arr1: [3]u8 = t1;
- expect(arr1[0] == 42);
- expect(arr1[1] == 56);
- expect(arr1[2] == 54);
-
+ try expect(arr1[0] == 42);
+ try expect(arr1[1] == 56);
+ try expect(arr1[2] == 54);
+
var x2: U = .{ .a = 42 };
const t2 = .{ x2, .{ .b = true }, .{ .c = "hello" } };
var arr2: [3]U = t2;
- expect(arr2[0].a == 42);
- expect(arr2[1].b == true);
- expect(mem.eql(u8, arr2[2].c, "hello"));
+ try expect(arr2[0].a == 42);
+ try expect(arr2[1].b == true);
+ try expect(mem.eql(u8, arr2[2].c, "hello"));
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "type coercion of pointer to anon struct literal to pointer to array" {
const S = struct {
- const U = union{
+ const U = union {
a: u32,
b: bool,
c: []const u8,
};
- fn doTheTest() void {
+ fn doTheTest() !void {
var x1: u8 = 42;
const t1 = &.{ x1, 56, 54 };
- var arr1: *const[3]u8 = t1;
- expect(arr1[0] == 42);
- expect(arr1[1] == 56);
- expect(arr1[2] == 54);
-
+ var arr1: *const [3]u8 = t1;
+ try expect(arr1[0] == 42);
+ try expect(arr1[1] == 56);
+ try expect(arr1[2] == 54);
+
var x2: U = .{ .a = 42 };
const t2 = &.{ x2, .{ .b = true }, .{ .c = "hello" } };
var arr2: *const [3]U = t2;
- expect(arr2[0].a == 42);
- expect(arr2[1].b == true);
- expect(mem.eql(u8, arr2[2].c, "hello"));
+ try expect(arr2[0].a == 42);
+ try expect(arr2[1].b == true);
+ try expect(mem.eql(u8, arr2[2].c, "hello"));
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test/stage1/behavior/asm.zig
@@ -15,7 +15,7 @@ comptime {
test "module level assembly" {
if (is_x86_64_linux) {
- expect(this_is_my_alias() == 1234);
+ try expect(this_is_my_alias() == 1234);
}
}
test/stage1/behavior/async_fn.zig
@@ -9,12 +9,12 @@ var global_x: i32 = 1;
test "simple coroutine suspend and resume" {
var frame = async simpleAsyncFn();
- expect(global_x == 2);
+ try expect(global_x == 2);
resume frame;
- expect(global_x == 3);
+ try expect(global_x == 3);
const af: anyframe->void = &frame;
resume frame;
- expect(global_x == 4);
+ try expect(global_x == 4);
}
fn simpleAsyncFn() void {
global_x += 1;
@@ -28,9 +28,9 @@ var global_y: i32 = 1;
test "pass parameter to coroutine" {
var p = async simpleAsyncFnWithArg(2);
- expect(global_y == 3);
+ try expect(global_y == 3);
resume p;
- expect(global_y == 5);
+ try expect(global_y == 5);
}
fn simpleAsyncFnWithArg(delta: i32) void {
global_y += delta;
@@ -42,10 +42,10 @@ test "suspend at end of function" {
const S = struct {
var x: i32 = 1;
- fn doTheTest() void {
- expect(x == 1);
+ fn doTheTest() !void {
+ try expect(x == 1);
const p = async suspendAtEnd();
- expect(x == 2);
+ try expect(x == 2);
}
fn suspendAtEnd() void {
@@ -53,23 +53,23 @@ test "suspend at end of function" {
suspend {}
}
};
- S.doTheTest();
+ try S.doTheTest();
}
test "local variable in async function" {
const S = struct {
var x: i32 = 0;
- fn doTheTest() void {
- expect(x == 0);
+ fn doTheTest() !void {
+ try expect(x == 0);
var p = async add(1, 2);
- expect(x == 0);
+ try expect(x == 0);
resume p;
- expect(x == 0);
+ try expect(x == 0);
resume p;
- expect(x == 0);
+ try expect(x == 0);
resume p;
- expect(x == 3);
+ try expect(x == 3);
}
fn add(a: i32, b: i32) void {
@@ -82,7 +82,7 @@ test "local variable in async function" {
x = accum;
}
};
- S.doTheTest();
+ try S.doTheTest();
}
test "calling an inferred async function" {
@@ -90,11 +90,11 @@ test "calling an inferred async function" {
var x: i32 = 1;
var other_frame: *@Frame(other) = undefined;
- fn doTheTest() void {
+ fn doTheTest() !void {
_ = async first();
- expect(x == 1);
+ try expect(x == 1);
resume other_frame.*;
- expect(x == 2);
+ try expect(x == 2);
}
fn first() void {
@@ -106,7 +106,7 @@ test "calling an inferred async function" {
x += 1;
}
};
- S.doTheTest();
+ try S.doTheTest();
}
test "@frameSize" {
@@ -114,16 +114,16 @@ test "@frameSize" {
return error.SkipZigTest;
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
{
var ptr = @ptrCast(fn (i32) callconv(.Async) void, other);
const size = @frameSize(ptr);
- expect(size == @sizeOf(@Frame(other)));
+ try expect(size == @sizeOf(@Frame(other)));
}
{
var ptr = @ptrCast(fn () callconv(.Async) void, first);
const size = @frameSize(ptr);
- expect(size == @sizeOf(@Frame(first)));
+ try expect(size == @sizeOf(@Frame(first)));
}
}
@@ -135,20 +135,20 @@ test "@frameSize" {
suspend {}
}
};
- S.doTheTest();
+ try S.doTheTest();
}
test "coroutine suspend, resume" {
const S = struct {
var frame: anyframe = undefined;
- fn doTheTest() void {
+ fn doTheTest() !void {
_ = async amain();
seq('d');
resume frame;
seq('h');
- expect(std.mem.eql(u8, &points, "abcdefgh"));
+ try expect(std.mem.eql(u8, &points, "abcdefgh"));
}
fn amain() void {
@@ -176,27 +176,27 @@ test "coroutine suspend, resume" {
index += 1;
}
};
- S.doTheTest();
+ try S.doTheTest();
}
test "coroutine suspend with block" {
const p = async testSuspendBlock();
- expect(!global_result);
+ try expect(!global_result);
resume a_promise;
- expect(global_result);
+ try expect(global_result);
}
var a_promise: anyframe = undefined;
var global_result = false;
fn testSuspendBlock() callconv(.Async) void {
suspend {
- comptime expect(@TypeOf(@frame()) == *@Frame(testSuspendBlock));
+ comptime expect(@TypeOf(@frame()) == *@Frame(testSuspendBlock)) catch unreachable;
a_promise = @frame();
}
// Test to make sure that @frame() works as advertised (issue #1296)
// var our_handle: anyframe = @frame();
- expect(a_promise == @as(anyframe, @frame()));
+ expect(a_promise == @as(anyframe, @frame())) catch @panic("test failed");
global_result = true;
}
@@ -210,8 +210,8 @@ test "coroutine await" {
await_seq('f');
resume await_a_promise;
await_seq('i');
- expect(await_final_result == 1234);
- expect(std.mem.eql(u8, &await_points, "abcdefghi"));
+ try expect(await_final_result == 1234);
+ try expect(std.mem.eql(u8, &await_points, "abcdefghi"));
}
fn await_amain() callconv(.Async) void {
await_seq('b');
@@ -244,8 +244,8 @@ test "coroutine await early return" {
early_seq('a');
var p = async early_amain();
early_seq('f');
- expect(early_final_result == 1234);
- expect(std.mem.eql(u8, &early_points, "abcdef"));
+ try expect(early_final_result == 1234);
+ try expect(std.mem.eql(u8, &early_points, "abcdef"));
}
fn early_amain() callconv(.Async) void {
early_seq('b');
@@ -276,7 +276,7 @@ test "async function with dot syntax" {
}
};
const p = async S.foo();
- expect(S.y == 2);
+ try expect(S.y == 2);
}
test "async fn pointer in a struct field" {
@@ -287,12 +287,12 @@ test "async fn pointer in a struct field" {
var foo = Foo{ .bar = simpleAsyncFn2 };
var bytes: [64]u8 align(16) = undefined;
const f = @asyncCall(&bytes, {}, foo.bar, .{&data});
- comptime expect(@TypeOf(f) == anyframe->void);
- expect(data == 2);
+ comptime try expect(@TypeOf(f) == anyframe->void);
+ try expect(data == 2);
resume f;
- expect(data == 4);
+ try expect(data == 4);
_ = async doTheAwait(f);
- expect(data == 4);
+ try expect(data == 4);
}
fn doTheAwait(f: anyframe->void) void {
@@ -323,22 +323,22 @@ test "@asyncCall with return type" {
var bytes: [150]u8 align(16) = undefined;
var aresult: i32 = 0;
_ = @asyncCall(&bytes, &aresult, foo.bar, .{});
- expect(aresult == 0);
+ try expect(aresult == 0);
resume Foo.global_frame;
- expect(aresult == 1234);
+ try expect(aresult == 1234);
}
test "async fn with inferred error set" {
const S = struct {
var global_frame: anyframe = undefined;
- fn doTheTest() void {
+ fn doTheTest() !void {
var frame: [1]@Frame(middle) = undefined;
var fn_ptr = middle;
var result: @typeInfo(@typeInfo(@TypeOf(fn_ptr)).Fn.return_type.?).ErrorUnion.error_set!void = undefined;
_ = @asyncCall(std.mem.sliceAsBytes(frame[0..]), &result, fn_ptr, .{});
resume global_frame;
- std.testing.expectError(error.Fail, result);
+ try std.testing.expectError(error.Fail, result);
}
fn middle() callconv(.Async) !void {
var f = async middle2();
@@ -355,7 +355,7 @@ test "async fn with inferred error set" {
return error.Fail;
}
};
- S.doTheTest();
+ try S.doTheTest();
}
test "error return trace across suspend points - early return" {
@@ -383,9 +383,9 @@ fn suspendThenFail() callconv(.Async) anyerror!void {
}
fn printTrace(p: anyframe->(anyerror!void)) callconv(.Async) void {
(await p) catch |e| {
- std.testing.expect(e == error.Fail);
+ std.testing.expect(e == error.Fail) catch @panic("test failure");
if (@errorReturnTrace()) |trace| {
- expect(trace.index == 1);
+ expect(trace.index == 1) catch @panic("test failure");
} else switch (builtin.mode) {
.Debug, .ReleaseSafe => @panic("expected return trace"),
.ReleaseFast, .ReleaseSmall => {},
@@ -396,7 +396,7 @@ fn printTrace(p: anyframe->(anyerror!void)) callconv(.Async) void {
test "break from suspend" {
var my_result: i32 = 1;
const p = async testBreakFromSuspend(&my_result);
- std.testing.expect(my_result == 2);
+ try std.testing.expect(my_result == 2);
}
fn testBreakFromSuspend(my_result: *i32) callconv(.Async) void {
suspend {
@@ -415,11 +415,11 @@ test "heap allocated async function frame" {
const frame = try std.testing.allocator.create(@Frame(someFunc));
defer std.testing.allocator.destroy(frame);
- expect(x == 42);
+ try expect(x == 42);
frame.* = async someFunc();
- expect(x == 43);
+ try expect(x == 43);
resume frame;
- expect(x == 44);
+ try expect(x == 44);
}
fn someFunc() void {
@@ -436,15 +436,15 @@ test "async function call return value" {
var frame: anyframe = undefined;
var pt = Point{ .x = 10, .y = 11 };
- fn doTheTest() void {
- expectEqual(pt.x, 10);
- expectEqual(pt.y, 11);
+ fn doTheTest() !void {
+ try expectEqual(pt.x, 10);
+ try expectEqual(pt.y, 11);
_ = async first();
- expectEqual(pt.x, 10);
- expectEqual(pt.y, 11);
+ try expectEqual(pt.x, 10);
+ try expectEqual(pt.y, 11);
resume frame;
- expectEqual(pt.x, 1);
- expectEqual(pt.y, 2);
+ try expectEqual(pt.x, 1);
+ try expectEqual(pt.y, 2);
}
fn first() void {
@@ -469,23 +469,23 @@ test "async function call return value" {
y: i32,
};
};
- S.doTheTest();
+ try S.doTheTest();
}
test "suspension points inside branching control flow" {
const S = struct {
var result: i32 = 10;
- fn doTheTest() void {
- expect(10 == result);
+ fn doTheTest() !void {
+ try expect(10 == result);
var frame = async func(true);
- expect(10 == result);
+ try expect(10 == result);
resume frame;
- expect(11 == result);
+ try expect(11 == result);
resume frame;
- expect(12 == result);
+ try expect(12 == result);
resume frame;
- expect(13 == result);
+ try expect(13 == result);
}
fn func(b: bool) void {
@@ -495,7 +495,7 @@ test "suspension points inside branching control flow" {
}
}
};
- S.doTheTest();
+ try S.doTheTest();
}
test "call async function which has struct return type" {
@@ -509,8 +509,8 @@ test "call async function which has struct return type" {
fn atest() void {
const result = func();
- expect(result.x == 5);
- expect(result.y == 6);
+ expect(result.x == 5) catch @panic("test failed");
+ expect(result.y == 6) catch @panic("test failed");
}
const Point = struct {
@@ -536,27 +536,27 @@ test "pass string literal to async function" {
var frame: anyframe = undefined;
var ok: bool = false;
- fn doTheTest() void {
+ fn doTheTest() !void {
_ = async hello("hello");
resume frame;
- expect(ok);
+ try expect(ok);
}
fn hello(msg: []const u8) void {
frame = @frame();
suspend {}
- expectEqualStrings("hello", msg);
+ expectEqualStrings("hello", msg) catch @panic("test failed");
ok = true;
}
};
- S.doTheTest();
+ try S.doTheTest();
}
test "await inside an errdefer" {
const S = struct {
var frame: anyframe = undefined;
- fn doTheTest() void {
+ fn doTheTest() !void {
_ = async amainWrap();
resume frame;
}
@@ -572,7 +572,7 @@ test "await inside an errdefer" {
suspend {}
}
};
- S.doTheTest();
+ try S.doTheTest();
}
test "try in an async function with error union and non-zero-bit payload" {
@@ -580,14 +580,14 @@ test "try in an async function with error union and non-zero-bit payload" {
var frame: anyframe = undefined;
var ok = false;
- fn doTheTest() void {
+ fn doTheTest() !void {
_ = async amain();
resume frame;
- expect(ok);
+ try expect(ok);
}
fn amain() void {
- std.testing.expectError(error.Bad, theProblem());
+ std.testing.expectError(error.Bad, theProblem()) catch @panic("test failed");
ok = true;
}
@@ -602,7 +602,7 @@ test "try in an async function with error union and non-zero-bit payload" {
return error.Bad;
}
};
- S.doTheTest();
+ try S.doTheTest();
}
test "returning a const error from async function" {
@@ -610,10 +610,10 @@ test "returning a const error from async function" {
var frame: anyframe = undefined;
var ok = false;
- fn doTheTest() void {
+ fn doTheTest() !void {
_ = async amain();
resume frame;
- expect(ok);
+ try expect(ok);
}
fn amain() !void {
@@ -630,7 +630,7 @@ test "returning a const error from async function" {
return error.OutOfMemory;
}
};
- S.doTheTest();
+ try S.doTheTest();
}
test "async/await typical usage" {
@@ -663,11 +663,11 @@ fn testAsyncAwaitTypicalUsage(
}
fn amainWrap() void {
if (amain()) |_| {
- expect(!simulate_fail_download);
- expect(!simulate_fail_file);
+ expect(!simulate_fail_download) catch @panic("test failure");
+ expect(!simulate_fail_file) catch @panic("test failure");
} else |e| switch (e) {
- error.NoResponse => expect(simulate_fail_download),
- error.FileNotFound => expect(simulate_fail_file),
+ error.NoResponse => expect(simulate_fail_download) catch @panic("test failure"),
+ error.FileNotFound => expect(simulate_fail_file) catch @panic("test failure"),
else => @panic("test failure"),
}
}
@@ -694,8 +694,8 @@ fn testAsyncAwaitTypicalUsage(
const file_text = try await file_frame;
defer allocator.free(file_text);
- expect(std.mem.eql(u8, "expected download text", download_text));
- expect(std.mem.eql(u8, "expected file text", file_text));
+ try expect(std.mem.eql(u8, "expected download text", download_text));
+ try expect(std.mem.eql(u8, "expected file text", file_text));
}
var global_download_frame: anyframe = undefined;
@@ -728,13 +728,13 @@ fn testAsyncAwaitTypicalUsage(
test "alignment of local variables in async functions" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var y: u8 = 123;
var x: u8 align(128) = 1;
- expect(@ptrToInt(&x) % 128 == 0);
+ try expect(@ptrToInt(&x) % 128 == 0);
}
};
- S.doTheTest();
+ try S.doTheTest();
}
test "no reason to resolve frame still works" {
@@ -746,10 +746,10 @@ fn simpleNothing() void {
test "async call a generic function" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var f = async func(i32, 2);
const result = await f;
- expect(result == 3);
+ try expect(result == 3);
}
fn func(comptime T: type, inc: T) T {
@@ -766,8 +766,8 @@ test "async call a generic function" {
test "return from suspend block" {
const S = struct {
- fn doTheTest() void {
- expect(func() == 1234);
+ fn doTheTest() !void {
+ expect(func() == 1234) catch @panic("test failure");
}
fn func() i32 {
suspend {
@@ -808,7 +808,7 @@ test "struct parameter to async function is copied to the frame" {
var pt = Point{ .x = 1, .y = 2 };
f.* = async foo(pt);
var result = await f;
- expect(result == 1);
+ expect(result == 1) catch @panic("test failure");
}
fn foo(point: Point) i32 {
@@ -833,7 +833,7 @@ test "cast fn to async fn when it is inferred to be async" {
var result: i32 = undefined;
const f = @asyncCall(&buf, &result, ptr, .{});
_ = await f;
- expect(result == 1234);
+ expect(result == 1234) catch @panic("test failure");
ok = true;
}
@@ -846,7 +846,7 @@ test "cast fn to async fn when it is inferred to be async" {
};
_ = async S.doTheTest();
resume S.frame;
- expect(S.ok);
+ try expect(S.ok);
}
test "cast fn to async fn when it is inferred to be async, awaited directly" {
@@ -860,7 +860,7 @@ test "cast fn to async fn when it is inferred to be async, awaited directly" {
var buf: [100]u8 align(16) = undefined;
var result: i32 = undefined;
_ = await @asyncCall(&buf, &result, ptr, .{});
- expect(result == 1234);
+ expect(result == 1234) catch @panic("test failure");
ok = true;
}
@@ -873,7 +873,7 @@ test "cast fn to async fn when it is inferred to be async, awaited directly" {
};
_ = async S.doTheTest();
resume S.frame;
- expect(S.ok);
+ try expect(S.ok);
}
test "await does not force async if callee is blocking" {
@@ -883,12 +883,12 @@ test "await does not force async if callee is blocking" {
}
};
var x = async S.simple();
- expect(await x == 1234);
+ try expect(await x == 1234);
}
test "recursive async function" {
- expect(recursiveAsyncFunctionTest(false).doTheTest() == 55);
- expect(recursiveAsyncFunctionTest(true).doTheTest() == 55);
+ try expect(recursiveAsyncFunctionTest(false).doTheTest() == 55);
+ try expect(recursiveAsyncFunctionTest(true).doTheTest() == 55);
}
fn recursiveAsyncFunctionTest(comptime suspending_implementation: bool) type {
@@ -952,12 +952,12 @@ test "@asyncCall with comptime-known function, but not awaited directly" {
const S = struct {
var global_frame: anyframe = undefined;
- fn doTheTest() void {
+ fn doTheTest() !void {
var frame: [1]@Frame(middle) = undefined;
var result: @typeInfo(@typeInfo(@TypeOf(middle)).Fn.return_type.?).ErrorUnion.error_set!void = undefined;
_ = @asyncCall(std.mem.sliceAsBytes(frame[0..]), &result, middle, .{});
resume global_frame;
- std.testing.expectError(error.Fail, result);
+ try std.testing.expectError(error.Fail, result);
}
fn middle() callconv(.Async) !void {
var f = async middle2();
@@ -974,7 +974,7 @@ test "@asyncCall with comptime-known function, but not awaited directly" {
return error.Fail;
}
};
- S.doTheTest();
+ try S.doTheTest();
}
test "@asyncCall with actual frame instead of byte buffer" {
@@ -988,7 +988,7 @@ test "@asyncCall with actual frame instead of byte buffer" {
var result: i32 = undefined;
const ptr = @asyncCall(&frame, &result, S.func, .{});
resume ptr;
- expect(result == 1234);
+ try expect(result == 1234);
}
test "@asyncCall using the result location inside the frame" {
@@ -1010,19 +1010,19 @@ test "@asyncCall using the result location inside the frame" {
var foo = Foo{ .bar = S.simple2 };
var bytes: [64]u8 align(16) = undefined;
const f = @asyncCall(&bytes, {}, foo.bar, .{&data});
- comptime expect(@TypeOf(f) == anyframe->i32);
- expect(data == 2);
+ comptime try expect(@TypeOf(f) == anyframe->i32);
+ try expect(data == 2);
resume f;
- expect(data == 4);
+ try expect(data == 4);
_ = async S.getAnswer(f, &data);
- expect(data == 1234);
+ try expect(data == 1234);
}
test "@TypeOf an async function call of generic fn with error union type" {
const S = struct {
fn func(comptime x: anytype) anyerror!i32 {
const T = @TypeOf(async func(x));
- comptime expect(T == @typeInfo(@TypeOf(@frame())).Pointer.child);
+ comptime try expect(T == @typeInfo(@TypeOf(@frame())).Pointer.child);
return undefined;
}
};
@@ -1051,7 +1051,7 @@ test "using @TypeOf on a generic function call" {
};
_ = async S.amain(@as(u32, 1));
resume S.global_frame;
- expect(S.global_ok);
+ try expect(S.global_ok);
}
test "recursive call of await @asyncCall with struct return type" {
@@ -1084,17 +1084,17 @@ test "recursive call of await @asyncCall with struct return type" {
var frame: @TypeOf(async S.amain(@as(u32, 1))) = undefined;
_ = @asyncCall(&frame, &res, S.amain, .{@as(u32, 1)});
resume S.global_frame;
- expect(S.global_ok);
- expect(res.x == 1);
- expect(res.y == 2);
- expect(res.z == 3);
+ try expect(S.global_ok);
+ try expect(res.x == 1);
+ try expect(res.y == 2);
+ try expect(res.z == 3);
}
test "nosuspend function call" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
const result = nosuspend add(50, 100);
- expect(result == 150);
+ try expect(result == 150);
}
fn add(a: i32, b: i32) i32 {
if (a > 100) {
@@ -1103,7 +1103,7 @@ test "nosuspend function call" {
return a + b;
}
};
- S.doTheTest();
+ try S.doTheTest();
}
test "await used in expression and awaiting fn with no suspend but async calling convention" {
@@ -1113,7 +1113,7 @@ test "await used in expression and awaiting fn with no suspend but async calling
var f2 = async add(3, 4);
const sum = (await f1) + (await f2);
- expect(sum == 10);
+ expect(sum == 10) catch @panic("test failure");
}
fn add(a: i32, b: i32) callconv(.Async) i32 {
return a + b;
@@ -1128,7 +1128,7 @@ test "await used in expression after a fn call" {
var f1 = async add(3, 4);
var sum: i32 = 0;
sum = foo() + await f1;
- expect(sum == 8);
+ expect(sum == 8) catch @panic("test failure");
}
fn add(a: i32, b: i32) callconv(.Async) i32 {
return a + b;
@@ -1145,7 +1145,7 @@ test "async fn call used in expression after a fn call" {
fn atest() void {
var sum: i32 = 0;
sum = foo() + add(3, 4);
- expect(sum == 8);
+ expect(sum == 8) catch @panic("test failure");
}
fn add(a: i32, b: i32) callconv(.Async) i32 {
return a + b;
@@ -1167,7 +1167,7 @@ test "suspend in for loop" {
}
fn atest() void {
- expect(func(&[_]u8{ 1, 2, 3 }) == 6);
+ expect(func(&[_]u8{ 1, 2, 3 }) == 6) catch @panic("test failure");
}
fn func(stuff: []const u8) u32 {
global_frame = @frame();
@@ -1193,8 +1193,8 @@ test "suspend in while loop" {
}
fn atest() void {
- expect(optional(6) == 6);
- expect(errunion(6) == 6);
+ expect(optional(6) == 6) catch @panic("test failure");
+ expect(errunion(6) == 6) catch @panic("test failure");
}
fn optional(stuff: ?u32) u32 {
global_frame = @frame();
@@ -1223,8 +1223,8 @@ test "correctly spill when returning the error union result of another async fn"
const S = struct {
var global_frame: anyframe = undefined;
- fn doTheTest() void {
- expect((atest() catch unreachable) == 1234);
+ fn doTheTest() !void {
+ expect((atest() catch unreachable) == 1234) catch @panic("test failure");
}
fn atest() !i32 {
@@ -1246,11 +1246,11 @@ test "spill target expr in a for loop" {
const S = struct {
var global_frame: anyframe = undefined;
- fn doTheTest() void {
+ fn doTheTest() !void {
var foo = Foo{
.slice = &[_]i32{ 1, 2 },
};
- expect(atest(&foo) == 3);
+ expect(atest(&foo) == 3) catch @panic("test failure");
}
const Foo = struct {
@@ -1277,11 +1277,11 @@ test "spill target expr in a for loop, with a var decl in the loop body" {
const S = struct {
var global_frame: anyframe = undefined;
- fn doTheTest() void {
+ fn doTheTest() !void {
var foo = Foo{
.slice = &[_]i32{ 1, 2 },
};
- expect(atest(&foo) == 3);
+ expect(atest(&foo) == 3) catch @panic("test failure");
}
const Foo = struct {
@@ -1319,7 +1319,7 @@ test "async call with @call" {
fn atest() void {
var frame = @call(.{ .modifier = .async_kw }, afoo, .{});
const res = await frame;
- expect(res == 42);
+ expect(res == 42) catch @panic("test failure");
}
fn afoo() i32 {
suspend {
@@ -1348,7 +1348,7 @@ test "async function passed 0-bit arg after non-0-bit arg" {
};
_ = async S.foo();
resume S.global_frame;
- expect(S.global_int == 1);
+ try expect(S.global_int == 1);
}
test "async function passed align(16) arg after align(8) arg" {
@@ -1362,7 +1362,7 @@ test "async function passed align(16) arg after align(8) arg" {
}
fn bar(x: u64, args: anytype) anyerror!void {
- expect(x == 10);
+ try expect(x == 10);
global_frame = @frame();
suspend {}
global_int = args[0];
@@ -1370,7 +1370,7 @@ test "async function passed align(16) arg after align(8) arg" {
};
_ = async S.foo();
resume S.global_frame;
- expect(S.global_int == 99);
+ try expect(S.global_int == 99);
}
test "async function call resolves target fn frame, comptime func" {
@@ -1392,7 +1392,7 @@ test "async function call resolves target fn frame, comptime func" {
};
_ = async S.foo();
resume S.global_frame;
- expect(S.global_int == 10);
+ try expect(S.global_int == 10);
}
test "async function call resolves target fn frame, runtime func" {
@@ -1415,7 +1415,7 @@ test "async function call resolves target fn frame, runtime func" {
};
_ = async S.foo();
resume S.global_frame;
- expect(S.global_int == 10);
+ try expect(S.global_int == 10);
}
test "properly spill optional payload capture value" {
@@ -1439,7 +1439,7 @@ test "properly spill optional payload capture value" {
};
_ = async S.foo();
resume S.global_frame;
- expect(S.global_int == 1237);
+ try expect(S.global_int == 1237);
}
test "handle defer interfering with return value spill" {
@@ -1449,16 +1449,16 @@ test "handle defer interfering with return value spill" {
var finished = false;
var baz_happened = false;
- fn doTheTest() void {
+ fn doTheTest() !void {
_ = async testFoo();
resume global_frame1;
resume global_frame2;
- expect(baz_happened);
- expect(finished);
+ try expect(baz_happened);
+ try expect(finished);
}
fn testFoo() void {
- expectError(error.Bad, foo());
+ expectError(error.Bad, foo()) catch @panic("test failure");
finished = true;
}
@@ -1479,7 +1479,7 @@ test "handle defer interfering with return value spill" {
baz_happened = true;
}
};
- S.doTheTest();
+ try S.doTheTest();
}
test "take address of temporary async frame" {
@@ -1487,14 +1487,14 @@ test "take address of temporary async frame" {
var global_frame: anyframe = undefined;
var finished = false;
- fn doTheTest() void {
+ fn doTheTest() !void {
_ = async asyncDoTheTest();
resume global_frame;
- expect(finished);
+ try expect(finished);
}
fn asyncDoTheTest() void {
- expect(finishIt(&async foo(10)) == 1245);
+ expect(finishIt(&async foo(10)) == 1245) catch @panic("test failure");
finished = true;
}
@@ -1508,16 +1508,16 @@ test "take address of temporary async frame" {
return (await frame) + 1;
}
};
- S.doTheTest();
+ try S.doTheTest();
}
test "nosuspend await" {
const S = struct {
var finished = false;
- fn doTheTest() void {
+ fn doTheTest() !void {
var frame = async foo(false);
- expect(nosuspend await frame == 42);
+ try expect(nosuspend await frame == 42);
finished = true;
}
@@ -1528,8 +1528,8 @@ test "nosuspend await" {
return 42;
}
};
- S.doTheTest();
- expect(S.finished);
+ try S.doTheTest();
+ try expect(S.finished);
}
test "nosuspend on function calls" {
@@ -1544,8 +1544,8 @@ test "nosuspend on function calls" {
return S0{};
}
};
- expectEqual(@as(i32, 42), nosuspend S1.c().b);
- expectEqual(@as(i32, 42), (try nosuspend S1.d()).b);
+ try expectEqual(@as(i32, 42), nosuspend S1.c().b);
+ try expectEqual(@as(i32, 42), (try nosuspend S1.d()).b);
}
test "nosuspend on async function calls" {
@@ -1561,9 +1561,9 @@ test "nosuspend on async function calls" {
}
};
var frame_c = nosuspend async S1.c();
- expectEqual(@as(i32, 42), (await frame_c).b);
+ try expectEqual(@as(i32, 42), (await frame_c).b);
var frame_d = nosuspend async S1.d();
- expectEqual(@as(i32, 42), (try await frame_d).b);
+ try expectEqual(@as(i32, 42), (try await frame_d).b);
}
// test "resume nosuspend async function calls" {
@@ -1582,10 +1582,10 @@ test "nosuspend on async function calls" {
// };
// var frame_c = nosuspend async S1.c();
// resume frame_c;
-// expectEqual(@as(i32, 42), (await frame_c).b);
+// try expectEqual(@as(i32, 42), (await frame_c).b);
// var frame_d = nosuspend async S1.d();
// resume frame_d;
-// expectEqual(@as(i32, 42), (try await frame_d).b);
+// try expectEqual(@as(i32, 42), (try await frame_d).b);
// }
test "nosuspend resume async function calls" {
@@ -1604,10 +1604,10 @@ test "nosuspend resume async function calls" {
};
var frame_c = async S1.c();
nosuspend resume frame_c;
- expectEqual(@as(i32, 42), (await frame_c).b);
+ try expectEqual(@as(i32, 42), (await frame_c).b);
var frame_d = async S1.d();
nosuspend resume frame_d;
- expectEqual(@as(i32, 42), (try await frame_d).b);
+ try expectEqual(@as(i32, 42), (try await frame_d).b);
}
test "avoid forcing frame alignment resolution implicit cast to *c_void" {
@@ -1623,7 +1623,7 @@ test "avoid forcing frame alignment resolution implicit cast to *c_void" {
};
var frame = async S.foo();
resume @ptrCast(anyframe->bool, @alignCast(@alignOf(@Frame(S.foo)), S.x));
- expect(nosuspend await frame);
+ try expect(nosuspend await frame);
}
test "@asyncCall with pass-by-value arguments" {
@@ -1638,9 +1638,9 @@ test "@asyncCall with pass-by-value arguments" {
pub fn f(_fill0: u64, s: ST, _fill1: u64, a: AT, _fill2: u64) callconv(.Async) void {
// Check that the array and struct arguments passed by value don't
// end up overflowing the adjacent fields in the frame structure.
- expectEqual(F0, _fill0);
- expectEqual(F1, _fill1);
- expectEqual(F2, _fill2);
+ expectEqual(F0, _fill0) catch @panic("test failure");
+ expectEqual(F1, _fill1) catch @panic("test failure");
+ expectEqual(F2, _fill2) catch @panic("test failure");
}
};
@@ -1664,8 +1664,8 @@ test "@asyncCall with arguments having non-standard alignment" {
pub fn f(_fill0: u32, s: struct { x: u64 align(16) }, _fill1: u64) callconv(.Async) void {
// The compiler inserts extra alignment for s, check that the
// generated code picks the right slot for fill1.
- expectEqual(F0, _fill0);
- expectEqual(F1, _fill1);
+ expectEqual(F0, _fill0) catch @panic("test failure");
+ expectEqual(F1, _fill1) catch @panic("test failure");
}
};
test/stage1/behavior/atomics.zig
@@ -4,25 +4,25 @@ const expectEqual = std.testing.expectEqual;
const builtin = @import("builtin");
test "cmpxchg" {
- testCmpxchg();
- comptime testCmpxchg();
+ try testCmpxchg();
+ comptime try testCmpxchg();
}
-fn testCmpxchg() void {
+fn testCmpxchg() !void {
var x: i32 = 1234;
if (@cmpxchgWeak(i32, &x, 99, 5678, .SeqCst, .SeqCst)) |x1| {
- expect(x1 == 1234);
+ try expect(x1 == 1234);
} else {
@panic("cmpxchg should have failed");
}
while (@cmpxchgWeak(i32, &x, 1234, 5678, .SeqCst, .SeqCst)) |x1| {
- expect(x1 == 1234);
+ try expect(x1 == 1234);
}
- expect(x == 5678);
+ try expect(x == 5678);
- expect(@cmpxchgStrong(i32, &x, 5678, 42, .SeqCst, .SeqCst) == null);
- expect(x == 42);
+ try expect(@cmpxchgStrong(i32, &x, 5678, 42, .SeqCst, .SeqCst) == null);
+ try expect(x == 42);
}
test "fence" {
@@ -33,25 +33,25 @@ test "fence" {
test "atomicrmw and atomicload" {
var data: u8 = 200;
- testAtomicRmw(&data);
- expect(data == 42);
- testAtomicLoad(&data);
+ try testAtomicRmw(&data);
+ try expect(data == 42);
+ try testAtomicLoad(&data);
}
-fn testAtomicRmw(ptr: *u8) void {
+fn testAtomicRmw(ptr: *u8) !void {
const prev_value = @atomicRmw(u8, ptr, .Xchg, 42, .SeqCst);
- expect(prev_value == 200);
+ try expect(prev_value == 200);
comptime {
var x: i32 = 1234;
const y: i32 = 12345;
- expect(@atomicLoad(i32, &x, .SeqCst) == 1234);
- expect(@atomicLoad(i32, &y, .SeqCst) == 12345);
+ try expect(@atomicLoad(i32, &x, .SeqCst) == 1234);
+ try expect(@atomicLoad(i32, &y, .SeqCst) == 12345);
}
}
-fn testAtomicLoad(ptr: *u8) void {
+fn testAtomicLoad(ptr: *u8) !void {
const x = @atomicLoad(u8, ptr, .SeqCst);
- expect(x == 42);
+ try expect(x == 42);
}
test "cmpxchg with ptr" {
@@ -60,18 +60,18 @@ test "cmpxchg with ptr" {
var data3: i32 = 9101;
var x: *i32 = &data1;
if (@cmpxchgWeak(*i32, &x, &data2, &data3, .SeqCst, .SeqCst)) |x1| {
- expect(x1 == &data1);
+ try expect(x1 == &data1);
} else {
@panic("cmpxchg should have failed");
}
while (@cmpxchgWeak(*i32, &x, &data1, &data3, .SeqCst, .SeqCst)) |x1| {
- expect(x1 == &data1);
+ try expect(x1 == &data1);
}
- expect(x == &data3);
+ try expect(x == &data3);
- expect(@cmpxchgStrong(*i32, &x, &data3, &data2, .SeqCst, .SeqCst) == null);
- expect(x == &data2);
+ try expect(@cmpxchgStrong(*i32, &x, &data3, &data2, .SeqCst, .SeqCst) == null);
+ try expect(x == &data2);
}
// TODO this test is disabled until this issue is resolved:
@@ -81,18 +81,18 @@ test "cmpxchg with ptr" {
//test "128-bit cmpxchg" {
// var x: u128 align(16) = 1234; // TODO: https://github.com/ziglang/zig/issues/2987
// if (@cmpxchgWeak(u128, &x, 99, 5678, .SeqCst, .SeqCst)) |x1| {
-// expect(x1 == 1234);
+// try expect(x1 == 1234);
// } else {
// @panic("cmpxchg should have failed");
// }
//
// while (@cmpxchgWeak(u128, &x, 1234, 5678, .SeqCst, .SeqCst)) |x1| {
-// expect(x1 == 1234);
+// try expect(x1 == 1234);
// }
-// expect(x == 5678);
+// try expect(x == 5678);
//
-// expect(@cmpxchgStrong(u128, &x, 5678, 42, .SeqCst, .SeqCst) == null);
-// expect(x == 42);
+// try expect(@cmpxchgStrong(u128, &x, 5678, 42, .SeqCst, .SeqCst) == null);
+// try expect(x == 42);
//}
test "cmpxchg with ignored result" {
@@ -101,14 +101,14 @@ test "cmpxchg with ignored result" {
_ = @cmpxchgStrong(i32, &x, 1234, 5678, .Monotonic, .Monotonic);
- expectEqual(@as(i32, 5678), x);
+ try expectEqual(@as(i32, 5678), x);
}
var a_global_variable = @as(u32, 1234);
test "cmpxchg on a global variable" {
_ = @cmpxchgWeak(u32, &a_global_variable, 1234, 42, .Acquire, .Monotonic);
- expectEqual(@as(u32, 42), a_global_variable);
+ try expectEqual(@as(u32, 42), a_global_variable);
}
test "atomic load and rmw with enum" {
@@ -119,33 +119,33 @@ test "atomic load and rmw with enum" {
};
var x = Value.a;
- expect(@atomicLoad(Value, &x, .SeqCst) != .b);
+ try expect(@atomicLoad(Value, &x, .SeqCst) != .b);
_ = @atomicRmw(Value, &x, .Xchg, .c, .SeqCst);
- expect(@atomicLoad(Value, &x, .SeqCst) == .c);
- expect(@atomicLoad(Value, &x, .SeqCst) != .a);
- expect(@atomicLoad(Value, &x, .SeqCst) != .b);
+ try expect(@atomicLoad(Value, &x, .SeqCst) == .c);
+ try expect(@atomicLoad(Value, &x, .SeqCst) != .a);
+ try expect(@atomicLoad(Value, &x, .SeqCst) != .b);
}
test "atomic store" {
var x: u32 = 0;
@atomicStore(u32, &x, 1, .SeqCst);
- expect(@atomicLoad(u32, &x, .SeqCst) == 1);
+ try expect(@atomicLoad(u32, &x, .SeqCst) == 1);
@atomicStore(u32, &x, 12345678, .SeqCst);
- expect(@atomicLoad(u32, &x, .SeqCst) == 12345678);
+ try expect(@atomicLoad(u32, &x, .SeqCst) == 12345678);
}
test "atomic store comptime" {
- comptime testAtomicStore();
- testAtomicStore();
+ comptime try testAtomicStore();
+ try testAtomicStore();
}
-fn testAtomicStore() void {
+fn testAtomicStore() !void {
var x: u32 = 0;
@atomicStore(u32, &x, 1, .SeqCst);
- expect(@atomicLoad(u32, &x, .SeqCst) == 1);
+ try expect(@atomicLoad(u32, &x, .SeqCst) == 1);
@atomicStore(u32, &x, 12345678, .SeqCst);
- expect(@atomicLoad(u32, &x, .SeqCst) == 12345678);
+ try expect(@atomicLoad(u32, &x, .SeqCst) == 12345678);
}
test "atomicrmw with floats" {
@@ -154,66 +154,66 @@ test "atomicrmw with floats" {
.aarch64, .arm, .thumb, .riscv64 => return error.SkipZigTest,
else => {},
}
- testAtomicRmwFloat();
- comptime testAtomicRmwFloat();
+ try testAtomicRmwFloat();
+ comptime try testAtomicRmwFloat();
}
-fn testAtomicRmwFloat() void {
+fn testAtomicRmwFloat() !void {
var x: f32 = 0;
- expect(x == 0);
+ try expect(x == 0);
_ = @atomicRmw(f32, &x, .Xchg, 1, .SeqCst);
- expect(x == 1);
+ try expect(x == 1);
_ = @atomicRmw(f32, &x, .Add, 5, .SeqCst);
- expect(x == 6);
+ try expect(x == 6);
_ = @atomicRmw(f32, &x, .Sub, 2, .SeqCst);
- expect(x == 4);
+ try expect(x == 4);
}
test "atomicrmw with ints" {
- testAtomicRmwInt();
- comptime testAtomicRmwInt();
+ try testAtomicRmwInt();
+ comptime try testAtomicRmwInt();
}
-fn testAtomicRmwInt() void {
+fn testAtomicRmwInt() !void {
var x: u8 = 1;
var res = @atomicRmw(u8, &x, .Xchg, 3, .SeqCst);
- expect(x == 3 and res == 1);
+ try expect(x == 3 and res == 1);
_ = @atomicRmw(u8, &x, .Add, 3, .SeqCst);
- expect(x == 6);
+ try expect(x == 6);
_ = @atomicRmw(u8, &x, .Sub, 1, .SeqCst);
- expect(x == 5);
+ try expect(x == 5);
_ = @atomicRmw(u8, &x, .And, 4, .SeqCst);
- expect(x == 4);
+ try expect(x == 4);
_ = @atomicRmw(u8, &x, .Nand, 4, .SeqCst);
- expect(x == 0xfb);
+ try expect(x == 0xfb);
_ = @atomicRmw(u8, &x, .Or, 6, .SeqCst);
- expect(x == 0xff);
+ try expect(x == 0xff);
_ = @atomicRmw(u8, &x, .Xor, 2, .SeqCst);
- expect(x == 0xfd);
+ try expect(x == 0xfd);
_ = @atomicRmw(u8, &x, .Max, 1, .SeqCst);
- expect(x == 0xfd);
+ try expect(x == 0xfd);
_ = @atomicRmw(u8, &x, .Min, 1, .SeqCst);
- expect(x == 1);
+ try expect(x == 1);
}
test "atomics with different types" {
- testAtomicsWithType(bool, true, false);
+ try testAtomicsWithType(bool, true, false);
inline for (.{ u1, i5, u15 }) |T| {
var x: T = 0;
- testAtomicsWithType(T, 0, 1);
+ try testAtomicsWithType(T, 0, 1);
}
- testAtomicsWithType(u0, 0, 0);
- testAtomicsWithType(i0, 0, 0);
+ try testAtomicsWithType(u0, 0, 0);
+ try testAtomicsWithType(i0, 0, 0);
}
-fn testAtomicsWithType(comptime T: type, a: T, b: T) void {
+fn testAtomicsWithType(comptime T: type, a: T, b: T) !void {
var x: T = b;
@atomicStore(T, &x, a, .SeqCst);
- expect(x == a);
- expect(@atomicLoad(T, &x, .SeqCst) == a);
- expect(@atomicRmw(T, &x, .Xchg, b, .SeqCst) == a);
- expect(@cmpxchgStrong(T, &x, b, a, .SeqCst, .SeqCst) == null);
+ try expect(x == a);
+ try expect(@atomicLoad(T, &x, .SeqCst) == a);
+ try expect(@atomicRmw(T, &x, .Xchg, b, .SeqCst) == a);
+ try expect(@cmpxchgStrong(T, &x, b, a, .SeqCst, .SeqCst) == null);
if (@sizeOf(T) != 0)
- expect(@cmpxchgStrong(T, &x, b, a, .SeqCst, .SeqCst).? == a);
+ try expect(@cmpxchgStrong(T, &x, b, a, .SeqCst, .SeqCst).? == a);
}
test/stage1/behavior/await_struct.zig
@@ -15,8 +15,8 @@ test "coroutine await struct" {
await_seq('f');
resume await_a_promise;
await_seq('i');
- expect(await_final_result.x == 1234);
- expect(std.mem.eql(u8, &await_points, "abcdefghi"));
+ try expect(await_final_result.x == 1234);
+ try expect(std.mem.eql(u8, &await_points, "abcdefghi"));
}
fn await_amain() callconv(.Async) void {
await_seq('b');
test/stage1/behavior/bit_shifting.zig
@@ -3,8 +3,8 @@ const expect = std.testing.expect;
fn ShardedTable(comptime Key: type, comptime mask_bit_count: comptime_int, comptime V: type) type {
const key_bits = @typeInfo(Key).Int.bits;
- expect(Key == std.meta.Int(.unsigned, key_bits));
- expect(key_bits >= mask_bit_count);
+ std.debug.assert(Key == std.meta.Int(.unsigned, key_bits));
+ std.debug.assert(key_bits >= mask_bit_count);
const shard_key_bits = mask_bit_count;
const ShardKey = std.meta.Int(.unsigned, mask_bit_count);
const shift_amount = key_bits - shard_key_bits;
@@ -61,31 +61,31 @@ fn ShardedTable(comptime Key: type, comptime mask_bit_count: comptime_int, compt
test "sharded table" {
// realistic 16-way sharding
- testShardedTable(u32, 4, 8);
+ try testShardedTable(u32, 4, 8);
- testShardedTable(u5, 0, 32); // ShardKey == u0
- testShardedTable(u5, 2, 32);
- testShardedTable(u5, 5, 32);
+ try testShardedTable(u5, 0, 32); // ShardKey == u0
+ try testShardedTable(u5, 2, 32);
+ try testShardedTable(u5, 5, 32);
- testShardedTable(u1, 0, 2);
- testShardedTable(u1, 1, 2); // this does u1 >> u0
+ try testShardedTable(u1, 0, 2);
+ try testShardedTable(u1, 1, 2); // this does u1 >> u0
- testShardedTable(u0, 0, 1);
+ try testShardedTable(u0, 0, 1);
}
-fn testShardedTable(comptime Key: type, comptime mask_bit_count: comptime_int, comptime node_count: comptime_int) void {
+fn testShardedTable(comptime Key: type, comptime mask_bit_count: comptime_int, comptime node_count: comptime_int) !void {
const Table = ShardedTable(Key, mask_bit_count, void);
var table = Table.create();
var node_buffer: [node_count]Table.Node = undefined;
for (node_buffer) |*node, i| {
const key = @intCast(Key, i);
- expect(table.get(key) == null);
+ try expect(table.get(key) == null);
node.init(key, {});
table.put(node);
}
for (node_buffer) |*node, i| {
- expect(table.get(@intCast(Key, i)) == node);
+ try expect(table.get(@intCast(Key, i)) == node);
}
}
@@ -93,9 +93,9 @@ fn testShardedTable(comptime Key: type, comptime mask_bit_count: comptime_int, c
test "comptime shr of BigInt" {
comptime {
var n0 = 0xdeadbeef0000000000000000;
- std.debug.assert(n0 >> 64 == 0xdeadbeef);
+ try expect(n0 >> 64 == 0xdeadbeef);
var n1 = 17908056155735594659;
- std.debug.assert(n1 >> 64 == 0);
+ try expect(n1 >> 64 == 0);
}
}
test/stage1/behavior/bitcast.zig
@@ -5,13 +5,13 @@ const expectEqual = std.testing.expectEqual;
const maxInt = std.math.maxInt;
test "@bitCast i32 -> u32" {
- testBitCast_i32_u32();
- comptime testBitCast_i32_u32();
+ try testBitCast_i32_u32();
+ comptime try testBitCast_i32_u32();
}
-fn testBitCast_i32_u32() void {
- expect(conv(-1) == maxInt(u32));
- expect(conv2(maxInt(u32)) == -1);
+fn testBitCast_i32_u32() !void {
+ try expect(conv(-1) == maxInt(u32));
+ try expect(conv2(maxInt(u32)) == -1);
}
fn conv(x: i32) u32 {
@@ -26,15 +26,15 @@ test "@bitCast extern enum to its integer type" {
A,
B,
- fn testBitCastExternEnum() void {
+ fn testBitCastExternEnum() !void {
var SOCK_DGRAM = @This().B;
var sock_dgram = @bitCast(c_int, SOCK_DGRAM);
- expect(sock_dgram == 1);
+ try expect(sock_dgram == 1);
}
};
- SOCK.testBitCastExternEnum();
- comptime SOCK.testBitCastExternEnum();
+ try SOCK.testBitCastExternEnum();
+ comptime try SOCK.testBitCastExternEnum();
}
test "@bitCast packed structs at runtime and comptime" {
@@ -47,25 +47,25 @@ test "@bitCast packed structs at runtime and comptime" {
quarter4: u4,
};
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var full = Full{ .number = 0x1234 };
var two_halves = @bitCast(Divided, full);
switch (builtin.endian) {
builtin.Endian.Big => {
- expect(two_halves.half1 == 0x12);
- expect(two_halves.quarter3 == 0x3);
- expect(two_halves.quarter4 == 0x4);
+ try expect(two_halves.half1 == 0x12);
+ try expect(two_halves.quarter3 == 0x3);
+ try expect(two_halves.quarter4 == 0x4);
},
builtin.Endian.Little => {
- expect(two_halves.half1 == 0x34);
- expect(two_halves.quarter3 == 0x2);
- expect(two_halves.quarter4 == 0x1);
+ try expect(two_halves.half1 == 0x34);
+ try expect(two_halves.quarter3 == 0x2);
+ try expect(two_halves.quarter4 == 0x1);
},
}
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "@bitCast extern structs at runtime and comptime" {
@@ -77,23 +77,23 @@ test "@bitCast extern structs at runtime and comptime" {
half2: u8,
};
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var full = Full{ .number = 0x1234 };
var two_halves = @bitCast(TwoHalves, full);
switch (builtin.endian) {
builtin.Endian.Big => {
- expect(two_halves.half1 == 0x12);
- expect(two_halves.half2 == 0x34);
+ try expect(two_halves.half1 == 0x12);
+ try expect(two_halves.half2 == 0x34);
},
builtin.Endian.Little => {
- expect(two_halves.half1 == 0x34);
- expect(two_halves.half2 == 0x12);
+ try expect(two_halves.half1 == 0x34);
+ try expect(two_halves.half2 == 0x12);
},
}
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "bitcast packed struct to integer and back" {
@@ -102,35 +102,35 @@ test "bitcast packed struct to integer and back" {
level: u7,
};
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var move = LevelUpMove{ .move_id = 1, .level = 2 };
var v = @bitCast(u16, move);
var back_to_a_move = @bitCast(LevelUpMove, v);
- expect(back_to_a_move.move_id == 1);
- expect(back_to_a_move.level == 2);
+ try expect(back_to_a_move.move_id == 1);
+ try expect(back_to_a_move.level == 2);
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "implicit cast to error union by returning" {
const S = struct {
- fn entry() void {
- expect((func(-1) catch unreachable) == maxInt(u64));
+ fn entry() !void {
+ try expect((func(-1) catch unreachable) == maxInt(u64));
}
pub fn func(sz: i64) anyerror!u64 {
return @bitCast(u64, sz);
}
};
- S.entry();
- comptime S.entry();
+ try S.entry();
+ comptime try S.entry();
}
// issue #3010: compiler segfault
test "bitcast literal [4]u8 param to u32" {
const ip = @bitCast(u32, [_]u8{ 255, 255, 255, 255 });
- expect(ip == maxInt(u32));
+ try expect(ip == maxInt(u32));
}
test "bitcast packed struct literal to byte" {
@@ -138,14 +138,14 @@ test "bitcast packed struct literal to byte" {
value: u8,
};
const casted = @bitCast(u8, Foo{ .value = 0xF });
- expect(casted == 0xf);
+ try expect(casted == 0xf);
}
test "comptime bitcast used in expression has the correct type" {
const Foo = packed struct {
value: u8,
};
- expect(@bitCast(u8, Foo{ .value = 0xF }) == 0xf);
+ try expect(@bitCast(u8, Foo{ .value = 0xF }) == 0xf);
}
test "bitcast result to _" {
@@ -154,43 +154,43 @@ test "bitcast result to _" {
test "nested bitcast" {
const S = struct {
- fn moo(x: isize) void {
- @import("std").testing.expectEqual(@intCast(isize, 42), x);
+ fn moo(x: isize) !void {
+ try @import("std").testing.expectEqual(@intCast(isize, 42), x);
}
- fn foo(x: isize) void {
- @This().moo(
+ fn foo(x: isize) !void {
+ try @This().moo(
@bitCast(isize, if (x != 0) @bitCast(usize, x) else @bitCast(usize, x)),
);
}
};
- S.foo(42);
- comptime S.foo(42);
+ try S.foo(42);
+ comptime try S.foo(42);
}
test "bitcast passed as tuple element" {
const S = struct {
- fn foo(args: anytype) void {
- comptime expect(@TypeOf(args[0]) == f32);
- expect(args[0] == 12.34);
+ fn foo(args: anytype) !void {
+ comptime try expect(@TypeOf(args[0]) == f32);
+ try expect(args[0] == 12.34);
}
};
- S.foo(.{@bitCast(f32, @as(u32, 0x414570A4))});
+ try S.foo(.{@bitCast(f32, @as(u32, 0x414570A4))});
}
test "triple level result location with bitcast sandwich passed as tuple element" {
const S = struct {
- fn foo(args: anytype) void {
- comptime expect(@TypeOf(args[0]) == f64);
- expect(args[0] > 12.33 and args[0] < 12.35);
+ fn foo(args: anytype) !void {
+ comptime try expect(@TypeOf(args[0]) == f64);
+ try expect(args[0] > 12.33 and args[0] < 12.35);
}
};
- S.foo(.{@as(f64, @bitCast(f32, @as(u32, 0x414570A4)))});
+ try S.foo(.{@as(f64, @bitCast(f32, @as(u32, 0x414570A4)))});
}
test "bitcast generates a temporary value" {
var y = @as(u16, 0x55AA);
const x = @bitCast(u16, @bitCast([2]u8, y));
- expectEqual(y, x);
+ try expectEqual(y, x);
}
test/stage1/behavior/bitreverse.zig
@@ -3,67 +3,67 @@ const expect = std.testing.expect;
const minInt = std.math.minInt;
test "@bitReverse" {
- comptime testBitReverse();
- testBitReverse();
+ comptime try testBitReverse();
+ try testBitReverse();
}
-fn testBitReverse() void {
+fn testBitReverse() !void {
// using comptime_ints, unsigned
- expect(@bitReverse(u0, 0) == 0);
- expect(@bitReverse(u5, 0x12) == 0x9);
- expect(@bitReverse(u8, 0x12) == 0x48);
- expect(@bitReverse(u16, 0x1234) == 0x2c48);
- expect(@bitReverse(u24, 0x123456) == 0x6a2c48);
- expect(@bitReverse(u32, 0x12345678) == 0x1e6a2c48);
- expect(@bitReverse(u40, 0x123456789a) == 0x591e6a2c48);
- expect(@bitReverse(u48, 0x123456789abc) == 0x3d591e6a2c48);
- expect(@bitReverse(u56, 0x123456789abcde) == 0x7b3d591e6a2c48);
- expect(@bitReverse(u64, 0x123456789abcdef1) == 0x8f7b3d591e6a2c48);
- expect(@bitReverse(u128, 0x123456789abcdef11121314151617181) == 0x818e868a828c84888f7b3d591e6a2c48);
+ try expect(@bitReverse(u0, 0) == 0);
+ try expect(@bitReverse(u5, 0x12) == 0x9);
+ try expect(@bitReverse(u8, 0x12) == 0x48);
+ try expect(@bitReverse(u16, 0x1234) == 0x2c48);
+ try expect(@bitReverse(u24, 0x123456) == 0x6a2c48);
+ try expect(@bitReverse(u32, 0x12345678) == 0x1e6a2c48);
+ try expect(@bitReverse(u40, 0x123456789a) == 0x591e6a2c48);
+ try expect(@bitReverse(u48, 0x123456789abc) == 0x3d591e6a2c48);
+ try expect(@bitReverse(u56, 0x123456789abcde) == 0x7b3d591e6a2c48);
+ try expect(@bitReverse(u64, 0x123456789abcdef1) == 0x8f7b3d591e6a2c48);
+ try expect(@bitReverse(u128, 0x123456789abcdef11121314151617181) == 0x818e868a828c84888f7b3d591e6a2c48);
// using runtime uints, unsigned
var num0: u0 = 0;
- expect(@bitReverse(u0, num0) == 0);
+ try expect(@bitReverse(u0, num0) == 0);
var num5: u5 = 0x12;
- expect(@bitReverse(u5, num5) == 0x9);
+ try expect(@bitReverse(u5, num5) == 0x9);
var num8: u8 = 0x12;
- expect(@bitReverse(u8, num8) == 0x48);
+ try expect(@bitReverse(u8, num8) == 0x48);
var num16: u16 = 0x1234;
- expect(@bitReverse(u16, num16) == 0x2c48);
+ try expect(@bitReverse(u16, num16) == 0x2c48);
var num24: u24 = 0x123456;
- expect(@bitReverse(u24, num24) == 0x6a2c48);
+ try expect(@bitReverse(u24, num24) == 0x6a2c48);
var num32: u32 = 0x12345678;
- expect(@bitReverse(u32, num32) == 0x1e6a2c48);
+ try expect(@bitReverse(u32, num32) == 0x1e6a2c48);
var num40: u40 = 0x123456789a;
- expect(@bitReverse(u40, num40) == 0x591e6a2c48);
+ try expect(@bitReverse(u40, num40) == 0x591e6a2c48);
var num48: u48 = 0x123456789abc;
- expect(@bitReverse(u48, num48) == 0x3d591e6a2c48);
+ try expect(@bitReverse(u48, num48) == 0x3d591e6a2c48);
var num56: u56 = 0x123456789abcde;
- expect(@bitReverse(u56, num56) == 0x7b3d591e6a2c48);
+ try expect(@bitReverse(u56, num56) == 0x7b3d591e6a2c48);
var num64: u64 = 0x123456789abcdef1;
- expect(@bitReverse(u64, num64) == 0x8f7b3d591e6a2c48);
+ try expect(@bitReverse(u64, num64) == 0x8f7b3d591e6a2c48);
var num128: u128 = 0x123456789abcdef11121314151617181;
- expect(@bitReverse(u128, num128) == 0x818e868a828c84888f7b3d591e6a2c48);
+ try expect(@bitReverse(u128, num128) == 0x818e868a828c84888f7b3d591e6a2c48);
// using comptime_ints, signed, positive
- expect(@bitReverse(u8, @as(u8, 0)) == 0);
- expect(@bitReverse(i8, @bitCast(i8, @as(u8, 0x92))) == @bitCast(i8, @as(u8, 0x49)));
- expect(@bitReverse(i16, @bitCast(i16, @as(u16, 0x1234))) == @bitCast(i16, @as(u16, 0x2c48)));
- expect(@bitReverse(i24, @bitCast(i24, @as(u24, 0x123456))) == @bitCast(i24, @as(u24, 0x6a2c48)));
- expect(@bitReverse(i32, @bitCast(i32, @as(u32, 0x12345678))) == @bitCast(i32, @as(u32, 0x1e6a2c48)));
- expect(@bitReverse(i40, @bitCast(i40, @as(u40, 0x123456789a))) == @bitCast(i40, @as(u40, 0x591e6a2c48)));
- expect(@bitReverse(i48, @bitCast(i48, @as(u48, 0x123456789abc))) == @bitCast(i48, @as(u48, 0x3d591e6a2c48)));
- expect(@bitReverse(i56, @bitCast(i56, @as(u56, 0x123456789abcde))) == @bitCast(i56, @as(u56, 0x7b3d591e6a2c48)));
- expect(@bitReverse(i64, @bitCast(i64, @as(u64, 0x123456789abcdef1))) == @bitCast(i64, @as(u64, 0x8f7b3d591e6a2c48)));
- expect(@bitReverse(i128, @bitCast(i128, @as(u128, 0x123456789abcdef11121314151617181))) == @bitCast(i128, @as(u128, 0x818e868a828c84888f7b3d591e6a2c48)));
+ try expect(@bitReverse(u8, @as(u8, 0)) == 0);
+ try expect(@bitReverse(i8, @bitCast(i8, @as(u8, 0x92))) == @bitCast(i8, @as(u8, 0x49)));
+ try expect(@bitReverse(i16, @bitCast(i16, @as(u16, 0x1234))) == @bitCast(i16, @as(u16, 0x2c48)));
+ try expect(@bitReverse(i24, @bitCast(i24, @as(u24, 0x123456))) == @bitCast(i24, @as(u24, 0x6a2c48)));
+ try expect(@bitReverse(i32, @bitCast(i32, @as(u32, 0x12345678))) == @bitCast(i32, @as(u32, 0x1e6a2c48)));
+ try expect(@bitReverse(i40, @bitCast(i40, @as(u40, 0x123456789a))) == @bitCast(i40, @as(u40, 0x591e6a2c48)));
+ try expect(@bitReverse(i48, @bitCast(i48, @as(u48, 0x123456789abc))) == @bitCast(i48, @as(u48, 0x3d591e6a2c48)));
+ try expect(@bitReverse(i56, @bitCast(i56, @as(u56, 0x123456789abcde))) == @bitCast(i56, @as(u56, 0x7b3d591e6a2c48)));
+ try expect(@bitReverse(i64, @bitCast(i64, @as(u64, 0x123456789abcdef1))) == @bitCast(i64, @as(u64, 0x8f7b3d591e6a2c48)));
+ try expect(@bitReverse(i128, @bitCast(i128, @as(u128, 0x123456789abcdef11121314151617181))) == @bitCast(i128, @as(u128, 0x818e868a828c84888f7b3d591e6a2c48)));
// using signed, negative. Compare to runtime ints returned from llvm.
var neg8: i8 = -18;
- expect(@bitReverse(i8, @as(i8, -18)) == @bitReverse(i8, neg8));
+ try expect(@bitReverse(i8, @as(i8, -18)) == @bitReverse(i8, neg8));
var neg16: i16 = -32694;
- expect(@bitReverse(i16, @as(i16, -32694)) == @bitReverse(i16, neg16));
+ try expect(@bitReverse(i16, @as(i16, -32694)) == @bitReverse(i16, neg16));
var neg24: i24 = -6773785;
- expect(@bitReverse(i24, @as(i24, -6773785)) == @bitReverse(i24, neg24));
+ try expect(@bitReverse(i24, @as(i24, -6773785)) == @bitReverse(i24, neg24));
var neg32: i32 = -16773785;
- expect(@bitReverse(i32, @as(i32, -16773785)) == @bitReverse(i32, neg32));
+ try expect(@bitReverse(i32, @as(i32, -16773785)) == @bitReverse(i32, neg32));
}
test/stage1/behavior/bool.zig
@@ -1,25 +1,25 @@
const expect = @import("std").testing.expect;
test "bool literals" {
- expect(true);
- expect(!false);
+ try expect(true);
+ try expect(!false);
}
test "cast bool to int" {
const t = true;
const f = false;
- expect(@boolToInt(t) == @as(u32, 1));
- expect(@boolToInt(f) == @as(u32, 0));
- nonConstCastBoolToInt(t, f);
+ try expect(@boolToInt(t) == @as(u32, 1));
+ try expect(@boolToInt(f) == @as(u32, 0));
+ try nonConstCastBoolToInt(t, f);
}
-fn nonConstCastBoolToInt(t: bool, f: bool) void {
- expect(@boolToInt(t) == @as(u32, 1));
- expect(@boolToInt(f) == @as(u32, 0));
+fn nonConstCastBoolToInt(t: bool, f: bool) !void {
+ try expect(@boolToInt(t) == @as(u32, 1));
+ try expect(@boolToInt(f) == @as(u32, 0));
}
test "bool cmp" {
- expect(testBoolCmp(true, false) == false);
+ try expect(testBoolCmp(true, false) == false);
}
fn testBoolCmp(a: bool, b: bool) bool {
return a == b;
@@ -30,6 +30,6 @@ const global_t = true;
const not_global_f = !global_f;
const not_global_t = !global_t;
test "compile time bool not" {
- expect(not_global_f);
- expect(!not_global_t);
+ try expect(not_global_f);
+ try expect(!not_global_t);
}
test/stage1/behavior/byteswap.zig
@@ -3,39 +3,39 @@ const expect = std.testing.expect;
test "@byteSwap integers" {
const ByteSwapIntTest = struct {
- fn run() void {
- t(u0, 0, 0);
- t(u8, 0x12, 0x12);
- t(u16, 0x1234, 0x3412);
- t(u24, 0x123456, 0x563412);
- t(u32, 0x12345678, 0x78563412);
- t(u40, 0x123456789a, 0x9a78563412);
- t(i48, 0x123456789abc, @bitCast(i48, @as(u48, 0xbc9a78563412)));
- t(u56, 0x123456789abcde, 0xdebc9a78563412);
- t(u64, 0x123456789abcdef1, 0xf1debc9a78563412);
- t(u128, 0x123456789abcdef11121314151617181, 0x8171615141312111f1debc9a78563412);
+ fn run() !void {
+ try t(u0, 0, 0);
+ try t(u8, 0x12, 0x12);
+ try t(u16, 0x1234, 0x3412);
+ try t(u24, 0x123456, 0x563412);
+ try t(u32, 0x12345678, 0x78563412);
+ try t(u40, 0x123456789a, 0x9a78563412);
+ try t(i48, 0x123456789abc, @bitCast(i48, @as(u48, 0xbc9a78563412)));
+ try t(u56, 0x123456789abcde, 0xdebc9a78563412);
+ try t(u64, 0x123456789abcdef1, 0xf1debc9a78563412);
+ try t(u128, 0x123456789abcdef11121314151617181, 0x8171615141312111f1debc9a78563412);
- t(u0, @as(u0, 0), 0);
- t(i8, @as(i8, -50), -50);
- t(i16, @bitCast(i16, @as(u16, 0x1234)), @bitCast(i16, @as(u16, 0x3412)));
- t(i24, @bitCast(i24, @as(u24, 0x123456)), @bitCast(i24, @as(u24, 0x563412)));
- t(i32, @bitCast(i32, @as(u32, 0x12345678)), @bitCast(i32, @as(u32, 0x78563412)));
- t(u40, @bitCast(i40, @as(u40, 0x123456789a)), @as(u40, 0x9a78563412));
- t(i48, @bitCast(i48, @as(u48, 0x123456789abc)), @bitCast(i48, @as(u48, 0xbc9a78563412)));
- t(i56, @bitCast(i56, @as(u56, 0x123456789abcde)), @bitCast(i56, @as(u56, 0xdebc9a78563412)));
- t(i64, @bitCast(i64, @as(u64, 0x123456789abcdef1)), @bitCast(i64, @as(u64, 0xf1debc9a78563412)));
- t(
+ try t(u0, @as(u0, 0), 0);
+ try t(i8, @as(i8, -50), -50);
+ try t(i16, @bitCast(i16, @as(u16, 0x1234)), @bitCast(i16, @as(u16, 0x3412)));
+ try t(i24, @bitCast(i24, @as(u24, 0x123456)), @bitCast(i24, @as(u24, 0x563412)));
+ try t(i32, @bitCast(i32, @as(u32, 0x12345678)), @bitCast(i32, @as(u32, 0x78563412)));
+ try t(u40, @bitCast(i40, @as(u40, 0x123456789a)), @as(u40, 0x9a78563412));
+ try t(i48, @bitCast(i48, @as(u48, 0x123456789abc)), @bitCast(i48, @as(u48, 0xbc9a78563412)));
+ try t(i56, @bitCast(i56, @as(u56, 0x123456789abcde)), @bitCast(i56, @as(u56, 0xdebc9a78563412)));
+ try t(i64, @bitCast(i64, @as(u64, 0x123456789abcdef1)), @bitCast(i64, @as(u64, 0xf1debc9a78563412)));
+ try t(
i128,
@bitCast(i128, @as(u128, 0x123456789abcdef11121314151617181)),
@bitCast(i128, @as(u128, 0x8171615141312111f1debc9a78563412)),
);
}
- fn t(comptime I: type, input: I, expected_output: I) void {
- std.testing.expectEqual(expected_output, @byteSwap(I, input));
+ fn t(comptime I: type, input: I, expected_output: I) !void {
+ try std.testing.expectEqual(expected_output, @byteSwap(I, input));
}
};
- comptime ByteSwapIntTest.run();
- ByteSwapIntTest.run();
+ comptime try ByteSwapIntTest.run();
+ try ByteSwapIntTest.run();
}
test "@byteSwap vectors" {
@@ -46,10 +46,10 @@ test "@byteSwap vectors" {
if (std.Target.current.cpu.arch == .mipsel or std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
const ByteSwapVectorTest = struct {
- fn run() void {
- t(u8, 2, [_]u8{ 0x12, 0x13 }, [_]u8{ 0x12, 0x13 });
- t(u16, 2, [_]u16{ 0x1234, 0x2345 }, [_]u16{ 0x3412, 0x4523 });
- t(u24, 2, [_]u24{ 0x123456, 0x234567 }, [_]u24{ 0x563412, 0x674523 });
+ fn run() !void {
+ try t(u8, 2, [_]u8{ 0x12, 0x13 }, [_]u8{ 0x12, 0x13 });
+ try t(u16, 2, [_]u16{ 0x1234, 0x2345 }, [_]u16{ 0x3412, 0x4523 });
+ try t(u24, 2, [_]u24{ 0x123456, 0x234567 }, [_]u24{ 0x563412, 0x674523 });
}
fn t(
@@ -57,12 +57,12 @@ test "@byteSwap vectors" {
comptime n: comptime_int,
input: std.meta.Vector(n, I),
expected_vector: std.meta.Vector(n, I),
- ) void {
+ ) !void {
const actual_output: [n]I = @byteSwap(I, input);
const expected_output: [n]I = expected_vector;
- std.testing.expectEqual(expected_output, actual_output);
+ try std.testing.expectEqual(expected_output, actual_output);
}
};
- comptime ByteSwapVectorTest.run();
- ByteSwapVectorTest.run();
+ comptime try ByteSwapVectorTest.run();
+ try ByteSwapVectorTest.run();
}
test/stage1/behavior/byval_arg_var.zig
@@ -6,7 +6,7 @@ test "pass string literal byvalue to a generic var param" {
start();
blowUpStack(10);
- std.testing.expect(std.mem.eql(u8, result, "string literal"));
+ try std.testing.expect(std.mem.eql(u8, result, "string literal"));
}
fn start() void {
test/stage1/behavior/call.zig
@@ -8,25 +8,25 @@ test "basic invocations" {
return 1234;
}
}.foo;
- expect(@call(.{}, foo, .{}) == 1234);
+ try expect(@call(.{}, foo, .{}) == 1234);
comptime {
// modifiers that allow comptime calls
- expect(@call(.{}, foo, .{}) == 1234);
- expect(@call(.{ .modifier = .no_async }, foo, .{}) == 1234);
- expect(@call(.{ .modifier = .always_tail }, foo, .{}) == 1234);
- expect(@call(.{ .modifier = .always_inline }, foo, .{}) == 1234);
+ try expect(@call(.{}, foo, .{}) == 1234);
+ try expect(@call(.{ .modifier = .no_async }, foo, .{}) == 1234);
+ try expect(@call(.{ .modifier = .always_tail }, foo, .{}) == 1234);
+ try expect(@call(.{ .modifier = .always_inline }, foo, .{}) == 1234);
}
{
// comptime call without comptime keyword
const result = @call(.{ .modifier = .compile_time }, foo, .{}) == 1234;
- comptime expect(result);
+ comptime try expect(result);
}
{
// call of non comptime-known function
var alias_foo = foo;
- expect(@call(.{ .modifier = .no_async }, alias_foo, .{}) == 1234);
- expect(@call(.{ .modifier = .never_tail }, alias_foo, .{}) == 1234);
- expect(@call(.{ .modifier = .never_inline }, alias_foo, .{}) == 1234);
+ try expect(@call(.{ .modifier = .no_async }, alias_foo, .{}) == 1234);
+ try expect(@call(.{ .modifier = .never_tail }, alias_foo, .{}) == 1234);
+ try expect(@call(.{ .modifier = .never_inline }, alias_foo, .{}) == 1234);
}
}
@@ -38,20 +38,20 @@ test "tuple parameters" {
}.add;
var a: i32 = 12;
var b: i32 = 34;
- expect(@call(.{}, add, .{ a, 34 }) == 46);
- expect(@call(.{}, add, .{ 12, b }) == 46);
- expect(@call(.{}, add, .{ a, b }) == 46);
- expect(@call(.{}, add, .{ 12, 34 }) == 46);
- comptime expect(@call(.{}, add, .{ 12, 34 }) == 46);
+ try expect(@call(.{}, add, .{ a, 34 }) == 46);
+ try expect(@call(.{}, add, .{ 12, b }) == 46);
+ try expect(@call(.{}, add, .{ a, b }) == 46);
+ try expect(@call(.{}, add, .{ 12, 34 }) == 46);
+ comptime try expect(@call(.{}, add, .{ 12, 34 }) == 46);
{
const separate_args0 = .{ a, b };
const separate_args1 = .{ a, 34 };
const separate_args2 = .{ 12, 34 };
const separate_args3 = .{ 12, b };
- expect(@call(.{ .modifier = .always_inline }, add, separate_args0) == 46);
- expect(@call(.{ .modifier = .always_inline }, add, separate_args1) == 46);
- expect(@call(.{ .modifier = .always_inline }, add, separate_args2) == 46);
- expect(@call(.{ .modifier = .always_inline }, add, separate_args3) == 46);
+ try expect(@call(.{ .modifier = .always_inline }, add, separate_args0) == 46);
+ try expect(@call(.{ .modifier = .always_inline }, add, separate_args1) == 46);
+ try expect(@call(.{ .modifier = .always_inline }, add, separate_args2) == 46);
+ try expect(@call(.{ .modifier = .always_inline }, add, separate_args3) == 46);
}
}
@@ -70,5 +70,5 @@ test "comptime call with bound function as parameter" {
};
var inst: S = undefined;
- expectEqual(?i32, S.ReturnType(inst.call_me_maybe));
+ try expectEqual(?i32, S.ReturnType(inst.call_me_maybe));
}
test/stage1/behavior/cast.zig
@@ -8,12 +8,12 @@ test "int to ptr cast" {
const x = @as(usize, 13);
const y = @intToPtr(*u8, x);
const z = @ptrToInt(y);
- expect(z == 13);
+ try expect(z == 13);
}
test "integer literal to pointer cast" {
const vga_mem = @intToPtr(*u16, 0xB8000);
- expect(@ptrToInt(vga_mem) == 0xB8000);
+ try expect(@ptrToInt(vga_mem) == 0xB8000);
}
test "pointer reinterpret const float to int" {
@@ -23,9 +23,9 @@ test "pointer reinterpret const float to int" {
const int_ptr = @ptrCast(*const i32, float_ptr);
const int_val = int_ptr.*;
if (std.builtin.endian == .Little)
- expect(int_val == 0x33333303)
+ try expect(int_val == 0x33333303)
else
- expect(int_val == 0x3fe33333);
+ try expect(int_val == 0x3fe33333);
}
test "implicitly cast indirect pointer to maybe-indirect pointer" {
@@ -49,62 +49,62 @@ test "implicitly cast indirect pointer to maybe-indirect pointer" {
const p = &s;
const q = &p;
const r = &q;
- expect(42 == S.constConst(q));
- expect(42 == S.maybeConstConst(q));
- expect(42 == S.constConstConst(r));
- expect(42 == S.maybeConstConstConst(r));
+ try expect(42 == S.constConst(q));
+ try expect(42 == S.maybeConstConst(q));
+ try expect(42 == S.constConstConst(r));
+ try expect(42 == S.maybeConstConstConst(r));
}
test "explicit cast from integer to error type" {
- testCastIntToErr(error.ItBroke);
- comptime testCastIntToErr(error.ItBroke);
+ try testCastIntToErr(error.ItBroke);
+ comptime try testCastIntToErr(error.ItBroke);
}
-fn testCastIntToErr(err: anyerror) void {
+fn testCastIntToErr(err: anyerror) !void {
const x = @errorToInt(err);
const y = @intToError(x);
- expect(error.ItBroke == y);
+ try expect(error.ItBroke == y);
}
test "peer resolve arrays of different size to const slice" {
- expect(mem.eql(u8, boolToStr(true), "true"));
- expect(mem.eql(u8, boolToStr(false), "false"));
- comptime expect(mem.eql(u8, boolToStr(true), "true"));
- comptime expect(mem.eql(u8, boolToStr(false), "false"));
+ try expect(mem.eql(u8, boolToStr(true), "true"));
+ try expect(mem.eql(u8, boolToStr(false), "false"));
+ comptime try expect(mem.eql(u8, boolToStr(true), "true"));
+ comptime try expect(mem.eql(u8, boolToStr(false), "false"));
}
fn boolToStr(b: bool) []const u8 {
return if (b) "true" else "false";
}
test "peer resolve array and const slice" {
- testPeerResolveArrayConstSlice(true);
- comptime testPeerResolveArrayConstSlice(true);
+ try testPeerResolveArrayConstSlice(true);
+ comptime try testPeerResolveArrayConstSlice(true);
}
-fn testPeerResolveArrayConstSlice(b: bool) void {
+fn testPeerResolveArrayConstSlice(b: bool) !void {
const value1 = if (b) "aoeu" else @as([]const u8, "zz");
const value2 = if (b) @as([]const u8, "zz") else "aoeu";
- expect(mem.eql(u8, value1, "aoeu"));
- expect(mem.eql(u8, value2, "zz"));
+ try expect(mem.eql(u8, value1, "aoeu"));
+ try expect(mem.eql(u8, value2, "zz"));
}
test "implicitly cast from T to anyerror!?T" {
- castToOptionalTypeError(1);
- comptime castToOptionalTypeError(1);
+ try castToOptionalTypeError(1);
+ comptime try castToOptionalTypeError(1);
}
const A = struct {
a: i32,
};
-fn castToOptionalTypeError(z: i32) void {
+fn castToOptionalTypeError(z: i32) !void {
const x = @as(i32, 1);
const y: anyerror!?i32 = x;
- expect((try y).? == 1);
+ try expect((try y).? == 1);
const f = z;
const g: anyerror!?i32 = f;
const a = A{ .a = z };
const b: anyerror!?A = a;
- expect((b catch unreachable).?.a == 1);
+ try expect((b catch unreachable).?.a == 1);
}
test "implicitly cast from int to anyerror!?T" {
@@ -119,7 +119,7 @@ fn implicitIntLitToOptional() void {
test "return null from fn() anyerror!?&T" {
const a = returnNullFromOptionalTypeErrorRef();
const b = returnNullLitFromOptionalTypeErrorRef();
- expect((try a) == null and (try b) == null);
+ try expect((try a) == null and (try b) == null);
}
fn returnNullFromOptionalTypeErrorRef() anyerror!?*A {
const a: ?*A = null;
@@ -130,11 +130,11 @@ fn returnNullLitFromOptionalTypeErrorRef() anyerror!?*A {
}
test "peer type resolution: ?T and T" {
- expect(peerTypeTAndOptionalT(true, false).? == 0);
- expect(peerTypeTAndOptionalT(false, false).? == 3);
+ try expect(peerTypeTAndOptionalT(true, false).? == 0);
+ try expect(peerTypeTAndOptionalT(false, false).? == 3);
comptime {
- expect(peerTypeTAndOptionalT(true, false).? == 0);
- expect(peerTypeTAndOptionalT(false, false).? == 3);
+ try expect(peerTypeTAndOptionalT(true, false).? == 0);
+ try expect(peerTypeTAndOptionalT(false, false).? == 3);
}
}
fn peerTypeTAndOptionalT(c: bool, b: bool) ?usize {
@@ -146,11 +146,11 @@ fn peerTypeTAndOptionalT(c: bool, b: bool) ?usize {
}
test "peer type resolution: [0]u8 and []const u8" {
- expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);
- expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);
+ try expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);
+ try expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);
comptime {
- expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);
- expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);
+ try expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);
+ try expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);
}
}
fn peerTypeEmptyArrayAndSlice(a: bool, slice: []const u8) []const u8 {
@@ -162,8 +162,8 @@ fn peerTypeEmptyArrayAndSlice(a: bool, slice: []const u8) []const u8 {
}
test "implicitly cast from [N]T to ?[]const T" {
- expect(mem.eql(u8, castToOptionalSlice().?, "hi"));
- comptime expect(mem.eql(u8, castToOptionalSlice().?, "hi"));
+ try expect(mem.eql(u8, castToOptionalSlice().?, "hi"));
+ comptime try expect(mem.eql(u8, castToOptionalSlice().?, "hi"));
}
fn castToOptionalSlice() ?[]const u8 {
@@ -171,12 +171,12 @@ fn castToOptionalSlice() ?[]const u8 {
}
test "implicitly cast from [0]T to anyerror![]T" {
- testCastZeroArrayToErrSliceMut();
- comptime testCastZeroArrayToErrSliceMut();
+ try testCastZeroArrayToErrSliceMut();
+ comptime try testCastZeroArrayToErrSliceMut();
}
-fn testCastZeroArrayToErrSliceMut() void {
- expect((gimmeErrOrSlice() catch unreachable).len == 0);
+fn testCastZeroArrayToErrSliceMut() !void {
+ try expect((gimmeErrOrSlice() catch unreachable).len == 0);
}
fn gimmeErrOrSlice() anyerror![]u8 {
@@ -189,19 +189,19 @@ test "peer type resolution: [0]u8, []const u8, and anyerror![]u8" {
{
var data = "hi".*;
const slice = data[0..];
- expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0);
- expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1);
+ try expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0);
+ try expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1);
}
{
var data: [2]u8 = "hi".*;
const slice = data[0..];
- expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0);
- expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1);
+ try expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0);
+ try expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1);
}
}
};
try S.doTheTest();
- try comptime S.doTheTest();
+ comptime try S.doTheTest();
}
fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) anyerror![]u8 {
if (a) {
@@ -212,43 +212,43 @@ fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) anyerror![]u8 {
}
test "resolve undefined with integer" {
- testResolveUndefWithInt(true, 1234);
- comptime testResolveUndefWithInt(true, 1234);
+ try testResolveUndefWithInt(true, 1234);
+ comptime try testResolveUndefWithInt(true, 1234);
}
-fn testResolveUndefWithInt(b: bool, x: i32) void {
+fn testResolveUndefWithInt(b: bool, x: i32) !void {
const value = if (b) x else undefined;
if (b) {
- expect(value == x);
+ try expect(value == x);
}
}
test "implicit cast from &const [N]T to []const T" {
- testCastConstArrayRefToConstSlice();
- comptime testCastConstArrayRefToConstSlice();
+ try testCastConstArrayRefToConstSlice();
+ comptime try testCastConstArrayRefToConstSlice();
}
-fn testCastConstArrayRefToConstSlice() void {
+fn testCastConstArrayRefToConstSlice() !void {
{
const blah = "aoeu".*;
const const_array_ref = &blah;
- expect(@TypeOf(const_array_ref) == *const [4:0]u8);
+ try expect(@TypeOf(const_array_ref) == *const [4:0]u8);
const slice: []const u8 = const_array_ref;
- expect(mem.eql(u8, slice, "aoeu"));
+ try expect(mem.eql(u8, slice, "aoeu"));
}
{
const blah: [4]u8 = "aoeu".*;
const const_array_ref = &blah;
- expect(@TypeOf(const_array_ref) == *const [4]u8);
+ try expect(@TypeOf(const_array_ref) == *const [4]u8);
const slice: []const u8 = const_array_ref;
- expect(mem.eql(u8, slice, "aoeu"));
+ try expect(mem.eql(u8, slice, "aoeu"));
}
}
test "peer type resolution: error and [N]T" {
- expect(mem.eql(u8, try testPeerErrorAndArray(0), "OK"));
- comptime expect(mem.eql(u8, try testPeerErrorAndArray(0), "OK"));
- expect(mem.eql(u8, try testPeerErrorAndArray2(1), "OKK"));
- comptime expect(mem.eql(u8, try testPeerErrorAndArray2(1), "OKK"));
+ try expect(mem.eql(u8, try testPeerErrorAndArray(0), "OK"));
+ comptime try expect(mem.eql(u8, try testPeerErrorAndArray(0), "OK"));
+ try expect(mem.eql(u8, try testPeerErrorAndArray2(1), "OKK"));
+ comptime try expect(mem.eql(u8, try testPeerErrorAndArray2(1), "OKK"));
}
fn testPeerErrorAndArray(x: u8) anyerror![]const u8 {
@@ -266,35 +266,35 @@ fn testPeerErrorAndArray2(x: u8) anyerror![]const u8 {
}
test "@floatToInt" {
- testFloatToInts();
- comptime testFloatToInts();
+ try testFloatToInts();
+ comptime try testFloatToInts();
}
-fn testFloatToInts() void {
+fn testFloatToInts() !void {
const x = @as(i32, 1e4);
- expect(x == 10000);
+ try expect(x == 10000);
const y = @floatToInt(i32, @as(f32, 1e4));
- expect(y == 10000);
- expectFloatToInt(f16, 255.1, u8, 255);
- expectFloatToInt(f16, 127.2, i8, 127);
- expectFloatToInt(f16, -128.2, i8, -128);
- expectFloatToInt(f32, 255.1, u8, 255);
- expectFloatToInt(f32, 127.2, i8, 127);
- expectFloatToInt(f32, -128.2, i8, -128);
- expectFloatToInt(comptime_int, 1234, i16, 1234);
+ try expect(y == 10000);
+ try expectFloatToInt(f16, 255.1, u8, 255);
+ try expectFloatToInt(f16, 127.2, i8, 127);
+ try expectFloatToInt(f16, -128.2, i8, -128);
+ try expectFloatToInt(f32, 255.1, u8, 255);
+ try expectFloatToInt(f32, 127.2, i8, 127);
+ try expectFloatToInt(f32, -128.2, i8, -128);
+ try expectFloatToInt(comptime_int, 1234, i16, 1234);
}
-fn expectFloatToInt(comptime F: type, f: F, comptime I: type, i: I) void {
- expect(@floatToInt(I, f) == i);
+fn expectFloatToInt(comptime F: type, f: F, comptime I: type, i: I) !void {
+ try expect(@floatToInt(I, f) == i);
}
test "cast u128 to f128 and back" {
- comptime testCast128();
- testCast128();
+ comptime try testCast128();
+ try testCast128();
}
-fn testCast128() void {
- expect(cast128Int(cast128Float(0x7fff0000000000000000000000000000)) == 0x7fff0000000000000000000000000000);
+fn testCast128() !void {
+ try expect(cast128Int(cast128Float(0x7fff0000000000000000000000000000)) == 0x7fff0000000000000000000000000000);
}
fn cast128Int(x: f128) u128 {
@@ -306,69 +306,69 @@ fn cast128Float(x: u128) f128 {
}
test "single-item pointer of array to slice and to unknown length pointer" {
- testCastPtrOfArrayToSliceAndPtr();
- comptime testCastPtrOfArrayToSliceAndPtr();
+ try testCastPtrOfArrayToSliceAndPtr();
+ comptime try testCastPtrOfArrayToSliceAndPtr();
}
-fn testCastPtrOfArrayToSliceAndPtr() void {
+fn testCastPtrOfArrayToSliceAndPtr() !void {
{
var array = "aoeu".*;
const x: [*]u8 = &array;
x[0] += 1;
- expect(mem.eql(u8, array[0..], "boeu"));
+ try expect(mem.eql(u8, array[0..], "boeu"));
const y: []u8 = &array;
y[0] += 1;
- expect(mem.eql(u8, array[0..], "coeu"));
+ try expect(mem.eql(u8, array[0..], "coeu"));
}
{
var array: [4]u8 = "aoeu".*;
const x: [*]u8 = &array;
x[0] += 1;
- expect(mem.eql(u8, array[0..], "boeu"));
+ try expect(mem.eql(u8, array[0..], "boeu"));
const y: []u8 = &array;
y[0] += 1;
- expect(mem.eql(u8, array[0..], "coeu"));
+ try expect(mem.eql(u8, array[0..], "coeu"));
}
}
test "cast *[1][*]const u8 to [*]const ?[*]const u8" {
const window_name = [1][*]const u8{"window name"};
const x: [*]const ?[*]const u8 = &window_name;
- expect(mem.eql(u8, std.mem.spanZ(@ptrCast([*:0]const u8, x[0].?)), "window name"));
+ try expect(mem.eql(u8, std.mem.spanZ(@ptrCast([*:0]const u8, x[0].?)), "window name"));
}
test "@intCast comptime_int" {
const result = @intCast(i32, 1234);
- expect(@TypeOf(result) == i32);
- expect(result == 1234);
+ try expect(@TypeOf(result) == i32);
+ try expect(result == 1234);
}
test "@floatCast comptime_int and comptime_float" {
{
const result = @floatCast(f16, 1234);
- expect(@TypeOf(result) == f16);
- expect(result == 1234.0);
+ try expect(@TypeOf(result) == f16);
+ try expect(result == 1234.0);
}
{
const result = @floatCast(f16, 1234.0);
- expect(@TypeOf(result) == f16);
- expect(result == 1234.0);
+ try expect(@TypeOf(result) == f16);
+ try expect(result == 1234.0);
}
{
const result = @floatCast(f32, 1234);
- expect(@TypeOf(result) == f32);
- expect(result == 1234.0);
+ try expect(@TypeOf(result) == f32);
+ try expect(result == 1234.0);
}
{
const result = @floatCast(f32, 1234.0);
- expect(@TypeOf(result) == f32);
- expect(result == 1234.0);
+ try expect(@TypeOf(result) == f32);
+ try expect(result == 1234.0);
}
}
test "vector casts" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
// Upcast (implicit, equivalent to @intCast)
var up0: Vector(2, u8) = [_]u8{ 0x55, 0xaa };
var up1 = @as(Vector(2, u16), up0);
@@ -380,55 +380,55 @@ test "vector casts" {
var down2 = @intCast(Vector(2, u16), down0);
var down3 = @intCast(Vector(2, u8), down0);
- expect(mem.eql(u16, &@as([2]u16, up1), &[2]u16{ 0x55, 0xaa }));
- expect(mem.eql(u32, &@as([2]u32, up2), &[2]u32{ 0x55, 0xaa }));
- expect(mem.eql(u64, &@as([2]u64, up3), &[2]u64{ 0x55, 0xaa }));
+ try expect(mem.eql(u16, &@as([2]u16, up1), &[2]u16{ 0x55, 0xaa }));
+ try expect(mem.eql(u32, &@as([2]u32, up2), &[2]u32{ 0x55, 0xaa }));
+ try expect(mem.eql(u64, &@as([2]u64, up3), &[2]u64{ 0x55, 0xaa }));
- expect(mem.eql(u32, &@as([2]u32, down1), &[2]u32{ 0x55, 0xaa }));
- expect(mem.eql(u16, &@as([2]u16, down2), &[2]u16{ 0x55, 0xaa }));
- expect(mem.eql(u8, &@as([2]u8, down3), &[2]u8{ 0x55, 0xaa }));
+ try expect(mem.eql(u32, &@as([2]u32, down1), &[2]u32{ 0x55, 0xaa }));
+ try expect(mem.eql(u16, &@as([2]u16, down2), &[2]u16{ 0x55, 0xaa }));
+ try expect(mem.eql(u8, &@as([2]u8, down3), &[2]u8{ 0x55, 0xaa }));
}
- fn doTheTestFloat() void {
+ fn doTheTestFloat() !void {
var vec = @splat(2, @as(f32, 1234.0));
var wider: Vector(2, f64) = vec;
- expect(wider[0] == 1234.0);
- expect(wider[1] == 1234.0);
+ try expect(wider[0] == 1234.0);
+ try expect(wider[1] == 1234.0);
}
};
- S.doTheTest();
- comptime S.doTheTest();
- S.doTheTestFloat();
- comptime S.doTheTestFloat();
+ try S.doTheTest();
+ comptime try S.doTheTest();
+ try S.doTheTestFloat();
+ comptime try S.doTheTestFloat();
}
test "comptime_int @intToFloat" {
{
const result = @intToFloat(f16, 1234);
- expect(@TypeOf(result) == f16);
- expect(result == 1234.0);
+ try expect(@TypeOf(result) == f16);
+ try expect(result == 1234.0);
}
{
const result = @intToFloat(f32, 1234);
- expect(@TypeOf(result) == f32);
- expect(result == 1234.0);
+ try expect(@TypeOf(result) == f32);
+ try expect(result == 1234.0);
}
{
const result = @intToFloat(f64, 1234);
- expect(@TypeOf(result) == f64);
- expect(result == 1234.0);
+ try expect(@TypeOf(result) == f64);
+ try expect(result == 1234.0);
}
{
const result = @intToFloat(f128, 1234);
- expect(@TypeOf(result) == f128);
- expect(result == 1234.0);
+ try expect(@TypeOf(result) == f128);
+ try expect(result == 1234.0);
}
// big comptime_int (> 64 bits) to f128 conversion
{
const result = @intToFloat(f128, 0x1_0000_0000_0000_0000);
- expect(@TypeOf(result) == f128);
- expect(result == 0x1_0000_0000_0000_0000.0);
+ try expect(@TypeOf(result) == f128);
+ try expect(result == 0x1_0000_0000_0000_0000.0);
}
}
@@ -436,25 +436,25 @@ test "@intCast i32 to u7" {
var x: u128 = maxInt(u128);
var y: i32 = 120;
var z = x >> @intCast(u7, y);
- expect(z == 0xff);
+ try expect(z == 0xff);
}
test "@floatCast cast down" {
{
var double: f64 = 0.001534;
var single = @floatCast(f32, double);
- expect(single == 0.001534);
+ try expect(single == 0.001534);
}
{
const double: f64 = 0.001534;
const single = @floatCast(f32, double);
- expect(single == 0.001534);
+ try expect(single == 0.001534);
}
}
test "implicit cast undefined to optional" {
- expect(MakeType(void).getNull() == null);
- expect(MakeType(void).getNonNull() != null);
+ try expect(MakeType(void).getNull() == null);
+ try expect(MakeType(void).getNonNull() != null);
}
fn MakeType(comptime T: type) type {
@@ -474,26 +474,26 @@ test "implicit cast from *[N]T to ?[*]T" {
var y: [4]u16 = [4]u16{ 0, 1, 2, 3 };
x = &y;
- expect(std.mem.eql(u16, x.?[0..4], y[0..4]));
+ try expect(std.mem.eql(u16, x.?[0..4], y[0..4]));
x.?[0] = 8;
y[3] = 6;
- expect(std.mem.eql(u16, x.?[0..4], y[0..4]));
+ try expect(std.mem.eql(u16, x.?[0..4], y[0..4]));
}
test "implicit cast from *[N]T to [*c]T" {
var x: [4]u16 = [4]u16{ 0, 1, 2, 3 };
var y: [*c]u16 = &x;
- expect(std.mem.eql(u16, x[0..4], y[0..4]));
+ try expect(std.mem.eql(u16, x[0..4], y[0..4]));
x[0] = 8;
y[3] = 6;
- expect(std.mem.eql(u16, x[0..4], y[0..4]));
+ try expect(std.mem.eql(u16, x[0..4], y[0..4]));
}
test "implicit cast from *T to ?*c_void" {
var a: u8 = 1;
incrementVoidPtrValue(&a);
- std.testing.expect(a == 2);
+ try std.testing.expect(a == 2);
}
fn incrementVoidPtrValue(value: ?*c_void) void {
@@ -504,7 +504,7 @@ test "implicit cast from [*]T to ?*c_void" {
var a = [_]u8{ 3, 2, 1 };
var runtime_zero: usize = 0;
incrementVoidPtrArray(a[runtime_zero..].ptr, 3);
- expect(std.mem.eql(u8, &a, &[_]u8{ 4, 3, 2 }));
+ try expect(std.mem.eql(u8, &a, &[_]u8{ 4, 3, 2 }));
}
fn incrementVoidPtrArray(array: ?*c_void, len: usize) void {
@@ -521,34 +521,34 @@ test "*usize to *void" {
}
test "compile time int to ptr of function" {
- foobar(FUNCTION_CONSTANT);
+ try foobar(FUNCTION_CONSTANT);
}
pub const FUNCTION_CONSTANT = @intToPtr(PFN_void, maxInt(usize));
pub const PFN_void = fn (*c_void) callconv(.C) void;
-fn foobar(func: PFN_void) void {
- std.testing.expect(@ptrToInt(func) == maxInt(usize));
+fn foobar(func: PFN_void) !void {
+ try std.testing.expect(@ptrToInt(func) == maxInt(usize));
}
test "implicit ptr to *c_void" {
var a: u32 = 1;
var ptr: *align(@alignOf(u32)) c_void = &a;
var b: *u32 = @ptrCast(*u32, ptr);
- expect(b.* == 1);
+ try expect(b.* == 1);
var ptr2: ?*align(@alignOf(u32)) c_void = &a;
var c: *u32 = @ptrCast(*u32, ptr2.?);
- expect(c.* == 1);
+ try expect(c.* == 1);
}
test "@intCast to comptime_int" {
- expect(@intCast(comptime_int, 0) == 0);
+ try expect(@intCast(comptime_int, 0) == 0);
}
test "implicit cast comptime numbers to any type when the value fits" {
const a: u64 = 255;
var b: u8 = a;
- expect(b == 255);
+ try expect(b == 255);
}
test "@intToEnum passed a comptime_int to an enum with one item" {
@@ -556,7 +556,7 @@ test "@intToEnum passed a comptime_int to an enum with one item" {
A,
};
const x = @intToEnum(E, 0);
- expect(x == E.A);
+ try expect(x == E.A);
}
test "@intToEnum runtime to an extern enum with duplicate values" {
@@ -566,33 +566,33 @@ test "@intToEnum runtime to an extern enum with duplicate values" {
};
var a: u8 = 1;
var x = @intToEnum(E, a);
- expect(x == E.A);
- expect(x == E.B);
+ try expect(x == E.A);
+ try expect(x == E.B);
}
test "@intCast to u0 and use the result" {
const S = struct {
- fn doTheTest(zero: u1, one: u1, bigzero: i32) void {
- expect((one << @intCast(u0, bigzero)) == 1);
- expect((zero << @intCast(u0, bigzero)) == 0);
+ fn doTheTest(zero: u1, one: u1, bigzero: i32) !void {
+ try expect((one << @intCast(u0, bigzero)) == 1);
+ try expect((zero << @intCast(u0, bigzero)) == 0);
}
};
- S.doTheTest(0, 1, 0);
- comptime S.doTheTest(0, 1, 0);
+ try S.doTheTest(0, 1, 0);
+ comptime try S.doTheTest(0, 1, 0);
}
test "peer type resolution: unreachable, null, slice" {
const S = struct {
- fn doTheTest(num: usize, word: []const u8) void {
+ fn doTheTest(num: usize, word: []const u8) !void {
const result = switch (num) {
0 => null,
1 => word,
else => unreachable,
};
- expect(mem.eql(u8, result.?, "hi"));
+ try expect(mem.eql(u8, result.?, "hi"));
}
};
- S.doTheTest(1, "hi");
+ try S.doTheTest(1, "hi");
}
test "peer type resolution: unreachable, error set, unreachable" {
@@ -615,17 +615,17 @@ test "peer type resolution: unreachable, error set, unreachable" {
error.FileDescriptorIncompatibleWithEpoll => unreachable,
error.Unexpected => unreachable,
};
- expect(transformed_err == error.SystemResources);
+ try expect(transformed_err == error.SystemResources);
}
test "implicit cast comptime_int to comptime_float" {
- comptime expect(@as(comptime_float, 10) == @as(f32, 10));
- expect(2 == 2.0);
+ comptime try expect(@as(comptime_float, 10) == @as(f32, 10));
+ try expect(2 == 2.0);
}
test "implicit cast *[0]T to E![]const u8" {
var x = @as(anyerror![]const u8, &[0]u8{});
- expect((x catch unreachable).len == 0);
+ try expect((x catch unreachable).len == 0);
}
test "peer cast *[0]T to E![]const T" {
@@ -633,7 +633,7 @@ test "peer cast *[0]T to E![]const T" {
var buf: anyerror![]const u8 = buffer[0..];
var b = false;
var y = if (b) &[0]u8{} else buf;
- expect(mem.eql(u8, "abcde", y catch unreachable));
+ try expect(mem.eql(u8, "abcde", y catch unreachable));
}
test "peer cast *[0]T to []const T" {
@@ -641,25 +641,25 @@ test "peer cast *[0]T to []const T" {
var buf: []const u8 = buffer[0..];
var b = false;
var y = if (b) &[0]u8{} else buf;
- expect(mem.eql(u8, "abcde", y));
+ try expect(mem.eql(u8, "abcde", y));
}
var global_array: [4]u8 = undefined;
test "cast from array reference to fn" {
const f = @ptrCast(fn () callconv(.C) void, &global_array);
- expect(@ptrToInt(f) == @ptrToInt(&global_array));
+ try expect(@ptrToInt(f) == @ptrToInt(&global_array));
}
test "*const [N]null u8 to ?[]const u8" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var a = "Hello";
var b: ?[]const u8 = a;
- expect(mem.eql(u8, b.?, "Hello"));
+ try expect(mem.eql(u8, b.?, "Hello"));
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "peer resolution of string literals" {
@@ -671,54 +671,54 @@ test "peer resolution of string literals" {
d,
};
- fn doTheTest(e: E) void {
+ fn doTheTest(e: E) !void {
const cmd = switch (e) {
.a => "one",
.b => "two",
.c => "three",
.d => "four",
};
- expect(mem.eql(u8, cmd, "two"));
+ try expect(mem.eql(u8, cmd, "two"));
}
};
- S.doTheTest(.b);
- comptime S.doTheTest(.b);
+ try S.doTheTest(.b);
+ comptime try S.doTheTest(.b);
}
test "type coercion related to sentinel-termination" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
// [:x]T to []T
{
var array = [4:0]i32{ 1, 2, 3, 4 };
var slice: [:0]i32 = &array;
var dest: []i32 = slice;
- expect(mem.eql(i32, dest, &[_]i32{ 1, 2, 3, 4 }));
+ try expect(mem.eql(i32, dest, &[_]i32{ 1, 2, 3, 4 }));
}
// [*:x]T to [*]T
{
var array = [4:99]i32{ 1, 2, 3, 4 };
var dest: [*]i32 = &array;
- expect(dest[0] == 1);
- expect(dest[1] == 2);
- expect(dest[2] == 3);
- expect(dest[3] == 4);
- expect(dest[4] == 99);
+ try expect(dest[0] == 1);
+ try expect(dest[1] == 2);
+ try expect(dest[2] == 3);
+ try expect(dest[3] == 4);
+ try expect(dest[4] == 99);
}
// [N:x]T to [N]T
{
var array = [4:0]i32{ 1, 2, 3, 4 };
var dest: [4]i32 = array;
- expect(mem.eql(i32, &dest, &[_]i32{ 1, 2, 3, 4 }));
+ try expect(mem.eql(i32, &dest, &[_]i32{ 1, 2, 3, 4 }));
}
// *[N:x]T to *[N]T
{
var array = [4:0]i32{ 1, 2, 3, 4 };
var dest: *[4]i32 = &array;
- expect(mem.eql(i32, dest, &[_]i32{ 1, 2, 3, 4 }));
+ try expect(mem.eql(i32, dest, &[_]i32{ 1, 2, 3, 4 }));
}
// [:x]T to [*:x]T
@@ -726,24 +726,24 @@ test "type coercion related to sentinel-termination" {
var array = [4:0]i32{ 1, 2, 3, 4 };
var slice: [:0]i32 = &array;
var dest: [*:0]i32 = slice;
- expect(dest[0] == 1);
- expect(dest[1] == 2);
- expect(dest[2] == 3);
- expect(dest[3] == 4);
- expect(dest[4] == 0);
+ try expect(dest[0] == 1);
+ try expect(dest[1] == 2);
+ try expect(dest[2] == 3);
+ try expect(dest[3] == 4);
+ try expect(dest[4] == 0);
}
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "cast i8 fn call peers to i32 result" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var cond = true;
const value: i32 = if (cond) smallBoi() else bigBoi();
- expect(value == 123);
+ try expect(value == 123);
}
fn smallBoi() i8 {
return 123;
@@ -752,21 +752,21 @@ test "cast i8 fn call peers to i32 result" {
return 1234;
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "return u8 coercing into ?u32 return type" {
const S = struct {
- fn doTheTest() void {
- expect(foo(123).? == 123);
+ fn doTheTest() !void {
+ try expect(foo(123).? == 123);
}
fn foo(arg: u8) ?u32 {
return arg;
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "peer result null and comptime_int" {
@@ -782,17 +782,17 @@ test "peer result null and comptime_int" {
}
};
- expect(S.blah(0) == null);
- comptime expect(S.blah(0) == null);
- expect(S.blah(10).? == 1);
- comptime expect(S.blah(10).? == 1);
- expect(S.blah(-10).? == -1);
- comptime expect(S.blah(-10).? == -1);
+ try expect(S.blah(0) == null);
+ comptime try expect(S.blah(0) == null);
+ try expect(S.blah(10).? == 1);
+ comptime try expect(S.blah(10).? == 1);
+ try expect(S.blah(-10).? == -1);
+ comptime try expect(S.blah(-10).? == -1);
}
test "peer type resolution implicit cast to return type" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
for ("hello") |c| _ = f(c);
}
fn f(c: u8) []const u8 {
@@ -803,13 +803,13 @@ test "peer type resolution implicit cast to return type" {
};
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "peer type resolution implicit cast to variable type" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var x: []const u8 = undefined;
for ("hello") |c| x = switch (c) {
'h', 'e' => &[_]u8{c}, // should cast to slice
@@ -818,14 +818,14 @@ test "peer type resolution implicit cast to variable type" {
};
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "variable initialization uses result locations properly with regards to the type" {
var b = true;
const x: i32 = if (b) 1 else 2;
- expect(x == 1);
+ try expect(x == 1);
}
test "cast between [*c]T and ?[*:0]T on fn parameter" {
@@ -847,27 +847,27 @@ test "cast between C pointer with different but compatible types" {
fn foo(arg: [*]c_ushort) u16 {
return arg[0];
}
- fn doTheTest() void {
+ fn doTheTest() !void {
var x = [_]u16{ 4, 2, 1, 3 };
- expect(foo(@ptrCast([*]u16, &x)) == 4);
+ try expect(foo(@ptrCast([*]u16, &x)) == 4);
}
};
- S.doTheTest();
+ try S.doTheTest();
}
var global_struct: struct { f0: usize } = undefined;
test "assignment to optional pointer result loc" {
var foo: struct { ptr: ?*c_void } = .{ .ptr = &global_struct };
- expect(foo.ptr.? == @ptrCast(*c_void, &global_struct));
+ try expect(foo.ptr.? == @ptrCast(*c_void, &global_struct));
}
test "peer type resolve string lit with sentinel-terminated mutable slice" {
var array: [4:0]u8 = undefined;
array[4] = 0; // TODO remove this when #4372 is solved
var slice: [:0]u8 = array[0..4 :0];
- comptime expect(@TypeOf(slice, "hi") == [:0]const u8);
- comptime expect(@TypeOf("hi", slice) == [:0]const u8);
+ comptime try expect(@TypeOf(slice, "hi") == [:0]const u8);
+ comptime try expect(@TypeOf("hi", slice) == [:0]const u8);
}
test "peer type unsigned int to signed" {
@@ -875,15 +875,15 @@ test "peer type unsigned int to signed" {
var x: u8 = 7;
var y: i32 = -5;
var a = w + y + x;
- comptime expect(@TypeOf(a) == i32);
- expect(a == 7);
+ comptime try expect(@TypeOf(a) == i32);
+ try expect(a == 7);
}
test "peer type resolve array pointers, one of them const" {
var array1: [4]u8 = undefined;
const array2: [5]u8 = undefined;
- comptime expect(@TypeOf(&array1, &array2) == []const u8);
- comptime expect(@TypeOf(&array2, &array1) == []const u8);
+ comptime try expect(@TypeOf(&array1, &array2) == []const u8);
+ comptime try expect(@TypeOf(&array2, &array1) == []const u8);
}
test "peer type resolve array pointer and unknown pointer" {
@@ -892,35 +892,35 @@ test "peer type resolve array pointer and unknown pointer" {
var const_ptr: [*]const u8 = undefined;
var ptr: [*]u8 = undefined;
- comptime expect(@TypeOf(&array, ptr) == [*]u8);
- comptime expect(@TypeOf(ptr, &array) == [*]u8);
+ comptime try expect(@TypeOf(&array, ptr) == [*]u8);
+ comptime try expect(@TypeOf(ptr, &array) == [*]u8);
- comptime expect(@TypeOf(&const_array, ptr) == [*]const u8);
- comptime expect(@TypeOf(ptr, &const_array) == [*]const u8);
+ comptime try expect(@TypeOf(&const_array, ptr) == [*]const u8);
+ comptime try expect(@TypeOf(ptr, &const_array) == [*]const u8);
- comptime expect(@TypeOf(&array, const_ptr) == [*]const u8);
- comptime expect(@TypeOf(const_ptr, &array) == [*]const u8);
+ comptime try expect(@TypeOf(&array, const_ptr) == [*]const u8);
+ comptime try expect(@TypeOf(const_ptr, &array) == [*]const u8);
- comptime expect(@TypeOf(&const_array, const_ptr) == [*]const u8);
- comptime expect(@TypeOf(const_ptr, &const_array) == [*]const u8);
+ comptime try expect(@TypeOf(&const_array, const_ptr) == [*]const u8);
+ comptime try expect(@TypeOf(const_ptr, &const_array) == [*]const u8);
}
test "comptime float casts" {
const a = @intToFloat(comptime_float, 1);
- expect(a == 1);
- expect(@TypeOf(a) == comptime_float);
+ try expect(a == 1);
+ try expect(@TypeOf(a) == comptime_float);
const b = @floatToInt(comptime_int, 2);
- expect(b == 2);
- expect(@TypeOf(b) == comptime_int);
+ try expect(b == 2);
+ try expect(@TypeOf(b) == comptime_int);
}
test "cast from ?[*]T to ??[*]T" {
const a: ??[*]u8 = @as(?[*]u8, null);
- expect(a != null and a.? == null);
+ try expect(a != null and a.? == null);
}
test "cast between *[N]void and []void" {
var a: [4]void = undefined;
var b: []void = &a;
- expect(b.len == 4);
+ try expect(b.len == 4);
}
test/stage1/behavior/const_slice_child.zig
@@ -12,24 +12,24 @@ test "const slice child" {
"three",
};
argv = &strs;
- bar(strs.len);
+ try bar(strs.len);
}
-fn foo(args: [][]const u8) void {
- expect(args.len == 3);
- expect(streql(args[0], "one"));
- expect(streql(args[1], "two"));
- expect(streql(args[2], "three"));
+fn foo(args: [][]const u8) !void {
+ try expect(args.len == 3);
+ try expect(streql(args[0], "one"));
+ try expect(streql(args[1], "two"));
+ try expect(streql(args[2], "three"));
}
-fn bar(argc: usize) void {
+fn bar(argc: usize) !void {
const args = testing.allocator.alloc([]const u8, argc) catch unreachable;
defer testing.allocator.free(args);
for (args) |_, i| {
const ptr = argv[i];
args[i] = ptr[0..strlen(ptr)];
}
- foo(args);
+ try foo(args);
}
fn strlen(ptr: [*]const u8) usize {
test/stage1/behavior/defer.zig
@@ -24,18 +24,18 @@ fn runSomeErrorDefers(x: bool) !bool {
}
test "mixing normal and error defers" {
- expect(runSomeErrorDefers(true) catch unreachable);
- expect(result[0] == 'c');
- expect(result[1] == 'a');
+ try expect(runSomeErrorDefers(true) catch unreachable);
+ try expect(result[0] == 'c');
+ try expect(result[1] == 'a');
const ok = runSomeErrorDefers(false) catch |err| x: {
- expect(err == error.FalseNotAllowed);
+ try expect(err == error.FalseNotAllowed);
break :x true;
};
- expect(ok);
- expect(result[0] == 'c');
- expect(result[1] == 'b');
- expect(result[2] == 'a');
+ try expect(ok);
+ try expect(result[0] == 'c');
+ try expect(result[1] == 'b');
+ try expect(result[2] == 'a');
}
test "break and continue inside loop inside defer expression" {
@@ -50,7 +50,7 @@ fn testBreakContInDefer(x: usize) void {
if (i < 5) continue;
if (i == 5) break;
}
- expect(i == 5);
+ expect(i == 5) catch @panic("test failure");
}
}
@@ -62,11 +62,11 @@ test "defer and labeled break" {
break :blk;
}
- expect(i == 1);
+ try expect(i == 1);
}
test "errdefer does not apply to fn inside fn" {
- if (testNestedFnErrDefer()) |_| @panic("expected error") else |e| expect(e == error.Bad);
+ if (testNestedFnErrDefer()) |_| @panic("expected error") else |e| try expect(e == error.Bad);
}
fn testNestedFnErrDefer() anyerror!void {
@@ -82,8 +82,8 @@ fn testNestedFnErrDefer() anyerror!void {
test "return variable while defer expression in scope to modify it" {
const S = struct {
- fn doTheTest() void {
- expect(notNull().? == 1);
+ fn doTheTest() !void {
+ try expect(notNull().? == 1);
}
fn notNull() ?u8 {
@@ -93,22 +93,22 @@ test "return variable while defer expression in scope to modify it" {
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "errdefer with payload" {
const S = struct {
fn foo() !i32 {
errdefer |a| {
- expectEqual(error.One, a);
+ expectEqual(error.One, a) catch @panic("test failure");
}
return error.One;
}
- fn doTheTest() void {
- expectError(error.One, foo());
+ fn doTheTest() !void {
+ try expectError(error.One, foo());
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test/stage1/behavior/enum.zig
@@ -29,41 +29,41 @@ test "non-exhaustive enum" {
b,
_,
};
- fn doTheTest(y: u8) void {
+ fn doTheTest(y: u8) !void {
var e: E = .b;
- expect(switch (e) {
+ try expect(switch (e) {
.a => false,
.b => true,
_ => false,
});
e = @intToEnum(E, 12);
- expect(switch (e) {
+ try expect(switch (e) {
.a => false,
.b => false,
_ => true,
});
- expect(switch (e) {
+ try expect(switch (e) {
.a => false,
.b => false,
else => true,
});
e = .b;
- expect(switch (e) {
+ try expect(switch (e) {
.a => false,
else => true,
});
- expect(@typeInfo(E).Enum.fields.len == 2);
+ try expect(@typeInfo(E).Enum.fields.len == 2);
e = @intToEnum(E, 12);
- expect(@enumToInt(e) == 12);
+ try expect(@enumToInt(e) == 12);
e = @intToEnum(E, y);
- expect(@enumToInt(e) == 52);
- expect(@typeInfo(E).Enum.is_exhaustive == false);
+ try expect(@enumToInt(e) == 52);
+ try expect(@typeInfo(E).Enum.is_exhaustive == false);
}
};
- S.doTheTest(52);
- comptime S.doTheTest(52);
+ try S.doTheTest(52);
+ comptime try S.doTheTest(52);
}
test "empty non-exhaustive enum" {
@@ -71,19 +71,19 @@ test "empty non-exhaustive enum" {
const E = enum(u8) {
_,
};
- fn doTheTest(y: u8) void {
+ fn doTheTest(y: u8) !void {
var e = @intToEnum(E, y);
- expect(switch (e) {
+ try expect(switch (e) {
_ => true,
});
- expect(@enumToInt(e) == y);
+ try expect(@enumToInt(e) == y);
- expect(@typeInfo(E).Enum.fields.len == 0);
- expect(@typeInfo(E).Enum.is_exhaustive == false);
+ try expect(@typeInfo(E).Enum.fields.len == 0);
+ try expect(@typeInfo(E).Enum.is_exhaustive == false);
}
};
- S.doTheTest(42);
- comptime S.doTheTest(42);
+ try S.doTheTest(42);
+ comptime try S.doTheTest(42);
}
test "single field non-exhaustive enum" {
@@ -92,35 +92,35 @@ test "single field non-exhaustive enum" {
a,
_,
};
- fn doTheTest(y: u8) void {
+ fn doTheTest(y: u8) !void {
var e: E = .a;
- expect(switch (e) {
+ try expect(switch (e) {
.a => true,
_ => false,
});
e = @intToEnum(E, 12);
- expect(switch (e) {
+ try expect(switch (e) {
.a => false,
_ => true,
});
- expect(switch (e) {
+ try expect(switch (e) {
.a => false,
else => true,
});
e = .a;
- expect(switch (e) {
+ try expect(switch (e) {
.a => true,
else => false,
});
- expect(@enumToInt(@intToEnum(E, y)) == y);
- expect(@typeInfo(E).Enum.fields.len == 1);
- expect(@typeInfo(E).Enum.is_exhaustive == false);
+ try expect(@enumToInt(@intToEnum(E, y)) == y);
+ try expect(@typeInfo(E).Enum.fields.len == 1);
+ try expect(@typeInfo(E).Enum.is_exhaustive == false);
}
};
- S.doTheTest(23);
- comptime S.doTheTest(23);
+ try S.doTheTest(23);
+ comptime try S.doTheTest(23);
}
test "enum type" {
@@ -133,16 +133,16 @@ test "enum type" {
};
const bar = Bar.B;
- expect(bar == Bar.B);
- expect(@typeInfo(Foo).Union.fields.len == 3);
- expect(@typeInfo(Bar).Enum.fields.len == 4);
- expect(@sizeOf(Foo) == @sizeOf(FooNoVoid));
- expect(@sizeOf(Bar) == 1);
+ try expect(bar == Bar.B);
+ try expect(@typeInfo(Foo).Union.fields.len == 3);
+ try expect(@typeInfo(Bar).Enum.fields.len == 4);
+ try expect(@sizeOf(Foo) == @sizeOf(FooNoVoid));
+ try expect(@sizeOf(Bar) == 1);
}
test "enum as return value" {
switch (returnAnInt(13)) {
- Foo.One => |value| expect(value == 13),
+ Foo.One => |value| try expect(value == 13),
else => unreachable,
}
}
@@ -206,22 +206,22 @@ const Number = enum {
};
test "enum to int" {
- shouldEqual(Number.Zero, 0);
- shouldEqual(Number.One, 1);
- shouldEqual(Number.Two, 2);
- shouldEqual(Number.Three, 3);
- shouldEqual(Number.Four, 4);
+ try shouldEqual(Number.Zero, 0);
+ try shouldEqual(Number.One, 1);
+ try shouldEqual(Number.Two, 2);
+ try shouldEqual(Number.Three, 3);
+ try shouldEqual(Number.Four, 4);
}
-fn shouldEqual(n: Number, expected: u3) void {
- expect(@enumToInt(n) == expected);
+fn shouldEqual(n: Number, expected: u3) !void {
+ try expect(@enumToInt(n) == expected);
}
test "int to enum" {
- testIntToEnumEval(3);
+ try testIntToEnumEval(3);
}
-fn testIntToEnumEval(x: i32) void {
- expect(@intToEnum(IntToEnumNumber, @intCast(u3, x)) == IntToEnumNumber.Three);
+fn testIntToEnumEval(x: i32) !void {
+ try expect(@intToEnum(IntToEnumNumber, @intCast(u3, x)) == IntToEnumNumber.Three);
}
const IntToEnumNumber = enum {
Zero,
@@ -232,18 +232,18 @@ const IntToEnumNumber = enum {
};
test "@tagName" {
- expect(mem.eql(u8, testEnumTagNameBare(BareNumber.Three), "Three"));
- comptime expect(mem.eql(u8, testEnumTagNameBare(BareNumber.Three), "Three"));
+ try expect(mem.eql(u8, testEnumTagNameBare(BareNumber.Three), "Three"));
+ comptime try expect(mem.eql(u8, testEnumTagNameBare(BareNumber.Three), "Three"));
}
test "@tagName extern enum with duplicates" {
- expect(mem.eql(u8, testEnumTagNameBare(ExternDuplicates.B), "A"));
- comptime expect(mem.eql(u8, testEnumTagNameBare(ExternDuplicates.B), "A"));
+ try expect(mem.eql(u8, testEnumTagNameBare(ExternDuplicates.B), "A"));
+ comptime try expect(mem.eql(u8, testEnumTagNameBare(ExternDuplicates.B), "A"));
}
test "@tagName non-exhaustive enum" {
- expect(mem.eql(u8, testEnumTagNameBare(NonExhaustive.B), "B"));
- comptime expect(mem.eql(u8, testEnumTagNameBare(NonExhaustive.B), "B"));
+ try expect(mem.eql(u8, testEnumTagNameBare(NonExhaustive.B), "B"));
+ comptime try expect(mem.eql(u8, testEnumTagNameBare(NonExhaustive.B), "B"));
}
fn testEnumTagNameBare(n: anytype) []const u8 {
@@ -269,8 +269,8 @@ const NonExhaustive = enum(u8) {
test "enum alignment" {
comptime {
- expect(@alignOf(AlignTestEnum) >= @alignOf([9]u8));
- expect(@alignOf(AlignTestEnum) >= @alignOf(u64));
+ try expect(@alignOf(AlignTestEnum) >= @alignOf([9]u8));
+ try expect(@alignOf(AlignTestEnum) >= @alignOf(u64));
}
}
@@ -806,10 +806,10 @@ const ValueCount257 = enum {
test "enum sizes" {
comptime {
- expect(@sizeOf(ValueCount1) == 0);
- expect(@sizeOf(ValueCount2) == 1);
- expect(@sizeOf(ValueCount256) == 1);
- expect(@sizeOf(ValueCount257) == 2);
+ try expect(@sizeOf(ValueCount1) == 0);
+ try expect(@sizeOf(ValueCount2) == 1);
+ try expect(@sizeOf(ValueCount256) == 1);
+ try expect(@sizeOf(ValueCount257) == 2);
}
}
@@ -828,12 +828,12 @@ test "set enum tag type" {
{
var x = Small.One;
x = Small.Two;
- comptime expect(Tag(Small) == u2);
+ comptime try expect(Tag(Small) == u2);
}
{
var x = Small2.One;
x = Small2.Two;
- comptime expect(Tag(Small2) == u2);
+ comptime try expect(Tag(Small2) == u2);
}
}
@@ -880,17 +880,17 @@ const bit_field_1 = BitFieldOfEnums{
test "bit field access with enum fields" {
var data = bit_field_1;
- expect(getA(&data) == A.Two);
- expect(getB(&data) == B.Three3);
- expect(getC(&data) == C.Four4);
- comptime expect(@sizeOf(BitFieldOfEnums) == 1);
+ try expect(getA(&data) == A.Two);
+ try expect(getB(&data) == B.Three3);
+ try expect(getC(&data) == C.Four4);
+ comptime try expect(@sizeOf(BitFieldOfEnums) == 1);
data.b = B.Four3;
- expect(data.b == B.Four3);
+ try expect(data.b == B.Four3);
data.a = A.Three;
- expect(data.a == A.Three);
- expect(data.b == B.Four3);
+ try expect(data.a == A.Three);
+ try expect(data.b == B.Four3);
}
fn getA(data: *const BitFieldOfEnums) A {
@@ -906,12 +906,12 @@ fn getC(data: *const BitFieldOfEnums) C {
}
test "casting enum to its tag type" {
- testCastEnumTag(Small2.Two);
- comptime testCastEnumTag(Small2.Two);
+ try testCastEnumTag(Small2.Two);
+ comptime try testCastEnumTag(Small2.Two);
}
-fn testCastEnumTag(value: Small2) void {
- expect(@enumToInt(value) == 1);
+fn testCastEnumTag(value: Small2) !void {
+ try expect(@enumToInt(value) == 1);
}
const MultipleChoice = enum(u32) {
@@ -922,13 +922,13 @@ const MultipleChoice = enum(u32) {
};
test "enum with specified tag values" {
- testEnumWithSpecifiedTagValues(MultipleChoice.C);
- comptime testEnumWithSpecifiedTagValues(MultipleChoice.C);
+ try testEnumWithSpecifiedTagValues(MultipleChoice.C);
+ comptime try testEnumWithSpecifiedTagValues(MultipleChoice.C);
}
-fn testEnumWithSpecifiedTagValues(x: MultipleChoice) void {
- expect(@enumToInt(x) == 60);
- expect(1234 == switch (x) {
+fn testEnumWithSpecifiedTagValues(x: MultipleChoice) !void {
+ try expect(@enumToInt(x) == 60);
+ try expect(1234 == switch (x) {
MultipleChoice.A => 1,
MultipleChoice.B => 2,
MultipleChoice.C => @as(u32, 1234),
@@ -949,13 +949,13 @@ const MultipleChoice2 = enum(u32) {
};
test "enum with specified and unspecified tag values" {
- testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2.D);
- comptime testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2.D);
+ try testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2.D);
+ comptime try testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2.D);
}
-fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: MultipleChoice2) void {
- expect(@enumToInt(x) == 1000);
- expect(1234 == switch (x) {
+fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: MultipleChoice2) !void {
+ try expect(@enumToInt(x) == 1000);
+ try expect(1234 == switch (x) {
MultipleChoice2.A => 1,
MultipleChoice2.B => 2,
MultipleChoice2.C => 3,
@@ -969,8 +969,8 @@ fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: MultipleChoice2) void {
}
test "cast integer literal to enum" {
- expect(@intToEnum(MultipleChoice2, 0) == MultipleChoice2.Unspecified1);
- expect(@intToEnum(MultipleChoice2, 40) == MultipleChoice2.B);
+ try expect(@intToEnum(MultipleChoice2, 0) == MultipleChoice2.Unspecified1);
+ try expect(@intToEnum(MultipleChoice2, 40) == MultipleChoice2.B);
}
const EnumWithOneMember = enum {
@@ -1008,14 +1008,14 @@ const EnumWithTagValues = enum(u4) {
D = 1 << 3,
};
test "enum with tag values don't require parens" {
- expect(@enumToInt(EnumWithTagValues.C) == 0b0100);
+ try expect(@enumToInt(EnumWithTagValues.C) == 0b0100);
}
test "enum with 1 field but explicit tag type should still have the tag type" {
const Enum = enum(u8) {
B = 2,
};
- comptime @import("std").testing.expect(@sizeOf(Enum) == @sizeOf(u8));
+ comptime try expect(@sizeOf(Enum) == @sizeOf(u8));
}
test "empty extern enum with members" {
@@ -1024,7 +1024,7 @@ test "empty extern enum with members" {
B,
C,
};
- expect(@sizeOf(E) == @sizeOf(c_int));
+ try expect(@sizeOf(E) == @sizeOf(c_int));
}
test "tag name with assigned enum values" {
@@ -1033,7 +1033,7 @@ test "tag name with assigned enum values" {
B = 0,
};
var b = LocalFoo.B;
- expect(mem.eql(u8, @tagName(b), "B"));
+ try expect(mem.eql(u8, @tagName(b), "B"));
}
test "enum literal equality" {
@@ -1041,8 +1041,8 @@ test "enum literal equality" {
const y = .ok;
const z = .hi;
- expect(x != y);
- expect(x == z);
+ try expect(x != y);
+ try expect(x == z);
}
test "enum literal cast to enum" {
@@ -1054,7 +1054,7 @@ test "enum literal cast to enum" {
var color1: Color = .Auto;
var color2 = Color.Auto;
- expect(color1 == color2);
+ try expect(color1 == color2);
}
test "peer type resolution with enum literal" {
@@ -1063,8 +1063,8 @@ test "peer type resolution with enum literal" {
two,
};
- expect(Items.two == .two);
- expect(.two == Items.two);
+ try expect(Items.two == .two);
+ try expect(.two == Items.two);
}
test "enum literal in array literal" {
@@ -1078,8 +1078,8 @@ test "enum literal in array literal" {
.two,
};
- expect(array[0] == .one);
- expect(array[1] == .two);
+ try expect(array[0] == .one);
+ try expect(array[1] == .two);
}
test "signed integer as enum tag" {
@@ -1089,9 +1089,9 @@ test "signed integer as enum tag" {
A2 = 1,
};
- expect(@enumToInt(SignedEnum.A0) == -1);
- expect(@enumToInt(SignedEnum.A1) == 0);
- expect(@enumToInt(SignedEnum.A2) == 1);
+ try expect(@enumToInt(SignedEnum.A0) == -1);
+ try expect(@enumToInt(SignedEnum.A1) == 0);
+ try expect(@enumToInt(SignedEnum.A2) == 1);
}
test "enum value allocation" {
@@ -1101,9 +1101,9 @@ test "enum value allocation" {
A2,
};
- expect(@enumToInt(LargeEnum.A0) == 0x80000000);
- expect(@enumToInt(LargeEnum.A1) == 0x80000001);
- expect(@enumToInt(LargeEnum.A2) == 0x80000002);
+ try expect(@enumToInt(LargeEnum.A0) == 0x80000000);
+ try expect(@enumToInt(LargeEnum.A1) == 0x80000001);
+ try expect(@enumToInt(LargeEnum.A2) == 0x80000002);
}
test "enum literal casting to tagged union" {
@@ -1130,32 +1130,32 @@ test "enum with one member and custom tag type" {
const E = enum(u2) {
One,
};
- expect(@enumToInt(E.One) == 0);
+ try expect(@enumToInt(E.One) == 0);
const E2 = enum(u2) {
One = 2,
};
- expect(@enumToInt(E2.One) == 2);
+ try expect(@enumToInt(E2.One) == 2);
}
test "enum literal casting to optional" {
var bar: ?Bar = undefined;
bar = .B;
- expect(bar.? == Bar.B);
+ try expect(bar.? == Bar.B);
}
test "enum literal casting to error union with payload enum" {
var bar: error{B}!Bar = undefined;
bar = .B; // should never cast to the error set
- expect((try bar) == Bar.B);
+ try expect((try bar) == Bar.B);
}
test "enum with one member and u1 tag type @enumToInt" {
const Enum = enum(u1) {
Test,
};
- expect(@enumToInt(Enum.Test) == 0);
+ try expect(@enumToInt(Enum.Test) == 0);
}
test "enum with comptime_int tag type" {
@@ -1164,19 +1164,19 @@ test "enum with comptime_int tag type" {
Two = 2,
Three = 1,
};
- comptime expect(Tag(Enum) == comptime_int);
+ comptime try expect(Tag(Enum) == comptime_int);
}
test "enum with one member default to u0 tag type" {
const E0 = enum {
X,
};
- comptime expect(Tag(E0) == u0);
+ comptime try expect(Tag(E0) == u0);
}
test "tagName on enum literals" {
- expect(mem.eql(u8, @tagName(.FooBar), "FooBar"));
- comptime expect(mem.eql(u8, @tagName(.FooBar), "FooBar"));
+ try expect(mem.eql(u8, @tagName(.FooBar), "FooBar"));
+ comptime try expect(mem.eql(u8, @tagName(.FooBar), "FooBar"));
}
test "method call on an enum" {
@@ -1193,12 +1193,12 @@ test "method call on an enum" {
return self.* == .two and foo == bool;
}
};
- fn doTheTest() void {
+ fn doTheTest() !void {
var e = E.two;
- expect(e.method());
- expect(e.generic_method(bool));
+ try expect(e.method());
+ try expect(e.generic_method(bool));
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test/stage1/behavior/enum_with_members.zig
@@ -19,9 +19,9 @@ test "enum with members" {
const b = ET{ .UINT = 42 };
var buf: [20]u8 = undefined;
- expect((a.print(buf[0..]) catch unreachable) == 3);
- expect(mem.eql(u8, buf[0..3], "-42"));
+ try expect((a.print(buf[0..]) catch unreachable) == 3);
+ try expect(mem.eql(u8, buf[0..3], "-42"));
- expect((b.print(buf[0..]) catch unreachable) == 2);
- expect(mem.eql(u8, buf[0..2], "42"));
+ try expect((b.print(buf[0..]) catch unreachable) == 2);
+ try expect(mem.eql(u8, buf[0..2], "42"));
}
test/stage1/behavior/error.zig
@@ -19,7 +19,7 @@ pub fn baz() anyerror!i32 {
}
test "error wrapping" {
- expect((baz() catch unreachable) == 15);
+ try expect((baz() catch unreachable) == 15);
}
fn gimmeItBroke() []const u8 {
@@ -27,14 +27,14 @@ fn gimmeItBroke() []const u8 {
}
test "@errorName" {
- expect(mem.eql(u8, @errorName(error.AnError), "AnError"));
- expect(mem.eql(u8, @errorName(error.ALongerErrorName), "ALongerErrorName"));
+ try expect(mem.eql(u8, @errorName(error.AnError), "AnError"));
+ try expect(mem.eql(u8, @errorName(error.ALongerErrorName), "ALongerErrorName"));
}
test "error values" {
const a = @errorToInt(error.err1);
const b = @errorToInt(error.err2);
- expect(a != b);
+ try expect(a != b);
}
test "redefinition of error values allowed" {
@@ -47,8 +47,8 @@ fn shouldBeNotEqual(a: anyerror, b: anyerror) void {
test "error binary operator" {
const a = errBinaryOperatorG(true) catch 3;
const b = errBinaryOperatorG(false) catch 3;
- expect(a == 3);
- expect(b == 10);
+ try expect(a == 3);
+ try expect(b == 10);
}
fn errBinaryOperatorG(x: bool) anyerror!isize {
return if (x) error.ItBroke else @as(isize, 10);
@@ -56,7 +56,7 @@ fn errBinaryOperatorG(x: bool) anyerror!isize {
test "unwrap simple value from error" {
const i = unwrapSimpleValueFromErrorDo() catch unreachable;
- expect(i == 13);
+ try expect(i == 13);
}
fn unwrapSimpleValueFromErrorDo() anyerror!isize {
return 13;
@@ -76,21 +76,21 @@ fn makeANonErr() anyerror!i32 {
}
test "error union type " {
- testErrorUnionType();
- comptime testErrorUnionType();
+ try testErrorUnionType();
+ comptime try testErrorUnionType();
}
-fn testErrorUnionType() void {
+fn testErrorUnionType() !void {
const x: anyerror!i32 = 1234;
- if (x) |value| expect(value == 1234) else |_| unreachable;
- expect(@typeInfo(@TypeOf(x)) == .ErrorUnion);
- expect(@typeInfo(@typeInfo(@TypeOf(x)).ErrorUnion.error_set) == .ErrorSet);
- expect(@typeInfo(@TypeOf(x)).ErrorUnion.error_set == anyerror);
+ if (x) |value| try expect(value == 1234) else |_| unreachable;
+ try expect(@typeInfo(@TypeOf(x)) == .ErrorUnion);
+ try expect(@typeInfo(@typeInfo(@TypeOf(x)).ErrorUnion.error_set) == .ErrorSet);
+ try expect(@typeInfo(@TypeOf(x)).ErrorUnion.error_set == anyerror);
}
test "error set type" {
- testErrorSetType();
- comptime testErrorSetType();
+ try testErrorSetType();
+ comptime try testErrorSetType();
}
const MyErrSet = error{
@@ -98,21 +98,21 @@ const MyErrSet = error{
FileNotFound,
};
-fn testErrorSetType() void {
- expect(@typeInfo(MyErrSet).ErrorSet.?.len == 2);
+fn testErrorSetType() !void {
+ try expect(@typeInfo(MyErrSet).ErrorSet.?.len == 2);
const a: MyErrSet!i32 = 5678;
const b: MyErrSet!i32 = MyErrSet.OutOfMemory;
- if (a) |value| expect(value == 5678) else |err| switch (err) {
+ if (a) |value| try expect(value == 5678) else |err| switch (err) {
error.OutOfMemory => unreachable,
error.FileNotFound => unreachable,
}
}
test "explicit error set cast" {
- testExplicitErrorSetCast(Set1.A);
- comptime testExplicitErrorSetCast(Set1.A);
+ try testExplicitErrorSetCast(Set1.A);
+ comptime try testExplicitErrorSetCast(Set1.A);
}
const Set1 = error{
@@ -124,26 +124,26 @@ const Set2 = error{
C,
};
-fn testExplicitErrorSetCast(set1: Set1) void {
+fn testExplicitErrorSetCast(set1: Set1) !void {
var x = @errSetCast(Set2, set1);
var y = @errSetCast(Set1, x);
- expect(y == error.A);
+ try expect(y == error.A);
}
test "comptime test error for empty error set" {
- testComptimeTestErrorEmptySet(1234);
- comptime testComptimeTestErrorEmptySet(1234);
+ try testComptimeTestErrorEmptySet(1234);
+ comptime try testComptimeTestErrorEmptySet(1234);
}
const EmptyErrorSet = error{};
-fn testComptimeTestErrorEmptySet(x: EmptyErrorSet!i32) void {
- if (x) |v| expect(v == 1234) else |err| @compileError("bad");
+fn testComptimeTestErrorEmptySet(x: EmptyErrorSet!i32) !void {
+ if (x) |v| try expect(v == 1234) else |err| @compileError("bad");
}
test "syntax: optional operator in front of error union operator" {
comptime {
- expect(?(anyerror!i32) == ?(anyerror!i32));
+ try expect(?(anyerror!i32) == ?(anyerror!i32));
}
}
@@ -165,10 +165,10 @@ test "empty error union" {
}
test "error union peer type resolution" {
- testErrorUnionPeerTypeResolution(1);
+ try testErrorUnionPeerTypeResolution(1);
}
-fn testErrorUnionPeerTypeResolution(x: i32) void {
+fn testErrorUnionPeerTypeResolution(x: i32) !void {
const y = switch (x) {
1 => bar_1(),
2 => baz_1(),
@@ -177,7 +177,7 @@ fn testErrorUnionPeerTypeResolution(x: i32) void {
if (y) |_| {
@panic("expected error");
} else |e| {
- expect(e == error.A);
+ try expect(e == error.A);
}
}
@@ -286,13 +286,13 @@ test "nested error union function call in optional unwrap" {
return null;
}
};
- expect((try S.errorable()) == 1234);
- expectError(error.Failure, S.errorable2());
- expectError(error.Other, S.errorable3());
+ try expect((try S.errorable()) == 1234);
+ try expectError(error.Failure, S.errorable2());
+ try expectError(error.Other, S.errorable3());
comptime {
- expect((try S.errorable()) == 1234);
- expectError(error.Failure, S.errorable2());
- expectError(error.Other, S.errorable3());
+ try expect((try S.errorable()) == 1234);
+ try expectError(error.Failure, S.errorable2());
+ try expectError(error.Other, S.errorable3());
}
}
@@ -307,7 +307,7 @@ test "widen cast integer payload of error union function call" {
return 1234;
}
};
- expect((try S.errorable()) == 1234);
+ try expect((try S.errorable()) == 1234);
}
test "return function call to error set from error union function" {
@@ -320,19 +320,19 @@ test "return function call to error set from error union function" {
return error.Failure;
}
};
- expectError(error.Failure, S.errorable());
- comptime expectError(error.Failure, S.errorable());
+ try expectError(error.Failure, S.errorable());
+ comptime try expectError(error.Failure, S.errorable());
}
test "optional error set is the same size as error set" {
- comptime expect(@sizeOf(?anyerror) == @sizeOf(anyerror));
+ comptime try expect(@sizeOf(?anyerror) == @sizeOf(anyerror));
const S = struct {
fn returnsOptErrSet() ?anyerror {
return null;
}
};
- expect(S.returnsOptErrSet() == null);
- comptime expect(S.returnsOptErrSet() == null);
+ try expect(S.returnsOptErrSet() == null);
+ comptime try expect(S.returnsOptErrSet() == null);
}
test "debug info for optional error set" {
@@ -342,8 +342,8 @@ test "debug info for optional error set" {
test "nested catch" {
const S = struct {
- fn entry() void {
- expectError(error.Bad, func());
+ fn entry() !void {
+ try expectError(error.Bad, func());
}
fn fail() anyerror!Foo {
return error.Wrong;
@@ -358,16 +358,16 @@ test "nested catch" {
field: i32,
};
};
- S.entry();
- comptime S.entry();
+ try S.entry();
+ comptime try S.entry();
}
test "implicit cast to optional to error union to return result loc" {
const S = struct {
- fn entry() void {
+ fn entry() !void {
var x: Foo = undefined;
if (func(&x)) |opt| {
- expect(opt != null);
+ try expect(opt != null);
} else |_| @panic("expected non error");
}
fn func(f: *Foo) anyerror!?*Foo {
@@ -377,7 +377,7 @@ test "implicit cast to optional to error union to return result loc" {
field: i32,
};
};
- S.entry();
+ try S.entry();
//comptime S.entry(); TODO
}
@@ -393,23 +393,23 @@ test "function pointer with return type that is error union with payload which i
return Err.UnspecifiedErr;
}
- fn doTheTest() void {
+ fn doTheTest() !void {
var x = Foo{ .fun = bar };
- expectError(error.UnspecifiedErr, x.fun(1));
+ try expectError(error.UnspecifiedErr, x.fun(1));
}
};
- S.doTheTest();
+ try S.doTheTest();
}
test "return result loc as peer result loc in inferred error set function" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
if (foo(2)) |x| {
- expect(x.Two);
+ try expect(x.Two);
} else |e| switch (e) {
error.Whatever => @panic("fail"),
}
- expectError(error.Whatever, foo(99));
+ try expectError(error.Whatever, foo(99));
}
const FormValue = union(enum) {
One: void,
@@ -424,8 +424,8 @@ test "return result loc as peer result loc in inferred error set function" {
};
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "error payload type is correctly resolved" {
@@ -439,7 +439,7 @@ test "error payload type is correctly resolved" {
}
};
- expectEqual(MyIntWrapper{ .x = 42 }, try MyIntWrapper.create());
+ try expectEqual(MyIntWrapper{ .x = 42 }, try MyIntWrapper.create());
}
test "error union comptime caching" {
@@ -449,4 +449,4 @@ test "error union comptime caching" {
S.foo(@as(anyerror!void, {}));
S.foo(@as(anyerror!void, {}));
-}
\ No newline at end of file
+}
test/stage1/behavior/eval.zig
@@ -4,7 +4,7 @@ const expectEqual = std.testing.expectEqual;
const builtin = @import("builtin");
test "compile time recursion" {
- expect(some_data.len == 21);
+ try expect(some_data.len == 21);
}
var some_data: [@intCast(usize, fibonacci(7))]u8 = undefined;
fn fibonacci(x: i32) i32 {
@@ -17,7 +17,7 @@ fn unwrapAndAddOne(blah: ?i32) i32 {
}
const should_be_1235 = unwrapAndAddOne(1234);
test "static add one" {
- expect(should_be_1235 == 1235);
+ try expect(should_be_1235 == 1235);
}
test "inlined loop" {
@@ -25,7 +25,7 @@ test "inlined loop" {
comptime var sum = 0;
inline while (i <= 5) : (i += 1)
sum += i;
- expect(sum == 15);
+ try expect(sum == 15);
}
fn gimme1or2(comptime a: bool) i32 {
@@ -35,12 +35,12 @@ fn gimme1or2(comptime a: bool) i32 {
return z;
}
test "inline variable gets result of const if" {
- expect(gimme1or2(true) == 1);
- expect(gimme1or2(false) == 2);
+ try expect(gimme1or2(true) == 1);
+ try expect(gimme1or2(false) == 2);
}
test "static function evaluation" {
- expect(statically_added_number == 3);
+ try expect(statically_added_number == 3);
}
const statically_added_number = staticAdd(1, 2);
fn staticAdd(a: i32, b: i32) i32 {
@@ -48,8 +48,8 @@ fn staticAdd(a: i32, b: i32) i32 {
}
test "const expr eval on single expr blocks" {
- expect(constExprEvalOnSingleExprBlocksFn(1, true) == 3);
- comptime expect(constExprEvalOnSingleExprBlocksFn(1, true) == 3);
+ try expect(constExprEvalOnSingleExprBlocksFn(1, true) == 3);
+ comptime try expect(constExprEvalOnSingleExprBlocksFn(1, true) == 3);
}
fn constExprEvalOnSingleExprBlocksFn(x: i32, b: bool) i32 {
@@ -65,10 +65,10 @@ fn constExprEvalOnSingleExprBlocksFn(x: i32, b: bool) i32 {
}
test "statically initialized list" {
- expect(static_point_list[0].x == 1);
- expect(static_point_list[0].y == 2);
- expect(static_point_list[1].x == 3);
- expect(static_point_list[1].y == 4);
+ try expect(static_point_list[0].x == 1);
+ try expect(static_point_list[0].y == 2);
+ try expect(static_point_list[1].x == 3);
+ try expect(static_point_list[1].y == 4);
}
const Point = struct {
x: i32,
@@ -86,8 +86,8 @@ fn makePoint(x: i32, y: i32) Point {
}
test "static eval list init" {
- expect(static_vec3.data[2] == 1.0);
- expect(vec3(0.0, 0.0, 3.0).data[2] == 3.0);
+ try expect(static_vec3.data[2] == 1.0);
+ try expect(vec3(0.0, 0.0, 3.0).data[2] == 3.0);
}
const static_vec3 = vec3(0.0, 0.0, 1.0);
pub const Vec3 = struct {
@@ -105,12 +105,12 @@ pub fn vec3(x: f32, y: f32, z: f32) Vec3 {
test "constant expressions" {
var array: [array_size]u8 = undefined;
- expect(@sizeOf(@TypeOf(array)) == 20);
+ try expect(@sizeOf(@TypeOf(array)) == 20);
}
const array_size: u8 = 20;
test "constant struct with negation" {
- expect(vertices[0].x == -0.6);
+ try expect(vertices[0].x == -0.6);
}
const Vertex = struct {
x: f32,
@@ -145,7 +145,7 @@ const vertices = [_]Vertex{
test "statically initialized struct" {
st_init_str_foo.x += 1;
- expect(st_init_str_foo.x == 14);
+ try expect(st_init_str_foo.x == 14);
}
const StInitStrFoo = struct {
x: i32,
@@ -158,7 +158,7 @@ var st_init_str_foo = StInitStrFoo{
test "statically initalized array literal" {
const y: [4]u8 = st_init_arr_lit_x;
- expect(y[3] == 4);
+ try expect(y[3] == 4);
}
const st_init_arr_lit_x = [_]u8{
1,
@@ -170,15 +170,15 @@ const st_init_arr_lit_x = [_]u8{
test "const slice" {
comptime {
const a = "1234567890";
- expect(a.len == 10);
+ try expect(a.len == 10);
const b = a[1..2];
- expect(b.len == 1);
- expect(b[0] == '2');
+ try expect(b.len == 1);
+ try expect(b[0] == '2');
}
}
test "try to trick eval with runtime if" {
- expect(testTryToTrickEvalWithRuntimeIf(true) == 10);
+ try expect(testTryToTrickEvalWithRuntimeIf(true) == 10);
}
fn testTryToTrickEvalWithRuntimeIf(b: bool) usize {
@@ -198,7 +198,7 @@ test "inlined loop has array literal with elided runtime scope on first iteratio
const result = if (i == 0) [1]i32{2} else runtime;
}
comptime {
- expect(i == 2);
+ try expect(i == 2);
}
}
@@ -215,16 +215,16 @@ fn letsTryToCompareBools(a: bool, b: bool) bool {
return max(bool, a, b);
}
test "inlined block and runtime block phi" {
- expect(letsTryToCompareBools(true, true));
- expect(letsTryToCompareBools(true, false));
- expect(letsTryToCompareBools(false, true));
- expect(!letsTryToCompareBools(false, false));
+ try expect(letsTryToCompareBools(true, true));
+ try expect(letsTryToCompareBools(true, false));
+ try expect(letsTryToCompareBools(false, true));
+ try expect(!letsTryToCompareBools(false, false));
comptime {
- expect(letsTryToCompareBools(true, true));
- expect(letsTryToCompareBools(true, false));
- expect(letsTryToCompareBools(false, true));
- expect(!letsTryToCompareBools(false, false));
+ try expect(letsTryToCompareBools(true, true));
+ try expect(letsTryToCompareBools(true, false));
+ try expect(letsTryToCompareBools(false, true));
+ try expect(!letsTryToCompareBools(false, false));
}
}
@@ -269,14 +269,14 @@ fn performFn(comptime prefix_char: u8, start_value: i32) i32 {
}
test "comptime iterate over fn ptr list" {
- expect(performFn('t', 1) == 6);
- expect(performFn('o', 0) == 1);
- expect(performFn('w', 99) == 99);
+ try expect(performFn('t', 1) == 6);
+ try expect(performFn('o', 0) == 1);
+ try expect(performFn('w', 99) == 99);
}
test "eval @setRuntimeSafety at compile-time" {
const result = comptime fnWithSetRuntimeSafety();
- expect(result == 1234);
+ try expect(result == 1234);
}
fn fnWithSetRuntimeSafety() i32 {
@@ -286,7 +286,7 @@ fn fnWithSetRuntimeSafety() i32 {
test "eval @setFloatMode at compile-time" {
const result = comptime fnWithFloatMode();
- expect(result == 1234.0);
+ try expect(result == 1234.0);
}
fn fnWithFloatMode() f32 {
@@ -307,15 +307,15 @@ var simple_struct = SimpleStruct{ .field = 1234 };
const bound_fn = simple_struct.method;
test "call method on bound fn referring to var instance" {
- expect(bound_fn() == 1237);
+ try expect(bound_fn() == 1237);
}
test "ptr to local array argument at comptime" {
comptime {
var bytes: [10]u8 = undefined;
modifySomeBytes(bytes[0..]);
- expect(bytes[0] == 'a');
- expect(bytes[9] == 'b');
+ try expect(bytes[0] == 'a');
+ try expect(bytes[9] == 'b');
}
}
@@ -343,9 +343,9 @@ fn testCompTimeUIntComparisons(x: u32) void {
}
test "const ptr to variable data changes at runtime" {
- expect(foo_ref.name[0] == 'a');
+ try expect(foo_ref.name[0] == 'a');
foo_ref.name = "b";
- expect(foo_ref.name[0] == 'b');
+ try expect(foo_ref.name[0] == 'b');
}
const Foo = struct {
@@ -356,8 +356,8 @@ var foo_contents = Foo{ .name = "a" };
const foo_ref = &foo_contents;
test "create global array with for loop" {
- expect(global_array[5] == 5 * 5);
- expect(global_array[9] == 9 * 9);
+ try expect(global_array[5] == 5 * 5);
+ try expect(global_array[9] == 9 * 9);
}
const global_array = x: {
@@ -372,18 +372,18 @@ test "compile-time downcast when the bits fit" {
comptime {
const spartan_count: u16 = 255;
const byte = @intCast(u8, spartan_count);
- expect(byte == 255);
+ try expect(byte == 255);
}
}
const hi1 = "hi";
const hi2 = hi1;
test "const global shares pointer with other same one" {
- assertEqualPtrs(&hi1[0], &hi2[0]);
- comptime expect(&hi1[0] == &hi2[0]);
+ try assertEqualPtrs(&hi1[0], &hi2[0]);
+ comptime try expect(&hi1[0] == &hi2[0]);
}
-fn assertEqualPtrs(ptr1: *const u8, ptr2: *const u8) void {
- expect(ptr1 == ptr2);
+fn assertEqualPtrs(ptr1: *const u8, ptr2: *const u8) !void {
+ try expect(ptr1 == ptr2);
}
test "@setEvalBranchQuota" {
@@ -395,29 +395,29 @@ test "@setEvalBranchQuota" {
while (i < 1001) : (i += 1) {
sum += i;
}
- expect(sum == 500500);
+ try expect(sum == 500500);
}
}
test "float literal at compile time not lossy" {
- expect(16777216.0 + 1.0 == 16777217.0);
- expect(9007199254740992.0 + 1.0 == 9007199254740993.0);
+ try expect(16777216.0 + 1.0 == 16777217.0);
+ try expect(9007199254740992.0 + 1.0 == 9007199254740993.0);
}
test "f32 at compile time is lossy" {
- expect(@as(f32, 1 << 24) + 1 == 1 << 24);
+ try expect(@as(f32, 1 << 24) + 1 == 1 << 24);
}
test "f64 at compile time is lossy" {
- expect(@as(f64, 1 << 53) + 1 == 1 << 53);
+ try expect(@as(f64, 1 << 53) + 1 == 1 << 53);
}
test "f128 at compile time is lossy" {
- expect(@as(f128, 10384593717069655257060992658440192.0) + 1 == 10384593717069655257060992658440192.0);
+ try expect(@as(f128, 10384593717069655257060992658440192.0) + 1 == 10384593717069655257060992658440192.0);
}
comptime {
- expect(@as(f128, 1 << 113) == 10384593717069655257060992658440192);
+ try expect(@as(f128, 1 << 113) == 10384593717069655257060992658440192);
}
pub fn TypeWithCompTimeSlice(comptime field_name: []const u8) type {
@@ -429,15 +429,15 @@ pub fn TypeWithCompTimeSlice(comptime field_name: []const u8) type {
test "string literal used as comptime slice is memoized" {
const a = "link";
const b = "link";
- comptime expect(TypeWithCompTimeSlice(a).Node == TypeWithCompTimeSlice(b).Node);
- comptime expect(TypeWithCompTimeSlice("link").Node == TypeWithCompTimeSlice("link").Node);
+ comptime try expect(TypeWithCompTimeSlice(a).Node == TypeWithCompTimeSlice(b).Node);
+ comptime try expect(TypeWithCompTimeSlice("link").Node == TypeWithCompTimeSlice("link").Node);
}
test "comptime slice of undefined pointer of length 0" {
const slice1 = @as([*]i32, undefined)[0..0];
- expect(slice1.len == 0);
+ try expect(slice1.len == 0);
const slice2 = @as([*]i32, undefined)[100..100];
- expect(slice2.len == 0);
+ try expect(slice2.len == 0);
}
fn copyWithPartialInline(s: []u32, b: []u8) void {
@@ -459,16 +459,16 @@ test "binary math operator in partially inlined function" {
r.* = @intCast(u8, i + 1);
copyWithPartialInline(s[0..], b[0..]);
- expect(s[0] == 0x1020304);
- expect(s[1] == 0x5060708);
- expect(s[2] == 0x90a0b0c);
- expect(s[3] == 0xd0e0f10);
+ try expect(s[0] == 0x1020304);
+ try expect(s[1] == 0x5060708);
+ try expect(s[2] == 0x90a0b0c);
+ try expect(s[3] == 0xd0e0f10);
}
test "comptime function with the same args is memoized" {
comptime {
- expect(MakeType(i32) == MakeType(i32));
- expect(MakeType(i32) != MakeType(f64));
+ try expect(MakeType(i32) == MakeType(i32));
+ try expect(MakeType(i32) != MakeType(f64));
}
}
@@ -484,7 +484,7 @@ test "comptime function with mutable pointer is not memoized" {
const ptr = &x;
increment(ptr);
increment(ptr);
- expect(x == 3);
+ try expect(x == 3);
}
}
@@ -510,14 +510,14 @@ fn doesAlotT(comptime T: type, value: usize) T {
}
test "@setEvalBranchQuota at same scope as generic function call" {
- expect(doesAlotT(u32, 2) == 2);
+ try expect(doesAlotT(u32, 2) == 2);
}
test "comptime slice of slice preserves comptime var" {
comptime {
var buff: [10]u8 = undefined;
buff[0..][0..][0] = 1;
- expect(buff[0..][0..][0] == 1);
+ try expect(buff[0..][0..][0] == 1);
}
}
@@ -526,7 +526,7 @@ test "comptime slice of pointer preserves comptime var" {
var buff: [10]u8 = undefined;
var a = @ptrCast([*]u8, &buff);
a[0..1][0] = 1;
- expect(buff[0..][0..][0] == 1);
+ try expect(buff[0..][0..][0] == 1);
}
}
@@ -540,9 +540,9 @@ const SingleFieldStruct = struct {
test "const ptr to comptime mutable data is not memoized" {
comptime {
var foo = SingleFieldStruct{ .x = 1 };
- expect(foo.read_x() == 1);
+ try expect(foo.read_x() == 1);
foo.x = 2;
- expect(foo.read_x() == 2);
+ try expect(foo.read_x() == 2);
}
}
@@ -551,7 +551,7 @@ test "array concat of slices gives slice" {
var a: []const u8 = "aoeu";
var b: []const u8 = "asdf";
const c = a ++ b;
- expect(std.mem.eql(u8, c, "aoeuasdf"));
+ try expect(std.mem.eql(u8, c, "aoeuasdf"));
}
}
@@ -568,14 +568,14 @@ test "comptime shlWithOverflow" {
break :amt amt;
};
- expect(ct_shifted == rt_shifted);
+ try expect(ct_shifted == rt_shifted);
}
test "runtime 128 bit integer division" {
var a: u128 = 152313999999999991610955792383;
var b: u128 = 10000000000000000000;
var c = a / b;
- expect(c == 15231399999);
+ try expect(c == 15231399999);
}
pub const Info = struct {
@@ -588,20 +588,20 @@ test "comptime modification of const struct field" {
comptime {
var res = diamond_info;
res.version = 1;
- expect(diamond_info.version == 0);
- expect(res.version == 1);
+ try expect(diamond_info.version == 0);
+ try expect(res.version == 1);
}
}
test "pointer to type" {
comptime {
var T: type = i32;
- expect(T == i32);
+ try expect(T == i32);
var ptr = &T;
- expect(@TypeOf(ptr) == *type);
+ try expect(@TypeOf(ptr) == *type);
ptr.* = f32;
- expect(T == f32);
- expect(*T == *f32);
+ try expect(T == f32);
+ try expect(*T == *f32);
}
}
@@ -610,17 +610,17 @@ test "slice of type" {
var types_array = [_]type{ i32, f64, type };
for (types_array) |T, i| {
switch (i) {
- 0 => expect(T == i32),
- 1 => expect(T == f64),
- 2 => expect(T == type),
+ 0 => try expect(T == i32),
+ 1 => try expect(T == f64),
+ 2 => try expect(T == type),
else => unreachable,
}
}
for (types_array[0..]) |T, i| {
switch (i) {
- 0 => expect(T == i32),
- 1 => expect(T == f64),
- 2 => expect(T == type),
+ 0 => try expect(T == i32),
+ 1 => try expect(T == f64),
+ 2 => try expect(T == type),
else => unreachable,
}
}
@@ -637,7 +637,7 @@ fn wrap(comptime T: type) Wrapper {
test "function which returns struct with type field causes implicit comptime" {
const ty = wrap(i32).T;
- expect(ty == i32);
+ try expect(ty == i32);
}
test "call method with comptime pass-by-non-copying-value self parameter" {
@@ -651,12 +651,12 @@ test "call method with comptime pass-by-non-copying-value self parameter" {
const s = S{ .a = 2 };
var b = s.b();
- expect(b == 2);
+ try expect(b == 2);
}
test "@tagName of @typeInfo" {
const str = @tagName(@typeInfo(u8));
- expect(std.mem.eql(u8, str, "Int"));
+ try expect(std.mem.eql(u8, str, "Int"));
}
test "setting backward branch quota just before a generic fn call" {
@@ -670,15 +670,15 @@ fn loopNTimes(comptime n: usize) void {
}
test "variable inside inline loop that has different types on different iterations" {
- testVarInsideInlineLoop(.{ true, @as(u32, 42) });
+ try testVarInsideInlineLoop(.{ true, @as(u32, 42) });
}
-fn testVarInsideInlineLoop(args: anytype) void {
+fn testVarInsideInlineLoop(args: anytype) !void {
comptime var i = 0;
inline while (i < args.len) : (i += 1) {
const x = args[i];
- if (i == 0) expect(x);
- if (i == 1) expect(x == 42);
+ if (i == 0) try expect(x);
+ if (i == 1) try expect(x == 42);
}
}
@@ -688,7 +688,7 @@ test "inline for with same type but different values" {
var a: T = undefined;
res += a.len;
}
- expect(res == 5);
+ try expect(res == 5);
}
test "refer to the type of a generic function" {
@@ -702,13 +702,13 @@ fn doNothingWithType(comptime T: type) void {}
test "zero extend from u0 to u1" {
var zero_u0: u0 = 0;
var zero_u1: u1 = zero_u0;
- expect(zero_u1 == 0);
+ try expect(zero_u1 == 0);
}
test "bit shift a u1" {
var x: u1 = 1;
var y = x << 0;
- expect(y == 1);
+ try expect(y == 1);
}
test "comptime pointer cast array and then slice" {
@@ -720,8 +720,8 @@ test "comptime pointer cast array and then slice" {
const ptrB: [*]const u8 = &array;
const sliceB: []const u8 = ptrB[0..2];
- expect(sliceA[1] == 2);
- expect(sliceB[1] == 2);
+ try expect(sliceA[1] == 2);
+ try expect(sliceB[1] == 2);
}
test "slice bounds in comptime concatenation" {
@@ -730,46 +730,46 @@ test "slice bounds in comptime concatenation" {
break :blk b[8..9];
};
const str = "" ++ bs;
- expect(str.len == 1);
- expect(std.mem.eql(u8, str, "1"));
+ try expect(str.len == 1);
+ try expect(std.mem.eql(u8, str, "1"));
const str2 = bs ++ "";
- expect(str2.len == 1);
- expect(std.mem.eql(u8, str2, "1"));
+ try expect(str2.len == 1);
+ try expect(std.mem.eql(u8, str2, "1"));
}
test "comptime bitwise operators" {
comptime {
- expect(3 & 1 == 1);
- expect(3 & -1 == 3);
- expect(-3 & -1 == -3);
- expect(3 | -1 == -1);
- expect(-3 | -1 == -1);
- expect(3 ^ -1 == -4);
- expect(-3 ^ -1 == 2);
- expect(~@as(i8, -1) == 0);
- expect(~@as(i128, -1) == 0);
- expect(18446744073709551615 & 18446744073709551611 == 18446744073709551611);
- expect(-18446744073709551615 & -18446744073709551611 == -18446744073709551615);
- expect(~@as(u128, 0) == 0xffffffffffffffffffffffffffffffff);
+ try expect(3 & 1 == 1);
+ try expect(3 & -1 == 3);
+ try expect(-3 & -1 == -3);
+ try expect(3 | -1 == -1);
+ try expect(-3 | -1 == -1);
+ try expect(3 ^ -1 == -4);
+ try expect(-3 ^ -1 == 2);
+ try expect(~@as(i8, -1) == 0);
+ try expect(~@as(i128, -1) == 0);
+ try expect(18446744073709551615 & 18446744073709551611 == 18446744073709551611);
+ try expect(-18446744073709551615 & -18446744073709551611 == -18446744073709551615);
+ try expect(~@as(u128, 0) == 0xffffffffffffffffffffffffffffffff);
}
}
test "*align(1) u16 is the same as *align(1:0:2) u16" {
comptime {
- expect(*align(1:0:2) u16 == *align(1) u16);
- expect(*align(2:0:2) u16 == *u16);
+ try expect(*align(1:0:2) u16 == *align(1) u16);
+ try expect(*align(2:0:2) u16 == *u16);
}
}
test "array concatenation forces comptime" {
var a = oneItem(3) ++ oneItem(4);
- expect(std.mem.eql(i32, &a, &[_]i32{ 3, 4 }));
+ try expect(std.mem.eql(i32, &a, &[_]i32{ 3, 4 }));
}
test "array multiplication forces comptime" {
var a = oneItem(3) ** scalar(2);
- expect(std.mem.eql(i32, &a, &[_]i32{ 3, 3 }));
+ try expect(std.mem.eql(i32, &a, &[_]i32{ 3, 3 }));
}
fn oneItem(x: i32) [1]i32 {
@@ -791,7 +791,7 @@ test "comptime assign int to optional int" {
var x: ?i32 = null;
x = 2;
x.? *= 10;
- expectEqual(20, x.?);
+ try expectEqual(20, x.?);
}
}
test/stage1/behavior/field_parent_ptr.zig
@@ -1,13 +1,13 @@
const expect = @import("std").testing.expect;
test "@fieldParentPtr non-first field" {
- testParentFieldPtr(&foo.c);
- comptime testParentFieldPtr(&foo.c);
+ try testParentFieldPtr(&foo.c);
+ comptime try testParentFieldPtr(&foo.c);
}
test "@fieldParentPtr first field" {
- testParentFieldPtrFirst(&foo.a);
- comptime testParentFieldPtrFirst(&foo.a);
+ try testParentFieldPtrFirst(&foo.a);
+ comptime try testParentFieldPtrFirst(&foo.a);
}
const Foo = struct {
@@ -24,18 +24,18 @@ const foo = Foo{
.d = -10,
};
-fn testParentFieldPtr(c: *const i32) void {
- expect(c == &foo.c);
+fn testParentFieldPtr(c: *const i32) !void {
+ try expect(c == &foo.c);
const base = @fieldParentPtr(Foo, "c", c);
- expect(base == &foo);
- expect(&base.c == c);
+ try expect(base == &foo);
+ try expect(&base.c == c);
}
-fn testParentFieldPtrFirst(a: *const bool) void {
- expect(a == &foo.a);
+fn testParentFieldPtrFirst(a: *const bool) !void {
+ try expect(a == &foo.a);
const base = @fieldParentPtr(Foo, "a", a);
- expect(base == &foo);
- expect(&base.a == a);
+ try expect(base == &foo);
+ try expect(&base.a == a);
}
test/stage1/behavior/floatop.zig
@@ -8,441 +8,441 @@ const Vector = std.meta.Vector;
const epsilon = 0.000001;
test "@sqrt" {
- comptime testSqrt();
- testSqrt();
+ comptime try testSqrt();
+ try testSqrt();
}
-fn testSqrt() void {
+fn testSqrt() !void {
{
var a: f16 = 4;
- expect(@sqrt(a) == 2);
+ try expect(@sqrt(a) == 2);
}
{
var a: f32 = 9;
- expect(@sqrt(a) == 3);
+ try expect(@sqrt(a) == 3);
var b: f32 = 1.1;
- expect(math.approxEqAbs(f32, @sqrt(b), 1.0488088481701516, epsilon));
+ try expect(math.approxEqAbs(f32, @sqrt(b), 1.0488088481701516, epsilon));
}
{
var a: f64 = 25;
- expect(@sqrt(a) == 5);
+ try expect(@sqrt(a) == 5);
}
{
const a: comptime_float = 25.0;
- expect(@sqrt(a) == 5.0);
+ try expect(@sqrt(a) == 5.0);
}
// TODO https://github.com/ziglang/zig/issues/4026
//{
// var a: f128 = 49;
- // expect(@sqrt(a) == 7);
+ //try expect(@sqrt(a) == 7);
//}
{
var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 3.3, 4.4 };
var result = @sqrt(v);
- expect(math.approxEqAbs(f32, @sqrt(@as(f32, 1.1)), result[0], epsilon));
- expect(math.approxEqAbs(f32, @sqrt(@as(f32, 2.2)), result[1], epsilon));
- expect(math.approxEqAbs(f32, @sqrt(@as(f32, 3.3)), result[2], epsilon));
- expect(math.approxEqAbs(f32, @sqrt(@as(f32, 4.4)), result[3], epsilon));
+ try expect(math.approxEqAbs(f32, @sqrt(@as(f32, 1.1)), result[0], epsilon));
+ try expect(math.approxEqAbs(f32, @sqrt(@as(f32, 2.2)), result[1], epsilon));
+ try expect(math.approxEqAbs(f32, @sqrt(@as(f32, 3.3)), result[2], epsilon));
+ try expect(math.approxEqAbs(f32, @sqrt(@as(f32, 4.4)), result[3], epsilon));
}
}
test "more @sqrt f16 tests" {
// TODO these are not all passing at comptime
- expect(@sqrt(@as(f16, 0.0)) == 0.0);
- expect(math.approxEqAbs(f16, @sqrt(@as(f16, 2.0)), 1.414214, epsilon));
- expect(math.approxEqAbs(f16, @sqrt(@as(f16, 3.6)), 1.897367, epsilon));
- expect(@sqrt(@as(f16, 4.0)) == 2.0);
- expect(math.approxEqAbs(f16, @sqrt(@as(f16, 7.539840)), 2.745877, epsilon));
- expect(math.approxEqAbs(f16, @sqrt(@as(f16, 19.230934)), 4.385309, epsilon));
- expect(@sqrt(@as(f16, 64.0)) == 8.0);
- expect(math.approxEqAbs(f16, @sqrt(@as(f16, 64.1)), 8.006248, epsilon));
- expect(math.approxEqAbs(f16, @sqrt(@as(f16, 8942.230469)), 94.563370, epsilon));
+ try expect(@sqrt(@as(f16, 0.0)) == 0.0);
+ try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 2.0)), 1.414214, epsilon));
+ try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 3.6)), 1.897367, epsilon));
+ try expect(@sqrt(@as(f16, 4.0)) == 2.0);
+ try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 7.539840)), 2.745877, epsilon));
+ try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 19.230934)), 4.385309, epsilon));
+ try expect(@sqrt(@as(f16, 64.0)) == 8.0);
+ try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 64.1)), 8.006248, epsilon));
+ try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 8942.230469)), 94.563370, epsilon));
// special cases
- expect(math.isPositiveInf(@sqrt(@as(f16, math.inf(f16)))));
- expect(@sqrt(@as(f16, 0.0)) == 0.0);
- expect(@sqrt(@as(f16, -0.0)) == -0.0);
- expect(math.isNan(@sqrt(@as(f16, -1.0))));
- expect(math.isNan(@sqrt(@as(f16, math.nan(f16)))));
+ try expect(math.isPositiveInf(@sqrt(@as(f16, math.inf(f16)))));
+ try expect(@sqrt(@as(f16, 0.0)) == 0.0);
+ try expect(@sqrt(@as(f16, -0.0)) == -0.0);
+ try expect(math.isNan(@sqrt(@as(f16, -1.0))));
+ try expect(math.isNan(@sqrt(@as(f16, math.nan(f16)))));
}
test "@sin" {
- comptime testSin();
- testSin();
+ comptime try testSin();
+ try testSin();
}
-fn testSin() void {
+fn testSin() !void {
// TODO test f128, and c_longdouble
// https://github.com/ziglang/zig/issues/4026
{
var a: f16 = 0;
- expect(@sin(a) == 0);
+ try expect(@sin(a) == 0);
}
{
var a: f32 = 0;
- expect(@sin(a) == 0);
+ try expect(@sin(a) == 0);
}
{
var a: f64 = 0;
- expect(@sin(a) == 0);
+ try expect(@sin(a) == 0);
}
{
var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 3.3, 4.4 };
var result = @sin(v);
- expect(math.approxEqAbs(f32, @sin(@as(f32, 1.1)), result[0], epsilon));
- expect(math.approxEqAbs(f32, @sin(@as(f32, 2.2)), result[1], epsilon));
- expect(math.approxEqAbs(f32, @sin(@as(f32, 3.3)), result[2], epsilon));
- expect(math.approxEqAbs(f32, @sin(@as(f32, 4.4)), result[3], epsilon));
+ try expect(math.approxEqAbs(f32, @sin(@as(f32, 1.1)), result[0], epsilon));
+ try expect(math.approxEqAbs(f32, @sin(@as(f32, 2.2)), result[1], epsilon));
+ try expect(math.approxEqAbs(f32, @sin(@as(f32, 3.3)), result[2], epsilon));
+ try expect(math.approxEqAbs(f32, @sin(@as(f32, 4.4)), result[3], epsilon));
}
}
test "@cos" {
- comptime testCos();
- testCos();
+ comptime try testCos();
+ try testCos();
}
-fn testCos() void {
+fn testCos() !void {
// TODO test f128, and c_longdouble
// https://github.com/ziglang/zig/issues/4026
{
var a: f16 = 0;
- expect(@cos(a) == 1);
+ try expect(@cos(a) == 1);
}
{
var a: f32 = 0;
- expect(@cos(a) == 1);
+ try expect(@cos(a) == 1);
}
{
var a: f64 = 0;
- expect(@cos(a) == 1);
+ try expect(@cos(a) == 1);
}
{
var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 3.3, 4.4 };
var result = @cos(v);
- expect(math.approxEqAbs(f32, @cos(@as(f32, 1.1)), result[0], epsilon));
- expect(math.approxEqAbs(f32, @cos(@as(f32, 2.2)), result[1], epsilon));
- expect(math.approxEqAbs(f32, @cos(@as(f32, 3.3)), result[2], epsilon));
- expect(math.approxEqAbs(f32, @cos(@as(f32, 4.4)), result[3], epsilon));
+ try expect(math.approxEqAbs(f32, @cos(@as(f32, 1.1)), result[0], epsilon));
+ try expect(math.approxEqAbs(f32, @cos(@as(f32, 2.2)), result[1], epsilon));
+ try expect(math.approxEqAbs(f32, @cos(@as(f32, 3.3)), result[2], epsilon));
+ try expect(math.approxEqAbs(f32, @cos(@as(f32, 4.4)), result[3], epsilon));
}
}
test "@exp" {
- comptime testExp();
- testExp();
+ comptime try testExp();
+ try testExp();
}
-fn testExp() void {
+fn testExp() !void {
// TODO test f128, and c_longdouble
// https://github.com/ziglang/zig/issues/4026
{
var a: f16 = 0;
- expect(@exp(a) == 1);
+ try expect(@exp(a) == 1);
}
{
var a: f32 = 0;
- expect(@exp(a) == 1);
+ try expect(@exp(a) == 1);
}
{
var a: f64 = 0;
- expect(@exp(a) == 1);
+ try expect(@exp(a) == 1);
}
{
var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };
var result = @exp(v);
- expect(math.approxEqAbs(f32, @exp(@as(f32, 1.1)), result[0], epsilon));
- expect(math.approxEqAbs(f32, @exp(@as(f32, 2.2)), result[1], epsilon));
- expect(math.approxEqAbs(f32, @exp(@as(f32, 0.3)), result[2], epsilon));
- expect(math.approxEqAbs(f32, @exp(@as(f32, 0.4)), result[3], epsilon));
+ try expect(math.approxEqAbs(f32, @exp(@as(f32, 1.1)), result[0], epsilon));
+ try expect(math.approxEqAbs(f32, @exp(@as(f32, 2.2)), result[1], epsilon));
+ try expect(math.approxEqAbs(f32, @exp(@as(f32, 0.3)), result[2], epsilon));
+ try expect(math.approxEqAbs(f32, @exp(@as(f32, 0.4)), result[3], epsilon));
}
}
test "@exp2" {
- comptime testExp2();
- testExp2();
+ comptime try testExp2();
+ try testExp2();
}
-fn testExp2() void {
+fn testExp2() !void {
// TODO test f128, and c_longdouble
// https://github.com/ziglang/zig/issues/4026
{
var a: f16 = 2;
- expect(@exp2(a) == 4);
+ try expect(@exp2(a) == 4);
}
{
var a: f32 = 2;
- expect(@exp2(a) == 4);
+ try expect(@exp2(a) == 4);
}
{
var a: f64 = 2;
- expect(@exp2(a) == 4);
+ try expect(@exp2(a) == 4);
}
{
var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };
var result = @exp2(v);
- expect(math.approxEqAbs(f32, @exp2(@as(f32, 1.1)), result[0], epsilon));
- expect(math.approxEqAbs(f32, @exp2(@as(f32, 2.2)), result[1], epsilon));
- expect(math.approxEqAbs(f32, @exp2(@as(f32, 0.3)), result[2], epsilon));
- expect(math.approxEqAbs(f32, @exp2(@as(f32, 0.4)), result[3], epsilon));
+ try expect(math.approxEqAbs(f32, @exp2(@as(f32, 1.1)), result[0], epsilon));
+ try expect(math.approxEqAbs(f32, @exp2(@as(f32, 2.2)), result[1], epsilon));
+ try expect(math.approxEqAbs(f32, @exp2(@as(f32, 0.3)), result[2], epsilon));
+ try expect(math.approxEqAbs(f32, @exp2(@as(f32, 0.4)), result[3], epsilon));
}
}
test "@log" {
// Old musl (and glibc?), and our current math.ln implementation do not return 1
// so also accept those values.
- comptime testLog();
- testLog();
+ comptime try testLog();
+ try testLog();
}
-fn testLog() void {
+fn testLog() !void {
// TODO test f128, and c_longdouble
// https://github.com/ziglang/zig/issues/4026
{
var a: f16 = e;
- expect(math.approxEqAbs(f16, @log(a), 1, epsilon));
+ try expect(math.approxEqAbs(f16, @log(a), 1, epsilon));
}
{
var a: f32 = e;
- expect(@log(a) == 1 or @log(a) == @bitCast(f32, @as(u32, 0x3f7fffff)));
+ try expect(@log(a) == 1 or @log(a) == @bitCast(f32, @as(u32, 0x3f7fffff)));
}
{
var a: f64 = e;
- expect(@log(a) == 1 or @log(a) == @bitCast(f64, @as(u64, 0x3ff0000000000000)));
+ try expect(@log(a) == 1 or @log(a) == @bitCast(f64, @as(u64, 0x3ff0000000000000)));
}
{
var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };
var result = @log(v);
- expect(math.approxEqAbs(f32, @log(@as(f32, 1.1)), result[0], epsilon));
- expect(math.approxEqAbs(f32, @log(@as(f32, 2.2)), result[1], epsilon));
- expect(math.approxEqAbs(f32, @log(@as(f32, 0.3)), result[2], epsilon));
- expect(math.approxEqAbs(f32, @log(@as(f32, 0.4)), result[3], epsilon));
+ try expect(math.approxEqAbs(f32, @log(@as(f32, 1.1)), result[0], epsilon));
+ try expect(math.approxEqAbs(f32, @log(@as(f32, 2.2)), result[1], epsilon));
+ try expect(math.approxEqAbs(f32, @log(@as(f32, 0.3)), result[2], epsilon));
+ try expect(math.approxEqAbs(f32, @log(@as(f32, 0.4)), result[3], epsilon));
}
}
test "@log2" {
- comptime testLog2();
- testLog2();
+ comptime try testLog2();
+ try testLog2();
}
-fn testLog2() void {
+fn testLog2() !void {
// TODO test f128, and c_longdouble
// https://github.com/ziglang/zig/issues/4026
{
var a: f16 = 4;
- expect(@log2(a) == 2);
+ try expect(@log2(a) == 2);
}
{
var a: f32 = 4;
- expect(@log2(a) == 2);
+ try expect(@log2(a) == 2);
}
{
var a: f64 = 4;
- expect(@log2(a) == 2);
+ try expect(@log2(a) == 2);
}
{
var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };
var result = @log2(v);
- expect(math.approxEqAbs(f32, @log2(@as(f32, 1.1)), result[0], epsilon));
- expect(math.approxEqAbs(f32, @log2(@as(f32, 2.2)), result[1], epsilon));
- expect(math.approxEqAbs(f32, @log2(@as(f32, 0.3)), result[2], epsilon));
- expect(math.approxEqAbs(f32, @log2(@as(f32, 0.4)), result[3], epsilon));
+ try expect(math.approxEqAbs(f32, @log2(@as(f32, 1.1)), result[0], epsilon));
+ try expect(math.approxEqAbs(f32, @log2(@as(f32, 2.2)), result[1], epsilon));
+ try expect(math.approxEqAbs(f32, @log2(@as(f32, 0.3)), result[2], epsilon));
+ try expect(math.approxEqAbs(f32, @log2(@as(f32, 0.4)), result[3], epsilon));
}
}
test "@log10" {
- comptime testLog10();
- testLog10();
+ comptime try testLog10();
+ try testLog10();
}
-fn testLog10() void {
+fn testLog10() !void {
// TODO test f128, and c_longdouble
// https://github.com/ziglang/zig/issues/4026
{
var a: f16 = 100;
- expect(@log10(a) == 2);
+ try expect(@log10(a) == 2);
}
{
var a: f32 = 100;
- expect(@log10(a) == 2);
+ try expect(@log10(a) == 2);
}
{
var a: f64 = 1000;
- expect(@log10(a) == 3);
+ try expect(@log10(a) == 3);
}
{
var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };
var result = @log10(v);
- expect(math.approxEqAbs(f32, @log10(@as(f32, 1.1)), result[0], epsilon));
- expect(math.approxEqAbs(f32, @log10(@as(f32, 2.2)), result[1], epsilon));
- expect(math.approxEqAbs(f32, @log10(@as(f32, 0.3)), result[2], epsilon));
- expect(math.approxEqAbs(f32, @log10(@as(f32, 0.4)), result[3], epsilon));
+ try expect(math.approxEqAbs(f32, @log10(@as(f32, 1.1)), result[0], epsilon));
+ try expect(math.approxEqAbs(f32, @log10(@as(f32, 2.2)), result[1], epsilon));
+ try expect(math.approxEqAbs(f32, @log10(@as(f32, 0.3)), result[2], epsilon));
+ try expect(math.approxEqAbs(f32, @log10(@as(f32, 0.4)), result[3], epsilon));
}
}
test "@fabs" {
- comptime testFabs();
- testFabs();
+ comptime try testFabs();
+ try testFabs();
}
-fn testFabs() void {
+fn testFabs() !void {
// TODO test f128, and c_longdouble
// https://github.com/ziglang/zig/issues/4026
{
var a: f16 = -2.5;
var b: f16 = 2.5;
- expect(@fabs(a) == 2.5);
- expect(@fabs(b) == 2.5);
+ try expect(@fabs(a) == 2.5);
+ try expect(@fabs(b) == 2.5);
}
{
var a: f32 = -2.5;
var b: f32 = 2.5;
- expect(@fabs(a) == 2.5);
- expect(@fabs(b) == 2.5);
+ try expect(@fabs(a) == 2.5);
+ try expect(@fabs(b) == 2.5);
}
{
var a: f64 = -2.5;
var b: f64 = 2.5;
- expect(@fabs(a) == 2.5);
- expect(@fabs(b) == 2.5);
+ try expect(@fabs(a) == 2.5);
+ try expect(@fabs(b) == 2.5);
}
{
var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 };
var result = @fabs(v);
- expect(math.approxEqAbs(f32, @fabs(@as(f32, 1.1)), result[0], epsilon));
- expect(math.approxEqAbs(f32, @fabs(@as(f32, -2.2)), result[1], epsilon));
- expect(math.approxEqAbs(f32, @fabs(@as(f32, 0.3)), result[2], epsilon));
- expect(math.approxEqAbs(f32, @fabs(@as(f32, -0.4)), result[3], epsilon));
+ try expect(math.approxEqAbs(f32, @fabs(@as(f32, 1.1)), result[0], epsilon));
+ try expect(math.approxEqAbs(f32, @fabs(@as(f32, -2.2)), result[1], epsilon));
+ try expect(math.approxEqAbs(f32, @fabs(@as(f32, 0.3)), result[2], epsilon));
+ try expect(math.approxEqAbs(f32, @fabs(@as(f32, -0.4)), result[3], epsilon));
}
}
test "@floor" {
- comptime testFloor();
- testFloor();
+ comptime try testFloor();
+ try testFloor();
}
-fn testFloor() void {
+fn testFloor() !void {
// TODO test f128, and c_longdouble
// https://github.com/ziglang/zig/issues/4026
{
var a: f16 = 2.1;
- expect(@floor(a) == 2);
+ try expect(@floor(a) == 2);
}
{
var a: f32 = 2.1;
- expect(@floor(a) == 2);
+ try expect(@floor(a) == 2);
}
{
var a: f64 = 3.5;
- expect(@floor(a) == 3);
+ try expect(@floor(a) == 3);
}
{
var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 };
var result = @floor(v);
- expect(math.approxEqAbs(f32, @floor(@as(f32, 1.1)), result[0], epsilon));
- expect(math.approxEqAbs(f32, @floor(@as(f32, -2.2)), result[1], epsilon));
- expect(math.approxEqAbs(f32, @floor(@as(f32, 0.3)), result[2], epsilon));
- expect(math.approxEqAbs(f32, @floor(@as(f32, -0.4)), result[3], epsilon));
+ try expect(math.approxEqAbs(f32, @floor(@as(f32, 1.1)), result[0], epsilon));
+ try expect(math.approxEqAbs(f32, @floor(@as(f32, -2.2)), result[1], epsilon));
+ try expect(math.approxEqAbs(f32, @floor(@as(f32, 0.3)), result[2], epsilon));
+ try expect(math.approxEqAbs(f32, @floor(@as(f32, -0.4)), result[3], epsilon));
}
}
test "@ceil" {
- comptime testCeil();
- testCeil();
+ comptime try testCeil();
+ try testCeil();
}
-fn testCeil() void {
+fn testCeil() !void {
// TODO test f128, and c_longdouble
// https://github.com/ziglang/zig/issues/4026
{
var a: f16 = 2.1;
- expect(@ceil(a) == 3);
+ try expect(@ceil(a) == 3);
}
{
var a: f32 = 2.1;
- expect(@ceil(a) == 3);
+ try expect(@ceil(a) == 3);
}
{
var a: f64 = 3.5;
- expect(@ceil(a) == 4);
+ try expect(@ceil(a) == 4);
}
{
var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 };
var result = @ceil(v);
- expect(math.approxEqAbs(f32, @ceil(@as(f32, 1.1)), result[0], epsilon));
- expect(math.approxEqAbs(f32, @ceil(@as(f32, -2.2)), result[1], epsilon));
- expect(math.approxEqAbs(f32, @ceil(@as(f32, 0.3)), result[2], epsilon));
- expect(math.approxEqAbs(f32, @ceil(@as(f32, -0.4)), result[3], epsilon));
+ try expect(math.approxEqAbs(f32, @ceil(@as(f32, 1.1)), result[0], epsilon));
+ try expect(math.approxEqAbs(f32, @ceil(@as(f32, -2.2)), result[1], epsilon));
+ try expect(math.approxEqAbs(f32, @ceil(@as(f32, 0.3)), result[2], epsilon));
+ try expect(math.approxEqAbs(f32, @ceil(@as(f32, -0.4)), result[3], epsilon));
}
}
test "@trunc" {
- comptime testTrunc();
- testTrunc();
+ comptime try testTrunc();
+ try testTrunc();
}
-fn testTrunc() void {
+fn testTrunc() !void {
// TODO test f128, and c_longdouble
// https://github.com/ziglang/zig/issues/4026
{
var a: f16 = 2.1;
- expect(@trunc(a) == 2);
+ try expect(@trunc(a) == 2);
}
{
var a: f32 = 2.1;
- expect(@trunc(a) == 2);
+ try expect(@trunc(a) == 2);
}
{
var a: f64 = -3.5;
- expect(@trunc(a) == -3);
+ try expect(@trunc(a) == -3);
}
{
var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 };
var result = @trunc(v);
- expect(math.approxEqAbs(f32, @trunc(@as(f32, 1.1)), result[0], epsilon));
- expect(math.approxEqAbs(f32, @trunc(@as(f32, -2.2)), result[1], epsilon));
- expect(math.approxEqAbs(f32, @trunc(@as(f32, 0.3)), result[2], epsilon));
- expect(math.approxEqAbs(f32, @trunc(@as(f32, -0.4)), result[3], epsilon));
+ try expect(math.approxEqAbs(f32, @trunc(@as(f32, 1.1)), result[0], epsilon));
+ try expect(math.approxEqAbs(f32, @trunc(@as(f32, -2.2)), result[1], epsilon));
+ try expect(math.approxEqAbs(f32, @trunc(@as(f32, 0.3)), result[2], epsilon));
+ try expect(math.approxEqAbs(f32, @trunc(@as(f32, -0.4)), result[3], epsilon));
}
}
test "floating point comparisons" {
- testFloatComparisons();
- comptime testFloatComparisons();
+ try testFloatComparisons();
+ comptime try testFloatComparisons();
}
-fn testFloatComparisons() void {
+fn testFloatComparisons() !void {
inline for ([_]type{ f16, f32, f64, f128 }) |ty| {
// No decimal part
{
const x: ty = 1.0;
- expect(x == 1);
- expect(x != 0);
- expect(x > 0);
- expect(x < 2);
- expect(x >= 1);
- expect(x <= 1);
+ try expect(x == 1);
+ try expect(x != 0);
+ try expect(x > 0);
+ try expect(x < 2);
+ try expect(x >= 1);
+ try expect(x <= 1);
}
// Non-zero decimal part
{
const x: ty = 1.5;
- expect(x != 1);
- expect(x != 2);
- expect(x > 1);
- expect(x < 2);
- expect(x >= 1);
- expect(x <= 2);
+ try expect(x != 1);
+ try expect(x != 2);
+ try expect(x > 1);
+ try expect(x < 2);
+ try expect(x >= 1);
+ try expect(x <= 2);
}
}
}
test "different sized float comparisons" {
- testDifferentSizedFloatComparisons();
- comptime testDifferentSizedFloatComparisons();
+ try testDifferentSizedFloatComparisons();
+ comptime try testDifferentSizedFloatComparisons();
}
-fn testDifferentSizedFloatComparisons() void {
+fn testDifferentSizedFloatComparisons() !void {
var a: f16 = 1;
var b: f64 = 2;
- expect(a < b);
+ try expect(a < b);
}
// TODO This is waiting on library support for the Windows build (not sure why the other's don't need it)
@@ -456,10 +456,10 @@ fn testDifferentSizedFloatComparisons() void {
// // https://github.com/ziglang/zig/issues/4026
// {
// var a: f32 = 2.1;
-// expect(@nearbyint(a) == 2);
+// try expect(@nearbyint(a) == 2);
// }
// {
// var a: f64 = -3.75;
-// expect(@nearbyint(a) == -4);
+// try expect(@nearbyint(a) == -4);
// }
//}
test/stage1/behavior/fn.zig
@@ -4,7 +4,7 @@ const expect = testing.expect;
const expectEqual = testing.expectEqual;
test "params" {
- expect(testParamsAdd(22, 11) == 33);
+ try expect(testParamsAdd(22, 11) == 33);
}
fn testParamsAdd(a: i32, b: i32) i32 {
return a + b;
@@ -19,37 +19,37 @@ fn testLocVars(b: i32) void {
}
test "void parameters" {
- voidFun(1, void{}, 2, {});
+ try voidFun(1, void{}, 2, {});
}
-fn voidFun(a: i32, b: void, c: i32, d: void) void {
+fn voidFun(a: i32, b: void, c: i32, d: void) !void {
const v = b;
const vv: void = if (a == 1) v else {};
- expect(a + c == 3);
+ try expect(a + c == 3);
return vv;
}
test "mutable local variables" {
var zero: i32 = 0;
- expect(zero == 0);
+ try expect(zero == 0);
var i = @as(i32, 0);
while (i != 3) {
i += 1;
}
- expect(i == 3);
+ try expect(i == 3);
}
test "separate block scopes" {
{
const no_conflict: i32 = 5;
- expect(no_conflict == 5);
+ try expect(no_conflict == 5);
}
const c = x: {
const no_conflict = @as(i32, 10);
break :x no_conflict;
};
- expect(c == 10);
+ try expect(c == 10);
}
test "call function with empty string" {
@@ -62,7 +62,7 @@ fn @"weird function name"() i32 {
return 1234;
}
test "weird function name" {
- expect(@"weird function name"() == 1234);
+ try expect(@"weird function name"() == 1234);
}
test "implicit cast function unreachable return" {
@@ -83,7 +83,7 @@ test "function pointers" {
fn4,
};
for (fns) |f, i| {
- expect(f() == @intCast(u32, i) + 5);
+ try expect(f() == @intCast(u32, i) + 5);
}
}
fn fn1() u32 {
@@ -100,12 +100,12 @@ fn fn4() u32 {
}
test "number literal as an argument" {
- numberLiteralArg(3);
- comptime numberLiteralArg(3);
+ try numberLiteralArg(3);
+ comptime try numberLiteralArg(3);
}
-fn numberLiteralArg(a: anytype) void {
- expect(a == 3);
+fn numberLiteralArg(a: anytype) !void {
+ try expect(a == 3);
}
test "assign inline fn to const variable" {
@@ -116,7 +116,7 @@ test "assign inline fn to const variable" {
fn inlineFn() callconv(.Inline) void {}
test "pass by non-copying value" {
- expect(addPointCoords(Point{ .x = 1, .y = 2 }) == 3);
+ try expect(addPointCoords(Point{ .x = 1, .y = 2 }) == 3);
}
const Point = struct {
@@ -129,17 +129,17 @@ fn addPointCoords(pt: Point) i32 {
}
test "pass by non-copying value through var arg" {
- expect(addPointCoordsVar(Point{ .x = 1, .y = 2 }) == 3);
+ try expect((try addPointCoordsVar(Point{ .x = 1, .y = 2 })) == 3);
}
-fn addPointCoordsVar(pt: anytype) i32 {
- comptime expect(@TypeOf(pt) == Point);
+fn addPointCoordsVar(pt: anytype) !i32 {
+ comptime try expect(@TypeOf(pt) == Point);
return pt.x + pt.y;
}
test "pass by non-copying value as method" {
var pt = Point2{ .x = 1, .y = 2 };
- expect(pt.addPointCoords() == 3);
+ try expect(pt.addPointCoords() == 3);
}
const Point2 = struct {
@@ -153,7 +153,7 @@ const Point2 = struct {
test "pass by non-copying value as method, which is generic" {
var pt = Point3{ .x = 1, .y = 2 };
- expect(pt.addPointCoords(i32) == 3);
+ try expect(pt.addPointCoords(i32) == 3);
}
const Point3 = struct {
@@ -168,7 +168,7 @@ const Point3 = struct {
test "pass by non-copying value as method, at comptime" {
comptime {
var pt = Point2{ .x = 1, .y = 2 };
- expect(pt.addPointCoords() == 3);
+ try expect(pt.addPointCoords() == 3);
}
}
@@ -184,7 +184,7 @@ fn outer(y: u32) fn (u32) u32 {
test "return inner function which references comptime variable of outer function" {
var func = outer(10);
- expect(func(3) == 7);
+ try expect(func(3) == 7);
}
test "extern struct with stdcallcc fn pointer" {
@@ -198,16 +198,16 @@ test "extern struct with stdcallcc fn pointer" {
var s: S = undefined;
s.ptr = S.foo;
- expect(s.ptr() == 1234);
+ try expect(s.ptr() == 1234);
}
test "implicit cast fn call result to optional in field result" {
const S = struct {
- fn entry() void {
+ fn entry() !void {
var x = Foo{
.field = optionalPtr(),
};
- expect(x.field.?.* == 999);
+ try expect(x.field.?.* == 999);
}
const glob: i32 = 999;
@@ -220,8 +220,8 @@ test "implicit cast fn call result to optional in field result" {
field: ?*const i32,
};
};
- S.entry();
- comptime S.entry();
+ try S.entry();
+ comptime try S.entry();
}
test "discard the result of a function that returns a struct" {
@@ -245,26 +245,26 @@ test "discard the result of a function that returns a struct" {
test "function call with anon list literal" {
const S = struct {
- fn doTheTest() void {
- consumeVec(.{ 9, 8, 7 });
+ fn doTheTest() !void {
+ try consumeVec(.{ 9, 8, 7 });
}
- fn consumeVec(vec: [3]f32) void {
- expect(vec[0] == 9);
- expect(vec[1] == 8);
- expect(vec[2] == 7);
+ fn consumeVec(vec: [3]f32) !void {
+ try expect(vec[0] == 9);
+ try expect(vec[1] == 8);
+ try expect(vec[2] == 7);
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "ability to give comptime types and non comptime types to same parameter" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var x: i32 = 1;
- expect(foo(x) == 10);
- expect(foo(i32) == 20);
+ try expect(foo(x) == 10);
+ try expect(foo(i32) == 20);
}
fn foo(arg: anytype) i32 {
@@ -272,8 +272,8 @@ test "ability to give comptime types and non comptime types to same parameter" {
return 9 + arg;
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "function with inferred error set but returning no error" {
@@ -282,5 +282,5 @@ test "function with inferred error set but returning no error" {
};
const return_ty = @typeInfo(@TypeOf(S.foo)).Fn.return_type.?;
- expectEqual(0, @typeInfo(@typeInfo(return_ty).ErrorUnion.error_set).ErrorSet.?.len);
+ try expectEqual(0, @typeInfo(@typeInfo(return_ty).ErrorUnion.error_set).ErrorSet.?.len);
}
test/stage1/behavior/fn_delegation.zig
@@ -32,8 +32,8 @@ fn custom(comptime T: type, comptime num: u64) fn (T) u64 {
test "fn delegation" {
const foo = Foo{};
- expect(foo.one() == 11);
- expect(foo.two() == 12);
- expect(foo.three() == 13);
- expect(foo.four() == 14);
+ try expect(foo.one() == 11);
+ try expect(foo.two() == 12);
+ try expect(foo.three() == 13);
+ try expect(foo.four() == 14);
}
test/stage1/behavior/fn_in_struct_in_comptime.zig
@@ -13,5 +13,5 @@ fn get_foo() fn (*u8) usize {
test "define a function in an anonymous struct in comptime" {
const foo = get_foo();
- expect(foo(@intToPtr(*u8, 12345)) == 12345);
+ try expect(foo(@intToPtr(*u8, 12345)) == 12345);
}
test/stage1/behavior/for.zig
@@ -27,12 +27,12 @@ test "for loop with pointer elem var" {
var target: [source.len]u8 = undefined;
mem.copy(u8, target[0..], source);
mangleString(target[0..]);
- expect(mem.eql(u8, &target, "bcdefgh"));
+ try expect(mem.eql(u8, &target, "bcdefgh"));
for (source) |*c, i|
- expect(@TypeOf(c) == *const u8);
+ try expect(@TypeOf(c) == *const u8);
for (target) |*c, i|
- expect(@TypeOf(c) == *u8);
+ try expect(@TypeOf(c) == *u8);
}
fn mangleString(s: []u8) void {
@@ -75,15 +75,15 @@ test "basic for loop" {
buf_index += 1;
}
- expect(mem.eql(u8, buffer[0..buf_index], &expected_result));
+ try expect(mem.eql(u8, buffer[0..buf_index], &expected_result));
}
test "break from outer for loop" {
- testBreakOuter();
- comptime testBreakOuter();
+ try testBreakOuter();
+ comptime try testBreakOuter();
}
-fn testBreakOuter() void {
+fn testBreakOuter() !void {
var array = "aoeu";
var count: usize = 0;
outer: for (array) |_| {
@@ -92,15 +92,15 @@ fn testBreakOuter() void {
break :outer;
}
}
- expect(count == 1);
+ try expect(count == 1);
}
test "continue outer for loop" {
- testContinueOuter();
- comptime testContinueOuter();
+ try testContinueOuter();
+ comptime try testContinueOuter();
}
-fn testContinueOuter() void {
+fn testContinueOuter() !void {
var array = "aoeu";
var counter: usize = 0;
outer: for (array) |_| {
@@ -109,28 +109,28 @@ fn testContinueOuter() void {
continue :outer;
}
}
- expect(counter == array.len);
+ try expect(counter == array.len);
}
test "2 break statements and an else" {
const S = struct {
- fn entry(t: bool, f: bool) void {
+ fn entry(t: bool, f: bool) !void {
var buf: [10]u8 = undefined;
var ok = false;
ok = for (buf) |item| {
if (f) break false;
if (t) break true;
} else false;
- expect(ok);
+ try expect(ok);
}
};
- S.entry(true, false);
- comptime S.entry(true, false);
+ try S.entry(true, false);
+ comptime try S.entry(true, false);
}
test "for with null and T peer types and inferred result location type" {
const S = struct {
- fn doTheTest(slice: []const u8) void {
+ fn doTheTest(slice: []const u8) !void {
if (for (slice) |item| {
if (item == 10) {
break item;
@@ -140,33 +140,33 @@ test "for with null and T peer types and inferred result location type" {
}
}
};
- S.doTheTest(&[_]u8{ 1, 2 });
- comptime S.doTheTest(&[_]u8{ 1, 2 });
+ try S.doTheTest(&[_]u8{ 1, 2 });
+ comptime try S.doTheTest(&[_]u8{ 1, 2 });
}
test "for copies its payload" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var x = [_]usize{ 1, 2, 3 };
for (x) |value, i| {
// Modify the original array
x[i] += 99;
- expectEqual(value, i + 1);
+ try expectEqual(value, i + 1);
}
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "for on slice with allowzero ptr" {
const S = struct {
- fn doTheTest(slice: []const u8) void {
+ fn doTheTest(slice: []const u8) !void {
var ptr = @ptrCast([*]allowzero const u8, slice.ptr)[0..slice.len];
- for (ptr) |x, i| expect(x == i + 1);
- for (ptr) |*x, i| expect(x.* == i + 1);
+ for (ptr) |x, i| try expect(x == i + 1);
+ for (ptr) |*x, i| try expect(x.* == i + 1);
}
};
- S.doTheTest(&[_]u8{ 1, 2, 3, 4 });
- comptime S.doTheTest(&[_]u8{ 1, 2, 3, 4 });
+ try S.doTheTest(&[_]u8{ 1, 2, 3, 4 });
+ comptime try S.doTheTest(&[_]u8{ 1, 2, 3, 4 });
}
test/stage1/behavior/generics.zig
@@ -4,9 +4,9 @@ const expect = testing.expect;
const expectEqual = testing.expectEqual;
test "simple generic fn" {
- expect(max(i32, 3, -1) == 3);
- expect(max(f32, 0.123, 0.456) == 0.456);
- expect(add(2, 3) == 5);
+ try expect(max(i32, 3, -1) == 3);
+ try expect(max(f32, 0.123, 0.456) == 0.456);
+ try expect(add(2, 3) == 5);
}
fn max(comptime T: type, a: T, b: T) T {
@@ -19,7 +19,7 @@ fn add(comptime a: i32, b: i32) i32 {
const the_max = max(u32, 1234, 5678);
test "compile time generic eval" {
- expect(the_max == 5678);
+ try expect(the_max == 5678);
}
fn gimmeTheBigOne(a: u32, b: u32) u32 {
@@ -35,19 +35,19 @@ fn sameButWithFloats(a: f64, b: f64) f64 {
}
test "fn with comptime args" {
- expect(gimmeTheBigOne(1234, 5678) == 5678);
- expect(shouldCallSameInstance(34, 12) == 34);
- expect(sameButWithFloats(0.43, 0.49) == 0.49);
+ try expect(gimmeTheBigOne(1234, 5678) == 5678);
+ try expect(shouldCallSameInstance(34, 12) == 34);
+ try expect(sameButWithFloats(0.43, 0.49) == 0.49);
}
test "var params" {
- expect(max_i32(12, 34) == 34);
- expect(max_f64(1.2, 3.4) == 3.4);
+ try expect(max_i32(12, 34) == 34);
+ try expect(max_f64(1.2, 3.4) == 3.4);
}
comptime {
- expect(max_i32(12, 34) == 34);
- expect(max_f64(1.2, 3.4) == 3.4);
+ try expect(max_i32(12, 34) == 34);
+ try expect(max_f64(1.2, 3.4) == 3.4);
}
fn max_var(a: anytype, b: anytype) @TypeOf(a + b) {
@@ -79,8 +79,8 @@ test "function with return type type" {
var list2: List(i32) = undefined;
list.length = 10;
list2.length = 10;
- expect(list.prealloc_items.len == 8);
- expect(list2.prealloc_items.len == 8);
+ try expect(list.prealloc_items.len == 8);
+ try expect(list2.prealloc_items.len == 8);
}
test "generic struct" {
@@ -92,9 +92,9 @@ test "generic struct" {
.value = true,
.next = null,
};
- expect(a1.value == 13);
- expect(a1.value == a1.getVal());
- expect(b1.getVal());
+ try expect(a1.value == 13);
+ try expect(a1.value == a1.getVal());
+ try expect(b1.getVal());
}
fn GenNode(comptime T: type) type {
return struct {
@@ -107,7 +107,7 @@ fn GenNode(comptime T: type) type {
}
test "const decls in struct" {
- expect(GenericDataThing(3).count_plus_one == 4);
+ try expect(GenericDataThing(3).count_plus_one == 4);
}
fn GenericDataThing(comptime count: isize) type {
return struct {
@@ -116,15 +116,15 @@ fn GenericDataThing(comptime count: isize) type {
}
test "use generic param in generic param" {
- expect(aGenericFn(i32, 3, 4) == 7);
+ try expect(aGenericFn(i32, 3, 4) == 7);
}
fn aGenericFn(comptime T: type, comptime a: T, b: T) T {
return a + b;
}
test "generic fn with implicit cast" {
- expect(getFirstByte(u8, &[_]u8{13}) == 13);
- expect(getFirstByte(u16, &[_]u16{
+ try expect(getFirstByte(u8, &[_]u8{13}) == 13);
+ try expect(getFirstByte(u16, &[_]u16{
0,
13,
}) == 0);
@@ -149,21 +149,21 @@ fn foo2(arg: anytype) bool {
}
test "array of generic fns" {
- expect(foos[0](true));
- expect(!foos[1](true));
+ try expect(foos[0](true));
+ try expect(!foos[1](true));
}
test "generic fn keeps non-generic parameter types" {
const A = 128;
const S = struct {
- fn f(comptime T: type, s: []T) void {
- expect(A != @typeInfo(@TypeOf(s)).Pointer.alignment);
+ fn f(comptime T: type, s: []T) !void {
+ try expect(A != @typeInfo(@TypeOf(s)).Pointer.alignment);
}
};
// The compiler monomorphizes `S.f` for `T=u8` on its first use, check that
// `x` type not affect `s` parameter type.
var x: [16]u8 align(A) = undefined;
- S.f(u8, &x);
+ try S.f(u8, &x);
}
test/stage1/behavior/hasdecl.zig
@@ -11,11 +11,11 @@ const Bar = struct {
};
test "@hasDecl" {
- expect(@hasDecl(Foo, "public_thing"));
- expect(!@hasDecl(Foo, "private_thing"));
- expect(!@hasDecl(Foo, "no_thing"));
+ try expect(@hasDecl(Foo, "public_thing"));
+ try expect(!@hasDecl(Foo, "private_thing"));
+ try expect(!@hasDecl(Foo, "no_thing"));
- expect(@hasDecl(Bar, "hi"));
- expect(@hasDecl(Bar, "blah"));
- expect(!@hasDecl(Bar, "nope"));
+ try expect(@hasDecl(Bar, "hi"));
+ try expect(@hasDecl(Bar, "blah"));
+ try expect(!@hasDecl(Bar, "nope"));
}
test/stage1/behavior/hasfield.zig
@@ -8,10 +8,10 @@ test "@hasField" {
pub const nope = 1;
};
- expect(@hasField(struc, "a") == true);
- expect(@hasField(struc, "b") == true);
- expect(@hasField(struc, "non-existant") == false);
- expect(@hasField(struc, "nope") == false);
+ try expect(@hasField(struc, "a") == true);
+ try expect(@hasField(struc, "b") == true);
+ try expect(@hasField(struc, "non-existant") == false);
+ try expect(@hasField(struc, "nope") == false);
const unin = union {
a: u64,
@@ -19,10 +19,10 @@ test "@hasField" {
pub const nope = 1;
};
- expect(@hasField(unin, "a") == true);
- expect(@hasField(unin, "b") == true);
- expect(@hasField(unin, "non-existant") == false);
- expect(@hasField(unin, "nope") == false);
+ try expect(@hasField(unin, "a") == true);
+ try expect(@hasField(unin, "b") == true);
+ try expect(@hasField(unin, "non-existant") == false);
+ try expect(@hasField(unin, "nope") == false);
const enm = enum {
a,
@@ -30,8 +30,8 @@ test "@hasField" {
pub const nope = 1;
};
- expect(@hasField(enm, "a") == true);
- expect(@hasField(enm, "b") == true);
- expect(@hasField(enm, "non-existant") == false);
- expect(@hasField(enm, "nope") == false);
+ try expect(@hasField(enm, "a") == true);
+ try expect(@hasField(enm, "b") == true);
+ try expect(@hasField(enm, "non-existant") == false);
+ try expect(@hasField(enm, "nope") == false);
}
test/stage1/behavior/if.zig
@@ -26,7 +26,7 @@ fn firstEqlThird(a: i32, b: i32, c: i32) void {
}
test "else if expression" {
- expect(elseIfExpressionF(1) == 1);
+ try expect(elseIfExpressionF(1) == 1);
}
fn elseIfExpressionF(c: u8) u8 {
if (c == 0) {
@@ -44,14 +44,14 @@ var global_with_err: anyerror!u32 = error.SomeError;
test "unwrap mutable global var" {
if (global_with_val) |v| {
- expect(v == 0);
+ try expect(v == 0);
} else |e| {
unreachable;
}
if (global_with_err) |_| {
unreachable;
} else |e| {
- expect(e == error.SomeError);
+ try expect(e == error.SomeError);
}
}
@@ -63,7 +63,7 @@ test "labeled break inside comptime if inside runtime if" {
break :blk @as(i32, 42);
};
}
- expect(answer == 42);
+ try expect(answer == 42);
}
test "const result loc, runtime if cond, else unreachable" {
@@ -74,36 +74,36 @@ test "const result loc, runtime if cond, else unreachable" {
var t = true;
const x = if (t) Num.Two else unreachable;
- expect(x == .Two);
+ try expect(x == .Two);
}
test "if prongs cast to expected type instead of peer type resolution" {
const S = struct {
- fn doTheTest(f: bool) void {
+ fn doTheTest(f: bool) !void {
var x: i32 = 0;
x = if (f) 1 else 2;
- expect(x == 2);
+ try expect(x == 2);
var b = true;
const y: i32 = if (b) 1 else 2;
- expect(y == 1);
+ try expect(y == 1);
}
};
- S.doTheTest(false);
- comptime S.doTheTest(false);
+ try S.doTheTest(false);
+ comptime try S.doTheTest(false);
}
test "while copies its payload" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var tmp: ?i32 = 10;
if (tmp) |value| {
// Modify the original variable
tmp = null;
- expectEqual(@as(i32, 10), value);
+ try expectEqual(@as(i32, 10), value);
} else unreachable;
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test/stage1/behavior/import.zig
@@ -3,18 +3,18 @@ const expectEqual = @import("std").testing.expectEqual;
const a_namespace = @import("import/a_namespace.zig");
test "call fn via namespace lookup" {
- expectEqual(@as(i32, 1234), a_namespace.foo());
+ try expectEqual(@as(i32, 1234), a_namespace.foo());
}
test "importing the same thing gives the same import" {
- expect(@import("std") == @import("std"));
+ try expect(@import("std") == @import("std"));
}
test "import in non-toplevel scope" {
const S = struct {
usingnamespace @import("import/a_namespace.zig");
};
- expectEqual(@as(i32, 1234), S.foo());
+ try expectEqual(@as(i32, 1234), S.foo());
}
test "import empty file" {
test/stage1/behavior/incomplete_struct_param_tld.zig
@@ -26,5 +26,5 @@ test "incomplete struct param top level declaration" {
.c = C{ .x = 13 },
},
};
- expect(foo(a) == 13);
+ try expect(foo(a) == 13);
}
test/stage1/behavior/inttoptr.zig
@@ -1,7 +1,3 @@
-const builtin = @import("builtin");
-const std = @import("std");
-const expect = std.testing.expect;
-
test "casting random address to function pointer" {
randomAddressToFunction();
comptime randomAddressToFunction();
test/stage1/behavior/ir_block_deps.zig
@@ -16,6 +16,6 @@ fn getErrInt() anyerror!i32 {
}
test "ir block deps" {
- expect((foo(1) catch unreachable) == 0);
- expect((foo(2) catch unreachable) == 0);
+ try expect((foo(1) catch unreachable) == 0);
+ try expect((foo(2) catch unreachable) == 0);
}
test/stage1/behavior/math.zig
@@ -7,71 +7,71 @@ const minInt = std.math.minInt;
const mem = std.mem;
test "division" {
- testDivision();
- comptime testDivision();
-}
-fn testDivision() void {
- expect(div(u32, 13, 3) == 4);
- expect(div(f16, 1.0, 2.0) == 0.5);
- expect(div(f32, 1.0, 2.0) == 0.5);
-
- expect(divExact(u32, 55, 11) == 5);
- expect(divExact(i32, -55, 11) == -5);
- expect(divExact(f16, 55.0, 11.0) == 5.0);
- expect(divExact(f16, -55.0, 11.0) == -5.0);
- expect(divExact(f32, 55.0, 11.0) == 5.0);
- expect(divExact(f32, -55.0, 11.0) == -5.0);
-
- expect(divFloor(i32, 5, 3) == 1);
- expect(divFloor(i32, -5, 3) == -2);
- expect(divFloor(f16, 5.0, 3.0) == 1.0);
- expect(divFloor(f16, -5.0, 3.0) == -2.0);
- expect(divFloor(f32, 5.0, 3.0) == 1.0);
- expect(divFloor(f32, -5.0, 3.0) == -2.0);
- expect(divFloor(i32, -0x80000000, -2) == 0x40000000);
- expect(divFloor(i32, 0, -0x80000000) == 0);
- expect(divFloor(i32, -0x40000001, 0x40000000) == -2);
- expect(divFloor(i32, -0x80000000, 1) == -0x80000000);
- expect(divFloor(i32, 10, 12) == 0);
- expect(divFloor(i32, -14, 12) == -2);
- expect(divFloor(i32, -2, 12) == -1);
-
- expect(divTrunc(i32, 5, 3) == 1);
- expect(divTrunc(i32, -5, 3) == -1);
- expect(divTrunc(f16, 5.0, 3.0) == 1.0);
- expect(divTrunc(f16, -5.0, 3.0) == -1.0);
- expect(divTrunc(f32, 5.0, 3.0) == 1.0);
- expect(divTrunc(f32, -5.0, 3.0) == -1.0);
- expect(divTrunc(f64, 5.0, 3.0) == 1.0);
- expect(divTrunc(f64, -5.0, 3.0) == -1.0);
- expect(divTrunc(i32, 10, 12) == 0);
- expect(divTrunc(i32, -14, 12) == -1);
- expect(divTrunc(i32, -2, 12) == 0);
-
- expect(mod(i32, 10, 12) == 10);
- expect(mod(i32, -14, 12) == 10);
- expect(mod(i32, -2, 12) == 10);
+ try testDivision();
+ comptime try testDivision();
+}
+fn testDivision() !void {
+ try expect(div(u32, 13, 3) == 4);
+ try expect(div(f16, 1.0, 2.0) == 0.5);
+ try expect(div(f32, 1.0, 2.0) == 0.5);
+
+ try expect(divExact(u32, 55, 11) == 5);
+ try expect(divExact(i32, -55, 11) == -5);
+ try expect(divExact(f16, 55.0, 11.0) == 5.0);
+ try expect(divExact(f16, -55.0, 11.0) == -5.0);
+ try expect(divExact(f32, 55.0, 11.0) == 5.0);
+ try expect(divExact(f32, -55.0, 11.0) == -5.0);
+
+ try expect(divFloor(i32, 5, 3) == 1);
+ try expect(divFloor(i32, -5, 3) == -2);
+ try expect(divFloor(f16, 5.0, 3.0) == 1.0);
+ try expect(divFloor(f16, -5.0, 3.0) == -2.0);
+ try expect(divFloor(f32, 5.0, 3.0) == 1.0);
+ try expect(divFloor(f32, -5.0, 3.0) == -2.0);
+ try expect(divFloor(i32, -0x80000000, -2) == 0x40000000);
+ try expect(divFloor(i32, 0, -0x80000000) == 0);
+ try expect(divFloor(i32, -0x40000001, 0x40000000) == -2);
+ try expect(divFloor(i32, -0x80000000, 1) == -0x80000000);
+ try expect(divFloor(i32, 10, 12) == 0);
+ try expect(divFloor(i32, -14, 12) == -2);
+ try expect(divFloor(i32, -2, 12) == -1);
+
+ try expect(divTrunc(i32, 5, 3) == 1);
+ try expect(divTrunc(i32, -5, 3) == -1);
+ try expect(divTrunc(f16, 5.0, 3.0) == 1.0);
+ try expect(divTrunc(f16, -5.0, 3.0) == -1.0);
+ try expect(divTrunc(f32, 5.0, 3.0) == 1.0);
+ try expect(divTrunc(f32, -5.0, 3.0) == -1.0);
+ try expect(divTrunc(f64, 5.0, 3.0) == 1.0);
+ try expect(divTrunc(f64, -5.0, 3.0) == -1.0);
+ try expect(divTrunc(i32, 10, 12) == 0);
+ try expect(divTrunc(i32, -14, 12) == -1);
+ try expect(divTrunc(i32, -2, 12) == 0);
+
+ try expect(mod(i32, 10, 12) == 10);
+ try expect(mod(i32, -14, 12) == 10);
+ try expect(mod(i32, -2, 12) == 10);
comptime {
- expect(
+ try expect(
1194735857077236777412821811143690633098347576 % 508740759824825164163191790951174292733114988 == 177254337427586449086438229241342047632117600,
);
- expect(
+ try expect(
@rem(-1194735857077236777412821811143690633098347576, 508740759824825164163191790951174292733114988) == -177254337427586449086438229241342047632117600,
);
- expect(
+ try expect(
1194735857077236777412821811143690633098347576 / 508740759824825164163191790951174292733114988 == 2,
);
- expect(
+ try expect(
@divTrunc(-1194735857077236777412821811143690633098347576, 508740759824825164163191790951174292733114988) == -2,
);
- expect(
+ try expect(
@divTrunc(1194735857077236777412821811143690633098347576, -508740759824825164163191790951174292733114988) == -2,
);
- expect(
+ try expect(
@divTrunc(-1194735857077236777412821811143690633098347576, -508740759824825164163191790951174292733114988) == 2,
);
- expect(
+ try expect(
4126227191251978491697987544882340798050766755606969681711 % 10 == 1,
);
}
@@ -94,9 +94,9 @@ fn mod(comptime T: type, a: T, b: T) T {
test "@addWithOverflow" {
var result: u8 = undefined;
- expect(@addWithOverflow(u8, 250, 100, &result));
- expect(!@addWithOverflow(u8, 100, 150, &result));
- expect(result == 250);
+ try expect(@addWithOverflow(u8, 250, 100, &result));
+ try expect(!@addWithOverflow(u8, 100, 150, &result));
+ try expect(result == 250);
}
// TODO test mulWithOverflow
@@ -104,31 +104,31 @@ test "@addWithOverflow" {
test "@shlWithOverflow" {
var result: u16 = undefined;
- expect(@shlWithOverflow(u16, 0b0010111111111111, 3, &result));
- expect(!@shlWithOverflow(u16, 0b0010111111111111, 2, &result));
- expect(result == 0b1011111111111100);
+ try expect(@shlWithOverflow(u16, 0b0010111111111111, 3, &result));
+ try expect(!@shlWithOverflow(u16, 0b0010111111111111, 2, &result));
+ try expect(result == 0b1011111111111100);
}
test "@*WithOverflow with u0 values" {
var result: u0 = undefined;
- expect(!@addWithOverflow(u0, 0, 0, &result));
- expect(!@subWithOverflow(u0, 0, 0, &result));
- expect(!@mulWithOverflow(u0, 0, 0, &result));
- expect(!@shlWithOverflow(u0, 0, 0, &result));
+ try expect(!@addWithOverflow(u0, 0, 0, &result));
+ try expect(!@subWithOverflow(u0, 0, 0, &result));
+ try expect(!@mulWithOverflow(u0, 0, 0, &result));
+ try expect(!@shlWithOverflow(u0, 0, 0, &result));
}
test "@clz" {
- testClz();
- comptime testClz();
+ try testClz();
+ comptime try testClz();
}
-fn testClz() void {
- expect(clz(u8, 0b10001010) == 0);
- expect(clz(u8, 0b00001010) == 4);
- expect(clz(u8, 0b00011010) == 3);
- expect(clz(u8, 0b00000000) == 8);
- expect(clz(u128, 0xffffffffffffffff) == 64);
- expect(clz(u128, 0x10000000000000000) == 63);
+fn testClz() !void {
+ try expect(clz(u8, 0b10001010) == 0);
+ try expect(clz(u8, 0b00001010) == 4);
+ try expect(clz(u8, 0b00011010) == 3);
+ try expect(clz(u8, 0b00000000) == 8);
+ try expect(clz(u128, 0xffffffffffffffff) == 64);
+ try expect(clz(u128, 0x10000000000000000) == 63);
}
fn clz(comptime T: type, x: T) usize {
@@ -136,15 +136,15 @@ fn clz(comptime T: type, x: T) usize {
}
test "@ctz" {
- testCtz();
- comptime testCtz();
+ try testCtz();
+ comptime try testCtz();
}
-fn testCtz() void {
- expect(ctz(u8, 0b10100000) == 5);
- expect(ctz(u8, 0b10001010) == 1);
- expect(ctz(u8, 0b00000000) == 8);
- expect(ctz(u16, 0b00000000) == 16);
+fn testCtz() !void {
+ try expect(ctz(u8, 0b10100000) == 5);
+ try expect(ctz(u8, 0b10001010) == 1);
+ try expect(ctz(u8, 0b00000000) == 8);
+ try expect(ctz(u16, 0b00000000) == 16);
}
fn ctz(comptime T: type, x: T) usize {
@@ -154,109 +154,109 @@ fn ctz(comptime T: type, x: T) usize {
test "assignment operators" {
var i: u32 = 0;
i += 5;
- expect(i == 5);
+ try expect(i == 5);
i -= 2;
- expect(i == 3);
+ try expect(i == 3);
i *= 20;
- expect(i == 60);
+ try expect(i == 60);
i /= 3;
- expect(i == 20);
+ try expect(i == 20);
i %= 11;
- expect(i == 9);
+ try expect(i == 9);
i <<= 1;
- expect(i == 18);
+ try expect(i == 18);
i >>= 2;
- expect(i == 4);
+ try expect(i == 4);
i = 6;
i &= 5;
- expect(i == 4);
+ try expect(i == 4);
i ^= 6;
- expect(i == 2);
+ try expect(i == 2);
i = 6;
i |= 3;
- expect(i == 7);
+ try expect(i == 7);
}
test "three expr in a row" {
- testThreeExprInARow(false, true);
- comptime testThreeExprInARow(false, true);
-}
-fn testThreeExprInARow(f: bool, t: bool) void {
- assertFalse(f or f or f);
- assertFalse(t and t and f);
- assertFalse(1 | 2 | 4 != 7);
- assertFalse(3 ^ 6 ^ 8 != 13);
- assertFalse(7 & 14 & 28 != 4);
- assertFalse(9 << 1 << 2 != 9 << 3);
- assertFalse(90 >> 1 >> 2 != 90 >> 3);
- assertFalse(100 - 1 + 1000 != 1099);
- assertFalse(5 * 4 / 2 % 3 != 1);
- assertFalse(@as(i32, @as(i32, 5)) != 5);
- assertFalse(!!false);
- assertFalse(@as(i32, 7) != --(@as(i32, 7)));
-}
-fn assertFalse(b: bool) void {
- expect(!b);
+ try testThreeExprInARow(false, true);
+ comptime try testThreeExprInARow(false, true);
+}
+fn testThreeExprInARow(f: bool, t: bool) !void {
+ try assertFalse(f or f or f);
+ try assertFalse(t and t and f);
+ try assertFalse(1 | 2 | 4 != 7);
+ try assertFalse(3 ^ 6 ^ 8 != 13);
+ try assertFalse(7 & 14 & 28 != 4);
+ try assertFalse(9 << 1 << 2 != 9 << 3);
+ try assertFalse(90 >> 1 >> 2 != 90 >> 3);
+ try assertFalse(100 - 1 + 1000 != 1099);
+ try assertFalse(5 * 4 / 2 % 3 != 1);
+ try assertFalse(@as(i32, @as(i32, 5)) != 5);
+ try assertFalse(!!false);
+ try assertFalse(@as(i32, 7) != --(@as(i32, 7)));
+}
+fn assertFalse(b: bool) !void {
+ try expect(!b);
}
test "const number literal" {
const one = 1;
const eleven = ten + one;
- expect(eleven == 11);
+ try expect(eleven == 11);
}
const ten = 10;
test "unsigned wrapping" {
- testUnsignedWrappingEval(maxInt(u32));
- comptime testUnsignedWrappingEval(maxInt(u32));
+ try testUnsignedWrappingEval(maxInt(u32));
+ comptime try testUnsignedWrappingEval(maxInt(u32));
}
-fn testUnsignedWrappingEval(x: u32) void {
+fn testUnsignedWrappingEval(x: u32) !void {
const zero = x +% 1;
- expect(zero == 0);
+ try expect(zero == 0);
const orig = zero -% 1;
- expect(orig == maxInt(u32));
+ try expect(orig == maxInt(u32));
}
test "signed wrapping" {
- testSignedWrappingEval(maxInt(i32));
- comptime testSignedWrappingEval(maxInt(i32));
+ try testSignedWrappingEval(maxInt(i32));
+ comptime try testSignedWrappingEval(maxInt(i32));
}
-fn testSignedWrappingEval(x: i32) void {
+fn testSignedWrappingEval(x: i32) !void {
const min_val = x +% 1;
- expect(min_val == minInt(i32));
+ try expect(min_val == minInt(i32));
const max_val = min_val -% 1;
- expect(max_val == maxInt(i32));
+ try expect(max_val == maxInt(i32));
}
test "signed negation wrapping" {
- testSignedNegationWrappingEval(minInt(i16));
- comptime testSignedNegationWrappingEval(minInt(i16));
+ try testSignedNegationWrappingEval(minInt(i16));
+ comptime try testSignedNegationWrappingEval(minInt(i16));
}
-fn testSignedNegationWrappingEval(x: i16) void {
- expect(x == -32768);
+fn testSignedNegationWrappingEval(x: i16) !void {
+ try expect(x == -32768);
const neg = -%x;
- expect(neg == -32768);
+ try expect(neg == -32768);
}
test "unsigned negation wrapping" {
- testUnsignedNegationWrappingEval(1);
- comptime testUnsignedNegationWrappingEval(1);
+ try testUnsignedNegationWrappingEval(1);
+ comptime try testUnsignedNegationWrappingEval(1);
}
-fn testUnsignedNegationWrappingEval(x: u16) void {
- expect(x == 1);
+fn testUnsignedNegationWrappingEval(x: u16) !void {
+ try expect(x == 1);
const neg = -%x;
- expect(neg == maxInt(u16));
+ try expect(neg == maxInt(u16));
}
test "unsigned 64-bit division" {
- test_u64_div();
- comptime test_u64_div();
+ try test_u64_div();
+ comptime try test_u64_div();
}
-fn test_u64_div() void {
+fn test_u64_div() !void {
const result = divWithResult(1152921504606846976, 34359738365);
- expect(result.quotient == 33554432);
- expect(result.remainder == 100663296);
+ try expect(result.quotient == 33554432);
+ try expect(result.remainder == 100663296);
}
fn divWithResult(a: u64, b: u64) DivResult {
return DivResult{
@@ -270,62 +270,62 @@ const DivResult = struct {
};
test "binary not" {
- expect(comptime x: {
+ try expect(comptime x: {
break :x ~@as(u16, 0b1010101010101010) == 0b0101010101010101;
});
- expect(comptime x: {
+ try expect(comptime x: {
break :x ~@as(u64, 2147483647) == 18446744071562067968;
});
- testBinaryNot(0b1010101010101010);
+ try testBinaryNot(0b1010101010101010);
}
-fn testBinaryNot(x: u16) void {
- expect(~x == 0b0101010101010101);
+fn testBinaryNot(x: u16) !void {
+ try expect(~x == 0b0101010101010101);
}
test "small int addition" {
var x: u2 = 0;
- expect(x == 0);
+ try expect(x == 0);
x += 1;
- expect(x == 1);
+ try expect(x == 1);
x += 1;
- expect(x == 2);
+ try expect(x == 2);
x += 1;
- expect(x == 3);
+ try expect(x == 3);
var result: @TypeOf(x) = 3;
- expect(@addWithOverflow(@TypeOf(x), x, 1, &result));
+ try expect(@addWithOverflow(@TypeOf(x), x, 1, &result));
- expect(result == 0);
+ try expect(result == 0);
}
test "float equality" {
const x: f64 = 0.012;
const y: f64 = x + 1.0;
- testFloatEqualityImpl(x, y);
- comptime testFloatEqualityImpl(x, y);
+ try testFloatEqualityImpl(x, y);
+ comptime try testFloatEqualityImpl(x, y);
}
-fn testFloatEqualityImpl(x: f64, y: f64) void {
+fn testFloatEqualityImpl(x: f64, y: f64) !void {
const y2 = x + 1.0;
- expect(y == y2);
+ try expect(y == y2);
}
test "allow signed integer division/remainder when values are comptime known and positive or exact" {
- expect(5 / 3 == 1);
- expect(-5 / -3 == 1);
- expect(-6 / 3 == -2);
+ try expect(5 / 3 == 1);
+ try expect(-5 / -3 == 1);
+ try expect(-6 / 3 == -2);
- expect(5 % 3 == 2);
- expect(-6 % 3 == 0);
+ try expect(5 % 3 == 2);
+ try expect(-6 % 3 == 0);
}
test "hex float literal parsing" {
- comptime expect(0x1.0 == 1.0);
+ comptime try expect(0x1.0 == 1.0);
}
test "quad hex float literal parsing in range" {
@@ -340,29 +340,29 @@ test "quad hex float literal parsing accurate" {
// implied 1 is dropped, with an exponent of 0 (0x3fff) after biasing.
const expected: u128 = 0x3fff1111222233334444555566667777;
- expect(@bitCast(u128, a) == expected);
+ try expect(@bitCast(u128, a) == expected);
// non-normalized
const b: f128 = 0x11.111222233334444555566667777p-4;
- expect(@bitCast(u128, b) == expected);
+ try expect(@bitCast(u128, b) == expected);
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
{
var f: f128 = 0x1.2eab345678439abcdefea56782346p+5;
- expect(@bitCast(u128, f) == 0x40042eab345678439abcdefea5678234);
+ try expect(@bitCast(u128, f) == 0x40042eab345678439abcdefea5678234);
}
{
var f: f128 = 0x1.edcb34a235253948765432134674fp-1;
- expect(@bitCast(u128, f) == 0x3ffeedcb34a235253948765432134674);
+ try expect(@bitCast(u128, f) == 0x3ffeedcb34a235253948765432134674);
}
{
var f: f128 = 0x1.353e45674d89abacc3a2ebf3ff4ffp-50;
- expect(@bitCast(u128, f) == 0x3fcd353e45674d89abacc3a2ebf3ff50);
+ try expect(@bitCast(u128, f) == 0x3fcd353e45674d89abacc3a2ebf3ff50);
}
{
var f: f128 = 0x1.ed8764648369535adf4be3214567fp-9;
- expect(@bitCast(u128, f) == 0x3ff6ed8764648369535adf4be3214568);
+ try expect(@bitCast(u128, f) == 0x3ff6ed8764648369535adf4be3214568);
}
const exp2ft = [_]f64{
0x1.6a09e667f3bcdp-1,
@@ -417,40 +417,40 @@ test "quad hex float literal parsing accurate" {
};
for (exp2ft) |x, i| {
- expect(@bitCast(u64, x) == answers[i]);
+ try expect(@bitCast(u64, x) == answers[i]);
}
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "underscore separator parsing" {
- expect(0_0_0_0 == 0);
- expect(1_234_567 == 1234567);
- expect(001_234_567 == 1234567);
- expect(0_0_1_2_3_4_5_6_7 == 1234567);
+ try expect(0_0_0_0 == 0);
+ try expect(1_234_567 == 1234567);
+ try expect(001_234_567 == 1234567);
+ try expect(0_0_1_2_3_4_5_6_7 == 1234567);
- expect(0b0_0_0_0 == 0);
- expect(0b1010_1010 == 0b10101010);
- expect(0b0000_1010_1010 == 0b10101010);
- expect(0b1_0_1_0_1_0_1_0 == 0b10101010);
+ try expect(0b0_0_0_0 == 0);
+ try expect(0b1010_1010 == 0b10101010);
+ try expect(0b0000_1010_1010 == 0b10101010);
+ try expect(0b1_0_1_0_1_0_1_0 == 0b10101010);
- expect(0o0_0_0_0 == 0);
- expect(0o1010_1010 == 0o10101010);
- expect(0o0000_1010_1010 == 0o10101010);
- expect(0o1_0_1_0_1_0_1_0 == 0o10101010);
+ try expect(0o0_0_0_0 == 0);
+ try expect(0o1010_1010 == 0o10101010);
+ try expect(0o0000_1010_1010 == 0o10101010);
+ try expect(0o1_0_1_0_1_0_1_0 == 0o10101010);
- expect(0x0_0_0_0 == 0);
- expect(0x1010_1010 == 0x10101010);
- expect(0x0000_1010_1010 == 0x10101010);
- expect(0x1_0_1_0_1_0_1_0 == 0x10101010);
+ try expect(0x0_0_0_0 == 0);
+ try expect(0x1010_1010 == 0x10101010);
+ try expect(0x0000_1010_1010 == 0x10101010);
+ try expect(0x1_0_1_0_1_0_1_0 == 0x10101010);
- expect(123_456.789_000e1_0 == 123456.789000e10);
- expect(0_1_2_3_4_5_6.7_8_9_0_0_0e0_0_1_0 == 123456.789000e10);
+ try expect(123_456.789_000e1_0 == 123456.789000e10);
+ try expect(0_1_2_3_4_5_6.7_8_9_0_0_0e0_0_1_0 == 123456.789000e10);
- expect(0x1234_5678.9ABC_DEF0p-1_0 == 0x12345678.9ABCDEF0p-10);
- expect(0x1_2_3_4_5_6_7_8.9_A_B_C_D_E_F_0p-0_0_0_1_0 == 0x12345678.9ABCDEF0p-10);
+ try expect(0x1234_5678.9ABC_DEF0p-1_0 == 0x12345678.9ABCDEF0p-10);
+ try expect(0x1_2_3_4_5_6_7_8.9_A_B_C_D_E_F_0p-0_0_0_1_0 == 0x12345678.9ABCDEF0p-10);
}
test "hex float literal within range" {
@@ -460,73 +460,73 @@ test "hex float literal within range" {
}
test "truncating shift left" {
- testShlTrunc(maxInt(u16));
- comptime testShlTrunc(maxInt(u16));
+ try testShlTrunc(maxInt(u16));
+ comptime try testShlTrunc(maxInt(u16));
}
-fn testShlTrunc(x: u16) void {
+fn testShlTrunc(x: u16) !void {
const shifted = x << 1;
- expect(shifted == 65534);
+ try expect(shifted == 65534);
}
test "truncating shift right" {
- testShrTrunc(maxInt(u16));
- comptime testShrTrunc(maxInt(u16));
+ try testShrTrunc(maxInt(u16));
+ comptime try testShrTrunc(maxInt(u16));
}
-fn testShrTrunc(x: u16) void {
+fn testShrTrunc(x: u16) !void {
const shifted = x >> 1;
- expect(shifted == 32767);
+ try expect(shifted == 32767);
}
test "exact shift left" {
- testShlExact(0b00110101);
- comptime testShlExact(0b00110101);
+ try testShlExact(0b00110101);
+ comptime try testShlExact(0b00110101);
}
-fn testShlExact(x: u8) void {
+fn testShlExact(x: u8) !void {
const shifted = @shlExact(x, 2);
- expect(shifted == 0b11010100);
+ try expect(shifted == 0b11010100);
}
test "exact shift right" {
- testShrExact(0b10110100);
- comptime testShrExact(0b10110100);
+ try testShrExact(0b10110100);
+ comptime try testShrExact(0b10110100);
}
-fn testShrExact(x: u8) void {
+fn testShrExact(x: u8) !void {
const shifted = @shrExact(x, 2);
- expect(shifted == 0b00101101);
+ try expect(shifted == 0b00101101);
}
test "shift left/right on u0 operand" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var x: u0 = 0;
var y: u0 = 0;
- expectEqual(@as(u0, 0), x << 0);
- expectEqual(@as(u0, 0), x >> 0);
- expectEqual(@as(u0, 0), x << y);
- expectEqual(@as(u0, 0), x >> y);
- expectEqual(@as(u0, 0), @shlExact(x, 0));
- expectEqual(@as(u0, 0), @shrExact(x, 0));
- expectEqual(@as(u0, 0), @shlExact(x, y));
- expectEqual(@as(u0, 0), @shrExact(x, y));
+ try expectEqual(@as(u0, 0), x << 0);
+ try expectEqual(@as(u0, 0), x >> 0);
+ try expectEqual(@as(u0, 0), x << y);
+ try expectEqual(@as(u0, 0), x >> y);
+ try expectEqual(@as(u0, 0), @shlExact(x, 0));
+ try expectEqual(@as(u0, 0), @shrExact(x, 0));
+ try expectEqual(@as(u0, 0), @shlExact(x, y));
+ try expectEqual(@as(u0, 0), @shrExact(x, y));
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "comptime_int addition" {
comptime {
- expect(35361831660712422535336160538497375248 + 101752735581729509668353361206450473702 == 137114567242441932203689521744947848950);
- expect(594491908217841670578297176641415611445982232488944558774612 + 390603545391089362063884922208143568023166603618446395589768 == 985095453608931032642182098849559179469148836107390954364380);
+ try expect(35361831660712422535336160538497375248 + 101752735581729509668353361206450473702 == 137114567242441932203689521744947848950);
+ try expect(594491908217841670578297176641415611445982232488944558774612 + 390603545391089362063884922208143568023166603618446395589768 == 985095453608931032642182098849559179469148836107390954364380);
}
}
test "comptime_int multiplication" {
comptime {
- expect(
+ try expect(
45960427431263824329884196484953148229 * 128339149605334697009938835852565949723 == 5898522172026096622534201617172456926982464453350084962781392314016180490567,
);
- expect(
+ try expect(
594491908217841670578297176641415611445982232488944558774612 * 390603545391089362063884922208143568023166603618446395589768 == 232210647056203049913662402532976186578842425262306016094292237500303028346593132411865381225871291702600263463125370016,
);
}
@@ -534,7 +534,7 @@ test "comptime_int multiplication" {
test "comptime_int shifting" {
comptime {
- expect((@as(u128, 1) << 127) == 0x80000000000000000000000000000000);
+ try expect((@as(u128, 1) << 127) == 0x80000000000000000000000000000000);
}
}
@@ -542,16 +542,16 @@ test "comptime_int multi-limb shift and mask" {
comptime {
var a = 0xefffffffa0000001eeeeeeefaaaaaaab;
- expect(@as(u32, a & 0xffffffff) == 0xaaaaaaab);
+ try expect(@as(u32, a & 0xffffffff) == 0xaaaaaaab);
a >>= 32;
- expect(@as(u32, a & 0xffffffff) == 0xeeeeeeef);
+ try expect(@as(u32, a & 0xffffffff) == 0xeeeeeeef);
a >>= 32;
- expect(@as(u32, a & 0xffffffff) == 0xa0000001);
+ try expect(@as(u32, a & 0xffffffff) == 0xa0000001);
a >>= 32;
- expect(@as(u32, a & 0xffffffff) == 0xefffffff);
+ try expect(@as(u32, a & 0xffffffff) == 0xefffffff);
a >>= 32;
- expect(a == 0);
+ try expect(a == 0);
}
}
@@ -559,227 +559,227 @@ test "comptime_int multi-limb partial shift right" {
comptime {
var a = 0x1ffffffffeeeeeeee;
a >>= 16;
- expect(a == 0x1ffffffffeeee);
+ try expect(a == 0x1ffffffffeeee);
}
}
test "xor" {
- test_xor();
- comptime test_xor();
+ try test_xor();
+ comptime try test_xor();
}
-fn test_xor() void {
- expect(0xFF ^ 0x00 == 0xFF);
- expect(0xF0 ^ 0x0F == 0xFF);
- expect(0xFF ^ 0xF0 == 0x0F);
- expect(0xFF ^ 0x0F == 0xF0);
- expect(0xFF ^ 0xFF == 0x00);
+fn test_xor() !void {
+ try expect(0xFF ^ 0x00 == 0xFF);
+ try expect(0xF0 ^ 0x0F == 0xFF);
+ try expect(0xFF ^ 0xF0 == 0x0F);
+ try expect(0xFF ^ 0x0F == 0xF0);
+ try expect(0xFF ^ 0xFF == 0x00);
}
test "comptime_int xor" {
comptime {
- expect(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF ^ 0x00000000000000000000000000000000 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
- expect(0xFFFFFFFFFFFFFFFF0000000000000000 ^ 0x0000000000000000FFFFFFFFFFFFFFFF == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
- expect(0xFFFFFFFFFFFFFFFF0000000000000000 ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0x0000000000000000FFFFFFFFFFFFFFFF);
- expect(0x0000000000000000FFFFFFFFFFFFFFFF ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0xFFFFFFFFFFFFFFFF0000000000000000);
- expect(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0x00000000000000000000000000000000);
- expect(0xFFFFFFFF00000000FFFFFFFF00000000 ^ 0x00000000FFFFFFFF00000000FFFFFFFF == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
- expect(0xFFFFFFFF00000000FFFFFFFF00000000 ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0x00000000FFFFFFFF00000000FFFFFFFF);
- expect(0x00000000FFFFFFFF00000000FFFFFFFF ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0xFFFFFFFF00000000FFFFFFFF00000000);
+ try expect(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF ^ 0x00000000000000000000000000000000 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
+ try expect(0xFFFFFFFFFFFFFFFF0000000000000000 ^ 0x0000000000000000FFFFFFFFFFFFFFFF == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
+ try expect(0xFFFFFFFFFFFFFFFF0000000000000000 ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0x0000000000000000FFFFFFFFFFFFFFFF);
+ try expect(0x0000000000000000FFFFFFFFFFFFFFFF ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0xFFFFFFFFFFFFFFFF0000000000000000);
+ try expect(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0x00000000000000000000000000000000);
+ try expect(0xFFFFFFFF00000000FFFFFFFF00000000 ^ 0x00000000FFFFFFFF00000000FFFFFFFF == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
+ try expect(0xFFFFFFFF00000000FFFFFFFF00000000 ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0x00000000FFFFFFFF00000000FFFFFFFF);
+ try expect(0x00000000FFFFFFFF00000000FFFFFFFF ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0xFFFFFFFF00000000FFFFFFFF00000000);
}
}
test "f128" {
- test_f128();
- comptime test_f128();
+ try test_f128();
+ comptime try test_f128();
}
fn make_f128(x: f128) f128 {
return x;
}
-fn test_f128() void {
- expect(@sizeOf(f128) == 16);
- expect(make_f128(1.0) == 1.0);
- expect(make_f128(1.0) != 1.1);
- expect(make_f128(1.0) > 0.9);
- expect(make_f128(1.0) >= 0.9);
- expect(make_f128(1.0) >= 1.0);
- should_not_be_zero(1.0);
+fn test_f128() !void {
+ try expect(@sizeOf(f128) == 16);
+ try expect(make_f128(1.0) == 1.0);
+ try expect(make_f128(1.0) != 1.1);
+ try expect(make_f128(1.0) > 0.9);
+ try expect(make_f128(1.0) >= 0.9);
+ try expect(make_f128(1.0) >= 1.0);
+ try should_not_be_zero(1.0);
}
-fn should_not_be_zero(x: f128) void {
- expect(x != 0.0);
+fn should_not_be_zero(x: f128) !void {
+ try expect(x != 0.0);
}
test "comptime float rem int" {
comptime {
var x = @as(f32, 1) % 2;
- expect(x == 1.0);
+ try expect(x == 1.0);
}
}
test "remainder division" {
- comptime remdiv(f16);
- comptime remdiv(f32);
- comptime remdiv(f64);
- comptime remdiv(f128);
- remdiv(f16);
- remdiv(f64);
- remdiv(f128);
+ comptime try remdiv(f16);
+ comptime try remdiv(f32);
+ comptime try remdiv(f64);
+ comptime try remdiv(f128);
+ try remdiv(f16);
+ try remdiv(f64);
+ try remdiv(f128);
}
-fn remdiv(comptime T: type) void {
- expect(@as(T, 1) == @as(T, 1) % @as(T, 2));
- expect(@as(T, 1) == @as(T, 7) % @as(T, 3));
+fn remdiv(comptime T: type) !void {
+ try expect(@as(T, 1) == @as(T, 1) % @as(T, 2));
+ try expect(@as(T, 1) == @as(T, 7) % @as(T, 3));
}
test "@sqrt" {
- testSqrt(f64, 12.0);
- comptime testSqrt(f64, 12.0);
- testSqrt(f32, 13.0);
- comptime testSqrt(f32, 13.0);
- testSqrt(f16, 13.0);
- comptime testSqrt(f16, 13.0);
+ try testSqrt(f64, 12.0);
+ comptime try testSqrt(f64, 12.0);
+ try testSqrt(f32, 13.0);
+ comptime try testSqrt(f32, 13.0);
+ try testSqrt(f16, 13.0);
+ comptime try testSqrt(f16, 13.0);
const x = 14.0;
const y = x * x;
const z = @sqrt(y);
- comptime expect(z == x);
+ comptime try expect(z == x);
}
-fn testSqrt(comptime T: type, x: T) void {
- expect(@sqrt(x * x) == x);
+fn testSqrt(comptime T: type, x: T) !void {
+ try expect(@sqrt(x * x) == x);
}
test "@fabs" {
- testFabs(f128, 12.0);
- comptime testFabs(f128, 12.0);
- testFabs(f64, 12.0);
- comptime testFabs(f64, 12.0);
- testFabs(f32, 12.0);
- comptime testFabs(f32, 12.0);
- testFabs(f16, 12.0);
- comptime testFabs(f16, 12.0);
+ try testFabs(f128, 12.0);
+ comptime try testFabs(f128, 12.0);
+ try testFabs(f64, 12.0);
+ comptime try testFabs(f64, 12.0);
+ try testFabs(f32, 12.0);
+ comptime try testFabs(f32, 12.0);
+ try testFabs(f16, 12.0);
+ comptime try testFabs(f16, 12.0);
const x = 14.0;
const y = -x;
const z = @fabs(y);
- comptime expectEqual(x, z);
+ comptime try expectEqual(x, z);
}
-fn testFabs(comptime T: type, x: T) void {
+fn testFabs(comptime T: type, x: T) !void {
const y = -x;
const z = @fabs(y);
- expectEqual(x, z);
+ try expectEqual(x, z);
}
test "@floor" {
// FIXME: Generates a floorl function call
// testFloor(f128, 12.0);
- comptime testFloor(f128, 12.0);
- testFloor(f64, 12.0);
- comptime testFloor(f64, 12.0);
- testFloor(f32, 12.0);
- comptime testFloor(f32, 12.0);
- testFloor(f16, 12.0);
- comptime testFloor(f16, 12.0);
+ comptime try testFloor(f128, 12.0);
+ try testFloor(f64, 12.0);
+ comptime try testFloor(f64, 12.0);
+ try testFloor(f32, 12.0);
+ comptime try testFloor(f32, 12.0);
+ try testFloor(f16, 12.0);
+ comptime try testFloor(f16, 12.0);
const x = 14.0;
const y = x + 0.7;
const z = @floor(y);
- comptime expectEqual(x, z);
+ comptime try expectEqual(x, z);
}
-fn testFloor(comptime T: type, x: T) void {
+fn testFloor(comptime T: type, x: T) !void {
const y = x + 0.6;
const z = @floor(y);
- expectEqual(x, z);
+ try expectEqual(x, z);
}
test "@ceil" {
// FIXME: Generates a ceill function call
//testCeil(f128, 12.0);
- comptime testCeil(f128, 12.0);
- testCeil(f64, 12.0);
- comptime testCeil(f64, 12.0);
- testCeil(f32, 12.0);
- comptime testCeil(f32, 12.0);
- testCeil(f16, 12.0);
- comptime testCeil(f16, 12.0);
+ comptime try testCeil(f128, 12.0);
+ try testCeil(f64, 12.0);
+ comptime try testCeil(f64, 12.0);
+ try testCeil(f32, 12.0);
+ comptime try testCeil(f32, 12.0);
+ try testCeil(f16, 12.0);
+ comptime try testCeil(f16, 12.0);
const x = 14.0;
const y = x - 0.7;
const z = @ceil(y);
- comptime expectEqual(x, z);
+ comptime try expectEqual(x, z);
}
-fn testCeil(comptime T: type, x: T) void {
+fn testCeil(comptime T: type, x: T) !void {
const y = x - 0.8;
const z = @ceil(y);
- expectEqual(x, z);
+ try expectEqual(x, z);
}
test "@trunc" {
// FIXME: Generates a truncl function call
//testTrunc(f128, 12.0);
- comptime testTrunc(f128, 12.0);
- testTrunc(f64, 12.0);
- comptime testTrunc(f64, 12.0);
- testTrunc(f32, 12.0);
- comptime testTrunc(f32, 12.0);
- testTrunc(f16, 12.0);
- comptime testTrunc(f16, 12.0);
+ comptime try testTrunc(f128, 12.0);
+ try testTrunc(f64, 12.0);
+ comptime try testTrunc(f64, 12.0);
+ try testTrunc(f32, 12.0);
+ comptime try testTrunc(f32, 12.0);
+ try testTrunc(f16, 12.0);
+ comptime try testTrunc(f16, 12.0);
const x = 14.0;
const y = x + 0.7;
const z = @trunc(y);
- comptime expectEqual(x, z);
+ comptime try expectEqual(x, z);
}
-fn testTrunc(comptime T: type, x: T) void {
+fn testTrunc(comptime T: type, x: T) !void {
{
const y = x + 0.8;
const z = @trunc(y);
- expectEqual(x, z);
+ try expectEqual(x, z);
}
{
const y = -x - 0.8;
const z = @trunc(y);
- expectEqual(-x, z);
+ try expectEqual(-x, z);
}
}
test "@round" {
// FIXME: Generates a roundl function call
//testRound(f128, 12.0);
- comptime testRound(f128, 12.0);
- testRound(f64, 12.0);
- comptime testRound(f64, 12.0);
- testRound(f32, 12.0);
- comptime testRound(f32, 12.0);
- testRound(f16, 12.0);
- comptime testRound(f16, 12.0);
+ comptime try testRound(f128, 12.0);
+ try testRound(f64, 12.0);
+ comptime try testRound(f64, 12.0);
+ try testRound(f32, 12.0);
+ comptime try testRound(f32, 12.0);
+ try testRound(f16, 12.0);
+ comptime try testRound(f16, 12.0);
const x = 14.0;
const y = x + 0.4;
const z = @round(y);
- comptime expectEqual(x, z);
+ comptime try expectEqual(x, z);
}
-fn testRound(comptime T: type, x: T) void {
+fn testRound(comptime T: type, x: T) !void {
const y = x - 0.5;
const z = @round(y);
- expectEqual(x, z);
+ try expectEqual(x, z);
}
test "comptime_int param and return" {
const a = comptimeAdd(35361831660712422535336160538497375248, 101752735581729509668353361206450473702);
- expect(a == 137114567242441932203689521744947848950);
+ try expect(a == 137114567242441932203689521744947848950);
const b = comptimeAdd(594491908217841670578297176641415611445982232488944558774612, 390603545391089362063884922208143568023166603618446395589768);
- expect(b == 985095453608931032642182098849559179469148836107390954364380);
+ try expect(b == 985095453608931032642182098849559179469148836107390954364380);
}
fn comptimeAdd(comptime a: comptime_int, comptime b: comptime_int) comptime_int {
@@ -788,85 +788,85 @@ fn comptimeAdd(comptime a: comptime_int, comptime b: comptime_int) comptime_int
test "vector integer addition" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var a: std.meta.Vector(4, i32) = [_]i32{ 1, 2, 3, 4 };
var b: std.meta.Vector(4, i32) = [_]i32{ 5, 6, 7, 8 };
var result = a + b;
var result_array: [4]i32 = result;
const expected = [_]i32{ 6, 8, 10, 12 };
- expectEqualSlices(i32, &expected, &result_array);
+ try expectEqualSlices(i32, &expected, &result_array);
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "NaN comparison" {
- testNanEqNan(f16);
- testNanEqNan(f32);
- testNanEqNan(f64);
- testNanEqNan(f128);
- comptime testNanEqNan(f16);
- comptime testNanEqNan(f32);
- comptime testNanEqNan(f64);
- comptime testNanEqNan(f128);
+ try testNanEqNan(f16);
+ try testNanEqNan(f32);
+ try testNanEqNan(f64);
+ try testNanEqNan(f128);
+ comptime try testNanEqNan(f16);
+ comptime try testNanEqNan(f32);
+ comptime try testNanEqNan(f64);
+ comptime try testNanEqNan(f128);
}
-fn testNanEqNan(comptime F: type) void {
+fn testNanEqNan(comptime F: type) !void {
var nan1 = std.math.nan(F);
var nan2 = std.math.nan(F);
- expect(nan1 != nan2);
- expect(!(nan1 == nan2));
- expect(!(nan1 > nan2));
- expect(!(nan1 >= nan2));
- expect(!(nan1 < nan2));
- expect(!(nan1 <= nan2));
+ try expect(nan1 != nan2);
+ try expect(!(nan1 == nan2));
+ try expect(!(nan1 > nan2));
+ try expect(!(nan1 >= nan2));
+ try expect(!(nan1 < nan2));
+ try expect(!(nan1 <= nan2));
}
test "128-bit multiplication" {
var a: i128 = 3;
var b: i128 = 2;
var c = a * b;
- expect(c == 6);
+ try expect(c == 6);
}
test "vector comparison" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var a: std.meta.Vector(6, i32) = [_]i32{ 1, 3, -1, 5, 7, 9 };
var b: std.meta.Vector(6, i32) = [_]i32{ -1, 3, 0, 6, 10, -10 };
- expect(mem.eql(bool, &@as([6]bool, a < b), &[_]bool{ false, false, true, true, true, false }));
- expect(mem.eql(bool, &@as([6]bool, a <= b), &[_]bool{ false, true, true, true, true, false }));
- expect(mem.eql(bool, &@as([6]bool, a == b), &[_]bool{ false, true, false, false, false, false }));
- expect(mem.eql(bool, &@as([6]bool, a != b), &[_]bool{ true, false, true, true, true, true }));
- expect(mem.eql(bool, &@as([6]bool, a > b), &[_]bool{ true, false, false, false, false, true }));
- expect(mem.eql(bool, &@as([6]bool, a >= b), &[_]bool{ true, true, false, false, false, true }));
+ try expect(mem.eql(bool, &@as([6]bool, a < b), &[_]bool{ false, false, true, true, true, false }));
+ try expect(mem.eql(bool, &@as([6]bool, a <= b), &[_]bool{ false, true, true, true, true, false }));
+ try expect(mem.eql(bool, &@as([6]bool, a == b), &[_]bool{ false, true, false, false, false, false }));
+ try expect(mem.eql(bool, &@as([6]bool, a != b), &[_]bool{ true, false, true, true, true, true }));
+ try expect(mem.eql(bool, &@as([6]bool, a > b), &[_]bool{ true, false, false, false, false, true }));
+ try expect(mem.eql(bool, &@as([6]bool, a >= b), &[_]bool{ true, true, false, false, false, true }));
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "compare undefined literal with comptime_int" {
var x = undefined == 1;
// x is now undefined with type bool
x = true;
- expect(x);
+ try expect(x);
}
test "signed zeros are represented properly" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
inline for ([_]type{ f16, f32, f64, f128 }) |T| {
const ST = std.meta.Int(.unsigned, @typeInfo(T).Float.bits);
var as_fp_val = -@as(T, 0.0);
var as_uint_val = @bitCast(ST, as_fp_val);
// Ensure the sign bit is set.
- expect(as_uint_val >> (@typeInfo(T).Float.bits - 1) == 1);
+ try expect(as_uint_val >> (@typeInfo(T).Float.bits - 1) == 1);
}
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test/stage1/behavior/misc.zig
@@ -25,18 +25,18 @@ test "call disabled extern fn" {
}
test "short circuit" {
- testShortCircuit(false, true);
- comptime testShortCircuit(false, true);
+ try testShortCircuit(false, true);
+ comptime try testShortCircuit(false, true);
}
-fn testShortCircuit(f: bool, t: bool) void {
+fn testShortCircuit(f: bool, t: bool) !void {
var hit_1 = f;
var hit_2 = f;
var hit_3 = f;
var hit_4 = f;
if (t or x: {
- expect(f);
+ try expect(f);
break :x f;
}) {
hit_1 = t;
@@ -45,31 +45,31 @@ fn testShortCircuit(f: bool, t: bool) void {
hit_2 = t;
break :x f;
}) {
- expect(f);
+ try expect(f);
}
if (t and x: {
hit_3 = t;
break :x f;
}) {
- expect(f);
+ try expect(f);
}
if (f and x: {
- expect(f);
+ try expect(f);
break :x f;
}) {
- expect(f);
+ try expect(f);
} else {
hit_4 = t;
}
- expect(hit_1);
- expect(hit_2);
- expect(hit_3);
- expect(hit_4);
+ try expect(hit_1);
+ try expect(hit_2);
+ try expect(hit_3);
+ try expect(hit_4);
}
test "truncate" {
- expect(testTruncate(0x10fd) == 0xfd);
+ try expect(testTruncate(0x10fd) == 0xfd);
}
fn testTruncate(x: u32) u8 {
return @truncate(u8, x);
@@ -80,16 +80,16 @@ fn first4KeysOfHomeRow() []const u8 {
}
test "return string from function" {
- expect(mem.eql(u8, first4KeysOfHomeRow(), "aoeu"));
+ try expect(mem.eql(u8, first4KeysOfHomeRow(), "aoeu"));
}
const g1: i32 = 1233 + 1;
var g2: i32 = 0;
test "global variables" {
- expect(g2 == 0);
+ try expect(g2 == 0);
g2 = g1;
- expect(g2 == 1234);
+ try expect(g2 == 1234);
}
test "memcpy and memset intrinsics" {
@@ -106,7 +106,7 @@ test "builtin static eval" {
const x: i32 = comptime x: {
break :x 1 + 2 + 3;
};
- expect(x == comptime 6);
+ try expect(x == comptime 6);
}
test "slicing" {
@@ -127,7 +127,7 @@ test "slicing" {
test "constant equal function pointers" {
const alias = emptyFn;
- expect(comptime x: {
+ try expect(comptime x: {
break :x emptyFn == alias;
});
}
@@ -135,25 +135,25 @@ test "constant equal function pointers" {
fn emptyFn() void {}
test "hex escape" {
- expect(mem.eql(u8, "\x68\x65\x6c\x6c\x6f", "hello"));
+ try expect(mem.eql(u8, "\x68\x65\x6c\x6c\x6f", "hello"));
}
test "string concatenation" {
- expect(mem.eql(u8, "OK" ++ " IT " ++ "WORKED", "OK IT WORKED"));
+ try expect(mem.eql(u8, "OK" ++ " IT " ++ "WORKED", "OK IT WORKED"));
}
test "array mult operator" {
- expect(mem.eql(u8, "ab" ** 5, "ababababab"));
+ try expect(mem.eql(u8, "ab" ** 5, "ababababab"));
}
test "string escapes" {
- expect(mem.eql(u8, "\"", "\x22"));
- expect(mem.eql(u8, "\'", "\x27"));
- expect(mem.eql(u8, "\n", "\x0a"));
- expect(mem.eql(u8, "\r", "\x0d"));
- expect(mem.eql(u8, "\t", "\x09"));
- expect(mem.eql(u8, "\\", "\x5c"));
- expect(mem.eql(u8, "\u{1234}\u{069}\u{1}", "\xe1\x88\xb4\x69\x01"));
+ try expect(mem.eql(u8, "\"", "\x22"));
+ try expect(mem.eql(u8, "\'", "\x27"));
+ try expect(mem.eql(u8, "\n", "\x0a"));
+ try expect(mem.eql(u8, "\r", "\x0d"));
+ try expect(mem.eql(u8, "\t", "\x09"));
+ try expect(mem.eql(u8, "\\", "\x5c"));
+ try expect(mem.eql(u8, "\u{1234}\u{069}\u{1}", "\xe1\x88\xb4\x69\x01"));
}
test "multiline string" {
@@ -163,7 +163,7 @@ test "multiline string" {
\\three
;
const s2 = "one\ntwo)\nthree";
- expect(mem.eql(u8, s1, s2));
+ try expect(mem.eql(u8, s1, s2));
}
test "multiline string comments at start" {
@@ -173,7 +173,7 @@ test "multiline string comments at start" {
\\three
;
const s2 = "two)\nthree";
- expect(mem.eql(u8, s1, s2));
+ try expect(mem.eql(u8, s1, s2));
}
test "multiline string comments at end" {
@@ -183,7 +183,7 @@ test "multiline string comments at end" {
//\\three
;
const s2 = "one\ntwo)";
- expect(mem.eql(u8, s1, s2));
+ try expect(mem.eql(u8, s1, s2));
}
test "multiline string comments in middle" {
@@ -193,7 +193,7 @@ test "multiline string comments in middle" {
\\three
;
const s2 = "one\nthree";
- expect(mem.eql(u8, s1, s2));
+ try expect(mem.eql(u8, s1, s2));
}
test "multiline string comments at multiple places" {
@@ -205,7 +205,7 @@ test "multiline string comments at multiple places" {
\\five
;
const s2 = "one\nthree\nfive";
- expect(mem.eql(u8, s1, s2));
+ try expect(mem.eql(u8, s1, s2));
}
test "multiline C string" {
@@ -215,11 +215,11 @@ test "multiline C string" {
\\three
;
const s2 = "one\ntwo)\nthree";
- expect(std.cstr.cmp(s1, s2) == 0);
+ try expect(std.cstr.cmp(s1, s2) == 0);
}
test "type equality" {
- expect(*const u8 != *u8);
+ try expect(*const u8 != *u8);
}
const global_a: i32 = 1234;
@@ -227,7 +227,7 @@ const global_b: *const i32 = &global_a;
const global_c: *const f32 = @ptrCast(*const f32, global_b);
test "compile time global reinterpret" {
const d = @ptrCast(*const i32, global_c);
- expect(d.* == 1234);
+ try expect(d.* == 1234);
}
test "explicit cast maybe pointers" {
@@ -253,8 +253,8 @@ test "cast undefined" {
fn testCastUndefined(x: []const u8) void {}
test "cast small unsigned to larger signed" {
- expect(castSmallUnsignedToLargerSigned1(200) == @as(i16, 200));
- expect(castSmallUnsignedToLargerSigned2(9999) == @as(i64, 9999));
+ try expect(castSmallUnsignedToLargerSigned1(200) == @as(i16, 200));
+ try expect(castSmallUnsignedToLargerSigned2(9999) == @as(i64, 9999));
}
fn castSmallUnsignedToLargerSigned1(x: u8) i16 {
return x;
@@ -264,7 +264,7 @@ fn castSmallUnsignedToLargerSigned2(x: u16) i64 {
}
test "implicit cast after unreachable" {
- expect(outer() == 1234);
+ try expect(outer() == 1234);
}
fn inner() i32 {
return 1234;
@@ -279,13 +279,13 @@ test "pointer dereferencing" {
y.* += 1;
- expect(x == 4);
- expect(y.* == 4);
+ try expect(x == 4);
+ try expect(y.* == 4);
}
test "call result of if else expression" {
- expect(mem.eql(u8, f2(true), "a"));
- expect(mem.eql(u8, f2(false), "b"));
+ try expect(mem.eql(u8, f2(true), "a"));
+ try expect(mem.eql(u8, f2(false), "b"));
}
fn f2(x: bool) []const u8 {
return (if (x) fA else fB)();
@@ -305,8 +305,8 @@ test "const expression eval handling of variables" {
}
test "constant enum initialization with differing sizes" {
- test3_1(test3_foo);
- test3_2(test3_bar);
+ try test3_1(test3_foo);
+ try test3_2(test3_bar);
}
const Test3Foo = union(enum) {
One: void,
@@ -324,41 +324,41 @@ const test3_foo = Test3Foo{
},
};
const test3_bar = Test3Foo{ .Two = 13 };
-fn test3_1(f: Test3Foo) void {
+fn test3_1(f: Test3Foo) !void {
switch (f) {
Test3Foo.Three => |pt| {
- expect(pt.x == 3);
- expect(pt.y == 4);
+ try expect(pt.x == 3);
+ try expect(pt.y == 4);
},
else => unreachable,
}
}
-fn test3_2(f: Test3Foo) void {
+fn test3_2(f: Test3Foo) !void {
switch (f) {
Test3Foo.Two => |x| {
- expect(x == 13);
+ try expect(x == 13);
},
else => unreachable,
}
}
test "character literals" {
- expect('\'' == single_quote);
+ try expect('\'' == single_quote);
}
const single_quote = '\'';
test "take address of parameter" {
- testTakeAddressOfParameter(12.34);
+ try testTakeAddressOfParameter(12.34);
}
-fn testTakeAddressOfParameter(f: f32) void {
+fn testTakeAddressOfParameter(f: f32) !void {
const f_ptr = &f;
- expect(f_ptr.* == 12.34);
+ try expect(f_ptr.* == 12.34);
}
test "pointer comparison" {
const a = @as([]const u8, "a");
const b = &a;
- expect(ptrEql(b, b));
+ try expect(ptrEql(b, b));
}
fn ptrEql(a: *const []const u8, b: *const []const u8) bool {
return a == b;
@@ -368,19 +368,19 @@ test "string concatenation" {
const a = "OK" ++ " IT " ++ "WORKED";
const b = "OK IT WORKED";
- comptime expect(@TypeOf(a) == *const [12:0]u8);
- comptime expect(@TypeOf(b) == *const [12:0]u8);
+ comptime try expect(@TypeOf(a) == *const [12:0]u8);
+ comptime try expect(@TypeOf(b) == *const [12:0]u8);
const len = mem.len(b);
const len_with_null = len + 1;
{
var i: u32 = 0;
while (i < len_with_null) : (i += 1) {
- expect(a[i] == b[i]);
+ try expect(a[i] == b[i]);
}
}
- expect(a[len] == 0);
- expect(b[len] == 0);
+ try expect(a[len] == 0);
+ try expect(b[len] == 0);
}
test "pointer to void return type" {
@@ -397,7 +397,7 @@ fn testPointerToVoidReturnType2() *const void {
test "non const ptr to aliased type" {
const int = i32;
- expect(?*int == ?*i32);
+ try expect(?*int == ?*i32);
}
test "array 2D const double ptr" {
@@ -405,13 +405,13 @@ test "array 2D const double ptr" {
[_]f32{1.0},
[_]f32{2.0},
};
- testArray2DConstDoublePtr(&rect_2d_vertexes[0][0]);
+ try testArray2DConstDoublePtr(&rect_2d_vertexes[0][0]);
}
-fn testArray2DConstDoublePtr(ptr: *const f32) void {
+fn testArray2DConstDoublePtr(ptr: *const f32) !void {
const ptr2 = @ptrCast([*]const f32, ptr);
- expect(ptr2[0] == 1.0);
- expect(ptr2[1] == 2.0);
+ try expect(ptr2[0] == 1.0);
+ try expect(ptr2[1] == 2.0);
}
const AStruct = struct {
@@ -439,13 +439,13 @@ test "@typeName" {
Unused,
};
comptime {
- expect(mem.eql(u8, @typeName(i64), "i64"));
- expect(mem.eql(u8, @typeName(*usize), "*usize"));
+ try expect(mem.eql(u8, @typeName(i64), "i64"));
+ try expect(mem.eql(u8, @typeName(*usize), "*usize"));
// https://github.com/ziglang/zig/issues/675
- expect(mem.eql(u8, "behavior.misc.TypeFromFn(u8)", @typeName(TypeFromFn(u8))));
- expect(mem.eql(u8, @typeName(Struct), "Struct"));
- expect(mem.eql(u8, @typeName(Union), "Union"));
- expect(mem.eql(u8, @typeName(Enum), "Enum"));
+ try expect(mem.eql(u8, "behavior.misc.TypeFromFn(u8)", @typeName(TypeFromFn(u8))));
+ try expect(mem.eql(u8, @typeName(Struct), "Struct"));
+ try expect(mem.eql(u8, @typeName(Union), "Union"));
+ try expect(mem.eql(u8, @typeName(Enum), "Enum"));
}
}
@@ -455,14 +455,14 @@ fn TypeFromFn(comptime T: type) type {
test "double implicit cast in same expression" {
var x = @as(i32, @as(u16, nine()));
- expect(x == 9);
+ try expect(x == 9);
}
fn nine() u8 {
return 9;
}
test "global variable initialized to global variable array element" {
- expect(global_ptr == &gdt[0]);
+ try expect(global_ptr == &gdt[0]);
}
const GDTEntry = struct {
field: i32,
@@ -483,9 +483,9 @@ export fn writeToVRam() void {
const OpaqueA = opaque {};
const OpaqueB = opaque {};
test "opaque types" {
- expect(*OpaqueA != *OpaqueB);
- expect(mem.eql(u8, @typeName(OpaqueA), "OpaqueA"));
- expect(mem.eql(u8, @typeName(OpaqueB), "OpaqueB"));
+ try expect(*OpaqueA != *OpaqueB);
+ try expect(mem.eql(u8, @typeName(OpaqueA), "OpaqueA"));
+ try expect(mem.eql(u8, @typeName(OpaqueB), "OpaqueB"));
}
test "variable is allowed to be a pointer to an opaque type" {
@@ -525,7 +525,7 @@ fn fnThatClosesOverLocalConst() type {
test "function closes over local const" {
const x = fnThatClosesOverLocalConst().g();
- expect(x == 1);
+ try expect(x == 1);
}
test "cold function" {
@@ -562,21 +562,21 @@ export fn testPackedStuff(a: *const PackedStruct, b: *const PackedUnion, c: Pack
test "slicing zero length array" {
const s1 = ""[0..];
const s2 = ([_]u32{})[0..];
- expect(s1.len == 0);
- expect(s2.len == 0);
- expect(mem.eql(u8, s1, ""));
- expect(mem.eql(u32, s2, &[_]u32{}));
+ try expect(s1.len == 0);
+ try expect(s2.len == 0);
+ try expect(mem.eql(u8, s1, ""));
+ try expect(mem.eql(u32, s2, &[_]u32{}));
}
const addr1 = @ptrCast(*const u8, emptyFn);
test "comptime cast fn to ptr" {
const addr2 = @ptrCast(*const u8, emptyFn);
- comptime expect(addr1 == addr2);
+ comptime try expect(addr1 == addr2);
}
test "equality compare fn ptrs" {
var a = emptyFn;
- expect(a == a);
+ try expect(a == a);
}
test "self reference through fn ptr field" {
@@ -591,34 +591,34 @@ test "self reference through fn ptr field" {
};
var a: S.A = undefined;
a.f = S.foo;
- expect(a.f(a) == 12);
+ try expect(a.f(a) == 12);
}
test "volatile load and store" {
var number: i32 = 1234;
const ptr = @as(*volatile i32, &number);
ptr.* += 1;
- expect(ptr.* == 1235);
+ try expect(ptr.* == 1235);
}
test "slice string literal has correct type" {
comptime {
- expect(@TypeOf("aoeu"[0..]) == *const [4:0]u8);
+ try expect(@TypeOf("aoeu"[0..]) == *const [4:0]u8);
const array = [_]i32{ 1, 2, 3, 4 };
- expect(@TypeOf(array[0..]) == *const [4]i32);
+ try expect(@TypeOf(array[0..]) == *const [4]i32);
}
var runtime_zero: usize = 0;
- comptime expect(@TypeOf("aoeu"[runtime_zero..]) == [:0]const u8);
+ comptime try expect(@TypeOf("aoeu"[runtime_zero..]) == [:0]const u8);
const array = [_]i32{ 1, 2, 3, 4 };
- comptime expect(@TypeOf(array[runtime_zero..]) == []const i32);
+ comptime try expect(@TypeOf(array[runtime_zero..]) == []const i32);
}
test "struct inside function" {
- testStructInFn();
- comptime testStructInFn();
+ try testStructInFn();
+ comptime try testStructInFn();
}
-fn testStructInFn() void {
+fn testStructInFn() !void {
const BlockKind = u32;
const Block = struct {
@@ -629,11 +629,11 @@ fn testStructInFn() void {
block.kind += 1;
- expect(block.kind == 1235);
+ try expect(block.kind == 1235);
}
test "fn call returning scalar optional in equality expression" {
- expect(getNull() == null);
+ try expect(getNull() == null);
}
fn getNull() ?*i32 {
@@ -645,16 +645,16 @@ test "thread local variable" {
threadlocal var t: i32 = 1234;
};
S.t += 1;
- expect(S.t == 1235);
+ try expect(S.t == 1235);
}
test "unicode escape in character literal" {
var a: u24 = '\u{01f4a9}';
- expect(a == 128169);
+ try expect(a == 128169);
}
test "unicode character in character literal" {
- expect('๐ฉ' == 128169);
+ try expect('๐ฉ' == 128169);
}
test "result location zero sized array inside struct field implicit cast to slice" {
@@ -662,7 +662,7 @@ test "result location zero sized array inside struct field implicit cast to slic
entries: []u32,
};
var foo = E{ .entries = &[_]u32{} };
- expect(foo.entries.len == 0);
+ try expect(foo.entries.len == 0);
}
var global_foo: *i32 = undefined;
@@ -677,7 +677,7 @@ test "global variable assignment with optional unwrapping with var initialized t
global_foo = S.foo() orelse {
@panic("bad");
};
- expect(global_foo.* == 1234);
+ try expect(global_foo.* == 1234);
}
test "peer result location with typed parent, runtime condition, comptime prongs" {
@@ -696,8 +696,8 @@ test "peer result location with typed parent, runtime condition, comptime prongs
bleh: i32,
};
};
- expect(S.doTheTest(0) == 1234);
- expect(S.doTheTest(1) == 1234);
+ try expect(S.doTheTest(0) == 1234);
+ try expect(S.doTheTest(1) == 1234);
}
test "nested optional field in struct" {
@@ -710,7 +710,7 @@ test "nested optional field in struct" {
var s = S1{
.x = S2{ .y = 127 },
};
- expect(s.x.?.y == 127);
+ try expect(s.x.?.y == 127);
}
fn maybe(x: bool) anyerror!?u32 {
@@ -722,7 +722,7 @@ fn maybe(x: bool) anyerror!?u32 {
test "result location is optional inside error union" {
const x = maybe(true) catch unreachable;
- expect(x.? == 42);
+ try expect(x.? == 42);
}
threadlocal var buffer: [11]u8 = undefined;
@@ -730,7 +730,7 @@ threadlocal var buffer: [11]u8 = undefined;
test "pointer to thread local array" {
const s = "Hello world";
std.mem.copy(u8, buffer[0..], s);
- std.testing.expectEqualSlices(u8, buffer[0..], s);
+ try std.testing.expectEqualSlices(u8, buffer[0..], s);
}
test "auto created variables have correct alignment" {
@@ -742,15 +742,15 @@ test "auto created variables have correct alignment" {
return 0;
}
};
- expect(S.foo("\x7a\x7a\x7a\x7a") == 0x7a7a7a7a);
- comptime expect(S.foo("\x7a\x7a\x7a\x7a") == 0x7a7a7a7a);
+ try expect(S.foo("\x7a\x7a\x7a\x7a") == 0x7a7a7a7a);
+ comptime try expect(S.foo("\x7a\x7a\x7a\x7a") == 0x7a7a7a7a);
}
extern var opaque_extern_var: opaque {};
var var_to_export: u32 = 42;
test "extern variable with non-pointer opaque type" {
@export(var_to_export, .{ .name = "opaque_extern_var" });
- expect(@ptrCast(*align(1) u32, &opaque_extern_var).* == 42);
+ try expect(@ptrCast(*align(1) u32, &opaque_extern_var).* == 42);
}
test "lazy typeInfo value as generic parameter" {
test/stage1/behavior/muladd.zig
@@ -1,34 +1,34 @@
const expect = @import("std").testing.expect;
test "@mulAdd" {
- comptime testMulAdd();
- testMulAdd();
+ comptime try testMulAdd();
+ try testMulAdd();
}
-fn testMulAdd() void {
+fn testMulAdd() !void {
{
var a: f16 = 5.5;
var b: f16 = 2.5;
var c: f16 = 6.25;
- expect(@mulAdd(f16, a, b, c) == 20);
+ try expect(@mulAdd(f16, a, b, c) == 20);
}
{
var a: f32 = 5.5;
var b: f32 = 2.5;
var c: f32 = 6.25;
- expect(@mulAdd(f32, a, b, c) == 20);
+ try expect(@mulAdd(f32, a, b, c) == 20);
}
{
var a: f64 = 5.5;
var b: f64 = 2.5;
var c: f64 = 6.25;
- expect(@mulAdd(f64, a, b, c) == 20);
+ try expect(@mulAdd(f64, a, b, c) == 20);
}
// Awaits implementation in libm.zig
//{
// var a: f16 = 5.5;
// var b: f128 = 2.5;
// var c: f128 = 6.25;
- // expect(@mulAdd(f128, a, b, c) == 20);
+ //try expect(@mulAdd(f128, a, b, c) == 20);
//}
}
test/stage1/behavior/namespace_depends_on_compile_var.zig
@@ -3,9 +3,9 @@ const expect = std.testing.expect;
test "namespace depends on compile var" {
if (some_namespace.a_bool) {
- expect(some_namespace.a_bool);
+ try expect(some_namespace.a_bool);
} else {
- expect(!some_namespace.a_bool);
+ try expect(!some_namespace.a_bool);
}
}
const some_namespace = switch (std.builtin.os.tag) {
test/stage1/behavior/null.zig
@@ -17,13 +17,13 @@ test "optional type" {
const z = next_x orelse 1234;
- expect(z == 1234);
+ try expect(z == 1234);
const final_x: ?i32 = 13;
const num = final_x orelse unreachable;
- expect(num == 13);
+ try expect(num == 13);
}
test "test maybe object and get a pointer to the inner value" {
@@ -33,7 +33,7 @@ test "test maybe object and get a pointer to the inner value" {
b.* = false;
}
- expect(maybe_bool.? == false);
+ try expect(maybe_bool.? == false);
}
test "rhs maybe unwrap return" {
@@ -42,14 +42,14 @@ test "rhs maybe unwrap return" {
}
test "maybe return" {
- maybeReturnImpl();
- comptime maybeReturnImpl();
+ try maybeReturnImpl();
+ comptime try maybeReturnImpl();
}
-fn maybeReturnImpl() void {
- expect(foo(1235).?);
+fn maybeReturnImpl() !void {
+ try expect(foo(1235).?);
if (foo(null) != null) unreachable;
- expect(!foo(1234).?);
+ try expect(!foo(1234).?);
}
fn foo(x: ?i32) ?bool {
@@ -58,7 +58,7 @@ fn foo(x: ?i32) ?bool {
}
test "if var maybe pointer" {
- expect(shouldBeAPlus1(Particle{
+ try expect(shouldBeAPlus1(Particle{
.a = 14,
.b = 1,
.c = 1,
@@ -84,10 +84,10 @@ const Particle = struct {
test "null literal outside function" {
const is_null = here_is_a_null_literal.context == null;
- expect(is_null);
+ try expect(is_null);
const is_non_null = here_is_a_null_literal.context != null;
- expect(!is_non_null);
+ try expect(!is_non_null);
}
const SillyStruct = struct {
context: ?i32,
@@ -95,21 +95,21 @@ const SillyStruct = struct {
const here_is_a_null_literal = SillyStruct{ .context = null };
test "test null runtime" {
- testTestNullRuntime(null);
+ try testTestNullRuntime(null);
}
-fn testTestNullRuntime(x: ?i32) void {
- expect(x == null);
- expect(!(x != null));
+fn testTestNullRuntime(x: ?i32) !void {
+ try expect(x == null);
+ try expect(!(x != null));
}
test "optional void" {
- optionalVoidImpl();
- comptime optionalVoidImpl();
+ try optionalVoidImpl();
+ comptime try optionalVoidImpl();
}
-fn optionalVoidImpl() void {
- expect(bar(null) == null);
- expect(bar({}) != null);
+fn optionalVoidImpl() !void {
+ try expect(bar(null) == null);
+ try expect(bar({}) != null);
}
fn bar(x: ?void) ?void {
@@ -133,7 +133,7 @@ test "unwrap optional which is field of global var" {
}
struct_with_optional.field = 1234;
if (struct_with_optional.field) |payload| {
- expect(payload == 1234);
+ try expect(payload == 1234);
} else {
unreachable;
}
@@ -141,13 +141,13 @@ test "unwrap optional which is field of global var" {
test "null with default unwrap" {
const x: i32 = null orelse 1;
- expect(x == 1);
+ try expect(x == 1);
}
test "optional types" {
comptime {
const opt_type_struct = StructWithOptionalType{ .t = u8 };
- expect(opt_type_struct.t != null and opt_type_struct.t.? == u8);
+ try expect(opt_type_struct.t != null and opt_type_struct.t.? == u8);
}
}
@@ -158,5 +158,5 @@ const StructWithOptionalType = struct {
test "optional pointer to 0 bit type null value at runtime" {
const EmptyStruct = struct {};
var x: ?*EmptyStruct = null;
- expect(x == null);
+ try expect(x == null);
}
test/stage1/behavior/optional.zig
@@ -8,28 +8,28 @@ pub const EmptyStruct = struct {};
test "optional pointer to size zero struct" {
var e = EmptyStruct{};
var o: ?*EmptyStruct = &e;
- expect(o != null);
+ try expect(o != null);
}
test "equality compare nullable pointers" {
- testNullPtrsEql();
- comptime testNullPtrsEql();
+ try testNullPtrsEql();
+ comptime try testNullPtrsEql();
}
-fn testNullPtrsEql() void {
+fn testNullPtrsEql() !void {
var number: i32 = 1234;
var x: ?*i32 = null;
var y: ?*i32 = null;
- expect(x == y);
+ try expect(x == y);
y = &number;
- expect(x != y);
- expect(x != &number);
- expect(&number != x);
+ try expect(x != y);
+ try expect(x != &number);
+ try expect(&number != x);
x = &number;
- expect(x == y);
- expect(x == &number);
- expect(&number == x);
+ try expect(x == y);
+ try expect(x == &number);
+ try expect(&number == x);
}
test "address of unwrap optional" {
@@ -46,23 +46,23 @@ test "address of unwrap optional" {
};
S.global = S.Foo{ .a = 1234 };
const foo = S.getFoo() catch unreachable;
- expect(foo.a == 1234);
+ try expect(foo.a == 1234);
}
test "equality compare optional with non-optional" {
- test_cmp_optional_non_optional();
- comptime test_cmp_optional_non_optional();
+ try test_cmp_optional_non_optional();
+ comptime try test_cmp_optional_non_optional();
}
-fn test_cmp_optional_non_optional() void {
+fn test_cmp_optional_non_optional() !void {
var ten: i32 = 10;
var opt_ten: ?i32 = 10;
var five: i32 = 5;
var int_n: ?i32 = null;
- expect(int_n != ten);
- expect(opt_ten == ten);
- expect(opt_ten != five);
+ try expect(int_n != ten);
+ try expect(opt_ten == ten);
+ try expect(opt_ten != five);
// test evaluation is always lexical
// ensure that the optional isn't always computed before the non-optional
@@ -71,14 +71,14 @@ fn test_cmp_optional_non_optional() void {
mutable_state += 1;
break :blk1 @as(?f64, 10.0);
} != blk2: {
- expect(mutable_state == 1);
+ try expect(mutable_state == 1);
break :blk2 @as(f64, 5.0);
};
_ = blk1: {
mutable_state += 1;
break :blk1 @as(f64, 10.0);
} != blk2: {
- expect(mutable_state == 2);
+ try expect(mutable_state == 2);
break :blk2 @as(?f64, 5.0);
};
}
@@ -94,15 +94,15 @@ test "passing an optional integer as a parameter" {
return x.? == 1234;
}
};
- expect(S.entry());
- comptime expect(S.entry());
+ try expect(S.entry());
+ comptime try expect(S.entry());
}
test "unwrap function call with optional pointer return value" {
const S = struct {
- fn entry() void {
- expect(foo().?.* == 1234);
- expect(bar() == null);
+ fn entry() !void {
+ try expect(foo().?.* == 1234);
+ try expect(bar() == null);
}
const global: i32 = 1234;
fn foo() ?*const i32 {
@@ -112,14 +112,14 @@ test "unwrap function call with optional pointer return value" {
return null;
}
};
- S.entry();
- comptime S.entry();
+ try S.entry();
+ comptime try S.entry();
}
test "nested orelse" {
const S = struct {
- fn entry() void {
- expect(func() == null);
+ fn entry() !void {
+ try expect(func() == null);
}
fn maybe() ?Foo {
return null;
@@ -134,8 +134,8 @@ test "nested orelse" {
field: i32,
};
};
- S.entry();
- comptime S.entry();
+ try S.entry();
+ comptime try S.entry();
}
test "self-referential struct through a slice of optional" {
@@ -154,7 +154,7 @@ test "self-referential struct through a slice of optional" {
};
var n = S.Node.new();
- expect(n.data == null);
+ try expect(n.data == null);
}
test "assigning to an unwrapped optional field in an inline loop" {
@@ -173,14 +173,14 @@ test "coerce an anon struct literal to optional struct" {
const Struct = struct {
field: u32,
};
- export fn doTheTest() void {
+ fn doTheTest() !void {
var maybe_dims: ?Struct = null;
maybe_dims = .{ .field = 1 };
- expect(maybe_dims.?.field == 1);
+ try expect(maybe_dims.?.field == 1);
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "optional with void type" {
@@ -188,15 +188,15 @@ test "optional with void type" {
x: ?void,
};
var x = Foo{ .x = null };
- expect(x.x == null);
+ try expect(x.x == null);
}
test "0-bit child type coerced to optional return ptr result location" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var y = Foo{};
var z = y.thing();
- expect(z != null);
+ try expect(z != null);
}
const Foo = struct {
@@ -209,17 +209,17 @@ test "0-bit child type coerced to optional return ptr result location" {
}
};
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "0-bit child type coerced to optional" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var it: Foo = .{
.list = undefined,
};
- expect(it.foo() != null);
+ try expect(it.foo() != null);
}
const Empty = struct {};
@@ -232,8 +232,8 @@ test "0-bit child type coerced to optional" {
}
};
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "array of optional unaligned types" {
@@ -255,15 +255,15 @@ test "array of optional unaligned types" {
// The index must be a runtime value
var i: usize = 0;
- expectEqual(Enum.one, values[i].?.Num);
+ try expectEqual(Enum.one, values[i].?.Num);
i += 1;
- expectEqual(Enum.two, values[i].?.Num);
+ try expectEqual(Enum.two, values[i].?.Num);
i += 1;
- expectEqual(Enum.three, values[i].?.Num);
+ try expectEqual(Enum.three, values[i].?.Num);
i += 1;
- expectEqual(Enum.one, values[i].?.Num);
+ try expectEqual(Enum.one, values[i].?.Num);
i += 1;
- expectEqual(Enum.two, values[i].?.Num);
+ try expectEqual(Enum.two, values[i].?.Num);
i += 1;
- expectEqual(Enum.three, values[i].?.Num);
+ try expectEqual(Enum.three, values[i].?.Num);
}
test/stage1/behavior/pointers.zig
@@ -4,15 +4,15 @@ const expect = testing.expect;
const expectError = testing.expectError;
test "dereference pointer" {
- comptime testDerefPtr();
- testDerefPtr();
+ comptime try testDerefPtr();
+ try testDerefPtr();
}
-fn testDerefPtr() void {
+fn testDerefPtr() !void {
var x: i32 = 1234;
var y = &x;
y.* += 1;
- expect(x == 1235);
+ try expect(x == 1235);
}
const Foo1 = struct {
@@ -20,41 +20,41 @@ const Foo1 = struct {
};
test "dereference pointer again" {
- testDerefPtrOneVal();
- comptime testDerefPtrOneVal();
+ try testDerefPtrOneVal();
+ comptime try testDerefPtrOneVal();
}
-fn testDerefPtrOneVal() void {
+fn testDerefPtrOneVal() !void {
// Foo1 satisfies the OnePossibleValueYes criteria
const x = &Foo1{ .x = {} };
const y = x.*;
- expect(@TypeOf(y.x) == void);
+ try expect(@TypeOf(y.x) == void);
}
test "pointer arithmetic" {
var ptr: [*]const u8 = "abcd";
- expect(ptr[0] == 'a');
+ try expect(ptr[0] == 'a');
ptr += 1;
- expect(ptr[0] == 'b');
+ try expect(ptr[0] == 'b');
ptr += 1;
- expect(ptr[0] == 'c');
+ try expect(ptr[0] == 'c');
ptr += 1;
- expect(ptr[0] == 'd');
+ try expect(ptr[0] == 'd');
ptr += 1;
- expect(ptr[0] == 0);
+ try expect(ptr[0] == 0);
ptr -= 1;
- expect(ptr[0] == 'd');
+ try expect(ptr[0] == 'd');
ptr -= 1;
- expect(ptr[0] == 'c');
+ try expect(ptr[0] == 'c');
ptr -= 1;
- expect(ptr[0] == 'b');
+ try expect(ptr[0] == 'b');
ptr -= 1;
- expect(ptr[0] == 'a');
+ try expect(ptr[0] == 'a');
}
test "double pointer parsing" {
- comptime expect(PtrOf(PtrOf(i32)) == **i32);
+ comptime try expect(PtrOf(PtrOf(i32)) == **i32);
}
fn PtrOf(comptime T: type) type {
@@ -72,33 +72,33 @@ test "implicit cast single item pointer to C pointer and back" {
var x: [*c]u8 = &y;
var z: *u8 = x;
z.* += 1;
- expect(y == 12);
+ try expect(y == 12);
}
test "C pointer comparison and arithmetic" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var one: usize = 1;
var ptr1: [*c]u32 = 0;
var ptr2 = ptr1 + 10;
- expect(ptr1 == 0);
- expect(ptr1 >= 0);
- expect(ptr1 <= 0);
+ try expect(ptr1 == 0);
+ try expect(ptr1 >= 0);
+ try expect(ptr1 <= 0);
// expect(ptr1 < 1);
// expect(ptr1 < one);
// expect(1 > ptr1);
// expect(one > ptr1);
- expect(ptr1 < ptr2);
- expect(ptr2 > ptr1);
- expect(ptr2 >= 40);
- expect(ptr2 == 40);
- expect(ptr2 <= 40);
+ try expect(ptr1 < ptr2);
+ try expect(ptr2 > ptr1);
+ try expect(ptr2 >= 40);
+ try expect(ptr2 == 40);
+ try expect(ptr2 <= 40);
ptr2 -= 10;
- expect(ptr1 == ptr2);
+ try expect(ptr1 == ptr2);
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "peer type resolution with C pointers" {
@@ -110,10 +110,10 @@ test "peer type resolution with C pointers" {
var x2 = if (t) ptr_many else ptr_c;
var x3 = if (t) ptr_c else ptr_one;
var x4 = if (t) ptr_c else ptr_many;
- expect(@TypeOf(x1) == [*c]u8);
- expect(@TypeOf(x2) == [*c]u8);
- expect(@TypeOf(x3) == [*c]u8);
- expect(@TypeOf(x4) == [*c]u8);
+ try expect(@TypeOf(x1) == [*c]u8);
+ try expect(@TypeOf(x2) == [*c]u8);
+ try expect(@TypeOf(x3) == [*c]u8);
+ try expect(@TypeOf(x4) == [*c]u8);
}
test "implicit casting between C pointer and optional non-C pointer" {
@@ -121,15 +121,15 @@ test "implicit casting between C pointer and optional non-C pointer" {
const opt_many_ptr: ?[*]const u8 = slice.ptr;
var ptr_opt_many_ptr = &opt_many_ptr;
var c_ptr: [*c]const [*c]const u8 = ptr_opt_many_ptr;
- expect(c_ptr.*.* == 'a');
+ try expect(c_ptr.*.* == 'a');
ptr_opt_many_ptr = c_ptr;
- expect(ptr_opt_many_ptr.*.?[1] == 'o');
+ try expect(ptr_opt_many_ptr.*.?[1] == 'o');
}
test "implicit cast error unions with non-optional to optional pointer" {
const S = struct {
- fn doTheTest() void {
- expectError(error.Fail, foo());
+ fn doTheTest() !void {
+ try expectError(error.Fail, foo());
}
fn foo() anyerror!?*u8 {
return bar() orelse error.Fail;
@@ -138,111 +138,111 @@ test "implicit cast error unions with non-optional to optional pointer" {
return null;
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "initialize const optional C pointer to null" {
const a: ?[*c]i32 = null;
- expect(a == null);
- comptime expect(a == null);
+ try expect(a == null);
+ comptime try expect(a == null);
}
test "compare equality of optional and non-optional pointer" {
const a = @intToPtr(*const usize, 0x12345678);
const b = @intToPtr(?*usize, 0x12345678);
- expect(a == b);
- expect(b == a);
+ try expect(a == b);
+ try expect(b == a);
}
test "allowzero pointer and slice" {
var ptr = @intToPtr([*]allowzero i32, 0);
var opt_ptr: ?[*]allowzero i32 = ptr;
- expect(opt_ptr != null);
- expect(@ptrToInt(ptr) == 0);
+ try expect(opt_ptr != null);
+ try expect(@ptrToInt(ptr) == 0);
var runtime_zero: usize = 0;
var slice = ptr[runtime_zero..10];
- comptime expect(@TypeOf(slice) == []allowzero i32);
- expect(@ptrToInt(&slice[5]) == 20);
+ comptime try expect(@TypeOf(slice) == []allowzero i32);
+ try expect(@ptrToInt(&slice[5]) == 20);
- comptime expect(@typeInfo(@TypeOf(ptr)).Pointer.is_allowzero);
- comptime expect(@typeInfo(@TypeOf(slice)).Pointer.is_allowzero);
+ comptime try expect(@typeInfo(@TypeOf(ptr)).Pointer.is_allowzero);
+ comptime try expect(@typeInfo(@TypeOf(slice)).Pointer.is_allowzero);
}
test "assign null directly to C pointer and test null equality" {
var x: [*c]i32 = null;
- expect(x == null);
- expect(null == x);
- expect(!(x != null));
- expect(!(null != x));
+ try expect(x == null);
+ try expect(null == x);
+ try expect(!(x != null));
+ try expect(!(null != x));
if (x) |same_x| {
@panic("fail");
}
var otherx: i32 = undefined;
- expect((x orelse &otherx) == &otherx);
+ try expect((x orelse &otherx) == &otherx);
const y: [*c]i32 = null;
- comptime expect(y == null);
- comptime expect(null == y);
- comptime expect(!(y != null));
- comptime expect(!(null != y));
+ comptime try expect(y == null);
+ comptime try expect(null == y);
+ comptime try expect(!(y != null));
+ comptime try expect(!(null != y));
if (y) |same_y| @panic("fail");
const othery: i32 = undefined;
- comptime expect((y orelse &othery) == &othery);
+ comptime try expect((y orelse &othery) == &othery);
var n: i32 = 1234;
var x1: [*c]i32 = &n;
- expect(!(x1 == null));
- expect(!(null == x1));
- expect(x1 != null);
- expect(null != x1);
- expect(x1.?.* == 1234);
+ try expect(!(x1 == null));
+ try expect(!(null == x1));
+ try expect(x1 != null);
+ try expect(null != x1);
+ try expect(x1.?.* == 1234);
if (x1) |same_x1| {
- expect(same_x1.* == 1234);
+ try expect(same_x1.* == 1234);
} else {
@panic("fail");
}
- expect((x1 orelse &otherx) == x1);
+ try expect((x1 orelse &otherx) == x1);
const nc: i32 = 1234;
const y1: [*c]const i32 = &nc;
- comptime expect(!(y1 == null));
- comptime expect(!(null == y1));
- comptime expect(y1 != null);
- comptime expect(null != y1);
- comptime expect(y1.?.* == 1234);
+ comptime try expect(!(y1 == null));
+ comptime try expect(!(null == y1));
+ comptime try expect(y1 != null);
+ comptime try expect(null != y1);
+ comptime try expect(y1.?.* == 1234);
if (y1) |same_y1| {
- expect(same_y1.* == 1234);
+ try expect(same_y1.* == 1234);
} else {
@compileError("fail");
}
- comptime expect((y1 orelse &othery) == y1);
+ comptime try expect((y1 orelse &othery) == y1);
}
test "null terminated pointer" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var array_with_zero = [_:0]u8{ 'h', 'e', 'l', 'l', 'o' };
var zero_ptr: [*:0]const u8 = @ptrCast([*:0]const u8, &array_with_zero);
var no_zero_ptr: [*]const u8 = zero_ptr;
var zero_ptr_again = @ptrCast([*:0]const u8, no_zero_ptr);
- expect(std.mem.eql(u8, std.mem.spanZ(zero_ptr_again), "hello"));
+ try expect(std.mem.eql(u8, std.mem.spanZ(zero_ptr_again), "hello"));
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "allow any sentinel" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var array = [_:std.math.minInt(i32)]i32{ 1, 2, 3, 4 };
var ptr: [*:std.math.minInt(i32)]i32 = &array;
- expect(ptr[4] == std.math.minInt(i32));
+ try expect(ptr[4] == std.math.minInt(i32));
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "pointer sentinel with enums" {
@@ -253,42 +253,42 @@ test "pointer sentinel with enums" {
sentinel,
};
- fn doTheTest() void {
+ fn doTheTest() !void {
var ptr: [*:.sentinel]const Number = &[_:.sentinel]Number{ .one, .two, .two, .one };
- expect(ptr[4] == .sentinel); // TODO this should be comptime expect, see #3731
+ try expect(ptr[4] == .sentinel); // TODO this should be comptime try expect, see #3731
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "pointer sentinel with optional element" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var ptr: [*:null]const ?i32 = &[_:null]?i32{ 1, 2, 3, 4 };
- expect(ptr[4] == null); // TODO this should be comptime expect, see #3731
+ try expect(ptr[4] == null); // TODO this should be comptime try expect, see #3731
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "pointer sentinel with +inf" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
const inf = std.math.inf_f32;
var ptr: [*:inf]const f32 = &[_:inf]f32{ 1.1, 2.2, 3.3, 4.4 };
- expect(ptr[4] == inf); // TODO this should be comptime expect, see #3731
+ try expect(ptr[4] == inf); // TODO this should be comptime try expect, see #3731
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "pointer to array at fixed address" {
const array = @intToPtr(*volatile [1]u32, 0x10);
// Silly check just to reference `array`
- expect(@ptrToInt(&array[0]) == 0x10);
+ try expect(@ptrToInt(&array[0]) == 0x10);
}
test "pointer arithmetic affects the alignment" {
@@ -296,28 +296,28 @@ test "pointer arithmetic affects the alignment" {
var ptr: [*]align(8) u32 = undefined;
var x: usize = 1;
- expect(@typeInfo(@TypeOf(ptr)).Pointer.alignment == 8);
+ try expect(@typeInfo(@TypeOf(ptr)).Pointer.alignment == 8);
const ptr1 = ptr + 1; // 1 * 4 = 4 -> lcd(4,8) = 4
- expect(@typeInfo(@TypeOf(ptr1)).Pointer.alignment == 4);
+ try expect(@typeInfo(@TypeOf(ptr1)).Pointer.alignment == 4);
const ptr2 = ptr + 4; // 4 * 4 = 16 -> lcd(16,8) = 8
- expect(@typeInfo(@TypeOf(ptr2)).Pointer.alignment == 8);
+ try expect(@typeInfo(@TypeOf(ptr2)).Pointer.alignment == 8);
const ptr3 = ptr + 0; // no-op
- expect(@typeInfo(@TypeOf(ptr3)).Pointer.alignment == 8);
+ try expect(@typeInfo(@TypeOf(ptr3)).Pointer.alignment == 8);
const ptr4 = ptr + x; // runtime-known addend
- expect(@typeInfo(@TypeOf(ptr4)).Pointer.alignment == 4);
+ try expect(@typeInfo(@TypeOf(ptr4)).Pointer.alignment == 4);
}
{
var ptr: [*]align(8) [3]u8 = undefined;
var x: usize = 1;
const ptr1 = ptr + 17; // 3 * 17 = 51
- expect(@typeInfo(@TypeOf(ptr1)).Pointer.alignment == 1);
+ try expect(@typeInfo(@TypeOf(ptr1)).Pointer.alignment == 1);
const ptr2 = ptr + x; // runtime-known addend
- expect(@typeInfo(@TypeOf(ptr2)).Pointer.alignment == 1);
+ try expect(@typeInfo(@TypeOf(ptr2)).Pointer.alignment == 1);
const ptr3 = ptr + 8; // 3 * 8 = 24 -> lcd(8,24) = 8
- expect(@typeInfo(@TypeOf(ptr3)).Pointer.alignment == 8);
+ try expect(@typeInfo(@TypeOf(ptr3)).Pointer.alignment == 8);
const ptr4 = ptr + 4; // 3 * 4 = 12 -> lcd(8,12) = 4
- expect(@typeInfo(@TypeOf(ptr4)).Pointer.alignment == 4);
+ try expect(@typeInfo(@TypeOf(ptr4)).Pointer.alignment == 4);
}
}
@@ -325,15 +325,15 @@ test "@ptrToInt on null optional at comptime" {
{
const pointer = @intToPtr(?*u8, 0x000);
const x = @ptrToInt(pointer);
- comptime expect(0 == @ptrToInt(pointer));
+ comptime try expect(0 == @ptrToInt(pointer));
}
{
const pointer = @intToPtr(?*u8, 0xf00);
- comptime expect(0xf00 == @ptrToInt(pointer));
+ comptime try expect(0xf00 == @ptrToInt(pointer));
}
}
test "indexing array with sentinel returns correct type" {
var s: [:0]const u8 = "abc";
- testing.expectEqualSlices(u8, "*const u8", @typeName(@TypeOf(&s[0])));
+ try testing.expectEqualSlices(u8, "*const u8", @typeName(@TypeOf(&s[0])));
}
test/stage1/behavior/popcount.zig
@@ -1,43 +1,43 @@
const expect = @import("std").testing.expect;
test "@popCount" {
- comptime testPopCount();
- testPopCount();
+ comptime try testPopCount();
+ try testPopCount();
}
-fn testPopCount() void {
+fn testPopCount() !void {
{
var x: u32 = 0xffffffff;
- expect(@popCount(u32, x) == 32);
+ try expect(@popCount(u32, x) == 32);
}
{
var x: u5 = 0x1f;
- expect(@popCount(u5, x) == 5);
+ try expect(@popCount(u5, x) == 5);
}
{
var x: u32 = 0xaa;
- expect(@popCount(u32, x) == 4);
+ try expect(@popCount(u32, x) == 4);
}
{
var x: u32 = 0xaaaaaaaa;
- expect(@popCount(u32, x) == 16);
+ try expect(@popCount(u32, x) == 16);
}
{
var x: u32 = 0xaaaaaaaa;
- expect(@popCount(u32, x) == 16);
+ try expect(@popCount(u32, x) == 16);
}
{
var x: i16 = -1;
- expect(@popCount(i16, x) == 16);
+ try expect(@popCount(i16, x) == 16);
}
{
var x: i8 = -120;
- expect(@popCount(i8, x) == 2);
+ try expect(@popCount(i8, x) == 2);
}
comptime {
- expect(@popCount(u8, @bitCast(u8, @as(i8, -120))) == 2);
+ try expect(@popCount(u8, @bitCast(u8, @as(i8, -120))) == 2);
}
comptime {
- expect(@popCount(i128, 0b11111111000110001100010000100001000011000011100101010001) == 24);
+ try expect(@popCount(i128, 0b11111111000110001100010000100001000011000011100101010001) == 24);
}
}
test/stage1/behavior/ptrcast.zig
@@ -3,25 +3,25 @@ const builtin = std.builtin;
const expect = std.testing.expect;
test "reinterpret bytes as integer with nonzero offset" {
- testReinterpretBytesAsInteger();
- comptime testReinterpretBytesAsInteger();
+ try testReinterpretBytesAsInteger();
+ comptime try testReinterpretBytesAsInteger();
}
-fn testReinterpretBytesAsInteger() void {
+fn testReinterpretBytesAsInteger() !void {
const bytes = "\x12\x34\x56\x78\xab";
const expected = switch (builtin.endian) {
builtin.Endian.Little => 0xab785634,
builtin.Endian.Big => 0x345678ab,
};
- expect(@ptrCast(*align(1) const u32, bytes[1..5]).* == expected);
+ try expect(@ptrCast(*align(1) const u32, bytes[1..5]).* == expected);
}
test "reinterpret bytes of an array into an extern struct" {
- testReinterpretBytesAsExternStruct();
- comptime testReinterpretBytesAsExternStruct();
+ try testReinterpretBytesAsExternStruct();
+ comptime try testReinterpretBytesAsExternStruct();
}
-fn testReinterpretBytesAsExternStruct() void {
+fn testReinterpretBytesAsExternStruct() !void {
var bytes align(2) = [_]u8{ 1, 2, 3, 4, 5, 6 };
const S = extern struct {
@@ -32,15 +32,15 @@ fn testReinterpretBytesAsExternStruct() void {
var ptr = @ptrCast(*const S, &bytes);
var val = ptr.c;
- expect(val == 5);
+ try expect(val == 5);
}
test "reinterpret struct field at comptime" {
const numNative = comptime Bytes.init(0x12345678);
if (builtin.endian != .Little) {
- expect(std.mem.eql(u8, &[_]u8{ 0x12, 0x34, 0x56, 0x78 }, &numNative.bytes));
+ try expect(std.mem.eql(u8, &[_]u8{ 0x12, 0x34, 0x56, 0x78 }, &numNative.bytes));
} else {
- expect(std.mem.eql(u8, &[_]u8{ 0x78, 0x56, 0x34, 0x12 }, &numNative.bytes));
+ try expect(std.mem.eql(u8, &[_]u8{ 0x78, 0x56, 0x34, 0x12 }, &numNative.bytes));
}
}
@@ -59,7 +59,7 @@ test "comptime ptrcast keeps larger alignment" {
comptime {
const a: u32 = 1234;
const p = @ptrCast([*]const u8, &a);
- std.debug.assert(@TypeOf(p) == [*]align(@alignOf(u32)) const u8);
+ try expect(@TypeOf(p) == [*]align(@alignOf(u32)) const u8);
}
}
@@ -68,5 +68,5 @@ test "implicit optional pointer to optional c_void pointer" {
var x: ?[*]u8 = &buf;
var y: ?*c_void = x;
var z = @ptrCast(*[4]u8, y);
- expect(std.mem.eql(u8, z, "aoeu"));
+ try expect(std.mem.eql(u8, z, "aoeu"));
}
test/stage1/behavior/pub_enum.zig
@@ -2,12 +2,12 @@ const other = @import("pub_enum/other.zig");
const expect = @import("std").testing.expect;
test "pub enum" {
- pubEnumTest(other.APubEnum.Two);
+ try pubEnumTest(other.APubEnum.Two);
}
-fn pubEnumTest(foo: other.APubEnum) void {
- expect(foo == other.APubEnum.Two);
+fn pubEnumTest(foo: other.APubEnum) !void {
+ try expect(foo == other.APubEnum.Two);
}
test "cast with imported symbol" {
- expect(@as(other.size_t, 42) == 42);
+ try expect(@as(other.size_t, 42) == 42);
}
test/stage1/behavior/ref_var_in_if_after_if_2nd_switch_prong.zig
@@ -3,12 +3,12 @@ const mem = @import("std").mem;
var ok: bool = false;
test "reference a variable in an if after an if in the 2nd switch prong" {
- foo(true, Num.Two, false, "aoeu");
- expect(!ok);
- foo(false, Num.One, false, "aoeu");
- expect(!ok);
- foo(true, Num.One, false, "aoeu");
- expect(ok);
+ try foo(true, Num.Two, false, "aoeu");
+ try expect(!ok);
+ try foo(false, Num.One, false, "aoeu");
+ try expect(!ok);
+ try foo(true, Num.One, false, "aoeu");
+ try expect(ok);
}
const Num = enum {
@@ -16,7 +16,7 @@ const Num = enum {
Two,
};
-fn foo(c: bool, k: Num, c2: bool, b: []const u8) void {
+fn foo(c: bool, k: Num, c2: bool, b: []const u8) !void {
switch (k) {
Num.Two => {},
Num.One => {
@@ -25,13 +25,13 @@ fn foo(c: bool, k: Num, c2: bool, b: []const u8) void {
if (c2) {}
- a(output_path);
+ try a(output_path);
}
},
}
}
-fn a(x: []const u8) void {
- expect(mem.eql(u8, x, "aoeu"));
+fn a(x: []const u8) !void {
+ try expect(mem.eql(u8, x, "aoeu"));
ok = true;
}
test/stage1/behavior/reflection.zig
@@ -5,12 +5,12 @@ const reflection = @This();
test "reflection: function return type, var args, and param types" {
comptime {
const info = @typeInfo(@TypeOf(dummy)).Fn;
- expect(info.return_type.? == i32);
- expect(!info.is_var_args);
- expect(info.args.len == 3);
- expect(info.args[0].arg_type.? == bool);
- expect(info.args[1].arg_type.? == i32);
- expect(info.args[2].arg_type.? == f32);
+ try expect(info.return_type.? == i32);
+ try expect(!info.is_var_args);
+ try expect(info.args.len == 3);
+ try expect(info.args[0].arg_type.? == bool);
+ try expect(info.args[1].arg_type.? == i32);
+ try expect(info.args[2].arg_type.? == f32);
}
}
@@ -25,18 +25,18 @@ test "reflection: @field" {
.three = void{},
};
- expect(f.one == f.one);
- expect(@field(f, "o" ++ "ne") == f.one);
- expect(@field(f, "t" ++ "wo") == f.two);
- expect(@field(f, "th" ++ "ree") == f.three);
- expect(@field(Foo, "const" ++ "ant") == Foo.constant);
- expect(@field(Bar, "O" ++ "ne") == Bar.One);
- expect(@field(Bar, "T" ++ "wo") == Bar.Two);
- expect(@field(Bar, "Th" ++ "ree") == Bar.Three);
- expect(@field(Bar, "F" ++ "our") == Bar.Four);
- expect(@field(reflection, "dum" ++ "my")(true, 1, 2) == dummy(true, 1, 2));
+ try expect(f.one == f.one);
+ try expect(@field(f, "o" ++ "ne") == f.one);
+ try expect(@field(f, "t" ++ "wo") == f.two);
+ try expect(@field(f, "th" ++ "ree") == f.three);
+ try expect(@field(Foo, "const" ++ "ant") == Foo.constant);
+ try expect(@field(Bar, "O" ++ "ne") == Bar.One);
+ try expect(@field(Bar, "T" ++ "wo") == Bar.Two);
+ try expect(@field(Bar, "Th" ++ "ree") == Bar.Three);
+ try expect(@field(Bar, "F" ++ "our") == Bar.Four);
+ try expect(@field(reflection, "dum" ++ "my")(true, 1, 2) == dummy(true, 1, 2));
@field(f, "o" ++ "ne") = 4;
- expect(f.one == 4);
+ try expect(f.one == 4);
}
const Foo = struct {
test/stage1/behavior/shuffle.zig
@@ -9,33 +9,33 @@ test "@shuffle" {
if (builtin.os.tag == .wasi) return error.SkipZigTest;
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var v: Vector(4, i32) = [4]i32{ 2147483647, -2, 30, 40 };
var x: Vector(4, i32) = [4]i32{ 1, 2147483647, 3, 4 };
const mask: Vector(4, i32) = [4]i32{ 0, ~@as(i32, 2), 3, ~@as(i32, 3) };
var res = @shuffle(i32, v, x, mask);
- expect(mem.eql(i32, &@as([4]i32, res), &[4]i32{ 2147483647, 3, 40, 4 }));
+ try expect(mem.eql(i32, &@as([4]i32, res), &[4]i32{ 2147483647, 3, 40, 4 }));
// Implicit cast from array (of mask)
res = @shuffle(i32, v, x, [4]i32{ 0, ~@as(i32, 2), 3, ~@as(i32, 3) });
- expect(mem.eql(i32, &@as([4]i32, res), &[4]i32{ 2147483647, 3, 40, 4 }));
+ try expect(mem.eql(i32, &@as([4]i32, res), &[4]i32{ 2147483647, 3, 40, 4 }));
// Undefined
const mask2: Vector(4, i32) = [4]i32{ 3, 1, 2, 0 };
res = @shuffle(i32, v, undefined, mask2);
- expect(mem.eql(i32, &@as([4]i32, res), &[4]i32{ 40, -2, 30, 2147483647 }));
+ try expect(mem.eql(i32, &@as([4]i32, res), &[4]i32{ 40, -2, 30, 2147483647 }));
// Upcasting of b
var v2: Vector(2, i32) = [2]i32{ 2147483647, undefined };
const mask3: Vector(4, i32) = [4]i32{ ~@as(i32, 0), 2, ~@as(i32, 0), 3 };
res = @shuffle(i32, x, v2, mask3);
- expect(mem.eql(i32, &@as([4]i32, res), &[4]i32{ 2147483647, 3, 2147483647, 4 }));
+ try expect(mem.eql(i32, &@as([4]i32, res), &[4]i32{ 2147483647, 3, 2147483647, 4 }));
// Upcasting of a
var v3: Vector(2, i32) = [2]i32{ 2147483647, -2 };
const mask4: Vector(4, i32) = [4]i32{ 0, ~@as(i32, 2), 1, ~@as(i32, 3) };
res = @shuffle(i32, v3, x, mask4);
- expect(mem.eql(i32, &@as([4]i32, res), &[4]i32{ 2147483647, 3, -2, 4 }));
+ try expect(mem.eql(i32, &@as([4]i32, res), &[4]i32{ 2147483647, 3, -2, 4 }));
// bool
// Disabled because of #3317
@@ -44,7 +44,7 @@ test "@shuffle" {
var v4: Vector(2, bool) = [2]bool{ true, false };
const mask5: Vector(4, i32) = [4]i32{ 0, ~@as(i32, 1), 1, 2 };
var res2 = @shuffle(bool, x2, v4, mask5);
- expect(mem.eql(bool, &@as([4]bool, res2), &[4]bool{ false, false, true, false }));
+ try expect(mem.eql(bool, &@as([4]bool, res2), &[4]bool{ false, false, true, false }));
}
// TODO re-enable when LLVM codegen is fixed
@@ -54,10 +54,10 @@ test "@shuffle" {
var v4: Vector(2, bool) = [2]bool{ true, false };
const mask5: Vector(4, i32) = [4]i32{ 0, ~@as(i32, 1), 1, 2 };
var res2 = @shuffle(bool, x2, v4, mask5);
- expect(mem.eql(bool, &@as([4]bool, res2), &[4]bool{ false, false, true, false }));
+ try expect(mem.eql(bool, &@as([4]bool, res2), &[4]bool{ false, false, true, false }));
}
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test/stage1/behavior/sizeof_and_typeof.zig
@@ -5,7 +5,7 @@ const expectEqual = std.testing.expectEqual;
test "@sizeOf and @TypeOf" {
const y: @TypeOf(x) = 120;
- expect(@sizeOf(@TypeOf(y)) == 2);
+ try expect(@sizeOf(@TypeOf(y)) == 2);
}
const x: u16 = 13;
const z: @TypeOf(x) = 19;
@@ -36,27 +36,27 @@ const P = packed struct {
test "@byteOffsetOf" {
// Packed structs have fixed memory layout
- expect(@byteOffsetOf(P, "a") == 0);
- expect(@byteOffsetOf(P, "b") == 1);
- expect(@byteOffsetOf(P, "c") == 5);
- expect(@byteOffsetOf(P, "d") == 6);
- expect(@byteOffsetOf(P, "e") == 6);
- expect(@byteOffsetOf(P, "f") == 7);
- expect(@byteOffsetOf(P, "g") == 9);
- expect(@byteOffsetOf(P, "h") == 11);
- expect(@byteOffsetOf(P, "i") == 12);
+ try expect(@byteOffsetOf(P, "a") == 0);
+ try expect(@byteOffsetOf(P, "b") == 1);
+ try expect(@byteOffsetOf(P, "c") == 5);
+ try expect(@byteOffsetOf(P, "d") == 6);
+ try expect(@byteOffsetOf(P, "e") == 6);
+ try expect(@byteOffsetOf(P, "f") == 7);
+ try expect(@byteOffsetOf(P, "g") == 9);
+ try expect(@byteOffsetOf(P, "h") == 11);
+ try expect(@byteOffsetOf(P, "i") == 12);
// Normal struct fields can be moved/padded
var a: A = undefined;
- expect(@ptrToInt(&a.a) - @ptrToInt(&a) == @byteOffsetOf(A, "a"));
- expect(@ptrToInt(&a.b) - @ptrToInt(&a) == @byteOffsetOf(A, "b"));
- expect(@ptrToInt(&a.c) - @ptrToInt(&a) == @byteOffsetOf(A, "c"));
- expect(@ptrToInt(&a.d) - @ptrToInt(&a) == @byteOffsetOf(A, "d"));
- expect(@ptrToInt(&a.e) - @ptrToInt(&a) == @byteOffsetOf(A, "e"));
- expect(@ptrToInt(&a.f) - @ptrToInt(&a) == @byteOffsetOf(A, "f"));
- expect(@ptrToInt(&a.g) - @ptrToInt(&a) == @byteOffsetOf(A, "g"));
- expect(@ptrToInt(&a.h) - @ptrToInt(&a) == @byteOffsetOf(A, "h"));
- expect(@ptrToInt(&a.i) - @ptrToInt(&a) == @byteOffsetOf(A, "i"));
+ try expect(@ptrToInt(&a.a) - @ptrToInt(&a) == @byteOffsetOf(A, "a"));
+ try expect(@ptrToInt(&a.b) - @ptrToInt(&a) == @byteOffsetOf(A, "b"));
+ try expect(@ptrToInt(&a.c) - @ptrToInt(&a) == @byteOffsetOf(A, "c"));
+ try expect(@ptrToInt(&a.d) - @ptrToInt(&a) == @byteOffsetOf(A, "d"));
+ try expect(@ptrToInt(&a.e) - @ptrToInt(&a) == @byteOffsetOf(A, "e"));
+ try expect(@ptrToInt(&a.f) - @ptrToInt(&a) == @byteOffsetOf(A, "f"));
+ try expect(@ptrToInt(&a.g) - @ptrToInt(&a) == @byteOffsetOf(A, "g"));
+ try expect(@ptrToInt(&a.h) - @ptrToInt(&a) == @byteOffsetOf(A, "h"));
+ try expect(@ptrToInt(&a.i) - @ptrToInt(&a) == @byteOffsetOf(A, "i"));
}
test "@byteOffsetOf packed struct, array length not power of 2 or multiple of native pointer width in bytes" {
@@ -65,68 +65,68 @@ test "@byteOffsetOf packed struct, array length not power of 2 or multiple of na
a: [p3a_len]u8,
b: usize,
};
- std.testing.expectEqual(0, @byteOffsetOf(P3, "a"));
- std.testing.expectEqual(p3a_len, @byteOffsetOf(P3, "b"));
+ try std.testing.expectEqual(0, @byteOffsetOf(P3, "a"));
+ try std.testing.expectEqual(p3a_len, @byteOffsetOf(P3, "b"));
const p5a_len = 5;
const P5 = packed struct {
a: [p5a_len]u8,
b: usize,
};
- std.testing.expectEqual(0, @byteOffsetOf(P5, "a"));
- std.testing.expectEqual(p5a_len, @byteOffsetOf(P5, "b"));
+ try std.testing.expectEqual(0, @byteOffsetOf(P5, "a"));
+ try std.testing.expectEqual(p5a_len, @byteOffsetOf(P5, "b"));
const p6a_len = 6;
const P6 = packed struct {
a: [p6a_len]u8,
b: usize,
};
- std.testing.expectEqual(0, @byteOffsetOf(P6, "a"));
- std.testing.expectEqual(p6a_len, @byteOffsetOf(P6, "b"));
+ try std.testing.expectEqual(0, @byteOffsetOf(P6, "a"));
+ try std.testing.expectEqual(p6a_len, @byteOffsetOf(P6, "b"));
const p7a_len = 7;
const P7 = packed struct {
a: [p7a_len]u8,
b: usize,
};
- std.testing.expectEqual(0, @byteOffsetOf(P7, "a"));
- std.testing.expectEqual(p7a_len, @byteOffsetOf(P7, "b"));
+ try std.testing.expectEqual(0, @byteOffsetOf(P7, "a"));
+ try std.testing.expectEqual(p7a_len, @byteOffsetOf(P7, "b"));
const p9a_len = 9;
const P9 = packed struct {
a: [p9a_len]u8,
b: usize,
};
- std.testing.expectEqual(0, @byteOffsetOf(P9, "a"));
- std.testing.expectEqual(p9a_len, @byteOffsetOf(P9, "b"));
+ try std.testing.expectEqual(0, @byteOffsetOf(P9, "a"));
+ try std.testing.expectEqual(p9a_len, @byteOffsetOf(P9, "b"));
// 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 25 etc. are further cases
}
test "@bitOffsetOf" {
// Packed structs have fixed memory layout
- expect(@bitOffsetOf(P, "a") == 0);
- expect(@bitOffsetOf(P, "b") == 8);
- expect(@bitOffsetOf(P, "c") == 40);
- expect(@bitOffsetOf(P, "d") == 48);
- expect(@bitOffsetOf(P, "e") == 51);
- expect(@bitOffsetOf(P, "f") == 56);
- expect(@bitOffsetOf(P, "g") == 72);
+ try expect(@bitOffsetOf(P, "a") == 0);
+ try expect(@bitOffsetOf(P, "b") == 8);
+ try expect(@bitOffsetOf(P, "c") == 40);
+ try expect(@bitOffsetOf(P, "d") == 48);
+ try expect(@bitOffsetOf(P, "e") == 51);
+ try expect(@bitOffsetOf(P, "f") == 56);
+ try expect(@bitOffsetOf(P, "g") == 72);
- expect(@byteOffsetOf(A, "a") * 8 == @bitOffsetOf(A, "a"));
- expect(@byteOffsetOf(A, "b") * 8 == @bitOffsetOf(A, "b"));
- expect(@byteOffsetOf(A, "c") * 8 == @bitOffsetOf(A, "c"));
- expect(@byteOffsetOf(A, "d") * 8 == @bitOffsetOf(A, "d"));
- expect(@byteOffsetOf(A, "e") * 8 == @bitOffsetOf(A, "e"));
- expect(@byteOffsetOf(A, "f") * 8 == @bitOffsetOf(A, "f"));
- expect(@byteOffsetOf(A, "g") * 8 == @bitOffsetOf(A, "g"));
+ try expect(@byteOffsetOf(A, "a") * 8 == @bitOffsetOf(A, "a"));
+ try expect(@byteOffsetOf(A, "b") * 8 == @bitOffsetOf(A, "b"));
+ try expect(@byteOffsetOf(A, "c") * 8 == @bitOffsetOf(A, "c"));
+ try expect(@byteOffsetOf(A, "d") * 8 == @bitOffsetOf(A, "d"));
+ try expect(@byteOffsetOf(A, "e") * 8 == @bitOffsetOf(A, "e"));
+ try expect(@byteOffsetOf(A, "f") * 8 == @bitOffsetOf(A, "f"));
+ try expect(@byteOffsetOf(A, "g") * 8 == @bitOffsetOf(A, "g"));
}
test "@sizeOf on compile-time types" {
- expect(@sizeOf(comptime_int) == 0);
- expect(@sizeOf(comptime_float) == 0);
- expect(@sizeOf(@TypeOf(.hi)) == 0);
- expect(@sizeOf(@TypeOf(type)) == 0);
+ try expect(@sizeOf(comptime_int) == 0);
+ try expect(@sizeOf(comptime_float) == 0);
+ try expect(@sizeOf(@TypeOf(.hi)) == 0);
+ try expect(@sizeOf(@TypeOf(type)) == 0);
}
test "@sizeOf(T) == 0 doesn't force resolving struct size" {
@@ -140,8 +140,8 @@ test "@sizeOf(T) == 0 doesn't force resolving struct size" {
};
};
- expect(@sizeOf(S.Foo) == 4);
- expect(@sizeOf(S.Bar) == 8);
+ try expect(@sizeOf(S.Foo) == 4);
+ try expect(@sizeOf(S.Bar) == 8);
}
test "@TypeOf() has no runtime side effects" {
@@ -153,8 +153,8 @@ test "@TypeOf() has no runtime side effects" {
};
var data: i32 = 0;
const T = @TypeOf(S.foo(i32, &data));
- comptime expect(T == i32);
- expect(data == 0);
+ comptime try expect(T == i32);
+ try expect(data == 0);
}
test "@TypeOf() with multiple arguments" {
@@ -162,21 +162,21 @@ test "@TypeOf() with multiple arguments" {
var var_1: u32 = undefined;
var var_2: u8 = undefined;
var var_3: u64 = undefined;
- comptime expect(@TypeOf(var_1, var_2, var_3) == u64);
+ comptime try expect(@TypeOf(var_1, var_2, var_3) == u64);
}
{
var var_1: f16 = undefined;
var var_2: f32 = undefined;
var var_3: f64 = undefined;
- comptime expect(@TypeOf(var_1, var_2, var_3) == f64);
+ comptime try expect(@TypeOf(var_1, var_2, var_3) == f64);
}
{
var var_1: u16 = undefined;
- comptime expect(@TypeOf(var_1, 0xffff) == u16);
+ comptime try expect(@TypeOf(var_1, 0xffff) == u16);
}
{
var var_1: f32 = undefined;
- comptime expect(@TypeOf(var_1, 3.1415) == f32);
+ comptime try expect(@TypeOf(var_1, 3.1415) == f32);
}
}
@@ -189,8 +189,8 @@ test "branching logic inside @TypeOf" {
}
};
const T = @TypeOf(S.foo() catch undefined);
- comptime expect(T == i32);
- expect(S.data == 0);
+ comptime try expect(T == i32);
+ try expect(S.data == 0);
}
fn fn1(alpha: bool) void {
@@ -203,12 +203,12 @@ test "lazy @sizeOf result is checked for definedness" {
}
test "@bitSizeOf" {
- expect(@bitSizeOf(u2) == 2);
- expect(@bitSizeOf(u8) == @sizeOf(u8) * 8);
- expect(@bitSizeOf(struct {
+ try expect(@bitSizeOf(u2) == 2);
+ try expect(@bitSizeOf(u8) == @sizeOf(u8) * 8);
+ try expect(@bitSizeOf(struct {
a: u2,
}) == 8);
- expect(@bitSizeOf(packed struct {
+ try expect(@bitSizeOf(packed struct {
a: u2,
}) == 2);
}
@@ -241,24 +241,24 @@ test "@sizeOf comparison against zero" {
f2: H(***@This()),
};
const S = struct {
- fn doTheTest(comptime T: type, comptime result: bool) void {
- expectEqual(result, @sizeOf(T) > 0);
+ fn doTheTest(comptime T: type, comptime result: bool) !void {
+ try expectEqual(result, @sizeOf(T) > 0);
}
};
// Zero-sized type
- S.doTheTest(u0, false);
- S.doTheTest(*u0, false);
+ try S.doTheTest(u0, false);
+ try S.doTheTest(*u0, false);
// Non byte-sized type
- S.doTheTest(u1, true);
- S.doTheTest(*u1, true);
+ try S.doTheTest(u1, true);
+ try S.doTheTest(*u1, true);
// Regular type
- S.doTheTest(u8, true);
- S.doTheTest(*u8, true);
- S.doTheTest(f32, true);
- S.doTheTest(*f32, true);
+ try S.doTheTest(u8, true);
+ try S.doTheTest(*u8, true);
+ try S.doTheTest(f32, true);
+ try S.doTheTest(*f32, true);
// Container with ptr pointing to themselves
- S.doTheTest(S0, true);
- S.doTheTest(U0, true);
- S.doTheTest(S1, true);
- S.doTheTest(U1, true);
+ try S.doTheTest(S0, true);
+ try S.doTheTest(U0, true);
+ try S.doTheTest(S1, true);
+ try S.doTheTest(U1, true);
}
test/stage1/behavior/slice.zig
@@ -7,11 +7,11 @@ const mem = std.mem;
const x = @intToPtr([*]i32, 0x1000)[0..0x500];
const y = x[0x100..];
test "compile time slice of pointer to hard coded address" {
- expect(@ptrToInt(x) == 0x1000);
- expect(x.len == 0x500);
+ try expect(@ptrToInt(x) == 0x1000);
+ try expect(x.len == 0x500);
- expect(@ptrToInt(y) == 0x1100);
- expect(y.len == 0x400);
+ try expect(@ptrToInt(y) == 0x1100);
+ try expect(y.len == 0x400);
}
test "runtime safety lets us slice from len..len" {
@@ -20,7 +20,7 @@ test "runtime safety lets us slice from len..len" {
2,
3,
};
- expect(mem.eql(u8, sliceFromLenToLen(an_array[0..], 3, 3), ""));
+ try expect(mem.eql(u8, sliceFromLenToLen(an_array[0..], 3, 3), ""));
}
fn sliceFromLenToLen(a_slice: []u8, start: usize, end: usize) []u8 {
@@ -29,18 +29,18 @@ fn sliceFromLenToLen(a_slice: []u8, start: usize, end: usize) []u8 {
test "implicitly cast array of size 0 to slice" {
var msg = [_]u8{};
- assertLenIsZero(&msg);
+ try assertLenIsZero(&msg);
}
-fn assertLenIsZero(msg: []const u8) void {
- expect(msg.len == 0);
+fn assertLenIsZero(msg: []const u8) !void {
+ try expect(msg.len == 0);
}
test "C pointer" {
var buf: [*c]const u8 = "kjdhfkjdhfdkjhfkfjhdfkjdhfkdjhfdkjhf";
var len: u32 = 10;
var slice = buf[0..len];
- expectEqualSlices(u8, "kjdhfkjdhf", slice);
+ try expectEqualSlices(u8, "kjdhfkjdhf", slice);
}
test "C pointer slice access" {
@@ -48,11 +48,11 @@ test "C pointer slice access" {
const c_ptr = @ptrCast([*c]const u32, &buf);
var runtime_zero: usize = 0;
- comptime expectEqual([]const u32, @TypeOf(c_ptr[runtime_zero..1]));
- comptime expectEqual(*const [1]u32, @TypeOf(c_ptr[0..1]));
+ comptime try expectEqual([]const u32, @TypeOf(c_ptr[runtime_zero..1]));
+ comptime try expectEqual(*const [1]u32, @TypeOf(c_ptr[0..1]));
for (c_ptr[0..5]) |*cl| {
- expectEqual(@as(u32, 42), cl.*);
+ try expectEqual(@as(u32, 42), cl.*);
}
}
@@ -65,8 +65,8 @@ fn sliceSum(comptime q: []const u8) i32 {
}
test "comptime slices are disambiguated" {
- expect(sliceSum(&[_]u8{ 1, 2 }) == 3);
- expect(sliceSum(&[_]u8{ 3, 4 }) == 7);
+ try expect(sliceSum(&[_]u8{ 1, 2 }) == 3);
+ try expect(sliceSum(&[_]u8{ 3, 4 }) == 7);
}
test "slice type with custom alignment" {
@@ -77,20 +77,20 @@ test "slice type with custom alignment" {
var array: [10]LazilyResolvedType align(32) = undefined;
slice = &array;
slice[1].anything = 42;
- expect(array[1].anything == 42);
+ try expect(array[1].anything == 42);
}
test "access len index of sentinel-terminated slice" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var slice: [:0]const u8 = "hello";
- expect(slice.len == 5);
- expect(slice[5] == 0);
+ try expect(slice.len == 5);
+ try expect(slice[5] == 0);
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "obtaining a null terminated slice" {
@@ -108,230 +108,230 @@ test "obtaining a null terminated slice" {
var runtime_len: usize = 3;
const ptr2 = buf[0..runtime_len :0];
// ptr2 is a null-terminated slice
- comptime expect(@TypeOf(ptr2) == [:0]u8);
- comptime expect(@TypeOf(ptr2[0..2]) == *[2]u8);
+ comptime try expect(@TypeOf(ptr2) == [:0]u8);
+ comptime try expect(@TypeOf(ptr2[0..2]) == *[2]u8);
var runtime_zero: usize = 0;
- comptime expect(@TypeOf(ptr2[runtime_zero..2]) == []u8);
+ comptime try expect(@TypeOf(ptr2[runtime_zero..2]) == []u8);
}
test "empty array to slice" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
const empty: []align(16) u8 = &[_]u8{};
const align_1: []align(1) u8 = empty;
const align_4: []align(4) u8 = empty;
const align_16: []align(16) u8 = empty;
- expectEqual(1, @typeInfo(@TypeOf(align_1)).Pointer.alignment);
- expectEqual(4, @typeInfo(@TypeOf(align_4)).Pointer.alignment);
- expectEqual(16, @typeInfo(@TypeOf(align_16)).Pointer.alignment);
+ try expectEqual(1, @typeInfo(@TypeOf(align_1)).Pointer.alignment);
+ try expectEqual(4, @typeInfo(@TypeOf(align_4)).Pointer.alignment);
+ try expectEqual(16, @typeInfo(@TypeOf(align_16)).Pointer.alignment);
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "@ptrCast slice to pointer" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var array align(@alignOf(u16)) = [5]u8{ 0xff, 0xff, 0xff, 0xff, 0xff };
var slice: []u8 = &array;
var ptr = @ptrCast(*u16, slice);
- expect(ptr.* == 65535);
+ try expect(ptr.* == 65535);
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "slice syntax resulting in pointer-to-array" {
const S = struct {
- fn doTheTest() void {
- testArray();
- testArrayZ();
- testArray0();
- testArrayAlign();
- testPointer();
- testPointerZ();
- testPointer0();
- testPointerAlign();
- testSlice();
- testSliceZ();
- testSlice0();
- testSliceOpt();
- testSliceAlign();
+ fn doTheTest() !void {
+ try testArray();
+ try testArrayZ();
+ try testArray0();
+ try testArrayAlign();
+ try testPointer();
+ try testPointerZ();
+ try testPointer0();
+ try testPointerAlign();
+ try testSlice();
+ try testSliceZ();
+ try testSlice0();
+ try testSliceOpt();
+ try testSliceAlign();
}
- fn testArray() void {
+ fn testArray() !void {
var array = [5]u8{ 1, 2, 3, 4, 5 };
var slice = array[1..3];
- comptime expect(@TypeOf(slice) == *[2]u8);
- expect(slice[0] == 2);
- expect(slice[1] == 3);
+ comptime try expect(@TypeOf(slice) == *[2]u8);
+ try expect(slice[0] == 2);
+ try expect(slice[1] == 3);
}
- fn testArrayZ() void {
+ fn testArrayZ() !void {
var array = [5:0]u8{ 1, 2, 3, 4, 5 };
- comptime expect(@TypeOf(array[1..3]) == *[2]u8);
- comptime expect(@TypeOf(array[1..5]) == *[4:0]u8);
- comptime expect(@TypeOf(array[1..]) == *[4:0]u8);
- comptime expect(@TypeOf(array[1..3 :4]) == *[2:4]u8);
+ comptime try expect(@TypeOf(array[1..3]) == *[2]u8);
+ comptime try expect(@TypeOf(array[1..5]) == *[4:0]u8);
+ comptime try expect(@TypeOf(array[1..]) == *[4:0]u8);
+ comptime try expect(@TypeOf(array[1..3 :4]) == *[2:4]u8);
}
- fn testArray0() void {
+ fn testArray0() !void {
{
var array = [0]u8{};
var slice = array[0..0];
- comptime expect(@TypeOf(slice) == *[0]u8);
+ comptime try expect(@TypeOf(slice) == *[0]u8);
}
{
var array = [0:0]u8{};
var slice = array[0..0];
- comptime expect(@TypeOf(slice) == *[0:0]u8);
- expect(slice[0] == 0);
+ comptime try expect(@TypeOf(slice) == *[0:0]u8);
+ try expect(slice[0] == 0);
}
}
- fn testArrayAlign() void {
+ fn testArrayAlign() !void {
var array align(4) = [5]u8{ 1, 2, 3, 4, 5 };
var slice = array[4..5];
- comptime expect(@TypeOf(slice) == *align(4) [1]u8);
- expect(slice[0] == 5);
- comptime expect(@TypeOf(array[0..2]) == *align(4) [2]u8);
+ comptime try expect(@TypeOf(slice) == *align(4) [1]u8);
+ try expect(slice[0] == 5);
+ comptime try expect(@TypeOf(array[0..2]) == *align(4) [2]u8);
}
- fn testPointer() void {
+ fn testPointer() !void {
var array = [5]u8{ 1, 2, 3, 4, 5 };
var pointer: [*]u8 = &array;
var slice = pointer[1..3];
- comptime expect(@TypeOf(slice) == *[2]u8);
- expect(slice[0] == 2);
- expect(slice[1] == 3);
+ comptime try expect(@TypeOf(slice) == *[2]u8);
+ try expect(slice[0] == 2);
+ try expect(slice[1] == 3);
}
- fn testPointerZ() void {
+ fn testPointerZ() !void {
var array = [5:0]u8{ 1, 2, 3, 4, 5 };
var pointer: [*:0]u8 = &array;
- comptime expect(@TypeOf(pointer[1..3]) == *[2]u8);
- comptime expect(@TypeOf(pointer[1..3 :4]) == *[2:4]u8);
+ comptime try expect(@TypeOf(pointer[1..3]) == *[2]u8);
+ comptime try expect(@TypeOf(pointer[1..3 :4]) == *[2:4]u8);
}
- fn testPointer0() void {
+ fn testPointer0() !void {
var pointer: [*]const u0 = &[1]u0{0};
var slice = pointer[0..1];
- comptime expect(@TypeOf(slice) == *const [1]u0);
- expect(slice[0] == 0);
+ comptime try expect(@TypeOf(slice) == *const [1]u0);
+ try expect(slice[0] == 0);
}
- fn testPointerAlign() void {
+ fn testPointerAlign() !void {
var array align(4) = [5]u8{ 1, 2, 3, 4, 5 };
var pointer: [*]align(4) u8 = &array;
var slice = pointer[4..5];
- comptime expect(@TypeOf(slice) == *align(4) [1]u8);
- expect(slice[0] == 5);
- comptime expect(@TypeOf(pointer[0..2]) == *align(4) [2]u8);
+ comptime try expect(@TypeOf(slice) == *align(4) [1]u8);
+ try expect(slice[0] == 5);
+ comptime try expect(@TypeOf(pointer[0..2]) == *align(4) [2]u8);
}
- fn testSlice() void {
+ fn testSlice() !void {
var array = [5]u8{ 1, 2, 3, 4, 5 };
var src_slice: []u8 = &array;
var slice = src_slice[1..3];
- comptime expect(@TypeOf(slice) == *[2]u8);
- expect(slice[0] == 2);
- expect(slice[1] == 3);
+ comptime try expect(@TypeOf(slice) == *[2]u8);
+ try expect(slice[0] == 2);
+ try expect(slice[1] == 3);
}
- fn testSliceZ() void {
+ fn testSliceZ() !void {
var array = [5:0]u8{ 1, 2, 3, 4, 5 };
var slice: [:0]u8 = &array;
- comptime expect(@TypeOf(slice[1..3]) == *[2]u8);
- comptime expect(@TypeOf(slice[1..]) == [:0]u8);
- comptime expect(@TypeOf(slice[1..3 :4]) == *[2:4]u8);
+ comptime try expect(@TypeOf(slice[1..3]) == *[2]u8);
+ comptime try expect(@TypeOf(slice[1..]) == [:0]u8);
+ comptime try expect(@TypeOf(slice[1..3 :4]) == *[2:4]u8);
}
- fn testSliceOpt() void {
+ fn testSliceOpt() !void {
var array: [2]u8 = [2]u8{ 1, 2 };
var slice: ?[]u8 = &array;
- comptime expect(@TypeOf(&array, slice) == ?[]u8);
- comptime expect(@TypeOf(slice.?[0..2]) == *[2]u8);
+ comptime try expect(@TypeOf(&array, slice) == ?[]u8);
+ comptime try expect(@TypeOf(slice.?[0..2]) == *[2]u8);
}
- fn testSlice0() void {
+ fn testSlice0() !void {
{
var array = [0]u8{};
var src_slice: []u8 = &array;
var slice = src_slice[0..0];
- comptime expect(@TypeOf(slice) == *[0]u8);
+ comptime try expect(@TypeOf(slice) == *[0]u8);
}
{
var array = [0:0]u8{};
var src_slice: [:0]u8 = &array;
var slice = src_slice[0..0];
- comptime expect(@TypeOf(slice) == *[0]u8);
+ comptime try expect(@TypeOf(slice) == *[0]u8);
}
}
- fn testSliceAlign() void {
+ fn testSliceAlign() !void {
var array align(4) = [5]u8{ 1, 2, 3, 4, 5 };
var src_slice: []align(4) u8 = &array;
var slice = src_slice[4..5];
- comptime expect(@TypeOf(slice) == *align(4) [1]u8);
- expect(slice[0] == 5);
- comptime expect(@TypeOf(src_slice[0..2]) == *align(4) [2]u8);
+ comptime try expect(@TypeOf(slice) == *align(4) [1]u8);
+ try expect(slice[0] == 5);
+ comptime try expect(@TypeOf(src_slice[0..2]) == *align(4) [2]u8);
}
- fn testConcatStrLiterals() void {
- expectEqualSlices("a"[0..] ++ "b"[0..], "ab");
- expectEqualSlices("a"[0..:0] ++ "b"[0..:0], "ab");
+ fn testConcatStrLiterals() !void {
+ try expectEqualSlices("a"[0..] ++ "b"[0..], "ab");
+ try expectEqualSlices("a"[0.. :0] ++ "b"[0.. :0], "ab");
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "slice of hardcoded address to pointer" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
const pointer = @intToPtr([*]u8, 0x04)[0..2];
- comptime expect(@TypeOf(pointer) == *[2]u8);
+ comptime try expect(@TypeOf(pointer) == *[2]u8);
const slice: []const u8 = pointer;
- expect(@ptrToInt(slice.ptr) == 4);
- expect(slice.len == 2);
+ try expect(@ptrToInt(slice.ptr) == 4);
+ try expect(slice.len == 2);
}
};
- S.doTheTest();
+ try S.doTheTest();
}
test "type coercion of pointer to anon struct literal to pointer to slice" {
const S = struct {
- const U = union{
+ const U = union {
a: u32,
b: bool,
c: []const u8,
};
- fn doTheTest() void {
+ fn doTheTest() !void {
var x1: u8 = 42;
const t1 = &.{ x1, 56, 54 };
var slice1: []const u8 = t1;
- expect(slice1.len == 3);
- expect(slice1[0] == 42);
- expect(slice1[1] == 56);
- expect(slice1[2] == 54);
-
+ try expect(slice1.len == 3);
+ try expect(slice1[0] == 42);
+ try expect(slice1[1] == 56);
+ try expect(slice1[2] == 54);
+
var x2: []const u8 = "hello";
const t2 = &.{ x2, ", ", "world!" };
// @compileLog(@TypeOf(t2));
var slice2: []const []const u8 = t2;
- expect(slice2.len == 3);
- expect(mem.eql(u8, slice2[0], "hello"));
- expect(mem.eql(u8, slice2[1], ", "));
- expect(mem.eql(u8, slice2[2], "world!"));
+ try expect(slice2.len == 3);
+ try expect(mem.eql(u8, slice2[0], "hello"));
+ try expect(mem.eql(u8, slice2[1], ", "));
+ try expect(mem.eql(u8, slice2[2], "world!"));
}
};
- // S.doTheTest();
- comptime S.doTheTest();
+ // try S.doTheTest();
+ comptime try S.doTheTest();
}
test/stage1/behavior/src.zig
@@ -2,16 +2,16 @@ const std = @import("std");
const expect = std.testing.expect;
test "@src" {
- doTheTest();
+ try doTheTest();
}
-fn doTheTest() void {
+fn doTheTest() !void {
const src = @src();
- expect(src.line == 9);
- expect(src.column == 17);
- expect(std.mem.endsWith(u8, src.fn_name, "doTheTest"));
- expect(std.mem.endsWith(u8, src.file, "src.zig"));
- expect(src.fn_name[src.fn_name.len] == 0);
- expect(src.file[src.file.len] == 0);
+ try expect(src.line == 9);
+ try expect(src.column == 17);
+ try expect(std.mem.endsWith(u8, src.fn_name, "doTheTest"));
+ try expect(std.mem.endsWith(u8, src.file, "src.zig"));
+ try expect(src.fn_name[src.fn_name.len] == 0);
+ try expect(src.file[src.file.len] == 0);
}
test/stage1/behavior/struct.zig
@@ -18,12 +18,12 @@ test "top level fields" {
.top_level_field = 1234,
};
instance.top_level_field += 1;
- expectEqual(@as(i32, 1235), instance.top_level_field);
+ try expectEqual(@as(i32, 1235), instance.top_level_field);
}
test "call struct static method" {
const result = StructWithNoFields.add(3, 4);
- expect(result == 7);
+ try expect(result == 7);
}
test "return empty struct instance" {
@@ -36,7 +36,7 @@ fn returnEmptyStructInstance() StructWithNoFields {
const should_be_11 = StructWithNoFields.add(5, 6);
test "invoke static method in global scope" {
- expect(should_be_11 == 11);
+ try expect(should_be_11 == 11);
}
test "void struct fields" {
@@ -45,8 +45,8 @@ test "void struct fields" {
.b = 1,
.c = void{},
};
- expect(foo.b == 1);
- expect(@sizeOf(VoidStructFieldsFoo) == 4);
+ try expect(foo.b == 1);
+ try expect(@sizeOf(VoidStructFieldsFoo) == 4);
}
const VoidStructFieldsFoo = struct {
a: void,
@@ -59,17 +59,17 @@ test "structs" {
@memset(@ptrCast([*]u8, &foo), 0, @sizeOf(StructFoo));
foo.a += 1;
foo.b = foo.a == 1;
- testFoo(foo);
+ try testFoo(foo);
testMutation(&foo);
- expect(foo.c == 100);
+ try expect(foo.c == 100);
}
const StructFoo = struct {
a: i32,
b: bool,
c: f32,
};
-fn testFoo(foo: StructFoo) void {
- expect(foo.b);
+fn testFoo(foo: StructFoo) !void {
+ try expect(foo.b);
}
fn testMutation(foo: *StructFoo) void {
foo.c = 100;
@@ -94,7 +94,7 @@ test "struct point to self" {
root.next = &node;
- expect(node.next.next.next.val.x == 1);
+ try expect(node.next.next.next.val.x == 1);
}
test "struct byval assign" {
@@ -103,14 +103,14 @@ test "struct byval assign" {
foo1.a = 1234;
foo2.a = 0;
- expect(foo2.a == 0);
+ try expect(foo2.a == 0);
foo2 = foo1;
- expect(foo2.a == 1234);
+ try expect(foo2.a == 1234);
}
fn structInitializer() void {
const val = Val{ .x = 42 };
- expect(val.x == 42);
+ try expect(val.x == 42);
}
test "fn call of struct field" {
@@ -127,14 +127,14 @@ test "fn call of struct field" {
}
};
- expect(S.callStructField(Foo{ .ptr = S.aFunc }) == 13);
+ try expect(S.callStructField(Foo{ .ptr = S.aFunc }) == 13);
}
test "store member function in variable" {
const instance = MemberFnTestFoo{ .x = 1234 };
const memberFn = MemberFnTestFoo.member;
const result = memberFn(instance);
- expect(result == 1234);
+ try expect(result == 1234);
}
const MemberFnTestFoo = struct {
x: i32,
@@ -146,12 +146,12 @@ const MemberFnTestFoo = struct {
test "call member function directly" {
const instance = MemberFnTestFoo{ .x = 1234 };
const result = MemberFnTestFoo.member(instance);
- expect(result == 1234);
+ try expect(result == 1234);
}
test "member functions" {
const r = MemberFnRand{ .seed = 1234 };
- expect(r.getSeed() == 1234);
+ try expect(r.getSeed() == 1234);
}
const MemberFnRand = struct {
seed: u32,
@@ -162,7 +162,7 @@ const MemberFnRand = struct {
test "return struct byval from function" {
const bar = makeBar(1234, 5678);
- expect(bar.y == 5678);
+ try expect(bar.y == 5678);
}
const Bar = struct {
x: i32,
@@ -177,7 +177,7 @@ fn makeBar(x: i32, y: i32) Bar {
test "empty struct method call" {
const es = EmptyStruct{};
- expect(es.method() == 1234);
+ try expect(es.method() == 1234);
}
const EmptyStruct = struct {
fn method(es: *const EmptyStruct) i32 {
@@ -194,7 +194,7 @@ fn testReturnEmptyStructFromFn() EmptyStruct2 {
}
test "pass slice of empty struct to fn" {
- expect(testPassSliceOfEmptyStructToFn(&[_]EmptyStruct2{EmptyStruct2{}}) == 1);
+ try expect(testPassSliceOfEmptyStructToFn(&[_]EmptyStruct2{EmptyStruct2{}}) == 1);
}
fn testPassSliceOfEmptyStructToFn(slice: []const EmptyStruct2) usize {
return slice.len;
@@ -212,7 +212,7 @@ test "packed struct" {
};
foo.y += 1;
const four = foo.x + foo.y;
- expect(four == 4);
+ try expect(four == 4);
}
const BitField1 = packed struct {
@@ -229,17 +229,17 @@ const bit_field_1 = BitField1{
test "bit field access" {
var data = bit_field_1;
- expect(getA(&data) == 1);
- expect(getB(&data) == 2);
- expect(getC(&data) == 3);
- comptime expect(@sizeOf(BitField1) == 1);
+ try expect(getA(&data) == 1);
+ try expect(getB(&data) == 2);
+ try expect(getC(&data) == 3);
+ comptime try expect(@sizeOf(BitField1) == 1);
data.b += 1;
- expect(data.b == 3);
+ try expect(data.b == 3);
data.a += 1;
- expect(data.a == 2);
- expect(data.b == 3);
+ try expect(data.a == 2);
+ try expect(data.b == 3);
}
fn getA(data: *const BitField1) u3 {
@@ -266,11 +266,11 @@ const Foo96Bits = packed struct {
test "packed struct 24bits" {
comptime {
- expect(@sizeOf(Foo24Bits) == 4);
+ try expect(@sizeOf(Foo24Bits) == 4);
if (@sizeOf(usize) == 4) {
- expect(@sizeOf(Foo96Bits) == 12);
+ try expect(@sizeOf(Foo96Bits) == 12);
} else {
- expect(@sizeOf(Foo96Bits) == 16);
+ try expect(@sizeOf(Foo96Bits) == 16);
}
}
@@ -281,28 +281,28 @@ test "packed struct 24bits" {
.d = 0,
};
value.a += 1;
- expect(value.a == 1);
- expect(value.b == 0);
- expect(value.c == 0);
- expect(value.d == 0);
+ try expect(value.a == 1);
+ try expect(value.b == 0);
+ try expect(value.c == 0);
+ try expect(value.d == 0);
value.b += 1;
- expect(value.a == 1);
- expect(value.b == 1);
- expect(value.c == 0);
- expect(value.d == 0);
+ try expect(value.a == 1);
+ try expect(value.b == 1);
+ try expect(value.c == 0);
+ try expect(value.d == 0);
value.c += 1;
- expect(value.a == 1);
- expect(value.b == 1);
- expect(value.c == 1);
- expect(value.d == 0);
+ try expect(value.a == 1);
+ try expect(value.b == 1);
+ try expect(value.c == 1);
+ try expect(value.d == 0);
value.d += 1;
- expect(value.a == 1);
- expect(value.b == 1);
- expect(value.c == 1);
- expect(value.d == 1);
+ try expect(value.a == 1);
+ try expect(value.b == 1);
+ try expect(value.c == 1);
+ try expect(value.d == 1);
}
const Foo32Bits = packed struct {
@@ -319,43 +319,43 @@ const FooArray24Bits = packed struct {
// TODO revisit this test when doing https://github.com/ziglang/zig/issues/1512
test "packed array 24bits" {
comptime {
- expect(@sizeOf([9]Foo32Bits) == 9 * 4);
- expect(@sizeOf(FooArray24Bits) == 2 + 2 * 4 + 2);
+ try expect(@sizeOf([9]Foo32Bits) == 9 * 4);
+ try expect(@sizeOf(FooArray24Bits) == 2 + 2 * 4 + 2);
}
var bytes = [_]u8{0} ** (@sizeOf(FooArray24Bits) + 1);
bytes[bytes.len - 1] = 0xaa;
const ptr = &std.mem.bytesAsSlice(FooArray24Bits, bytes[0 .. bytes.len - 1])[0];
- expect(ptr.a == 0);
- expect(ptr.b[0].field == 0);
- expect(ptr.b[1].field == 0);
- expect(ptr.c == 0);
+ try expect(ptr.a == 0);
+ try expect(ptr.b[0].field == 0);
+ try expect(ptr.b[1].field == 0);
+ try expect(ptr.c == 0);
ptr.a = maxInt(u16);
- expect(ptr.a == maxInt(u16));
- expect(ptr.b[0].field == 0);
- expect(ptr.b[1].field == 0);
- expect(ptr.c == 0);
+ try expect(ptr.a == maxInt(u16));
+ try expect(ptr.b[0].field == 0);
+ try expect(ptr.b[1].field == 0);
+ try expect(ptr.c == 0);
ptr.b[0].field = maxInt(u24);
- expect(ptr.a == maxInt(u16));
- expect(ptr.b[0].field == maxInt(u24));
- expect(ptr.b[1].field == 0);
- expect(ptr.c == 0);
+ try expect(ptr.a == maxInt(u16));
+ try expect(ptr.b[0].field == maxInt(u24));
+ try expect(ptr.b[1].field == 0);
+ try expect(ptr.c == 0);
ptr.b[1].field = maxInt(u24);
- expect(ptr.a == maxInt(u16));
- expect(ptr.b[0].field == maxInt(u24));
- expect(ptr.b[1].field == maxInt(u24));
- expect(ptr.c == 0);
+ try expect(ptr.a == maxInt(u16));
+ try expect(ptr.b[0].field == maxInt(u24));
+ try expect(ptr.b[1].field == maxInt(u24));
+ try expect(ptr.c == 0);
ptr.c = maxInt(u16);
- expect(ptr.a == maxInt(u16));
- expect(ptr.b[0].field == maxInt(u24));
- expect(ptr.b[1].field == maxInt(u24));
- expect(ptr.c == maxInt(u16));
+ try expect(ptr.a == maxInt(u16));
+ try expect(ptr.b[0].field == maxInt(u24));
+ try expect(ptr.b[1].field == maxInt(u24));
+ try expect(ptr.c == maxInt(u16));
- expect(bytes[bytes.len - 1] == 0xaa);
+ try expect(bytes[bytes.len - 1] == 0xaa);
}
const FooStructAligned = packed struct {
@@ -369,17 +369,17 @@ const FooArrayOfAligned = packed struct {
test "aligned array of packed struct" {
comptime {
- expect(@sizeOf(FooStructAligned) == 2);
- expect(@sizeOf(FooArrayOfAligned) == 2 * 2);
+ try expect(@sizeOf(FooStructAligned) == 2);
+ try expect(@sizeOf(FooArrayOfAligned) == 2 * 2);
}
var bytes = [_]u8{0xbb} ** @sizeOf(FooArrayOfAligned);
const ptr = &std.mem.bytesAsSlice(FooArrayOfAligned, bytes[0..])[0];
- expect(ptr.a[0].a == 0xbb);
- expect(ptr.a[0].b == 0xbb);
- expect(ptr.a[1].a == 0xbb);
- expect(ptr.a[1].b == 0xbb);
+ try expect(ptr.a[0].a == 0xbb);
+ try expect(ptr.a[0].b == 0xbb);
+ try expect(ptr.a[1].a == 0xbb);
+ try expect(ptr.a[1].b == 0xbb);
}
test "runtime struct initialization of bitfield" {
@@ -392,10 +392,10 @@ test "runtime struct initialization of bitfield" {
.y = @intCast(u4, x2),
};
- expect(s1.x == x1);
- expect(s1.y == x1);
- expect(s2.x == @intCast(u4, x2));
- expect(s2.y == @intCast(u4, x2));
+ try expect(s1.x == x1);
+ try expect(s1.y == x1);
+ try expect(s2.x == @intCast(u4, x2));
+ try expect(s2.y == @intCast(u4, x2));
}
var x1 = @as(u4, 1);
@@ -425,18 +425,18 @@ test "native bit field understands endianness" {
@memcpy(&bytes, @ptrCast([*]u8, &all), 8);
var bitfields = @ptrCast(*Bitfields, &bytes).*;
- expect(bitfields.f1 == 0x1111);
- expect(bitfields.f2 == 0x2222);
- expect(bitfields.f3 == 0x33);
- expect(bitfields.f4 == 0x44);
- expect(bitfields.f5 == 0x5);
- expect(bitfields.f6 == 0x6);
- expect(bitfields.f7 == 0x77);
+ try expect(bitfields.f1 == 0x1111);
+ try expect(bitfields.f2 == 0x2222);
+ try expect(bitfields.f3 == 0x33);
+ try expect(bitfields.f4 == 0x44);
+ try expect(bitfields.f5 == 0x5);
+ try expect(bitfields.f6 == 0x6);
+ try expect(bitfields.f7 == 0x77);
}
test "align 1 field before self referential align 8 field as slice return type" {
const result = alloc(Expr);
- expect(result.len == 0);
+ try expect(result.len == 0);
}
const Expr = union(enum) {
@@ -459,10 +459,10 @@ test "call method with mutable reference to struct with no fields" {
};
var s = S{};
- expect(S.doC(&s));
- expect(s.doC());
- expect(S.do(&s));
- expect(s.do());
+ try expect(S.doC(&s));
+ try expect(s.doC());
+ try expect(S.do(&s));
+ try expect(s.do());
}
test "implicit cast packed struct field to const ptr" {
@@ -478,7 +478,7 @@ test "implicit cast packed struct field to const ptr" {
var lup: LevelUpMove = undefined;
lup.level = 12;
const res = LevelUpMove.toInt(lup.level);
- expect(res == 12);
+ try expect(res == 12);
}
test "pointer to packed struct member in a stack variable" {
@@ -489,9 +489,9 @@ test "pointer to packed struct member in a stack variable" {
var s = S{ .a = 2, .b = 0 };
var b_ptr = &s.b;
- expect(s.b == 0);
+ try expect(s.b == 0);
b_ptr.* = 2;
- expect(s.b == 2);
+ try expect(s.b == 2);
}
test "non-byte-aligned array inside packed struct" {
@@ -500,20 +500,20 @@ test "non-byte-aligned array inside packed struct" {
b: [0x16]u8,
};
const S = struct {
- fn bar(slice: []const u8) void {
- expectEqualSlices(u8, slice, "abcdefghijklmnopqurstu");
+ fn bar(slice: []const u8) !void {
+ try expectEqualSlices(u8, slice, "abcdefghijklmnopqurstu");
}
- fn doTheTest() void {
+ fn doTheTest() !void {
var foo = Foo{
.a = true,
.b = "abcdefghijklmnopqurstu".*,
};
const value = foo.b;
- bar(&value);
+ try bar(&value);
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "packed struct with u0 field access" {
@@ -521,7 +521,7 @@ test "packed struct with u0 field access" {
f0: u0,
};
var s = S{ .f0 = 0 };
- comptime expect(s.f0 == 0);
+ comptime try expect(s.f0 == 0);
}
const S0 = struct {
@@ -540,7 +540,7 @@ var g_foo: S0 = S0.init();
test "access to global struct fields" {
g_foo.bar.value = 42;
- expect(g_foo.bar.value == 42);
+ try expect(g_foo.bar.value == 42);
}
test "packed struct with fp fields" {
@@ -559,9 +559,9 @@ test "packed struct with fp fields" {
s.data[1] = 2.0;
s.data[2] = 3.0;
s.frob();
- expectEqual(@as(f32, 6.0), s.data[0]);
- expectEqual(@as(f32, 11.0), s.data[1]);
- expectEqual(@as(f32, 20.0), s.data[2]);
+ try expectEqual(@as(f32, 6.0), s.data[0]);
+ try expectEqual(@as(f32, 11.0), s.data[1]);
+ try expectEqual(@as(f32, 20.0), s.data[2]);
}
test "use within struct scope" {
@@ -572,7 +572,7 @@ test "use within struct scope" {
}
};
};
- expectEqual(@as(i32, 42), S.inner());
+ try expectEqual(@as(i32, 42), S.inner());
}
test "default struct initialization fields" {
@@ -590,14 +590,14 @@ test "default struct initialization fields" {
const y = S{
.b = five,
};
- expectEqual(1239, x.a + x.b);
+ try expectEqual(1239, x.a + x.b);
}
test "fn with C calling convention returns struct by value" {
const S = struct {
- fn entry() void {
+ fn entry() !void {
var x = makeBar(10);
- expectEqual(@as(i32, 10), x.handle);
+ try expectEqual(@as(i32, 10), x.handle);
}
const ExternBar = extern struct {
@@ -610,8 +610,8 @@ test "fn with C calling convention returns struct by value" {
};
}
};
- S.entry();
- comptime S.entry();
+ try S.entry();
+ comptime try S.entry();
}
test "for loop over pointers to struct, getting field from struct pointer" {
@@ -632,7 +632,7 @@ test "for loop over pointers to struct, getting field from struct pointer" {
}
};
- fn doTheTest() void {
+ fn doTheTest() !void {
var objects: ArrayList = undefined;
for (objects.toSlice()) |obj| {
@@ -641,10 +641,10 @@ test "for loop over pointers to struct, getting field from struct pointer" {
}
}
- expect(ok);
+ try expect(ok);
}
};
- S.doTheTest();
+ try S.doTheTest();
}
test "zero-bit field in packed struct" {
@@ -657,20 +657,20 @@ test "zero-bit field in packed struct" {
test "struct field init with catch" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var x: anyerror!isize = 1;
var req = Foo{
.field = x catch undefined,
};
- expect(req.field == 1);
+ try expect(req.field == 1);
}
pub const Foo = extern struct {
field: isize,
};
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "packed struct with non-ABI-aligned field" {
@@ -681,8 +681,8 @@ test "packed struct with non-ABI-aligned field" {
var s: S = undefined;
s.x = 1;
s.y = 42;
- expect(s.x == 1);
- expect(s.y == 42);
+ try expect(s.x == 1);
+ try expect(s.y == 42);
}
test "non-packed struct with u128 entry in union" {
@@ -698,10 +698,10 @@ test "non-packed struct with u128 entry in union" {
var sx: S = undefined;
var s = &sx;
- std.testing.expect(@ptrToInt(&s.f2) - @ptrToInt(&s.f1) == @byteOffsetOf(S, "f2"));
+ try std.testing.expect(@ptrToInt(&s.f2) - @ptrToInt(&s.f1) == @byteOffsetOf(S, "f2"));
var v2 = U{ .Num = 123 };
s.f2 = v2;
- std.testing.expect(s.f2.Num == 123);
+ try std.testing.expect(s.f2.Num == 123);
}
test "packed struct field passed to generic function" {
@@ -721,7 +721,7 @@ test "packed struct field passed to generic function" {
var p: S.P = undefined;
p.b = 29;
var loaded = S.genericReadPackedField(&p.b);
- expect(loaded == 29);
+ try expect(loaded == 29);
}
test "anonymous struct literal syntax" {
@@ -731,63 +731,63 @@ test "anonymous struct literal syntax" {
y: i32,
};
- fn doTheTest() void {
+ fn doTheTest() !void {
var p: Point = .{
.x = 1,
.y = 2,
};
- expect(p.x == 1);
- expect(p.y == 2);
+ try expect(p.x == 1);
+ try expect(p.y == 2);
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "fully anonymous struct" {
const S = struct {
- fn doTheTest() void {
- dump(.{
+ fn doTheTest() !void {
+ try dump(.{
.int = @as(u32, 1234),
.float = @as(f64, 12.34),
.b = true,
.s = "hi",
});
}
- fn dump(args: anytype) void {
- expect(args.int == 1234);
- expect(args.float == 12.34);
- expect(args.b);
- expect(args.s[0] == 'h');
- expect(args.s[1] == 'i');
+ fn dump(args: anytype) !void {
+ try expect(args.int == 1234);
+ try expect(args.float == 12.34);
+ try expect(args.b);
+ try expect(args.s[0] == 'h');
+ try expect(args.s[1] == 'i');
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "fully anonymous list literal" {
const S = struct {
- fn doTheTest() void {
- dump(.{ @as(u32, 1234), @as(f64, 12.34), true, "hi" });
+ fn doTheTest() !void {
+ try dump(.{ @as(u32, 1234), @as(f64, 12.34), true, "hi" });
}
- fn dump(args: anytype) void {
- expect(args.@"0" == 1234);
- expect(args.@"1" == 12.34);
- expect(args.@"2");
- expect(args.@"3"[0] == 'h');
- expect(args.@"3"[1] == 'i');
+ fn dump(args: anytype) !void {
+ try expect(args.@"0" == 1234);
+ try expect(args.@"1" == 12.34);
+ try expect(args.@"2");
+ try expect(args.@"3"[0] == 'h');
+ try expect(args.@"3"[1] == 'i');
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "anonymous struct literal assigned to variable" {
var vec = .{ @as(i32, 22), @as(i32, 55), @as(i32, 99) };
- expect(vec.@"0" == 22);
- expect(vec.@"1" == 55);
- expect(vec.@"2" == 99);
+ try expect(vec.@"0" == 22);
+ try expect(vec.@"1" == 55);
+ try expect(vec.@"2" == 99);
}
test "struct with var field" {
@@ -799,8 +799,8 @@ test "struct with var field" {
.x = 1,
.y = 2,
};
- expect(pt.x == 1);
- expect(pt.y == 2);
+ try expect(pt.x == 1);
+ try expect(pt.y == 2);
}
test "comptime struct field" {
@@ -810,21 +810,21 @@ test "comptime struct field" {
};
var foo: T = undefined;
- comptime expect(foo.b == 1234);
+ comptime try expect(foo.b == 1234);
}
test "anon struct literal field value initialized with fn call" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var x = .{foo()};
- expectEqualSlices(u8, x[0], "hi");
+ try expectEqualSlices(u8, x[0], "hi");
}
fn foo() []const u8 {
return "hi";
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "self-referencing struct via array member" {
@@ -833,7 +833,7 @@ test "self-referencing struct via array member" {
};
var x: T = undefined;
x = T{ .children = .{&x} };
- expect(x.children[0] == &x);
+ try expect(x.children[0] == &x);
}
test "struct with union field" {
@@ -848,8 +848,8 @@ test "struct with union field" {
var True = Value{
.kind = .{ .Bool = true },
};
- expectEqual(@as(u32, 2), True.ref);
- expectEqual(true, True.kind.Bool);
+ try expectEqual(@as(u32, 2), True.ref);
+ try expectEqual(true, True.kind.Bool);
}
test "type coercion of anon struct literal to struct" {
@@ -865,24 +865,24 @@ test "type coercion of anon struct literal to struct" {
field: i32 = 1234,
};
- fn doTheTest() void {
+ fn doTheTest() !void {
var y: u32 = 42;
const t0 = .{ .A = 123, .B = "foo", .C = {} };
const t1 = .{ .A = y, .B = "foo", .C = {} };
const y0: S2 = t0;
var y1: S2 = t1;
- expect(y0.A == 123);
- expect(std.mem.eql(u8, y0.B, "foo"));
- expect(y0.C == {});
- expect(y0.D.field == 1234);
- expect(y1.A == y);
- expect(std.mem.eql(u8, y1.B, "foo"));
- expect(y1.C == {});
- expect(y1.D.field == 1234);
+ try expect(y0.A == 123);
+ try expect(std.mem.eql(u8, y0.B, "foo"));
+ try expect(y0.C == {});
+ try expect(y0.D.field == 1234);
+ try expect(y1.A == y);
+ try expect(std.mem.eql(u8, y1.B, "foo"));
+ try expect(y1.C == {});
+ try expect(y1.D.field == 1234);
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "type coercion of pointer to anon struct literal to pointer to struct" {
@@ -898,24 +898,24 @@ test "type coercion of pointer to anon struct literal to pointer to struct" {
field: i32 = 1234,
};
- fn doTheTest() void {
+ fn doTheTest() !void {
var y: u32 = 42;
const t0 = &.{ .A = 123, .B = "foo", .C = {} };
const t1 = &.{ .A = y, .B = "foo", .C = {} };
const y0: *const S2 = t0;
var y1: *const S2 = t1;
- expect(y0.A == 123);
- expect(std.mem.eql(u8, y0.B, "foo"));
- expect(y0.C == {});
- expect(y0.D.field == 1234);
- expect(y1.A == y);
- expect(std.mem.eql(u8, y1.B, "foo"));
- expect(y1.C == {});
- expect(y1.D.field == 1234);
+ try expect(y0.A == 123);
+ try expect(std.mem.eql(u8, y0.B, "foo"));
+ try expect(y0.C == {});
+ try expect(y0.D.field == 1234);
+ try expect(y1.A == y);
+ try expect(std.mem.eql(u8, y1.B, "foo"));
+ try expect(y1.C == {});
+ try expect(y1.D.field == 1234);
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "packed struct with undefined initializers" {
@@ -929,16 +929,16 @@ test "packed struct with undefined initializers" {
_c: u3 = undefined,
};
- fn doTheTest() void {
+ fn doTheTest() !void {
var p: P = undefined;
p = P{ .a = 2, .b = 4, .c = 6 };
// Make sure the compiler doesn't touch the unprefixed fields.
- expectEqual(@as(u3, 2), p.a);
- expectEqual(@as(u3, 4), p.b);
- expectEqual(@as(u3, 6), p.c);
+ try expectEqual(@as(u3, 2), p.a);
+ try expectEqual(@as(u3, 4), p.b);
+ try expectEqual(@as(u3, 6), p.c);
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test/stage1/behavior/struct_contains_null_ptr_itself.zig
@@ -3,7 +3,7 @@ const expect = std.testing.expect;
test "struct contains null pointer which contains original struct" {
var x: ?*NodeLineComment = null;
- expect(x == null);
+ try expect(x == null);
}
pub const Node = struct {
test/stage1/behavior/struct_contains_slice_of_itself.zig
@@ -39,12 +39,12 @@ test "struct contains slice of itself" {
.payload = 1234,
.children = nodes[0..],
};
- expect(root.payload == 1234);
- expect(root.children[0].payload == 1);
- expect(root.children[1].payload == 2);
- expect(root.children[2].payload == 3);
- expect(root.children[2].children[0].payload == 31);
- expect(root.children[2].children[1].payload == 32);
+ try expect(root.payload == 1234);
+ try expect(root.children[0].payload == 1);
+ try expect(root.children[1].payload == 2);
+ try expect(root.children[2].payload == 3);
+ try expect(root.children[2].children[0].payload == 31);
+ try expect(root.children[2].children[1].payload == 32);
}
test "struct contains aligned slice of itself" {
@@ -76,10 +76,10 @@ test "struct contains aligned slice of itself" {
.payload = 1234,
.children = nodes[0..],
};
- expect(root.payload == 1234);
- expect(root.children[0].payload == 1);
- expect(root.children[1].payload == 2);
- expect(root.children[2].payload == 3);
- expect(root.children[2].children[0].payload == 31);
- expect(root.children[2].children[1].payload == 32);
+ try expect(root.payload == 1234);
+ try expect(root.children[0].payload == 1);
+ try expect(root.children[1].payload == 2);
+ try expect(root.children[2].payload == 3);
+ try expect(root.children[2].children[0].payload == 31);
+ try expect(root.children[2].children[1].payload == 32);
}
test/stage1/behavior/switch.zig
@@ -4,23 +4,23 @@ const expectError = std.testing.expectError;
const expectEqual = std.testing.expectEqual;
test "switch with numbers" {
- testSwitchWithNumbers(13);
+ try testSwitchWithNumbers(13);
}
-fn testSwitchWithNumbers(x: u32) void {
+fn testSwitchWithNumbers(x: u32) !void {
const result = switch (x) {
1, 2, 3, 4...8 => false,
13 => true,
else => false,
};
- expect(result);
+ try expect(result);
}
test "switch with all ranges" {
- expect(testSwitchWithAllRanges(50, 3) == 1);
- expect(testSwitchWithAllRanges(101, 0) == 2);
- expect(testSwitchWithAllRanges(300, 5) == 3);
- expect(testSwitchWithAllRanges(301, 6) == 6);
+ try expect(testSwitchWithAllRanges(50, 3) == 1);
+ try expect(testSwitchWithAllRanges(101, 0) == 2);
+ try expect(testSwitchWithAllRanges(300, 5) == 3);
+ try expect(testSwitchWithAllRanges(301, 6) == 6);
}
fn testSwitchWithAllRanges(x: u32, y: u32) u32 {
@@ -43,7 +43,7 @@ test "implicit comptime switch" {
};
comptime {
- expect(result + 1 == 14);
+ try expect(result + 1 == 14);
}
}
@@ -65,16 +65,16 @@ fn nonConstSwitchOnEnum(fruit: Fruit) void {
}
test "switch statement" {
- nonConstSwitch(SwitchStatmentFoo.C);
+ try nonConstSwitch(SwitchStatmentFoo.C);
}
-fn nonConstSwitch(foo: SwitchStatmentFoo) void {
+fn nonConstSwitch(foo: SwitchStatmentFoo) !void {
const val = switch (foo) {
SwitchStatmentFoo.A => @as(i32, 1),
SwitchStatmentFoo.B => 2,
SwitchStatmentFoo.C => 3,
SwitchStatmentFoo.D => 4,
};
- expect(val == 3);
+ try expect(val == 3);
}
const SwitchStatmentFoo = enum {
A,
@@ -84,22 +84,22 @@ const SwitchStatmentFoo = enum {
};
test "switch prong with variable" {
- switchProngWithVarFn(SwitchProngWithVarEnum{ .One = 13 });
- switchProngWithVarFn(SwitchProngWithVarEnum{ .Two = 13.0 });
- switchProngWithVarFn(SwitchProngWithVarEnum{ .Meh = {} });
+ try switchProngWithVarFn(SwitchProngWithVarEnum{ .One = 13 });
+ try switchProngWithVarFn(SwitchProngWithVarEnum{ .Two = 13.0 });
+ try switchProngWithVarFn(SwitchProngWithVarEnum{ .Meh = {} });
}
const SwitchProngWithVarEnum = union(enum) {
One: i32,
Two: f32,
Meh: void,
};
-fn switchProngWithVarFn(a: SwitchProngWithVarEnum) void {
+fn switchProngWithVarFn(a: SwitchProngWithVarEnum) !void {
switch (a) {
SwitchProngWithVarEnum.One => |x| {
- expect(x == 13);
+ try expect(x == 13);
},
SwitchProngWithVarEnum.Two => |x| {
- expect(x == 13.0);
+ try expect(x == 13.0);
},
SwitchProngWithVarEnum.Meh => |x| {
const v: void = x;
@@ -108,18 +108,18 @@ fn switchProngWithVarFn(a: SwitchProngWithVarEnum) void {
}
test "switch on enum using pointer capture" {
- testSwitchEnumPtrCapture();
- comptime testSwitchEnumPtrCapture();
+ try testSwitchEnumPtrCapture();
+ comptime try testSwitchEnumPtrCapture();
}
-fn testSwitchEnumPtrCapture() void {
+fn testSwitchEnumPtrCapture() !void {
var value = SwitchProngWithVarEnum{ .One = 1234 };
switch (value) {
SwitchProngWithVarEnum.One => |*x| x.* += 1,
else => unreachable,
}
switch (value) {
- SwitchProngWithVarEnum.One => |x| expect(x == 1235),
+ SwitchProngWithVarEnum.One => |x| try expect(x == 1235),
else => unreachable,
}
}
@@ -130,7 +130,7 @@ test "switch with multiple expressions" {
4, 5, 6 => 2,
else => @as(i32, 3),
};
- expect(x == 2);
+ try expect(x == 2);
}
fn returnsFive() i32 {
return 5;
@@ -152,12 +152,12 @@ fn returnsFalse() bool {
}
}
test "switch on const enum with var" {
- expect(!returnsFalse());
+ try expect(!returnsFalse());
}
test "switch on type" {
- expect(trueIfBoolFalseOtherwise(bool));
- expect(!trueIfBoolFalseOtherwise(i32));
+ try expect(trueIfBoolFalseOtherwise(bool));
+ try expect(!trueIfBoolFalseOtherwise(i32));
}
fn trueIfBoolFalseOtherwise(comptime T: type) bool {
@@ -168,21 +168,21 @@ fn trueIfBoolFalseOtherwise(comptime T: type) bool {
}
test "switch handles all cases of number" {
- testSwitchHandleAllCases();
- comptime testSwitchHandleAllCases();
+ try testSwitchHandleAllCases();
+ comptime try testSwitchHandleAllCases();
}
-fn testSwitchHandleAllCases() void {
- expect(testSwitchHandleAllCasesExhaustive(0) == 3);
- expect(testSwitchHandleAllCasesExhaustive(1) == 2);
- expect(testSwitchHandleAllCasesExhaustive(2) == 1);
- expect(testSwitchHandleAllCasesExhaustive(3) == 0);
+fn testSwitchHandleAllCases() !void {
+ try expect(testSwitchHandleAllCasesExhaustive(0) == 3);
+ try expect(testSwitchHandleAllCasesExhaustive(1) == 2);
+ try expect(testSwitchHandleAllCasesExhaustive(2) == 1);
+ try expect(testSwitchHandleAllCasesExhaustive(3) == 0);
- expect(testSwitchHandleAllCasesRange(100) == 0);
- expect(testSwitchHandleAllCasesRange(200) == 1);
- expect(testSwitchHandleAllCasesRange(201) == 2);
- expect(testSwitchHandleAllCasesRange(202) == 4);
- expect(testSwitchHandleAllCasesRange(230) == 3);
+ try expect(testSwitchHandleAllCasesRange(100) == 0);
+ try expect(testSwitchHandleAllCasesRange(200) == 1);
+ try expect(testSwitchHandleAllCasesRange(201) == 2);
+ try expect(testSwitchHandleAllCasesRange(202) == 4);
+ try expect(testSwitchHandleAllCasesRange(230) == 3);
}
fn testSwitchHandleAllCasesExhaustive(x: u2) u2 {
@@ -205,13 +205,13 @@ fn testSwitchHandleAllCasesRange(x: u8) u8 {
}
test "switch all prongs unreachable" {
- testAllProngsUnreachable();
- comptime testAllProngsUnreachable();
+ try testAllProngsUnreachable();
+ comptime try testAllProngsUnreachable();
}
-fn testAllProngsUnreachable() void {
- expect(switchWithUnreachable(1) == 2);
- expect(switchWithUnreachable(2) == 10);
+fn testAllProngsUnreachable() !void {
+ try expect(switchWithUnreachable(1) == 2);
+ try expect(switchWithUnreachable(2) == 10);
}
fn switchWithUnreachable(x: i32) i32 {
@@ -233,23 +233,23 @@ test "capture value of switch with all unreachable prongs" {
const x = return_a_number() catch |err| switch (err) {
else => unreachable,
};
- expect(x == 1);
+ try expect(x == 1);
}
test "switching on booleans" {
- testSwitchOnBools();
- comptime testSwitchOnBools();
+ try testSwitchOnBools();
+ comptime try testSwitchOnBools();
}
-fn testSwitchOnBools() void {
- expect(testSwitchOnBoolsTrueAndFalse(true) == false);
- expect(testSwitchOnBoolsTrueAndFalse(false) == true);
+fn testSwitchOnBools() !void {
+ try expect(testSwitchOnBoolsTrueAndFalse(true) == false);
+ try expect(testSwitchOnBoolsTrueAndFalse(false) == true);
- expect(testSwitchOnBoolsTrueWithElse(true) == false);
- expect(testSwitchOnBoolsTrueWithElse(false) == true);
+ try expect(testSwitchOnBoolsTrueWithElse(true) == false);
+ try expect(testSwitchOnBoolsTrueWithElse(false) == true);
- expect(testSwitchOnBoolsFalseWithElse(true) == false);
- expect(testSwitchOnBoolsFalseWithElse(false) == true);
+ try expect(testSwitchOnBoolsFalseWithElse(true) == false);
+ try expect(testSwitchOnBoolsFalseWithElse(false) == true);
}
fn testSwitchOnBoolsTrueAndFalse(x: bool) bool {
@@ -276,14 +276,14 @@ fn testSwitchOnBoolsFalseWithElse(x: bool) bool {
test "u0" {
var val: u0 = 0;
switch (val) {
- 0 => expect(val == 0),
+ 0 => try expect(val == 0),
}
}
test "undefined.u0" {
var val: u0 = undefined;
switch (val) {
- 0 => expect(val == 0),
+ 0 => try expect(val == 0),
}
}
@@ -295,15 +295,15 @@ test "anon enum literal used in switch on union enum" {
var foo = Foo{ .a = 1234 };
switch (foo) {
.a => |x| {
- expect(x == 1234);
+ try expect(x == 1234);
},
}
}
test "else prong of switch on error set excludes other cases" {
const S = struct {
- fn doTheTest() void {
- expectError(error.C, bar());
+ fn doTheTest() !void {
+ try expectError(error.C, bar());
}
const E = error{
A,
@@ -326,14 +326,14 @@ test "else prong of switch on error set excludes other cases" {
};
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "switch prongs with error set cases make a new error set type for capture value" {
const S = struct {
- fn doTheTest() void {
- expectError(error.B, bar());
+ fn doTheTest() !void {
+ try expectError(error.B, bar());
}
const E = E1 || E2;
@@ -358,14 +358,14 @@ test "switch prongs with error set cases make a new error set type for capture v
};
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "return result loc and then switch with range implicit casted to error union" {
const S = struct {
- fn doTheTest() void {
- expect((func(0xb) catch unreachable) == 0xb);
+ fn doTheTest() !void {
+ try expect((func(0xb) catch unreachable) == 0xb);
}
fn func(d: u8) anyerror!u8 {
return switch (d) {
@@ -374,13 +374,13 @@ test "return result loc and then switch with range implicit casted to error unio
};
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "switch with null and T peer types and inferred result location type" {
const S = struct {
- fn doTheTest(c: u8) void {
+ fn doTheTest(c: u8) !void {
if (switch (c) {
0 => true,
else => null,
@@ -389,8 +389,8 @@ test "switch with null and T peer types and inferred result location type" {
}
}
};
- S.doTheTest(1);
- comptime S.doTheTest(1);
+ try S.doTheTest(1);
+ comptime try S.doTheTest(1);
}
test "switch prongs with cases with identical payload types" {
@@ -400,31 +400,31 @@ test "switch prongs with cases with identical payload types" {
C: usize,
};
const S = struct {
- fn doTheTest() void {
- doTheSwitch1(Union{ .A = 8 });
- doTheSwitch2(Union{ .B = -8 });
+ fn doTheTest() !void {
+ try doTheSwitch1(Union{ .A = 8 });
+ try doTheSwitch2(Union{ .B = -8 });
}
- fn doTheSwitch1(u: Union) void {
+ fn doTheSwitch1(u: Union) !void {
switch (u) {
.A, .C => |e| {
- expect(@TypeOf(e) == usize);
- expect(e == 8);
+ try expect(@TypeOf(e) == usize);
+ try expect(e == 8);
},
.B => |e| @panic("fail"),
}
}
- fn doTheSwitch2(u: Union) void {
+ fn doTheSwitch2(u: Union) !void {
switch (u) {
.A, .C => |e| @panic("fail"),
.B => |e| {
- expect(@TypeOf(e) == isize);
- expect(e == -8);
+ try expect(@TypeOf(e) == isize);
+ try expect(e == -8);
},
}
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "switch with disjoint range" {
@@ -438,19 +438,19 @@ test "switch with disjoint range" {
test "switch variable for range and multiple prongs" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var u: u8 = 16;
- doTheSwitch(u);
- comptime doTheSwitch(u);
+ try doTheSwitch(u);
+ comptime try doTheSwitch(u);
var v: u8 = 42;
- doTheSwitch(v);
- comptime doTheSwitch(v);
+ try doTheSwitch(v);
+ comptime try doTheSwitch(v);
}
- fn doTheSwitch(q: u8) void {
+ fn doTheSwitch(q: u8) !void {
switch (q) {
- 0...40 => |x| expect(x == 16),
- 41, 42, 43 => |x| expect(x == 42),
- else => expect(false),
+ 0...40 => |x| try expect(x == 16),
+ 41, 42, 43 => |x| try expect(x == 42),
+ else => try expect(false),
}
}
};
@@ -493,31 +493,31 @@ test "switch on pointer type" {
}
};
- expect(1 == S.doTheTest(S.P1));
- expect(2 == S.doTheTest(S.P2));
- expect(3 == S.doTheTest(S.P3));
- comptime expect(1 == S.doTheTest(S.P1));
- comptime expect(2 == S.doTheTest(S.P2));
- comptime expect(3 == S.doTheTest(S.P3));
+ try expect(1 == S.doTheTest(S.P1));
+ try expect(2 == S.doTheTest(S.P2));
+ try expect(3 == S.doTheTest(S.P3));
+ comptime try expect(1 == S.doTheTest(S.P1));
+ comptime try expect(2 == S.doTheTest(S.P2));
+ comptime try expect(3 == S.doTheTest(S.P3));
}
test "switch on error set with single else" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var some: error{Foo} = error.Foo;
- expect(switch (some) {
+ try expect(switch (some) {
else => |a| true,
});
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "while copies its payload" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var tmp: union(enum) {
A: u8,
B: u32,
@@ -526,12 +526,12 @@ test "while copies its payload" {
.A => |value| {
// Modify the original union
tmp = .{ .B = 0x10101010 };
- expectEqual(@as(u8, 42), value);
+ try expectEqual(@as(u8, 42), value);
},
else => unreachable,
}
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test/stage1/behavior/switch_prong_err_enum.zig
@@ -22,9 +22,9 @@ fn doThing(form_id: u64) anyerror!FormValue {
test "switch prong returns error enum" {
switch (doThing(17) catch unreachable) {
FormValue.Address => |payload| {
- expect(payload == 1);
+ try expect(payload == 1);
},
else => unreachable,
}
- expect(read_count == 1);
+ try expect(read_count == 1);
}
test/stage1/behavior/switch_prong_implicit_cast.zig
@@ -18,5 +18,5 @@ test "switch prong implicit cast" {
FormValue.One => false,
FormValue.Two => |x| x,
};
- expect(result);
+ try expect(result);
}
test/stage1/behavior/this.zig
@@ -20,7 +20,7 @@ fn add(x: i32, y: i32) i32 {
}
test "this refer to module call private fn" {
- expect(module.add(1, 2) == 3);
+ try expect(module.add(1, 2) == 3);
}
test "this refer to container" {
@@ -29,6 +29,6 @@ test "this refer to container" {
.y = 34,
};
pt.addOne();
- expect(pt.x == 13);
- expect(pt.y == 35);
+ try expect(pt.x == 13);
+ try expect(pt.y == 35);
}
test/stage1/behavior/translate_c_macros.zig
@@ -4,7 +4,7 @@ const expectEqual = @import("std").testing.expectEqual;
const h = @cImport(@cInclude("stage1/behavior/translate_c_macros.h"));
test "initializer list expression" {
- expectEqual(h.Color{
+ try expectEqual(h.Color{
.r = 200,
.g = 200,
.b = 200,
@@ -13,10 +13,10 @@ test "initializer list expression" {
}
test "sizeof in macros" {
- expectEqual(@as(c_int, @sizeOf(u32)), h.MY_SIZEOF(u32));
- expectEqual(@as(c_int, @sizeOf(u32)), h.MY_SIZEOF2(u32));
+ try expectEqual(@as(c_int, @sizeOf(u32)), h.MY_SIZEOF(u32));
+ try expectEqual(@as(c_int, @sizeOf(u32)), h.MY_SIZEOF2(u32));
}
test "reference to a struct type" {
- expectEqual(@sizeOf(h.struct_Foo), h.SIZE_OF_FOO);
+ try expectEqual(@sizeOf(h.struct_Foo), h.SIZE_OF_FOO);
}
test/stage1/behavior/truncate.zig
@@ -4,33 +4,33 @@ const expect = std.testing.expect;
test "truncate u0 to larger integer allowed and has comptime known result" {
var x: u0 = 0;
const y = @truncate(u8, x);
- comptime expect(y == 0);
+ comptime try expect(y == 0);
}
test "truncate.u0.literal" {
var z = @truncate(u0, 0);
- expect(z == 0);
+ try expect(z == 0);
}
test "truncate.u0.const" {
const c0: usize = 0;
var z = @truncate(u0, c0);
- expect(z == 0);
+ try expect(z == 0);
}
test "truncate.u0.var" {
var d: u8 = 2;
var z = @truncate(u0, d);
- expect(z == 0);
+ try expect(z == 0);
}
test "truncate sign mismatch but comptime known so it works anyway" {
const x: u32 = 10;
var result = @truncate(i8, x);
- expect(result == 10);
+ try expect(result == 10);
}
test "truncate on comptime integer" {
var x = @truncate(u16, 9999);
- expect(x == 9999);
+ try expect(x == 9999);
}
test/stage1/behavior/try.zig
@@ -1,17 +1,17 @@
const expect = @import("std").testing.expect;
test "try on error union" {
- tryOnErrorUnionImpl();
- comptime tryOnErrorUnionImpl();
+ try tryOnErrorUnionImpl();
+ comptime try tryOnErrorUnionImpl();
}
-fn tryOnErrorUnionImpl() void {
+fn tryOnErrorUnionImpl() !void {
const x = if (returnsTen()) |val| val + 1 else |err| switch (err) {
error.ItBroke, error.NoMem => 1,
error.CrappedOut => @as(i32, 2),
else => unreachable,
};
- expect(x == 11);
+ try expect(x == 11);
}
fn returnsTen() anyerror!i32 {
@@ -20,10 +20,10 @@ fn returnsTen() anyerror!i32 {
test "try without vars" {
const result1 = if (failIfTrue(true)) 1 else |_| @as(i32, 2);
- expect(result1 == 2);
+ try expect(result1 == 2);
const result2 = if (failIfTrue(false)) 1 else |_| @as(i32, 2);
- expect(result2 == 1);
+ try expect(result2 == 1);
}
fn failIfTrue(ok: bool) anyerror!void {
@@ -38,6 +38,6 @@ test "try then not executed with assignment" {
if (failIfTrue(true)) {
unreachable;
} else |err| {
- expect(err == error.ItBroke);
+ try expect(err == error.ItBroke);
}
}
test/stage1/behavior/tuple.zig
@@ -5,93 +5,93 @@ const expectEqual = testing.expectEqual;
test "tuple concatenation" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var a: i32 = 1;
var b: i32 = 2;
var x = .{a};
var y = .{b};
var c = x ++ y;
- expectEqual(@as(i32, 1), c[0]);
- expectEqual(@as(i32, 2), c[1]);
+ try expectEqual(@as(i32, 1), c[0]);
+ try expectEqual(@as(i32, 2), c[1]);
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "tuple multiplication" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
{
const t = .{} ** 4;
- expectEqual(0, @typeInfo(@TypeOf(t)).Struct.fields.len);
+ try expectEqual(0, @typeInfo(@TypeOf(t)).Struct.fields.len);
}
{
const t = .{'a'} ** 4;
- expectEqual(4, @typeInfo(@TypeOf(t)).Struct.fields.len);
- inline for (t) |x| expectEqual('a', x);
+ try expectEqual(4, @typeInfo(@TypeOf(t)).Struct.fields.len);
+ inline for (t) |x| try expectEqual('a', x);
}
{
const t = .{ 1, 2, 3 } ** 4;
- expectEqual(12, @typeInfo(@TypeOf(t)).Struct.fields.len);
- inline for (t) |x, i| expectEqual(1 + i % 3, x);
+ try expectEqual(12, @typeInfo(@TypeOf(t)).Struct.fields.len);
+ inline for (t) |x, i| try expectEqual(1 + i % 3, x);
}
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
const T = struct {
- fn consume_tuple(tuple: anytype, len: usize) void {
- expect(tuple.len == len);
+ fn consume_tuple(tuple: anytype, len: usize) !void {
+ try expect(tuple.len == len);
}
- fn doTheTest() void {
+ fn doTheTest() !void {
const t1 = .{};
var rt_var: u8 = 42;
const t2 = .{rt_var} ++ .{};
- expect(t2.len == 1);
- expect(t2.@"0" == rt_var);
- expect(t2.@"0" == 42);
- expect(&t2.@"0" != &rt_var);
+ try expect(t2.len == 1);
+ try expect(t2.@"0" == rt_var);
+ try expect(t2.@"0" == 42);
+ try expect(&t2.@"0" != &rt_var);
- consume_tuple(t1 ++ t1, 0);
- consume_tuple(.{} ++ .{}, 0);
- consume_tuple(.{0} ++ .{}, 1);
- consume_tuple(.{0} ++ .{1}, 2);
- consume_tuple(.{ 0, 1, 2 } ++ .{ u8, 1, noreturn }, 6);
- consume_tuple(t2 ++ t1, 1);
- consume_tuple(t1 ++ t2, 1);
- consume_tuple(t2 ++ t2, 2);
- consume_tuple(.{rt_var} ++ .{}, 1);
- consume_tuple(.{rt_var} ++ t1, 1);
- consume_tuple(.{} ++ .{rt_var}, 1);
- consume_tuple(t2 ++ .{void}, 2);
- consume_tuple(t2 ++ .{0}, 2);
- consume_tuple(.{0} ++ t2, 2);
- consume_tuple(.{void} ++ t2, 2);
- consume_tuple(.{u8} ++ .{rt_var} ++ .{true}, 3);
+ try consume_tuple(t1 ++ t1, 0);
+ try consume_tuple(.{} ++ .{}, 0);
+ try consume_tuple(.{0} ++ .{}, 1);
+ try consume_tuple(.{0} ++ .{1}, 2);
+ try consume_tuple(.{ 0, 1, 2 } ++ .{ u8, 1, noreturn }, 6);
+ try consume_tuple(t2 ++ t1, 1);
+ try consume_tuple(t1 ++ t2, 1);
+ try consume_tuple(t2 ++ t2, 2);
+ try consume_tuple(.{rt_var} ++ .{}, 1);
+ try consume_tuple(.{rt_var} ++ t1, 1);
+ try consume_tuple(.{} ++ .{rt_var}, 1);
+ try consume_tuple(t2 ++ .{void}, 2);
+ try consume_tuple(t2 ++ .{0}, 2);
+ try consume_tuple(.{0} ++ t2, 2);
+ try consume_tuple(.{void} ++ t2, 2);
+ try consume_tuple(.{u8} ++ .{rt_var} ++ .{true}, 3);
}
};
- T.doTheTest();
- comptime T.doTheTest();
+ try T.doTheTest();
+ comptime try T.doTheTest();
}
test "pass tuple to comptime var parameter" {
const S = struct {
- fn Foo(comptime args: anytype) void {
- expect(args[0] == 1);
+ fn Foo(comptime args: anytype) !void {
+ try expect(args[0] == 1);
}
- fn doTheTest() void {
- Foo(.{1});
+ fn doTheTest() !void {
+ try Foo(.{1});
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "tuple initializer for var" {
test/stage1/behavior/type.zig
@@ -4,52 +4,52 @@ const TypeInfo = builtin.TypeInfo;
const std = @import("std");
const testing = std.testing;
-fn testTypes(comptime types: []const type) void {
+fn testTypes(comptime types: []const type) !void {
inline for (types) |testType| {
- testing.expect(testType == @Type(@typeInfo(testType)));
+ try testing.expect(testType == @Type(@typeInfo(testType)));
}
}
test "Type.MetaType" {
- testing.expect(type == @Type(TypeInfo{ .Type = undefined }));
- testTypes(&[_]type{type});
+ try testing.expect(type == @Type(TypeInfo{ .Type = undefined }));
+ try testTypes(&[_]type{type});
}
test "Type.Void" {
- testing.expect(void == @Type(TypeInfo{ .Void = undefined }));
- testTypes(&[_]type{void});
+ try testing.expect(void == @Type(TypeInfo{ .Void = undefined }));
+ try testTypes(&[_]type{void});
}
test "Type.Bool" {
- testing.expect(bool == @Type(TypeInfo{ .Bool = undefined }));
- testTypes(&[_]type{bool});
+ try testing.expect(bool == @Type(TypeInfo{ .Bool = undefined }));
+ try testTypes(&[_]type{bool});
}
test "Type.NoReturn" {
- testing.expect(noreturn == @Type(TypeInfo{ .NoReturn = undefined }));
- testTypes(&[_]type{noreturn});
+ try testing.expect(noreturn == @Type(TypeInfo{ .NoReturn = undefined }));
+ try testTypes(&[_]type{noreturn});
}
test "Type.Int" {
- testing.expect(u1 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .unsigned, .bits = 1 } }));
- testing.expect(i1 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .signed, .bits = 1 } }));
- testing.expect(u8 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .unsigned, .bits = 8 } }));
- testing.expect(i8 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .signed, .bits = 8 } }));
- testing.expect(u64 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .unsigned, .bits = 64 } }));
- testing.expect(i64 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .signed, .bits = 64 } }));
- testTypes(&[_]type{ u8, u32, i64 });
+ try testing.expect(u1 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .unsigned, .bits = 1 } }));
+ try testing.expect(i1 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .signed, .bits = 1 } }));
+ try testing.expect(u8 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .unsigned, .bits = 8 } }));
+ try testing.expect(i8 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .signed, .bits = 8 } }));
+ try testing.expect(u64 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .unsigned, .bits = 64 } }));
+ try testing.expect(i64 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .signed, .bits = 64 } }));
+ try testTypes(&[_]type{ u8, u32, i64 });
}
test "Type.Float" {
- testing.expect(f16 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 16 } }));
- testing.expect(f32 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 32 } }));
- testing.expect(f64 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 64 } }));
- testing.expect(f128 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 128 } }));
- testTypes(&[_]type{ f16, f32, f64, f128 });
+ try testing.expect(f16 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 16 } }));
+ try testing.expect(f32 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 32 } }));
+ try testing.expect(f64 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 64 } }));
+ try testing.expect(f128 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 128 } }));
+ try testTypes(&[_]type{ f16, f32, f64, f128 });
}
test "Type.Pointer" {
- testTypes(&[_]type{
+ try testTypes(&[_]type{
// One Value Pointer Types
*u8, *const u8,
*volatile u8, *const volatile u8,
@@ -94,41 +94,41 @@ test "Type.Pointer" {
}
test "Type.Array" {
- testing.expect([123]u8 == @Type(TypeInfo{
+ try testing.expect([123]u8 == @Type(TypeInfo{
.Array = TypeInfo.Array{
.len = 123,
.child = u8,
.sentinel = null,
},
}));
- testing.expect([2]u32 == @Type(TypeInfo{
+ try testing.expect([2]u32 == @Type(TypeInfo{
.Array = TypeInfo.Array{
.len = 2,
.child = u32,
.sentinel = null,
},
}));
- testing.expect([2:0]u32 == @Type(TypeInfo{
+ try testing.expect([2:0]u32 == @Type(TypeInfo{
.Array = TypeInfo.Array{
.len = 2,
.child = u32,
.sentinel = 0,
},
}));
- testTypes(&[_]type{ [1]u8, [30]usize, [7]bool });
+ try testTypes(&[_]type{ [1]u8, [30]usize, [7]bool });
}
test "Type.ComptimeFloat" {
- testTypes(&[_]type{comptime_float});
+ try testTypes(&[_]type{comptime_float});
}
test "Type.ComptimeInt" {
- testTypes(&[_]type{comptime_int});
+ try testTypes(&[_]type{comptime_int});
}
test "Type.Undefined" {
- testTypes(&[_]type{@TypeOf(undefined)});
+ try testTypes(&[_]type{@TypeOf(undefined)});
}
test "Type.Null" {
- testTypes(&[_]type{@TypeOf(null)});
+ try testTypes(&[_]type{@TypeOf(null)});
}
test "@Type create slice with null sentinel" {
const Slice = @Type(builtin.TypeInfo{
@@ -142,10 +142,10 @@ test "@Type create slice with null sentinel" {
.sentinel = null,
},
});
- testing.expect(Slice == []align(8) const *i32);
+ try testing.expect(Slice == []align(8) const *i32);
}
test "@Type picks up the sentinel value from TypeInfo" {
- testTypes(&[_]type{
+ try testTypes(&[_]type{
[11:0]u8, [4:10]u8,
[*:0]u8, [*:0]const u8,
[*:0]volatile u8, [*:0]const volatile u8,
@@ -173,7 +173,7 @@ test "@Type picks up the sentinel value from TypeInfo" {
}
test "Type.Optional" {
- testTypes(&[_]type{
+ try testTypes(&[_]type{
?u8,
?*u8,
?[]u8,
@@ -183,7 +183,7 @@ test "Type.Optional" {
}
test "Type.ErrorUnion" {
- testTypes(&[_]type{
+ try testTypes(&[_]type{
error{}!void,
error{Error}!void,
});
@@ -195,8 +195,8 @@ test "Type.Opaque" {
.decls = &[_]TypeInfo.Declaration{},
},
});
- testing.expect(Opaque != opaque {});
- testing.expectEqualSlices(
+ try testing.expect(Opaque != opaque {});
+ try testing.expectEqualSlices(
TypeInfo.Declaration,
&[_]TypeInfo.Declaration{},
@typeInfo(Opaque).Opaque.decls,
@@ -204,7 +204,7 @@ test "Type.Opaque" {
}
test "Type.Vector" {
- testTypes(&[_]type{
+ try testTypes(&[_]type{
@Vector(0, u8),
@Vector(4, u8),
@Vector(8, *u8),
@@ -215,7 +215,7 @@ test "Type.Vector" {
}
test "Type.AnyFrame" {
- testTypes(&[_]type{
+ try testTypes(&[_]type{
anyframe,
anyframe->u8,
anyframe->anyframe->u8,
@@ -223,7 +223,7 @@ test "Type.AnyFrame" {
}
test "Type.EnumLiteral" {
- testTypes(&[_]type{
+ try testTypes(&[_]type{
@TypeOf(.Dummy),
});
}
@@ -233,7 +233,7 @@ fn add(a: i32, b: i32) i32 {
}
test "Type.Frame" {
- testTypes(&[_]type{
+ try testTypes(&[_]type{
@Frame(add),
});
}
@@ -248,45 +248,45 @@ test "Type.ErrorSet" {
test "Type.Struct" {
const A = @Type(@typeInfo(struct { x: u8, y: u32 }));
const infoA = @typeInfo(A).Struct;
- testing.expectEqual(TypeInfo.ContainerLayout.Auto, infoA.layout);
- testing.expectEqualSlices(u8, "x", infoA.fields[0].name);
- testing.expectEqual(u8, infoA.fields[0].field_type);
- testing.expectEqual(@as(?u8, null), infoA.fields[0].default_value);
- testing.expectEqualSlices(u8, "y", infoA.fields[1].name);
- testing.expectEqual(u32, infoA.fields[1].field_type);
- testing.expectEqual(@as(?u32, null), infoA.fields[1].default_value);
- testing.expectEqualSlices(TypeInfo.Declaration, &[_]TypeInfo.Declaration{}, infoA.decls);
- testing.expectEqual(@as(bool, false), infoA.is_tuple);
+ try testing.expectEqual(TypeInfo.ContainerLayout.Auto, infoA.layout);
+ try testing.expectEqualSlices(u8, "x", infoA.fields[0].name);
+ try testing.expectEqual(u8, infoA.fields[0].field_type);
+ try testing.expectEqual(@as(?u8, null), infoA.fields[0].default_value);
+ try testing.expectEqualSlices(u8, "y", infoA.fields[1].name);
+ try testing.expectEqual(u32, infoA.fields[1].field_type);
+ try testing.expectEqual(@as(?u32, null), infoA.fields[1].default_value);
+ try testing.expectEqualSlices(TypeInfo.Declaration, &[_]TypeInfo.Declaration{}, infoA.decls);
+ try testing.expectEqual(@as(bool, false), infoA.is_tuple);
var a = A{ .x = 0, .y = 1 };
- testing.expectEqual(@as(u8, 0), a.x);
- testing.expectEqual(@as(u32, 1), a.y);
+ try testing.expectEqual(@as(u8, 0), a.x);
+ try testing.expectEqual(@as(u32, 1), a.y);
a.y += 1;
- testing.expectEqual(@as(u32, 2), a.y);
+ try testing.expectEqual(@as(u32, 2), a.y);
const B = @Type(@typeInfo(extern struct { x: u8, y: u32 = 5 }));
const infoB = @typeInfo(B).Struct;
- testing.expectEqual(TypeInfo.ContainerLayout.Extern, infoB.layout);
- testing.expectEqualSlices(u8, "x", infoB.fields[0].name);
- testing.expectEqual(u8, infoB.fields[0].field_type);
- testing.expectEqual(@as(?u8, null), infoB.fields[0].default_value);
- testing.expectEqualSlices(u8, "y", infoB.fields[1].name);
- testing.expectEqual(u32, infoB.fields[1].field_type);
- testing.expectEqual(@as(?u32, 5), infoB.fields[1].default_value);
- testing.expectEqual(@as(usize, 0), infoB.decls.len);
- testing.expectEqual(@as(bool, false), infoB.is_tuple);
+ try testing.expectEqual(TypeInfo.ContainerLayout.Extern, infoB.layout);
+ try testing.expectEqualSlices(u8, "x", infoB.fields[0].name);
+ try testing.expectEqual(u8, infoB.fields[0].field_type);
+ try testing.expectEqual(@as(?u8, null), infoB.fields[0].default_value);
+ try testing.expectEqualSlices(u8, "y", infoB.fields[1].name);
+ try testing.expectEqual(u32, infoB.fields[1].field_type);
+ try testing.expectEqual(@as(?u32, 5), infoB.fields[1].default_value);
+ try testing.expectEqual(@as(usize, 0), infoB.decls.len);
+ try testing.expectEqual(@as(bool, false), infoB.is_tuple);
const C = @Type(@typeInfo(packed struct { x: u8 = 3, y: u32 = 5 }));
const infoC = @typeInfo(C).Struct;
- testing.expectEqual(TypeInfo.ContainerLayout.Packed, infoC.layout);
- testing.expectEqualSlices(u8, "x", infoC.fields[0].name);
- testing.expectEqual(u8, infoC.fields[0].field_type);
- testing.expectEqual(@as(?u8, 3), infoC.fields[0].default_value);
- testing.expectEqualSlices(u8, "y", infoC.fields[1].name);
- testing.expectEqual(u32, infoC.fields[1].field_type);
- testing.expectEqual(@as(?u32, 5), infoC.fields[1].default_value);
- testing.expectEqual(@as(usize, 0), infoC.decls.len);
- testing.expectEqual(@as(bool, false), infoC.is_tuple);
+ try testing.expectEqual(TypeInfo.ContainerLayout.Packed, infoC.layout);
+ try testing.expectEqualSlices(u8, "x", infoC.fields[0].name);
+ try testing.expectEqual(u8, infoC.fields[0].field_type);
+ try testing.expectEqual(@as(?u8, 3), infoC.fields[0].default_value);
+ try testing.expectEqualSlices(u8, "y", infoC.fields[1].name);
+ try testing.expectEqual(u32, infoC.fields[1].field_type);
+ try testing.expectEqual(@as(?u32, 5), infoC.fields[1].default_value);
+ try testing.expectEqual(@as(usize, 0), infoC.decls.len);
+ try testing.expectEqual(@as(bool, false), infoC.is_tuple);
}
test "Type.Enum" {
@@ -302,9 +302,9 @@ test "Type.Enum" {
.is_exhaustive = true,
},
});
- testing.expectEqual(true, @typeInfo(Foo).Enum.is_exhaustive);
- testing.expectEqual(@as(u8, 1), @enumToInt(Foo.a));
- testing.expectEqual(@as(u8, 5), @enumToInt(Foo.b));
+ try testing.expectEqual(true, @typeInfo(Foo).Enum.is_exhaustive);
+ try testing.expectEqual(@as(u8, 1), @enumToInt(Foo.a));
+ try testing.expectEqual(@as(u8, 5), @enumToInt(Foo.b));
const Bar = @Type(.{
.Enum = .{
.layout = .Extern,
@@ -317,10 +317,10 @@ test "Type.Enum" {
.is_exhaustive = false,
},
});
- testing.expectEqual(false, @typeInfo(Bar).Enum.is_exhaustive);
- testing.expectEqual(@as(u32, 1), @enumToInt(Bar.a));
- testing.expectEqual(@as(u32, 5), @enumToInt(Bar.b));
- testing.expectEqual(@as(u32, 6), @enumToInt(@intToEnum(Bar, 6)));
+ try testing.expectEqual(false, @typeInfo(Bar).Enum.is_exhaustive);
+ try testing.expectEqual(@as(u32, 1), @enumToInt(Bar.a));
+ try testing.expectEqual(@as(u32, 5), @enumToInt(Bar.b));
+ try testing.expectEqual(@as(u32, 6), @enumToInt(@intToEnum(Bar, 6)));
}
test "Type.Union" {
@@ -338,7 +338,7 @@ test "Type.Union" {
var untagged = Untagged{ .int = 1 };
untagged.float = 2.0;
untagged.int = 3;
- testing.expectEqual(@as(i32, 3), untagged.int);
+ try testing.expectEqual(@as(i32, 3), untagged.int);
const PackedUntagged = @Type(.{
.Union = .{
@@ -352,8 +352,8 @@ test "Type.Union" {
},
});
var packed_untagged = PackedUntagged{ .signed = -1 };
- testing.expectEqual(@as(i32, -1), packed_untagged.signed);
- testing.expectEqual(~@as(u32, 0), packed_untagged.unsigned);
+ try testing.expectEqual(@as(i32, -1), packed_untagged.signed);
+ try testing.expectEqual(~@as(u32, 0), packed_untagged.unsigned);
const Tag = @Type(.{
.Enum = .{
@@ -379,9 +379,9 @@ test "Type.Union" {
},
});
var tagged = Tagged{ .signed = -1 };
- testing.expectEqual(Tag.signed, tagged);
+ try testing.expectEqual(Tag.signed, tagged);
tagged = .{ .unsigned = 1 };
- testing.expectEqual(Tag.unsigned, tagged);
+ try testing.expectEqual(Tag.unsigned, tagged);
}
test "Type.Union from Type.Enum" {
@@ -447,7 +447,7 @@ test "Type.BoundFn" {
pub fn foo(self: *const @This()) align(4) callconv(.Unspecified) void {}
};
const test_instance: TestStruct = undefined;
- testing.expect(std.meta.eql(
+ try testing.expect(std.meta.eql(
@typeName(@TypeOf(test_instance.foo)),
@typeName(@Type(@typeInfo(@TypeOf(test_instance.foo)))),
));
test/stage1/behavior/type_info.zig
@@ -9,151 +9,151 @@ const expect = std.testing.expect;
const expectEqualStrings = std.testing.expectEqualStrings;
test "type info: tag type, void info" {
- testBasic();
- comptime testBasic();
+ try testBasic();
+ comptime try testBasic();
}
-fn testBasic() void {
- expect(@typeInfo(TypeInfo).Union.tag_type == TypeId);
+fn testBasic() !void {
+ try expect(@typeInfo(TypeInfo).Union.tag_type == TypeId);
const void_info = @typeInfo(void);
- expect(void_info == TypeId.Void);
- expect(void_info.Void == {});
+ try expect(void_info == TypeId.Void);
+ try expect(void_info.Void == {});
}
test "type info: integer, floating point type info" {
- testIntFloat();
- comptime testIntFloat();
+ try testIntFloat();
+ comptime try testIntFloat();
}
-fn testIntFloat() void {
+fn testIntFloat() !void {
const u8_info = @typeInfo(u8);
- expect(u8_info == .Int);
- expect(u8_info.Int.signedness == .unsigned);
- expect(u8_info.Int.bits == 8);
+ try expect(u8_info == .Int);
+ try expect(u8_info.Int.signedness == .unsigned);
+ try expect(u8_info.Int.bits == 8);
const f64_info = @typeInfo(f64);
- expect(f64_info == .Float);
- expect(f64_info.Float.bits == 64);
+ try expect(f64_info == .Float);
+ try expect(f64_info.Float.bits == 64);
}
test "type info: pointer type info" {
- testPointer();
- comptime testPointer();
+ try testPointer();
+ comptime try testPointer();
}
-fn testPointer() void {
+fn testPointer() !void {
const u32_ptr_info = @typeInfo(*u32);
- expect(u32_ptr_info == .Pointer);
- expect(u32_ptr_info.Pointer.size == TypeInfo.Pointer.Size.One);
- expect(u32_ptr_info.Pointer.is_const == false);
- expect(u32_ptr_info.Pointer.is_volatile == false);
- expect(u32_ptr_info.Pointer.alignment == @alignOf(u32));
- expect(u32_ptr_info.Pointer.child == u32);
- expect(u32_ptr_info.Pointer.sentinel == null);
+ try expect(u32_ptr_info == .Pointer);
+ try expect(u32_ptr_info.Pointer.size == TypeInfo.Pointer.Size.One);
+ try expect(u32_ptr_info.Pointer.is_const == false);
+ try expect(u32_ptr_info.Pointer.is_volatile == false);
+ try expect(u32_ptr_info.Pointer.alignment == @alignOf(u32));
+ try expect(u32_ptr_info.Pointer.child == u32);
+ try expect(u32_ptr_info.Pointer.sentinel == null);
}
test "type info: unknown length pointer type info" {
- testUnknownLenPtr();
- comptime testUnknownLenPtr();
+ try testUnknownLenPtr();
+ comptime try testUnknownLenPtr();
}
-fn testUnknownLenPtr() void {
+fn testUnknownLenPtr() !void {
const u32_ptr_info = @typeInfo([*]const volatile f64);
- expect(u32_ptr_info == .Pointer);
- expect(u32_ptr_info.Pointer.size == TypeInfo.Pointer.Size.Many);
- expect(u32_ptr_info.Pointer.is_const == true);
- expect(u32_ptr_info.Pointer.is_volatile == true);
- expect(u32_ptr_info.Pointer.sentinel == null);
- expect(u32_ptr_info.Pointer.alignment == @alignOf(f64));
- expect(u32_ptr_info.Pointer.child == f64);
+ try expect(u32_ptr_info == .Pointer);
+ try expect(u32_ptr_info.Pointer.size == TypeInfo.Pointer.Size.Many);
+ try expect(u32_ptr_info.Pointer.is_const == true);
+ try expect(u32_ptr_info.Pointer.is_volatile == true);
+ try expect(u32_ptr_info.Pointer.sentinel == null);
+ try expect(u32_ptr_info.Pointer.alignment == @alignOf(f64));
+ try expect(u32_ptr_info.Pointer.child == f64);
}
test "type info: null terminated pointer type info" {
- testNullTerminatedPtr();
- comptime testNullTerminatedPtr();
+ try testNullTerminatedPtr();
+ comptime try testNullTerminatedPtr();
}
-fn testNullTerminatedPtr() void {
+fn testNullTerminatedPtr() !void {
const ptr_info = @typeInfo([*:0]u8);
- expect(ptr_info == .Pointer);
- expect(ptr_info.Pointer.size == TypeInfo.Pointer.Size.Many);
- expect(ptr_info.Pointer.is_const == false);
- expect(ptr_info.Pointer.is_volatile == false);
- expect(ptr_info.Pointer.sentinel.? == 0);
+ try expect(ptr_info == .Pointer);
+ try expect(ptr_info.Pointer.size == TypeInfo.Pointer.Size.Many);
+ try expect(ptr_info.Pointer.is_const == false);
+ try expect(ptr_info.Pointer.is_volatile == false);
+ try expect(ptr_info.Pointer.sentinel.? == 0);
- expect(@typeInfo([:0]u8).Pointer.sentinel != null);
+ try expect(@typeInfo([:0]u8).Pointer.sentinel != null);
}
test "type info: C pointer type info" {
- testCPtr();
- comptime testCPtr();
+ try testCPtr();
+ comptime try testCPtr();
}
-fn testCPtr() void {
+fn testCPtr() !void {
const ptr_info = @typeInfo([*c]align(4) const i8);
- expect(ptr_info == .Pointer);
- expect(ptr_info.Pointer.size == .C);
- expect(ptr_info.Pointer.is_const);
- expect(!ptr_info.Pointer.is_volatile);
- expect(ptr_info.Pointer.alignment == 4);
- expect(ptr_info.Pointer.child == i8);
+ try expect(ptr_info == .Pointer);
+ try expect(ptr_info.Pointer.size == .C);
+ try expect(ptr_info.Pointer.is_const);
+ try expect(!ptr_info.Pointer.is_volatile);
+ try expect(ptr_info.Pointer.alignment == 4);
+ try expect(ptr_info.Pointer.child == i8);
}
test "type info: slice type info" {
- testSlice();
- comptime testSlice();
+ try testSlice();
+ comptime try testSlice();
}
-fn testSlice() void {
+fn testSlice() !void {
const u32_slice_info = @typeInfo([]u32);
- expect(u32_slice_info == .Pointer);
- expect(u32_slice_info.Pointer.size == .Slice);
- expect(u32_slice_info.Pointer.is_const == false);
- expect(u32_slice_info.Pointer.is_volatile == false);
- expect(u32_slice_info.Pointer.alignment == 4);
- expect(u32_slice_info.Pointer.child == u32);
+ try expect(u32_slice_info == .Pointer);
+ try expect(u32_slice_info.Pointer.size == .Slice);
+ try expect(u32_slice_info.Pointer.is_const == false);
+ try expect(u32_slice_info.Pointer.is_volatile == false);
+ try expect(u32_slice_info.Pointer.alignment == 4);
+ try expect(u32_slice_info.Pointer.child == u32);
}
test "type info: array type info" {
- testArray();
- comptime testArray();
+ try testArray();
+ comptime try testArray();
}
-fn testArray() void {
+fn testArray() !void {
{
const info = @typeInfo([42]u8);
- expect(info == .Array);
- expect(info.Array.len == 42);
- expect(info.Array.child == u8);
- expect(info.Array.sentinel == null);
+ try expect(info == .Array);
+ try expect(info.Array.len == 42);
+ try expect(info.Array.child == u8);
+ try expect(info.Array.sentinel == null);
}
{
const info = @typeInfo([10:0]u8);
- expect(info.Array.len == 10);
- expect(info.Array.child == u8);
- expect(info.Array.sentinel.? == @as(u8, 0));
- expect(@sizeOf([10:0]u8) == info.Array.len + 1);
+ try expect(info.Array.len == 10);
+ try expect(info.Array.child == u8);
+ try expect(info.Array.sentinel.? == @as(u8, 0));
+ try expect(@sizeOf([10:0]u8) == info.Array.len + 1);
}
}
test "type info: optional type info" {
- testOptional();
- comptime testOptional();
+ try testOptional();
+ comptime try testOptional();
}
-fn testOptional() void {
+fn testOptional() !void {
const null_info = @typeInfo(?void);
- expect(null_info == .Optional);
- expect(null_info.Optional.child == void);
+ try expect(null_info == .Optional);
+ try expect(null_info.Optional.child == void);
}
test "type info: error set, error union info" {
- testErrorSet();
- comptime testErrorSet();
+ try testErrorSet();
+ comptime try testErrorSet();
}
-fn testErrorSet() void {
+fn testErrorSet() !void {
const TestErrorSet = error{
First,
Second,
@@ -161,26 +161,26 @@ fn testErrorSet() void {
};
const error_set_info = @typeInfo(TestErrorSet);
- expect(error_set_info == .ErrorSet);
- expect(error_set_info.ErrorSet.?.len == 3);
- expect(mem.eql(u8, error_set_info.ErrorSet.?[0].name, "First"));
+ try expect(error_set_info == .ErrorSet);
+ try expect(error_set_info.ErrorSet.?.len == 3);
+ try expect(mem.eql(u8, error_set_info.ErrorSet.?[0].name, "First"));
const error_union_info = @typeInfo(TestErrorSet!usize);
- expect(error_union_info == .ErrorUnion);
- expect(error_union_info.ErrorUnion.error_set == TestErrorSet);
- expect(error_union_info.ErrorUnion.payload == usize);
+ try expect(error_union_info == .ErrorUnion);
+ try expect(error_union_info.ErrorUnion.error_set == TestErrorSet);
+ try expect(error_union_info.ErrorUnion.payload == usize);
const global_info = @typeInfo(anyerror);
- expect(global_info == .ErrorSet);
- expect(global_info.ErrorSet == null);
+ try expect(global_info == .ErrorSet);
+ try expect(global_info.ErrorSet == null);
}
test "type info: enum info" {
- testEnum();
- comptime testEnum();
+ try testEnum();
+ comptime try testEnum();
}
-fn testEnum() void {
+fn testEnum() !void {
const Os = enum {
Windows,
Macos,
@@ -189,28 +189,28 @@ fn testEnum() void {
};
const os_info = @typeInfo(Os);
- expect(os_info == .Enum);
- expect(os_info.Enum.layout == .Auto);
- expect(os_info.Enum.fields.len == 4);
- expect(mem.eql(u8, os_info.Enum.fields[1].name, "Macos"));
- expect(os_info.Enum.fields[3].value == 3);
- expect(os_info.Enum.tag_type == u2);
- expect(os_info.Enum.decls.len == 0);
+ try expect(os_info == .Enum);
+ try expect(os_info.Enum.layout == .Auto);
+ try expect(os_info.Enum.fields.len == 4);
+ try expect(mem.eql(u8, os_info.Enum.fields[1].name, "Macos"));
+ try expect(os_info.Enum.fields[3].value == 3);
+ try expect(os_info.Enum.tag_type == u2);
+ try expect(os_info.Enum.decls.len == 0);
}
test "type info: union info" {
- testUnion();
- comptime testUnion();
+ try testUnion();
+ comptime try testUnion();
}
-fn testUnion() void {
+fn testUnion() !void {
const typeinfo_info = @typeInfo(TypeInfo);
- expect(typeinfo_info == .Union);
- expect(typeinfo_info.Union.layout == .Auto);
- expect(typeinfo_info.Union.tag_type.? == TypeId);
- expect(typeinfo_info.Union.fields.len == 25);
- expect(typeinfo_info.Union.fields[4].field_type == @TypeOf(@typeInfo(u8).Int));
- expect(typeinfo_info.Union.decls.len == 22);
+ try expect(typeinfo_info == .Union);
+ try expect(typeinfo_info.Union.layout == .Auto);
+ try expect(typeinfo_info.Union.tag_type.? == TypeId);
+ try expect(typeinfo_info.Union.fields.len == 25);
+ try expect(typeinfo_info.Union.fields[4].field_type == @TypeOf(@typeInfo(u8).Int));
+ try expect(typeinfo_info.Union.decls.len == 22);
const TestNoTagUnion = union {
Foo: void,
@@ -218,52 +218,52 @@ fn testUnion() void {
};
const notag_union_info = @typeInfo(TestNoTagUnion);
- expect(notag_union_info == .Union);
- expect(notag_union_info.Union.tag_type == null);
- expect(notag_union_info.Union.layout == .Auto);
- expect(notag_union_info.Union.fields.len == 2);
- expect(notag_union_info.Union.fields[0].alignment == @alignOf(void));
- expect(notag_union_info.Union.fields[1].field_type == u32);
- expect(notag_union_info.Union.fields[1].alignment == @alignOf(u32));
+ try expect(notag_union_info == .Union);
+ try expect(notag_union_info.Union.tag_type == null);
+ try expect(notag_union_info.Union.layout == .Auto);
+ try expect(notag_union_info.Union.fields.len == 2);
+ try expect(notag_union_info.Union.fields[0].alignment == @alignOf(void));
+ try expect(notag_union_info.Union.fields[1].field_type == u32);
+ try expect(notag_union_info.Union.fields[1].alignment == @alignOf(u32));
const TestExternUnion = extern union {
foo: *c_void,
};
const extern_union_info = @typeInfo(TestExternUnion);
- expect(extern_union_info.Union.layout == .Extern);
- expect(extern_union_info.Union.tag_type == null);
- expect(extern_union_info.Union.fields[0].field_type == *c_void);
+ try expect(extern_union_info.Union.layout == .Extern);
+ try expect(extern_union_info.Union.tag_type == null);
+ try expect(extern_union_info.Union.fields[0].field_type == *c_void);
}
test "type info: struct info" {
- testStruct();
- comptime testStruct();
+ try testStruct();
+ comptime try testStruct();
}
-fn testStruct() void {
+fn testStruct() !void {
const unpacked_struct_info = @typeInfo(TestUnpackedStruct);
- expect(unpacked_struct_info.Struct.is_tuple == false);
- expect(unpacked_struct_info.Struct.fields[0].alignment == @alignOf(u32));
- expect(unpacked_struct_info.Struct.fields[0].default_value.? == 4);
- expectEqualStrings("foobar", unpacked_struct_info.Struct.fields[1].default_value.?);
+ try expect(unpacked_struct_info.Struct.is_tuple == false);
+ try expect(unpacked_struct_info.Struct.fields[0].alignment == @alignOf(u32));
+ try expect(unpacked_struct_info.Struct.fields[0].default_value.? == 4);
+ try expectEqualStrings("foobar", unpacked_struct_info.Struct.fields[1].default_value.?);
const struct_info = @typeInfo(TestStruct);
- expect(struct_info == .Struct);
- expect(struct_info.Struct.is_tuple == false);
- expect(struct_info.Struct.layout == .Packed);
- expect(struct_info.Struct.fields.len == 4);
- expect(struct_info.Struct.fields[0].alignment == 2 * @alignOf(usize));
- expect(struct_info.Struct.fields[2].field_type == *TestStruct);
- expect(struct_info.Struct.fields[2].default_value == null);
- expect(struct_info.Struct.fields[3].default_value.? == 4);
- expect(struct_info.Struct.fields[3].alignment == 1);
- expect(struct_info.Struct.decls.len == 2);
- expect(struct_info.Struct.decls[0].is_pub);
- expect(!struct_info.Struct.decls[0].data.Fn.is_extern);
- expect(struct_info.Struct.decls[0].data.Fn.lib_name == null);
- expect(struct_info.Struct.decls[0].data.Fn.return_type == void);
- expect(struct_info.Struct.decls[0].data.Fn.fn_type == fn (*const TestStruct) void);
+ try expect(struct_info == .Struct);
+ try expect(struct_info.Struct.is_tuple == false);
+ try expect(struct_info.Struct.layout == .Packed);
+ try expect(struct_info.Struct.fields.len == 4);
+ try expect(struct_info.Struct.fields[0].alignment == 2 * @alignOf(usize));
+ try expect(struct_info.Struct.fields[2].field_type == *TestStruct);
+ try expect(struct_info.Struct.fields[2].default_value == null);
+ try expect(struct_info.Struct.fields[3].default_value.? == 4);
+ try expect(struct_info.Struct.fields[3].alignment == 1);
+ try expect(struct_info.Struct.decls.len == 2);
+ try expect(struct_info.Struct.decls[0].is_pub);
+ try expect(!struct_info.Struct.decls[0].data.Fn.is_extern);
+ try expect(struct_info.Struct.decls[0].data.Fn.lib_name == null);
+ try expect(struct_info.Struct.decls[0].data.Fn.return_type == void);
+ try expect(struct_info.Struct.decls[0].data.Fn.fn_type == fn (*const TestStruct) void);
}
const TestUnpackedStruct = struct {
@@ -282,43 +282,43 @@ const TestStruct = packed struct {
};
test "type info: opaque info" {
- testOpaque();
- comptime testOpaque();
+ try testOpaque();
+ comptime try testOpaque();
}
-fn testOpaque() void {
+fn testOpaque() !void {
const Foo = opaque {
const A = 1;
fn b() void {}
};
const foo_info = @typeInfo(Foo);
- expect(foo_info.Opaque.decls.len == 2);
+ try expect(foo_info.Opaque.decls.len == 2);
}
test "type info: function type info" {
// wasm doesn't support align attributes on functions
if (builtin.arch == .wasm32 or builtin.arch == .wasm64) return error.SkipZigTest;
- testFunction();
- comptime testFunction();
+ try testFunction();
+ comptime try testFunction();
}
-fn testFunction() void {
+fn testFunction() !void {
const fn_info = @typeInfo(@TypeOf(foo));
- expect(fn_info == .Fn);
- expect(fn_info.Fn.alignment > 0);
- expect(fn_info.Fn.calling_convention == .C);
- expect(!fn_info.Fn.is_generic);
- expect(fn_info.Fn.args.len == 2);
- expect(fn_info.Fn.is_var_args);
- expect(fn_info.Fn.return_type.? == usize);
+ try expect(fn_info == .Fn);
+ try expect(fn_info.Fn.alignment > 0);
+ try expect(fn_info.Fn.calling_convention == .C);
+ try expect(!fn_info.Fn.is_generic);
+ try expect(fn_info.Fn.args.len == 2);
+ try expect(fn_info.Fn.is_var_args);
+ try expect(fn_info.Fn.return_type.? == usize);
const fn_aligned_info = @typeInfo(@TypeOf(fooAligned));
- expect(fn_aligned_info.Fn.alignment == 4);
+ try expect(fn_aligned_info.Fn.alignment == 4);
const test_instance: TestStruct = undefined;
const bound_fn_info = @typeInfo(@TypeOf(test_instance.foo));
- expect(bound_fn_info == .BoundFn);
- expect(bound_fn_info.BoundFn.args[0].arg_type.? == *const TestStruct);
+ try expect(bound_fn_info == .BoundFn);
+ try expect(bound_fn_info.BoundFn.args[0].arg_type.? == *const TestStruct);
}
extern fn foo(a: usize, b: bool, ...) callconv(.C) usize;
@@ -332,33 +332,33 @@ test "typeInfo with comptime parameter in struct fn def" {
}
test "type info: vectors" {
- testVector();
- comptime testVector();
+ try testVector();
+ comptime try testVector();
}
-fn testVector() void {
+fn testVector() !void {
const vec_info = @typeInfo(std.meta.Vector(4, i32));
- expect(vec_info == .Vector);
- expect(vec_info.Vector.len == 4);
- expect(vec_info.Vector.child == i32);
+ try expect(vec_info == .Vector);
+ try expect(vec_info.Vector.len == 4);
+ try expect(vec_info.Vector.child == i32);
}
test "type info: anyframe and anyframe->T" {
- testAnyFrame();
- comptime testAnyFrame();
+ try testAnyFrame();
+ comptime try testAnyFrame();
}
-fn testAnyFrame() void {
+fn testAnyFrame() !void {
{
const anyframe_info = @typeInfo(anyframe->i32);
- expect(anyframe_info == .AnyFrame);
- expect(anyframe_info.AnyFrame.child.? == i32);
+ try expect(anyframe_info == .AnyFrame);
+ try expect(anyframe_info.AnyFrame.child.? == i32);
}
{
const anyframe_info = @typeInfo(anyframe);
- expect(anyframe_info == .AnyFrame);
- expect(anyframe_info.AnyFrame.child == null);
+ try expect(anyframe_info == .AnyFrame);
+ try expect(anyframe_info.AnyFrame.child == null);
}
}
@@ -385,9 +385,9 @@ test "type info: extern fns with and without lib names" {
comptime {
for (info.Struct.decls) |decl| {
if (std.mem.eql(u8, decl.name, "bar1")) {
- expect(decl.data.Fn.lib_name == null);
+ try expect(decl.data.Fn.lib_name == null);
} else {
- expectEqualStrings("cool", decl.data.Fn.lib_name.?);
+ try expectEqualStrings("cool", decl.data.Fn.lib_name.?);
}
}
}
@@ -397,12 +397,12 @@ test "data field is a compile-time value" {
const S = struct {
const Bar = @as(isize, -1);
};
- comptime expect(@typeInfo(S).Struct.decls[0].data.Var == isize);
+ comptime try expect(@typeInfo(S).Struct.decls[0].data.Var == isize);
}
test "sentinel of opaque pointer type" {
const c_void_info = @typeInfo(*c_void);
- expect(c_void_info.Pointer.sentinel == null);
+ try expect(c_void_info.Pointer.sentinel == null);
}
test "@typeInfo does not force declarations into existence" {
@@ -413,12 +413,12 @@ test "@typeInfo does not force declarations into existence" {
@compileError("test failed");
}
};
- comptime expect(@typeInfo(S).Struct.fields.len == 1);
+ comptime try expect(@typeInfo(S).Struct.fields.len == 1);
}
test "defaut value for a var-typed field" {
const S = struct { x: anytype };
- expect(@typeInfo(S).Struct.fields[0].default_value == null);
+ try expect(@typeInfo(S).Struct.fields[0].default_value == null);
}
fn add(a: i32, b: i32) i32 {
@@ -428,7 +428,7 @@ fn add(a: i32, b: i32) i32 {
test "type info for async frames" {
switch (@typeInfo(@Frame(add))) {
.Frame => |frame| {
- expect(frame.function == add);
+ try expect(frame.function == add);
},
else => unreachable,
}
@@ -438,7 +438,7 @@ test "type info: value is correctly copied" {
comptime {
var ptrInfo = @typeInfo([]u32);
ptrInfo.Pointer.size = .One;
- expect(@typeInfo([]u32).Pointer.size == .Slice);
+ try expect(@typeInfo([]u32).Pointer.size == .Slice);
}
}
@@ -451,22 +451,22 @@ test "Declarations are returned in declaration order" {
const e = 5;
};
const d = @typeInfo(S).Struct.decls;
- expect(std.mem.eql(u8, d[0].name, "a"));
- expect(std.mem.eql(u8, d[1].name, "b"));
- expect(std.mem.eql(u8, d[2].name, "c"));
- expect(std.mem.eql(u8, d[3].name, "d"));
- expect(std.mem.eql(u8, d[4].name, "e"));
+ try expect(std.mem.eql(u8, d[0].name, "a"));
+ try expect(std.mem.eql(u8, d[1].name, "b"));
+ try expect(std.mem.eql(u8, d[2].name, "c"));
+ try expect(std.mem.eql(u8, d[3].name, "d"));
+ try expect(std.mem.eql(u8, d[4].name, "e"));
}
test "Struct.is_tuple" {
- expect(@typeInfo(@TypeOf(.{0})).Struct.is_tuple);
- expect(!@typeInfo(@TypeOf(.{ .a = 0 })).Struct.is_tuple);
+ try expect(@typeInfo(@TypeOf(.{0})).Struct.is_tuple);
+ try expect(!@typeInfo(@TypeOf(.{ .a = 0 })).Struct.is_tuple);
}
test "StructField.is_comptime" {
const info = @typeInfo(struct { x: u8 = 3, comptime y: u32 = 5 }).Struct;
- expect(!info.fields[0].is_comptime);
- expect(info.fields[1].is_comptime);
+ try expect(!info.fields[0].is_comptime);
+ try expect(info.fields[1].is_comptime);
}
test "typeInfo resolves usingnamespace declarations" {
@@ -479,6 +479,6 @@ test "typeInfo resolves usingnamespace declarations" {
usingnamespace A;
};
- expect(@typeInfo(B).Struct.decls.len == 2);
+ try expect(@typeInfo(B).Struct.decls.len == 2);
//a
}
test/stage1/behavior/typename.zig
@@ -3,5 +3,5 @@ const expect = std.testing.expect;
const expectEqualSlices = std.testing.expectEqualSlices;
test "slice" {
- expectEqualSlices(u8, "[]u8", @typeName([]u8));
+ try expectEqualSlices(u8, "[]u8", @typeName([]u8));
}
test/stage1/behavior/undefined.zig
@@ -12,16 +12,16 @@ fn initStaticArray() [10]i32 {
}
const static_array = initStaticArray();
test "init static array to undefined" {
- expect(static_array[0] == 1);
- expect(static_array[4] == 2);
- expect(static_array[7] == 3);
- expect(static_array[9] == 4);
+ try expect(static_array[0] == 1);
+ try expect(static_array[4] == 2);
+ try expect(static_array[7] == 3);
+ try expect(static_array[9] == 4);
comptime {
- expect(static_array[0] == 1);
- expect(static_array[4] == 2);
- expect(static_array[7] == 3);
- expect(static_array[9] == 4);
+ try expect(static_array[0] == 1);
+ try expect(static_array[4] == 2);
+ try expect(static_array[7] == 3);
+ try expect(static_array[9] == 4);
}
}
@@ -41,12 +41,12 @@ test "assign undefined to struct" {
comptime {
var foo: Foo = undefined;
setFooX(&foo);
- expect(foo.x == 2);
+ try expect(foo.x == 2);
}
{
var foo: Foo = undefined;
setFooX(&foo);
- expect(foo.x == 2);
+ try expect(foo.x == 2);
}
}
@@ -54,16 +54,16 @@ test "assign undefined to struct with method" {
comptime {
var foo: Foo = undefined;
foo.setFooXMethod();
- expect(foo.x == 3);
+ try expect(foo.x == 3);
}
{
var foo: Foo = undefined;
foo.setFooXMethod();
- expect(foo.x == 3);
+ try expect(foo.x == 3);
}
}
test "type name of undefined" {
const x = undefined;
- expect(mem.eql(u8, @typeName(@TypeOf(x)), "(undefined)"));
+ try expect(mem.eql(u8, @typeName(@TypeOf(x)), "(undefined)"));
}
test/stage1/behavior/union.zig
@@ -30,11 +30,11 @@ const array = [_]Value{
test "unions embedded in aggregate types" {
switch (array[1]) {
- Value.Array => |arr| expect(arr[4] == 3),
+ Value.Array => |arr| try expect(arr[4] == 3),
else => unreachable,
}
switch ((err catch unreachable).val1) {
- Value.Int => |x| expect(x == 1234),
+ Value.Int => |x| try expect(x == 1234),
else => unreachable,
}
}
@@ -46,18 +46,18 @@ const Foo = union {
test "basic unions" {
var foo = Foo{ .int = 1 };
- expect(foo.int == 1);
+ try expect(foo.int == 1);
foo = Foo{ .float = 12.34 };
- expect(foo.float == 12.34);
+ try expect(foo.float == 12.34);
}
test "comptime union field access" {
comptime {
var foo = Foo{ .int = 0 };
- expect(foo.int == 0);
+ try expect(foo.int == 0);
foo = Foo{ .float = 42.42 };
- expect(foo.float == 42.42);
+ try expect(foo.float == 42.42);
}
}
@@ -65,10 +65,10 @@ test "init union with runtime value" {
var foo: Foo = undefined;
setFloat(&foo, 12.34);
- expect(foo.float == 12.34);
+ try expect(foo.float == 12.34);
setInt(&foo, 42);
- expect(foo.int == 42);
+ try expect(foo.int == 42);
}
fn setFloat(foo: *Foo, x: f64) void {
@@ -86,9 +86,9 @@ const FooExtern = extern union {
test "basic extern unions" {
var foo = FooExtern{ .int = 1 };
- expect(foo.int == 1);
+ try expect(foo.int == 1);
foo.float = 12.34;
- expect(foo.float == 12.34);
+ try expect(foo.float == 12.34);
}
const Letter = enum {
@@ -103,16 +103,16 @@ const Payload = union(Letter) {
};
test "union with specified enum tag" {
- doTest();
- comptime doTest();
+ try doTest();
+ comptime try doTest();
}
-fn doTest() void {
- expect(bar(Payload{ .A = 1234 }) == -10);
+fn doTest() !void {
+ try expect((try bar(Payload{ .A = 1234 })) == -10);
}
-fn bar(value: Payload) i32 {
- expect(@as(Letter, value) == Letter.A);
+fn bar(value: Payload) !i32 {
+ try expect(@as(Letter, value) == Letter.A);
return switch (value) {
Payload.A => |x| return x - 1244,
Payload.B => |x| if (x == 12.34) @as(i32, 20) else 21,
@@ -128,8 +128,8 @@ const MultipleChoice = union(enum(u32)) {
};
test "simple union(enum(u32))" {
var x = MultipleChoice.C;
- expect(x == MultipleChoice.C);
- expect(@enumToInt(@as(Tag(MultipleChoice), x)) == 60);
+ try expect(x == MultipleChoice.C);
+ try expect(@enumToInt(@as(Tag(MultipleChoice), x)) == 60);
}
const MultipleChoice2 = union(enum(u32)) {
@@ -145,14 +145,14 @@ const MultipleChoice2 = union(enum(u32)) {
};
test "union(enum(u32)) with specified and unspecified tag values" {
- comptime expect(Tag(Tag(MultipleChoice2)) == u32);
- testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2{ .C = 123 });
- comptime testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2{ .C = 123 });
+ comptime try expect(Tag(Tag(MultipleChoice2)) == u32);
+ try testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2{ .C = 123 });
+ comptime try testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2{ .C = 123 });
}
-fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: MultipleChoice2) void {
- expect(@enumToInt(@as(Tag(MultipleChoice2), x)) == 60);
- expect(1123 == switch (x) {
+fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: MultipleChoice2) !void {
+ try expect(@enumToInt(@as(Tag(MultipleChoice2), x)) == 60);
+ try expect(1123 == switch (x) {
MultipleChoice2.A => 1,
MultipleChoice2.B => 2,
MultipleChoice2.C => |v| @as(i32, 1000) + v,
@@ -170,7 +170,7 @@ const ExternPtrOrInt = extern union {
int: u64,
};
test "extern union size" {
- comptime expect(@sizeOf(ExternPtrOrInt) == 8);
+ comptime try expect(@sizeOf(ExternPtrOrInt) == 8);
}
const PackedPtrOrInt = packed union {
@@ -178,14 +178,14 @@ const PackedPtrOrInt = packed union {
int: u64,
};
test "extern union size" {
- comptime expect(@sizeOf(PackedPtrOrInt) == 8);
+ comptime try expect(@sizeOf(PackedPtrOrInt) == 8);
}
const ZeroBits = union {
OnlyField: void,
};
test "union with only 1 field which is void should be zero bits" {
- comptime expect(@sizeOf(ZeroBits) == 0);
+ comptime try expect(@sizeOf(ZeroBits) == 0);
}
const TheTag = enum {
@@ -199,23 +199,23 @@ const TheUnion = union(TheTag) {
C: i32,
};
test "union field access gives the enum values" {
- expect(TheUnion.A == TheTag.A);
- expect(TheUnion.B == TheTag.B);
- expect(TheUnion.C == TheTag.C);
+ try expect(TheUnion.A == TheTag.A);
+ try expect(TheUnion.B == TheTag.B);
+ try expect(TheUnion.C == TheTag.C);
}
test "cast union to tag type of union" {
- testCastUnionToTag(TheUnion{ .B = 1234 });
- comptime testCastUnionToTag(TheUnion{ .B = 1234 });
+ try testCastUnionToTag(TheUnion{ .B = 1234 });
+ comptime try testCastUnionToTag(TheUnion{ .B = 1234 });
}
-fn testCastUnionToTag(x: TheUnion) void {
- expect(@as(TheTag, x) == TheTag.B);
+fn testCastUnionToTag(x: TheUnion) !void {
+ try expect(@as(TheTag, x) == TheTag.B);
}
test "cast tag type of union to union" {
var x: Value2 = Letter2.B;
- expect(@as(Letter2, x) == Letter2.B);
+ try expect(@as(Letter2, x) == Letter2.B);
}
const Letter2 = enum {
A,
@@ -230,11 +230,11 @@ const Value2 = union(Letter2) {
test "implicit cast union to its tag type" {
var x: Value2 = Letter2.B;
- expect(x == Letter2.B);
- giveMeLetterB(x);
+ try expect(x == Letter2.B);
+ try giveMeLetterB(x);
}
-fn giveMeLetterB(x: Letter2) void {
- expect(x == Value2.B);
+fn giveMeLetterB(x: Letter2) !void {
+ try expect(x == Value2.B);
}
pub const PackThis = union(enum) {
@@ -243,11 +243,11 @@ pub const PackThis = union(enum) {
};
test "constant packed union" {
- testConstPackedUnion(&[_]PackThis{PackThis{ .StringLiteral = 1 }});
+ try testConstPackedUnion(&[_]PackThis{PackThis{ .StringLiteral = 1 }});
}
-fn testConstPackedUnion(expected_tokens: []const PackThis) void {
- expect(expected_tokens[0].StringLiteral == 1);
+fn testConstPackedUnion(expected_tokens: []const PackThis) !void {
+ try expect(expected_tokens[0].StringLiteral == 1);
}
test "switch on union with only 1 field" {
@@ -259,7 +259,7 @@ test "switch on union with only 1 field" {
z = PartialInstWithPayload{ .Compiled = 1234 };
switch (z) {
PartialInstWithPayload.Compiled => |x| {
- expect(x == 1234);
+ try expect(x == 1234);
return;
},
}
@@ -285,11 +285,11 @@ test "access a member of tagged union with conflicting enum tag name" {
const B = void;
};
- comptime expect(Bar.A == u8);
+ comptime try expect(Bar.A == u8);
}
test "tagged union initialization with runtime void" {
- expect(testTaggedUnionInit({}));
+ try expect(testTaggedUnionInit({}));
}
const TaggedUnionWithAVoid = union(enum) {
@@ -327,9 +327,9 @@ test "union with only 1 field casted to its enum type" {
var e = Expr{ .Literal = Literal{ .Bool = true } };
const ExprTag = Tag(Expr);
- comptime expect(Tag(ExprTag) == u0);
+ comptime try expect(Tag(ExprTag) == u0);
var t = @as(ExprTag, e);
- expect(t == Expr.Literal);
+ try expect(t == Expr.Literal);
}
test "union with only 1 field casted to its enum type which has enum value specified" {
@@ -347,11 +347,11 @@ test "union with only 1 field casted to its enum type which has enum value speci
};
var e = Expr{ .Literal = Literal{ .Bool = true } };
- comptime expect(Tag(ExprTag) == comptime_int);
+ comptime try expect(Tag(ExprTag) == comptime_int);
var t = @as(ExprTag, e);
- expect(t == Expr.Literal);
- expect(@enumToInt(t) == 33);
- comptime expect(@enumToInt(t) == 33);
+ try expect(t == Expr.Literal);
+ try expect(@enumToInt(t) == 33);
+ comptime try expect(@enumToInt(t) == 33);
}
test "@enumToInt works on unions" {
@@ -364,9 +364,9 @@ test "@enumToInt works on unions" {
const a = Bar{ .A = true };
var b = Bar{ .B = undefined };
var c = Bar.C;
- expect(@enumToInt(a) == 0);
- expect(@enumToInt(b) == 1);
- expect(@enumToInt(c) == 2);
+ try expect(@enumToInt(a) == 0);
+ try expect(@enumToInt(b) == 1);
+ try expect(@enumToInt(c) == 2);
}
const Attribute = union(enum) {
@@ -393,23 +393,23 @@ test "comptime union field value equality" {
const b1 = Setter(Attribute{ .B = 9 });
const b2 = Setter(Attribute{ .B = 5 });
- expect(a0 == a0);
- expect(a1 == a1);
- expect(a0 == a2);
+ try expect(a0 == a0);
+ try expect(a1 == a1);
+ try expect(a0 == a2);
- expect(b0 == b0);
- expect(b1 == b1);
- expect(b0 == b2);
+ try expect(b0 == b0);
+ try expect(b1 == b1);
+ try expect(b0 == b2);
- expect(a0 != b0);
- expect(a0 != a1);
- expect(b0 != b1);
+ try expect(a0 != b0);
+ try expect(a0 != a1);
+ try expect(b0 != b1);
}
test "return union init with void payload" {
const S = struct {
- fn entry() void {
- expect(func().state == State.one);
+ fn entry() !void {
+ try expect(func().state == State.one);
}
const Outer = union(enum) {
state: State,
@@ -422,8 +422,8 @@ test "return union init with void payload" {
return Outer{ .state = State{ .one = {} } };
}
};
- S.entry();
- comptime S.entry();
+ try S.entry();
+ comptime try S.entry();
}
test "@unionInit can modify a union type" {
@@ -435,14 +435,14 @@ test "@unionInit can modify a union type" {
var value: UnionInitEnum = undefined;
value = @unionInit(UnionInitEnum, "Boolean", true);
- expect(value.Boolean == true);
+ try expect(value.Boolean == true);
value.Boolean = false;
- expect(value.Boolean == false);
+ try expect(value.Boolean == false);
value = @unionInit(UnionInitEnum, "Byte", 2);
- expect(value.Byte == 2);
+ try expect(value.Byte == 2);
value.Byte = 3;
- expect(value.Byte == 3);
+ try expect(value.Byte == 3);
}
test "@unionInit can modify a pointer value" {
@@ -455,10 +455,10 @@ test "@unionInit can modify a pointer value" {
var value_ptr = &value;
value_ptr.* = @unionInit(UnionInitEnum, "Boolean", true);
- expect(value.Boolean == true);
+ try expect(value.Boolean == true);
value_ptr.* = @unionInit(UnionInitEnum, "Byte", 2);
- expect(value.Byte == 2);
+ try expect(value.Byte == 2);
}
test "union no tag with struct member" {
@@ -471,38 +471,38 @@ test "union no tag with struct member" {
u.foo();
}
-fn testComparison() void {
+fn testComparison() !void {
var x = Payload{ .A = 42 };
- expect(x == .A);
- expect(x != .B);
- expect(x != .C);
- expect((x == .B) == false);
- expect((x == .C) == false);
- expect((x != .A) == false);
+ try expect(x == .A);
+ try expect(x != .B);
+ try expect(x != .C);
+ try expect((x == .B) == false);
+ try expect((x == .C) == false);
+ try expect((x != .A) == false);
}
test "comparison between union and enum literal" {
- testComparison();
- comptime testComparison();
+ try testComparison();
+ comptime try testComparison();
}
test "packed union generates correctly aligned LLVM type" {
const U = packed union {
- f1: fn () void,
+ f1: fn () error{TestUnexpectedResult}!void,
f2: u32,
};
var foo = [_]U{
U{ .f1 = doTest },
U{ .f2 = 0 },
};
- foo[0].f1();
+ try foo[0].f1();
}
test "union with one member defaults to u0 tag type" {
const U0 = union(enum) {
X: u32,
};
- comptime expect(Tag(Tag(U0)) == u0);
+ comptime try expect(Tag(Tag(U0)) == u0);
}
test "union with comptime_int tag" {
@@ -511,7 +511,7 @@ test "union with comptime_int tag" {
Y: u16,
Z: u8,
};
- comptime expect(Tag(Tag(Union)) == comptime_int);
+ comptime try expect(Tag(Tag(Union)) == comptime_int);
}
test "extern union doesn't trigger field check at comptime" {
@@ -521,7 +521,7 @@ test "extern union doesn't trigger field check at comptime" {
};
const x = U{ .x = 0x55AAAA55 };
- comptime expect(x.y == 0x55);
+ comptime try expect(x.y == 0x55);
}
const Foo1 = union(enum) {
@@ -535,7 +535,7 @@ test "global union with single field is correctly initialized" {
glbl = Foo1{
.f = @typeInfo(Foo1).Union.fields[0].field_type{ .x = 123 },
};
- expect(glbl.f.x == 123);
+ try expect(glbl.f.x == 123);
}
pub const FooUnion = union(enum) {
@@ -548,8 +548,8 @@ var glbl_array: [2]FooUnion = undefined;
test "initialize global array of union" {
glbl_array[1] = FooUnion{ .U1 = 2 };
glbl_array[0] = FooUnion{ .U0 = 1 };
- expect(glbl_array[0].U0 == 1);
- expect(glbl_array[1].U1 == 2);
+ try expect(glbl_array[0].U0 == 1);
+ try expect(glbl_array[1].U1 == 2);
}
test "anonymous union literal syntax" {
@@ -559,19 +559,19 @@ test "anonymous union literal syntax" {
float: f64,
};
- fn doTheTest() void {
+ fn doTheTest() !void {
var i: Number = .{ .int = 42 };
var f = makeNumber();
- expect(i.int == 42);
- expect(f.float == 12.34);
+ try expect(i.int == 42);
+ try expect(f.float == 12.34);
}
fn makeNumber() Number {
return .{ .float = 12.34 };
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "update the tag value for zero-sized unions" {
@@ -580,9 +580,9 @@ test "update the tag value for zero-sized unions" {
U1: void,
};
var x = S{ .U0 = {} };
- expect(x == .U0);
+ try expect(x == .U0);
x = S{ .U1 = {} };
- expect(x == .U1);
+ try expect(x == .U1);
}
test "function call result coerces from tagged union to the tag" {
@@ -594,12 +594,12 @@ test "function call result coerces from tagged union to the tag" {
const ArchTag = Tag(Arch);
- fn doTheTest() void {
+ fn doTheTest() !void {
var x: ArchTag = getArch1();
- expect(x == .One);
+ try expect(x == .One);
var y: ArchTag = getArch2();
- expect(y == .Two);
+ try expect(y == .Two);
}
pub fn getArch1() Arch {
@@ -610,8 +610,8 @@ test "function call result coerces from tagged union to the tag" {
return .{ .Two = 99 };
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "0-sized extern union definition" {
@@ -620,7 +620,7 @@ test "0-sized extern union definition" {
const f = 1;
};
- expect(U.f == 1);
+ try expect(U.f == 1);
}
test "union initializer generates padding only if needed" {
@@ -629,7 +629,7 @@ test "union initializer generates padding only if needed" {
};
var v = U{ .A = 532 };
- expect(v.A == 532);
+ try expect(v.A == 532);
}
test "runtime tag name with single field" {
@@ -638,7 +638,7 @@ test "runtime tag name with single field" {
};
var v = U{ .A = 42 };
- expect(std.mem.eql(u8, @tagName(v), "A"));
+ try expect(std.mem.eql(u8, @tagName(v), "A"));
}
test "cast from anonymous struct to union" {
@@ -648,7 +648,7 @@ test "cast from anonymous struct to union" {
B: []const u8,
C: void,
};
- fn doTheTest() void {
+ fn doTheTest() !void {
var y: u32 = 42;
const t0 = .{ .A = 123 };
const t1 = .{ .B = "foo" };
@@ -658,14 +658,14 @@ test "cast from anonymous struct to union" {
var x1: U = t1;
const x2: U = t2;
var x3: U = t3;
- expect(x0.A == 123);
- expect(std.mem.eql(u8, x1.B, "foo"));
- expect(x2 == .C);
- expect(x3.A == y);
+ try expect(x0.A == 123);
+ try expect(std.mem.eql(u8, x1.B, "foo"));
+ try expect(x2 == .C);
+ try expect(x3.A == y);
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "cast from pointer to anonymous struct to pointer to union" {
@@ -675,7 +675,7 @@ test "cast from pointer to anonymous struct to pointer to union" {
B: []const u8,
C: void,
};
- fn doTheTest() void {
+ fn doTheTest() !void {
var y: u32 = 42;
const t0 = &.{ .A = 123 };
const t1 = &.{ .B = "foo" };
@@ -685,14 +685,14 @@ test "cast from pointer to anonymous struct to pointer to union" {
var x1: *const U = t1;
const x2: *const U = t2;
var x3: *const U = t3;
- expect(x0.A == 123);
- expect(std.mem.eql(u8, x1.B, "foo"));
- expect(x2.* == .C);
- expect(x3.A == y);
+ try expect(x0.A == 123);
+ try expect(std.mem.eql(u8, x1.B, "foo"));
+ try expect(x2.* == .C);
+ try expect(x3.A == y);
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "method call on an empty union" {
@@ -707,13 +707,13 @@ test "method call on an empty union" {
}
};
- fn doTheTest() void {
+ fn doTheTest() !void {
var u = MyUnion{ .X1 = [0]u8{} };
- expect(u.useIt());
+ try expect(u.useIt());
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "switching on non exhaustive union" {
@@ -727,16 +727,16 @@ test "switching on non exhaustive union" {
a: i32,
b: u32,
};
- fn doTheTest() void {
+ fn doTheTest() !void {
var a = U{ .a = 2 };
switch (a) {
- .a => |val| expect(val == 2),
+ .a => |val| try expect(val == 2),
.b => unreachable,
}
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "containers with single-field enums" {
@@ -746,21 +746,21 @@ test "containers with single-field enums" {
const C = struct { a: A };
const D = struct { a: B };
- fn doTheTest() void {
+ fn doTheTest() !void {
var array1 = [1]A{A{ .f1 = {} }};
var array2 = [1]B{B{ .f1 = {} }};
- expect(array1[0] == .f1);
- expect(array2[0] == .f1);
+ try expect(array1[0] == .f1);
+ try expect(array2[0] == .f1);
var struct1 = C{ .a = A{ .f1 = {} } };
var struct2 = D{ .a = B{ .f1 = {} } };
- expect(struct1.a == .f1);
- expect(struct2.a == .f1);
+ try expect(struct1.a == .f1);
+ try expect(struct2.a == .f1);
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "@unionInit on union w/ tag but no fields" {
@@ -776,18 +776,18 @@ test "@unionInit on union w/ tag but no fields" {
};
comptime {
- expect(@sizeOf(Data) != 0);
+ try expect(@sizeOf(Data) != 0);
}
- fn doTheTest() void {
+ fn doTheTest() !void {
var data: Data = .{ .no_op = .{} };
var o = Data.decode(&[_]u8{});
- expectEqual(Type.no_op, o);
+ try expectEqual(Type.no_op, o);
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "union enum type gets a separate scope" {
@@ -797,10 +797,10 @@ test "union enum type gets a separate scope" {
const foo = 1;
};
- fn doTheTest() void {
- expect(!@hasDecl(Tag(U), "foo"));
+ fn doTheTest() !void {
+ try expect(!@hasDecl(Tag(U), "foo"));
}
};
- S.doTheTest();
+ try S.doTheTest();
}
test/stage1/behavior/usingnamespace.zig
@@ -9,8 +9,8 @@ fn Foo(comptime T: type) type {
test "usingnamespace inside a generic struct" {
const std2 = Foo(std);
const testing2 = Foo(std.testing);
- std2.testing.expect(true);
- testing2.expect(true);
+ try std2.testing.expect(true);
+ try testing2.expect(true);
}
usingnamespace struct {
@@ -18,5 +18,5 @@ usingnamespace struct {
};
test "usingnamespace does not redeclare an imported variable" {
- comptime std.testing.expect(foo == 42);
+ comptime try std.testing.expect(foo == 42);
}
test/stage1/behavior/var_args.zig
@@ -12,9 +12,9 @@ fn add(args: anytype) i32 {
}
test "add arbitrary args" {
- expect(add(.{ @as(i32, 1), @as(i32, 2), @as(i32, 3), @as(i32, 4) }) == 10);
- expect(add(.{@as(i32, 1234)}) == 1234);
- expect(add(.{}) == 0);
+ try expect(add(.{ @as(i32, 1), @as(i32, 2), @as(i32, 3), @as(i32, 4) }) == 10);
+ try expect(add(.{@as(i32, 1234)}) == 1234);
+ try expect(add(.{}) == 0);
}
fn readFirstVarArg(args: anytype) void {
@@ -26,9 +26,9 @@ test "send void arg to var args" {
}
test "pass args directly" {
- expect(addSomeStuff(.{ @as(i32, 1), @as(i32, 2), @as(i32, 3), @as(i32, 4) }) == 10);
- expect(addSomeStuff(.{@as(i32, 1234)}) == 1234);
- expect(addSomeStuff(.{}) == 0);
+ try expect(addSomeStuff(.{ @as(i32, 1), @as(i32, 2), @as(i32, 3), @as(i32, 4) }) == 10);
+ try expect(addSomeStuff(.{@as(i32, 1234)}) == 1234);
+ try expect(addSomeStuff(.{}) == 0);
}
fn addSomeStuff(args: anytype) i32 {
@@ -36,23 +36,23 @@ fn addSomeStuff(args: anytype) i32 {
}
test "runtime parameter before var args" {
- expect(extraFn(10, .{}) == 0);
- expect(extraFn(10, .{false}) == 1);
- expect(extraFn(10, .{ false, true }) == 2);
+ try expect((try extraFn(10, .{})) == 0);
+ try expect((try extraFn(10, .{false})) == 1);
+ try expect((try extraFn(10, .{ false, true })) == 2);
comptime {
- expect(extraFn(10, .{}) == 0);
- expect(extraFn(10, .{false}) == 1);
- expect(extraFn(10, .{ false, true }) == 2);
+ try expect((try extraFn(10, .{})) == 0);
+ try expect((try extraFn(10, .{false})) == 1);
+ try expect((try extraFn(10, .{ false, true })) == 2);
}
}
-fn extraFn(extra: u32, args: anytype) usize {
+fn extraFn(extra: u32, args: anytype) !usize {
if (args.len >= 1) {
- expect(args[0] == false);
+ try expect(args[0] == false);
}
if (args.len >= 2) {
- expect(args[1] == true);
+ try expect(args[1] == true);
}
return args.len;
}
@@ -70,8 +70,8 @@ fn foo2(args: anytype) bool {
}
test "array of var args functions" {
- expect(foos[0](.{}));
- expect(!foos[1](.{}));
+ try expect(foos[0](.{}));
+ try expect(!foos[1](.{}));
}
test "pass zero length array to var args param" {
test/stage1/behavior/vector.zig
@@ -9,104 +9,104 @@ const Vector = std.meta.Vector;
test "implicit cast vector to array - bool" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
const a: Vector(4, bool) = [_]bool{ true, false, true, false };
const result_array: [4]bool = a;
- expect(mem.eql(bool, &result_array, &[4]bool{ true, false, true, false }));
+ try expect(mem.eql(bool, &result_array, &[4]bool{ true, false, true, false }));
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "vector wrap operators" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var v: Vector(4, i32) = [4]i32{ 2147483647, -2, 30, 40 };
var x: Vector(4, i32) = [4]i32{ 1, 2147483647, 3, 4 };
- expect(mem.eql(i32, &@as([4]i32, v +% x), &[4]i32{ -2147483648, 2147483645, 33, 44 }));
- expect(mem.eql(i32, &@as([4]i32, v -% x), &[4]i32{ 2147483646, 2147483647, 27, 36 }));
- expect(mem.eql(i32, &@as([4]i32, v *% x), &[4]i32{ 2147483647, 2, 90, 160 }));
+ try expect(mem.eql(i32, &@as([4]i32, v +% x), &[4]i32{ -2147483648, 2147483645, 33, 44 }));
+ try expect(mem.eql(i32, &@as([4]i32, v -% x), &[4]i32{ 2147483646, 2147483647, 27, 36 }));
+ try expect(mem.eql(i32, &@as([4]i32, v *% x), &[4]i32{ 2147483647, 2, 90, 160 }));
var z: Vector(4, i32) = [4]i32{ 1, 2, 3, -2147483648 };
- expect(mem.eql(i32, &@as([4]i32, -%z), &[4]i32{ -1, -2, -3, -2147483648 }));
+ try expect(mem.eql(i32, &@as([4]i32, -%z), &[4]i32{ -1, -2, -3, -2147483648 }));
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "vector bin compares with mem.eql" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var v: Vector(4, i32) = [4]i32{ 2147483647, -2, 30, 40 };
var x: Vector(4, i32) = [4]i32{ 1, 2147483647, 30, 4 };
- expect(mem.eql(bool, &@as([4]bool, v == x), &[4]bool{ false, false, true, false }));
- expect(mem.eql(bool, &@as([4]bool, v != x), &[4]bool{ true, true, false, true }));
- expect(mem.eql(bool, &@as([4]bool, v < x), &[4]bool{ false, true, false, false }));
- expect(mem.eql(bool, &@as([4]bool, v > x), &[4]bool{ true, false, false, true }));
- expect(mem.eql(bool, &@as([4]bool, v <= x), &[4]bool{ false, true, true, false }));
- expect(mem.eql(bool, &@as([4]bool, v >= x), &[4]bool{ true, false, true, true }));
+ try expect(mem.eql(bool, &@as([4]bool, v == x), &[4]bool{ false, false, true, false }));
+ try expect(mem.eql(bool, &@as([4]bool, v != x), &[4]bool{ true, true, false, true }));
+ try expect(mem.eql(bool, &@as([4]bool, v < x), &[4]bool{ false, true, false, false }));
+ try expect(mem.eql(bool, &@as([4]bool, v > x), &[4]bool{ true, false, false, true }));
+ try expect(mem.eql(bool, &@as([4]bool, v <= x), &[4]bool{ false, true, true, false }));
+ try expect(mem.eql(bool, &@as([4]bool, v >= x), &[4]bool{ true, false, true, true }));
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "vector int operators" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var v: Vector(4, i32) = [4]i32{ 10, 20, 30, 40 };
var x: Vector(4, i32) = [4]i32{ 1, 2, 3, 4 };
- expect(mem.eql(i32, &@as([4]i32, v + x), &[4]i32{ 11, 22, 33, 44 }));
- expect(mem.eql(i32, &@as([4]i32, v - x), &[4]i32{ 9, 18, 27, 36 }));
- expect(mem.eql(i32, &@as([4]i32, v * x), &[4]i32{ 10, 40, 90, 160 }));
- expect(mem.eql(i32, &@as([4]i32, -v), &[4]i32{ -10, -20, -30, -40 }));
+ try expect(mem.eql(i32, &@as([4]i32, v + x), &[4]i32{ 11, 22, 33, 44 }));
+ try expect(mem.eql(i32, &@as([4]i32, v - x), &[4]i32{ 9, 18, 27, 36 }));
+ try expect(mem.eql(i32, &@as([4]i32, v * x), &[4]i32{ 10, 40, 90, 160 }));
+ try expect(mem.eql(i32, &@as([4]i32, -v), &[4]i32{ -10, -20, -30, -40 }));
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "vector float operators" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var v: Vector(4, f32) = [4]f32{ 10, 20, 30, 40 };
var x: Vector(4, f32) = [4]f32{ 1, 2, 3, 4 };
- expect(mem.eql(f32, &@as([4]f32, v + x), &[4]f32{ 11, 22, 33, 44 }));
- expect(mem.eql(f32, &@as([4]f32, v - x), &[4]f32{ 9, 18, 27, 36 }));
- expect(mem.eql(f32, &@as([4]f32, v * x), &[4]f32{ 10, 40, 90, 160 }));
- expect(mem.eql(f32, &@as([4]f32, -x), &[4]f32{ -1, -2, -3, -4 }));
+ try expect(mem.eql(f32, &@as([4]f32, v + x), &[4]f32{ 11, 22, 33, 44 }));
+ try expect(mem.eql(f32, &@as([4]f32, v - x), &[4]f32{ 9, 18, 27, 36 }));
+ try expect(mem.eql(f32, &@as([4]f32, v * x), &[4]f32{ 10, 40, 90, 160 }));
+ try expect(mem.eql(f32, &@as([4]f32, -x), &[4]f32{ -1, -2, -3, -4 }));
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "vector bit operators" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var v: Vector(4, u8) = [4]u8{ 0b10101010, 0b10101010, 0b10101010, 0b10101010 };
var x: Vector(4, u8) = [4]u8{ 0b11110000, 0b00001111, 0b10101010, 0b01010101 };
- expect(mem.eql(u8, &@as([4]u8, v ^ x), &[4]u8{ 0b01011010, 0b10100101, 0b00000000, 0b11111111 }));
- expect(mem.eql(u8, &@as([4]u8, v | x), &[4]u8{ 0b11111010, 0b10101111, 0b10101010, 0b11111111 }));
- expect(mem.eql(u8, &@as([4]u8, v & x), &[4]u8{ 0b10100000, 0b00001010, 0b10101010, 0b00000000 }));
+ try expect(mem.eql(u8, &@as([4]u8, v ^ x), &[4]u8{ 0b01011010, 0b10100101, 0b00000000, 0b11111111 }));
+ try expect(mem.eql(u8, &@as([4]u8, v | x), &[4]u8{ 0b11111010, 0b10101111, 0b10101010, 0b11111111 }));
+ try expect(mem.eql(u8, &@as([4]u8, v & x), &[4]u8{ 0b10100000, 0b00001010, 0b10101010, 0b00000000 }));
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "implicit cast vector to array" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var a: Vector(4, i32) = [_]i32{ 1, 2, 3, 4 };
var result_array: [4]i32 = a;
result_array = a;
- expect(mem.eql(i32, &result_array, &[4]i32{ 1, 2, 3, 4 }));
+ try expect(mem.eql(i32, &result_array, &[4]i32{ 1, 2, 3, 4 }));
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "array to vector" {
@@ -120,141 +120,141 @@ test "vector casts of sizes not divisable by 8" {
if (std.Target.current.os.tag == .dragonfly) return error.SkipZigTest;
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
{
var v: Vector(4, u3) = [4]u3{ 5, 2, 3, 0 };
var x: [4]u3 = v;
- expect(mem.eql(u3, &x, &@as([4]u3, v)));
+ try expect(mem.eql(u3, &x, &@as([4]u3, v)));
}
{
var v: Vector(4, u2) = [4]u2{ 1, 2, 3, 0 };
var x: [4]u2 = v;
- expect(mem.eql(u2, &x, &@as([4]u2, v)));
+ try expect(mem.eql(u2, &x, &@as([4]u2, v)));
}
{
var v: Vector(4, u1) = [4]u1{ 1, 0, 1, 0 };
var x: [4]u1 = v;
- expect(mem.eql(u1, &x, &@as([4]u1, v)));
+ try expect(mem.eql(u1, &x, &@as([4]u1, v)));
}
{
var v: Vector(4, bool) = [4]bool{ false, false, true, false };
var x: [4]bool = v;
- expect(mem.eql(bool, &x, &@as([4]bool, v)));
+ try expect(mem.eql(bool, &x, &@as([4]bool, v)));
}
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "vector @splat" {
const S = struct {
- fn testForT(comptime N: comptime_int, v: anytype) void {
+ fn testForT(comptime N: comptime_int, v: anytype) !void {
const T = @TypeOf(v);
var vec = @splat(N, v);
- expectEqual(Vector(N, T), @TypeOf(vec));
+ try expectEqual(Vector(N, T), @TypeOf(vec));
var as_array = @as([N]T, vec);
- for (as_array) |elem| expectEqual(v, elem);
+ for (as_array) |elem| try expectEqual(v, elem);
}
- fn doTheTest() void {
+ fn doTheTest() !void {
// Splats with multiple-of-8 bit types that fill a 128bit vector.
- testForT(16, @as(u8, 0xEE));
- testForT(8, @as(u16, 0xBEEF));
- testForT(4, @as(u32, 0xDEADBEEF));
- testForT(2, @as(u64, 0xCAFEF00DDEADBEEF));
+ try testForT(16, @as(u8, 0xEE));
+ try testForT(8, @as(u16, 0xBEEF));
+ try testForT(4, @as(u32, 0xDEADBEEF));
+ try testForT(2, @as(u64, 0xCAFEF00DDEADBEEF));
- testForT(8, @as(f16, 3.1415));
- testForT(4, @as(f32, 3.1415));
- testForT(2, @as(f64, 3.1415));
+ try testForT(8, @as(f16, 3.1415));
+ try testForT(4, @as(f32, 3.1415));
+ try testForT(2, @as(f64, 3.1415));
// Same but fill more than 128 bits.
- testForT(16 * 2, @as(u8, 0xEE));
- testForT(8 * 2, @as(u16, 0xBEEF));
- testForT(4 * 2, @as(u32, 0xDEADBEEF));
- testForT(2 * 2, @as(u64, 0xCAFEF00DDEADBEEF));
-
- testForT(8 * 2, @as(f16, 3.1415));
- testForT(4 * 2, @as(f32, 3.1415));
- testForT(2 * 2, @as(f64, 3.1415));
+ try testForT(16 * 2, @as(u8, 0xEE));
+ try testForT(8 * 2, @as(u16, 0xBEEF));
+ try testForT(4 * 2, @as(u32, 0xDEADBEEF));
+ try testForT(2 * 2, @as(u64, 0xCAFEF00DDEADBEEF));
+
+ try testForT(8 * 2, @as(f16, 3.1415));
+ try testForT(4 * 2, @as(f32, 3.1415));
+ try testForT(2 * 2, @as(f64, 3.1415));
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "load vector elements via comptime index" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var v: Vector(4, i32) = [_]i32{ 1, 2, 3, undefined };
- expect(v[0] == 1);
- expect(v[1] == 2);
- expect(loadv(&v[2]) == 3);
+ try expect(v[0] == 1);
+ try expect(v[1] == 2);
+ try expect(loadv(&v[2]) == 3);
}
fn loadv(ptr: anytype) i32 {
return ptr.*;
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "store vector elements via comptime index" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var v: Vector(4, i32) = [_]i32{ 1, 5, 3, undefined };
v[2] = 42;
- expect(v[1] == 5);
+ try expect(v[1] == 5);
v[3] = -364;
- expect(v[2] == 42);
- expect(-364 == v[3]);
+ try expect(v[2] == 42);
+ try expect(-364 == v[3]);
storev(&v[0], 100);
- expect(v[0] == 100);
+ try expect(v[0] == 100);
}
fn storev(ptr: anytype, x: i32) void {
ptr.* = x;
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "load vector elements via runtime index" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var v: Vector(4, i32) = [_]i32{ 1, 2, 3, undefined };
var i: u32 = 0;
- expect(v[i] == 1);
+ try expect(v[i] == 1);
i += 1;
- expect(v[i] == 2);
+ try expect(v[i] == 2);
i += 1;
- expect(v[i] == 3);
+ try expect(v[i] == 3);
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "store vector elements via runtime index" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var v: Vector(4, i32) = [_]i32{ 1, 5, 3, undefined };
var i: u32 = 2;
v[i] = 1;
- expect(v[1] == 5);
- expect(v[2] == 1);
+ try expect(v[1] == 5);
+ try expect(v[2] == 1);
i += 1;
v[i] = -364;
- expect(-364 == v[3]);
+ try expect(-364 == v[3]);
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "initialize vector which is a struct field" {
@@ -263,155 +263,155 @@ test "initialize vector which is a struct field" {
};
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var foo = Vec4Obj{
.data = [_]f32{ 1, 2, 3, 4 },
};
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "vector comparison operators" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
{
const v1: Vector(4, bool) = [_]bool{ true, false, true, false };
const v2: Vector(4, bool) = [_]bool{ false, true, false, true };
- expectEqual(@splat(4, true), v1 == v1);
- expectEqual(@splat(4, false), v1 == v2);
- expectEqual(@splat(4, true), v1 != v2);
- expectEqual(@splat(4, false), v2 != v2);
+ try expectEqual(@splat(4, true), v1 == v1);
+ try expectEqual(@splat(4, false), v1 == v2);
+ try expectEqual(@splat(4, true), v1 != v2);
+ try expectEqual(@splat(4, false), v2 != v2);
}
{
const v1 = @splat(4, @as(u32, 0xc0ffeeee));
const v2: Vector(4, c_uint) = v1;
const v3 = @splat(4, @as(u32, 0xdeadbeef));
- expectEqual(@splat(4, true), v1 == v2);
- expectEqual(@splat(4, false), v1 == v3);
- expectEqual(@splat(4, true), v1 != v3);
- expectEqual(@splat(4, false), v1 != v2);
+ try expectEqual(@splat(4, true), v1 == v2);
+ try expectEqual(@splat(4, false), v1 == v3);
+ try expectEqual(@splat(4, true), v1 != v3);
+ try expectEqual(@splat(4, false), v1 != v2);
}
{
// Comptime-known LHS/RHS
var v1: @Vector(4, u32) = [_]u32{ 2, 1, 2, 1 };
const v2 = @splat(4, @as(u32, 2));
const v3: @Vector(4, bool) = [_]bool{ true, false, true, false };
- expectEqual(v3, v1 == v2);
- expectEqual(v3, v2 == v1);
+ try expectEqual(v3, v1 == v2);
+ try expectEqual(v3, v2 == v1);
}
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "vector division operators" {
const S = struct {
- fn doTheTestDiv(comptime T: type, x: Vector(4, T), y: Vector(4, T)) void {
+ fn doTheTestDiv(comptime T: type, x: Vector(4, T), y: Vector(4, T)) !void {
if (!comptime std.meta.trait.isSignedInt(T)) {
const d0 = x / y;
for (@as([4]T, d0)) |v, i| {
- expectEqual(x[i] / y[i], v);
+ try expectEqual(x[i] / y[i], v);
}
}
const d1 = @divExact(x, y);
for (@as([4]T, d1)) |v, i| {
- expectEqual(@divExact(x[i], y[i]), v);
+ try expectEqual(@divExact(x[i], y[i]), v);
}
const d2 = @divFloor(x, y);
for (@as([4]T, d2)) |v, i| {
- expectEqual(@divFloor(x[i], y[i]), v);
+ try expectEqual(@divFloor(x[i], y[i]), v);
}
const d3 = @divTrunc(x, y);
for (@as([4]T, d3)) |v, i| {
- expectEqual(@divTrunc(x[i], y[i]), v);
+ try expectEqual(@divTrunc(x[i], y[i]), v);
}
}
- fn doTheTestMod(comptime T: type, x: Vector(4, T), y: Vector(4, T)) void {
+ fn doTheTestMod(comptime T: type, x: Vector(4, T), y: Vector(4, T)) !void {
if ((!comptime std.meta.trait.isSignedInt(T)) and @typeInfo(T) != .Float) {
const r0 = x % y;
for (@as([4]T, r0)) |v, i| {
- expectEqual(x[i] % y[i], v);
+ try expectEqual(x[i] % y[i], v);
}
}
const r1 = @mod(x, y);
for (@as([4]T, r1)) |v, i| {
- expectEqual(@mod(x[i], y[i]), v);
+ try expectEqual(@mod(x[i], y[i]), v);
}
const r2 = @rem(x, y);
for (@as([4]T, r2)) |v, i| {
- expectEqual(@rem(x[i], y[i]), v);
+ try expectEqual(@rem(x[i], y[i]), v);
}
}
- fn doTheTest() void {
+ fn doTheTest() !void {
// https://github.com/ziglang/zig/issues/4952
if (std.builtin.os.tag != .windows) {
- doTheTestDiv(f16, [4]f16{ 4.0, -4.0, 4.0, -4.0 }, [4]f16{ 1.0, 2.0, -1.0, -2.0 });
+ try doTheTestDiv(f16, [4]f16{ 4.0, -4.0, 4.0, -4.0 }, [4]f16{ 1.0, 2.0, -1.0, -2.0 });
}
- doTheTestDiv(f32, [4]f32{ 4.0, -4.0, 4.0, -4.0 }, [4]f32{ 1.0, 2.0, -1.0, -2.0 });
- doTheTestDiv(f64, [4]f64{ 4.0, -4.0, 4.0, -4.0 }, [4]f64{ 1.0, 2.0, -1.0, -2.0 });
+ try doTheTestDiv(f32, [4]f32{ 4.0, -4.0, 4.0, -4.0 }, [4]f32{ 1.0, 2.0, -1.0, -2.0 });
+ try doTheTestDiv(f64, [4]f64{ 4.0, -4.0, 4.0, -4.0 }, [4]f64{ 1.0, 2.0, -1.0, -2.0 });
// https://github.com/ziglang/zig/issues/4952
if (std.builtin.os.tag != .windows) {
- doTheTestMod(f16, [4]f16{ 4.0, -4.0, 4.0, -4.0 }, [4]f16{ 1.0, 2.0, 0.5, 3.0 });
+ try doTheTestMod(f16, [4]f16{ 4.0, -4.0, 4.0, -4.0 }, [4]f16{ 1.0, 2.0, 0.5, 3.0 });
}
- doTheTestMod(f32, [4]f32{ 4.0, -4.0, 4.0, -4.0 }, [4]f32{ 1.0, 2.0, 0.5, 3.0 });
- doTheTestMod(f64, [4]f64{ 4.0, -4.0, 4.0, -4.0 }, [4]f64{ 1.0, 2.0, 0.5, 3.0 });
-
- doTheTestDiv(i8, [4]i8{ 4, -4, 4, -4 }, [4]i8{ 1, 2, -1, -2 });
- doTheTestDiv(i16, [4]i16{ 4, -4, 4, -4 }, [4]i16{ 1, 2, -1, -2 });
- doTheTestDiv(i32, [4]i32{ 4, -4, 4, -4 }, [4]i32{ 1, 2, -1, -2 });
- doTheTestDiv(i64, [4]i64{ 4, -4, 4, -4 }, [4]i64{ 1, 2, -1, -2 });
-
- doTheTestMod(i8, [4]i8{ 4, -4, 4, -4 }, [4]i8{ 1, 2, 4, 8 });
- doTheTestMod(i16, [4]i16{ 4, -4, 4, -4 }, [4]i16{ 1, 2, 4, 8 });
- doTheTestMod(i32, [4]i32{ 4, -4, 4, -4 }, [4]i32{ 1, 2, 4, 8 });
- doTheTestMod(i64, [4]i64{ 4, -4, 4, -4 }, [4]i64{ 1, 2, 4, 8 });
-
- doTheTestDiv(u8, [4]u8{ 1, 2, 4, 8 }, [4]u8{ 1, 1, 2, 4 });
- doTheTestDiv(u16, [4]u16{ 1, 2, 4, 8 }, [4]u16{ 1, 1, 2, 4 });
- doTheTestDiv(u32, [4]u32{ 1, 2, 4, 8 }, [4]u32{ 1, 1, 2, 4 });
- doTheTestDiv(u64, [4]u64{ 1, 2, 4, 8 }, [4]u64{ 1, 1, 2, 4 });
-
- doTheTestMod(u8, [4]u8{ 1, 2, 4, 8 }, [4]u8{ 1, 1, 2, 4 });
- doTheTestMod(u16, [4]u16{ 1, 2, 4, 8 }, [4]u16{ 1, 1, 2, 4 });
- doTheTestMod(u32, [4]u32{ 1, 2, 4, 8 }, [4]u32{ 1, 1, 2, 4 });
- doTheTestMod(u64, [4]u64{ 1, 2, 4, 8 }, [4]u64{ 1, 1, 2, 4 });
+ try doTheTestMod(f32, [4]f32{ 4.0, -4.0, 4.0, -4.0 }, [4]f32{ 1.0, 2.0, 0.5, 3.0 });
+ try doTheTestMod(f64, [4]f64{ 4.0, -4.0, 4.0, -4.0 }, [4]f64{ 1.0, 2.0, 0.5, 3.0 });
+
+ try doTheTestDiv(i8, [4]i8{ 4, -4, 4, -4 }, [4]i8{ 1, 2, -1, -2 });
+ try doTheTestDiv(i16, [4]i16{ 4, -4, 4, -4 }, [4]i16{ 1, 2, -1, -2 });
+ try doTheTestDiv(i32, [4]i32{ 4, -4, 4, -4 }, [4]i32{ 1, 2, -1, -2 });
+ try doTheTestDiv(i64, [4]i64{ 4, -4, 4, -4 }, [4]i64{ 1, 2, -1, -2 });
+
+ try doTheTestMod(i8, [4]i8{ 4, -4, 4, -4 }, [4]i8{ 1, 2, 4, 8 });
+ try doTheTestMod(i16, [4]i16{ 4, -4, 4, -4 }, [4]i16{ 1, 2, 4, 8 });
+ try doTheTestMod(i32, [4]i32{ 4, -4, 4, -4 }, [4]i32{ 1, 2, 4, 8 });
+ try doTheTestMod(i64, [4]i64{ 4, -4, 4, -4 }, [4]i64{ 1, 2, 4, 8 });
+
+ try doTheTestDiv(u8, [4]u8{ 1, 2, 4, 8 }, [4]u8{ 1, 1, 2, 4 });
+ try doTheTestDiv(u16, [4]u16{ 1, 2, 4, 8 }, [4]u16{ 1, 1, 2, 4 });
+ try doTheTestDiv(u32, [4]u32{ 1, 2, 4, 8 }, [4]u32{ 1, 1, 2, 4 });
+ try doTheTestDiv(u64, [4]u64{ 1, 2, 4, 8 }, [4]u64{ 1, 1, 2, 4 });
+
+ try doTheTestMod(u8, [4]u8{ 1, 2, 4, 8 }, [4]u8{ 1, 1, 2, 4 });
+ try doTheTestMod(u16, [4]u16{ 1, 2, 4, 8 }, [4]u16{ 1, 1, 2, 4 });
+ try doTheTestMod(u32, [4]u32{ 1, 2, 4, 8 }, [4]u32{ 1, 1, 2, 4 });
+ try doTheTestMod(u64, [4]u64{ 1, 2, 4, 8 }, [4]u64{ 1, 1, 2, 4 });
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "vector bitwise not operator" {
const S = struct {
- fn doTheTestNot(comptime T: type, x: Vector(4, T)) void {
+ fn doTheTestNot(comptime T: type, x: Vector(4, T)) !void {
var y = ~x;
for (@as([4]T, y)) |v, i| {
- expectEqual(~x[i], v);
+ try expectEqual(~x[i], v);
}
}
- fn doTheTest() void {
- doTheTestNot(u8, [_]u8{ 0, 2, 4, 255 });
- doTheTestNot(u16, [_]u16{ 0, 2, 4, 255 });
- doTheTestNot(u32, [_]u32{ 0, 2, 4, 255 });
- doTheTestNot(u64, [_]u64{ 0, 2, 4, 255 });
-
- doTheTestNot(u8, [_]u8{ 0, 2, 4, 255 });
- doTheTestNot(u16, [_]u16{ 0, 2, 4, 255 });
- doTheTestNot(u32, [_]u32{ 0, 2, 4, 255 });
- doTheTestNot(u64, [_]u64{ 0, 2, 4, 255 });
+ fn doTheTest() !void {
+ try doTheTestNot(u8, [_]u8{ 0, 2, 4, 255 });
+ try doTheTestNot(u16, [_]u16{ 0, 2, 4, 255 });
+ try doTheTestNot(u32, [_]u32{ 0, 2, 4, 255 });
+ try doTheTestNot(u64, [_]u64{ 0, 2, 4, 255 });
+
+ try doTheTestNot(u8, [_]u8{ 0, 2, 4, 255 });
+ try doTheTestNot(u16, [_]u16{ 0, 2, 4, 255 });
+ try doTheTestNot(u32, [_]u32{ 0, 2, 4, 255 });
+ try doTheTestNot(u64, [_]u64{ 0, 2, 4, 255 });
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "vector shift operators" {
@@ -419,7 +419,7 @@ test "vector shift operators" {
if (builtin.os.tag == .wasi) return error.SkipZigTest;
const S = struct {
- fn doTheTestShift(x: anytype, y: anytype) void {
+ fn doTheTestShift(x: anytype, y: anytype) !void {
const N = @typeInfo(@TypeOf(x)).Array.len;
const TX = @typeInfo(@TypeOf(x)).Array.child;
const TY = @typeInfo(@TypeOf(y)).Array.child;
@@ -429,14 +429,14 @@ test "vector shift operators" {
var z0 = xv >> yv;
for (@as([N]TX, z0)) |v, i| {
- expectEqual(x[i] >> y[i], v);
+ try expectEqual(x[i] >> y[i], v);
}
var z1 = xv << yv;
for (@as([N]TX, z1)) |v, i| {
- expectEqual(x[i] << y[i], v);
+ try expectEqual(x[i] << y[i], v);
}
}
- fn doTheTestShiftExact(x: anytype, y: anytype, dir: enum { Left, Right }) void {
+ fn doTheTestShiftExact(x: anytype, y: anytype, dir: enum { Left, Right }) !void {
const N = @typeInfo(@TypeOf(x)).Array.len;
const TX = @typeInfo(@TypeOf(x)).Array.child;
const TY = @typeInfo(@TypeOf(y)).Array.child;
@@ -447,33 +447,33 @@ test "vector shift operators" {
var z = if (dir == .Left) @shlExact(xv, yv) else @shrExact(xv, yv);
for (@as([N]TX, z)) |v, i| {
const check = if (dir == .Left) x[i] << y[i] else x[i] >> y[i];
- expectEqual(check, v);
+ try expectEqual(check, v);
}
}
- fn doTheTest() void {
- doTheTestShift([_]u8{ 0, 2, 4, math.maxInt(u8) }, [_]u3{ 2, 0, 2, 7 });
- doTheTestShift([_]u16{ 0, 2, 4, math.maxInt(u16) }, [_]u4{ 2, 0, 2, 15 });
- doTheTestShift([_]u24{ 0, 2, 4, math.maxInt(u24) }, [_]u5{ 2, 0, 2, 23 });
- doTheTestShift([_]u32{ 0, 2, 4, math.maxInt(u32) }, [_]u5{ 2, 0, 2, 31 });
- doTheTestShift([_]u64{ 0xfe, math.maxInt(u64) }, [_]u6{ 0, 63 });
-
- doTheTestShift([_]i8{ 0, 2, 4, math.maxInt(i8) }, [_]u3{ 2, 0, 2, 7 });
- doTheTestShift([_]i16{ 0, 2, 4, math.maxInt(i16) }, [_]u4{ 2, 0, 2, 7 });
- doTheTestShift([_]i24{ 0, 2, 4, math.maxInt(i24) }, [_]u5{ 2, 0, 2, 7 });
- doTheTestShift([_]i32{ 0, 2, 4, math.maxInt(i32) }, [_]u5{ 2, 0, 2, 7 });
- doTheTestShift([_]i64{ 0xfe, math.maxInt(i64) }, [_]u6{ 0, 63 });
-
- doTheTestShiftExact([_]u8{ 0, 1, 1 << 7, math.maxInt(u8) ^ 1 }, [_]u3{ 4, 0, 7, 1 }, .Right);
- doTheTestShiftExact([_]u16{ 0, 1, 1 << 15, math.maxInt(u16) ^ 1 }, [_]u4{ 4, 0, 15, 1 }, .Right);
- doTheTestShiftExact([_]u24{ 0, 1, 1 << 23, math.maxInt(u24) ^ 1 }, [_]u5{ 4, 0, 23, 1 }, .Right);
- doTheTestShiftExact([_]u32{ 0, 1, 1 << 31, math.maxInt(u32) ^ 1 }, [_]u5{ 4, 0, 31, 1 }, .Right);
- doTheTestShiftExact([_]u64{ 1 << 63, 1 }, [_]u6{ 63, 0 }, .Right);
-
- doTheTestShiftExact([_]u8{ 0, 1, 1, math.maxInt(u8) ^ (1 << 7) }, [_]u3{ 4, 0, 7, 1 }, .Left);
- doTheTestShiftExact([_]u16{ 0, 1, 1, math.maxInt(u16) ^ (1 << 15) }, [_]u4{ 4, 0, 15, 1 }, .Left);
- doTheTestShiftExact([_]u24{ 0, 1, 1, math.maxInt(u24) ^ (1 << 23) }, [_]u5{ 4, 0, 23, 1 }, .Left);
- doTheTestShiftExact([_]u32{ 0, 1, 1, math.maxInt(u32) ^ (1 << 31) }, [_]u5{ 4, 0, 31, 1 }, .Left);
- doTheTestShiftExact([_]u64{ 1 << 63, 1 }, [_]u6{ 0, 63 }, .Left);
+ fn doTheTest() !void {
+ try doTheTestShift([_]u8{ 0, 2, 4, math.maxInt(u8) }, [_]u3{ 2, 0, 2, 7 });
+ try doTheTestShift([_]u16{ 0, 2, 4, math.maxInt(u16) }, [_]u4{ 2, 0, 2, 15 });
+ try doTheTestShift([_]u24{ 0, 2, 4, math.maxInt(u24) }, [_]u5{ 2, 0, 2, 23 });
+ try doTheTestShift([_]u32{ 0, 2, 4, math.maxInt(u32) }, [_]u5{ 2, 0, 2, 31 });
+ try doTheTestShift([_]u64{ 0xfe, math.maxInt(u64) }, [_]u6{ 0, 63 });
+
+ try doTheTestShift([_]i8{ 0, 2, 4, math.maxInt(i8) }, [_]u3{ 2, 0, 2, 7 });
+ try doTheTestShift([_]i16{ 0, 2, 4, math.maxInt(i16) }, [_]u4{ 2, 0, 2, 7 });
+ try doTheTestShift([_]i24{ 0, 2, 4, math.maxInt(i24) }, [_]u5{ 2, 0, 2, 7 });
+ try doTheTestShift([_]i32{ 0, 2, 4, math.maxInt(i32) }, [_]u5{ 2, 0, 2, 7 });
+ try doTheTestShift([_]i64{ 0xfe, math.maxInt(i64) }, [_]u6{ 0, 63 });
+
+ try doTheTestShiftExact([_]u8{ 0, 1, 1 << 7, math.maxInt(u8) ^ 1 }, [_]u3{ 4, 0, 7, 1 }, .Right);
+ try doTheTestShiftExact([_]u16{ 0, 1, 1 << 15, math.maxInt(u16) ^ 1 }, [_]u4{ 4, 0, 15, 1 }, .Right);
+ try doTheTestShiftExact([_]u24{ 0, 1, 1 << 23, math.maxInt(u24) ^ 1 }, [_]u5{ 4, 0, 23, 1 }, .Right);
+ try doTheTestShiftExact([_]u32{ 0, 1, 1 << 31, math.maxInt(u32) ^ 1 }, [_]u5{ 4, 0, 31, 1 }, .Right);
+ try doTheTestShiftExact([_]u64{ 1 << 63, 1 }, [_]u6{ 63, 0 }, .Right);
+
+ try doTheTestShiftExact([_]u8{ 0, 1, 1, math.maxInt(u8) ^ (1 << 7) }, [_]u3{ 4, 0, 7, 1 }, .Left);
+ try doTheTestShiftExact([_]u16{ 0, 1, 1, math.maxInt(u16) ^ (1 << 15) }, [_]u4{ 4, 0, 15, 1 }, .Left);
+ try doTheTestShiftExact([_]u24{ 0, 1, 1, math.maxInt(u24) ^ (1 << 23) }, [_]u5{ 4, 0, 23, 1 }, .Left);
+ try doTheTestShiftExact([_]u32{ 0, 1, 1, math.maxInt(u32) ^ (1 << 31) }, [_]u5{ 4, 0, 31, 1 }, .Left);
+ try doTheTestShiftExact([_]u64{ 1 << 63, 1 }, [_]u6{ 0, 63 }, .Left);
}
};
@@ -500,19 +500,19 @@ test "vector shift operators" {
else => {},
}
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test "vector reduce operation" {
const S = struct {
- fn doTheTestReduce(comptime op: builtin.ReduceOp, x: anytype, expected: anytype) void {
+ fn doTheTestReduce(comptime op: builtin.ReduceOp, x: anytype, expected: anytype) !void {
const N = @typeInfo(@TypeOf(x)).Array.len;
const TX = @typeInfo(@TypeOf(x)).Array.child;
var r = @reduce(op, @as(Vector(N, TX), x));
switch (@typeInfo(TX)) {
- .Int, .Bool => expectEqual(expected, r),
+ .Int, .Bool => try expectEqual(expected, r),
.Float => {
const expected_nan = math.isNan(expected);
const got_nan = math.isNan(r);
@@ -521,120 +521,120 @@ test "vector reduce operation" {
// Do this check explicitly as two NaN values are never
// equal.
} else {
- expectApproxEqRel(expected, r, math.sqrt(math.epsilon(TX)));
+ try expectApproxEqRel(expected, r, math.sqrt(math.epsilon(TX)));
}
},
else => unreachable,
}
}
- fn doTheTest() void {
- doTheTestReduce(.Add, [4]i16{ -9, -99, -999, -9999 }, @as(i32, -11106));
- doTheTestReduce(.Add, [4]u16{ 9, 99, 999, 9999 }, @as(u32, 11106));
- doTheTestReduce(.Add, [4]i32{ -9, -99, -999, -9999 }, @as(i32, -11106));
- doTheTestReduce(.Add, [4]u32{ 9, 99, 999, 9999 }, @as(u32, 11106));
- doTheTestReduce(.Add, [4]i64{ -9, -99, -999, -9999 }, @as(i64, -11106));
- doTheTestReduce(.Add, [4]u64{ 9, 99, 999, 9999 }, @as(u64, 11106));
- doTheTestReduce(.Add, [4]i128{ -9, -99, -999, -9999 }, @as(i128, -11106));
- doTheTestReduce(.Add, [4]u128{ 9, 99, 999, 9999 }, @as(u128, 11106));
- doTheTestReduce(.Add, [4]f16{ -1.9, 5.1, -60.3, 100.0 }, @as(f16, 42.9));
- doTheTestReduce(.Add, [4]f32{ -1.9, 5.1, -60.3, 100.0 }, @as(f32, 42.9));
- doTheTestReduce(.Add, [4]f64{ -1.9, 5.1, -60.3, 100.0 }, @as(f64, 42.9));
-
- doTheTestReduce(.And, [4]bool{ true, false, true, true }, @as(bool, false));
- doTheTestReduce(.And, [4]u1{ 1, 0, 1, 1 }, @as(u1, 0));
- doTheTestReduce(.And, [4]u16{ 0xffff, 0xff55, 0xaaff, 0x1010 }, @as(u16, 0x10));
- doTheTestReduce(.And, [4]u32{ 0xffffffff, 0xffff5555, 0xaaaaffff, 0x10101010 }, @as(u32, 0x1010));
- doTheTestReduce(.And, [4]u64{ 0xffffffff, 0xffff5555, 0xaaaaffff, 0x10101010 }, @as(u64, 0x1010));
-
- doTheTestReduce(.Min, [4]i16{ -1, 2, 3, 4 }, @as(i16, -1));
- doTheTestReduce(.Min, [4]u16{ 1, 2, 3, 4 }, @as(u16, 1));
- doTheTestReduce(.Min, [4]i32{ 1234567, -386, 0, 3 }, @as(i32, -386));
- doTheTestReduce(.Min, [4]u32{ 99, 9999, 9, 99999 }, @as(u32, 9));
+ fn doTheTest() !void {
+ try doTheTestReduce(.Add, [4]i16{ -9, -99, -999, -9999 }, @as(i32, -11106));
+ try doTheTestReduce(.Add, [4]u16{ 9, 99, 999, 9999 }, @as(u32, 11106));
+ try doTheTestReduce(.Add, [4]i32{ -9, -99, -999, -9999 }, @as(i32, -11106));
+ try doTheTestReduce(.Add, [4]u32{ 9, 99, 999, 9999 }, @as(u32, 11106));
+ try doTheTestReduce(.Add, [4]i64{ -9, -99, -999, -9999 }, @as(i64, -11106));
+ try doTheTestReduce(.Add, [4]u64{ 9, 99, 999, 9999 }, @as(u64, 11106));
+ try doTheTestReduce(.Add, [4]i128{ -9, -99, -999, -9999 }, @as(i128, -11106));
+ try doTheTestReduce(.Add, [4]u128{ 9, 99, 999, 9999 }, @as(u128, 11106));
+ try doTheTestReduce(.Add, [4]f16{ -1.9, 5.1, -60.3, 100.0 }, @as(f16, 42.9));
+ try doTheTestReduce(.Add, [4]f32{ -1.9, 5.1, -60.3, 100.0 }, @as(f32, 42.9));
+ try doTheTestReduce(.Add, [4]f64{ -1.9, 5.1, -60.3, 100.0 }, @as(f64, 42.9));
+
+ try doTheTestReduce(.And, [4]bool{ true, false, true, true }, @as(bool, false));
+ try doTheTestReduce(.And, [4]u1{ 1, 0, 1, 1 }, @as(u1, 0));
+ try doTheTestReduce(.And, [4]u16{ 0xffff, 0xff55, 0xaaff, 0x1010 }, @as(u16, 0x10));
+ try doTheTestReduce(.And, [4]u32{ 0xffffffff, 0xffff5555, 0xaaaaffff, 0x10101010 }, @as(u32, 0x1010));
+ try doTheTestReduce(.And, [4]u64{ 0xffffffff, 0xffff5555, 0xaaaaffff, 0x10101010 }, @as(u64, 0x1010));
+
+ try doTheTestReduce(.Min, [4]i16{ -1, 2, 3, 4 }, @as(i16, -1));
+ try doTheTestReduce(.Min, [4]u16{ 1, 2, 3, 4 }, @as(u16, 1));
+ try doTheTestReduce(.Min, [4]i32{ 1234567, -386, 0, 3 }, @as(i32, -386));
+ try doTheTestReduce(.Min, [4]u32{ 99, 9999, 9, 99999 }, @as(u32, 9));
// LLVM 11 ERROR: Cannot select type
// https://github.com/ziglang/zig/issues/7138
if (std.builtin.arch != .aarch64) {
- doTheTestReduce(.Min, [4]i64{ 1234567, -386, 0, 3 }, @as(i64, -386));
- doTheTestReduce(.Min, [4]u64{ 99, 9999, 9, 99999 }, @as(u64, 9));
+ try doTheTestReduce(.Min, [4]i64{ 1234567, -386, 0, 3 }, @as(i64, -386));
+ try doTheTestReduce(.Min, [4]u64{ 99, 9999, 9, 99999 }, @as(u64, 9));
}
- doTheTestReduce(.Min, [4]i128{ 1234567, -386, 0, 3 }, @as(i128, -386));
- doTheTestReduce(.Min, [4]u128{ 99, 9999, 9, 99999 }, @as(u128, 9));
- doTheTestReduce(.Min, [4]f16{ -10.3, 10.0e9, 13.0, -100.0 }, @as(f16, -100.0));
- doTheTestReduce(.Min, [4]f32{ -10.3, 10.0e9, 13.0, -100.0 }, @as(f32, -100.0));
- doTheTestReduce(.Min, [4]f64{ -10.3, 10.0e9, 13.0, -100.0 }, @as(f64, -100.0));
+ try doTheTestReduce(.Min, [4]i128{ 1234567, -386, 0, 3 }, @as(i128, -386));
+ try doTheTestReduce(.Min, [4]u128{ 99, 9999, 9, 99999 }, @as(u128, 9));
+ try doTheTestReduce(.Min, [4]f16{ -10.3, 10.0e9, 13.0, -100.0 }, @as(f16, -100.0));
+ try doTheTestReduce(.Min, [4]f32{ -10.3, 10.0e9, 13.0, -100.0 }, @as(f32, -100.0));
+ try doTheTestReduce(.Min, [4]f64{ -10.3, 10.0e9, 13.0, -100.0 }, @as(f64, -100.0));
- doTheTestReduce(.Max, [4]i16{ -1, 2, 3, 4 }, @as(i16, 4));
- doTheTestReduce(.Max, [4]u16{ 1, 2, 3, 4 }, @as(u16, 4));
- doTheTestReduce(.Max, [4]i32{ 1234567, -386, 0, 3 }, @as(i32, 1234567));
- doTheTestReduce(.Max, [4]u32{ 99, 9999, 9, 99999 }, @as(u32, 99999));
+ try doTheTestReduce(.Max, [4]i16{ -1, 2, 3, 4 }, @as(i16, 4));
+ try doTheTestReduce(.Max, [4]u16{ 1, 2, 3, 4 }, @as(u16, 4));
+ try doTheTestReduce(.Max, [4]i32{ 1234567, -386, 0, 3 }, @as(i32, 1234567));
+ try doTheTestReduce(.Max, [4]u32{ 99, 9999, 9, 99999 }, @as(u32, 99999));
// LLVM 11 ERROR: Cannot select type
// https://github.com/ziglang/zig/issues/7138
if (std.builtin.arch != .aarch64) {
- doTheTestReduce(.Max, [4]i64{ 1234567, -386, 0, 3 }, @as(i64, 1234567));
- doTheTestReduce(.Max, [4]u64{ 99, 9999, 9, 99999 }, @as(u64, 99999));
+ try doTheTestReduce(.Max, [4]i64{ 1234567, -386, 0, 3 }, @as(i64, 1234567));
+ try doTheTestReduce(.Max, [4]u64{ 99, 9999, 9, 99999 }, @as(u64, 99999));
}
- doTheTestReduce(.Max, [4]i128{ 1234567, -386, 0, 3 }, @as(i128, 1234567));
- doTheTestReduce(.Max, [4]u128{ 99, 9999, 9, 99999 }, @as(u128, 99999));
- doTheTestReduce(.Max, [4]f16{ -10.3, 10.0e9, 13.0, -100.0 }, @as(f16, 10.0e9));
- doTheTestReduce(.Max, [4]f32{ -10.3, 10.0e9, 13.0, -100.0 }, @as(f32, 10.0e9));
- doTheTestReduce(.Max, [4]f64{ -10.3, 10.0e9, 13.0, -100.0 }, @as(f64, 10.0e9));
-
- doTheTestReduce(.Mul, [4]i16{ -1, 2, 3, 4 }, @as(i16, -24));
- doTheTestReduce(.Mul, [4]u16{ 1, 2, 3, 4 }, @as(u16, 24));
- doTheTestReduce(.Mul, [4]i32{ -9, -99, -999, 999 }, @as(i32, -889218891));
- doTheTestReduce(.Mul, [4]u32{ 1, 2, 3, 4 }, @as(u32, 24));
- doTheTestReduce(.Mul, [4]i64{ 9, 99, 999, 9999 }, @as(i64, 8900199891));
- doTheTestReduce(.Mul, [4]u64{ 9, 99, 999, 9999 }, @as(u64, 8900199891));
- doTheTestReduce(.Mul, [4]i128{ -9, -99, -999, 9999 }, @as(i128, -8900199891));
- doTheTestReduce(.Mul, [4]u128{ 9, 99, 999, 9999 }, @as(u128, 8900199891));
- doTheTestReduce(.Mul, [4]f16{ -1.9, 5.1, -60.3, 100.0 }, @as(f16, 58430.7));
- doTheTestReduce(.Mul, [4]f32{ -1.9, 5.1, -60.3, 100.0 }, @as(f32, 58430.7));
- doTheTestReduce(.Mul, [4]f64{ -1.9, 5.1, -60.3, 100.0 }, @as(f64, 58430.7));
-
- doTheTestReduce(.Or, [4]bool{ false, true, false, false }, @as(bool, true));
- doTheTestReduce(.Or, [4]u1{ 0, 1, 0, 0 }, @as(u1, 1));
- doTheTestReduce(.Or, [4]u16{ 0xff00, 0xff00, 0xf0, 0xf }, ~@as(u16, 0));
- doTheTestReduce(.Or, [4]u32{ 0xffff0000, 0xff00, 0xf0, 0xf }, ~@as(u32, 0));
- doTheTestReduce(.Or, [4]u64{ 0xffff0000, 0xff00, 0xf0, 0xf }, @as(u64, 0xffffffff));
- doTheTestReduce(.Or, [4]u128{ 0xffff0000, 0xff00, 0xf0, 0xf }, @as(u128, 0xffffffff));
-
- doTheTestReduce(.Xor, [4]bool{ true, true, true, false }, @as(bool, true));
- doTheTestReduce(.Xor, [4]u1{ 1, 1, 1, 0 }, @as(u1, 1));
- doTheTestReduce(.Xor, [4]u16{ 0x0000, 0x3333, 0x8888, 0x4444 }, ~@as(u16, 0));
- doTheTestReduce(.Xor, [4]u32{ 0x00000000, 0x33333333, 0x88888888, 0x44444444 }, ~@as(u32, 0));
- doTheTestReduce(.Xor, [4]u64{ 0x00000000, 0x33333333, 0x88888888, 0x44444444 }, @as(u64, 0xffffffff));
- doTheTestReduce(.Xor, [4]u128{ 0x00000000, 0x33333333, 0x88888888, 0x44444444 }, @as(u128, 0xffffffff));
+ try doTheTestReduce(.Max, [4]i128{ 1234567, -386, 0, 3 }, @as(i128, 1234567));
+ try doTheTestReduce(.Max, [4]u128{ 99, 9999, 9, 99999 }, @as(u128, 99999));
+ try doTheTestReduce(.Max, [4]f16{ -10.3, 10.0e9, 13.0, -100.0 }, @as(f16, 10.0e9));
+ try doTheTestReduce(.Max, [4]f32{ -10.3, 10.0e9, 13.0, -100.0 }, @as(f32, 10.0e9));
+ try doTheTestReduce(.Max, [4]f64{ -10.3, 10.0e9, 13.0, -100.0 }, @as(f64, 10.0e9));
+
+ try doTheTestReduce(.Mul, [4]i16{ -1, 2, 3, 4 }, @as(i16, -24));
+ try doTheTestReduce(.Mul, [4]u16{ 1, 2, 3, 4 }, @as(u16, 24));
+ try doTheTestReduce(.Mul, [4]i32{ -9, -99, -999, 999 }, @as(i32, -889218891));
+ try doTheTestReduce(.Mul, [4]u32{ 1, 2, 3, 4 }, @as(u32, 24));
+ try doTheTestReduce(.Mul, [4]i64{ 9, 99, 999, 9999 }, @as(i64, 8900199891));
+ try doTheTestReduce(.Mul, [4]u64{ 9, 99, 999, 9999 }, @as(u64, 8900199891));
+ try doTheTestReduce(.Mul, [4]i128{ -9, -99, -999, 9999 }, @as(i128, -8900199891));
+ try doTheTestReduce(.Mul, [4]u128{ 9, 99, 999, 9999 }, @as(u128, 8900199891));
+ try doTheTestReduce(.Mul, [4]f16{ -1.9, 5.1, -60.3, 100.0 }, @as(f16, 58430.7));
+ try doTheTestReduce(.Mul, [4]f32{ -1.9, 5.1, -60.3, 100.0 }, @as(f32, 58430.7));
+ try doTheTestReduce(.Mul, [4]f64{ -1.9, 5.1, -60.3, 100.0 }, @as(f64, 58430.7));
+
+ try doTheTestReduce(.Or, [4]bool{ false, true, false, false }, @as(bool, true));
+ try doTheTestReduce(.Or, [4]u1{ 0, 1, 0, 0 }, @as(u1, 1));
+ try doTheTestReduce(.Or, [4]u16{ 0xff00, 0xff00, 0xf0, 0xf }, ~@as(u16, 0));
+ try doTheTestReduce(.Or, [4]u32{ 0xffff0000, 0xff00, 0xf0, 0xf }, ~@as(u32, 0));
+ try doTheTestReduce(.Or, [4]u64{ 0xffff0000, 0xff00, 0xf0, 0xf }, @as(u64, 0xffffffff));
+ try doTheTestReduce(.Or, [4]u128{ 0xffff0000, 0xff00, 0xf0, 0xf }, @as(u128, 0xffffffff));
+
+ try doTheTestReduce(.Xor, [4]bool{ true, true, true, false }, @as(bool, true));
+ try doTheTestReduce(.Xor, [4]u1{ 1, 1, 1, 0 }, @as(u1, 1));
+ try doTheTestReduce(.Xor, [4]u16{ 0x0000, 0x3333, 0x8888, 0x4444 }, ~@as(u16, 0));
+ try doTheTestReduce(.Xor, [4]u32{ 0x00000000, 0x33333333, 0x88888888, 0x44444444 }, ~@as(u32, 0));
+ try doTheTestReduce(.Xor, [4]u64{ 0x00000000, 0x33333333, 0x88888888, 0x44444444 }, @as(u64, 0xffffffff));
+ try doTheTestReduce(.Xor, [4]u128{ 0x00000000, 0x33333333, 0x88888888, 0x44444444 }, @as(u128, 0xffffffff));
// Test the reduction on vectors containing NaNs.
const f16_nan = math.nan(f16);
const f32_nan = math.nan(f32);
const f64_nan = math.nan(f64);
- doTheTestReduce(.Add, [4]f16{ -1.9, 5.1, f16_nan, 100.0 }, f16_nan);
- doTheTestReduce(.Add, [4]f32{ -1.9, 5.1, f32_nan, 100.0 }, f32_nan);
- doTheTestReduce(.Add, [4]f64{ -1.9, 5.1, f64_nan, 100.0 }, f64_nan);
+ try doTheTestReduce(.Add, [4]f16{ -1.9, 5.1, f16_nan, 100.0 }, f16_nan);
+ try doTheTestReduce(.Add, [4]f32{ -1.9, 5.1, f32_nan, 100.0 }, f32_nan);
+ try doTheTestReduce(.Add, [4]f64{ -1.9, 5.1, f64_nan, 100.0 }, f64_nan);
// LLVM 11 ERROR: Cannot select type
// https://github.com/ziglang/zig/issues/7138
if (false) {
- doTheTestReduce(.Min, [4]f16{ -1.9, 5.1, f16_nan, 100.0 }, f16_nan);
- doTheTestReduce(.Min, [4]f32{ -1.9, 5.1, f32_nan, 100.0 }, f32_nan);
- doTheTestReduce(.Min, [4]f64{ -1.9, 5.1, f64_nan, 100.0 }, f64_nan);
+ try doTheTestReduce(.Min, [4]f16{ -1.9, 5.1, f16_nan, 100.0 }, f16_nan);
+ try doTheTestReduce(.Min, [4]f32{ -1.9, 5.1, f32_nan, 100.0 }, f32_nan);
+ try doTheTestReduce(.Min, [4]f64{ -1.9, 5.1, f64_nan, 100.0 }, f64_nan);
- doTheTestReduce(.Max, [4]f16{ -1.9, 5.1, f16_nan, 100.0 }, f16_nan);
- doTheTestReduce(.Max, [4]f32{ -1.9, 5.1, f32_nan, 100.0 }, f32_nan);
- doTheTestReduce(.Max, [4]f64{ -1.9, 5.1, f64_nan, 100.0 }, f64_nan);
+ try doTheTestReduce(.Max, [4]f16{ -1.9, 5.1, f16_nan, 100.0 }, f16_nan);
+ try doTheTestReduce(.Max, [4]f32{ -1.9, 5.1, f32_nan, 100.0 }, f32_nan);
+ try doTheTestReduce(.Max, [4]f64{ -1.9, 5.1, f64_nan, 100.0 }, f64_nan);
}
- doTheTestReduce(.Mul, [4]f16{ -1.9, 5.1, f16_nan, 100.0 }, f16_nan);
- doTheTestReduce(.Mul, [4]f32{ -1.9, 5.1, f32_nan, 100.0 }, f32_nan);
- doTheTestReduce(.Mul, [4]f64{ -1.9, 5.1, f64_nan, 100.0 }, f64_nan);
+ try doTheTestReduce(.Mul, [4]f16{ -1.9, 5.1, f16_nan, 100.0 }, f16_nan);
+ try doTheTestReduce(.Mul, [4]f32{ -1.9, 5.1, f32_nan, 100.0 }, f32_nan);
+ try doTheTestReduce(.Mul, [4]f64{ -1.9, 5.1, f64_nan, 100.0 }, f64_nan);
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test/stage1/behavior/void.zig
@@ -13,14 +13,14 @@ test "compare void with void compile time known" {
.b = 1,
.c = {},
};
- expect(foo.a == {});
+ try expect(foo.a == {});
}
}
test "iterate over a void slice" {
var j: usize = 0;
for (times(10)) |_, i| {
- expect(i == j);
+ try expect(i == j);
j += 1;
}
}
@@ -31,7 +31,7 @@ fn times(n: usize) []const void {
test "void optional" {
var x: ?void = {};
- expect(x != null);
+ try expect(x != null);
}
test "void array as a local variable initializer" {
test/stage1/behavior/wasm.zig
@@ -3,6 +3,6 @@ const expect = std.testing.expect;
test "memory size and grow" {
var prev = @wasmMemorySize(0);
- expect(prev == @wasmMemoryGrow(0, 1));
- expect(prev + 1 == @wasmMemorySize(0));
+ try expect(prev == @wasmMemoryGrow(0, 1));
+ try expect(prev + 1 == @wasmMemorySize(0));
}
test/stage1/behavior/while.zig
@@ -6,8 +6,8 @@ test "while loop" {
while (i < 4) {
i += 1;
}
- expect(i == 4);
- expect(whileLoop1() == 1);
+ try expect(i == 4);
+ try expect(whileLoop1() == 1);
}
fn whileLoop1() i32 {
return whileLoop2();
@@ -19,7 +19,7 @@ fn whileLoop2() i32 {
}
test "static eval while" {
- expect(static_eval_while_number == 1);
+ try expect(static_eval_while_number == 1);
}
const static_eval_while_number = staticWhileLoop1();
fn staticWhileLoop1() i32 {
@@ -32,11 +32,11 @@ fn staticWhileLoop2() i32 {
}
test "continue and break" {
- runContinueAndBreakTest();
- expect(continue_and_break_counter == 8);
+ try runContinueAndBreakTest();
+ try expect(continue_and_break_counter == 8);
}
var continue_and_break_counter: i32 = 0;
-fn runContinueAndBreakTest() void {
+fn runContinueAndBreakTest() !void {
var i: i32 = 0;
while (true) {
continue_and_break_counter += 2;
@@ -46,7 +46,7 @@ fn runContinueAndBreakTest() void {
}
break;
}
- expect(i == 4);
+ try expect(i == 4);
}
test "return with implicit cast from while loop" {
@@ -67,7 +67,7 @@ test "while with continue expression" {
sum += i;
}
}
- expect(sum == 40);
+ try expect(sum == 40);
}
test "while with else" {
@@ -79,8 +79,8 @@ test "while with else" {
} else {
got_else += 1;
}
- expect(sum == 10);
- expect(got_else == 1);
+ try expect(sum == 10);
+ try expect(got_else == 1);
}
test "while with optional as condition" {
@@ -89,7 +89,7 @@ test "while with optional as condition" {
while (getNumberOrNull()) |value| {
sum += value;
}
- expect(sum == 45);
+ try expect(sum == 45);
}
test "while with optional as condition with else" {
@@ -98,12 +98,12 @@ test "while with optional as condition with else" {
var got_else: i32 = 0;
while (getNumberOrNull()) |value| {
sum += value;
- expect(got_else == 0);
+ try expect(got_else == 0);
} else {
got_else += 1;
}
- expect(sum == 45);
- expect(got_else == 1);
+ try expect(sum == 45);
+ try expect(got_else == 1);
}
test "while with error union condition" {
@@ -113,11 +113,11 @@ test "while with error union condition" {
while (getNumberOrErr()) |value| {
sum += value;
} else |err| {
- expect(err == error.OutOfNumbers);
+ try expect(err == error.OutOfNumbers);
got_else += 1;
}
- expect(sum == 45);
- expect(got_else == 1);
+ try expect(sum == 45);
+ try expect(got_else == 1);
}
var numbers_left: i32 = undefined;
@@ -137,49 +137,43 @@ fn getNumberOrNull() ?i32 {
test "while on optional with else result follow else prong" {
const result = while (returnNull()) |value| {
break value;
- } else
- @as(i32, 2);
- expect(result == 2);
+ } else @as(i32, 2);
+ try expect(result == 2);
}
test "while on optional with else result follow break prong" {
const result = while (returnOptional(10)) |value| {
break value;
- } else
- @as(i32, 2);
- expect(result == 10);
+ } else @as(i32, 2);
+ try expect(result == 10);
}
test "while on error union with else result follow else prong" {
const result = while (returnError()) |value| {
break value;
- } else |err|
- @as(i32, 2);
- expect(result == 2);
+ } else |err| @as(i32, 2);
+ try expect(result == 2);
}
test "while on error union with else result follow break prong" {
const result = while (returnSuccess(10)) |value| {
break value;
- } else |err|
- @as(i32, 2);
- expect(result == 10);
+ } else |err| @as(i32, 2);
+ try expect(result == 10);
}
test "while on bool with else result follow else prong" {
const result = while (returnFalse()) {
break @as(i32, 10);
- } else
- @as(i32, 2);
- expect(result == 2);
+ } else @as(i32, 2);
+ try expect(result == 2);
}
test "while on bool with else result follow break prong" {
const result = while (returnTrue()) {
break @as(i32, 10);
- } else
- @as(i32, 2);
- expect(result == 10);
+ } else @as(i32, 2);
+ try expect(result == 10);
}
test "break from outer while loop" {
@@ -230,60 +224,60 @@ fn returnTrue() bool {
test "while bool 2 break statements and an else" {
const S = struct {
- fn entry(t: bool, f: bool) void {
+ fn entry(t: bool, f: bool) !void {
var ok = false;
ok = while (t) {
if (f) break false;
if (t) break true;
} else false;
- expect(ok);
+ try expect(ok);
}
};
- S.entry(true, false);
- comptime S.entry(true, false);
+ try S.entry(true, false);
+ comptime try S.entry(true, false);
}
test "while optional 2 break statements and an else" {
const S = struct {
- fn entry(opt_t: ?bool, f: bool) void {
+ fn entry(opt_t: ?bool, f: bool) !void {
var ok = false;
ok = while (opt_t) |t| {
if (f) break false;
if (t) break true;
} else false;
- expect(ok);
+ try expect(ok);
}
};
- S.entry(true, false);
- comptime S.entry(true, false);
+ try S.entry(true, false);
+ comptime try S.entry(true, false);
}
test "while error 2 break statements and an else" {
const S = struct {
- fn entry(opt_t: anyerror!bool, f: bool) void {
+ fn entry(opt_t: anyerror!bool, f: bool) !void {
var ok = false;
ok = while (opt_t) |t| {
if (f) break false;
if (t) break true;
} else |_| false;
- expect(ok);
+ try expect(ok);
}
};
- S.entry(true, false);
- comptime S.entry(true, false);
+ try S.entry(true, false);
+ comptime try S.entry(true, false);
}
test "while copies its payload" {
const S = struct {
- fn doTheTest() void {
+ fn doTheTest() !void {
var tmp: ?i32 = 10;
while (tmp) |value| {
// Modify the original variable
tmp = null;
- expect(value == 10);
+ try expect(value == 10);
}
}
};
- S.doTheTest();
- comptime S.doTheTest();
+ try S.doTheTest();
+ comptime try S.doTheTest();
}
test/stage1/behavior/widening.zig
@@ -9,13 +9,13 @@ test "integer widening" {
var d: u64 = c;
var e: u64 = d;
var f: u128 = e;
- expect(f == a);
+ try expect(f == a);
}
test "implicit unsigned integer to signed integer" {
var a: u8 = 250;
var b: i16 = a;
- expect(b == 250);
+ try expect(b == 250);
}
test "float widening" {
@@ -23,9 +23,9 @@ test "float widening" {
var b: f32 = a;
var c: f64 = b;
var d: f128 = c;
- expect(a == b);
- expect(b == c);
- expect(c == d);
+ try expect(a == b);
+ try expect(b == c);
+ try expect(c == d);
}
test "float widening f16 to f128" {
@@ -35,5 +35,5 @@ test "float widening f16 to f128" {
var x: f16 = 12.34;
var y: f128 = x;
- expect(x == y);
+ try expect(x == y);
}
test/stage1/c_abi/main.zig
@@ -24,11 +24,11 @@ extern fn c_i64(i64) void;
extern fn c_five_integers(i32, i32, i32, i32, i32) void;
export fn zig_five_integers(a: i32, b: i32, c: i32, d: i32, e: i32) void {
- expect(a == 12);
- expect(b == 34);
- expect(c == 56);
- expect(d == 78);
- expect(e == 90);
+ expect(a == 12) catch @panic("test failure");
+ expect(b == 34) catch @panic("test failure");
+ expect(c == 56) catch @panic("test failure");
+ expect(d == 78) catch @panic("test failure");
+ expect(e == 90) catch @panic("test failure");
}
test "C ABI integers" {
@@ -45,28 +45,28 @@ test "C ABI integers" {
}
export fn zig_u8(x: u8) void {
- expect(x == 0xff);
+ expect(x == 0xff) catch @panic("test failure");
}
export fn zig_u16(x: u16) void {
- expect(x == 0xfffe);
+ expect(x == 0xfffe) catch @panic("test failure");
}
export fn zig_u32(x: u32) void {
- expect(x == 0xfffffffd);
+ expect(x == 0xfffffffd) catch @panic("test failure");
}
export fn zig_u64(x: u64) void {
- expect(x == 0xfffffffffffffffc);
+ expect(x == 0xfffffffffffffffc) catch @panic("test failure");
}
export fn zig_i8(x: i8) void {
- expect(x == -1);
+ expect(x == -1) catch @panic("test failure");
}
export fn zig_i16(x: i16) void {
- expect(x == -2);
+ expect(x == -2) catch @panic("test failure");
}
export fn zig_i32(x: i32) void {
- expect(x == -3);
+ expect(x == -3) catch @panic("test failure");
}
export fn zig_i64(x: i64) void {
- expect(x == -4);
+ expect(x == -4) catch @panic("test failure");
}
extern fn c_f32(f32) void;
@@ -76,11 +76,11 @@ extern fn c_f64(f64) void;
extern fn c_five_floats(f32, f32, f32, f32, f32) void;
export fn zig_five_floats(a: f32, b: f32, c: f32, d: f32, e: f32) void {
- expect(a == 1.0);
- expect(b == 2.0);
- expect(c == 3.0);
- expect(d == 4.0);
- expect(e == 5.0);
+ expect(a == 1.0) catch @panic("test failure");
+ expect(b == 2.0) catch @panic("test failure");
+ expect(c == 3.0) catch @panic("test failure");
+ expect(d == 4.0) catch @panic("test failure");
+ expect(e == 5.0) catch @panic("test failure");
}
test "C ABI floats" {
@@ -90,10 +90,10 @@ test "C ABI floats" {
}
export fn zig_f32(x: f32) void {
- expect(x == 12.34);
+ expect(x == 12.34) catch @panic("test failure");
}
export fn zig_f64(x: f64) void {
- expect(x == 56.78);
+ expect(x == 56.78) catch @panic("test failure");
}
extern fn c_ptr(*c_void) void;
@@ -103,7 +103,7 @@ test "C ABI pointer" {
}
export fn zig_ptr(x: *c_void) void {
- expect(@ptrToInt(x) == 0xdeadbeef);
+ expect(@ptrToInt(x) == 0xdeadbeef) catch @panic("test failure");
}
extern fn c_bool(bool) void;
@@ -113,7 +113,7 @@ test "C ABI bool" {
}
export fn zig_bool(x: bool) void {
- expect(x);
+ expect(x) catch @panic("test failure");
}
const BigStruct = extern struct {
@@ -137,11 +137,11 @@ test "C ABI big struct" {
}
export fn zig_big_struct(x: BigStruct) void {
- expect(x.a == 1);
- expect(x.b == 2);
- expect(x.c == 3);
- expect(x.d == 4);
- expect(x.e == 5);
+ expect(x.a == 1) catch @panic("test failure");
+ expect(x.b == 2) catch @panic("test failure");
+ expect(x.c == 3) catch @panic("test failure");
+ expect(x.d == 4) catch @panic("test failure");
+ expect(x.e == 5) catch @panic("test failure");
}
const BigUnion = extern union {
@@ -163,11 +163,11 @@ test "C ABI big union" {
}
export fn zig_big_union(x: BigUnion) void {
- expect(x.a.a == 1);
- expect(x.a.b == 2);
- expect(x.a.c == 3);
- expect(x.a.d == 4);
- expect(x.a.e == 5);
+ expect(x.a.a == 1) catch @panic("test failure");
+ expect(x.a.b == 2) catch @panic("test failure");
+ expect(x.a.c == 3) catch @panic("test failure");
+ expect(x.a.d == 4) catch @panic("test failure");
+ expect(x.a.e == 5) catch @panic("test failure");
}
const SmallStructInts = extern struct {
@@ -189,10 +189,10 @@ test "C ABI small struct of ints" {
}
export fn zig_small_struct_ints(x: SmallStructInts) void {
- expect(x.a == 1);
- expect(x.b == 2);
- expect(x.c == 3);
- expect(x.d == 4);
+ expect(x.a == 1) catch @panic("test failure");
+ expect(x.b == 2) catch @panic("test failure");
+ expect(x.c == 3) catch @panic("test failure");
+ expect(x.d == 4) catch @panic("test failure");
}
const SplitStructInt = extern struct {
@@ -212,9 +212,9 @@ test "C ABI split struct of ints" {
}
export fn zig_split_struct_ints(x: SplitStructInt) void {
- expect(x.a == 1234);
- expect(x.b == 100);
- expect(x.c == 1337);
+ expect(x.a == 1234) catch @panic("test failure");
+ expect(x.b == 100) catch @panic("test failure");
+ expect(x.c == 1337) catch @panic("test failure");
}
extern fn c_big_struct_both(BigStruct) BigStruct;
@@ -228,19 +228,19 @@ test "C ABI sret and byval together" {
.e = 5,
};
var y = c_big_struct_both(s);
- expect(y.a == 10);
- expect(y.b == 11);
- expect(y.c == 12);
- expect(y.d == 13);
- expect(y.e == 14);
+ try expect(y.a == 10);
+ try expect(y.b == 11);
+ try expect(y.c == 12);
+ try expect(y.d == 13);
+ try expect(y.e == 14);
}
export fn zig_big_struct_both(x: BigStruct) BigStruct {
- expect(x.a == 30);
- expect(x.b == 31);
- expect(x.c == 32);
- expect(x.d == 33);
- expect(x.e == 34);
+ expect(x.a == 30) catch @panic("test failure");
+ expect(x.b == 31) catch @panic("test failure");
+ expect(x.c == 32) catch @panic("test failure");
+ expect(x.d == 33) catch @panic("test failure");
+ expect(x.e == 34) catch @panic("test failure");
var s = BigStruct{
.a = 20,
.b = 21,
@@ -324,15 +324,15 @@ extern fn c_ret_i32() i32;
extern fn c_ret_i64() i64;
test "C ABI integer return types" {
- expect(c_ret_bool() == true);
+ try expect(c_ret_bool() == true);
- expect(c_ret_u8() == 0xff);
- expect(c_ret_u16() == 0xffff);
- expect(c_ret_u32() == 0xffffffff);
- expect(c_ret_u64() == 0xffffffffffffffff);
+ try expect(c_ret_u8() == 0xff);
+ try expect(c_ret_u16() == 0xffff);
+ try expect(c_ret_u32() == 0xffffffff);
+ try expect(c_ret_u64() == 0xffffffffffffffff);
- expect(c_ret_i8() == -1);
- expect(c_ret_i16() == -1);
- expect(c_ret_i32() == -1);
- expect(c_ret_i64() == -1);
+ try expect(c_ret_i8() == -1);
+ try expect(c_ret_i16() == -1);
+ try expect(c_ret_i32() == -1);
+ try expect(c_ret_i64() == -1);
}
test/standalone/brace_expansion/main.zig
@@ -241,52 +241,52 @@ pub fn main() !void {
test "invalid inputs" {
global_allocator = std.testing.allocator;
- expectError("}ABC", error.InvalidInput);
- expectError("{ABC", error.InvalidInput);
- expectError("}{", error.InvalidInput);
- expectError("{}", error.InvalidInput);
- expectError("A,B,C", error.InvalidInput);
- expectError("{A{B,C}", error.InvalidInput);
- expectError("{A,}", error.InvalidInput);
-
- expectError("\n", error.InvalidInput);
+ try expectError("}ABC", error.InvalidInput);
+ try expectError("{ABC", error.InvalidInput);
+ try expectError("}{", error.InvalidInput);
+ try expectError("{}", error.InvalidInput);
+ try expectError("A,B,C", error.InvalidInput);
+ try expectError("{A{B,C}", error.InvalidInput);
+ try expectError("{A,}", error.InvalidInput);
+
+ try expectError("\n", error.InvalidInput);
}
-fn expectError(test_input: []const u8, expected_err: anyerror) void {
+fn expectError(test_input: []const u8, expected_err: anyerror) !void {
var output_buf = ArrayList(u8).init(global_allocator);
defer output_buf.deinit();
- testing.expectError(expected_err, expandString(test_input, &output_buf));
+ try testing.expectError(expected_err, expandString(test_input, &output_buf));
}
test "valid inputs" {
global_allocator = std.testing.allocator;
- expectExpansion("{x,y,z}", "x y z");
- expectExpansion("{A,B}{x,y}", "Ax Ay Bx By");
- expectExpansion("{A,B{x,y}}", "A Bx By");
-
- expectExpansion("{ABC}", "ABC");
- expectExpansion("{A,B,C}", "A B C");
- expectExpansion("ABC", "ABC");
-
- expectExpansion("", "");
- expectExpansion("{A,B}{C,{x,y}}{g,h}", "ACg ACh Axg Axh Ayg Ayh BCg BCh Bxg Bxh Byg Byh");
- expectExpansion("{A,B}{C,C{x,y}}{g,h}", "ACg ACh ACxg ACxh ACyg ACyh BCg BCh BCxg BCxh BCyg BCyh");
- expectExpansion("{A,B}a", "Aa Ba");
- expectExpansion("{C,{x,y}}", "C x y");
- expectExpansion("z{C,{x,y}}", "zC zx zy");
- expectExpansion("a{b,c{d,e{f,g}}}", "ab acd acef aceg");
- expectExpansion("a{x,y}b", "axb ayb");
- expectExpansion("z{{a,b}}", "za zb");
- expectExpansion("a{b}", "ab");
+ try expectExpansion("{x,y,z}", "x y z");
+ try expectExpansion("{A,B}{x,y}", "Ax Ay Bx By");
+ try expectExpansion("{A,B{x,y}}", "A Bx By");
+
+ try expectExpansion("{ABC}", "ABC");
+ try expectExpansion("{A,B,C}", "A B C");
+ try expectExpansion("ABC", "ABC");
+
+ try expectExpansion("", "");
+ try expectExpansion("{A,B}{C,{x,y}}{g,h}", "ACg ACh Axg Axh Ayg Ayh BCg BCh Bxg Bxh Byg Byh");
+ try expectExpansion("{A,B}{C,C{x,y}}{g,h}", "ACg ACh ACxg ACxh ACyg ACyh BCg BCh BCxg BCxh BCyg BCyh");
+ try expectExpansion("{A,B}a", "Aa Ba");
+ try expectExpansion("{C,{x,y}}", "C x y");
+ try expectExpansion("z{C,{x,y}}", "zC zx zy");
+ try expectExpansion("a{b,c{d,e{f,g}}}", "ab acd acef aceg");
+ try expectExpansion("a{x,y}b", "axb ayb");
+ try expectExpansion("z{{a,b}}", "za zb");
+ try expectExpansion("a{b}", "ab");
}
-fn expectExpansion(test_input: []const u8, expected_result: []const u8) void {
+fn expectExpansion(test_input: []const u8, expected_result: []const u8) !void {
var result = ArrayList(u8).init(global_allocator);
defer result.deinit();
expandString(test_input, &result) catch unreachable;
- testing.expectEqualSlices(u8, expected_result, result.items);
+ try testing.expectEqualSlices(u8, expected_result, result.items);
}
test/standalone/empty_env/main.zig
@@ -1,6 +1,6 @@
const std = @import("std");
-pub fn main() void {
+pub fn main() !void {
const env_map = std.process.getEnvMap(std.testing.allocator) catch @panic("unable to get env map");
- std.testing.expect(env_map.count() == 0);
+ try std.testing.expect(env_map.count() == 0);
}
test/standalone/global_linkage/main.zig
@@ -4,6 +4,6 @@ extern var obj1_integer: usize;
extern var obj2_integer: usize;
test "access the external integers" {
- std.testing.expect(obj1_integer == 421);
- std.testing.expect(obj2_integer == 422);
+ try std.testing.expect(obj1_integer == 421);
+ try std.testing.expect(obj2_integer == 422);
}
test/standalone/issue_794/main.zig
@@ -3,5 +3,5 @@ const std = @import("std");
const testing = std.testing;
test "c import" {
- comptime testing.expect(c.NUMBER == 1234);
+ comptime try testing.expect(c.NUMBER == 1234);
}
test/standalone/link_interdependent_static_c_libs/main.zig
@@ -4,5 +4,5 @@ const c = @cImport(@cInclude("b.h"));
test "import C sub" {
const result = c.sub(2, 1);
- expect(result == 1);
+ try expect(result == 1);
}
test/standalone/static_c_lib/foo.zig
@@ -4,9 +4,9 @@ const c = @cImport(@cInclude("foo.h"));
test "C add" {
const result = c.add(1, 2);
- expect(result == 3);
+ try expect(result == 3);
}
test "C extern variable" {
- expect(c.foo == 12345);
+ try expect(c.foo == 12345);
}
test/standalone/use_alias/main.zig
@@ -6,5 +6,5 @@ test "symbol exists" {
.a = 1,
.b = 1,
};
- expect(foo.a + foo.b == 2);
+ try expect(foo.a + foo.b == 2);
}
test/cli.zig
@@ -29,7 +29,7 @@ pub fn main() !void {
const dir_path = try fs.path.join(a, &[_][]const u8{ cache_root, "clitest" });
defer fs.cwd().deleteTree(dir_path) catch {};
-
+
const TestFn = fn ([]const u8, []const u8) anyerror!void;
const test_fns = [_]TestFn{
testZigInitLib,
@@ -94,13 +94,13 @@ fn exec(cwd: []const u8, expect_0: bool, argv: []const []const u8) !ChildProcess
fn testZigInitLib(zig_exe: []const u8, dir_path: []const u8) !void {
_ = try exec(dir_path, true, &[_][]const u8{ zig_exe, "init-lib" });
const test_result = try exec(dir_path, true, &[_][]const u8{ zig_exe, "build", "test" });
- testing.expectStringEndsWith(test_result.stderr, "All 1 tests passed.\n");
+ try testing.expectStringEndsWith(test_result.stderr, "All 1 tests passed.\n");
}
fn testZigInitExe(zig_exe: []const u8, dir_path: []const u8) !void {
_ = try exec(dir_path, true, &[_][]const u8{ zig_exe, "init-exe" });
const run_result = try exec(dir_path, true, &[_][]const u8{ zig_exe, "build", "run" });
- testing.expectEqualStrings("info: All your codebase are belong to us.\n", run_result.stderr);
+ try testing.expectEqualStrings("info: All your codebase are belong to us.\n", run_result.stderr);
}
fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) anyerror!void {
@@ -136,9 +136,9 @@ fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) anyerror!void {
_ = try exec(dir_path, true, args.items);
const out_asm = try std.fs.cwd().readFileAlloc(a, example_s_path, std.math.maxInt(usize));
- testing.expect(std.mem.indexOf(u8, out_asm, "square:") != null);
- testing.expect(std.mem.indexOf(u8, out_asm, "mov\teax, edi") != null);
- testing.expect(std.mem.indexOf(u8, out_asm, "imul\teax, edi") != null);
+ try testing.expect(std.mem.indexOf(u8, out_asm, "square:") != null);
+ try testing.expect(std.mem.indexOf(u8, out_asm, "mov\teax, edi") != null);
+ try testing.expect(std.mem.indexOf(u8, out_asm, "imul\teax, edi") != null);
}
fn testMissingOutputPath(zig_exe: []const u8, dir_path: []const u8) !void {
@@ -149,7 +149,7 @@ fn testMissingOutputPath(zig_exe: []const u8, dir_path: []const u8) !void {
const result = try exec(dir_path, false, &[_][]const u8{ zig_exe, "build-exe", source_path, output_arg });
const s = std.fs.path.sep_str;
const expected: []const u8 = "error: unable to open output directory 'does" ++ s ++ "not" ++ s ++ "exist': FileNotFound\n";
- testing.expectEqualStrings(expected, result.stderr);
+ try testing.expectEqualStrings(expected, result.stderr);
}
fn testZigFmt(zig_exe: []const u8, dir_path: []const u8) !void {
@@ -162,20 +162,20 @@ fn testZigFmt(zig_exe: []const u8, dir_path: []const u8) !void {
const run_result1 = try exec(dir_path, true, &[_][]const u8{ zig_exe, "fmt", fmt1_zig_path });
// stderr should be file path + \n
- testing.expect(std.mem.startsWith(u8, run_result1.stdout, fmt1_zig_path));
- testing.expect(run_result1.stdout.len == fmt1_zig_path.len + 1 and run_result1.stdout[run_result1.stdout.len - 1] == '\n');
+ try testing.expect(std.mem.startsWith(u8, run_result1.stdout, fmt1_zig_path));
+ try testing.expect(run_result1.stdout.len == fmt1_zig_path.len + 1 and run_result1.stdout[run_result1.stdout.len - 1] == '\n');
const fmt2_zig_path = try fs.path.join(a, &[_][]const u8{ dir_path, "fmt2.zig" });
try fs.cwd().writeFile(fmt2_zig_path, unformatted_code);
const run_result2 = try exec(dir_path, true, &[_][]const u8{ zig_exe, "fmt", dir_path });
// running it on the dir, only the new file should be changed
- testing.expect(std.mem.startsWith(u8, run_result2.stdout, fmt2_zig_path));
- testing.expect(run_result2.stdout.len == fmt2_zig_path.len + 1 and run_result2.stdout[run_result2.stdout.len - 1] == '\n');
+ try testing.expect(std.mem.startsWith(u8, run_result2.stdout, fmt2_zig_path));
+ try testing.expect(run_result2.stdout.len == fmt2_zig_path.len + 1 and run_result2.stdout[run_result2.stdout.len - 1] == '\n');
const run_result3 = try exec(dir_path, true, &[_][]const u8{ zig_exe, "fmt", dir_path });
// both files have been formatted, nothing should change now
- testing.expect(run_result3.stdout.len == 0);
+ try testing.expect(run_result3.stdout.len == 0);
// Check UTF-16 decoding
const fmt4_zig_path = try fs.path.join(a, &[_][]const u8{ dir_path, "fmt4.zig" });
@@ -183,6 +183,6 @@ fn testZigFmt(zig_exe: []const u8, dir_path: []const u8) !void {
try fs.cwd().writeFile(fmt4_zig_path, unformatted_code_utf16);
const run_result4 = try exec(dir_path, true, &[_][]const u8{ zig_exe, "fmt", dir_path });
- testing.expect(std.mem.startsWith(u8, run_result4.stdout, fmt4_zig_path));
- testing.expect(run_result4.stdout.len == fmt4_zig_path.len + 1 and run_result4.stdout[run_result4.stdout.len - 1] == '\n');
+ try testing.expect(std.mem.startsWith(u8, run_result4.stdout, fmt4_zig_path));
+ try testing.expect(run_result4.stdout.len == fmt4_zig_path.len + 1 and run_result4.stdout[run_result4.stdout.len - 1] == '\n');
}
test/compile_errors.zig
@@ -230,7 +230,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
cases.add("array in c exported function",
\\export fn zig_array(x: [10]u8) void {
- \\ expect(std.mem.eql(u8, &x, "1234567890"));
+ \\try expect(std.mem.eql(u8, &x, "1234567890"));
\\}
\\
\\export fn zig_return_array() [10]u8 {
test/stack_traces.zig
@@ -5,13 +5,13 @@ const tests = @import("tests.zig");
pub fn addCases(cases: *tests.StackTracesContext) void {
cases.addCase(.{
.name = "return",
- .source =
+ .source =
\\pub fn main() !void {
\\ return error.TheSkyIsFalling;
\\}
,
.Debug = .{
- .expect =
+ .expect =
\\error: TheSkyIsFalling
\\source.zig:2:5: [address] in main (test)
\\ return error.TheSkyIsFalling;
@@ -23,7 +23,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
.exclude_os = .{
.windows, // segfault
},
- .expect =
+ .expect =
\\error: TheSkyIsFalling
\\source.zig:2:5: [address] in [function]
\\ return error.TheSkyIsFalling;
@@ -32,13 +32,13 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
,
},
.ReleaseFast = .{
- .expect =
+ .expect =
\\error: TheSkyIsFalling
\\
,
},
.ReleaseSmall = .{
- .expect =
+ .expect =
\\error: TheSkyIsFalling
\\
,
@@ -47,7 +47,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
cases.addCase(.{
.name = "try return",
- .source =
+ .source =
\\fn foo() !void {
\\ return error.TheSkyIsFalling;
\\}
@@ -57,7 +57,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
\\}
,
.Debug = .{
- .expect =
+ .expect =
\\error: TheSkyIsFalling
\\source.zig:2:5: [address] in foo (test)
\\ return error.TheSkyIsFalling;
@@ -72,7 +72,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
.exclude_os = .{
.windows, // segfault
},
- .expect =
+ .expect =
\\error: TheSkyIsFalling
\\source.zig:2:5: [address] in [function]
\\ return error.TheSkyIsFalling;
@@ -84,13 +84,13 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
,
},
.ReleaseFast = .{
- .expect =
+ .expect =
\\error: TheSkyIsFalling
\\
,
},
.ReleaseSmall = .{
- .expect =
+ .expect =
\\error: TheSkyIsFalling
\\
,
@@ -99,7 +99,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
cases.addCase(.{
.name = "try try return return",
- .source =
+ .source =
\\fn foo() !void {
\\ try bar();
\\}
@@ -117,7 +117,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
\\}
,
.Debug = .{
- .expect =
+ .expect =
\\error: TheSkyIsFalling
\\source.zig:10:5: [address] in make_error (test)
\\ return error.TheSkyIsFalling;
@@ -138,7 +138,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
.exclude_os = .{
.windows, // segfault
},
- .expect =
+ .expect =
\\error: TheSkyIsFalling
\\source.zig:10:5: [address] in [function]
\\ return error.TheSkyIsFalling;
@@ -156,13 +156,13 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
,
},
.ReleaseFast = .{
- .expect =
+ .expect =
\\error: TheSkyIsFalling
\\
,
},
.ReleaseSmall = .{
- .expect =
+ .expect =
\\error: TheSkyIsFalling
\\
,
@@ -174,7 +174,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
.windows,
},
.name = "dumpCurrentStackTrace",
- .source =
+ .source =
\\const std = @import("std");
\\
\\fn bar() void {
@@ -189,7 +189,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
\\}
,
.Debug = .{
- .expect =
+ .expect =
\\source.zig:7:8: [address] in foo (test)
\\ bar();
\\ ^