langref: specific expectEqual* functions instead of expect

This commit is contained in:
Ivan Agafonov 2026-02-11 02:49:11 +05:00 committed by Andrew Kelley
parent a3a9dc111d
commit 333055ced7
105 changed files with 422 additions and 410 deletions

View file

@ -35,7 +35,7 @@ fn charToDigit(c: u8) u8 {
test "parse u64" {
const result = try parseU64("1234", 10);
try std.testing.expect(result == 1234);
try std.testing.expectEqual(1234, result);
}
// test

View file

@ -1,4 +1,4 @@
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
test "attempt to swap array elements with array initializer" {
var arr: [2]u32 = .{ 1, 2 };
arr = .{ arr[1], arr[0] };
@ -6,8 +6,8 @@ test "attempt to swap array elements with array initializer" {
// arr[0] = arr[1];
// arr[1] = arr[0];
// So this fails!
try expect(arr[0] == 2); // succeeds
try expect(arr[1] == 1); // fails
try expectEqual(2, arr[0]); // succeeds
try expectEqual(1, arr[1]); // fails
}
// test_error=

View file

@ -1,11 +1,11 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
test "no runtime side effects" {
var data: i32 = 0;
const T = @TypeOf(foo(i32, &data));
try comptime expect(T == i32);
try expect(data == 0);
try comptime expectEqual(i32, T);
try expectEqual(0, data);
}
fn foo(comptime T: type, ptr: *T) T {

View file

@ -1,13 +1,13 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const expect = std.testing.expect;
const expectEqualStrings = std.testing.expectEqualStrings;
test "using an allocator" {
var buffer: [100]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&buffer);
const allocator = fba.allocator();
const result = try concat(allocator, "foo", "bar");
try expect(std.mem.eql(u8, "foobar", result));
try expectEqualStrings("foobar", result);
}
fn concat(allocator: Allocator, a: []const u8, b: []const u8) ![]u8 {

View file

@ -1,11 +1,11 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
test "allowzero" {
var zero: usize = 0; // var to make to runtime-known
_ = &zero; // suppress 'var is never mutated' error
const ptr: *allowzero i32 = @ptrFromInt(zero);
try expect(@intFromPtr(ptr) == 0);
try expectEqual(0, @intFromPtr(ptr));
}
// test

View file

@ -1,5 +1,6 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
test "fully anonymous struct" {
try check(.{
@ -11,11 +12,11 @@ test "fully anonymous struct" {
}
fn check(args: anytype) !void {
try expect(args.int == 1234);
try expect(args.float == 12.34);
try expectEqual(1234, args.int);
try expectEqual(12.34, args.float);
try expect(args.b);
try expect(args.s[0] == 'h');
try expect(args.s[1] == 'i');
try expectEqual('h', args.s[0]);
try expectEqual('i', args.s[1]);
}
// test

View file

@ -1,5 +1,5 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const Number = union {
int: i32,
@ -9,8 +9,8 @@ const Number = union {
test "anonymous union literal syntax" {
const i: Number = .{ .int = 42 };
const f = makeNumber();
try expect(i.int == 42);
try expect(f.float == 12.34);
try expectEqual(42, i.int);
try expectEqual(12.34, f.float);
}
fn makeNumber() Number {

View file

@ -1,4 +1,4 @@
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
const assert = @import("std").debug.assert;
const mem = @import("std").mem;
@ -29,7 +29,7 @@ test "iterate over an array" {
for (message) |byte| {
sum += byte;
}
try expect(sum == 'h' + 'e' + 'l' * 2 + 'o');
try expectEqual('h' + 'e' + 'l' * 2 + 'o', sum);
}
// modifiable array
@ -39,8 +39,8 @@ test "modify an array" {
for (&some_integers, 0..) |*item, i| {
item.* = @intCast(i);
}
try expect(some_integers[10] == 10);
try expect(some_integers[99] == 99);
try expectEqual(10, some_integers[10]);
try expectEqual(99, some_integers[99]);
}
// array concatenation works if the values are known
@ -91,8 +91,8 @@ const Point = struct {
};
test "compile-time array initialization" {
try expect(fancy_array[4].x == 4);
try expect(fancy_array[4].y == 8);
try expectEqual(4, fancy_array[4].x);
try expectEqual(8, fancy_array[4].y);
}
// call a function to initialize an array
@ -104,9 +104,9 @@ fn makePoint(x: i32) Point {
};
}
test "array initialization with function calls" {
try expect(more_points[4].x == 3);
try expect(more_points[4].y == 6);
try expect(more_points.len == 10);
try expectEqual(3, more_points[4].x);
try expectEqual(6, more_points[4].y);
try expectEqual(10, more_points.len);
}
// test

View file

@ -1,4 +1,4 @@
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
const expectEqualSlices = @import("std").testing.expectEqualSlices;
test "basic slices" {
@ -12,14 +12,14 @@ test "basic slices" {
try expectEqualSlices(i32, slice, alt_slice);
try expect(@TypeOf(slice) == []i32);
try expect(&slice[0] == &array[0]);
try expect(slice.len == array.len);
try expectEqual([]i32, @TypeOf(slice));
try expectEqual(&array[0], &slice[0]);
try expectEqual(array.len, slice.len);
// If you slice with comptime-known start and end positions, the result is
// a pointer to an array, rather than a slice.
const array_ptr = array[0..array.len];
try expect(@TypeOf(array_ptr) == *[array.len]i32);
try expectEqual(*[array.len]i32, @TypeOf(array_ptr));
// You can perform a slice-by-length by slicing twice. This allows the compiler
// to perform some optimisations like recognising a comptime-known length when
@ -28,13 +28,13 @@ test "basic slices" {
_ = &runtime_start;
const length = 2;
const array_ptr_len = array[runtime_start..][0..length];
try expect(@TypeOf(array_ptr_len) == *[length]i32);
try expectEqual(*[length]i32, @TypeOf(array_ptr_len));
// Using the address-of operator on a slice gives a single-item pointer.
try expect(@TypeOf(&slice[0]) == *i32);
try expectEqual(*i32, @TypeOf(&slice[0]));
// Using the `ptr` field gives a many-item pointer.
try expect(@TypeOf(slice.ptr) == [*]i32);
try expect(@intFromPtr(slice.ptr) == @intFromPtr(&slice[0]));
try expectEqual([*]i32, @TypeOf(slice.ptr));
try expectEqual(@intFromPtr(slice.ptr), @intFromPtr(&slice[0]));
// Slices have array bounds checking. If you try to access something out
// of bounds, you'll get a safety check failure:
@ -47,8 +47,8 @@ test "basic slices" {
const empty1 = &[0]u8{};
// If the type is known you can use this short hand:
const empty2: []u8 = &.{};
try expect(empty1.len == 0);
try expect(empty2.len == 0);
try expectEqual(0, empty1.len);
try expectEqual(0, empty2.len);
// A zero-length initialization can always be used to create an empty slice, even if the slice is mutable.
// This is because the pointed-to data is zero bits long, so its immutability is irrelevant.

View file

@ -1,5 +1,5 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const BitField = packed struct {
a: u3,
@ -9,13 +9,13 @@ const BitField = packed struct {
test "offsets of non-byte-aligned fields" {
comptime {
try expect(@bitOffsetOf(BitField, "a") == 0);
try expect(@bitOffsetOf(BitField, "b") == 3);
try expect(@bitOffsetOf(BitField, "c") == 6);
try expectEqual(0, @bitOffsetOf(BitField, "a"));
try expectEqual(3, @bitOffsetOf(BitField, "b"));
try expectEqual(6, @bitOffsetOf(BitField, "c"));
try expect(@offsetOf(BitField, "a") == 0);
try expect(@offsetOf(BitField, "b") == 0);
try expect(@offsetOf(BitField, "c") == 0);
try expectEqual(0, @offsetOf(BitField, "a"));
try expectEqual(0, @offsetOf(BitField, "b"));
try expectEqual(0, @offsetOf(BitField, "c"));
}
}

View file

@ -1,7 +1,7 @@
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
test "noinline function call" {
try expect(@call(.auto, add, .{ 3, 9 }) == 12);
try expectEqual(12, @call(.auto, add, .{ 3, 9 }));
}
fn add(a: i32, b: i32) i32 {

View file

@ -12,7 +12,7 @@ const AllocationError = error{
test "coerce subset to superset" {
const err = foo(AllocationError.OutOfMemory);
try std.testing.expect(err == FileOpenError.OutOfMemory);
try std.testing.expectEqual(FileOpenError.OutOfMemory, err);
}
fn foo(err: AllocationError) FileOpenError {

View file

@ -1,10 +1,10 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
test "coercing large integer type to smaller one when value is comptime-known to fit" {
const x: u64 = 255;
const y: u8 = x;
try expect(y == 255);
try expectEqual(255, y);
}
// test

View file

@ -1,12 +1,12 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
test "coerce to optionals wrapped in error union" {
const x: anyerror!?i32 = 1234;
const y: anyerror!?i32 = null;
try expect((try x).? == 1234);
try expect((try y) == null);
try expectEqual(1234, (try x).?);
try expectEqual(null, (try y));
}
// test

View file

@ -1,12 +1,12 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
test "coerce to optionals" {
const x: ?i32 = 1234;
const y: ?i32 = null;
try expect(x.? == 1234);
try expect(y == null);
try expectEqual(1234, x.?);
try expectEqual(null, y);
}
// test

View file

@ -1,5 +1,7 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const expectEqualStrings = std.testing.expectEqualStrings;
const expectEqualSlices = std.testing.expectEqualSlices;
// You can assign constant pointers to arrays to a slice with
// const modifier on the element type. Useful in particular for
@ -7,48 +9,48 @@ const expect = std.testing.expect;
test "*const [N]T to []const T" {
const x1: []const u8 = "hello";
const x2: []const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 };
try expect(std.mem.eql(u8, x1, x2));
try expectEqualStrings(x1, x2);
const y: []const f32 = &[2]f32{ 1.2, 3.4 };
try expect(y[0] == 1.2);
try expectEqual(1.2, y[0]);
}
// Likewise, it works when the destination type is an error union.
test "*const [N]T to E![]const T" {
const x1: anyerror![]const u8 = "hello";
const x2: anyerror![]const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 };
try expect(std.mem.eql(u8, try x1, try x2));
try expectEqualStrings(try x1, try x2);
const y: anyerror![]const f32 = &[2]f32{ 1.2, 3.4 };
try expect((try y)[0] == 1.2);
try expectEqual(1.2, (try y)[0]);
}
// Likewise, it works when the destination type is an optional.
test "*const [N]T to ?[]const T" {
const x1: ?[]const u8 = "hello";
const x2: ?[]const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 };
try expect(std.mem.eql(u8, x1.?, x2.?));
try expectEqualStrings(x1.?, x2.?);
const y: ?[]const f32 = &[2]f32{ 1.2, 3.4 };
try expect(y.?[0] == 1.2);
try expectEqual(1.2, y.?[0]);
}
// In this cast, the array length becomes the slice length.
test "*[N]T to []T" {
var buf: [5]u8 = "hello".*;
const x: []u8 = &buf;
try expect(std.mem.eql(u8, x, "hello"));
try expectEqualStrings("hello", x);
const buf2 = [2]f32{ 1.2, 3.4 };
const x2: []const f32 = &buf2;
try expect(std.mem.eql(f32, x2, &[2]f32{ 1.2, 3.4 }));
try expectEqualSlices(f32, &[2]f32{ 1.2, 3.4 }, x2);
}
// Single-item pointers to arrays can be coerced to many-item pointers.
test "*[N]T to [*]T" {
var buf: [5]u8 = "hello".*;
const x: [*]u8 = &buf;
try expect(x[4] == 'o');
try expectEqual('o', x[4]);
// x[5] would be an uncaught out of bounds pointer dereference!
}
@ -56,7 +58,7 @@ test "*[N]T to [*]T" {
test "*[N]T to ?[*]T" {
var buf: [5]u8 = "hello".*;
const x: ?[*]u8 = &buf;
try expect(x.?[4] == 'o');
try expectEqual('o', x.?[4]);
}
// Single-item pointers can be cast to len-1 single-item arrays.
@ -64,14 +66,14 @@ test "*T to *[1]T" {
var x: i32 = 1234;
const y: *[1]i32 = &x;
const z: [*]i32 = y;
try expect(z[0] == 1234);
try expectEqual(1234, z[0]);
}
// Sentinel-terminated slices can be coerced into sentinel-terminated pointers
test "[:x]T to [*:x]T" {
const buf: [:0]const u8 = "hello";
const buf2: [*:0]const u8 = buf;
try expect(buf2[4] == 'o');
try expectEqual('o', buf2[4]);
}
// test

View file

@ -1,11 +1,11 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
test "coercion to error unions" {
const x: anyerror!i32 = 1234;
const y: anyerror!i32 = error.Failure;
try expect((try x) == 1234);
try expectEqual(1234, (try x));
try std.testing.expectError(error.Failure, y);
}

View file

@ -1,5 +1,5 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const Tuple = struct { u8, u8 };
test "coercion from homogeneous tuple to array" {

View file

@ -1,5 +1,5 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const E = enum {
one,
@ -28,22 +28,22 @@ const U2 = union(enum) {
test "coercion between unions and enums" {
const u = U{ .two = 12.34 };
const e: E = u; // coerce union to enum
try expect(e == E.two);
try expectEqual(E.two, e);
const three = E.three;
const u_2: U = three; // coerce enum to union
try expect(u_2 == E.three);
try expectEqual(E.three, u_2);
const u_3: U = .three; // coerce enum literal to union
try expect(u_3 == E.three);
try expectEqual(E.three, u_3);
const u_4: U2 = .a; // coerce enum literal to union with inferred enum tag type.
try expect(u_4.tag() == 1);
try expectEqual(1, u_4.tag());
// The following example is invalid.
// error: coercion from enum '@EnumLiteral()' to union 'test_coerce_unions_enum.U2' must initialize 'f32' field 'b'
//var u_5: U2 = .b;
//try expect(u_5.tag() == 2);
//try expectEqual(2, u_5.tag());
}
// test

View file

@ -1,4 +1,4 @@
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
const CmdFn = struct {
name: []const u8,
@ -32,9 +32,9 @@ fn performFn(comptime prefix_char: u8, start_value: i32) i32 {
}
test "perform fn" {
try expect(performFn('t', 1) == 6);
try expect(performFn('o', 0) == 1);
try expect(performFn('w', 99) == 99);
try expectEqual(6, performFn('t', 1));
try expectEqual(1, performFn('o', 0));
try expectEqual(99, performFn('w', 99));
}
// test

View file

@ -8,7 +8,7 @@ fn max(comptime T: type, a: T, b: T) T {
}
}
test "try to compare bools" {
try @import("std").testing.expect(max(bool, false, true) == true);
try @import("std").testing.expectEqual(true, max(bool, false, true));
}
// test

View file

@ -1,4 +1,4 @@
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
test "comptime @ptrFromInt" {
comptime {
@ -6,8 +6,8 @@ test "comptime @ptrFromInt" {
// ptr is never dereferenced.
const ptr: *i32 = @ptrFromInt(0xdeadbee0);
const addr = @intFromPtr(ptr);
try expect(@TypeOf(addr) == usize);
try expect(addr == 0xdeadbee0);
try expectEqual(usize, @TypeOf(addr));
try expectEqual(0xdeadbee0, addr);
}
}

View file

@ -1,4 +1,4 @@
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
test "comptime pointers" {
comptime {
@ -6,7 +6,7 @@ test "comptime pointers" {
const ptr = &x;
ptr.* += 1;
x += 1;
try expect(ptr.* == 3);
try expectEqual(3, ptr.*);
}
}

View file

@ -1,5 +1,5 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
test "comptime vars" {
var x: i32 = 1;
@ -8,8 +8,8 @@ test "comptime vars" {
x += 1;
y += 1;
try expect(x == 2);
try expect(y == 2);
try expectEqual(2, x);
try expectEqual(2, y);
if (y != 2) {
// This compile error never triggers because y is a comptime variable,

View file

@ -31,7 +31,7 @@ fn sum(numbers: []const i32) i32 {
}
test "variable values" {
try @import("std").testing.expect(sum_of_first_25_primes == 1060);
try @import("std").testing.expectEqual(1060, sum_of_first_25_primes);
}
// test

View file

@ -2,8 +2,8 @@ var y: i32 = add(10, x);
const x: i32 = add(12, 34);
test "container level variables" {
try expect(x == 46);
try expect(y == 56);
try expectEqual(46, x);
try expectEqual(56, y);
}
fn add(a: i32, b: i32) i32 {
@ -11,6 +11,6 @@ fn add(a: i32, b: i32) i32 {
}
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
// test

View file

@ -1,5 +1,5 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const print = std.debug.print;
fn deferExample() !usize {
@ -9,14 +9,14 @@ fn deferExample() !usize {
defer a = 2;
a = 1;
}
try expect(a == 2);
try expectEqual(2, a);
a = 5;
return a;
}
test "defer basics" {
try expect((try deferExample()) == 5);
try expectEqual(5, (try deferExample()));
}
// test

View file

@ -1,12 +1,12 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
test {
const a = {};
const b = void{};
try expect(@TypeOf(a) == void);
try expect(@TypeOf(b) == void);
try expect(a == b);
try expectEqual(void, @TypeOf(a));
try expectEqual(void, @TypeOf(b));
try expectEqual(a, b);
}
// test

View file

@ -1,5 +1,6 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const Color = enum {
auto,
@ -10,7 +11,7 @@ const Color = enum {
test "enum literals" {
const color1: Color = .auto;
const color2 = Color.auto;
try expect(color1 == color2);
try expectEqual(color1, color2);
}
test "switch using enum literals" {

View file

@ -1,4 +1,6 @@
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
const expectEqualStrings = @import("std").testing.expectEqualStrings;
const mem = @import("std").mem;
// Declare an enum.
@ -20,9 +22,9 @@ const Value = enum(u2) {
// Now you can cast between u2 and Value.
// The ordinal value starts from 0, counting up by 1 from the previous member.
test "enum ordinal value" {
try expect(@intFromEnum(Value.zero) == 0);
try expect(@intFromEnum(Value.one) == 1);
try expect(@intFromEnum(Value.two) == 2);
try expectEqual(0, @intFromEnum(Value.zero));
try expectEqual(1, @intFromEnum(Value.one));
try expectEqual(2, @intFromEnum(Value.two));
}
// You can override the ordinal value for an enum.
@ -32,9 +34,9 @@ const Value2 = enum(u32) {
million = 1000000,
};
test "set enum ordinal value" {
try expect(@intFromEnum(Value2.hundred) == 100);
try expect(@intFromEnum(Value2.thousand) == 1000);
try expect(@intFromEnum(Value2.million) == 1000000);
try expectEqual(100, @intFromEnum(Value2.hundred));
try expectEqual(1000, @intFromEnum(Value2.thousand));
try expectEqual(1000000, @intFromEnum(Value2.million));
}
// You can also override only some values.
@ -46,11 +48,11 @@ const Value3 = enum(u4) {
e,
};
test "enum implicit ordinal values and overridden values" {
try expect(@intFromEnum(Value3.a) == 0);
try expect(@intFromEnum(Value3.b) == 8);
try expect(@intFromEnum(Value3.c) == 9);
try expect(@intFromEnum(Value3.d) == 4);
try expect(@intFromEnum(Value3.e) == 5);
try expectEqual(0, @intFromEnum(Value3.a));
try expectEqual(8, @intFromEnum(Value3.b));
try expectEqual(9, @intFromEnum(Value3.c));
try expectEqual(4, @intFromEnum(Value3.d));
try expectEqual(5, @intFromEnum(Value3.e));
}
// Enums can have methods, the same as structs and unions.
@ -84,7 +86,7 @@ test "enum switch" {
Foo.number => "this is a number",
Foo.none => "this is a none",
};
try expect(mem.eql(u8, what_is_it, "this is a number"));
try expectEqualStrings(what_is_it, "this is a number");
}
// @typeInfo can be used to access the integer tag type of an enum.
@ -95,18 +97,18 @@ const Small = enum {
four,
};
test "std.meta.Tag" {
try expect(@typeInfo(Small).@"enum".tag_type == u2);
try expectEqual(u2, @typeInfo(Small).@"enum".tag_type);
}
// @typeInfo tells us the field count and the fields names:
test "@typeInfo" {
try expect(@typeInfo(Small).@"enum".fields.len == 4);
try expect(mem.eql(u8, @typeInfo(Small).@"enum".fields[1].name, "two"));
try expectEqual(4, @typeInfo(Small).@"enum".fields.len);
try expectEqualStrings(@typeInfo(Small).@"enum".fields[1].name, "two");
}
// @tagName gives a [:0]const u8 representation of an enum value:
test "@tagName" {
try expect(mem.eql(u8, @tagName(Small.three), "three"));
try expectEqualStrings(@tagName(Small.three), "three");
}
// test

View file

@ -1,4 +1,4 @@
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
test "error union" {
var foo: anyerror!i32 = undefined;
@ -10,10 +10,10 @@ test "error union" {
foo = error.SomeError;
// Use compile-time reflection to access the payload type of an error union:
try comptime expect(@typeInfo(@TypeOf(foo)).error_union.payload == i32);
try comptime expectEqual(i32, @typeInfo(@TypeOf(foo)).error_union.payload);
// Use compile-time reflection to access the error set type of an error union:
try comptime expect(@typeInfo(@TypeOf(foo)).error_union.error_set == anyerror);
try comptime expectEqual(anyerror, @typeInfo(@TypeOf(foo)).error_union.error_set);
}
// test

View file

@ -1,4 +1,4 @@
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
fn fibonacci(index: u32) u32 {
//if (index < 2) return index;
@ -6,7 +6,7 @@ fn fibonacci(index: u32) u32 {
}
test "fibonacci" {
try comptime expect(fibonacci(7) == 13);
try comptime expectEqual(13, fibonacci(7));
}
// test_error=overflow of integer type

View file

@ -1,4 +1,4 @@
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
fn fibonacci(index: u32) u32 {
if (index < 2) return index;
@ -7,10 +7,10 @@ fn fibonacci(index: u32) u32 {
test "fibonacci" {
// test fibonacci at run-time
try expect(fibonacci(7) == 13);
try expectEqual(13, fibonacci(7));
// test fibonacci at compile-time
try comptime expect(fibonacci(7) == 13);
try comptime expectEqual(13, fibonacci(7));
}
// test

View file

@ -1,4 +1,5 @@
const std = @import("std");
const expectEqual = std.testing.expectEqual;
const Point = struct {
x: u32,
@ -8,23 +9,20 @@ const Point = struct {
};
test "field access by string" {
const expect = std.testing.expect;
var p = Point{ .x = 0, .y = 0 };
@field(p, "x") = 4;
@field(p, "y") = @field(p, "x") + 1;
try expect(@field(p, "x") == 4);
try expect(@field(p, "y") == 5);
try expectEqual(4, @field(p, "x"));
try expectEqual(5, @field(p, "y"));
}
test "decl access by string" {
const expect = std.testing.expect;
try expect(@field(Point, "z") == 1);
try expectEqual(1, @field(Point, "z"));
@field(Point, "z") = 2;
try expect(@field(Point, "z") == 2);
try expectEqual(2, @field(Point, "z"));
}
// test

View file

@ -3,8 +3,8 @@ const math = std.math;
const testing = std.testing;
test "fn reflection" {
try testing.expect(@typeInfo(@TypeOf(testing.expect)).@"fn".params[0].type.? == bool);
try testing.expect(@typeInfo(@TypeOf(testing.tmpDir)).@"fn".return_type.? == testing.TmpDir);
try testing.expectEqual(bool, @typeInfo(@TypeOf(testing.expect)).@"fn".params[0].type.?);
try testing.expectEqual(testing.TmpDir, @typeInfo(@TypeOf(testing.tmpDir)).@"fn".return_type.?);
try testing.expect(@typeInfo(@TypeOf(math.Log2Int)).@"fn".is_generic);
}

View file

@ -1,15 +1,15 @@
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
fn addFortyTwo(x: anytype) @TypeOf(x) {
return x + 42;
}
test "fn type inference" {
try expect(addFortyTwo(1) == 43);
try expect(@TypeOf(addFortyTwo(1)) == comptime_int);
try expectEqual(43, addFortyTwo(1));
try expectEqual(comptime_int, @TypeOf(addFortyTwo(1)));
const y: i64 = 2;
try expect(addFortyTwo(y) == 44);
try expect(@TypeOf(addFortyTwo(y)) == i64);
try expectEqual(44, addFortyTwo(y));
try expectEqual(i64, @TypeOf(addFortyTwo(y)));
}
// test

View file

@ -1,4 +1,4 @@
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
test "for basics" {
const items = [_]i32{ 4, 5, 3, 4, 0 };
@ -12,22 +12,22 @@ test "for basics" {
}
sum += value;
}
try expect(sum == 16);
try expectEqual(16, sum);
// To iterate over a portion of a slice, reslice.
for (items[0..1]) |value| {
sum += value;
}
try expect(sum == 20);
try expectEqual(20, sum);
// To access the index of iteration, specify a second condition as well
// as a second capture value.
var sum2: i32 = 0;
for (items, 0..) |_, i| {
try expect(@TypeOf(i) == usize);
try expectEqual(usize, @TypeOf(i));
sum2 += @as(i32, @intCast(i));
}
try expect(sum2 == 10);
try expectEqual(10, sum2);
// To iterate over consecutive integers, use the range syntax.
// Unbounded range is always a compile error.
@ -35,7 +35,7 @@ test "for basics" {
for (0..5) |i| {
sum3 += i;
}
try expect(sum3 == 10);
try expectEqual(10, sum3);
}
test "multi object for" {
@ -50,7 +50,7 @@ test "multi object for" {
count += i + j;
}
try expect(count == 21);
try expectEqual(21, count);
}
test "for reference" {
@ -62,9 +62,9 @@ test "for reference" {
value.* += 1;
}
try expect(items[0] == 4);
try expect(items[1] == 5);
try expect(items[2] == 3);
try expectEqual(4, items[0]);
try expectEqual(5, items[1]);
try expectEqual(3, items[2]);
}
test "for else" {
@ -79,10 +79,10 @@ test "for else" {
sum += value.?;
}
} else blk: {
try expect(sum == 12);
try expectEqual(12, sum);
break :blk sum;
};
try expect(result == 12);
try expectEqual(12, result);
}
// test

View file

@ -1,5 +1,5 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
test "nested break" {
var count: usize = 0;
@ -9,7 +9,7 @@ test "nested break" {
break :outer;
}
}
try expect(count == 1);
try expectEqual(1, count);
}
test "nested continue" {
@ -21,7 +21,7 @@ test "nested continue" {
}
}
try expect(count == 8);
try expectEqual(8, count);
}
// test

View file

@ -1,7 +1,7 @@
const std = @import("std");
const builtin = @import("builtin");
const native_arch = builtin.cpu.arch;
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
// Functions are declared like this
fn add(a: i8, b: i8) i8 {
@ -57,8 +57,8 @@ fn doOp(fnCall: Call2Op, op1: i8, op2: i8) i8 {
}
test "function" {
try expect(doOp(add, 5, 6) == 11);
try expect(doOp(sub2, 5, 6) == -1);
try expectEqual(11, doOp(add, 5, 6));
try expectEqual(-1, doOp(sub2, 5, 6));
}
// test

View file

@ -1,5 +1,5 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
comptime {
asm (
@ -14,7 +14,7 @@ comptime {
extern fn my_func(a: i32, b: i32) i32;
test "global assembly" {
try expect(my_func(12, 34) == 46);
try expectEqual(46, my_func(12, 34));
}
// test

View file

@ -4,13 +4,14 @@
// * anyerror!T
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
test "if expression" {
// If expressions are used instead of a ternary expression.
const a: u32 = 5;
const b: u32 = 4;
const result = if (a != b) 47 else 3089;
try expect(result == 47);
try expectEqual(result, 47);
}
test "if boolean" {
@ -32,7 +33,7 @@ test "if error union" {
const a: anyerror!u32 = 0;
if (a) |value| {
try expect(value == 0);
try expectEqual(value, 0);
} else |err| {
_ = err;
unreachable;
@ -43,17 +44,17 @@ test "if error union" {
_ = value;
unreachable;
} else |err| {
try expect(err == error.BadValue);
try expectEqual(err, error.BadValue);
}
// The else and |err| capture is strictly required.
if (a) |value| {
try expect(value == 0);
try expectEqual(value, 0);
} else |_| {}
// To check only the error value, use an empty block expression.
if (b) |_| {} else |err| {
try expect(err == error.BadValue);
try expectEqual(err, error.BadValue);
}
// Access the value by reference using a pointer capture.
@ -65,7 +66,7 @@ test "if error union" {
}
if (c) |value| {
try expect(value == 9);
try expectEqual(value, 9);
} else |_| {
unreachable;
}

View file

@ -1,11 +1,12 @@
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
test "if optional" {
// If expressions test for null.
const a: ?u32 = 0;
if (a) |value| {
try expect(value == 0);
try expectEqual(0, value);
} else {
unreachable;
}
@ -19,7 +20,7 @@ test "if optional" {
// The else is not required.
if (a) |value| {
try expect(value == 0);
try expectEqual(0, value);
}
// To test against null only, use the binary equality operator.
@ -34,7 +35,7 @@ test "if optional" {
}
if (c) |value| {
try expect(value == 2);
try expectEqual(2, value);
} else {
unreachable;
}
@ -46,7 +47,7 @@ test "if error union with optional" {
const a: anyerror!?u32 = 0;
if (a) |optional_value| {
try expect(optional_value.? == 0);
try expectEqual(0, optional_value.?);
} else |err| {
_ = err;
unreachable;
@ -54,7 +55,7 @@ test "if error union with optional" {
const b: anyerror!?u32 = null;
if (b) |optional_value| {
try expect(optional_value == null);
try expectEqual(null, optional_value);
} else |_| {
unreachable;
}
@ -64,7 +65,7 @@ test "if error union with optional" {
_ = optional_value;
unreachable;
} else |err| {
try expect(err == error.BadValue);
try expectEqual(error.BadValue, err);
}
// Access the value by reference by using a pointer capture each time.
@ -78,7 +79,7 @@ test "if error union with optional" {
}
if (d) |optional_value| {
try expect(optional_value.? == 9);
try expectEqual(9, optional_value.?);
} else |_| {
unreachable;
}

View file

@ -3,7 +3,7 @@ const std = @import("std");
test "pointer alignment safety" {
var array align(4) = [_]u32{ 0x11111111, 0x11111111 };
const bytes = std.mem.sliceAsBytes(array[0..]);
try std.testing.expect(foo(bytes) == 0x11111111);
try std.testing.expectEqual(0x11111111, foo(bytes));
}
fn foo(bytes: []u8) u32 {
const slice4 = bytes[1..5];

View file

@ -1,5 +1,5 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const SliceTypeA = extern struct {
len: usize,
@ -42,8 +42,8 @@ fn withSwitch(any: AnySlice) usize {
test "inline for and inline else similarity" {
const any = AnySlice{ .c = "hello" };
try expect(withFor(any) == 5);
try expect(withSwitch(any) == 5);
try expectEqual(5, withFor(any));
try expectEqual(5, withSwitch(any));
}
// test

View file

@ -1,4 +1,4 @@
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
test "inline for loop" {
const nums = [_]i32{ 2, 4, 6 };
@ -12,7 +12,7 @@ test "inline for loop" {
};
sum += typeNameLength(T);
}
try expect(sum == 9);
try expectEqual(9, sum);
}
fn typeNameLength(comptime T: type) usize {

View file

@ -1,5 +1,5 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const U = union(enum) {
a: u32,
@ -21,7 +21,7 @@ fn getNum(u: U) u32 {
test "test" {
const u = U{ .b = 42 };
try expect(getNum(u) == 42);
try expectEqual(42, getNum(u));
}
// test

View file

@ -1,4 +1,4 @@
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
test "inline while loop" {
comptime var i = 0;
@ -12,7 +12,7 @@ test "inline while loop" {
};
sum += typeNameLength(T);
}
try expect(sum == 9);
try expectEqual(9, sum);
}
fn typeNameLength(comptime T: type) usize {

View file

@ -1,10 +1,10 @@
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
test "@intFromPtr and @ptrFromInt" {
const ptr: *i32 = @ptrFromInt(0xdeadbee0);
const addr = @intFromPtr(ptr);
try expect(@TypeOf(addr) == usize);
try expect(addr == 0xdeadbee0);
try expectEqual(usize, @TypeOf(addr));
try expectEqual(0xdeadbee0, addr);
}
// test

View file

@ -1,6 +1,6 @@
const std = @import("std");
const builtin = @import("builtin");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const mem = std.mem;
test "integer widening" {
@ -10,13 +10,13 @@ test "integer widening" {
const d: u64 = c;
const e: u64 = d;
const f: u128 = e;
try expect(f == a);
try expectEqual(f, a);
}
test "implicit unsigned integer to signed integer" {
const a: u8 = 250;
const b: i16 = a;
try expect(b == 250);
try expectEqual(250, b);
}
test "float widening" {
@ -24,7 +24,7 @@ test "float widening" {
const b: f32 = a;
const c: f64 = b;
const d: f128 = c;
try expect(d == a);
try expectEqual(d, a);
}
// test

View file

@ -1,5 +1,5 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
test "labeled break from labeled block expression" {
var y: i32 = 123;
@ -8,8 +8,8 @@ test "labeled break from labeled block expression" {
y += 1;
break :blk y;
};
try expect(x == 124);
try expect(y == 124);
try expectEqual(124, x);
try expectEqual(124, y);
}
// test

View file

@ -1,5 +1,5 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const BitField = packed struct {
a: u3,
@ -14,7 +14,7 @@ var bit_field = BitField{
};
test "pointer to non-byte-aligned field" {
try expect(bar(&bit_field.b) == 2);
try expectEqual(2, bar(&bit_field.b));
}
fn bar(x: *const u3) u3 {

View file

@ -1,5 +1,4 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const mat4x5 = [4][5]f32{
@ -13,20 +12,20 @@ test "multidimensional arrays" {
try expectEqual(mat4x5[1], [_]f32{ 0.0, 1.0, 0.0, 1.0, 0.0 });
// Access the 2D array by indexing the outer array, and then the inner array.
try expect(mat4x5[3][4] == 9.9);
try expectEqual(9.9, mat4x5[3][4]);
// Here we iterate with for loops.
for (mat4x5, 0..) |row, row_index| {
for (row, 0..) |cell, column_index| {
if (row_index == column_index) {
try expect(cell == 1.0);
try expectEqual(1.0, cell);
}
}
}
// Initialize a multidimensional array to zeros.
const all_zero: [4][5]f32 = .{.{0} ** 5} ** 4;
try expect(all_zero[0][0] == 0);
try expectEqual(0, all_zero[0][0]);
}
// test

View file

@ -1,9 +1,9 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
test "namespaced container level variable" {
try expect(foo() == 1235);
try expect(foo() == 1236);
try expectEqual(1235, foo());
try expectEqual(1236, foo());
}
const S = struct {

View file

@ -1,14 +1,14 @@
const std = @import("std");
const builtin = @import("builtin");
const native_arch = builtin.cpu.arch;
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const WINAPI: std.builtin.CallingConvention = if (native_arch == .x86) .{ .x86_stdcall = .{} } else .c;
extern "kernel32" fn ExitProcess(exit_code: c_uint) callconv(WINAPI) noreturn;
test "foo" {
const value = bar() catch ExitProcess(1);
try expect(value == 1234);
try expectEqual(1234, value);
}
fn bar() anyerror!u32 {

View file

@ -1,21 +1,21 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
test "0-terminated sentinel array" {
const array = [_:0]u8{ 1, 2, 3, 4 };
try expect(@TypeOf(array) == [4:0]u8);
try expect(array.len == 4);
try expect(array[4] == 0);
try expectEqual([4:0]u8, @TypeOf(array));
try expectEqual(4, array.len);
try expectEqual(0, array[4]);
}
test "extra 0s in 0-terminated sentinel array" {
// The sentinel value may appear earlier, but does not influence the compile-time 'len'.
const array = [_:0]u8{ 1, 0, 0, 4 };
try expect(@TypeOf(array) == [4:0]u8);
try expect(array.len == 4);
try expect(array[4] == 0);
try expectEqual([4:0]u8, @TypeOf(array));
try expectEqual(4, array.len);
try expectEqual(0, array[4]);
}
// test

View file

@ -1,11 +1,11 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
test "0-terminated slice" {
const slice: [:0]const u8 = "hello";
try expect(slice.len == 5);
try expect(slice[5] == 0);
try expectEqual(5, slice.len);
try expectEqual(0, slice[5]);
}
// test

View file

@ -1,5 +1,5 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
test "0-terminated slicing" {
var array = [_]u8{ 3, 2, 1, 0, 3, 2, 1, 0 };
@ -7,8 +7,8 @@ test "0-terminated slicing" {
_ = &runtime_length;
const slice = array[0..runtime_length :0];
try expect(@TypeOf(slice) == [:0]u8);
try expect(slice.len == 3);
try expectEqual([:0]u8, @TypeOf(slice));
try expectEqual(3, slice.len);
}
// test

View file

@ -1,4 +1,4 @@
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
test "optional pointers" {
// Pointers cannot be null. If you want a null pointer, use the optional
@ -8,11 +8,11 @@ test "optional pointers" {
var x: i32 = 1;
ptr = &x;
try expect(ptr.?.* == 1);
try expectEqual(1, ptr.?.*);
// Optional pointers are the same size as normal pointers, because pointer
// value 0 is used as the null value.
try expect(@sizeOf(?*i32) == @sizeOf(*i32));
try expectEqual(@sizeOf(?*i32), @sizeOf(*i32));
}
// test

View file

@ -1,4 +1,4 @@
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
test "optional type" {
// Declare an optional and coerce from null:
@ -8,7 +8,7 @@ test "optional type" {
foo = 1234;
// Use compile-time reflection to access the child type of the optional:
try comptime expect(@typeInfo(@TypeOf(foo)).optional.child == i32);
try comptime expectEqual(i32, @typeInfo(@TypeOf(foo)).optional.child);
}
// test

View file

@ -1,5 +1,5 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const S = packed struct {
a: u32,
@ -9,7 +9,7 @@ test "overaligned pointer to packed struct" {
var foo: S align(4) = .{ .a = 1, .b = 2 };
const ptr: *align(4) S = &foo;
const ptr_to_b = &ptr.b;
try expect(ptr_to_b.* == 2);
try expectEqual(2, ptr_to_b.*);
}
// test

View file

@ -1,5 +1,5 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
test "packed struct equality" {
const S = packed struct {
@ -8,7 +8,7 @@ test "packed struct equality" {
};
const x: S = .{ .a = 1, .b = 2 };
const y: S = .{ .b = 2, .a = 1 };
try expect(x == y);
try expectEqual(x, y);
}
// test

View file

@ -1,5 +1,5 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const BitField = packed struct {
a: u3,
@ -14,8 +14,8 @@ var bit_field = BitField{
};
test "pointers of sub-byte-aligned fields share addresses" {
try expect(@intFromPtr(&bit_field.a) == @intFromPtr(&bit_field.b));
try expect(@intFromPtr(&bit_field.a) == @intFromPtr(&bit_field.c));
try expectEqual(@intFromPtr(&bit_field.a), @intFromPtr(&bit_field.b));
try expectEqual(@intFromPtr(&bit_field.a), @intFromPtr(&bit_field.c));
}
// test

View file

@ -1,6 +1,6 @@
const std = @import("std");
const native_endian = @import("builtin").target.cpu.arch.endian();
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const Full = packed struct {
number: u16,
@ -17,23 +17,23 @@ test "@bitCast between packed structs" {
}
fn doTheTest() !void {
try expect(@sizeOf(Full) == 2);
try expect(@sizeOf(Divided) == 2);
try expectEqual(2, @sizeOf(Full));
try expectEqual(2, @sizeOf(Divided));
const full = Full{ .number = 0x1234 };
const divided: Divided = @bitCast(full);
try expect(divided.half1 == 0x34);
try expect(divided.quarter3 == 0x2);
try expect(divided.quarter4 == 0x1);
try expectEqual(0x34, divided.half1);
try expectEqual(0x2, divided.quarter3);
try expectEqual(0x1, divided.quarter4);
const ordered: [2]u8 = @bitCast(full);
switch (native_endian) {
.big => {
try expect(ordered[0] == 0x12);
try expect(ordered[1] == 0x34);
try expectEqual(0x12, ordered[0]);
try expectEqual(0x34, ordered[1]);
},
.little => {
try expect(ordered[0] == 0x34);
try expect(ordered[1] == 0x12);
try expectEqual(0x34, ordered[0]);
try expectEqual(0x12, ordered[1]);
},
}
}

View file

@ -11,10 +11,10 @@ fn foo(point: Point) i32 {
return point.x + point.y;
}
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
test "pass struct to function" {
try expect(foo(Point{ .x = 1, .y = 2 }) == 3);
try expectEqual(3, foo(Point{ .x = 1, .y = 2 }));
}
// test

View file

@ -1,20 +1,20 @@
const std = @import("std");
const expect = std.testing.expect;
const mem = std.mem;
const expectEqual = std.testing.expectEqual;
const expectEqualStrings = std.testing.expectEqualStrings;
test "peer resolve int widening" {
const a: i8 = 12;
const b: i16 = 34;
const c = a + b;
try expect(c == 46);
try expect(@TypeOf(c) == i16);
try expectEqual(46, c);
try expectEqual(i16, @TypeOf(c));
}
test "peer resolve arrays of different size to const slice" {
try expect(mem.eql(u8, boolToStr(true), "true"));
try expect(mem.eql(u8, boolToStr(false), "false"));
try comptime expect(mem.eql(u8, boolToStr(true), "true"));
try comptime expect(mem.eql(u8, boolToStr(false), "false"));
try expectEqualStrings("true", boolToStr(true));
try expectEqualStrings("false", boolToStr(false));
try comptime expectEqualStrings("true", boolToStr(true));
try comptime expectEqualStrings("false", boolToStr(false));
}
fn boolToStr(b: bool) []const u8 {
return if (b) "true" else "false";
@ -27,16 +27,16 @@ test "peer resolve array and const slice" {
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";
try expect(mem.eql(u8, value1, "aoeu"));
try expect(mem.eql(u8, value2, "zz"));
try expectEqualStrings("aoeu", value1);
try expectEqualStrings("zz", value2);
}
test "peer type resolution: ?T and T" {
try expect(peerTypeTAndOptionalT(true, false).? == 0);
try expect(peerTypeTAndOptionalT(false, false).? == 3);
try expectEqual(0, peerTypeTAndOptionalT(true, false).?);
try expectEqual(3, peerTypeTAndOptionalT(false, false).?);
comptime {
try expect(peerTypeTAndOptionalT(true, false).? == 0);
try expect(peerTypeTAndOptionalT(false, false).? == 3);
try expectEqual(0, peerTypeTAndOptionalT(true, false).?);
try expectEqual(3, peerTypeTAndOptionalT(false, false).?);
}
}
fn peerTypeTAndOptionalT(c: bool, b: bool) ?usize {
@ -48,11 +48,11 @@ fn peerTypeTAndOptionalT(c: bool, b: bool) ?usize {
}
test "peer type resolution: *[0]u8 and []const u8" {
try expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);
try expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);
try expectEqual(0, peerTypeEmptyArrayAndSlice(true, "hi").len);
try expectEqual(1, peerTypeEmptyArrayAndSlice(false, "hi").len);
comptime {
try expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);
try expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);
try expectEqual(0, peerTypeEmptyArrayAndSlice(true, "hi").len);
try expectEqual(1, peerTypeEmptyArrayAndSlice(false, "hi").len);
}
}
fn peerTypeEmptyArrayAndSlice(a: bool, slice: []const u8) []const u8 {
@ -66,14 +66,14 @@ test "peer type resolution: *[0]u8, []const u8, and anyerror![]u8" {
{
var data = "hi".*;
const slice = data[0..];
try expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0);
try expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1);
try expectEqual(0, (try peerTypeEmptyArrayAndSliceAndError(true, slice)).len);
try expectEqual(1, (try peerTypeEmptyArrayAndSliceAndError(false, slice)).len);
}
comptime {
var data = "hi".*;
const slice = data[0..];
try expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0);
try expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1);
try expectEqual(0, (try peerTypeEmptyArrayAndSliceAndError(true, slice)).len);
try expectEqual(1, (try peerTypeEmptyArrayAndSliceAndError(false, slice)).len);
}
}
fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) anyerror![]u8 {
@ -87,8 +87,8 @@ fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) anyerror![]u8 {
test "peer type resolution: *const T and ?*T" {
const a: *const usize = @ptrFromInt(0x123456780);
const b: ?*usize = @ptrFromInt(0x123456780);
try expect(a == b);
try expect(b == a);
try expectEqual(a, b);
try expectEqual(b, a);
}
test "peer type resolution: error union switch" {
@ -104,7 +104,7 @@ test "peer type resolution: error union switch" {
error.B => 1,
error.C => null,
};
try expect(@TypeOf(b) == ?u32);
try expectEqual(?u32, @TypeOf(b));
// The non-error and error cases are only peers if the error case is just a switch expression;
// the pattern `x catch |err| blk: { switch (err) {...} }` does not consider the unwrapped `x`
@ -114,7 +114,7 @@ test "peer type resolution: error union switch" {
error.B => 1,
error.C => null,
};
try expect(@TypeOf(c) == ?u32);
try expectEqual(?u32, @TypeOf(c));
}
// test

View file

@ -1,19 +1,19 @@
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
test "pointer arithmetic with many-item pointer" {
const array = [_]i32{ 1, 2, 3, 4 };
var ptr: [*]const i32 = &array;
try expect(ptr[0] == 1);
try expectEqual(1, ptr[0]);
ptr += 1;
try expect(ptr[0] == 2);
try expectEqual(2, ptr[0]);
// slicing a many-item pointer without an end is equivalent to
// pointer arithmetic: `ptr[start..] == ptr + start`
try expect(ptr[1..] == ptr + 1);
try expectEqual(ptr[1..], ptr + 1);
// subtraction between any two pointers except slices based on element size is supported
try expect(&ptr[1] - &ptr[0] == 1);
try expectEqual(1, &ptr[1] - &ptr[0]);
}
test "pointer arithmetic with slices" {
@ -22,14 +22,14 @@ test "pointer arithmetic with slices" {
_ = &length; // suppress 'var is never mutated' error
var slice = array[length..array.len];
try expect(slice[0] == 1);
try expect(slice.len == 4);
try expectEqual(1, slice[0]);
try expectEqual(4, slice.len);
slice.ptr += 1;
// now the slice is in an bad state since len has not been updated
try expect(slice[0] == 2);
try expect(slice.len == 4);
try expectEqual(2, slice[0]);
try expectEqual(4, slice.len);
}
// test

View file

@ -1,23 +1,23 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
test "pointer casting" {
const bytes align(@alignOf(u32)) = [_]u8{ 0x12, 0x12, 0x12, 0x12 };
const u32_ptr: *const u32 = @ptrCast(&bytes);
try expect(u32_ptr.* == 0x12121212);
try expectEqual(0x12121212, u32_ptr.*);
// Even this example is contrived - there are better ways to do the above than
// pointer casting. For example, using a slice narrowing cast:
const u32_value = std.mem.bytesAsSlice(u32, bytes[0..])[0];
try expect(u32_value == 0x12121212);
try expectEqual(0x12121212, u32_value);
// And even another way, the most straightforward way to do it:
try expect(@as(u32, @bitCast(bytes)) == 0x12121212);
try expectEqual(0x12121212, @as(u32, @bitCast(bytes)));
}
test "pointer child type" {
// pointer types have a `child` field which tells you the type they point to.
try expect(@typeInfo(*u32).pointer.child == u32);
try expectEqual(u32, @typeInfo(*u32).pointer.child);
}
// test

View file

@ -1,11 +1,11 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqualStrings = std.testing.expectEqualStrings;
const mem = std.mem;
test "cast *[1][*:0]const u8 to []const ?[*:0]const u8" {
const window_name = [1][*:0]const u8{"window name"};
const x: []const ?[*:0]const u8 = &window_name;
try expect(mem.eql(u8, mem.span(x[0].?), "window name"));
try expectEqualStrings("window name", mem.span(x[0].?));
}
// test

View file

@ -1,5 +1,5 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const BitField = packed struct {
a: u3,
@ -15,7 +15,7 @@ var foo = BitField{
test "pointer to non-byte-aligned field" {
const ptr = &foo.b;
try expect(ptr.* == 2);
try expectEqual(2, ptr.*);
}
// test

View file

@ -1,15 +1,15 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
test "vector @reduce" {
const V = @Vector(4, i32);
const value = V{ 1, -1, 1, -1 };
const result = value > @as(V, @splat(0));
// result is { true, false, true, false };
try comptime expect(@TypeOf(result) == @Vector(4, bool));
try comptime expectEqual(@Vector(4, bool), @TypeOf(result));
const is_all_true = @reduce(.And, result);
try comptime expect(@TypeOf(is_all_true) == bool);
try expect(is_all_true == false);
try comptime expectEqual(bool, @TypeOf(is_all_true));
try expectEqual(false, is_all_true);
}
// test

View file

@ -1,10 +1,10 @@
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
test "@round" {
try expect(@round(1.4) == 1);
try expect(@round(1.5) == 2);
try expect(@round(-1.4) == -1);
try expect(@round(-2.5) == -3);
try expectEqual(1, @round(1.4));
try expectEqual(2, @round(1.5));
try expectEqual(-1, @round(-1.4));
try expectEqual(-3, @round(-2.5));
}
// test

View file

@ -1,5 +1,5 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
test "sentinel mismatch" {
var array = [_]u8{ 3, 2, 1, 0 };

View file

@ -1,5 +1,5 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqualStrings = std.testing.expectEqualStrings;
test "vector @shuffle" {
const a = @Vector(7, u8){ 'o', 'l', 'h', 'e', 'r', 'z', 'w' };
@ -9,12 +9,12 @@ test "vector @shuffle" {
// Notice that we can re-order, duplicate, or omit elements of the input vector
const mask1 = @Vector(5, i32){ 2, 3, 1, 1, 0 };
const res1: @Vector(5, u8) = @shuffle(u8, a, undefined, mask1);
try expect(std.mem.eql(u8, &@as([5]u8, res1), "hello"));
try expectEqualStrings("hello", &@as([5]u8, res1));
// Combining two vectors
const mask2 = @Vector(6, i32){ -1, 0, 4, 1, -2, -3 };
const res2: @Vector(6, u8) = @shuffle(u8, a, b, mask2);
try expect(std.mem.eql(u8, &@as([6]u8, res2), "world!"));
try expectEqualStrings("world!", &@as([6]u8, res2));
}
// test

View file

@ -1,5 +1,5 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const Payload = union {
int: i64,
@ -8,9 +8,9 @@ const Payload = union {
};
test "simple union" {
var payload = Payload{ .int = 1234 };
try expect(payload.int == 1234);
try expectEqual(1234, payload.int);
payload = Payload{ .float = 12.34 };
try expect(payload.float == 12.34);
try expectEqual(12.34, payload.float);
}
// test

View file

@ -1,4 +1,4 @@
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
test "address of syntax" {
// Get the address of a variable:
@ -6,17 +6,17 @@ test "address of syntax" {
const x_ptr = &x;
// Dereference a pointer:
try expect(x_ptr.* == 1234);
try expectEqual(1234, x_ptr.*);
// When you get the address of a const variable, you get a const single-item pointer.
try expect(@TypeOf(x_ptr) == *const i32);
try expectEqual(*const i32, @TypeOf(x_ptr));
// If you want to mutate the value, you'd need an address of a mutable variable:
var y: i32 = 5678;
const y_ptr = &y;
try expect(@TypeOf(y_ptr) == *i32);
try expectEqual(*i32, @TypeOf(y_ptr));
y_ptr.* += 1;
try expect(y_ptr.* == 5679);
try expectEqual(5679, y_ptr.*);
}
test "pointer array access" {
@ -25,11 +25,11 @@ test "pointer array access" {
// does not support pointer arithmetic.
var array = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
const ptr = &array[2];
try expect(@TypeOf(ptr) == *u8);
try expectEqual(*u8, @TypeOf(ptr));
try expect(array[2] == 3);
try expectEqual(3, array[2]);
ptr.* += 1;
try expect(array[2] == 4);
try expectEqual(4, array[2]);
}
test "slice syntax" {
@ -39,11 +39,11 @@ test "slice syntax" {
// Convert to array pointer using slice syntax:
const x_array_ptr = x_ptr[0..1];
try expect(@TypeOf(x_array_ptr) == *[1]i32);
try expectEqual(*[1]i32, @TypeOf(x_array_ptr));
// Coerce to many-item pointer:
const x_many_ptr: [*]i32 = x_array_ptr;
try expect(x_many_ptr[0] == 1234);
try expectEqual(1234, x_many_ptr[0]);
}
// test

View file

@ -1,15 +1,15 @@
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
test "pointer slicing" {
var array = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var start: usize = 2; // var to make it runtime-known
_ = &start; // suppress 'var is never mutated' error
const slice = array[start..4];
try expect(slice.len == 2);
try expectEqual(2, slice.len);
try expect(array[3] == 4);
try expectEqual(4, array[3]);
slice[1] += 1;
try expect(array[3] == 5);
try expectEqual(5, array[3]);
}
// test

View file

@ -1,6 +1,6 @@
const std = @import("std");
const expect = std.testing.expect;
const mem = std.mem;
const expectEqual = std.testing.expectEqual;
const expectEqualStrings = std.testing.expectEqualStrings;
const fmt = std.fmt;
test "using slices for strings" {
@ -23,13 +23,13 @@ test "using slices for strings" {
// Generally, you can use UTF-8 and not worry about whether something is a
// string. If you don't need to deal with individual characters, no need
// to decode.
try expect(mem.eql(u8, hello_world, "hello 世界"));
try expectEqualStrings("hello 世界", hello_world);
}
test "slice pointer" {
var array: [10]u8 = undefined;
const ptr = &array;
try expect(@TypeOf(ptr) == *[10]u8);
try expectEqual(*[10]u8, @TypeOf(ptr));
// A pointer to an array can be sliced just like an array:
var start: usize = 0;
@ -37,16 +37,16 @@ test "slice pointer" {
_ = .{ &start, &end };
const slice = ptr[start..end];
// The slice is mutable because we sliced a mutable pointer.
try expect(@TypeOf(slice) == []u8);
try expectEqual([]u8, @TypeOf(slice));
slice[2] = 3;
try expect(array[2] == 3);
try expectEqual(3, array[2]);
// Again, slicing with comptime-known indexes will produce another pointer
// to an array:
const ptr2 = slice[2..3];
try expect(ptr2.len == 1);
try expect(ptr2[0] == 3);
try expect(@TypeOf(ptr2) == *[1]u8);
try expectEqual(1, ptr2.len);
try expectEqual(3, ptr2[0]);
try expectEqual(*[1]u8, @TypeOf(ptr2));
}
// test

View file

@ -1,16 +1,16 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqualSlices = std.testing.expectEqualSlices;
test "vector @splat" {
const scalar: u32 = 5;
const result: @Vector(4, u32) = @splat(scalar);
try expect(std.mem.eql(u32, &@as([4]u32, result), &[_]u32{ 5, 5, 5, 5 }));
try expectEqualSlices(u32, &[_]u32{ 5, 5, 5, 5 }, &@as([4]u32, result));
}
test "array @splat" {
const scalar: u32 = 5;
const result: [4]u32 = @splat(scalar);
try expect(std.mem.eql(u32, &@as([4]u32, result), &[_]u32{ 5, 5, 5, 5 }));
try expectEqualSlices(u32, &[_]u32{ 5, 5, 5, 5 }, &@as([4]u32, result));
}
// test

View file

@ -1,5 +1,6 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
test "@src" {
try doTheTest();
@ -8,8 +9,8 @@ test "@src" {
fn doTheTest() !void {
const src = @src();
try expect(src.line == 9);
try expect(src.column == 17);
try expectEqual(10, src.line);
try expectEqual(17, src.column);
try expect(std.mem.endsWith(u8, src.fn_name, "doTheTest"));
try expect(std.mem.endsWith(u8, src.file, "test_src_builtin.zig"));
}

View file

@ -1,9 +1,9 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
test "static local variable" {
try expect(foo() == 1235);
try expect(foo() == 1236);
try expectEqual(1235, foo());
try expectEqual(1236, foo());
}
fn foo() i32 {

View file

@ -1,5 +1,5 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const Point = struct { x: i32, y: i32 };
@ -8,8 +8,8 @@ test "anonymous struct literal" {
.x = 13,
.y = 67,
};
try expect(pt.x == 13);
try expect(pt.y == 67);
try expectEqual(13, pt.x);
try expectEqual(67, pt.y);
}
// test

View file

@ -34,12 +34,12 @@ const Vec3 = struct {
test "dot product" {
const v1 = Vec3.init(1.0, 0.0, 0.0);
const v2 = Vec3.init(0.0, 1.0, 0.0);
try expect(v1.dot(v2) == 0.0);
try expectEqual(0.0, v1.dot(v2));
// Other than being available to call with dot syntax, struct methods are
// not special. You can reference them as any other declaration inside
// the struct:
try expect(Vec3.dot(v1, v2) == 0.0);
try expectEqual(0.0, Vec3.dot(v1, v2));
}
// Structs can have declarations.
@ -48,8 +48,8 @@ const Empty = struct {
pub const PI = 3.14;
};
test "struct namespaced variable" {
try expect(Empty.PI == 3.14);
try expect(@sizeOf(Empty) == 0);
try expectEqual(3.14, Empty.PI);
try expectEqual(0, @sizeOf(Empty));
// Empty structs can be instantiated the same as usual.
const does_nothing: Empty = .{};
@ -69,7 +69,7 @@ test "field parent pointer" {
.y = 0.5678,
};
setYBasedOnX(&point.x, 0.9);
try expect(point.y == 0.9);
try expectEqual(0.9, point.y);
}
// Structs can be returned from functions.
@ -89,19 +89,19 @@ fn LinkedList(comptime T: type) type {
test "linked list" {
// Functions called at compile-time are memoized.
try expect(LinkedList(i32) == LinkedList(i32));
try expectEqual(LinkedList(i32), LinkedList(i32));
const list = LinkedList(i32){
.first = null,
.last = null,
.len = 0,
};
try expect(list.len == 0);
try expectEqual(0, list.len);
// Since types are first class values you can instantiate the type
// by assigning it to a variable:
const ListOfInts = LinkedList(i32);
try expect(ListOfInts == LinkedList(i32));
try expectEqual(LinkedList(i32), ListOfInts);
var node = ListOfInts.Node{
.prev = null,
@ -117,10 +117,10 @@ test "linked list" {
// When using a pointer to a struct, fields can be accessed directly,
// without explicitly dereferencing the pointer.
// So you can do
try expect(list2.first.?.data == 1234);
// instead of try expect(list2.first.?.*.data == 1234);
try expectEqual(1234, list2.first.?.data);
// instead of try expectEqual(1234, list2.first.?.*.data);
}
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
// test

View file

@ -1,6 +1,6 @@
const std = @import("std");
const builtin = @import("builtin");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
test "switch simple" {
const a: u64 = 10;
@ -40,7 +40,7 @@ test "switch simple" {
else => 9,
};
try expect(b == 1);
try expectEqual(1, b);
}
// Switch expressions can be used outside a function:

View file

@ -1,5 +1,5 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const ComplexTypeTag = enum {
ok,
@ -18,7 +18,7 @@ test "modify tagged union in switch" {
ComplexTypeTag.not_ok => unreachable,
}
try expect(c.ok == 43);
try expectEqual(43, c.ok);
}
// test

View file

@ -1,4 +1,4 @@
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
test "switch on tagged union" {
const Point = struct {
@ -31,8 +31,8 @@ test "switch on tagged union" {
Item.d => 8,
};
try expect(b == 6);
try expect(a.c.x == 2);
try expectEqual(6, b);
try expectEqual(2, a.c.x);
}
// test

View file

@ -1,5 +1,5 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqualSlices = std.testing.expectEqualSlices;
const Small2 = union(enum) {
a: i32,
@ -7,7 +7,7 @@ const Small2 = union(enum) {
c: u8,
};
test "@tagName" {
try expect(std.mem.eql(u8, @tagName(Small2.a), "a"));
try expectEqualSlices(u8, "a", @tagName(Small2.a));
}
// test

View file

@ -1,5 +1,5 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const ComplexTypeTag = enum {
ok,
@ -12,10 +12,10 @@ const ComplexType = union(ComplexTypeTag) {
test "switch on tagged union" {
const c = ComplexType{ .ok = 42 };
try expect(@as(ComplexTypeTag, c) == ComplexTypeTag.ok);
try expectEqual(ComplexTypeTag.ok, @as(ComplexTypeTag, c));
switch (c) {
.ok => |value| try expect(value == 42),
.ok => |value| try expectEqual(42, value),
.not_ok => unreachable,
}
@ -29,7 +29,7 @@ test "switch on tagged union" {
}
test "get tag type" {
try expect(std.meta.Tag(ComplexType) == ComplexTypeTag);
try expectEqual(ComplexTypeTag, std.meta.Tag(ComplexType));
}
// test

View file

@ -1,5 +1,5 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const Tagged = union(enum(u32)) {
int: i64 = 123,
@ -8,10 +8,10 @@ const Tagged = union(enum(u32)) {
test "tag values" {
const int: Tagged = .{ .int = -40 };
try expect(@intFromEnum(int) == 123);
try expectEqual(123, @intFromEnum(int));
const boolean: Tagged = .{ .boolean = false };
try expect(@intFromEnum(boolean) == 67);
try expectEqual(67, @intFromEnum(boolean));
}
// test

View file

@ -1,10 +1,10 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
test "@This()" {
var items = [_]i32{ 1, 2, 3, 4 };
const list = List(i32){ .items = items[0..] };
try expect(list.length() == 4);
try expectEqual(4, list.length());
}
fn List(comptime T: type) type {

View file

@ -1,10 +1,10 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
test "integer truncation" {
const a: u16 = 0xabcd;
const b: u8 = @truncate(a);
try expect(b == 0xcd);
try expectEqual(0xcd, b);
}
// test

View file

@ -1,5 +1,6 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
test "tuple" {
const values = .{
@ -8,14 +9,14 @@ test "tuple" {
true,
"hi",
} ++ .{false} ** 2;
try expect(values[0] == 1234);
try expect(values[4] == false);
try expectEqual(1234, values[0]);
try expectEqual(false, values[4]);
inline for (values, 0..) |v, i| {
if (i != 2) continue;
try expect(v);
}
try expect(values.len == 6);
try expect(values.@"3"[0] == 'h');
try expectEqual(6, values.len);
try expectEqual('h', values.@"3"[0]);
}
// test

View file

@ -1,14 +1,14 @@
const std = @import("std");
const builtin = @import("builtin");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
test "variable alignment" {
var x: i32 = 1234;
const align_of_i32 = @alignOf(@TypeOf(x));
try expect(@TypeOf(&x) == *i32);
try expect(*i32 == *align(align_of_i32) i32);
try expectEqual(*i32, @TypeOf(&x));
try expectEqual(*align(align_of_i32) i32, *i32);
if (builtin.target.cpu.arch == .x86_64) {
try expect(@typeInfo(*i32).pointer.alignment == 4);
try expectEqual(4, @typeInfo(*i32).pointer.alignment);
}
}

View file

@ -1,14 +1,14 @@
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
var foo: u8 align(4) = 100;
test "global variable alignment" {
try expect(@typeInfo(@TypeOf(&foo)).pointer.alignment == 4);
try expect(@TypeOf(&foo) == *align(4) u8);
try expectEqual(4, @typeInfo(@TypeOf(&foo)).pointer.alignment);
try expectEqual(*align(4) u8, @TypeOf(&foo));
const as_pointer_to_array: *align(4) [1]u8 = &foo;
const as_slice: []align(4) u8 = as_pointer_to_array;
const as_unaligned_slice: []u8 = as_slice;
try expect(as_unaligned_slice[0] == 100);
try expectEqual(100, as_unaligned_slice[0]);
}
fn derp() align(@sizeOf(usize) * 2) i32 {
@ -18,17 +18,17 @@ fn noop1() align(1) void {}
fn noop4() align(4) void {}
test "function alignment" {
try expect(derp() == 1234);
try expect(@TypeOf(derp) == fn () i32);
try expect(@TypeOf(&derp) == *align(@sizeOf(usize) * 2) const fn () i32);
try expectEqual(1234, derp());
try expectEqual(fn () i32, @TypeOf(derp));
try expectEqual(*align(@sizeOf(usize) * 2) const fn () i32, @TypeOf(&derp));
noop1();
try expect(@TypeOf(noop1) == fn () void);
try expect(@TypeOf(&noop1) == *align(1) const fn () void);
try expectEqual(fn () void, @TypeOf(noop1));
try expectEqual(*align(1) const fn () void, @TypeOf(&noop1));
noop4();
try expect(@TypeOf(noop4) == fn () void);
try expect(@TypeOf(&noop4) == *align(4) const fn () void);
try expectEqual(fn () void, @TypeOf(noop4));
try expectEqual(*align(4) const fn () void, @TypeOf(&noop4));
}
// test

View file

@ -4,7 +4,7 @@ const testing = std.testing;
pub extern "c" fn printf(format: [*:0]const u8, ...) c_int;
test "variadic function" {
try testing.expect(printf("Hello, world!\n") == 14);
try testing.expectEqual(14, printf("Hello, world!\n"));
try testing.expect(@typeInfo(@TypeOf(printf)).@"fn".is_var_args);
}

View file

@ -1,8 +1,8 @@
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
test "volatile" {
const mmio_ptr: *volatile u8 = @ptrFromInt(0x12345678);
try expect(@TypeOf(mmio_ptr) == *volatile u8);
try expectEqual(*volatile u8, @TypeOf(mmio_ptr));
}
// test

View file

@ -1,13 +1,13 @@
const std = @import("std");
const native_arch = @import("builtin").target.cpu.arch;
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
test "@wasmMemoryGrow" {
if (native_arch != .wasm32) return error.SkipZigTest;
const prev = @wasmMemorySize(0);
try expect(prev == @wasmMemoryGrow(0, 1));
try expect(prev + 1 == @wasmMemorySize(0));
try expectEqual(@wasmMemoryGrow(0, 1), prev);
try expectEqual(@wasmMemorySize(0), prev + 1);
}
// test

View file

@ -1,11 +1,11 @@
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
test "while basic" {
var i: usize = 0;
while (i < 10) {
i += 1;
}
try expect(i == 10);
try expectEqual(10, i);
}
// test

View file

@ -1,4 +1,4 @@
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
test "while break" {
var i: usize = 0;
@ -7,7 +7,7 @@ test "while break" {
break;
i += 1;
}
try expect(i == 10);
try expectEqual(10, i);
}
// test

View file

@ -1,4 +1,4 @@
const expect = @import("std").testing.expect;
const expectEqual = @import("std").testing.expectEqual;
test "while continue" {
var i: usize = 0;
@ -8,7 +8,7 @@ test "while continue" {
continue;
break;
}
try expect(i == 10);
try expectEqual(10, i);
}
// test

View file

@ -1,9 +1,10 @@
const expectEqual = @import("std").testing.expectEqual;
const expect = @import("std").testing.expect;
test "while loop continue expression" {
var i: usize = 0;
while (i < 10) : (i += 1) {}
try expect(i == 10);
try expectEqual(10, i);
}
test "while loop continue expression, more complicated" {

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