update usage of std.testing in behavior and standalone tests

This commit is contained in:
Veikka Tuominen 2021-05-04 21:23:22 +03:00
parent fd77f2cfed
commit 0a38f61d58
125 changed files with 3783 additions and 3793 deletions

View file

@ -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');
}

View file

@ -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 {

View file

@ -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();
\\ ^

View file

@ -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;

View file

@ -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);
}
}

View file

@ -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();
}

View file

@ -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);
}
}

View file

@ -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");
}
};

View file

@ -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);
}

View file

@ -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');

View file

@ -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);
}
}

View file

@ -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);
}

View file

@ -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));
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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"));
}
}

View file

@ -19,5 +19,5 @@ test "bug 1120" {
1 => &b.a,
else => unreachable,
};
expect(ptr.* == 2);
try expect(ptr.* == 2);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -27,5 +27,5 @@ fn parseNote() ?i32 {
test "fixed" {
const result = parseNote();
std.testing.expect(result.? == 9);
try std.testing.expect(result.? == 9);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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" {

View file

@ -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);
}

View file

@ -14,5 +14,5 @@ test "bug 394 fixed" {
.x = 3,
.y = E{ .B = 1 },
};
expect(x.x == 3);
try expect(x.x == 3);
}

View file

@ -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 {

View file

@ -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();
}

View file

@ -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 {

View file

@ -1 +1 @@
//
//

View file

@ -1 +1 @@
//!
//!

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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();
}

View file

@ -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);
}

View file

@ -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);
}
}

View file

@ -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);
}

View file

@ -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 => {},

View file

@ -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);
}

View file

@ -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 {

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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]);
}
}

View file

@ -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();
}

View file

@ -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 {

View file

@ -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));
}

View file

@ -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);
}

View file

@ -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 {

View file

@ -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();
}

View file

@ -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();
}

View file

@ -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"));
}

View file

@ -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, {}));
}
}

View file

@ -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.?);
}
}

View file

@ -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);
}

View file

@ -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);
// }
//}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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 });
}

View file

@ -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);
}

View file

@ -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"));
}

View file

@ -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);
}

View file

@ -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();
}

View file

@ -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" {

View file

@ -26,5 +26,5 @@ test "incomplete struct param top level declaration" {
.c = C{ .x = 13 },
},
};
expect(foo(a) == 13);
try expect(foo(a) == 13);
}

View file

@ -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();

View file

@ -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);
}

File diff suppressed because it is too large Load diff

View file

@ -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" {

View file

@ -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);
//}
}

View file

@ -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) {

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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])));
}

View file

@ -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);
}
}

View file

@ -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"));
}

View file

@ -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);
}

View file

@ -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;
}

View file

@ -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 {

View file

@ -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();
}

View file

@ -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);
}

View file

@ -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();
}

View file

@ -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);
}

View file

@ -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();
}

View file

@ -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 {

View file

@ -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);
}

View file

@ -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();
}

View file

@ -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);
}

View file

@ -18,5 +18,5 @@ test "switch prong implicit cast" {
FormValue.One => false,
FormValue.Two => |x| x,
};
expect(result);
try expect(result);
}

Some files were not shown because too many files have changed in this diff Show more