| author | |
| committer | |
| log | 333055ced72ae73c1ec74a66aa9436291d8b7d1d |
| tree | 7a195896c91173a99cd5014d85c372de367b785a |
| parent | a3a9dc111da7a8455f9a476782f291f349d0ed8d |
105 files changed, 422 insertions(+), 410 deletions(-)
doc/langref/error_union_parsing_u64.zig+1-1| ... | ... | @@ -35,7 +35,7 @@ fn charToDigit(c: u8) u8 { |
| 35 | 35 | |
| 36 | 36 | test "parse u64" { |
| 37 | 37 | const result = try parseU64("1234", 10); |
| 38 | try std.testing.expect(result == 1234); | |
| 38 | try std.testing.expectEqual(1234, result); | |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | // test |
doc/langref/result_location_interfering_with_swap.zig+3-3| ... | ... | @@ -1,4 +1,4 @@ |
| 1 | const expect = @import("std").testing.expect; | |
| 1 | const expectEqual = @import("std").testing.expectEqual; | |
| 2 | 2 | test "attempt to swap array elements with array initializer" { |
| 3 | 3 | var arr: [2]u32 = .{ 1, 2 }; |
| 4 | 4 | arr = .{ arr[1], arr[0] }; |
| ... | ... | @@ -6,8 +6,8 @@ test "attempt to swap array elements with array initializer" { |
| 6 | 6 | // arr[0] = arr[1]; |
| 7 | 7 | // arr[1] = arr[0]; |
| 8 | 8 | // So this fails! |
| 9 | try expect(arr[0] == 2); // succeeds | |
| 10 | try expect(arr[1] == 1); // fails | |
| 9 | try expectEqual(2, arr[0]); // succeeds | |
| 10 | try expectEqual(1, arr[1]); // fails | |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | // test_error= |
doc/langref/test_TypeOf_builtin.zig+3-3| ... | ... | @@ -1,11 +1,11 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | test "no runtime side effects" { |
| 5 | 5 | var data: i32 = 0; |
| 6 | 6 | const T = @TypeOf(foo(i32, &data)); |
| 7 | try comptime expect(T == i32); | |
| 8 | try expect(data == 0); | |
| 7 | try comptime expectEqual(i32, T); | |
| 8 | try expectEqual(0, data); | |
| 9 | 9 | } |
| 10 | 10 | |
| 11 | 11 | fn foo(comptime T: type, ptr: *T) T { |
doc/langref/test_allocator.zig+2-2| ... | ... | @@ -1,13 +1,13 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | const Allocator = std.mem.Allocator; |
| 3 | const expect = std.testing.expect; | |
| 3 | const expectEqualStrings = std.testing.expectEqualStrings; | |
| 4 | 4 | |
| 5 | 5 | test "using an allocator" { |
| 6 | 6 | var buffer: [100]u8 = undefined; |
| 7 | 7 | var fba = std.heap.FixedBufferAllocator.init(&buffer); |
| 8 | 8 | const allocator = fba.allocator(); |
| 9 | 9 | const result = try concat(allocator, "foo", "bar"); |
| 10 | try expect(std.mem.eql(u8, "foobar", result)); | |
| 10 | try expectEqualStrings("foobar", result); | |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | fn concat(allocator: Allocator, a: []const u8, b: []const u8) ![]u8 { |
doc/langref/test_allowzero.zig+2-2| ... | ... | @@ -1,11 +1,11 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | test "allowzero" { |
| 5 | 5 | var zero: usize = 0; // var to make to runtime-known |
| 6 | 6 | _ = &zero; // suppress 'var is never mutated' error |
| 7 | 7 | const ptr: *allowzero i32 = @ptrFromInt(zero); |
| 8 | try expect(@intFromPtr(ptr) == 0); | |
| 8 | try expectEqual(0, @intFromPtr(ptr)); | |
| 9 | 9 | } |
| 10 | 10 | |
| 11 | 11 | // test |
doc/langref/test_anonymous_struct.zig+5-4| ... | ... | @@ -1,5 +1,6 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | const expect = std.testing.expect; |
| 3 | const expectEqual = std.testing.expectEqual; | |
| 3 | 4 | |
| 4 | 5 | test "fully anonymous struct" { |
| 5 | 6 | try check(.{ |
| ... | ... | @@ -11,11 +12,11 @@ test "fully anonymous struct" { |
| 11 | 12 | } |
| 12 | 13 | |
| 13 | 14 | fn check(args: anytype) !void { |
| 14 | try expect(args.int == 1234); | |
| 15 | try expect(args.float == 12.34); | |
| 15 | try expectEqual(1234, args.int); | |
| 16 | try expectEqual(12.34, args.float); | |
| 16 | 17 | try expect(args.b); |
| 17 | try expect(args.s[0] == 'h'); | |
| 18 | try expect(args.s[1] == 'i'); | |
| 18 | try expectEqual('h', args.s[0]); | |
| 19 | try expectEqual('i', args.s[1]); | |
| 19 | 20 | } |
| 20 | 21 | |
| 21 | 22 | // test |
doc/langref/test_anonymous_union.zig+3-3| ... | ... | @@ -1,5 +1,5 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | const Number = union { |
| 5 | 5 | int: i32, |
| ... | ... | @@ -9,8 +9,8 @@ const Number = union { |
| 9 | 9 | test "anonymous union literal syntax" { |
| 10 | 10 | const i: Number = .{ .int = 42 }; |
| 11 | 11 | const f = makeNumber(); |
| 12 | try expect(i.int == 42); | |
| 13 | try expect(f.float == 12.34); | |
| 12 | try expectEqual(42, i.int); | |
| 13 | try expectEqual(12.34, f.float); | |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | fn makeNumber() Number { |
doc/langref/test_arrays.zig+9-9| ... | ... | @@ -1,4 +1,4 @@ |
| 1 | const expect = @import("std").testing.expect; | |
| 1 | const expectEqual = @import("std").testing.expectEqual; | |
| 2 | 2 | const assert = @import("std").debug.assert; |
| 3 | 3 | const mem = @import("std").mem; |
| 4 | 4 | |
| ... | ... | @@ -29,7 +29,7 @@ test "iterate over an array" { |
| 29 | 29 | for (message) |byte| { |
| 30 | 30 | sum += byte; |
| 31 | 31 | } |
| 32 | try expect(sum == 'h' + 'e' + 'l' * 2 + 'o'); | |
| 32 | try expectEqual('h' + 'e' + 'l' * 2 + 'o', sum); | |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | // modifiable array |
| ... | ... | @@ -39,8 +39,8 @@ test "modify an array" { |
| 39 | 39 | for (&some_integers, 0..) |*item, i| { |
| 40 | 40 | item.* = @intCast(i); |
| 41 | 41 | } |
| 42 | try expect(some_integers[10] == 10); | |
| 43 | try expect(some_integers[99] == 99); | |
| 42 | try expectEqual(10, some_integers[10]); | |
| 43 | try expectEqual(99, some_integers[99]); | |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | // array concatenation works if the values are known |
| ... | ... | @@ -91,8 +91,8 @@ const Point = struct { |
| 91 | 91 | }; |
| 92 | 92 | |
| 93 | 93 | test "compile-time array initialization" { |
| 94 | try expect(fancy_array[4].x == 4); | |
| 95 | try expect(fancy_array[4].y == 8); | |
| 94 | try expectEqual(4, fancy_array[4].x); | |
| 95 | try expectEqual(8, fancy_array[4].y); | |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | // call a function to initialize an array |
| ... | ... | @@ -104,9 +104,9 @@ fn makePoint(x: i32) Point { |
| 104 | 104 | }; |
| 105 | 105 | } |
| 106 | 106 | test "array initialization with function calls" { |
| 107 | try expect(more_points[4].x == 3); | |
| 108 | try expect(more_points[4].y == 6); | |
| 109 | try expect(more_points.len == 10); | |
| 107 | try expectEqual(3, more_points[4].x); | |
| 108 | try expectEqual(6, more_points[4].y); | |
| 109 | try expectEqual(10, more_points.len); | |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | // test |
doc/langref/test_basic_slices.zig+11-11| ... | ... | @@ -1,4 +1,4 @@ |
| 1 | const expect = @import("std").testing.expect; | |
| 1 | const expectEqual = @import("std").testing.expectEqual; | |
| 2 | 2 | const expectEqualSlices = @import("std").testing.expectEqualSlices; |
| 3 | 3 | |
| 4 | 4 | test "basic slices" { |
| ... | ... | @@ -12,14 +12,14 @@ test "basic slices" { |
| 12 | 12 | |
| 13 | 13 | try expectEqualSlices(i32, slice, alt_slice); |
| 14 | 14 | |
| 15 | try expect(@TypeOf(slice) == []i32); | |
| 16 | try expect(&slice[0] == &array[0]); | |
| 17 | try expect(slice.len == array.len); | |
| 15 | try expectEqual([]i32, @TypeOf(slice)); | |
| 16 | try expectEqual(&array[0], &slice[0]); | |
| 17 | try expectEqual(array.len, slice.len); | |
| 18 | 18 | |
| 19 | 19 | // If you slice with comptime-known start and end positions, the result is |
| 20 | 20 | // a pointer to an array, rather than a slice. |
| 21 | 21 | const array_ptr = array[0..array.len]; |
| 22 | try expect(@TypeOf(array_ptr) == *[array.len]i32); | |
| 22 | try expectEqual(*[array.len]i32, @TypeOf(array_ptr)); | |
| 23 | 23 | |
| 24 | 24 | // You can perform a slice-by-length by slicing twice. This allows the compiler |
| 25 | 25 | // to perform some optimisations like recognising a comptime-known length when |
| ... | ... | @@ -28,13 +28,13 @@ test "basic slices" { |
| 28 | 28 | _ = &runtime_start; |
| 29 | 29 | const length = 2; |
| 30 | 30 | const array_ptr_len = array[runtime_start..][0..length]; |
| 31 | try expect(@TypeOf(array_ptr_len) == *[length]i32); | |
| 31 | try expectEqual(*[length]i32, @TypeOf(array_ptr_len)); | |
| 32 | 32 | |
| 33 | 33 | // Using the address-of operator on a slice gives a single-item pointer. |
| 34 | try expect(@TypeOf(&slice[0]) == *i32); | |
| 34 | try expectEqual(*i32, @TypeOf(&slice[0])); | |
| 35 | 35 | // Using the `ptr` field gives a many-item pointer. |
| 36 | try expect(@TypeOf(slice.ptr) == [*]i32); | |
| 37 | try expect(@intFromPtr(slice.ptr) == @intFromPtr(&slice[0])); | |
| 36 | try expectEqual([*]i32, @TypeOf(slice.ptr)); | |
| 37 | try expectEqual(@intFromPtr(slice.ptr), @intFromPtr(&slice[0])); | |
| 38 | 38 | |
| 39 | 39 | // Slices have array bounds checking. If you try to access something out |
| 40 | 40 | // of bounds, you'll get a safety check failure: |
| ... | ... | @@ -47,8 +47,8 @@ test "basic slices" { |
| 47 | 47 | const empty1 = &[0]u8{}; |
| 48 | 48 | // If the type is known you can use this short hand: |
| 49 | 49 | const empty2: []u8 = &.{}; |
| 50 | try expect(empty1.len == 0); | |
| 51 | try expect(empty2.len == 0); | |
| 50 | try expectEqual(0, empty1.len); | |
| 51 | try expectEqual(0, empty2.len); | |
| 52 | 52 | |
| 53 | 53 | // A zero-length initialization can always be used to create an empty slice, even if the slice is mutable. |
| 54 | 54 | // This is because the pointed-to data is zero bits long, so its immutability is irrelevant. |
doc/langref/test_bitOffsetOf_offsetOf.zig+7-7| ... | ... | @@ -1,5 +1,5 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | const BitField = packed struct { |
| 5 | 5 | a: u3, |
| ... | ... | @@ -9,13 +9,13 @@ const BitField = packed struct { |
| 9 | 9 | |
| 10 | 10 | test "offsets of non-byte-aligned fields" { |
| 11 | 11 | comptime { |
| 12 | try expect(@bitOffsetOf(BitField, "a") == 0); | |
| 13 | try expect(@bitOffsetOf(BitField, "b") == 3); | |
| 14 | try expect(@bitOffsetOf(BitField, "c") == 6); | |
| 12 | try expectEqual(0, @bitOffsetOf(BitField, "a")); | |
| 13 | try expectEqual(3, @bitOffsetOf(BitField, "b")); | |
| 14 | try expectEqual(6, @bitOffsetOf(BitField, "c")); | |
| 15 | 15 | |
| 16 | try expect(@offsetOf(BitField, "a") == 0); | |
| 17 | try expect(@offsetOf(BitField, "b") == 0); | |
| 18 | try expect(@offsetOf(BitField, "c") == 0); | |
| 16 | try expectEqual(0, @offsetOf(BitField, "a")); | |
| 17 | try expectEqual(0, @offsetOf(BitField, "b")); | |
| 18 | try expectEqual(0, @offsetOf(BitField, "c")); | |
| 19 | 19 | } |
| 20 | 20 | } |
| 21 | 21 |
doc/langref/test_call_builtin.zig+2-2| ... | ... | @@ -1,7 +1,7 @@ |
| 1 | const expect = @import("std").testing.expect; | |
| 1 | const expectEqual = @import("std").testing.expectEqual; | |
| 2 | 2 | |
| 3 | 3 | test "noinline function call" { |
| 4 | try expect(@call(.auto, add, .{ 3, 9 }) == 12); | |
| 4 | try expectEqual(12, @call(.auto, add, .{ 3, 9 })); | |
| 5 | 5 | } |
| 6 | 6 | |
| 7 | 7 | fn add(a: i32, b: i32) i32 { |
doc/langref/test_coerce_error_subset_to_superset.zig+1-1| ... | ... | @@ -12,7 +12,7 @@ const AllocationError = error{ |
| 12 | 12 | |
| 13 | 13 | test "coerce subset to superset" { |
| 14 | 14 | const err = foo(AllocationError.OutOfMemory); |
| 15 | try std.testing.expect(err == FileOpenError.OutOfMemory); | |
| 15 | try std.testing.expectEqual(FileOpenError.OutOfMemory, err); | |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | fn foo(err: AllocationError) FileOpenError { |
doc/langref/test_coerce_large_to_small.zig+2-2| ... | ... | @@ -1,10 +1,10 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | test "coercing large integer type to smaller one when value is comptime-known to fit" { |
| 5 | 5 | const x: u64 = 255; |
| 6 | 6 | const y: u8 = x; |
| 7 | try expect(y == 255); | |
| 7 | try expectEqual(255, y); | |
| 8 | 8 | } |
| 9 | 9 | |
| 10 | 10 | // test |
doc/langref/test_coerce_optional_wrapped_error_union.zig+3-3| ... | ... | @@ -1,12 +1,12 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | test "coerce to optionals wrapped in error union" { |
| 5 | 5 | const x: anyerror!?i32 = 1234; |
| 6 | 6 | const y: anyerror!?i32 = null; |
| 7 | 7 | |
| 8 | try expect((try x).? == 1234); | |
| 9 | try expect((try y) == null); | |
| 8 | try expectEqual(1234, (try x).?); | |
| 9 | try expectEqual(null, (try y)); | |
| 10 | 10 | } |
| 11 | 11 | |
| 12 | 12 | // test |
doc/langref/test_coerce_optionals.zig+3-3| ... | ... | @@ -1,12 +1,12 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | test "coerce to optionals" { |
| 5 | 5 | const x: ?i32 = 1234; |
| 6 | 6 | const y: ?i32 = null; |
| 7 | 7 | |
| 8 | try expect(x.? == 1234); | |
| 9 | try expect(y == null); | |
| 8 | try expectEqual(1234, x.?); | |
| 9 | try expectEqual(null, y); | |
| 10 | 10 | } |
| 11 | 11 | |
| 12 | 12 | // test |
doc/langref/test_coerce_slices_arrays_and_pointers.zig+15-13| ... | ... | @@ -1,5 +1,7 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | const expectEqualStrings = std.testing.expectEqualStrings; | |
| 4 | const expectEqualSlices = std.testing.expectEqualSlices; | |
| 3 | 5 | |
| 4 | 6 | // You can assign constant pointers to arrays to a slice with |
| 5 | 7 | // const modifier on the element type. Useful in particular for |
| ... | ... | @@ -7,48 +9,48 @@ const expect = std.testing.expect; |
| 7 | 9 | test "*const [N]T to []const T" { |
| 8 | 10 | const x1: []const u8 = "hello"; |
| 9 | 11 | const x2: []const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 }; |
| 10 | try expect(std.mem.eql(u8, x1, x2)); | |
| 12 | try expectEqualStrings(x1, x2); | |
| 11 | 13 | |
| 12 | 14 | const y: []const f32 = &[2]f32{ 1.2, 3.4 }; |
| 13 | try expect(y[0] == 1.2); | |
| 15 | try expectEqual(1.2, y[0]); | |
| 14 | 16 | } |
| 15 | 17 | |
| 16 | 18 | // Likewise, it works when the destination type is an error union. |
| 17 | 19 | test "*const [N]T to E![]const T" { |
| 18 | 20 | const x1: anyerror![]const u8 = "hello"; |
| 19 | 21 | const x2: anyerror![]const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 }; |
| 20 | try expect(std.mem.eql(u8, try x1, try x2)); | |
| 22 | try expectEqualStrings(try x1, try x2); | |
| 21 | 23 | |
| 22 | 24 | const y: anyerror![]const f32 = &[2]f32{ 1.2, 3.4 }; |
| 23 | try expect((try y)[0] == 1.2); | |
| 25 | try expectEqual(1.2, (try y)[0]); | |
| 24 | 26 | } |
| 25 | 27 | |
| 26 | 28 | // Likewise, it works when the destination type is an optional. |
| 27 | 29 | test "*const [N]T to ?[]const T" { |
| 28 | 30 | const x1: ?[]const u8 = "hello"; |
| 29 | 31 | const x2: ?[]const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 }; |
| 30 | try expect(std.mem.eql(u8, x1.?, x2.?)); | |
| 32 | try expectEqualStrings(x1.?, x2.?); | |
| 31 | 33 | |
| 32 | 34 | const y: ?[]const f32 = &[2]f32{ 1.2, 3.4 }; |
| 33 | try expect(y.?[0] == 1.2); | |
| 35 | try expectEqual(1.2, y.?[0]); | |
| 34 | 36 | } |
| 35 | 37 | |
| 36 | 38 | // In this cast, the array length becomes the slice length. |
| 37 | 39 | test "*[N]T to []T" { |
| 38 | 40 | var buf: [5]u8 = "hello".*; |
| 39 | 41 | const x: []u8 = &buf; |
| 40 | try expect(std.mem.eql(u8, x, "hello")); | |
| 42 | try expectEqualStrings("hello", x); | |
| 41 | 43 | |
| 42 | 44 | const buf2 = [2]f32{ 1.2, 3.4 }; |
| 43 | 45 | const x2: []const f32 = &buf2; |
| 44 | try expect(std.mem.eql(f32, x2, &[2]f32{ 1.2, 3.4 })); | |
| 46 | try expectEqualSlices(f32, &[2]f32{ 1.2, 3.4 }, x2); | |
| 45 | 47 | } |
| 46 | 48 | |
| 47 | 49 | // Single-item pointers to arrays can be coerced to many-item pointers. |
| 48 | 50 | test "*[N]T to [*]T" { |
| 49 | 51 | var buf: [5]u8 = "hello".*; |
| 50 | 52 | const x: [*]u8 = &buf; |
| 51 | try expect(x[4] == 'o'); | |
| 53 | try expectEqual('o', x[4]); | |
| 52 | 54 | // x[5] would be an uncaught out of bounds pointer dereference! |
| 53 | 55 | } |
| 54 | 56 | |
| ... | ... | @@ -56,7 +58,7 @@ test "*[N]T to [*]T" { |
| 56 | 58 | test "*[N]T to ?[*]T" { |
| 57 | 59 | var buf: [5]u8 = "hello".*; |
| 58 | 60 | const x: ?[*]u8 = &buf; |
| 59 | try expect(x.?[4] == 'o'); | |
| 61 | try expectEqual('o', x.?[4]); | |
| 60 | 62 | } |
| 61 | 63 | |
| 62 | 64 | // Single-item pointers can be cast to len-1 single-item arrays. |
| ... | ... | @@ -64,14 +66,14 @@ test "*T to *[1]T" { |
| 64 | 66 | var x: i32 = 1234; |
| 65 | 67 | const y: *[1]i32 = &x; |
| 66 | 68 | const z: [*]i32 = y; |
| 67 | try expect(z[0] == 1234); | |
| 69 | try expectEqual(1234, z[0]); | |
| 68 | 70 | } |
| 69 | 71 | |
| 70 | 72 | // Sentinel-terminated slices can be coerced into sentinel-terminated pointers |
| 71 | 73 | test "[:x]T to [*:x]T" { |
| 72 | 74 | const buf: [:0]const u8 = "hello"; |
| 73 | 75 | const buf2: [*:0]const u8 = buf; |
| 74 | try expect(buf2[4] == 'o'); | |
| 76 | try expectEqual('o', buf2[4]); | |
| 75 | 77 | } |
| 76 | 78 | |
| 77 | 79 | // test |
doc/langref/test_coerce_to_error_union.zig+2-2| ... | ... | @@ -1,11 +1,11 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | test "coercion to error unions" { |
| 5 | 5 | const x: anyerror!i32 = 1234; |
| 6 | 6 | const y: anyerror!i32 = error.Failure; |
| 7 | 7 | |
| 8 | try expect((try x) == 1234); | |
| 8 | try expectEqual(1234, (try x)); | |
| 9 | 9 | try std.testing.expectError(error.Failure, y); |
| 10 | 10 | } |
| 11 | 11 |
doc/langref/test_coerce_tuples_arrays.zig+1-1| ... | ... | @@ -1,5 +1,5 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | const Tuple = struct { u8, u8 }; |
| 5 | 5 | test "coercion from homogeneous tuple to array" { |
doc/langref/test_coerce_unions_enums.zig+6-6| ... | ... | @@ -1,5 +1,5 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | const E = enum { |
| 5 | 5 | one, |
| ... | ... | @@ -28,22 +28,22 @@ const U2 = union(enum) { |
| 28 | 28 | test "coercion between unions and enums" { |
| 29 | 29 | const u = U{ .two = 12.34 }; |
| 30 | 30 | const e: E = u; // coerce union to enum |
| 31 | try expect(e == E.two); | |
| 31 | try expectEqual(E.two, e); | |
| 32 | 32 | |
| 33 | 33 | const three = E.three; |
| 34 | 34 | const u_2: U = three; // coerce enum to union |
| 35 | try expect(u_2 == E.three); | |
| 35 | try expectEqual(E.three, u_2); | |
| 36 | 36 | |
| 37 | 37 | const u_3: U = .three; // coerce enum literal to union |
| 38 | try expect(u_3 == E.three); | |
| 38 | try expectEqual(E.three, u_3); | |
| 39 | 39 | |
| 40 | 40 | const u_4: U2 = .a; // coerce enum literal to union with inferred enum tag type. |
| 41 | try expect(u_4.tag() == 1); | |
| 41 | try expectEqual(1, u_4.tag()); | |
| 42 | 42 | |
| 43 | 43 | // The following example is invalid. |
| 44 | 44 | // error: coercion from enum '@EnumLiteral()' to union 'test_coerce_unions_enum.U2' must initialize 'f32' field 'b' |
| 45 | 45 | //var u_5: U2 = .b; |
| 46 | //try expect(u_5.tag() == 2); | |
| 46 | //try expectEqual(2, u_5.tag()); | |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | // test |
doc/langref/test_comptime_evaluation.zig+4-4| ... | ... | @@ -1,4 +1,4 @@ |
| 1 | const expect = @import("std").testing.expect; | |
| 1 | const expectEqual = @import("std").testing.expectEqual; | |
| 2 | 2 | |
| 3 | 3 | const CmdFn = struct { |
| 4 | 4 | name: []const u8, |
| ... | ... | @@ -32,9 +32,9 @@ fn performFn(comptime prefix_char: u8, start_value: i32) i32 { |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | test "perform fn" { |
| 35 | try expect(performFn('t', 1) == 6); | |
| 36 | try expect(performFn('o', 0) == 1); | |
| 37 | try expect(performFn('w', 99) == 99); | |
| 35 | try expectEqual(6, performFn('t', 1)); | |
| 36 | try expectEqual(1, performFn('o', 0)); | |
| 37 | try expectEqual(99, performFn('w', 99)); | |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | // test |
doc/langref/test_comptime_max_with_bool.zig+1-1| ... | ... | @@ -8,7 +8,7 @@ fn max(comptime T: type, a: T, b: T) T { |
| 8 | 8 | } |
| 9 | 9 | } |
| 10 | 10 | test "try to compare bools" { |
| 11 | try @import("std").testing.expect(max(bool, false, true) == true); | |
| 11 | try @import("std").testing.expectEqual(true, max(bool, false, true)); | |
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | // test |
doc/langref/test_comptime_pointer_conversion.zig+3-3| ... | ... | @@ -1,4 +1,4 @@ |
| 1 | const expect = @import("std").testing.expect; | |
| 1 | const expectEqual = @import("std").testing.expectEqual; | |
| 2 | 2 | |
| 3 | 3 | test "comptime @ptrFromInt" { |
| 4 | 4 | comptime { |
| ... | ... | @@ -6,8 +6,8 @@ test "comptime @ptrFromInt" { |
| 6 | 6 | // ptr is never dereferenced. |
| 7 | 7 | const ptr: *i32 = @ptrFromInt(0xdeadbee0); |
| 8 | 8 | const addr = @intFromPtr(ptr); |
| 9 | try expect(@TypeOf(addr) == usize); | |
| 10 | try expect(addr == 0xdeadbee0); | |
| 9 | try expectEqual(usize, @TypeOf(addr)); | |
| 10 | try expectEqual(0xdeadbee0, addr); | |
| 11 | 11 | } |
| 12 | 12 | } |
| 13 | 13 |
doc/langref/test_comptime_pointers.zig+2-2| ... | ... | @@ -1,4 +1,4 @@ |
| 1 | const expect = @import("std").testing.expect; | |
| 1 | const expectEqual = @import("std").testing.expectEqual; | |
| 2 | 2 | |
| 3 | 3 | test "comptime pointers" { |
| 4 | 4 | comptime { |
| ... | ... | @@ -6,7 +6,7 @@ test "comptime pointers" { |
| 6 | 6 | const ptr = &x; |
| 7 | 7 | ptr.* += 1; |
| 8 | 8 | x += 1; |
| 9 | try expect(ptr.* == 3); | |
| 9 | try expectEqual(3, ptr.*); | |
| 10 | 10 | } |
| 11 | 11 | } |
| 12 | 12 |
doc/langref/test_comptime_variables.zig+3-3| ... | ... | @@ -1,5 +1,5 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | test "comptime vars" { |
| 5 | 5 | var x: i32 = 1; |
| ... | ... | @@ -8,8 +8,8 @@ test "comptime vars" { |
| 8 | 8 | x += 1; |
| 9 | 9 | y += 1; |
| 10 | 10 | |
| 11 | try expect(x == 2); | |
| 12 | try expect(y == 2); | |
| 11 | try expectEqual(2, x); | |
| 12 | try expectEqual(2, y); | |
| 13 | 13 | |
| 14 | 14 | if (y != 2) { |
| 15 | 15 | // This compile error never triggers because y is a comptime variable, |
doc/langref/test_container-level_comptime_expressions.zig+1-1| ... | ... | @@ -31,7 +31,7 @@ fn sum(numbers: []const i32) i32 { |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | test "variable values" { |
| 34 | try @import("std").testing.expect(sum_of_first_25_primes == 1060); | |
| 34 | try @import("std").testing.expectEqual(1060, sum_of_first_25_primes); | |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | // test |
doc/langref/test_container_level_variables.zig+3-3| ... | ... | @@ -2,8 +2,8 @@ var y: i32 = add(10, x); |
| 2 | 2 | const x: i32 = add(12, 34); |
| 3 | 3 | |
| 4 | 4 | test "container level variables" { |
| 5 | try expect(x == 46); | |
| 6 | try expect(y == 56); | |
| 5 | try expectEqual(46, x); | |
| 6 | try expectEqual(56, y); | |
| 7 | 7 | } |
| 8 | 8 | |
| 9 | 9 | fn add(a: i32, b: i32) i32 { |
| ... | ... | @@ -11,6 +11,6 @@ fn add(a: i32, b: i32) i32 { |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | const std = @import("std"); |
| 14 | const expect = std.testing.expect; | |
| 14 | const expectEqual = std.testing.expectEqual; | |
| 15 | 15 | |
| 16 | 16 | // test |
doc/langref/test_defer.zig+3-3| ... | ... | @@ -1,5 +1,5 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | const print = std.debug.print; |
| 4 | 4 | |
| 5 | 5 | fn deferExample() !usize { |
| ... | ... | @@ -9,14 +9,14 @@ fn deferExample() !usize { |
| 9 | 9 | defer a = 2; |
| 10 | 10 | a = 1; |
| 11 | 11 | } |
| 12 | try expect(a == 2); | |
| 12 | try expectEqual(2, a); | |
| 13 | 13 | |
| 14 | 14 | a = 5; |
| 15 | 15 | return a; |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | test "defer basics" { |
| 19 | try expect((try deferExample()) == 5); | |
| 19 | try expectEqual(5, (try deferExample())); | |
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | // test |
doc/langref/test_empty_block.zig+4-4| ... | ... | @@ -1,12 +1,12 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | test { |
| 5 | 5 | const a = {}; |
| 6 | 6 | const b = void{}; |
| 7 | try expect(@TypeOf(a) == void); | |
| 8 | try expect(@TypeOf(b) == void); | |
| 9 | try expect(a == b); | |
| 7 | try expectEqual(void, @TypeOf(a)); | |
| 8 | try expectEqual(void, @TypeOf(b)); | |
| 9 | try expectEqual(a, b); | |
| 10 | 10 | } |
| 11 | 11 | |
| 12 | 12 | // test |
doc/langref/test_enum_literals.zig+2-1| ... | ... | @@ -1,5 +1,6 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | const expect = std.testing.expect; |
| 3 | const expectEqual = std.testing.expectEqual; | |
| 3 | 4 | |
| 4 | 5 | const Color = enum { |
| 5 | 6 | auto, |
| ... | ... | @@ -10,7 +11,7 @@ const Color = enum { |
| 10 | 11 | test "enum literals" { |
| 11 | 12 | const color1: Color = .auto; |
| 12 | 13 | const color2 = Color.auto; |
| 13 | try expect(color1 == color2); | |
| 14 | try expectEqual(color1, color2); | |
| 14 | 15 | } |
| 15 | 16 | |
| 16 | 17 | test "switch using enum literals" { |
doc/langref/test_enums.zig+18-16| ... | ... | @@ -1,4 +1,6 @@ |
| 1 | 1 | const expect = @import("std").testing.expect; |
| 2 | const expectEqual = @import("std").testing.expectEqual; | |
| 3 | const expectEqualStrings = @import("std").testing.expectEqualStrings; | |
| 2 | 4 | const mem = @import("std").mem; |
| 3 | 5 | |
| 4 | 6 | // Declare an enum. |
| ... | ... | @@ -20,9 +22,9 @@ const Value = enum(u2) { |
| 20 | 22 | // Now you can cast between u2 and Value. |
| 21 | 23 | // The ordinal value starts from 0, counting up by 1 from the previous member. |
| 22 | 24 | test "enum ordinal value" { |
| 23 | try expect(@intFromEnum(Value.zero) == 0); | |
| 24 | try expect(@intFromEnum(Value.one) == 1); | |
| 25 | try expect(@intFromEnum(Value.two) == 2); | |
| 25 | try expectEqual(0, @intFromEnum(Value.zero)); | |
| 26 | try expectEqual(1, @intFromEnum(Value.one)); | |
| 27 | try expectEqual(2, @intFromEnum(Value.two)); | |
| 26 | 28 | } |
| 27 | 29 | |
| 28 | 30 | // You can override the ordinal value for an enum. |
| ... | ... | @@ -32,9 +34,9 @@ const Value2 = enum(u32) { |
| 32 | 34 | million = 1000000, |
| 33 | 35 | }; |
| 34 | 36 | test "set enum ordinal value" { |
| 35 | try expect(@intFromEnum(Value2.hundred) == 100); | |
| 36 | try expect(@intFromEnum(Value2.thousand) == 1000); | |
| 37 | try expect(@intFromEnum(Value2.million) == 1000000); | |
| 37 | try expectEqual(100, @intFromEnum(Value2.hundred)); | |
| 38 | try expectEqual(1000, @intFromEnum(Value2.thousand)); | |
| 39 | try expectEqual(1000000, @intFromEnum(Value2.million)); | |
| 38 | 40 | } |
| 39 | 41 | |
| 40 | 42 | // You can also override only some values. |
| ... | ... | @@ -46,11 +48,11 @@ const Value3 = enum(u4) { |
| 46 | 48 | e, |
| 47 | 49 | }; |
| 48 | 50 | test "enum implicit ordinal values and overridden values" { |
| 49 | try expect(@intFromEnum(Value3.a) == 0); | |
| 50 | try expect(@intFromEnum(Value3.b) == 8); | |
| 51 | try expect(@intFromEnum(Value3.c) == 9); | |
| 52 | try expect(@intFromEnum(Value3.d) == 4); | |
| 53 | try expect(@intFromEnum(Value3.e) == 5); | |
| 51 | try expectEqual(0, @intFromEnum(Value3.a)); | |
| 52 | try expectEqual(8, @intFromEnum(Value3.b)); | |
| 53 | try expectEqual(9, @intFromEnum(Value3.c)); | |
| 54 | try expectEqual(4, @intFromEnum(Value3.d)); | |
| 55 | try expectEqual(5, @intFromEnum(Value3.e)); | |
| 54 | 56 | } |
| 55 | 57 | |
| 56 | 58 | // Enums can have methods, the same as structs and unions. |
| ... | ... | @@ -84,7 +86,7 @@ test "enum switch" { |
| 84 | 86 | Foo.number => "this is a number", |
| 85 | 87 | Foo.none => "this is a none", |
| 86 | 88 | }; |
| 87 | try expect(mem.eql(u8, what_is_it, "this is a number")); | |
| 89 | try expectEqualStrings(what_is_it, "this is a number"); | |
| 88 | 90 | } |
| 89 | 91 | |
| 90 | 92 | // @typeInfo can be used to access the integer tag type of an enum. |
| ... | ... | @@ -95,18 +97,18 @@ const Small = enum { |
| 95 | 97 | four, |
| 96 | 98 | }; |
| 97 | 99 | test "std.meta.Tag" { |
| 98 | try expect(@typeInfo(Small).@"enum".tag_type == u2); | |
| 100 | try expectEqual(u2, @typeInfo(Small).@"enum".tag_type); | |
| 99 | 101 | } |
| 100 | 102 | |
| 101 | 103 | // @typeInfo tells us the field count and the fields names: |
| 102 | 104 | test "@typeInfo" { |
| 103 | try expect(@typeInfo(Small).@"enum".fields.len == 4); | |
| 104 | try expect(mem.eql(u8, @typeInfo(Small).@"enum".fields[1].name, "two")); | |
| 105 | try expectEqual(4, @typeInfo(Small).@"enum".fields.len); | |
| 106 | try expectEqualStrings(@typeInfo(Small).@"enum".fields[1].name, "two"); | |
| 105 | 107 | } |
| 106 | 108 | |
| 107 | 109 | // @tagName gives a [:0]const u8 representation of an enum value: |
| 108 | 110 | test "@tagName" { |
| 109 | try expect(mem.eql(u8, @tagName(Small.three), "three")); | |
| 111 | try expectEqualStrings(@tagName(Small.three), "three"); | |
| 110 | 112 | } |
| 111 | 113 | |
| 112 | 114 | // test |
doc/langref/test_error_union.zig+3-3| ... | ... | @@ -1,4 +1,4 @@ |
| 1 | const expect = @import("std").testing.expect; | |
| 1 | const expectEqual = @import("std").testing.expectEqual; | |
| 2 | 2 | |
| 3 | 3 | test "error union" { |
| 4 | 4 | var foo: anyerror!i32 = undefined; |
| ... | ... | @@ -10,10 +10,10 @@ test "error union" { |
| 10 | 10 | foo = error.SomeError; |
| 11 | 11 | |
| 12 | 12 | // Use compile-time reflection to access the payload type of an error union: |
| 13 | try comptime expect(@typeInfo(@TypeOf(foo)).error_union.payload == i32); | |
| 13 | try comptime expectEqual(i32, @typeInfo(@TypeOf(foo)).error_union.payload); | |
| 14 | 14 | |
| 15 | 15 | // Use compile-time reflection to access the error set type of an error union: |
| 16 | try comptime expect(@typeInfo(@TypeOf(foo)).error_union.error_set == anyerror); | |
| 16 | try comptime expectEqual(anyerror, @typeInfo(@TypeOf(foo)).error_union.error_set); | |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | // test |
doc/langref/test_fibonacci_comptime_overflow.zig+2-2| ... | ... | @@ -1,4 +1,4 @@ |
| 1 | const expect = @import("std").testing.expect; | |
| 1 | const expectEqual = @import("std").testing.expectEqual; | |
| 2 | 2 | |
| 3 | 3 | fn fibonacci(index: u32) u32 { |
| 4 | 4 | //if (index < 2) return index; |
| ... | ... | @@ -6,7 +6,7 @@ fn fibonacci(index: u32) u32 { |
| 6 | 6 | } |
| 7 | 7 | |
| 8 | 8 | test "fibonacci" { |
| 9 | try comptime expect(fibonacci(7) == 13); | |
| 9 | try comptime expectEqual(13, fibonacci(7)); | |
| 10 | 10 | } |
| 11 | 11 | |
| 12 | 12 | // test_error=overflow of integer type |
doc/langref/test_fibonacci_recursion.zig+3-3| ... | ... | @@ -1,4 +1,4 @@ |
| 1 | const expect = @import("std").testing.expect; | |
| 1 | const expectEqual = @import("std").testing.expectEqual; | |
| 2 | 2 | |
| 3 | 3 | fn fibonacci(index: u32) u32 { |
| 4 | 4 | if (index < 2) return index; |
| ... | ... | @@ -7,10 +7,10 @@ fn fibonacci(index: u32) u32 { |
| 7 | 7 | |
| 8 | 8 | test "fibonacci" { |
| 9 | 9 | // test fibonacci at run-time |
| 10 | try expect(fibonacci(7) == 13); | |
| 10 | try expectEqual(13, fibonacci(7)); | |
| 11 | 11 | |
| 12 | 12 | // test fibonacci at compile-time |
| 13 | try comptime expect(fibonacci(7) == 13); | |
| 13 | try comptime expectEqual(13, fibonacci(7)); | |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | // test |
doc/langref/test_field_builtin.zig+5-7| ... | ... | @@ -1,4 +1,5 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 2 | 3 | |
| 3 | 4 | const Point = struct { |
| 4 | 5 | x: u32, |
| ... | ... | @@ -8,23 +9,20 @@ const Point = struct { |
| 8 | 9 | }; |
| 9 | 10 | |
| 10 | 11 | test "field access by string" { |
| 11 | const expect = std.testing.expect; | |
| 12 | 12 | var p = Point{ .x = 0, .y = 0 }; |
| 13 | 13 | |
| 14 | 14 | @field(p, "x") = 4; |
| 15 | 15 | @field(p, "y") = @field(p, "x") + 1; |
| 16 | 16 | |
| 17 | try expect(@field(p, "x") == 4); | |
| 18 | try expect(@field(p, "y") == 5); | |
| 17 | try expectEqual(4, @field(p, "x")); | |
| 18 | try expectEqual(5, @field(p, "y")); | |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | test "decl access by string" { |
| 22 | const expect = std.testing.expect; | |
| 23 | ||
| 24 | try expect(@field(Point, "z") == 1); | |
| 22 | try expectEqual(1, @field(Point, "z")); | |
| 25 | 23 | |
| 26 | 24 | @field(Point, "z") = 2; |
| 27 | try expect(@field(Point, "z") == 2); | |
| 25 | try expectEqual(2, @field(Point, "z")); | |
| 28 | 26 | } |
| 29 | 27 | |
| 30 | 28 | // test |
doc/langref/test_fn_reflection.zig+2-2| ... | ... | @@ -3,8 +3,8 @@ const math = std.math; |
| 3 | 3 | const testing = std.testing; |
| 4 | 4 | |
| 5 | 5 | test "fn reflection" { |
| 6 | try testing.expect(@typeInfo(@TypeOf(testing.expect)).@"fn".params[0].type.? == bool); | |
| 7 | try testing.expect(@typeInfo(@TypeOf(testing.tmpDir)).@"fn".return_type.? == testing.TmpDir); | |
| 6 | try testing.expectEqual(bool, @typeInfo(@TypeOf(testing.expect)).@"fn".params[0].type.?); | |
| 7 | try testing.expectEqual(testing.TmpDir, @typeInfo(@TypeOf(testing.tmpDir)).@"fn".return_type.?); | |
| 8 | 8 | |
| 9 | 9 | try testing.expect(@typeInfo(@TypeOf(math.Log2Int)).@"fn".is_generic); |
| 10 | 10 | } |
doc/langref/test_fn_type_inference.zig+5-5| ... | ... | @@ -1,15 +1,15 @@ |
| 1 | const expect = @import("std").testing.expect; | |
| 1 | const expectEqual = @import("std").testing.expectEqual; | |
| 2 | 2 | |
| 3 | 3 | fn addFortyTwo(x: anytype) @TypeOf(x) { |
| 4 | 4 | return x + 42; |
| 5 | 5 | } |
| 6 | 6 | |
| 7 | 7 | test "fn type inference" { |
| 8 | try expect(addFortyTwo(1) == 43); | |
| 9 | try expect(@TypeOf(addFortyTwo(1)) == comptime_int); | |
| 8 | try expectEqual(43, addFortyTwo(1)); | |
| 9 | try expectEqual(comptime_int, @TypeOf(addFortyTwo(1))); | |
| 10 | 10 | const y: i64 = 2; |
| 11 | try expect(addFortyTwo(y) == 44); | |
| 12 | try expect(@TypeOf(addFortyTwo(y)) == i64); | |
| 11 | try expectEqual(44, addFortyTwo(y)); | |
| 12 | try expectEqual(i64, @TypeOf(addFortyTwo(y))); | |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | 15 | // test |
doc/langref/test_for.zig+12-12| ... | ... | @@ -1,4 +1,4 @@ |
| 1 | const expect = @import("std").testing.expect; | |
| 1 | const expectEqual = @import("std").testing.expectEqual; | |
| 2 | 2 | |
| 3 | 3 | test "for basics" { |
| 4 | 4 | const items = [_]i32{ 4, 5, 3, 4, 0 }; |
| ... | ... | @@ -12,22 +12,22 @@ test "for basics" { |
| 12 | 12 | } |
| 13 | 13 | sum += value; |
| 14 | 14 | } |
| 15 | try expect(sum == 16); | |
| 15 | try expectEqual(16, sum); | |
| 16 | 16 | |
| 17 | 17 | // To iterate over a portion of a slice, reslice. |
| 18 | 18 | for (items[0..1]) |value| { |
| 19 | 19 | sum += value; |
| 20 | 20 | } |
| 21 | try expect(sum == 20); | |
| 21 | try expectEqual(20, sum); | |
| 22 | 22 | |
| 23 | 23 | // To access the index of iteration, specify a second condition as well |
| 24 | 24 | // as a second capture value. |
| 25 | 25 | var sum2: i32 = 0; |
| 26 | 26 | for (items, 0..) |_, i| { |
| 27 | try expect(@TypeOf(i) == usize); | |
| 27 | try expectEqual(usize, @TypeOf(i)); | |
| 28 | 28 | sum2 += @as(i32, @intCast(i)); |
| 29 | 29 | } |
| 30 | try expect(sum2 == 10); | |
| 30 | try expectEqual(10, sum2); | |
| 31 | 31 | |
| 32 | 32 | // To iterate over consecutive integers, use the range syntax. |
| 33 | 33 | // Unbounded range is always a compile error. |
| ... | ... | @@ -35,7 +35,7 @@ test "for basics" { |
| 35 | 35 | for (0..5) |i| { |
| 36 | 36 | sum3 += i; |
| 37 | 37 | } |
| 38 | try expect(sum3 == 10); | |
| 38 | try expectEqual(10, sum3); | |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | test "multi object for" { |
| ... | ... | @@ -50,7 +50,7 @@ test "multi object for" { |
| 50 | 50 | count += i + j; |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | try expect(count == 21); | |
| 53 | try expectEqual(21, count); | |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | test "for reference" { |
| ... | ... | @@ -62,9 +62,9 @@ test "for reference" { |
| 62 | 62 | value.* += 1; |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | try expect(items[0] == 4); | |
| 66 | try expect(items[1] == 5); | |
| 67 | try expect(items[2] == 3); | |
| 65 | try expectEqual(4, items[0]); | |
| 66 | try expectEqual(5, items[1]); | |
| 67 | try expectEqual(3, items[2]); | |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | test "for else" { |
| ... | ... | @@ -79,10 +79,10 @@ test "for else" { |
| 79 | 79 | sum += value.?; |
| 80 | 80 | } |
| 81 | 81 | } else blk: { |
| 82 | try expect(sum == 12); | |
| 82 | try expectEqual(12, sum); | |
| 83 | 83 | break :blk sum; |
| 84 | 84 | }; |
| 85 | try expect(result == 12); | |
| 85 | try expectEqual(12, result); | |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | // test |
doc/langref/test_for_nested_break.zig+3-3| ... | ... | @@ -1,5 +1,5 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | test "nested break" { |
| 5 | 5 | var count: usize = 0; |
| ... | ... | @@ -9,7 +9,7 @@ test "nested break" { |
| 9 | 9 | break :outer; |
| 10 | 10 | } |
| 11 | 11 | } |
| 12 | try expect(count == 1); | |
| 12 | try expectEqual(1, count); | |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | 15 | test "nested continue" { |
| ... | ... | @@ -21,7 +21,7 @@ test "nested continue" { |
| 21 | 21 | } |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | try expect(count == 8); | |
| 24 | try expectEqual(8, count); | |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | // test |
doc/langref/test_functions.zig+3-3| ... | ... | @@ -1,7 +1,7 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | const builtin = @import("builtin"); |
| 3 | 3 | const native_arch = builtin.cpu.arch; |
| 4 | const expect = std.testing.expect; | |
| 4 | const expectEqual = std.testing.expectEqual; | |
| 5 | 5 | |
| 6 | 6 | // Functions are declared like this |
| 7 | 7 | fn add(a: i8, b: i8) i8 { |
| ... | ... | @@ -57,8 +57,8 @@ fn doOp(fnCall: Call2Op, op1: i8, op2: i8) i8 { |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | test "function" { |
| 60 | try expect(doOp(add, 5, 6) == 11); | |
| 61 | try expect(doOp(sub2, 5, 6) == -1); | |
| 60 | try expectEqual(11, doOp(add, 5, 6)); | |
| 61 | try expectEqual(-1, doOp(sub2, 5, 6)); | |
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | // test |
doc/langref/test_global_assembly.zig+2-2| ... | ... | @@ -1,5 +1,5 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | comptime { |
| 5 | 5 | asm ( |
| ... | ... | @@ -14,7 +14,7 @@ comptime { |
| 14 | 14 | extern fn my_func(a: i32, b: i32) i32; |
| 15 | 15 | |
| 16 | 16 | test "global assembly" { |
| 17 | try expect(my_func(12, 34) == 46); | |
| 17 | try expectEqual(46, my_func(12, 34)); | |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | // test |
doc/langref/test_if.zig+7-6| ... | ... | @@ -4,13 +4,14 @@ |
| 4 | 4 | // * anyerror!T |
| 5 | 5 | |
| 6 | 6 | const expect = @import("std").testing.expect; |
| 7 | const expectEqual = @import("std").testing.expectEqual; | |
| 7 | 8 | |
| 8 | 9 | test "if expression" { |
| 9 | 10 | // If expressions are used instead of a ternary expression. |
| 10 | 11 | const a: u32 = 5; |
| 11 | 12 | const b: u32 = 4; |
| 12 | 13 | const result = if (a != b) 47 else 3089; |
| 13 | try expect(result == 47); | |
| 14 | try expectEqual(result, 47); | |
| 14 | 15 | } |
| 15 | 16 | |
| 16 | 17 | test "if boolean" { |
| ... | ... | @@ -32,7 +33,7 @@ test "if error union" { |
| 32 | 33 | |
| 33 | 34 | const a: anyerror!u32 = 0; |
| 34 | 35 | if (a) |value| { |
| 35 | try expect(value == 0); | |
| 36 | try expectEqual(value, 0); | |
| 36 | 37 | } else |err| { |
| 37 | 38 | _ = err; |
| 38 | 39 | unreachable; |
| ... | ... | @@ -43,17 +44,17 @@ test "if error union" { |
| 43 | 44 | _ = value; |
| 44 | 45 | unreachable; |
| 45 | 46 | } else |err| { |
| 46 | try expect(err == error.BadValue); | |
| 47 | try expectEqual(err, error.BadValue); | |
| 47 | 48 | } |
| 48 | 49 | |
| 49 | 50 | // The else and |err| capture is strictly required. |
| 50 | 51 | if (a) |value| { |
| 51 | try expect(value == 0); | |
| 52 | try expectEqual(value, 0); | |
| 52 | 53 | } else |_| {} |
| 53 | 54 | |
| 54 | 55 | // To check only the error value, use an empty block expression. |
| 55 | 56 | if (b) |_| {} else |err| { |
| 56 | try expect(err == error.BadValue); | |
| 57 | try expectEqual(err, error.BadValue); | |
| 57 | 58 | } |
| 58 | 59 | |
| 59 | 60 | // Access the value by reference using a pointer capture. |
| ... | ... | @@ -65,7 +66,7 @@ test "if error union" { |
| 65 | 66 | } |
| 66 | 67 | |
| 67 | 68 | if (c) |value| { |
| 68 | try expect(value == 9); | |
| 69 | try expectEqual(value, 9); | |
| 69 | 70 | } else |_| { |
| 70 | 71 | unreachable; |
| 71 | 72 | } |
doc/langref/test_if_optionals.zig+8-7| ... | ... | @@ -1,11 +1,12 @@ |
| 1 | 1 | const expect = @import("std").testing.expect; |
| 2 | const expectEqual = @import("std").testing.expectEqual; | |
| 2 | 3 | |
| 3 | 4 | test "if optional" { |
| 4 | 5 | // If expressions test for null. |
| 5 | 6 | |
| 6 | 7 | const a: ?u32 = 0; |
| 7 | 8 | if (a) |value| { |
| 8 | try expect(value == 0); | |
| 9 | try expectEqual(0, value); | |
| 9 | 10 | } else { |
| 10 | 11 | unreachable; |
| 11 | 12 | } |
| ... | ... | @@ -19,7 +20,7 @@ test "if optional" { |
| 19 | 20 | |
| 20 | 21 | // The else is not required. |
| 21 | 22 | if (a) |value| { |
| 22 | try expect(value == 0); | |
| 23 | try expectEqual(0, value); | |
| 23 | 24 | } |
| 24 | 25 | |
| 25 | 26 | // To test against null only, use the binary equality operator. |
| ... | ... | @@ -34,7 +35,7 @@ test "if optional" { |
| 34 | 35 | } |
| 35 | 36 | |
| 36 | 37 | if (c) |value| { |
| 37 | try expect(value == 2); | |
| 38 | try expectEqual(2, value); | |
| 38 | 39 | } else { |
| 39 | 40 | unreachable; |
| 40 | 41 | } |
| ... | ... | @@ -46,7 +47,7 @@ test "if error union with optional" { |
| 46 | 47 | |
| 47 | 48 | const a: anyerror!?u32 = 0; |
| 48 | 49 | if (a) |optional_value| { |
| 49 | try expect(optional_value.? == 0); | |
| 50 | try expectEqual(0, optional_value.?); | |
| 50 | 51 | } else |err| { |
| 51 | 52 | _ = err; |
| 52 | 53 | unreachable; |
| ... | ... | @@ -54,7 +55,7 @@ test "if error union with optional" { |
| 54 | 55 | |
| 55 | 56 | const b: anyerror!?u32 = null; |
| 56 | 57 | if (b) |optional_value| { |
| 57 | try expect(optional_value == null); | |
| 58 | try expectEqual(null, optional_value); | |
| 58 | 59 | } else |_| { |
| 59 | 60 | unreachable; |
| 60 | 61 | } |
| ... | ... | @@ -64,7 +65,7 @@ test "if error union with optional" { |
| 64 | 65 | _ = optional_value; |
| 65 | 66 | unreachable; |
| 66 | 67 | } else |err| { |
| 67 | try expect(err == error.BadValue); | |
| 68 | try expectEqual(error.BadValue, err); | |
| 68 | 69 | } |
| 69 | 70 | |
| 70 | 71 | // Access the value by reference by using a pointer capture each time. |
| ... | ... | @@ -78,7 +79,7 @@ test "if error union with optional" { |
| 78 | 79 | } |
| 79 | 80 | |
| 80 | 81 | if (d) |optional_value| { |
| 81 | try expect(optional_value.? == 9); | |
| 82 | try expectEqual(9, optional_value.?); | |
| 82 | 83 | } else |_| { |
| 83 | 84 | unreachable; |
| 84 | 85 | } |
doc/langref/test_incorrect_pointer_alignment.zig+1-1| ... | ... | @@ -3,7 +3,7 @@ const std = @import("std"); |
| 3 | 3 | test "pointer alignment safety" { |
| 4 | 4 | var array align(4) = [_]u32{ 0x11111111, 0x11111111 }; |
| 5 | 5 | const bytes = std.mem.sliceAsBytes(array[0..]); |
| 6 | try std.testing.expect(foo(bytes) == 0x11111111); | |
| 6 | try std.testing.expectEqual(0x11111111, foo(bytes)); | |
| 7 | 7 | } |
| 8 | 8 | fn foo(bytes: []u8) u32 { |
| 9 | 9 | const slice4 = bytes[1..5]; |
doc/langref/test_inline_else.zig+3-3| ... | ... | @@ -1,5 +1,5 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | const SliceTypeA = extern struct { |
| 5 | 5 | len: usize, |
| ... | ... | @@ -42,8 +42,8 @@ fn withSwitch(any: AnySlice) usize { |
| 42 | 42 | |
| 43 | 43 | test "inline for and inline else similarity" { |
| 44 | 44 | const any = AnySlice{ .c = "hello" }; |
| 45 | try expect(withFor(any) == 5); | |
| 46 | try expect(withSwitch(any) == 5); | |
| 45 | try expectEqual(5, withFor(any)); | |
| 46 | try expectEqual(5, withSwitch(any)); | |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | // test |
doc/langref/test_inline_for.zig+2-2| ... | ... | @@ -1,4 +1,4 @@ |
| 1 | const expect = @import("std").testing.expect; | |
| 1 | const expectEqual = @import("std").testing.expectEqual; | |
| 2 | 2 | |
| 3 | 3 | test "inline for loop" { |
| 4 | 4 | const nums = [_]i32{ 2, 4, 6 }; |
| ... | ... | @@ -12,7 +12,7 @@ test "inline for loop" { |
| 12 | 12 | }; |
| 13 | 13 | sum += typeNameLength(T); |
| 14 | 14 | } |
| 15 | try expect(sum == 9); | |
| 15 | try expectEqual(9, sum); | |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | fn typeNameLength(comptime T: type) usize { |
doc/langref/test_inline_switch_union_tag.zig+2-2| ... | ... | @@ -1,5 +1,5 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | const U = union(enum) { |
| 5 | 5 | a: u32, |
| ... | ... | @@ -21,7 +21,7 @@ fn getNum(u: U) u32 { |
| 21 | 21 | |
| 22 | 22 | test "test" { |
| 23 | 23 | const u = U{ .b = 42 }; |
| 24 | try expect(getNum(u) == 42); | |
| 24 | try expectEqual(42, getNum(u)); | |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | // test |
doc/langref/test_inline_while.zig+2-2| ... | ... | @@ -1,4 +1,4 @@ |
| 1 | const expect = @import("std").testing.expect; | |
| 1 | const expectEqual = @import("std").testing.expectEqual; | |
| 2 | 2 | |
| 3 | 3 | test "inline while loop" { |
| 4 | 4 | comptime var i = 0; |
| ... | ... | @@ -12,7 +12,7 @@ test "inline while loop" { |
| 12 | 12 | }; |
| 13 | 13 | sum += typeNameLength(T); |
| 14 | 14 | } |
| 15 | try expect(sum == 9); | |
| 15 | try expectEqual(9, sum); | |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | fn typeNameLength(comptime T: type) usize { |
doc/langref/test_integer_pointer_conversion.zig+3-3| ... | ... | @@ -1,10 +1,10 @@ |
| 1 | const expect = @import("std").testing.expect; | |
| 1 | const expectEqual = @import("std").testing.expectEqual; | |
| 2 | 2 | |
| 3 | 3 | test "@intFromPtr and @ptrFromInt" { |
| 4 | 4 | const ptr: *i32 = @ptrFromInt(0xdeadbee0); |
| 5 | 5 | const addr = @intFromPtr(ptr); |
| 6 | try expect(@TypeOf(addr) == usize); | |
| 7 | try expect(addr == 0xdeadbee0); | |
| 6 | try expectEqual(usize, @TypeOf(addr)); | |
| 7 | try expectEqual(0xdeadbee0, addr); | |
| 8 | 8 | } |
| 9 | 9 | |
| 10 | 10 | // test |
doc/langref/test_integer_widening.zig+4-4| ... | ... | @@ -1,6 +1,6 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | const builtin = @import("builtin"); |
| 3 | const expect = std.testing.expect; | |
| 3 | const expectEqual = std.testing.expectEqual; | |
| 4 | 4 | const mem = std.mem; |
| 5 | 5 | |
| 6 | 6 | test "integer widening" { |
| ... | ... | @@ -10,13 +10,13 @@ test "integer widening" { |
| 10 | 10 | const d: u64 = c; |
| 11 | 11 | const e: u64 = d; |
| 12 | 12 | const f: u128 = e; |
| 13 | try expect(f == a); | |
| 13 | try expectEqual(f, a); | |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | test "implicit unsigned integer to signed integer" { |
| 17 | 17 | const a: u8 = 250; |
| 18 | 18 | const b: i16 = a; |
| 19 | try expect(b == 250); | |
| 19 | try expectEqual(250, b); | |
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | test "float widening" { |
| ... | ... | @@ -24,7 +24,7 @@ test "float widening" { |
| 24 | 24 | const b: f32 = a; |
| 25 | 25 | const c: f64 = b; |
| 26 | 26 | const d: f128 = c; |
| 27 | try expect(d == a); | |
| 27 | try expectEqual(d, a); | |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | // test |
doc/langref/test_labeled_break.zig+3-3| ... | ... | @@ -1,5 +1,5 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | test "labeled break from labeled block expression" { |
| 5 | 5 | var y: i32 = 123; |
| ... | ... | @@ -8,8 +8,8 @@ test "labeled break from labeled block expression" { |
| 8 | 8 | y += 1; |
| 9 | 9 | break :blk y; |
| 10 | 10 | }; |
| 11 | try expect(x == 124); | |
| 12 | try expect(y == 124); | |
| 11 | try expectEqual(124, x); | |
| 12 | try expectEqual(124, y); | |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | 15 | // test |
doc/langref/test_misaligned_pointer.zig+2-2| ... | ... | @@ -1,5 +1,5 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | const BitField = packed struct { |
| 5 | 5 | a: u3, |
| ... | ... | @@ -14,7 +14,7 @@ var bit_field = BitField{ |
| 14 | 14 | }; |
| 15 | 15 | |
| 16 | 16 | test "pointer to non-byte-aligned field" { |
| 17 | try expect(bar(&bit_field.b) == 2); | |
| 17 | try expectEqual(2, bar(&bit_field.b)); | |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | fn bar(x: *const u3) u3 { |
doc/langref/test_multidimensional_arrays.zig+3-4| ... | ... | @@ -1,5 +1,4 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 3 | 2 | const expectEqual = std.testing.expectEqual; |
| 4 | 3 | |
| 5 | 4 | const mat4x5 = [4][5]f32{ |
| ... | ... | @@ -13,20 +12,20 @@ test "multidimensional arrays" { |
| 13 | 12 | try expectEqual(mat4x5[1], [_]f32{ 0.0, 1.0, 0.0, 1.0, 0.0 }); |
| 14 | 13 | |
| 15 | 14 | // Access the 2D array by indexing the outer array, and then the inner array. |
| 16 | try expect(mat4x5[3][4] == 9.9); | |
| 15 | try expectEqual(9.9, mat4x5[3][4]); | |
| 17 | 16 | |
| 18 | 17 | // Here we iterate with for loops. |
| 19 | 18 | for (mat4x5, 0..) |row, row_index| { |
| 20 | 19 | for (row, 0..) |cell, column_index| { |
| 21 | 20 | if (row_index == column_index) { |
| 22 | try expect(cell == 1.0); | |
| 21 | try expectEqual(1.0, cell); | |
| 23 | 22 | } |
| 24 | 23 | } |
| 25 | 24 | } |
| 26 | 25 | |
| 27 | 26 | // Initialize a multidimensional array to zeros. |
| 28 | 27 | const all_zero: [4][5]f32 = .{.{0} ** 5} ** 4; |
| 29 | try expect(all_zero[0][0] == 0); | |
| 28 | try expectEqual(0, all_zero[0][0]); | |
| 30 | 29 | } |
| 31 | 30 | |
| 32 | 31 | // test |
doc/langref/test_namespaced_container_level_variable.zig+3-3| ... | ... | @@ -1,9 +1,9 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | test "namespaced container level variable" { |
| 5 | try expect(foo() == 1235); | |
| 6 | try expect(foo() == 1236); | |
| 5 | try expectEqual(1235, foo()); | |
| 6 | try expectEqual(1236, foo()); | |
| 7 | 7 | } |
| 8 | 8 | |
| 9 | 9 | const S = struct { |
doc/langref/test_noreturn_from_exit.zig+2-2| ... | ... | @@ -1,14 +1,14 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | const builtin = @import("builtin"); |
| 3 | 3 | const native_arch = builtin.cpu.arch; |
| 4 | const expect = std.testing.expect; | |
| 4 | const expectEqual = std.testing.expectEqual; | |
| 5 | 5 | |
| 6 | 6 | const WINAPI: std.builtin.CallingConvention = if (native_arch == .x86) .{ .x86_stdcall = .{} } else .c; |
| 7 | 7 | extern "kernel32" fn ExitProcess(exit_code: c_uint) callconv(WINAPI) noreturn; |
| 8 | 8 | |
| 9 | 9 | test "foo" { |
| 10 | 10 | const value = bar() catch ExitProcess(1); |
| 11 | try expect(value == 1234); | |
| 11 | try expectEqual(1234, value); | |
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | fn bar() anyerror!u32 { |
doc/langref/test_null_terminated_array.zig+7-7| ... | ... | @@ -1,21 +1,21 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | test "0-terminated sentinel array" { |
| 5 | 5 | const array = [_:0]u8{ 1, 2, 3, 4 }; |
| 6 | 6 | |
| 7 | try expect(@TypeOf(array) == [4:0]u8); | |
| 8 | try expect(array.len == 4); | |
| 9 | try expect(array[4] == 0); | |
| 7 | try expectEqual([4:0]u8, @TypeOf(array)); | |
| 8 | try expectEqual(4, array.len); | |
| 9 | try expectEqual(0, array[4]); | |
| 10 | 10 | } |
| 11 | 11 | |
| 12 | 12 | test "extra 0s in 0-terminated sentinel array" { |
| 13 | 13 | // The sentinel value may appear earlier, but does not influence the compile-time 'len'. |
| 14 | 14 | const array = [_:0]u8{ 1, 0, 0, 4 }; |
| 15 | 15 | |
| 16 | try expect(@TypeOf(array) == [4:0]u8); | |
| 17 | try expect(array.len == 4); | |
| 18 | try expect(array[4] == 0); | |
| 16 | try expectEqual([4:0]u8, @TypeOf(array)); | |
| 17 | try expectEqual(4, array.len); | |
| 18 | try expectEqual(0, array[4]); | |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | // test |
doc/langref/test_null_terminated_slice.zig+3-3| ... | ... | @@ -1,11 +1,11 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | test "0-terminated slice" { |
| 5 | 5 | const slice: [:0]const u8 = "hello"; |
| 6 | 6 | |
| 7 | try expect(slice.len == 5); | |
| 8 | try expect(slice[5] == 0); | |
| 7 | try expectEqual(5, slice.len); | |
| 8 | try expectEqual(0, slice[5]); | |
| 9 | 9 | } |
| 10 | 10 | |
| 11 | 11 | // test |
doc/langref/test_null_terminated_slicing.zig+3-3| ... | ... | @@ -1,5 +1,5 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | test "0-terminated slicing" { |
| 5 | 5 | var array = [_]u8{ 3, 2, 1, 0, 3, 2, 1, 0 }; |
| ... | ... | @@ -7,8 +7,8 @@ test "0-terminated slicing" { |
| 7 | 7 | _ = &runtime_length; |
| 8 | 8 | const slice = array[0..runtime_length :0]; |
| 9 | 9 | |
| 10 | try expect(@TypeOf(slice) == [:0]u8); | |
| 11 | try expect(slice.len == 3); | |
| 10 | try expectEqual([:0]u8, @TypeOf(slice)); | |
| 11 | try expectEqual(3, slice.len); | |
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | // test |
doc/langref/test_optional_pointer.zig+3-3| ... | ... | @@ -1,4 +1,4 @@ |
| 1 | const expect = @import("std").testing.expect; | |
| 1 | const expectEqual = @import("std").testing.expectEqual; | |
| 2 | 2 | |
| 3 | 3 | test "optional pointers" { |
| 4 | 4 | // Pointers cannot be null. If you want a null pointer, use the optional |
| ... | ... | @@ -8,11 +8,11 @@ test "optional pointers" { |
| 8 | 8 | var x: i32 = 1; |
| 9 | 9 | ptr = &x; |
| 10 | 10 | |
| 11 | try expect(ptr.?.* == 1); | |
| 11 | try expectEqual(1, ptr.?.*); | |
| 12 | 12 | |
| 13 | 13 | // Optional pointers are the same size as normal pointers, because pointer |
| 14 | 14 | // value 0 is used as the null value. |
| 15 | try expect(@sizeOf(?*i32) == @sizeOf(*i32)); | |
| 15 | try expectEqual(@sizeOf(?*i32), @sizeOf(*i32)); | |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | // test |
doc/langref/test_optional_type.zig+2-2| ... | ... | @@ -1,4 +1,4 @@ |
| 1 | const expect = @import("std").testing.expect; | |
| 1 | const expectEqual = @import("std").testing.expectEqual; | |
| 2 | 2 | |
| 3 | 3 | test "optional type" { |
| 4 | 4 | // Declare an optional and coerce from null: |
| ... | ... | @@ -8,7 +8,7 @@ test "optional type" { |
| 8 | 8 | foo = 1234; |
| 9 | 9 | |
| 10 | 10 | // Use compile-time reflection to access the child type of the optional: |
| 11 | try comptime expect(@typeInfo(@TypeOf(foo)).optional.child == i32); | |
| 11 | try comptime expectEqual(i32, @typeInfo(@TypeOf(foo)).optional.child); | |
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | // test |
doc/langref/test_overaligned_packed_struct.zig+2-2| ... | ... | @@ -1,5 +1,5 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | const S = packed struct { |
| 5 | 5 | a: u32, |
| ... | ... | @@ -9,7 +9,7 @@ test "overaligned pointer to packed struct" { |
| 9 | 9 | var foo: S align(4) = .{ .a = 1, .b = 2 }; |
| 10 | 10 | const ptr: *align(4) S = &foo; |
| 11 | 11 | const ptr_to_b = &ptr.b; |
| 12 | try expect(ptr_to_b.* == 2); | |
| 12 | try expectEqual(2, ptr_to_b.*); | |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | 15 | // test |
doc/langref/test_packed_struct_equality.zig+2-2| ... | ... | @@ -1,5 +1,5 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | test "packed struct equality" { |
| 5 | 5 | const S = packed struct { |
| ... | ... | @@ -8,7 +8,7 @@ test "packed struct equality" { |
| 8 | 8 | }; |
| 9 | 9 | const x: S = .{ .a = 1, .b = 2 }; |
| 10 | 10 | const y: S = .{ .b = 2, .a = 1 }; |
| 11 | try expect(x == y); | |
| 11 | try expectEqual(x, y); | |
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | // test |
doc/langref/test_packed_struct_field_address.zig+3-3| ... | ... | @@ -1,5 +1,5 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | const BitField = packed struct { |
| 5 | 5 | a: u3, |
| ... | ... | @@ -14,8 +14,8 @@ var bit_field = BitField{ |
| 14 | 14 | }; |
| 15 | 15 | |
| 16 | 16 | test "pointers of sub-byte-aligned fields share addresses" { |
| 17 | try expect(@intFromPtr(&bit_field.a) == @intFromPtr(&bit_field.b)); | |
| 18 | try expect(@intFromPtr(&bit_field.a) == @intFromPtr(&bit_field.c)); | |
| 17 | try expectEqual(@intFromPtr(&bit_field.a), @intFromPtr(&bit_field.b)); | |
| 18 | try expectEqual(@intFromPtr(&bit_field.a), @intFromPtr(&bit_field.c)); | |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | // test |
doc/langref/test_packed_structs.zig+10-10| ... | ... | @@ -1,6 +1,6 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | const native_endian = @import("builtin").target.cpu.arch.endian(); |
| 3 | const expect = std.testing.expect; | |
| 3 | const expectEqual = std.testing.expectEqual; | |
| 4 | 4 | |
| 5 | 5 | const Full = packed struct { |
| 6 | 6 | number: u16, |
| ... | ... | @@ -17,23 +17,23 @@ test "@bitCast between packed structs" { |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | fn doTheTest() !void { |
| 20 | try expect(@sizeOf(Full) == 2); | |
| 21 | try expect(@sizeOf(Divided) == 2); | |
| 20 | try expectEqual(2, @sizeOf(Full)); | |
| 21 | try expectEqual(2, @sizeOf(Divided)); | |
| 22 | 22 | const full = Full{ .number = 0x1234 }; |
| 23 | 23 | const divided: Divided = @bitCast(full); |
| 24 | try expect(divided.half1 == 0x34); | |
| 25 | try expect(divided.quarter3 == 0x2); | |
| 26 | try expect(divided.quarter4 == 0x1); | |
| 24 | try expectEqual(0x34, divided.half1); | |
| 25 | try expectEqual(0x2, divided.quarter3); | |
| 26 | try expectEqual(0x1, divided.quarter4); | |
| 27 | 27 | |
| 28 | 28 | const ordered: [2]u8 = @bitCast(full); |
| 29 | 29 | switch (native_endian) { |
| 30 | 30 | .big => { |
| 31 | try expect(ordered[0] == 0x12); | |
| 32 | try expect(ordered[1] == 0x34); | |
| 31 | try expectEqual(0x12, ordered[0]); | |
| 32 | try expectEqual(0x34, ordered[1]); | |
| 33 | 33 | }, |
| 34 | 34 | .little => { |
| 35 | try expect(ordered[0] == 0x34); | |
| 36 | try expect(ordered[1] == 0x12); | |
| 35 | try expectEqual(0x34, ordered[0]); | |
| 36 | try expectEqual(0x12, ordered[1]); | |
| 37 | 37 | }, |
| 38 | 38 | } |
| 39 | 39 | } |
doc/langref/test_pass_by_reference_or_value.zig+2-2| ... | ... | @@ -11,10 +11,10 @@ fn foo(point: Point) i32 { |
| 11 | 11 | return point.x + point.y; |
| 12 | 12 | } |
| 13 | 13 | |
| 14 | const expect = @import("std").testing.expect; | |
| 14 | const expectEqual = @import("std").testing.expectEqual; | |
| 15 | 15 | |
| 16 | 16 | test "pass struct to function" { |
| 17 | try expect(foo(Point{ .x = 1, .y = 2 }) == 3); | |
| 17 | try expectEqual(3, foo(Point{ .x = 1, .y = 2 })); | |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | // test |
doc/langref/test_peer_type_resolution.zig+26-26| ... | ... | @@ -1,20 +1,20 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 3 | const mem = std.mem; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | const expectEqualStrings = std.testing.expectEqualStrings; | |
| 4 | 4 | |
| 5 | 5 | test "peer resolve int widening" { |
| 6 | 6 | const a: i8 = 12; |
| 7 | 7 | const b: i16 = 34; |
| 8 | 8 | const c = a + b; |
| 9 | try expect(c == 46); | |
| 10 | try expect(@TypeOf(c) == i16); | |
| 9 | try expectEqual(46, c); | |
| 10 | try expectEqual(i16, @TypeOf(c)); | |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | test "peer resolve arrays of different size to const slice" { |
| 14 | try expect(mem.eql(u8, boolToStr(true), "true")); | |
| 15 | try expect(mem.eql(u8, boolToStr(false), "false")); | |
| 16 | try comptime expect(mem.eql(u8, boolToStr(true), "true")); | |
| 17 | try comptime expect(mem.eql(u8, boolToStr(false), "false")); | |
| 14 | try expectEqualStrings("true", boolToStr(true)); | |
| 15 | try expectEqualStrings("false", boolToStr(false)); | |
| 16 | try comptime expectEqualStrings("true", boolToStr(true)); | |
| 17 | try comptime expectEqualStrings("false", boolToStr(false)); | |
| 18 | 18 | } |
| 19 | 19 | fn boolToStr(b: bool) []const u8 { |
| 20 | 20 | return if (b) "true" else "false"; |
| ... | ... | @@ -27,16 +27,16 @@ test "peer resolve array and const slice" { |
| 27 | 27 | fn testPeerResolveArrayConstSlice(b: bool) !void { |
| 28 | 28 | const value1 = if (b) "aoeu" else @as([]const u8, "zz"); |
| 29 | 29 | const value2 = if (b) @as([]const u8, "zz") else "aoeu"; |
| 30 | try expect(mem.eql(u8, value1, "aoeu")); | |
| 31 | try expect(mem.eql(u8, value2, "zz")); | |
| 30 | try expectEqualStrings("aoeu", value1); | |
| 31 | try expectEqualStrings("zz", value2); | |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | test "peer type resolution: ?T and T" { |
| 35 | try expect(peerTypeTAndOptionalT(true, false).? == 0); | |
| 36 | try expect(peerTypeTAndOptionalT(false, false).? == 3); | |
| 35 | try expectEqual(0, peerTypeTAndOptionalT(true, false).?); | |
| 36 | try expectEqual(3, peerTypeTAndOptionalT(false, false).?); | |
| 37 | 37 | comptime { |
| 38 | try expect(peerTypeTAndOptionalT(true, false).? == 0); | |
| 39 | try expect(peerTypeTAndOptionalT(false, false).? == 3); | |
| 38 | try expectEqual(0, peerTypeTAndOptionalT(true, false).?); | |
| 39 | try expectEqual(3, peerTypeTAndOptionalT(false, false).?); | |
| 40 | 40 | } |
| 41 | 41 | } |
| 42 | 42 | fn peerTypeTAndOptionalT(c: bool, b: bool) ?usize { |
| ... | ... | @@ -48,11 +48,11 @@ fn peerTypeTAndOptionalT(c: bool, b: bool) ?usize { |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | test "peer type resolution: *[0]u8 and []const u8" { |
| 51 | try expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0); | |
| 52 | try expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1); | |
| 51 | try expectEqual(0, peerTypeEmptyArrayAndSlice(true, "hi").len); | |
| 52 | try expectEqual(1, peerTypeEmptyArrayAndSlice(false, "hi").len); | |
| 53 | 53 | comptime { |
| 54 | try expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0); | |
| 55 | try expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1); | |
| 54 | try expectEqual(0, peerTypeEmptyArrayAndSlice(true, "hi").len); | |
| 55 | try expectEqual(1, peerTypeEmptyArrayAndSlice(false, "hi").len); | |
| 56 | 56 | } |
| 57 | 57 | } |
| 58 | 58 | fn peerTypeEmptyArrayAndSlice(a: bool, slice: []const u8) []const u8 { |
| ... | ... | @@ -66,14 +66,14 @@ test "peer type resolution: *[0]u8, []const u8, and anyerror![]u8" { |
| 66 | 66 | { |
| 67 | 67 | var data = "hi".*; |
| 68 | 68 | const slice = data[0..]; |
| 69 | try expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0); | |
| 70 | try expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1); | |
| 69 | try expectEqual(0, (try peerTypeEmptyArrayAndSliceAndError(true, slice)).len); | |
| 70 | try expectEqual(1, (try peerTypeEmptyArrayAndSliceAndError(false, slice)).len); | |
| 71 | 71 | } |
| 72 | 72 | comptime { |
| 73 | 73 | var data = "hi".*; |
| 74 | 74 | const slice = data[0..]; |
| 75 | try expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0); | |
| 76 | try expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1); | |
| 75 | try expectEqual(0, (try peerTypeEmptyArrayAndSliceAndError(true, slice)).len); | |
| 76 | try expectEqual(1, (try peerTypeEmptyArrayAndSliceAndError(false, slice)).len); | |
| 77 | 77 | } |
| 78 | 78 | } |
| 79 | 79 | fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) anyerror![]u8 { |
| ... | ... | @@ -87,8 +87,8 @@ fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) anyerror![]u8 { |
| 87 | 87 | test "peer type resolution: *const T and ?*T" { |
| 88 | 88 | const a: *const usize = @ptrFromInt(0x123456780); |
| 89 | 89 | const b: ?*usize = @ptrFromInt(0x123456780); |
| 90 | try expect(a == b); | |
| 91 | try expect(b == a); | |
| 90 | try expectEqual(a, b); | |
| 91 | try expectEqual(b, a); | |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | test "peer type resolution: error union switch" { |
| ... | ... | @@ -104,7 +104,7 @@ test "peer type resolution: error union switch" { |
| 104 | 104 | error.B => 1, |
| 105 | 105 | error.C => null, |
| 106 | 106 | }; |
| 107 | try expect(@TypeOf(b) == ?u32); | |
| 107 | try expectEqual(?u32, @TypeOf(b)); | |
| 108 | 108 | |
| 109 | 109 | // The non-error and error cases are only peers if the error case is just a switch expression; |
| 110 | 110 | // 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" { |
| 114 | 114 | error.B => 1, |
| 115 | 115 | error.C => null, |
| 116 | 116 | }; |
| 117 | try expect(@TypeOf(c) == ?u32); | |
| 117 | try expectEqual(?u32, @TypeOf(c)); | |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | // test |
doc/langref/test_pointer_arithmetic.zig+9-9| ... | ... | @@ -1,19 +1,19 @@ |
| 1 | const expect = @import("std").testing.expect; | |
| 1 | const expectEqual = @import("std").testing.expectEqual; | |
| 2 | 2 | |
| 3 | 3 | test "pointer arithmetic with many-item pointer" { |
| 4 | 4 | const array = [_]i32{ 1, 2, 3, 4 }; |
| 5 | 5 | var ptr: [*]const i32 = &array; |
| 6 | 6 | |
| 7 | try expect(ptr[0] == 1); | |
| 7 | try expectEqual(1, ptr[0]); | |
| 8 | 8 | ptr += 1; |
| 9 | try expect(ptr[0] == 2); | |
| 9 | try expectEqual(2, ptr[0]); | |
| 10 | 10 | |
| 11 | 11 | // slicing a many-item pointer without an end is equivalent to |
| 12 | 12 | // pointer arithmetic: `ptr[start..] == ptr + start` |
| 13 | try expect(ptr[1..] == ptr + 1); | |
| 13 | try expectEqual(ptr[1..], ptr + 1); | |
| 14 | 14 | |
| 15 | 15 | // subtraction between any two pointers except slices based on element size is supported |
| 16 | try expect(&ptr[1] - &ptr[0] == 1); | |
| 16 | try expectEqual(1, &ptr[1] - &ptr[0]); | |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | test "pointer arithmetic with slices" { |
| ... | ... | @@ -22,14 +22,14 @@ test "pointer arithmetic with slices" { |
| 22 | 22 | _ = &length; // suppress 'var is never mutated' error |
| 23 | 23 | var slice = array[length..array.len]; |
| 24 | 24 | |
| 25 | try expect(slice[0] == 1); | |
| 26 | try expect(slice.len == 4); | |
| 25 | try expectEqual(1, slice[0]); | |
| 26 | try expectEqual(4, slice.len); | |
| 27 | 27 | |
| 28 | 28 | slice.ptr += 1; |
| 29 | 29 | // now the slice is in an bad state since len has not been updated |
| 30 | 30 | |
| 31 | try expect(slice[0] == 2); | |
| 32 | try expect(slice.len == 4); | |
| 31 | try expectEqual(2, slice[0]); | |
| 32 | try expectEqual(4, slice.len); | |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | // test |
doc/langref/test_pointer_casting.zig+5-5| ... | ... | @@ -1,23 +1,23 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | test "pointer casting" { |
| 5 | 5 | const bytes align(@alignOf(u32)) = [_]u8{ 0x12, 0x12, 0x12, 0x12 }; |
| 6 | 6 | const u32_ptr: *const u32 = @ptrCast(&bytes); |
| 7 | try expect(u32_ptr.* == 0x12121212); | |
| 7 | try expectEqual(0x12121212, u32_ptr.*); | |
| 8 | 8 | |
| 9 | 9 | // Even this example is contrived - there are better ways to do the above than |
| 10 | 10 | // pointer casting. For example, using a slice narrowing cast: |
| 11 | 11 | const u32_value = std.mem.bytesAsSlice(u32, bytes[0..])[0]; |
| 12 | try expect(u32_value == 0x12121212); | |
| 12 | try expectEqual(0x12121212, u32_value); | |
| 13 | 13 | |
| 14 | 14 | // And even another way, the most straightforward way to do it: |
| 15 | try expect(@as(u32, @bitCast(bytes)) == 0x12121212); | |
| 15 | try expectEqual(0x12121212, @as(u32, @bitCast(bytes))); | |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | test "pointer child type" { |
| 19 | 19 | // pointer types have a `child` field which tells you the type they point to. |
| 20 | try expect(@typeInfo(*u32).pointer.child == u32); | |
| 20 | try expectEqual(u32, @typeInfo(*u32).pointer.child); | |
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | // test |
doc/langref/test_pointer_coerce_const_optional.zig+2-2| ... | ... | @@ -1,11 +1,11 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqualStrings = std.testing.expectEqualStrings; | |
| 3 | 3 | const mem = std.mem; |
| 4 | 4 | |
| 5 | 5 | test "cast *[1][*:0]const u8 to []const ?[*:0]const u8" { |
| 6 | 6 | const window_name = [1][*:0]const u8{"window name"}; |
| 7 | 7 | const x: []const ?[*:0]const u8 = &window_name; |
| 8 | try expect(mem.eql(u8, mem.span(x[0].?), "window name")); | |
| 8 | try expectEqualStrings("window name", mem.span(x[0].?)); | |
| 9 | 9 | } |
| 10 | 10 | |
| 11 | 11 | // test |
doc/langref/test_pointer_to_non-byte_aligned_field.zig+2-2| ... | ... | @@ -1,5 +1,5 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | const BitField = packed struct { |
| 5 | 5 | a: u3, |
| ... | ... | @@ -15,7 +15,7 @@ var foo = BitField{ |
| 15 | 15 | |
| 16 | 16 | test "pointer to non-byte-aligned field" { |
| 17 | 17 | const ptr = &foo.b; |
| 18 | try expect(ptr.* == 2); | |
| 18 | try expectEqual(2, ptr.*); | |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | // test |
doc/langref/test_reduce_builtin.zig+4-4| ... | ... | @@ -1,15 +1,15 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | test "vector @reduce" { |
| 5 | 5 | const V = @Vector(4, i32); |
| 6 | 6 | const value = V{ 1, -1, 1, -1 }; |
| 7 | 7 | const result = value > @as(V, @splat(0)); |
| 8 | 8 | // result is { true, false, true, false }; |
| 9 | try comptime expect(@TypeOf(result) == @Vector(4, bool)); | |
| 9 | try comptime expectEqual(@Vector(4, bool), @TypeOf(result)); | |
| 10 | 10 | const is_all_true = @reduce(.And, result); |
| 11 | try comptime expect(@TypeOf(is_all_true) == bool); | |
| 12 | try expect(is_all_true == false); | |
| 11 | try comptime expectEqual(bool, @TypeOf(is_all_true)); | |
| 12 | try expectEqual(false, is_all_true); | |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | 15 | // test |
doc/langref/test_round_builtin.zig+5-5| ... | ... | @@ -1,10 +1,10 @@ |
| 1 | const expect = @import("std").testing.expect; | |
| 1 | const expectEqual = @import("std").testing.expectEqual; | |
| 2 | 2 | |
| 3 | 3 | test "@round" { |
| 4 | try expect(@round(1.4) == 1); | |
| 5 | try expect(@round(1.5) == 2); | |
| 6 | try expect(@round(-1.4) == -1); | |
| 7 | try expect(@round(-2.5) == -3); | |
| 4 | try expectEqual(1, @round(1.4)); | |
| 5 | try expectEqual(2, @round(1.5)); | |
| 6 | try expectEqual(-1, @round(-1.4)); | |
| 7 | try expectEqual(-3, @round(-2.5)); | |
| 8 | 8 | } |
| 9 | 9 | |
| 10 | 10 | // test |
doc/langref/test_sentinel_mismatch.zig+1-1| ... | ... | @@ -1,5 +1,5 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | test "sentinel mismatch" { |
| 5 | 5 | var array = [_]u8{ 3, 2, 1, 0 }; |
doc/langref/test_shuffle_builtin.zig+3-3| ... | ... | @@ -1,5 +1,5 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqualStrings = std.testing.expectEqualStrings; | |
| 3 | 3 | |
| 4 | 4 | test "vector @shuffle" { |
| 5 | 5 | const a = @Vector(7, u8){ 'o', 'l', 'h', 'e', 'r', 'z', 'w' }; |
| ... | ... | @@ -9,12 +9,12 @@ test "vector @shuffle" { |
| 9 | 9 | // Notice that we can re-order, duplicate, or omit elements of the input vector |
| 10 | 10 | const mask1 = @Vector(5, i32){ 2, 3, 1, 1, 0 }; |
| 11 | 11 | const res1: @Vector(5, u8) = @shuffle(u8, a, undefined, mask1); |
| 12 | try expect(std.mem.eql(u8, &@as([5]u8, res1), "hello")); | |
| 12 | try expectEqualStrings("hello", &@as([5]u8, res1)); | |
| 13 | 13 | |
| 14 | 14 | // Combining two vectors |
| 15 | 15 | const mask2 = @Vector(6, i32){ -1, 0, 4, 1, -2, -3 }; |
| 16 | 16 | const res2: @Vector(6, u8) = @shuffle(u8, a, b, mask2); |
| 17 | try expect(std.mem.eql(u8, &@as([6]u8, res2), "world!")); | |
| 17 | try expectEqualStrings("world!", &@as([6]u8, res2)); | |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | // test |
doc/langref/test_simple_union.zig+3-3| ... | ... | @@ -1,5 +1,5 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | const Payload = union { |
| 5 | 5 | int: i64, |
| ... | ... | @@ -8,9 +8,9 @@ const Payload = union { |
| 8 | 8 | }; |
| 9 | 9 | test "simple union" { |
| 10 | 10 | var payload = Payload{ .int = 1234 }; |
| 11 | try expect(payload.int == 1234); | |
| 11 | try expectEqual(1234, payload.int); | |
| 12 | 12 | payload = Payload{ .float = 12.34 }; |
| 13 | try expect(payload.float == 12.34); | |
| 13 | try expectEqual(12.34, payload.float); | |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | // test |
doc/langref/test_single_item_pointer.zig+10-10| ... | ... | @@ -1,4 +1,4 @@ |
| 1 | const expect = @import("std").testing.expect; | |
| 1 | const expectEqual = @import("std").testing.expectEqual; | |
| 2 | 2 | |
| 3 | 3 | test "address of syntax" { |
| 4 | 4 | // Get the address of a variable: |
| ... | ... | @@ -6,17 +6,17 @@ test "address of syntax" { |
| 6 | 6 | const x_ptr = &x; |
| 7 | 7 | |
| 8 | 8 | // Dereference a pointer: |
| 9 | try expect(x_ptr.* == 1234); | |
| 9 | try expectEqual(1234, x_ptr.*); | |
| 10 | 10 | |
| 11 | 11 | // When you get the address of a const variable, you get a const single-item pointer. |
| 12 | try expect(@TypeOf(x_ptr) == *const i32); | |
| 12 | try expectEqual(*const i32, @TypeOf(x_ptr)); | |
| 13 | 13 | |
| 14 | 14 | // If you want to mutate the value, you'd need an address of a mutable variable: |
| 15 | 15 | var y: i32 = 5678; |
| 16 | 16 | const y_ptr = &y; |
| 17 | try expect(@TypeOf(y_ptr) == *i32); | |
| 17 | try expectEqual(*i32, @TypeOf(y_ptr)); | |
| 18 | 18 | y_ptr.* += 1; |
| 19 | try expect(y_ptr.* == 5679); | |
| 19 | try expectEqual(5679, y_ptr.*); | |
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | test "pointer array access" { |
| ... | ... | @@ -25,11 +25,11 @@ test "pointer array access" { |
| 25 | 25 | // does not support pointer arithmetic. |
| 26 | 26 | var array = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; |
| 27 | 27 | const ptr = &array[2]; |
| 28 | try expect(@TypeOf(ptr) == *u8); | |
| 28 | try expectEqual(*u8, @TypeOf(ptr)); | |
| 29 | 29 | |
| 30 | try expect(array[2] == 3); | |
| 30 | try expectEqual(3, array[2]); | |
| 31 | 31 | ptr.* += 1; |
| 32 | try expect(array[2] == 4); | |
| 32 | try expectEqual(4, array[2]); | |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | test "slice syntax" { |
| ... | ... | @@ -39,11 +39,11 @@ test "slice syntax" { |
| 39 | 39 | |
| 40 | 40 | // Convert to array pointer using slice syntax: |
| 41 | 41 | const x_array_ptr = x_ptr[0..1]; |
| 42 | try expect(@TypeOf(x_array_ptr) == *[1]i32); | |
| 42 | try expectEqual(*[1]i32, @TypeOf(x_array_ptr)); | |
| 43 | 43 | |
| 44 | 44 | // Coerce to many-item pointer: |
| 45 | 45 | const x_many_ptr: [*]i32 = x_array_ptr; |
| 46 | try expect(x_many_ptr[0] == 1234); | |
| 46 | try expectEqual(1234, x_many_ptr[0]); | |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | // test |
doc/langref/test_slice_bounds.zig+4-4| ... | ... | @@ -1,15 +1,15 @@ |
| 1 | const expect = @import("std").testing.expect; | |
| 1 | const expectEqual = @import("std").testing.expectEqual; | |
| 2 | 2 | |
| 3 | 3 | test "pointer slicing" { |
| 4 | 4 | var array = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; |
| 5 | 5 | var start: usize = 2; // var to make it runtime-known |
| 6 | 6 | _ = &start; // suppress 'var is never mutated' error |
| 7 | 7 | const slice = array[start..4]; |
| 8 | try expect(slice.len == 2); | |
| 8 | try expectEqual(2, slice.len); | |
| 9 | 9 | |
| 10 | try expect(array[3] == 4); | |
| 10 | try expectEqual(4, array[3]); | |
| 11 | 11 | slice[1] += 1; |
| 12 | try expect(array[3] == 5); | |
| 12 | try expectEqual(5, array[3]); | |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | 15 | // test |
doc/langref/test_slices.zig+9-9| ... | ... | @@ -1,6 +1,6 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 3 | const mem = std.mem; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | const expectEqualStrings = std.testing.expectEqualStrings; | |
| 4 | 4 | const fmt = std.fmt; |
| 5 | 5 | |
| 6 | 6 | test "using slices for strings" { |
| ... | ... | @@ -23,13 +23,13 @@ test "using slices for strings" { |
| 23 | 23 | // Generally, you can use UTF-8 and not worry about whether something is a |
| 24 | 24 | // string. If you don't need to deal with individual characters, no need |
| 25 | 25 | // to decode. |
| 26 | try expect(mem.eql(u8, hello_world, "hello 世界")); | |
| 26 | try expectEqualStrings("hello 世界", hello_world); | |
| 27 | 27 | } |
| 28 | 28 | |
| 29 | 29 | test "slice pointer" { |
| 30 | 30 | var array: [10]u8 = undefined; |
| 31 | 31 | const ptr = &array; |
| 32 | try expect(@TypeOf(ptr) == *[10]u8); | |
| 32 | try expectEqual(*[10]u8, @TypeOf(ptr)); | |
| 33 | 33 | |
| 34 | 34 | // A pointer to an array can be sliced just like an array: |
| 35 | 35 | var start: usize = 0; |
| ... | ... | @@ -37,16 +37,16 @@ test "slice pointer" { |
| 37 | 37 | _ = .{ &start, &end }; |
| 38 | 38 | const slice = ptr[start..end]; |
| 39 | 39 | // The slice is mutable because we sliced a mutable pointer. |
| 40 | try expect(@TypeOf(slice) == []u8); | |
| 40 | try expectEqual([]u8, @TypeOf(slice)); | |
| 41 | 41 | slice[2] = 3; |
| 42 | try expect(array[2] == 3); | |
| 42 | try expectEqual(3, array[2]); | |
| 43 | 43 | |
| 44 | 44 | // Again, slicing with comptime-known indexes will produce another pointer |
| 45 | 45 | // to an array: |
| 46 | 46 | const ptr2 = slice[2..3]; |
| 47 | try expect(ptr2.len == 1); | |
| 48 | try expect(ptr2[0] == 3); | |
| 49 | try expect(@TypeOf(ptr2) == *[1]u8); | |
| 47 | try expectEqual(1, ptr2.len); | |
| 48 | try expectEqual(3, ptr2[0]); | |
| 49 | try expectEqual(*[1]u8, @TypeOf(ptr2)); | |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | // test |
doc/langref/test_splat_builtin.zig+3-3| ... | ... | @@ -1,16 +1,16 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqualSlices = std.testing.expectEqualSlices; | |
| 3 | 3 | |
| 4 | 4 | test "vector @splat" { |
| 5 | 5 | const scalar: u32 = 5; |
| 6 | 6 | const result: @Vector(4, u32) = @splat(scalar); |
| 7 | try expect(std.mem.eql(u32, &@as([4]u32, result), &[_]u32{ 5, 5, 5, 5 })); | |
| 7 | try expectEqualSlices(u32, &[_]u32{ 5, 5, 5, 5 }, &@as([4]u32, result)); | |
| 8 | 8 | } |
| 9 | 9 | |
| 10 | 10 | test "array @splat" { |
| 11 | 11 | const scalar: u32 = 5; |
| 12 | 12 | const result: [4]u32 = @splat(scalar); |
| 13 | try expect(std.mem.eql(u32, &@as([4]u32, result), &[_]u32{ 5, 5, 5, 5 })); | |
| 13 | try expectEqualSlices(u32, &[_]u32{ 5, 5, 5, 5 }, &@as([4]u32, result)); | |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | // test |
doc/langref/test_src_builtin.zig+3-2| ... | ... | @@ -1,5 +1,6 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | const expect = std.testing.expect; |
| 3 | const expectEqual = std.testing.expectEqual; | |
| 3 | 4 | |
| 4 | 5 | test "@src" { |
| 5 | 6 | try doTheTest(); |
| ... | ... | @@ -8,8 +9,8 @@ test "@src" { |
| 8 | 9 | fn doTheTest() !void { |
| 9 | 10 | const src = @src(); |
| 10 | 11 | |
| 11 | try expect(src.line == 9); | |
| 12 | try expect(src.column == 17); | |
| 12 | try expectEqual(10, src.line); | |
| 13 | try expectEqual(17, src.column); | |
| 13 | 14 | try expect(std.mem.endsWith(u8, src.fn_name, "doTheTest")); |
| 14 | 15 | try expect(std.mem.endsWith(u8, src.file, "test_src_builtin.zig")); |
| 15 | 16 | } |
doc/langref/test_static_local_variable.zig+3-3| ... | ... | @@ -1,9 +1,9 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | test "static local variable" { |
| 5 | try expect(foo() == 1235); | |
| 6 | try expect(foo() == 1236); | |
| 5 | try expectEqual(1235, foo()); | |
| 6 | try expectEqual(1236, foo()); | |
| 7 | 7 | } |
| 8 | 8 | |
| 9 | 9 | fn foo() i32 { |
doc/langref/test_struct_result.zig+3-3| ... | ... | @@ -1,5 +1,5 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | const Point = struct { x: i32, y: i32 }; |
| 5 | 5 | |
| ... | ... | @@ -8,8 +8,8 @@ test "anonymous struct literal" { |
| 8 | 8 | .x = 13, |
| 9 | 9 | .y = 67, |
| 10 | 10 | }; |
| 11 | try expect(pt.x == 13); | |
| 12 | try expect(pt.y == 67); | |
| 11 | try expectEqual(13, pt.x); | |
| 12 | try expectEqual(67, pt.y); | |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | 15 | // test |
doc/langref/test_structs.zig+11-11| ... | ... | @@ -34,12 +34,12 @@ const Vec3 = struct { |
| 34 | 34 | test "dot product" { |
| 35 | 35 | const v1 = Vec3.init(1.0, 0.0, 0.0); |
| 36 | 36 | const v2 = Vec3.init(0.0, 1.0, 0.0); |
| 37 | try expect(v1.dot(v2) == 0.0); | |
| 37 | try expectEqual(0.0, v1.dot(v2)); | |
| 38 | 38 | |
| 39 | 39 | // Other than being available to call with dot syntax, struct methods are |
| 40 | 40 | // not special. You can reference them as any other declaration inside |
| 41 | 41 | // the struct: |
| 42 | try expect(Vec3.dot(v1, v2) == 0.0); | |
| 42 | try expectEqual(0.0, Vec3.dot(v1, v2)); | |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | // Structs can have declarations. |
| ... | ... | @@ -48,8 +48,8 @@ const Empty = struct { |
| 48 | 48 | pub const PI = 3.14; |
| 49 | 49 | }; |
| 50 | 50 | test "struct namespaced variable" { |
| 51 | try expect(Empty.PI == 3.14); | |
| 52 | try expect(@sizeOf(Empty) == 0); | |
| 51 | try expectEqual(3.14, Empty.PI); | |
| 52 | try expectEqual(0, @sizeOf(Empty)); | |
| 53 | 53 | |
| 54 | 54 | // Empty structs can be instantiated the same as usual. |
| 55 | 55 | const does_nothing: Empty = .{}; |
| ... | ... | @@ -69,7 +69,7 @@ test "field parent pointer" { |
| 69 | 69 | .y = 0.5678, |
| 70 | 70 | }; |
| 71 | 71 | setYBasedOnX(&point.x, 0.9); |
| 72 | try expect(point.y == 0.9); | |
| 72 | try expectEqual(0.9, point.y); | |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | // Structs can be returned from functions. |
| ... | ... | @@ -89,19 +89,19 @@ fn LinkedList(comptime T: type) type { |
| 89 | 89 | |
| 90 | 90 | test "linked list" { |
| 91 | 91 | // Functions called at compile-time are memoized. |
| 92 | try expect(LinkedList(i32) == LinkedList(i32)); | |
| 92 | try expectEqual(LinkedList(i32), LinkedList(i32)); | |
| 93 | 93 | |
| 94 | 94 | const list = LinkedList(i32){ |
| 95 | 95 | .first = null, |
| 96 | 96 | .last = null, |
| 97 | 97 | .len = 0, |
| 98 | 98 | }; |
| 99 | try expect(list.len == 0); | |
| 99 | try expectEqual(0, list.len); | |
| 100 | 100 | |
| 101 | 101 | // Since types are first class values you can instantiate the type |
| 102 | 102 | // by assigning it to a variable: |
| 103 | 103 | const ListOfInts = LinkedList(i32); |
| 104 | try expect(ListOfInts == LinkedList(i32)); | |
| 104 | try expectEqual(LinkedList(i32), ListOfInts); | |
| 105 | 105 | |
| 106 | 106 | var node = ListOfInts.Node{ |
| 107 | 107 | .prev = null, |
| ... | ... | @@ -117,10 +117,10 @@ test "linked list" { |
| 117 | 117 | // When using a pointer to a struct, fields can be accessed directly, |
| 118 | 118 | // without explicitly dereferencing the pointer. |
| 119 | 119 | // So you can do |
| 120 | try expect(list2.first.?.data == 1234); | |
| 121 | // instead of try expect(list2.first.?.*.data == 1234); | |
| 120 | try expectEqual(1234, list2.first.?.data); | |
| 121 | // instead of try expectEqual(1234, list2.first.?.*.data); | |
| 122 | 122 | } |
| 123 | 123 | |
| 124 | const expect = @import("std").testing.expect; | |
| 124 | const expectEqual = @import("std").testing.expectEqual; | |
| 125 | 125 | |
| 126 | 126 | // test |
doc/langref/test_switch.zig+2-2| ... | ... | @@ -1,6 +1,6 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | const builtin = @import("builtin"); |
| 3 | const expect = std.testing.expect; | |
| 3 | const expectEqual = std.testing.expectEqual; | |
| 4 | 4 | |
| 5 | 5 | test "switch simple" { |
| 6 | 6 | const a: u64 = 10; |
| ... | ... | @@ -40,7 +40,7 @@ test "switch simple" { |
| 40 | 40 | else => 9, |
| 41 | 41 | }; |
| 42 | 42 | |
| 43 | try expect(b == 1); | |
| 43 | try expectEqual(1, b); | |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | // Switch expressions can be used outside a function: |
doc/langref/test_switch_modify_tagged_union.zig+2-2| ... | ... | @@ -1,5 +1,5 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | const ComplexTypeTag = enum { |
| 5 | 5 | ok, |
| ... | ... | @@ -18,7 +18,7 @@ test "modify tagged union in switch" { |
| 18 | 18 | ComplexTypeTag.not_ok => unreachable, |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | try expect(c.ok == 43); | |
| 21 | try expectEqual(43, c.ok); | |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | // test |
doc/langref/test_switch_tagged_union.zig+3-3| ... | ... | @@ -1,4 +1,4 @@ |
| 1 | const expect = @import("std").testing.expect; | |
| 1 | const expectEqual = @import("std").testing.expectEqual; | |
| 2 | 2 | |
| 3 | 3 | test "switch on tagged union" { |
| 4 | 4 | const Point = struct { |
| ... | ... | @@ -31,8 +31,8 @@ test "switch on tagged union" { |
| 31 | 31 | Item.d => 8, |
| 32 | 32 | }; |
| 33 | 33 | |
| 34 | try expect(b == 6); | |
| 35 | try expect(a.c.x == 2); | |
| 34 | try expectEqual(6, b); | |
| 35 | try expectEqual(2, a.c.x); | |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | // test |
doc/langref/test_tagName.zig+2-2| ... | ... | @@ -1,5 +1,5 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqualSlices = std.testing.expectEqualSlices; | |
| 3 | 3 | |
| 4 | 4 | const Small2 = union(enum) { |
| 5 | 5 | a: i32, |
| ... | ... | @@ -7,7 +7,7 @@ const Small2 = union(enum) { |
| 7 | 7 | c: u8, |
| 8 | 8 | }; |
| 9 | 9 | test "@tagName" { |
| 10 | try expect(std.mem.eql(u8, @tagName(Small2.a), "a")); | |
| 10 | try expectEqualSlices(u8, "a", @tagName(Small2.a)); | |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | // test |
doc/langref/test_tagged_union.zig+4-4| ... | ... | @@ -1,5 +1,5 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | const ComplexTypeTag = enum { |
| 5 | 5 | ok, |
| ... | ... | @@ -12,10 +12,10 @@ const ComplexType = union(ComplexTypeTag) { |
| 12 | 12 | |
| 13 | 13 | test "switch on tagged union" { |
| 14 | 14 | const c = ComplexType{ .ok = 42 }; |
| 15 | try expect(@as(ComplexTypeTag, c) == ComplexTypeTag.ok); | |
| 15 | try expectEqual(ComplexTypeTag.ok, @as(ComplexTypeTag, c)); | |
| 16 | 16 | |
| 17 | 17 | switch (c) { |
| 18 | .ok => |value| try expect(value == 42), | |
| 18 | .ok => |value| try expectEqual(42, value), | |
| 19 | 19 | .not_ok => unreachable, |
| 20 | 20 | } |
| 21 | 21 | |
| ... | ... | @@ -29,7 +29,7 @@ test "switch on tagged union" { |
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | test "get tag type" { |
| 32 | try expect(std.meta.Tag(ComplexType) == ComplexTypeTag); | |
| 32 | try expectEqual(ComplexTypeTag, std.meta.Tag(ComplexType)); | |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | // test |
doc/langref/test_tagged_union_with_tag_values.zig+3-3| ... | ... | @@ -1,5 +1,5 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | const Tagged = union(enum(u32)) { |
| 5 | 5 | int: i64 = 123, |
| ... | ... | @@ -8,10 +8,10 @@ const Tagged = union(enum(u32)) { |
| 8 | 8 | |
| 9 | 9 | test "tag values" { |
| 10 | 10 | const int: Tagged = .{ .int = -40 }; |
| 11 | try expect(@intFromEnum(int) == 123); | |
| 11 | try expectEqual(123, @intFromEnum(int)); | |
| 12 | 12 | |
| 13 | 13 | const boolean: Tagged = .{ .boolean = false }; |
| 14 | try expect(@intFromEnum(boolean) == 67); | |
| 14 | try expectEqual(67, @intFromEnum(boolean)); | |
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | // test |
doc/langref/test_this_builtin.zig+2-2| ... | ... | @@ -1,10 +1,10 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | test "@This()" { |
| 5 | 5 | var items = [_]i32{ 1, 2, 3, 4 }; |
| 6 | 6 | const list = List(i32){ .items = items[0..] }; |
| 7 | try expect(list.length() == 4); | |
| 7 | try expectEqual(4, list.length()); | |
| 8 | 8 | } |
| 9 | 9 | |
| 10 | 10 | fn List(comptime T: type) type { |
doc/langref/test_truncate_builtin.zig+2-2| ... | ... | @@ -1,10 +1,10 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | |
| 4 | 4 | test "integer truncation" { |
| 5 | 5 | const a: u16 = 0xabcd; |
| 6 | 6 | const b: u8 = @truncate(a); |
| 7 | try expect(b == 0xcd); | |
| 7 | try expectEqual(0xcd, b); | |
| 8 | 8 | } |
| 9 | 9 | |
| 10 | 10 | // test |
doc/langref/test_tuples.zig+5-4| ... | ... | @@ -1,5 +1,6 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | const expect = std.testing.expect; |
| 3 | const expectEqual = std.testing.expectEqual; | |
| 3 | 4 | |
| 4 | 5 | test "tuple" { |
| 5 | 6 | const values = .{ |
| ... | ... | @@ -8,14 +9,14 @@ test "tuple" { |
| 8 | 9 | true, |
| 9 | 10 | "hi", |
| 10 | 11 | } ++ .{false} ** 2; |
| 11 | try expect(values[0] == 1234); | |
| 12 | try expect(values[4] == false); | |
| 12 | try expectEqual(1234, values[0]); | |
| 13 | try expectEqual(false, values[4]); | |
| 13 | 14 | inline for (values, 0..) |v, i| { |
| 14 | 15 | if (i != 2) continue; |
| 15 | 16 | try expect(v); |
| 16 | 17 | } |
| 17 | try expect(values.len == 6); | |
| 18 | try expect(values.@"3"[0] == 'h'); | |
| 18 | try expectEqual(6, values.len); | |
| 19 | try expectEqual('h', values.@"3"[0]); | |
| 19 | 20 | } |
| 20 | 21 | |
| 21 | 22 | // test |
doc/langref/test_variable_alignment.zig+4-4| ... | ... | @@ -1,14 +1,14 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | const builtin = @import("builtin"); |
| 3 | const expect = std.testing.expect; | |
| 3 | const expectEqual = std.testing.expectEqual; | |
| 4 | 4 | |
| 5 | 5 | test "variable alignment" { |
| 6 | 6 | var x: i32 = 1234; |
| 7 | 7 | const align_of_i32 = @alignOf(@TypeOf(x)); |
| 8 | try expect(@TypeOf(&x) == *i32); | |
| 9 | try expect(*i32 == *align(align_of_i32) i32); | |
| 8 | try expectEqual(*i32, @TypeOf(&x)); | |
| 9 | try expectEqual(*align(align_of_i32) i32, *i32); | |
| 10 | 10 | if (builtin.target.cpu.arch == .x86_64) { |
| 11 | try expect(@typeInfo(*i32).pointer.alignment == 4); | |
| 11 | try expectEqual(4, @typeInfo(*i32).pointer.alignment); | |
| 12 | 12 | } |
| 13 | 13 | } |
| 14 | 14 |
doc/langref/test_variable_func_alignment.zig+11-11| ... | ... | @@ -1,14 +1,14 @@ |
| 1 | const expect = @import("std").testing.expect; | |
| 1 | const expectEqual = @import("std").testing.expectEqual; | |
| 2 | 2 | |
| 3 | 3 | var foo: u8 align(4) = 100; |
| 4 | 4 | |
| 5 | 5 | test "global variable alignment" { |
| 6 | try expect(@typeInfo(@TypeOf(&foo)).pointer.alignment == 4); | |
| 7 | try expect(@TypeOf(&foo) == *align(4) u8); | |
| 6 | try expectEqual(4, @typeInfo(@TypeOf(&foo)).pointer.alignment); | |
| 7 | try expectEqual(*align(4) u8, @TypeOf(&foo)); | |
| 8 | 8 | const as_pointer_to_array: *align(4) [1]u8 = &foo; |
| 9 | 9 | const as_slice: []align(4) u8 = as_pointer_to_array; |
| 10 | 10 | const as_unaligned_slice: []u8 = as_slice; |
| 11 | try expect(as_unaligned_slice[0] == 100); | |
| 11 | try expectEqual(100, as_unaligned_slice[0]); | |
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | fn derp() align(@sizeOf(usize) * 2) i32 { |
| ... | ... | @@ -18,17 +18,17 @@ fn noop1() align(1) void {} |
| 18 | 18 | fn noop4() align(4) void {} |
| 19 | 19 | |
| 20 | 20 | test "function alignment" { |
| 21 | try expect(derp() == 1234); | |
| 22 | try expect(@TypeOf(derp) == fn () i32); | |
| 23 | try expect(@TypeOf(&derp) == *align(@sizeOf(usize) * 2) const fn () i32); | |
| 21 | try expectEqual(1234, derp()); | |
| 22 | try expectEqual(fn () i32, @TypeOf(derp)); | |
| 23 | try expectEqual(*align(@sizeOf(usize) * 2) const fn () i32, @TypeOf(&derp)); | |
| 24 | 24 | |
| 25 | 25 | noop1(); |
| 26 | try expect(@TypeOf(noop1) == fn () void); | |
| 27 | try expect(@TypeOf(&noop1) == *align(1) const fn () void); | |
| 26 | try expectEqual(fn () void, @TypeOf(noop1)); | |
| 27 | try expectEqual(*align(1) const fn () void, @TypeOf(&noop1)); | |
| 28 | 28 | |
| 29 | 29 | noop4(); |
| 30 | try expect(@TypeOf(noop4) == fn () void); | |
| 31 | try expect(@TypeOf(&noop4) == *align(4) const fn () void); | |
| 30 | try expectEqual(fn () void, @TypeOf(noop4)); | |
| 31 | try expectEqual(*align(4) const fn () void, @TypeOf(&noop4)); | |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | // test |
doc/langref/test_variadic_function.zig+1-1| ... | ... | @@ -4,7 +4,7 @@ const testing = std.testing; |
| 4 | 4 | pub extern "c" fn printf(format: [*:0]const u8, ...) c_int; |
| 5 | 5 | |
| 6 | 6 | test "variadic function" { |
| 7 | try testing.expect(printf("Hello, world!\n") == 14); | |
| 7 | try testing.expectEqual(14, printf("Hello, world!\n")); | |
| 8 | 8 | try testing.expect(@typeInfo(@TypeOf(printf)).@"fn".is_var_args); |
| 9 | 9 | } |
| 10 | 10 |
doc/langref/test_volatile.zig+2-2| ... | ... | @@ -1,8 +1,8 @@ |
| 1 | const expect = @import("std").testing.expect; | |
| 1 | const expectEqual = @import("std").testing.expectEqual; | |
| 2 | 2 | |
| 3 | 3 | test "volatile" { |
| 4 | 4 | const mmio_ptr: *volatile u8 = @ptrFromInt(0x12345678); |
| 5 | try expect(@TypeOf(mmio_ptr) == *volatile u8); | |
| 5 | try expectEqual(*volatile u8, @TypeOf(mmio_ptr)); | |
| 6 | 6 | } |
| 7 | 7 | |
| 8 | 8 | // test |
doc/langref/test_wasmMemoryGrow_builtin.zig+3-3| ... | ... | @@ -1,13 +1,13 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | const native_arch = @import("builtin").target.cpu.arch; |
| 3 | const expect = std.testing.expect; | |
| 3 | const expectEqual = std.testing.expectEqual; | |
| 4 | 4 | |
| 5 | 5 | test "@wasmMemoryGrow" { |
| 6 | 6 | if (native_arch != .wasm32) return error.SkipZigTest; |
| 7 | 7 | |
| 8 | 8 | const prev = @wasmMemorySize(0); |
| 9 | try expect(prev == @wasmMemoryGrow(0, 1)); | |
| 10 | try expect(prev + 1 == @wasmMemorySize(0)); | |
| 9 | try expectEqual(@wasmMemoryGrow(0, 1), prev); | |
| 10 | try expectEqual(@wasmMemorySize(0), prev + 1); | |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | // test |
doc/langref/test_while.zig+2-2| ... | ... | @@ -1,11 +1,11 @@ |
| 1 | const expect = @import("std").testing.expect; | |
| 1 | const expectEqual = @import("std").testing.expectEqual; | |
| 2 | 2 | |
| 3 | 3 | test "while basic" { |
| 4 | 4 | var i: usize = 0; |
| 5 | 5 | while (i < 10) { |
| 6 | 6 | i += 1; |
| 7 | 7 | } |
| 8 | try expect(i == 10); | |
| 8 | try expectEqual(10, i); | |
| 9 | 9 | } |
| 10 | 10 | |
| 11 | 11 | // test |
doc/langref/test_while_break.zig+2-2| ... | ... | @@ -1,4 +1,4 @@ |
| 1 | const expect = @import("std").testing.expect; | |
| 1 | const expectEqual = @import("std").testing.expectEqual; | |
| 2 | 2 | |
| 3 | 3 | test "while break" { |
| 4 | 4 | var i: usize = 0; |
| ... | ... | @@ -7,7 +7,7 @@ test "while break" { |
| 7 | 7 | break; |
| 8 | 8 | i += 1; |
| 9 | 9 | } |
| 10 | try expect(i == 10); | |
| 10 | try expectEqual(10, i); | |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | // test |
doc/langref/test_while_continue.zig+2-2| ... | ... | @@ -1,4 +1,4 @@ |
| 1 | const expect = @import("std").testing.expect; | |
| 1 | const expectEqual = @import("std").testing.expectEqual; | |
| 2 | 2 | |
| 3 | 3 | test "while continue" { |
| 4 | 4 | var i: usize = 0; |
| ... | ... | @@ -8,7 +8,7 @@ test "while continue" { |
| 8 | 8 | continue; |
| 9 | 9 | break; |
| 10 | 10 | } |
| 11 | try expect(i == 10); | |
| 11 | try expectEqual(10, i); | |
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | // test |
doc/langref/test_while_continue_expression.zig+2-1| ... | ... | @@ -1,9 +1,10 @@ |
| 1 | const expectEqual = @import("std").testing.expectEqual; | |
| 1 | 2 | const expect = @import("std").testing.expect; |
| 2 | 3 | |
| 3 | 4 | test "while loop continue expression" { |
| 4 | 5 | var i: usize = 0; |
| 5 | 6 | while (i < 10) : (i += 1) {} |
| 6 | try expect(i == 10); | |
| 7 | try expectEqual(10, i); | |
| 7 | 8 | } |
| 8 | 9 | |
| 9 | 10 | test "while loop continue expression, more complicated" { |
doc/langref/test_while_error_capture.zig+2-2| ... | ... | @@ -1,4 +1,4 @@ |
| 1 | const expect = @import("std").testing.expect; | |
| 1 | const expectEqual = @import("std").testing.expectEqual; | |
| 2 | 2 | |
| 3 | 3 | test "while error union capture" { |
| 4 | 4 | var sum1: u32 = 0; |
| ... | ... | @@ -6,7 +6,7 @@ test "while error union capture" { |
| 6 | 6 | while (eventuallyErrorSequence()) |value| { |
| 7 | 7 | sum1 += value; |
| 8 | 8 | } else |err| { |
| 9 | try expect(err == error.ReachedZero); | |
| 9 | try expectEqual(error.ReachedZero, err); | |
| 10 | 10 | } |
| 11 | 11 | } |
| 12 | 12 |
doc/langref/test_while_null_capture.zig+4-4| ... | ... | @@ -1,4 +1,4 @@ |
| 1 | const expect = @import("std").testing.expect; | |
| 1 | const expectEqual = @import("std").testing.expectEqual; | |
| 2 | 2 | |
| 3 | 3 | test "while null capture" { |
| 4 | 4 | var sum1: u32 = 0; |
| ... | ... | @@ -6,7 +6,7 @@ test "while null capture" { |
| 6 | 6 | while (eventuallyNullSequence()) |value| { |
| 7 | 7 | sum1 += value; |
| 8 | 8 | } |
| 9 | try expect(sum1 == 3); | |
| 9 | try expectEqual(3, sum1); | |
| 10 | 10 | |
| 11 | 11 | // null capture with an else block |
| 12 | 12 | var sum2: u32 = 0; |
| ... | ... | @@ -14,7 +14,7 @@ test "while null capture" { |
| 14 | 14 | while (eventuallyNullSequence()) |value| { |
| 15 | 15 | sum2 += value; |
| 16 | 16 | } else { |
| 17 | try expect(sum2 == 3); | |
| 17 | try expectEqual(3, sum2); | |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | // null capture with a continue expression |
| ... | ... | @@ -24,7 +24,7 @@ test "while null capture" { |
| 24 | 24 | while (eventuallyNullSequence()) |value| : (i += 1) { |
| 25 | 25 | sum3 += value; |
| 26 | 26 | } |
| 27 | try expect(i == 3); | |
| 27 | try expectEqual(3, i); | |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | var numbers_left: u32 = undefined; |
doc/langref/test_wraparound_semantics.zig+3-3| ... | ... | @@ -1,14 +1,14 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | |
| 2 | const expectEqual = std.testing.expectEqual; | |
| 3 | 3 | const minInt = std.math.minInt; |
| 4 | 4 | const maxInt = std.math.maxInt; |
| 5 | 5 | |
| 6 | 6 | test "wraparound addition and subtraction" { |
| 7 | 7 | const x: i32 = maxInt(i32); |
| 8 | 8 | const min_val = x +% 1; |
| 9 | try expect(min_val == minInt(i32)); | |
| 9 | try expectEqual(minInt(i32), min_val); | |
| 10 | 10 | const max_val = min_val -% 1; |
| 11 | try expect(max_val == maxInt(i32)); | |
| 11 | try expectEqual(maxInt(i32), max_val); | |
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | // test |
doc/langref/testing_detect_leak.zig+1-1| ... | ... | @@ -6,7 +6,7 @@ test "detect leak" { |
| 6 | 6 | // missing `defer list.deinit(gpa);` |
| 7 | 7 | try list.append(gpa, '☔'); |
| 8 | 8 | |
| 9 | try std.testing.expect(list.items.len == 1); | |
| 9 | try std.testing.expectEqual(1, list.items.len); | |
| 10 | 10 | } |
| 11 | 11 | |
| 12 | 12 | // test_error=1 tests leaked memory |
doc/langref/testing_introduction.zig+5-1| ... | ... | @@ -7,12 +7,16 @@ test "expect addOne adds one to 41" { |
| 7 | 7 | // It will return an error if its argument is false to indicate a failure. |
| 8 | 8 | // `try` is used to return an error to the test runner to notify it that the test failed. |
| 9 | 9 | try std.testing.expect(addOne(41) == 42); |
| 10 | ||
| 11 | // However, in most cases it is more convenient to use a more specific function like `expectEqual`. | |
| 12 | // This gives you much clearer and more helpful error messages when a test fails. | |
| 13 | try std.testing.expectEqual(42, addOne(41)); | |
| 10 | 14 | } |
| 11 | 15 | |
| 12 | 16 | test addOne { |
| 13 | 17 | // A test name can also be written using an identifier. |
| 14 | 18 | // This is a doctest, and serves as documentation for `addOne`. |
| 15 | try std.testing.expect(addOne(41) == 42); | |
| 19 | try std.testing.expectEqual(42, addOne(41)); | |
| 16 | 20 | } |
| 17 | 21 | |
| 18 | 22 | /// The function `addOne` adds one to the number given as its argument. |