authorgravatar for mrpaul@aestheticwisdom.comPaul Espinosa <mrpaul@aestheticwisdom.com> 2020-07-11 09:27:26+07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-10-22 15:54:57-07:00
logddd39b994b1eb9751d06227c6db02f15a5f71e9f
tree0ca901104b628f09ed01446a260daef11a0e9c42
parent1e13e8e8172c1409c5152e8b4e935c5a1d31704b

Use std.testing.expect in language reference samples

In this commit, the code samples in the language reference have been changed to use `std.testing.expect` rather than `std.debug.assert` when they are written in `test` code. This will teach Zig learners best practices when they write their own test code. Not all uses of `std.debug.assert` have been replaced. There are examples where using `assert` fits the context of the sample. Using `std.debug.assert` in test code can lead to errors if running tests in ReleaseFast mode. In ReleaseFast mode, the `unreachable` in `assert` is undefined behavior. It is possible that `assert` always causes `zig test` to pass thus possibly leading to incorrect test code outcomes. The goal is to prevent incorrect code from passing test cases. Closes #5836

1 files changed, 426 insertions(+), 426 deletions(-)

doc/langref.html.in+426-426
...@@ -344,16 +344,16 @@ pub fn main() void {...@@ -344,16 +344,16 @@ pub fn main() void {
344 {#header_close#}344 {#header_close#}
345 {#header_open|Comments#}345 {#header_open|Comments#}
346 {#code_begin|test|comments#}346 {#code_begin|test|comments#}
347const assert = @import("std").debug.assert;347const expect = @import("std").testing.expect;
348348
349test "comments" {349test "comments" {
350 // Comments in Zig start with "//" and end at the next LF byte (end of line).350 // Comments in Zig start with "//" and end at the next LF byte (end of line).
351 // The below line is a comment, and won't be executed.351 // The below line is a comment, and won't be executed.
352352
353 //assert(false);353 //expect(false);
354354
355 const x = true; // another comment355 const x = true; // another comment
356 assert(x);356 expect(x);
357}357}
358 {#code_end#}358 {#code_end#}
359 <p>359 <p>
...@@ -695,19 +695,19 @@ pub fn main() void {...@@ -695,19 +695,19 @@ pub fn main() void {
695 and character literals.695 and character literals.
696 </p>696 </p>
697 {#code_begin|test#}697 {#code_begin|test#}
698const assert = @import("std").debug.assert;698const expect = @import("std").testing.expect;
699const mem = @import("std").mem;699const mem = @import("std").mem;
700700
701test "string literals" {701test "string literals" {
702 const bytes = "hello";702 const bytes = "hello";
703 assert(@TypeOf(bytes) == *const [5:0]u8);703 expect(@TypeOf(bytes) == *const [5:0]u8);
704 assert(bytes.len == 5);704 expect(bytes.len == 5);
705 assert(bytes[1] == 'e');705 expect(bytes[1] == 'e');
706 assert(bytes[5] == 0);706 expect(bytes[5] == 0);
707 assert('e' == '\x65');707 expect('e' == '\x65');
708 assert('\u{1f4a9}' == 128169);708 expect('\u{1f4a9}' == 128169);
709 assert('💯' == 128175);709 expect('💯' == 128175);
710 assert(mem.eql(u8, "hello", "h\x65llo"));710 expect(mem.eql(u8, "hello", "h\x65llo"));
711}711}
712 {#code_end#}712 {#code_end#}
713 {#see_also|Arrays|Zig Test|Source Encoding#}713 {#see_also|Arrays|Zig Test|Source Encoding#}
...@@ -800,14 +800,14 @@ test "assignment" {...@@ -800,14 +800,14 @@ test "assignment" {
800 <p>{#syntax#}const{#endsyntax#} applies to all of the bytes that the identifier immediately addresses. {#link|Pointers#} have their own const-ness.</p>800 <p>{#syntax#}const{#endsyntax#} applies to all of the bytes that the identifier immediately addresses. {#link|Pointers#} have their own const-ness.</p>
801 <p>If you need a variable that you can modify, use the {#syntax#}var{#endsyntax#} keyword:</p>801 <p>If you need a variable that you can modify, use the {#syntax#}var{#endsyntax#} keyword:</p>
802 {#code_begin|test#}802 {#code_begin|test#}
803const assert = @import("std").debug.assert;803const expect = @import("std").testing.expect;
804804
805test "var" {805test "var" {
806 var y: i32 = 5678;806 var y: i32 = 5678;
807807
808 y += 1;808 y += 1;
809809
810 assert(y == 5679);810 expect(y == 5679);
811}811}
812 {#code_end#}812 {#code_end#}
813 <p>Variables must be initialized:</p>813 <p>Variables must be initialized:</p>
...@@ -821,12 +821,12 @@ test "initialization" {...@@ -821,12 +821,12 @@ test "initialization" {
821 {#header_open|undefined#}821 {#header_open|undefined#}
822 <p>Use {#syntax#}undefined{#endsyntax#} to leave variables uninitialized:</p>822 <p>Use {#syntax#}undefined{#endsyntax#} to leave variables uninitialized:</p>
823 {#code_begin|test#}823 {#code_begin|test#}
824const assert = @import("std").debug.assert;824const expect = @import("std").testing.expect;
825825
826test "init with undefined" {826test "init with undefined" {
827 var x: i32 = undefined;827 var x: i32 = undefined;
828 x = 1;828 x = 1;
829 assert(x == 1);829 expect(x == 1);
830}830}
831 {#code_end#}831 {#code_end#}
832 <p>832 <p>
...@@ -868,8 +868,8 @@ var y: i32 = add(10, x);...@@ -868,8 +868,8 @@ var y: i32 = add(10, x);
868const x: i32 = add(12, 34);868const x: i32 = add(12, 34);
869869
870test "global variables" {870test "global variables" {
871 assert(x == 46);871 expect(x == 46);
872 assert(y == 56);872 expect(y == 56);
873}873}
874874
875fn add(a: i32, b: i32) i32 {875fn add(a: i32, b: i32) i32 {
...@@ -877,18 +877,18 @@ fn add(a: i32, b: i32) i32 {...@@ -877,18 +877,18 @@ fn add(a: i32, b: i32) i32 {
877}877}
878878
879const std = @import("std");879const std = @import("std");
880const assert = std.debug.assert;880const expect = std.testing.expect;
881 {#code_end#}881 {#code_end#}
882 <p>882 <p>
883 Global variables may be declared inside a {#link|struct#}, {#link|union#}, or {#link|enum#}:883 Global variables may be declared inside a {#link|struct#}, {#link|union#}, or {#link|enum#}:
884 </p>884 </p>
885 {#code_begin|test|namespaced_global#}885 {#code_begin|test|namespaced_global#}
886const std = @import("std");886const std = @import("std");
887const assert = std.debug.assert;887const expect = std.testing.expect;
888888
889test "namespaced global variable" {889test "namespaced global variable" {
890 assert(foo() == 1235);890 expect(foo() == 1235);
891 assert(foo() == 1236);891 expect(foo() == 1236);
892}892}
893893
894fn foo() i32 {894fn foo() i32 {
...@@ -957,7 +957,7 @@ fn testTls(context: void) void {...@@ -957,7 +957,7 @@ fn testTls(context: void) void {
957 </p>957 </p>
958 {#code_begin|test|comptime_vars#}958 {#code_begin|test|comptime_vars#}
959const std = @import("std");959const std = @import("std");
960const assert = std.debug.assert;960const expect = std.testing.expect;
961961
962test "comptime vars" {962test "comptime vars" {
963 var x: i32 = 1;963 var x: i32 = 1;
...@@ -966,8 +966,8 @@ test "comptime vars" {...@@ -966,8 +966,8 @@ test "comptime vars" {
966 x += 1;966 x += 1;
967 y += 1;967 y += 1;
968968
969 assert(x == 2);969 expect(x == 2);
970 assert(y == 2);970 expect(y == 2);
971971
972 if (y != 2) {972 if (y != 2) {
973 // This compile error never triggers because y is a comptime variable,973 // This compile error never triggers because y is a comptime variable,
...@@ -1757,7 +1757,7 @@ orelse catch...@@ -1757,7 +1757,7 @@ orelse catch
1757 {#header_close#}1757 {#header_close#}
1758 {#header_open|Arrays#}1758 {#header_open|Arrays#}
1759 {#code_begin|test|arrays#}1759 {#code_begin|test|arrays#}
1760const assert = @import("std").debug.assert;1760const expect = @import("std").testing.expect;
1761const mem = @import("std").mem;1761const mem = @import("std").mem;
17621762
1763// array literal1763// array literal
...@@ -1765,14 +1765,14 @@ const message = [_]u8{ 'h', 'e', 'l', 'l', 'o' };...@@ -1765,14 +1765,14 @@ const message = [_]u8{ 'h', 'e', 'l', 'l', 'o' };
17651765
1766// get the size of an array1766// get the size of an array
1767comptime {1767comptime {
1768 assert(message.len == 5);1768 expect(message.len == 5);
1769}1769}
17701770
1771// A string literal is a pointer to an array literal.1771// A string literal is a pointer to an array literal.
1772const same_message = "hello";1772const same_message = "hello";
17731773
1774comptime {1774comptime {
1775 assert(mem.eql(u8, &message, same_message));1775 expect(mem.eql(u8, &message, same_message));
1776}1776}
17771777
1778test "iterate over an array" {1778test "iterate over an array" {
...@@ -1780,7 +1780,7 @@ test "iterate over an array" {...@@ -1780,7 +1780,7 @@ test "iterate over an array" {
1780 for (message) |byte| {1780 for (message) |byte| {
1781 sum += byte;1781 sum += byte;
1782 }1782 }
1783 assert(sum == 'h' + 'e' + 'l' * 2 + 'o');1783 expect(sum == 'h' + 'e' + 'l' * 2 + 'o');
1784}1784}
17851785
1786// modifiable array1786// modifiable array
...@@ -1790,8 +1790,8 @@ test "modify an array" {...@@ -1790,8 +1790,8 @@ test "modify an array" {
1790 for (some_integers) |*item, i| {1790 for (some_integers) |*item, i| {
1791 item.* = @intCast(i32, i);1791 item.* = @intCast(i32, i);
1792 }1792 }
1793 assert(some_integers[10] == 10);1793 expect(some_integers[10] == 10);
1794 assert(some_integers[99] == 99);1794 expect(some_integers[99] == 99);
1795}1795}
17961796
1797// array concatenation works if the values are known1797// array concatenation works if the values are known
...@@ -1800,7 +1800,7 @@ const part_one = [_]i32{ 1, 2, 3, 4 };...@@ -1800,7 +1800,7 @@ const part_one = [_]i32{ 1, 2, 3, 4 };
1800const part_two = [_]i32{ 5, 6, 7, 8 };1800const part_two = [_]i32{ 5, 6, 7, 8 };
1801const all_of_it = part_one ++ part_two;1801const all_of_it = part_one ++ part_two;
1802comptime {1802comptime {
1803 assert(mem.eql(i32, &all_of_it, &[_]i32{ 1, 2, 3, 4, 5, 6, 7, 8 }));1803 expect(mem.eql(i32, &all_of_it, &[_]i32{ 1, 2, 3, 4, 5, 6, 7, 8 }));
1804}1804}
18051805
1806// remember that string literals are arrays1806// remember that string literals are arrays
...@@ -1808,21 +1808,21 @@ const hello = "hello";...@@ -1808,21 +1808,21 @@ const hello = "hello";
1808const world = "world";1808const world = "world";
1809const hello_world = hello ++ " " ++ world;1809const hello_world = hello ++ " " ++ world;
1810comptime {1810comptime {
1811 assert(mem.eql(u8, hello_world, "hello world"));1811 expect(mem.eql(u8, hello_world, "hello world"));
1812}1812}
18131813
1814// ** does repeating patterns1814// ** does repeating patterns
1815const pattern = "ab" ** 3;1815const pattern = "ab" ** 3;
1816comptime {1816comptime {
1817 assert(mem.eql(u8, pattern, "ababab"));1817 expect(mem.eql(u8, pattern, "ababab"));
1818}1818}
18191819
1820// initialize an array to zero1820// initialize an array to zero
1821const all_zero = [_]u16{0} ** 10;1821const all_zero = [_]u16{0} ** 10;
18221822
1823comptime {1823comptime {
1824 assert(all_zero.len == 10);1824 expect(all_zero.len == 10);
1825 assert(all_zero[5] == 0);1825 expect(all_zero[5] == 0);
1826}1826}
18271827
1828// use compile-time code to initialize an array1828// use compile-time code to initialize an array
...@@ -1842,8 +1842,8 @@ const Point = struct {...@@ -1842,8 +1842,8 @@ const Point = struct {
1842};1842};
18431843
1844test "compile-time array initialization" {1844test "compile-time array initialization" {
1845 assert(fancy_array[4].x == 4);1845 expect(fancy_array[4].x == 4);
1846 assert(fancy_array[4].y == 8);1846 expect(fancy_array[4].y == 8);
1847}1847}
18481848
1849// call a function to initialize an array1849// call a function to initialize an array
...@@ -1855,9 +1855,9 @@ fn makePoint(x: i32) Point {...@@ -1855,9 +1855,9 @@ fn makePoint(x: i32) Point {
1855 };1855 };
1856}1856}
1857test "array initialization with function calls" {1857test "array initialization with function calls" {
1858 assert(more_points[4].x == 3);1858 expect(more_points[4].x == 3);
1859 assert(more_points[4].y == 6);1859 expect(more_points[4].y == 6);
1860 assert(more_points.len == 10);1860 expect(more_points.len == 10);
1861}1861}
1862 {#code_end#}1862 {#code_end#}
1863 {#see_also|for|Slices#}1863 {#see_also|for|Slices#}
...@@ -1867,14 +1867,14 @@ test "array initialization with function calls" {...@@ -1867,14 +1867,14 @@ test "array initialization with function calls" {
1867 the type can be omitted from array literals:</p>1867 the type can be omitted from array literals:</p>
1868 {#code_begin|test|anon_list#}1868 {#code_begin|test|anon_list#}
1869const std = @import("std");1869const std = @import("std");
1870const assert = std.debug.assert;1870const expect = std.testing.expect;
18711871
1872test "anonymous list literal syntax" {1872test "anonymous list literal syntax" {
1873 var array: [4]u8 = .{11, 22, 33, 44};1873 var array: [4]u8 = .{11, 22, 33, 44};
1874 assert(array[0] == 11);1874 expect(array[0] == 11);
1875 assert(array[1] == 22);1875 expect(array[1] == 22);
1876 assert(array[2] == 33);1876 expect(array[2] == 33);
1877 assert(array[3] == 44);1877 expect(array[3] == 44);
1878}1878}
1879 {#code_end#}1879 {#code_end#}
1880 <p>1880 <p>
...@@ -1883,18 +1883,18 @@ test "anonymous list literal syntax" {...@@ -1883,18 +1883,18 @@ test "anonymous list literal syntax" {
1883 </p>1883 </p>
1884 {#code_begin|test|infer_list_literal#}1884 {#code_begin|test|infer_list_literal#}
1885const std = @import("std");1885const std = @import("std");
1886const assert = std.debug.assert;1886const expect = std.testing.expect;
18871887
1888test "fully anonymous list literal" {1888test "fully anonymous list literal" {
1889 dump(.{ @as(u32, 1234), @as(f64, 12.34), true, "hi"});1889 dump(.{ @as(u32, 1234), @as(f64, 12.34), true, "hi"});
1890}1890}
18911891
1892fn dump(args: anytype) void {1892fn dump(args: anytype) void {
1893 assert(args.@"0" == 1234);1893 expect(args.@"0" == 1234);
1894 assert(args.@"1" == 12.34);1894 expect(args.@"1" == 12.34);
1895 assert(args.@"2");1895 expect(args.@"2");
1896 assert(args.@"3"[0] == 'h');1896 expect(args.@"3"[0] == 'h');
1897 assert(args.@"3"[1] == 'i');1897 expect(args.@"3"[1] == 'i');
1898}1898}
1899 {#code_end#}1899 {#code_end#}
1900 {#header_close#}1900 {#header_close#}
...@@ -1905,7 +1905,7 @@ fn dump(args: anytype) void {...@@ -1905,7 +1905,7 @@ fn dump(args: anytype) void {
1905 </p>1905 </p>
1906 {#code_begin|test|multidimensional#}1906 {#code_begin|test|multidimensional#}
1907const std = @import("std");1907const std = @import("std");
1908const assert = std.debug.assert;1908const expect = std.testing.expect;
19091909
1910const mat4x4 = [4][4]f32{1910const mat4x4 = [4][4]f32{
1911 [_]f32{ 1.0, 0.0, 0.0, 0.0 },1911 [_]f32{ 1.0, 0.0, 0.0, 0.0 },
...@@ -1915,13 +1915,13 @@ const mat4x4 = [4][4]f32{...@@ -1915,13 +1915,13 @@ const mat4x4 = [4][4]f32{
1915};1915};
1916test "multidimensional arrays" {1916test "multidimensional arrays" {
1917 // Access the 2D array by indexing the outer array, and then the inner array.1917 // Access the 2D array by indexing the outer array, and then the inner array.
1918 assert(mat4x4[1][1] == 1.0);1918 expect(mat4x4[1][1] == 1.0);
19191919
1920 // Here we iterate with for loops.1920 // Here we iterate with for loops.
1921 for (mat4x4) |row, row_index| {1921 for (mat4x4) |row, row_index| {
1922 for (row) |cell, column_index| {1922 for (row) |cell, column_index| {
1923 if (row_index == column_index) {1923 if (row_index == column_index) {
1924 assert(cell == 1.0);1924 expect(cell == 1.0);
1925 }1925 }
1926 }1926 }
1927 }1927 }
...@@ -1936,14 +1936,14 @@ test "multidimensional arrays" {...@@ -1936,14 +1936,14 @@ test "multidimensional arrays" {
1936 </p>1936 </p>
1937 {#code_begin|test|null_terminated_array#}1937 {#code_begin|test|null_terminated_array#}
1938const std = @import("std");1938const std = @import("std");
1939const assert = std.debug.assert;1939const expect = std.testing.expect;
19401940
1941test "null terminated array" {1941test "null terminated array" {
1942 const array = [_:0]u8 {1, 2, 3, 4};1942 const array = [_:0]u8 {1, 2, 3, 4};
19431943
1944 assert(@TypeOf(array) == [4:0]u8);1944 expect(@TypeOf(array) == [4:0]u8);
1945 assert(array.len == 4);1945 expect(array.len == 4);
1946 assert(array[4] == 0);1946 expect(array[4] == 0);
1947}1947}
1948 {#code_end#}1948 {#code_end#}
1949 {#see_also|Sentinel-Terminated Pointers|Sentinel-Terminated Slices#}1949 {#see_also|Sentinel-Terminated Pointers|Sentinel-Terminated Slices#}
...@@ -2013,7 +2013,7 @@ test "null terminated array" {...@@ -2013,7 +2013,7 @@ test "null terminated array" {
2013 </ul>2013 </ul>
2014 <p>Use {#syntax#}&x{#endsyntax#} to obtain a single-item pointer:</p>2014 <p>Use {#syntax#}&x{#endsyntax#} to obtain a single-item pointer:</p>
2015 {#code_begin|test#}2015 {#code_begin|test#}
2016const assert = @import("std").debug.assert;2016const expect = @import("std").testing.expect;
20172017
2018test "address of syntax" {2018test "address of syntax" {
2019 // Get the address of a variable:2019 // Get the address of a variable:
...@@ -2021,17 +2021,17 @@ test "address of syntax" {...@@ -2021,17 +2021,17 @@ test "address of syntax" {
2021 const x_ptr = &x;2021 const x_ptr = &x;
20222022
2023 // Dereference a pointer:2023 // Dereference a pointer:
2024 assert(x_ptr.* == 1234);2024 expect(x_ptr.* == 1234);
20252025
2026 // When you get the address of a const variable, you get a const pointer to a single item.2026 // When you get the address of a const variable, you get a const pointer to a single item.
2027 assert(@TypeOf(x_ptr) == *const i32);2027 expect(@TypeOf(x_ptr) == *const i32);
20282028
2029 // If you want to mutate the value, you'd need an address of a mutable variable:2029 // If you want to mutate the value, you'd need an address of a mutable variable:
2030 var y: i32 = 5678;2030 var y: i32 = 5678;
2031 const y_ptr = &y;2031 const y_ptr = &y;
2032 assert(@TypeOf(y_ptr) == *i32);2032 expect(@TypeOf(y_ptr) == *i32);
2033 y_ptr.* += 1;2033 y_ptr.* += 1;
2034 assert(y_ptr.* == 5679);2034 expect(y_ptr.* == 5679);
2035}2035}
20362036
2037test "pointer array access" {2037test "pointer array access" {
...@@ -2040,11 +2040,11 @@ test "pointer array access" {...@@ -2040,11 +2040,11 @@ test "pointer array access" {
2040 // does not support pointer arithmetic.2040 // does not support pointer arithmetic.
2041 var array = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };2041 var array = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
2042 const ptr = &array[2];2042 const ptr = &array[2];
2043 assert(@TypeOf(ptr) == *u8);2043 expect(@TypeOf(ptr) == *u8);
20442044
2045 assert(array[2] == 3);2045 expect(array[2] == 3);
2046 ptr.* += 1;2046 ptr.* += 1;
2047 assert(array[2] == 4);2047 expect(array[2] == 4);
2048}2048}
2049 {#code_end#}2049 {#code_end#}
2050 <p>2050 <p>
...@@ -2057,22 +2057,22 @@ test "pointer array access" {...@@ -2057,22 +2057,22 @@ test "pointer array access" {
2057 we prefer slices to pointers.2057 we prefer slices to pointers.
2058 </p>2058 </p>
2059 {#code_begin|test#}2059 {#code_begin|test#}
2060const assert = @import("std").debug.assert;2060const expect = @import("std").testing.expect;
20612061
2062test "pointer slicing" {2062test "pointer slicing" {
2063 var array = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };2063 var array = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
2064 const slice = array[2..4];2064 const slice = array[2..4];
2065 assert(slice.len == 2);2065 expect(slice.len == 2);
20662066
2067 assert(array[3] == 4);2067 expect(array[3] == 4);
2068 slice[1] += 1;2068 slice[1] += 1;
2069 assert(array[3] == 5);2069 expect(array[3] == 5);
2070}2070}
2071 {#code_end#}2071 {#code_end#}
2072 <p>Pointers work at compile-time too, as long as the code does not depend on2072 <p>Pointers work at compile-time too, as long as the code does not depend on
2073 an undefined memory layout:</p>2073 an undefined memory layout:</p>
2074 {#code_begin|test#}2074 {#code_begin|test#}
2075const assert = @import("std").debug.assert;2075const expect = @import("std").testing.expect;
20762076
2077test "comptime pointers" {2077test "comptime pointers" {
2078 comptime {2078 comptime {
...@@ -2080,26 +2080,26 @@ test "comptime pointers" {...@@ -2080,26 +2080,26 @@ test "comptime pointers" {
2080 const ptr = &x;2080 const ptr = &x;
2081 ptr.* += 1;2081 ptr.* += 1;
2082 x += 1;2082 x += 1;
2083 assert(ptr.* == 3);2083 expect(ptr.* == 3);
2084 }2084 }
2085}2085}
2086 {#code_end#}2086 {#code_end#}
2087 <p>To convert an integer address into a pointer, use {#syntax#}@intToPtr{#endsyntax#}.2087 <p>To convert an integer address into a pointer, use {#syntax#}@intToPtr{#endsyntax#}.
2088 To convert a pointer to an integer, use {#syntax#}@ptrToInt{#endsyntax#}:</p>2088 To convert a pointer to an integer, use {#syntax#}@ptrToInt{#endsyntax#}:</p>
2089 {#code_begin|test#}2089 {#code_begin|test#}
2090const assert = @import("std").debug.assert;2090const expect = @import("std").testing.expect;
20912091
2092test "@ptrToInt and @intToPtr" {2092test "@ptrToInt and @intToPtr" {
2093 const ptr = @intToPtr(*i32, 0xdeadbee0);2093 const ptr = @intToPtr(*i32, 0xdeadbee0);
2094 const addr = @ptrToInt(ptr);2094 const addr = @ptrToInt(ptr);
2095 assert(@TypeOf(addr) == usize);2095 expect(@TypeOf(addr) == usize);
2096 assert(addr == 0xdeadbee0);2096 expect(addr == 0xdeadbee0);
2097}2097}
2098 {#code_end#}2098 {#code_end#}
2099 <p>Zig is able to preserve memory addresses in comptime code, as long as2099 <p>Zig is able to preserve memory addresses in comptime code, as long as
2100 the pointer is never dereferenced:</p>2100 the pointer is never dereferenced:</p>
2101 {#code_begin|test#}2101 {#code_begin|test#}
2102const assert = @import("std").debug.assert;2102const expect = @import("std").testing.expect;
21032103
2104test "comptime @intToPtr" {2104test "comptime @intToPtr" {
2105 comptime {2105 comptime {
...@@ -2107,8 +2107,8 @@ test "comptime @intToPtr" {...@@ -2107,8 +2107,8 @@ test "comptime @intToPtr" {
2107 // ptr is never dereferenced.2107 // ptr is never dereferenced.
2108 const ptr = @intToPtr(*i32, 0xdeadbee0);2108 const ptr = @intToPtr(*i32, 0xdeadbee0);
2109 const addr = @ptrToInt(ptr);2109 const addr = @ptrToInt(ptr);
2110 assert(@TypeOf(addr) == usize);2110 expect(@TypeOf(addr) == usize);
2111 assert(addr == 0xdeadbee0);2111 expect(addr == 0xdeadbee0);
2112 }2112 }
2113}2113}
2114 {#code_end#}2114 {#code_end#}
...@@ -2119,11 +2119,11 @@ test "comptime @intToPtr" {...@@ -2119,11 +2119,11 @@ test "comptime @intToPtr" {
2119 In the following code, loads and stores with {#syntax#}mmio_ptr{#endsyntax#} are guaranteed to all happen2119 In the following code, loads and stores with {#syntax#}mmio_ptr{#endsyntax#} are guaranteed to all happen
2120 and in the same order as in source code:</p>2120 and in the same order as in source code:</p>
2121 {#code_begin|test#}2121 {#code_begin|test#}
2122const assert = @import("std").debug.assert;2122const expect = @import("std").testing.expect;
21232123
2124test "volatile" {2124test "volatile" {
2125 const mmio_ptr = @intToPtr(*volatile u8, 0x12345678);2125 const mmio_ptr = @intToPtr(*volatile u8, 0x12345678);
2126 assert(@TypeOf(mmio_ptr) == *volatile u8);2126 expect(@TypeOf(mmio_ptr) == *volatile u8);
2127}2127}
2128 {#code_end#}2128 {#code_end#}
2129 <p>2129 <p>
...@@ -2139,25 +2139,25 @@ test "volatile" {...@@ -2139,25 +2139,25 @@ test "volatile" {
2139 </p>2139 </p>
2140 {#code_begin|test#}2140 {#code_begin|test#}
2141const std = @import("std");2141const std = @import("std");
2142const assert = std.debug.assert;2142const expect = std.testing.expect;
21432143
2144test "pointer casting" {2144test "pointer casting" {
2145 const bytes align(@alignOf(u32)) = [_]u8{ 0x12, 0x12, 0x12, 0x12 };2145 const bytes align(@alignOf(u32)) = [_]u8{ 0x12, 0x12, 0x12, 0x12 };
2146 const u32_ptr = @ptrCast(*const u32, &bytes);2146 const u32_ptr = @ptrCast(*const u32, &bytes);
2147 assert(u32_ptr.* == 0x12121212);2147 expect(u32_ptr.* == 0x12121212);
21482148
2149 // Even this example is contrived - there are better ways to do the above than2149 // Even this example is contrived - there are better ways to do the above than
2150 // pointer casting. For example, using a slice narrowing cast:2150 // pointer casting. For example, using a slice narrowing cast:
2151 const u32_value = std.mem.bytesAsSlice(u32, bytes[0..])[0];2151 const u32_value = std.mem.bytesAsSlice(u32, bytes[0..])[0];
2152 assert(u32_value == 0x12121212);2152 expect(u32_value == 0x12121212);
21532153
2154 // And even another way, the most straightforward way to do it:2154 // And even another way, the most straightforward way to do it:
2155 assert(@bitCast(u32, bytes) == 0x12121212);2155 expect(@bitCast(u32, bytes) == 0x12121212);
2156}2156}
21572157
2158test "pointer child type" {2158test "pointer child type" {
2159 // pointer types have a `child` field which tells you the type they point to.2159 // pointer types have a `child` field which tells you the type they point to.
2160 assert(@typeInfo(*u32).Pointer.child == u32);2160 expect(@typeInfo(*u32).Pointer.child == u32);
2161}2161}
2162 {#code_end#}2162 {#code_end#}
2163 {#header_open|Alignment#}2163 {#header_open|Alignment#}
...@@ -2177,15 +2177,15 @@ test "pointer child type" {...@@ -2177,15 +2177,15 @@ test "pointer child type" {
2177 </p>2177 </p>
2178 {#code_begin|test#}2178 {#code_begin|test#}
2179const std = @import("std");2179const std = @import("std");
2180const assert = std.debug.assert;2180const expect = std.testing.expect;
21812181
2182test "variable alignment" {2182test "variable alignment" {
2183 var x: i32 = 1234;2183 var x: i32 = 1234;
2184 const align_of_i32 = @alignOf(@TypeOf(x));2184 const align_of_i32 = @alignOf(@TypeOf(x));
2185 assert(@TypeOf(&x) == *i32);2185 expect(@TypeOf(&x) == *i32);
2186 assert(*i32 == *align(align_of_i32) i32);2186 expect(*i32 == *align(align_of_i32) i32);
2187 if (std.Target.current.cpu.arch == .x86_64) {2187 if (std.Target.current.cpu.arch == .x86_64) {
2188 assert(@typeInfo(*i32).Pointer.alignment == 4);2188 expect(@typeInfo(*i32).Pointer.alignment == 4);
2189 }2189 }
2190}2190}
2191 {#code_end#}2191 {#code_end#}
...@@ -2198,16 +2198,16 @@ test "variable alignment" {...@@ -2198,16 +2198,16 @@ test "variable alignment" {
2198 pointers to them get the specified alignment:2198 pointers to them get the specified alignment:
2199 </p>2199 </p>
2200 {#code_begin|test#}2200 {#code_begin|test#}
2201const assert = @import("std").debug.assert;2201const expect = @import("std").testing.expect;
22022202
2203var foo: u8 align(4) = 100;2203var foo: u8 align(4) = 100;
22042204
2205test "global variable alignment" {2205test "global variable alignment" {
2206 assert(@typeInfo(@TypeOf(&foo)).Pointer.alignment == 4);2206 expect(@typeInfo(@TypeOf(&foo)).Pointer.alignment == 4);
2207 assert(@TypeOf(&foo) == *align(4) u8);2207 expect(@TypeOf(&foo) == *align(4) u8);
2208 const as_pointer_to_array: *[1]u8 = &foo;2208 const as_pointer_to_array: *[1]u8 = &foo;
2209 const as_slice: []u8 = as_pointer_to_array;2209 const as_slice: []u8 = as_pointer_to_array;
2210 assert(@TypeOf(as_slice) == []align(4) u8);2210 expect(@TypeOf(as_slice) == []align(4) u8);
2211}2211}
22122212
2213fn derp() align(@sizeOf(usize) * 2) i32 { return 1234; }2213fn derp() align(@sizeOf(usize) * 2) i32 { return 1234; }
...@@ -2215,9 +2215,9 @@ fn noop1() align(1) void {}...@@ -2215,9 +2215,9 @@ fn noop1() align(1) void {}
2215fn noop4() align(4) void {}2215fn noop4() align(4) void {}
22162216
2217test "function alignment" {2217test "function alignment" {
2218 assert(derp() == 1234);2218 expect(derp() == 1234);
2219 assert(@TypeOf(noop1) == fn() align(1) void);2219 expect(@TypeOf(noop1) == fn() align(1) void);
2220 assert(@TypeOf(noop4) == fn() align(4) void);2220 expect(@TypeOf(noop4) == fn() align(4) void);
2221 noop1();2221 noop1();
2222 noop4();2222 noop4();
2223}2223}
...@@ -2234,7 +2234,7 @@ const std = @import("std");...@@ -2234,7 +2234,7 @@ const std = @import("std");
2234test "pointer alignment safety" {2234test "pointer alignment safety" {
2235 var array align(4) = [_]u32{ 0x11111111, 0x11111111 };2235 var array align(4) = [_]u32{ 0x11111111, 0x11111111 };
2236 const bytes = std.mem.sliceAsBytes(array[0..]);2236 const bytes = std.mem.sliceAsBytes(array[0..]);
2237 std.debug.assert(foo(bytes) == 0x11111111);2237 std.testing.expect(foo(bytes) == 0x11111111);
2238}2238}
2239fn foo(bytes: []u8) u32 {2239fn foo(bytes: []u8) u32 {
2240 const slice4 = bytes[1..5];2240 const slice4 = bytes[1..5];
...@@ -2255,12 +2255,12 @@ fn foo(bytes: []u8) u32 {...@@ -2255,12 +2255,12 @@ fn foo(bytes: []u8) u32 {
2255 </p>2255 </p>
2256 {#code_begin|test|allowzero#}2256 {#code_begin|test|allowzero#}
2257const std = @import("std");2257const std = @import("std");
2258const assert = std.debug.assert;2258const expect = std.testing.expect;
22592259
2260test "allowzero" {2260test "allowzero" {
2261 var zero: usize = 0;2261 var zero: usize = 0;
2262 var ptr = @intToPtr(*allowzero i32, zero);2262 var ptr = @intToPtr(*allowzero i32, zero);
2263 assert(@ptrToInt(ptr) == 0);2263 expect(@ptrToInt(ptr) == 0);
2264}2264}
2265 {#code_end#}2265 {#code_end#}
2266 {#header_close#}2266 {#header_close#}
...@@ -2292,7 +2292,7 @@ pub fn main() anyerror!void {...@@ -2292,7 +2292,7 @@ pub fn main() anyerror!void {
22922292
2293 {#header_open|Slices#}2293 {#header_open|Slices#}
2294 {#code_begin|test_safety|index out of bounds#}2294 {#code_begin|test_safety|index out of bounds#}
2295const assert = @import("std").debug.assert;2295const expect = @import("std").testing.expect;
22962296
2297test "basic slices" {2297test "basic slices" {
2298 var array = [_]i32{ 1, 2, 3, 4 };2298 var array = [_]i32{ 1, 2, 3, 4 };
...@@ -2302,14 +2302,14 @@ test "basic slices" {...@@ -2302,14 +2302,14 @@ test "basic slices" {
2302 // Both can be accessed with the `len` field.2302 // Both can be accessed with the `len` field.
2303 var known_at_runtime_zero: usize = 0;2303 var known_at_runtime_zero: usize = 0;
2304 const slice = array[known_at_runtime_zero..array.len];2304 const slice = array[known_at_runtime_zero..array.len];
2305 assert(&slice[0] == &array[0]);2305 expect(&slice[0] == &array[0]);
2306 assert(slice.len == array.len);2306 expect(slice.len == array.len);
23072307
2308 // Using the address-of operator on a slice gives a pointer to a single2308 // Using the address-of operator on a slice gives a pointer to a single
2309 // item, while using the `ptr` field gives an unknown length pointer.2309 // item, while using the `ptr` field gives an unknown length pointer.
2310 assert(@TypeOf(slice.ptr) == [*]i32);2310 expect(@TypeOf(slice.ptr) == [*]i32);
2311 assert(@TypeOf(&slice[0]) == *i32);2311 expect(@TypeOf(&slice[0]) == *i32);
2312 assert(@ptrToInt(slice.ptr) == @ptrToInt(&slice[0]));2312 expect(@ptrToInt(slice.ptr) == @ptrToInt(&slice[0]));
23132313
2314 // Slices have array bounds checking. If you try to access something out2314 // Slices have array bounds checking. If you try to access something out
2315 // of bounds, you'll get a safety check failure:2315 // of bounds, you'll get a safety check failure:
...@@ -2322,7 +2322,7 @@ test "basic slices" {...@@ -2322,7 +2322,7 @@ test "basic slices" {
2322 <p>This is one reason we prefer slices to pointers.</p>2322 <p>This is one reason we prefer slices to pointers.</p>
2323 {#code_begin|test|slices#}2323 {#code_begin|test|slices#}
2324const std = @import("std");2324const std = @import("std");
2325const assert = std.debug.assert;2325const expect = std.testing.expect;
2326const mem = std.mem;2326const mem = std.mem;
2327const fmt = std.fmt;2327const fmt = std.fmt;
23282328
...@@ -2343,7 +2343,7 @@ test "using slices for strings" {...@@ -2343,7 +2343,7 @@ test "using slices for strings" {
2343 // Generally, you can use UTF-8 and not worry about whether something is a2343 // Generally, you can use UTF-8 and not worry about whether something is a
2344 // string. If you don't need to deal with individual characters, no need2344 // string. If you don't need to deal with individual characters, no need
2345 // to decode.2345 // to decode.
2346 assert(mem.eql(u8, hello_world, "hello 世界"));2346 expect(mem.eql(u8, hello_world, "hello 世界"));
2347}2347}
23482348
2349test "slice pointer" {2349test "slice pointer" {
...@@ -2353,16 +2353,16 @@ test "slice pointer" {...@@ -2353,16 +2353,16 @@ test "slice pointer" {
2353 // You can use slicing syntax to convert a pointer into a slice:2353 // You can use slicing syntax to convert a pointer into a slice:
2354 const slice = ptr[0..5];2354 const slice = ptr[0..5];
2355 slice[2] = 3;2355 slice[2] = 3;
2356 assert(slice[2] == 3);2356 expect(slice[2] == 3);
2357 // The slice is mutable because we sliced a mutable pointer.2357 // The slice is mutable because we sliced a mutable pointer.
2358 // Furthermore, it is actually a pointer to an array, since the start2358 // Furthermore, it is actually a pointer to an array, since the start
2359 // and end indexes were both comptime-known.2359 // and end indexes were both comptime-known.
2360 assert(@TypeOf(slice) == *[5]u8);2360 expect(@TypeOf(slice) == *[5]u8);
23612361
2362 // You can also slice a slice:2362 // You can also slice a slice:
2363 const slice2 = slice[2..3];2363 const slice2 = slice[2..3];
2364 assert(slice2.len == 1);2364 expect(slice2.len == 1);
2365 assert(slice2[0] == 3);2365 expect(slice2[0] == 3);
2366}2366}
2367 {#code_end#}2367 {#code_end#}
2368 {#see_also|Pointers|for|Arrays#}2368 {#see_also|Pointers|for|Arrays#}
...@@ -2376,13 +2376,13 @@ test "slice pointer" {...@@ -2376,13 +2376,13 @@ test "slice pointer" {
2376 </p>2376 </p>
2377 {#code_begin|test|null_terminated_slice#}2377 {#code_begin|test|null_terminated_slice#}
2378const std = @import("std");2378const std = @import("std");
2379const assert = std.debug.assert;2379const expect = std.testing.expect;
23802380
2381test "null terminated slice" {2381test "null terminated slice" {
2382 const slice: [:0]const u8 = "hello";2382 const slice: [:0]const u8 = "hello";
23832383
2384 assert(slice.len == 5);2384 expect(slice.len == 5);
2385 assert(slice[5] == 0);2385 expect(slice[5] == 0);
2386}2386}
2387 {#code_end#}2387 {#code_end#}
2388 {#see_also|Sentinel-Terminated Pointers|Sentinel-Terminated Arrays#}2388 {#see_also|Sentinel-Terminated Pointers|Sentinel-Terminated Arrays#}
...@@ -2440,16 +2440,16 @@ const Vec3 = struct {...@@ -2440,16 +2440,16 @@ const Vec3 = struct {
2440 }2440 }
2441};2441};
24422442
2443const assert = @import("std").debug.assert;2443const expect = @import("std").testing.expect;
2444test "dot product" {2444test "dot product" {
2445 const v1 = Vec3.init(1.0, 0.0, 0.0);2445 const v1 = Vec3.init(1.0, 0.0, 0.0);
2446 const v2 = Vec3.init(0.0, 1.0, 0.0);2446 const v2 = Vec3.init(0.0, 1.0, 0.0);
2447 assert(v1.dot(v2) == 0.0);2447 expect(v1.dot(v2) == 0.0);
24482448
2449 // Other than being available to call with dot syntax, struct methods are2449 // Other than being available to call with dot syntax, struct methods are
2450 // not special. You can reference them as any other declaration inside2450 // not special. You can reference them as any other declaration inside
2451 // the struct:2451 // the struct:
2452 assert(Vec3.dot(v1, v2) == 0.0);2452 expect(Vec3.dot(v1, v2) == 0.0);
2453}2453}
24542454
2455// Structs can have global declarations.2455// Structs can have global declarations.
...@@ -2458,8 +2458,8 @@ const Empty = struct {...@@ -2458,8 +2458,8 @@ const Empty = struct {
2458 pub const PI = 3.14;2458 pub const PI = 3.14;
2459};2459};
2460test "struct namespaced variable" {2460test "struct namespaced variable" {
2461 assert(Empty.PI == 3.14);2461 expect(Empty.PI == 3.14);
2462 assert(@sizeOf(Empty) == 0);2462 expect(@sizeOf(Empty) == 0);
24632463
2464 // you can still instantiate an empty struct2464 // you can still instantiate an empty struct
2465 const does_nothing = Empty {};2465 const does_nothing = Empty {};
...@@ -2477,7 +2477,7 @@ test "field parent pointer" {...@@ -2477,7 +2477,7 @@ test "field parent pointer" {
2477 .y = 0.5678,2477 .y = 0.5678,
2478 };2478 };
2479 setYBasedOnX(&point.x, 0.9);2479 setYBasedOnX(&point.x, 0.9);
2480 assert(point.y == 0.9);2480 expect(point.y == 0.9);
2481}2481}
24822482
2483// You can return a struct from a function. This is how we do generics2483// You can return a struct from a function. This is how we do generics
...@@ -2499,19 +2499,19 @@ fn LinkedList(comptime T: type) type {...@@ -2499,19 +2499,19 @@ fn LinkedList(comptime T: type) type {
2499test "linked list" {2499test "linked list" {
2500 // Functions called at compile-time are memoized. This means you can2500 // Functions called at compile-time are memoized. This means you can
2501 // do this:2501 // do this:
2502 assert(LinkedList(i32) == LinkedList(i32));2502 expect(LinkedList(i32) == LinkedList(i32));
25032503
2504 var list = LinkedList(i32) {2504 var list = LinkedList(i32) {
2505 .first = null,2505 .first = null,
2506 .last = null,2506 .last = null,
2507 .len = 0,2507 .len = 0,
2508 };2508 };
2509 assert(list.len == 0);2509 expect(list.len == 0);
25102510
2511 // Since types are first class values you can instantiate the type2511 // Since types are first class values you can instantiate the type
2512 // by assigning it to a variable:2512 // by assigning it to a variable:
2513 const ListOfInts = LinkedList(i32);2513 const ListOfInts = LinkedList(i32);
2514 assert(ListOfInts == LinkedList(i32));2514 expect(ListOfInts == LinkedList(i32));
25152515
2516 var node = ListOfInts.Node {2516 var node = ListOfInts.Node {
2517 .prev = null,2517 .prev = null,
...@@ -2523,7 +2523,7 @@ test "linked list" {...@@ -2523,7 +2523,7 @@ test "linked list" {
2523 .last = &node,2523 .last = &node,
2524 .len = 1,2524 .len = 1,
2525 };2525 };
2526 assert(list2.first.?.data == 1234);2526 expect(list2.first.?.data == 1234);
2527}2527}
2528 {#code_end#}2528 {#code_end#}
25292529
...@@ -2584,7 +2584,7 @@ test "default struct initialization fields" {...@@ -2584,7 +2584,7 @@ test "default struct initialization fields" {
2584 {#code_begin|test#}2584 {#code_begin|test#}
2585const std = @import("std");2585const std = @import("std");
2586const builtin = std.builtin;2586const builtin = std.builtin;
2587const assert = std.debug.assert;2587const expect = std.testing.expect;
25882588
2589const Full = packed struct {2589const Full = packed struct {
2590 number: u16,2590 number: u16,
...@@ -2601,20 +2601,20 @@ test "@bitCast between packed structs" {...@@ -2601,20 +2601,20 @@ test "@bitCast between packed structs" {
2601}2601}
26022602
2603fn doTheTest() void {2603fn doTheTest() void {
2604 assert(@sizeOf(Full) == 2);2604 expect(@sizeOf(Full) == 2);
2605 assert(@sizeOf(Divided) == 2);2605 expect(@sizeOf(Divided) == 2);
2606 var full = Full{ .number = 0x1234 };2606 var full = Full{ .number = 0x1234 };
2607 var divided = @bitCast(Divided, full);2607 var divided = @bitCast(Divided, full);
2608 switch (builtin.endian) {2608 switch (builtin.endian) {
2609 .Big => {2609 .Big => {
2610 assert(divided.half1 == 0x12);2610 expect(divided.half1 == 0x12);
2611 assert(divided.quarter3 == 0x3);2611 expect(divided.quarter3 == 0x3);
2612 assert(divided.quarter4 == 0x4);2612 expect(divided.quarter4 == 0x4);
2613 },2613 },
2614 .Little => {2614 .Little => {
2615 assert(divided.half1 == 0x34);2615 expect(divided.half1 == 0x34);
2616 assert(divided.quarter3 == 0x2);2616 expect(divided.quarter3 == 0x2);
2617 assert(divided.quarter4 == 0x1);2617 expect(divided.quarter4 == 0x1);
2618 },2618 },
2619 }2619 }
2620}2620}
...@@ -2624,7 +2624,7 @@ fn doTheTest() void {...@@ -2624,7 +2624,7 @@ fn doTheTest() void {
2624 </p>2624 </p>
2625 {#code_begin|test#}2625 {#code_begin|test#}
2626const std = @import("std");2626const std = @import("std");
2627const assert = std.debug.assert;2627const expect = std.testing.expect;
26282628
2629const BitField = packed struct {2629const BitField = packed struct {
2630 a: u3,2630 a: u3,
...@@ -2640,7 +2640,7 @@ var foo = BitField{...@@ -2640,7 +2640,7 @@ var foo = BitField{
26402640
2641test "pointer to non-byte-aligned field" {2641test "pointer to non-byte-aligned field" {
2642 const ptr = &foo.b;2642 const ptr = &foo.b;
2643 assert(ptr.* == 2);2643 expect(ptr.* == 2);
2644}2644}
2645 {#code_end#}2645 {#code_end#}
2646 <p>2646 <p>
...@@ -2649,7 +2649,7 @@ test "pointer to non-byte-aligned field" {...@@ -2649,7 +2649,7 @@ test "pointer to non-byte-aligned field" {
2649 </p>2649 </p>
2650 {#code_begin|test_err|expected type#}2650 {#code_begin|test_err|expected type#}
2651const std = @import("std");2651const std = @import("std");
2652const assert = std.debug.assert;2652const expect = std.testing.expect;
26532653
2654const BitField = packed struct {2654const BitField = packed struct {
2655 a: u3,2655 a: u3,
...@@ -2664,7 +2664,7 @@ var bit_field = BitField{...@@ -2664,7 +2664,7 @@ var bit_field = BitField{
2664};2664};
26652665
2666test "pointer to non-bit-aligned field" {2666test "pointer to non-bit-aligned field" {
2667 assert(bar(&bit_field.b) == 2);2667 expect(bar(&bit_field.b) == 2);
2668}2668}
26692669
2670fn bar(x: *const u3) u3 {2670fn bar(x: *const u3) u3 {
...@@ -2680,7 +2680,7 @@ fn bar(x: *const u3) u3 {...@@ -2680,7 +2680,7 @@ fn bar(x: *const u3) u3 {
2680 </p>2680 </p>
2681 {#code_begin|test#}2681 {#code_begin|test#}
2682const std = @import("std");2682const std = @import("std");
2683const assert = std.debug.assert;2683const expect = std.testing.expect;
26842684
2685const BitField = packed struct {2685const BitField = packed struct {
2686 a: u3,2686 a: u3,
...@@ -2695,8 +2695,8 @@ var bit_field = BitField{...@@ -2695,8 +2695,8 @@ var bit_field = BitField{
2695};2695};
26962696
2697test "pointer to non-bit-aligned field" {2697test "pointer to non-bit-aligned field" {
2698 assert(@ptrToInt(&bit_field.a) == @ptrToInt(&bit_field.b));2698 expect(@ptrToInt(&bit_field.a) == @ptrToInt(&bit_field.b));
2699 assert(@ptrToInt(&bit_field.a) == @ptrToInt(&bit_field.c));2699 expect(@ptrToInt(&bit_field.a) == @ptrToInt(&bit_field.c));
2700}2700}
2701 {#code_end#}2701 {#code_end#}
2702 <p>2702 <p>
...@@ -2704,7 +2704,7 @@ test "pointer to non-bit-aligned field" {...@@ -2704,7 +2704,7 @@ test "pointer to non-bit-aligned field" {
2704 </p>2704 </p>
2705 {#code_begin|test#}2705 {#code_begin|test#}
2706const std = @import("std");2706const std = @import("std");
2707const assert = std.debug.assert;2707const expect = std.testing.expect;
27082708
2709const BitField = packed struct {2709const BitField = packed struct {
2710 a: u3,2710 a: u3,
...@@ -2714,13 +2714,13 @@ const BitField = packed struct {...@@ -2714,13 +2714,13 @@ const BitField = packed struct {
27142714
2715test "pointer to non-bit-aligned field" {2715test "pointer to non-bit-aligned field" {
2716 comptime {2716 comptime {
2717 assert(@bitOffsetOf(BitField, "a") == 0);2717 expect(@bitOffsetOf(BitField, "a") == 0);
2718 assert(@bitOffsetOf(BitField, "b") == 3);2718 expect(@bitOffsetOf(BitField, "b") == 3);
2719 assert(@bitOffsetOf(BitField, "c") == 6);2719 expect(@bitOffsetOf(BitField, "c") == 6);
27202720
2721 assert(@byteOffsetOf(BitField, "a") == 0);2721 expect(@byteOffsetOf(BitField, "a") == 0);
2722 assert(@byteOffsetOf(BitField, "b") == 0);2722 expect(@byteOffsetOf(BitField, "b") == 0);
2723 assert(@byteOffsetOf(BitField, "c") == 0);2723 expect(@byteOffsetOf(BitField, "c") == 0);
2724 }2724 }
2725}2725}
2726 {#code_end#}2726 {#code_end#}
...@@ -2791,7 +2791,7 @@ fn List(comptime T: type) type {...@@ -2791,7 +2791,7 @@ fn List(comptime T: type) type {
2791 </p>2791 </p>
2792 {#code_begin|test|struct_result#}2792 {#code_begin|test|struct_result#}
2793const std = @import("std");2793const std = @import("std");
2794const assert = std.debug.assert;2794const expect = std.testing.expect;
27952795
2796const Point = struct {x: i32, y: i32};2796const Point = struct {x: i32, y: i32};
27972797
...@@ -2800,8 +2800,8 @@ test "anonymous struct literal" {...@@ -2800,8 +2800,8 @@ test "anonymous struct literal" {
2800 .x = 13,2800 .x = 13,
2801 .y = 67,2801 .y = 67,
2802 };2802 };
2803 assert(pt.x == 13);2803 expect(pt.x == 13);
2804 assert(pt.y == 67);2804 expect(pt.y == 67);
2805}2805}
2806 {#code_end#}2806 {#code_end#}
2807 <p>2807 <p>
...@@ -2810,7 +2810,7 @@ test "anonymous struct literal" {...@@ -2810,7 +2810,7 @@ test "anonymous struct literal" {
2810 </p>2810 </p>
2811 {#code_begin|test|struct_anon#}2811 {#code_begin|test|struct_anon#}
2812const std = @import("std");2812const std = @import("std");
2813const assert = std.debug.assert;2813const expect = std.testing.expect;
28142814
2815test "fully anonymous struct" {2815test "fully anonymous struct" {
2816 dump(.{2816 dump(.{
...@@ -2822,11 +2822,11 @@ test "fully anonymous struct" {...@@ -2822,11 +2822,11 @@ test "fully anonymous struct" {
2822}2822}
28232823
2824fn dump(args: anytype) void {2824fn dump(args: anytype) void {
2825 assert(args.int == 1234);2825 expect(args.int == 1234);
2826 assert(args.float == 12.34);2826 expect(args.float == 12.34);
2827 assert(args.b);2827 expect(args.b);
2828 assert(args.s[0] == 'h');2828 expect(args.s[0] == 'h');
2829 assert(args.s[1] == 'i');2829 expect(args.s[1] == 'i');
2830}2830}
2831 {#code_end#}2831 {#code_end#}
2832 {#header_close#}2832 {#header_close#}
...@@ -2834,7 +2834,7 @@ fn dump(args: anytype) void {...@@ -2834,7 +2834,7 @@ fn dump(args: anytype) void {
2834 {#header_close#}2834 {#header_close#}
2835 {#header_open|enum#}2835 {#header_open|enum#}
2836 {#code_begin|test|enums#}2836 {#code_begin|test|enums#}
2837const assert = @import("std").debug.assert;2837const expect = @import("std").testing.expect;
2838const mem = @import("std").mem;2838const mem = @import("std").mem;
28392839
2840// Declare an enum.2840// Declare an enum.
...@@ -2857,9 +2857,9 @@ const Value = enum(u2) {...@@ -2857,9 +2857,9 @@ const Value = enum(u2) {
2857// Now you can cast between u2 and Value.2857// Now you can cast between u2 and Value.
2858// The ordinal value starts from 0, counting up for each member.2858// The ordinal value starts from 0, counting up for each member.
2859test "enum ordinal value" {2859test "enum ordinal value" {
2860 assert(@enumToInt(Value.zero) == 0);2860 expect(@enumToInt(Value.zero) == 0);
2861 assert(@enumToInt(Value.one) == 1);2861 expect(@enumToInt(Value.one) == 1);
2862 assert(@enumToInt(Value.two) == 2);2862 expect(@enumToInt(Value.two) == 2);
2863}2863}
28642864
2865// You can override the ordinal value for an enum.2865// You can override the ordinal value for an enum.
...@@ -2869,9 +2869,9 @@ const Value2 = enum(u32) {...@@ -2869,9 +2869,9 @@ const Value2 = enum(u32) {
2869 million = 1000000,2869 million = 1000000,
2870};2870};
2871test "set enum ordinal value" {2871test "set enum ordinal value" {
2872 assert(@enumToInt(Value2.hundred) == 100);2872 expect(@enumToInt(Value2.hundred) == 100);
2873 assert(@enumToInt(Value2.thousand) == 1000);2873 expect(@enumToInt(Value2.thousand) == 1000);
2874 assert(@enumToInt(Value2.million) == 1000000);2874 expect(@enumToInt(Value2.million) == 1000000);
2875}2875}
28762876
2877// Enums can have methods, the same as structs and unions.2877// Enums can have methods, the same as structs and unions.
...@@ -2889,7 +2889,7 @@ const Suit = enum {...@@ -2889,7 +2889,7 @@ const Suit = enum {
2889};2889};
2890test "enum method" {2890test "enum method" {
2891 const p = Suit.spades;2891 const p = Suit.spades;
2892 assert(!p.isClubs());2892 expect(!p.isClubs());
2893}2893}
28942894
2895// An enum variant of different types can be switched upon.2895// An enum variant of different types can be switched upon.
...@@ -2905,7 +2905,7 @@ test "enum variant switch" {...@@ -2905,7 +2905,7 @@ test "enum variant switch" {
2905 Foo.number => "this is a number",2905 Foo.number => "this is a number",
2906 Foo.none => "this is a none",2906 Foo.none => "this is a none",
2907 };2907 };
2908 assert(mem.eql(u8, what_is_it, "this is a number"));2908 expect(mem.eql(u8, what_is_it, "this is a number"));
2909}2909}
29102910
2911// @TagType can be used to access the integer tag type of an enum.2911// @TagType can be used to access the integer tag type of an enum.
...@@ -2916,18 +2916,18 @@ const Small = enum {...@@ -2916,18 +2916,18 @@ const Small = enum {
2916 four,2916 four,
2917};2917};
2918test "@TagType" {2918test "@TagType" {
2919 assert(@TagType(Small) == u2);2919 expect(@TagType(Small) == u2);
2920}2920}
29212921
2922// @typeInfo tells us the field count and the fields names:2922// @typeInfo tells us the field count and the fields names:
2923test "@typeInfo" {2923test "@typeInfo" {
2924 assert(@typeInfo(Small).Enum.fields.len == 4);2924 expect(@typeInfo(Small).Enum.fields.len == 4);
2925 assert(mem.eql(u8, @typeInfo(Small).Enum.fields[1].name, "two"));2925 expect(mem.eql(u8, @typeInfo(Small).Enum.fields[1].name, "two"));
2926}2926}
29272927
2928// @tagName gives a []const u8 representation of an enum value:2928// @tagName gives a []const u8 representation of an enum value:
2929test "@tagName" {2929test "@tagName" {
2930 assert(mem.eql(u8, @tagName(Small.three), "three"));2930 expect(mem.eql(u8, @tagName(Small.three), "three"));
2931}2931}
2932 {#code_end#}2932 {#code_end#}
2933 {#see_also|@typeInfo|@tagName|@sizeOf#}2933 {#see_also|@typeInfo|@tagName|@sizeOf#}
...@@ -2962,7 +2962,7 @@ test "packed enum" {...@@ -2962,7 +2962,7 @@ test "packed enum" {
2962 two,2962 two,
2963 three,2963 three,
2964 };2964 };
2965 std.debug.assert(@sizeOf(Number) == @sizeOf(u8));2965 std.testing.expect(@sizeOf(Number) == @sizeOf(u8));
2966}2966}
2967 {#code_end#}2967 {#code_end#}
2968 <p>This makes the enum eligible to be in a {#link|packed struct#}.</p>2968 <p>This makes the enum eligible to be in a {#link|packed struct#}.</p>
...@@ -2974,7 +2974,7 @@ test "packed enum" {...@@ -2974,7 +2974,7 @@ test "packed enum" {
2974 </p>2974 </p>
2975 {#code_begin|test#}2975 {#code_begin|test#}
2976const std = @import("std");2976const std = @import("std");
2977const assert = std.debug.assert;2977const expect = std.testing.expect;
29782978
2979const Color = enum {2979const Color = enum {
2980 auto,2980 auto,
...@@ -2985,7 +2985,7 @@ const Color = enum {...@@ -2985,7 +2985,7 @@ const Color = enum {
2985test "enum literals" {2985test "enum literals" {
2986 const color1: Color = .auto;2986 const color1: Color = .auto;
2987 const color2 = Color.auto;2987 const color2 = Color.auto;
2988 assert(color1 == color2);2988 expect(color1 == color2);
2989}2989}
29902990
2991test "switch using enum literals" {2991test "switch using enum literals" {
...@@ -2995,7 +2995,7 @@ test "switch using enum literals" {...@@ -2995,7 +2995,7 @@ test "switch using enum literals" {
2995 .on => true,2995 .on => true,
2996 .off => false,2996 .off => false,
2997 };2997 };
2998 assert(result);2998 expect(result);
2999}2999}
3000 {#code_end#}3000 {#code_end#}
3001 {#header_close#}3001 {#header_close#}
...@@ -3014,7 +3014,7 @@ test "switch using enum literals" {...@@ -3014,7 +3014,7 @@ test "switch using enum literals" {
3014 </p>3014 </p>
3015 {#code_begin|test#}3015 {#code_begin|test#}
3016const std = @import("std");3016const std = @import("std");
3017const assert = std.debug.assert;3017const expect = std.testing.expect;
30183018
3019const Number = enum(u8) {3019const Number = enum(u8) {
3020 one,3020 one,
...@@ -3031,12 +3031,12 @@ test "switch on non-exhaustive enum" {...@@ -3031,12 +3031,12 @@ test "switch on non-exhaustive enum" {
3031 .three => false,3031 .three => false,
3032 _ => false,3032 _ => false,
3033 };3033 };
3034 assert(result);3034 expect(result);
3035 const is_one = switch (number) {3035 const is_one = switch (number) {
3036 .one => true,3036 .one => true,
3037 else => false,3037 else => false,
3038 };3038 };
3039 assert(is_one);3039 expect(is_one);
3040}3040}
3041 {#code_end#}3041 {#code_end#}
3042 {#header_close#}3042 {#header_close#}
...@@ -3067,7 +3067,7 @@ test "simple union" {...@@ -3067,7 +3067,7 @@ test "simple union" {
3067 <p>You can activate another field by assigning the entire union:</p>3067 <p>You can activate another field by assigning the entire union:</p>
3068 {#code_begin|test#}3068 {#code_begin|test#}
3069const std = @import("std");3069const std = @import("std");
3070const assert = std.debug.assert;3070const expect = std.testing.expect;
30713071
3072const Payload = union {3072const Payload = union {
3073 int: i64,3073 int: i64,
...@@ -3076,9 +3076,9 @@ const Payload = union {...@@ -3076,9 +3076,9 @@ const Payload = union {
3076};3076};
3077test "simple union" {3077test "simple union" {
3078 var payload = Payload{ .int = 1234 };3078 var payload = Payload{ .int = 1234 };
3079 assert(payload.int == 1234);3079 expect(payload.int == 1234);
3080 payload = Payload{ .float = 12.34 };3080 payload = Payload{ .float = 12.34 };
3081 assert(payload.float == 12.34);3081 expect(payload.float == 12.34);
3082}3082}
3083 {#code_end#}3083 {#code_end#}
3084 <p>3084 <p>
...@@ -3097,7 +3097,7 @@ test "simple union" {...@@ -3097,7 +3097,7 @@ test "simple union" {
3097 </p>3097 </p>
3098 {#code_begin|test#}3098 {#code_begin|test#}
3099const std = @import("std");3099const std = @import("std");
3100const assert = std.debug.assert;3100const expect = std.testing.expect;
31013101
3102const ComplexTypeTag = enum {3102const ComplexTypeTag = enum {
3103 ok,3103 ok,
...@@ -3110,24 +3110,24 @@ const ComplexType = union(ComplexTypeTag) {...@@ -3110,24 +3110,24 @@ const ComplexType = union(ComplexTypeTag) {
31103110
3111test "switch on tagged union" {3111test "switch on tagged union" {
3112 const c = ComplexType{ .ok = 42 };3112 const c = ComplexType{ .ok = 42 };
3113 assert(@as(ComplexTypeTag, c) == ComplexTypeTag.ok);3113 expect(@as(ComplexTypeTag, c) == ComplexTypeTag.ok);
31143114
3115 switch (c) {3115 switch (c) {
3116 ComplexTypeTag.ok => |value| assert(value == 42),3116 ComplexTypeTag.ok => |value| expect(value == 42),
3117 ComplexTypeTag.not_ok => unreachable,3117 ComplexTypeTag.not_ok => unreachable,
3118 }3118 }
3119}3119}
31203120
3121test "@TagType" {3121test "@TagType" {
3122 assert(@TagType(ComplexType) == ComplexTypeTag);3122 expect(@TagType(ComplexType) == ComplexTypeTag);
3123}3123}
31243124
3125test "coerce to enum" {3125test "coerce to enum" {
3126 const c1 = ComplexType{ .ok = 42 };3126 const c1 = ComplexType{ .ok = 42 };
3127 const c2 = ComplexType.not_ok;3127 const c2 = ComplexType.not_ok;
31283128
3129 assert(c1 == .ok);3129 expect(c1 == .ok);
3130 assert(c2 == .not_ok);3130 expect(c2 == .not_ok);
3131}3131}
3132 {#code_end#}3132 {#code_end#}
3133 <p>In order to modify the payload of a tagged union in a switch expression,3133 <p>In order to modify the payload of a tagged union in a switch expression,
...@@ -3135,7 +3135,7 @@ test "coerce to enum" {...@@ -3135,7 +3135,7 @@ test "coerce to enum" {
3135 </p>3135 </p>
3136 {#code_begin|test#}3136 {#code_begin|test#}
3137const std = @import("std");3137const std = @import("std");
3138const assert = std.debug.assert;3138const expect = std.testing.expect;
31393139
3140const ComplexTypeTag = enum {3140const ComplexTypeTag = enum {
3141 ok,3141 ok,
...@@ -3148,14 +3148,14 @@ const ComplexType = union(ComplexTypeTag) {...@@ -3148,14 +3148,14 @@ const ComplexType = union(ComplexTypeTag) {
31483148
3149test "modify tagged union in switch" {3149test "modify tagged union in switch" {
3150 var c = ComplexType{ .ok = 42 };3150 var c = ComplexType{ .ok = 42 };
3151 assert(@as(ComplexTypeTag, c) == ComplexTypeTag.ok);3151 expect(@as(ComplexTypeTag, c) == ComplexTypeTag.ok);
31523152
3153 switch (c) {3153 switch (c) {
3154 ComplexTypeTag.ok => |*value| value.* += 1,3154 ComplexTypeTag.ok => |*value| value.* += 1,
3155 ComplexTypeTag.not_ok => unreachable,3155 ComplexTypeTag.not_ok => unreachable,
3156 }3156 }
31573157
3158 assert(c.ok == 43);3158 expect(c.ok == 43);
3159}3159}
3160 {#code_end#}3160 {#code_end#}
3161 <p>3161 <p>
...@@ -3164,7 +3164,7 @@ test "modify tagged union in switch" {...@@ -3164,7 +3164,7 @@ test "modify tagged union in switch" {
3164 </p>3164 </p>
3165 {#code_begin|test#}3165 {#code_begin|test#}
3166const std = @import("std");3166const std = @import("std");
3167const assert = std.debug.assert;3167const expect = std.testing.expect;
31683168
3169const Variant = union(enum) {3169const Variant = union(enum) {
3170 int: i32,3170 int: i32,
...@@ -3186,8 +3186,8 @@ test "union method" {...@@ -3186,8 +3186,8 @@ test "union method" {
3186 var v1 = Variant{ .int = 1 };3186 var v1 = Variant{ .int = 1 };
3187 var v2 = Variant{ .boolean = false };3187 var v2 = Variant{ .boolean = false };
31883188
3189 assert(v1.truthy());3189 expect(v1.truthy());
3190 assert(!v2.truthy());3190 expect(!v2.truthy());
3191}3191}
3192 {#code_end#}3192 {#code_end#}
3193 <p>3193 <p>
...@@ -3196,7 +3196,7 @@ test "union method" {...@@ -3196,7 +3196,7 @@ test "union method" {
3196 </p>3196 </p>
3197 {#code_begin|test#}3197 {#code_begin|test#}
3198const std = @import("std");3198const std = @import("std");
3199const assert = std.debug.assert;3199const expect = std.testing.expect;
32003200
3201const Small2 = union(enum) {3201const Small2 = union(enum) {
3202 a: i32,3202 a: i32,
...@@ -3204,7 +3204,7 @@ const Small2 = union(enum) {...@@ -3204,7 +3204,7 @@ const Small2 = union(enum) {
3204 c: u8,3204 c: u8,
3205};3205};
3206test "@tagName" {3206test "@tagName" {
3207 assert(std.mem.eql(u8, @tagName(Small2.a), "a"));3207 expect(std.mem.eql(u8, @tagName(Small2.a), "a"));
3208}3208}
3209 {#code_end#}3209 {#code_end#}
3210 {#header_close#}3210 {#header_close#}
...@@ -3227,7 +3227,7 @@ test "@tagName" {...@@ -3227,7 +3227,7 @@ test "@tagName" {
3227 the type:</p>3227 the type:</p>
3228 {#code_begin|test|anon_union#}3228 {#code_begin|test|anon_union#}
3229const std = @import("std");3229const std = @import("std");
3230const assert = std.debug.assert;3230const expect = std.testing.expect;
32313231
3232const Number = union {3232const Number = union {
3233 int: i32,3233 int: i32,
...@@ -3237,8 +3237,8 @@ const Number = union {...@@ -3237,8 +3237,8 @@ const Number = union {
3237test "anonymous union literal syntax" {3237test "anonymous union literal syntax" {
3238 var i: Number = .{.int = 42};3238 var i: Number = .{.int = 42};
3239 var f = makeNumber();3239 var f = makeNumber();
3240 assert(i.int == 42);3240 expect(i.int == 42);
3241 assert(f.float == 12.34);3241 expect(f.float == 12.34);
3242}3242}
32433243
3244fn makeNumber() Number {3244fn makeNumber() Number {
...@@ -3291,7 +3291,7 @@ test "access variable after block scope" {...@@ -3291,7 +3291,7 @@ test "access variable after block scope" {
3291 </p>3291 </p>
3292 {#code_begin|test#}3292 {#code_begin|test#}
3293const std = @import("std");3293const std = @import("std");
3294const assert = std.debug.assert;3294const expect = std.testing.expect;
32953295
3296test "labeled break from labeled block expression" {3296test "labeled break from labeled block expression" {
3297 var y: i32 = 123;3297 var y: i32 = 123;
...@@ -3300,8 +3300,8 @@ test "labeled break from labeled block expression" {...@@ -3300,8 +3300,8 @@ test "labeled break from labeled block expression" {
3300 y += 1;3300 y += 1;
3301 break :blk y;3301 break :blk y;
3302 };3302 };
3303 assert(x == 124);3303 expect(x == 124);
3304 assert(y == 124);3304 expect(y == 124);
3305}3305}
3306 {#code_end#}3306 {#code_end#}
3307 <p>Here, {#syntax#}blk{#endsyntax#} can be any name.</p>3307 <p>Here, {#syntax#}blk{#endsyntax#} can be any name.</p>
...@@ -3339,7 +3339,7 @@ test "separate scopes" {...@@ -3339,7 +3339,7 @@ test "separate scopes" {
3339 {#header_open|switch#}3339 {#header_open|switch#}
3340 {#code_begin|test|switch#}3340 {#code_begin|test|switch#}
3341const std = @import("std");3341const std = @import("std");
3342const assert = std.debug.assert;3342const expect = std.testing.expect;
33433343
3344test "switch simple" {3344test "switch simple" {
3345 const a: u64 = 10;3345 const a: u64 = 10;
...@@ -3379,7 +3379,7 @@ test "switch simple" {...@@ -3379,7 +3379,7 @@ test "switch simple" {
3379 else => 9,3379 else => 9,
3380 };3380 };
33813381
3382 assert(b == 1);3382 expect(b == 1);
3383}3383}
33843384
3385// Switch expressions can be used outside a function:3385// Switch expressions can be used outside a function:
...@@ -3409,7 +3409,7 @@ test "switch inside function" {...@@ -3409,7 +3409,7 @@ test "switch inside function" {
3409 turning it into a pointer.3409 turning it into a pointer.
3410 </p>3410 </p>
3411 {#code_begin|test#}3411 {#code_begin|test#}
3412const assert = @import("std").debug.assert;3412const expect = @import("std").testing.expect;
34133413
3414test "switch on tagged union" {3414test "switch on tagged union" {
3415 const Point = struct {3415 const Point = struct {
...@@ -3442,8 +3442,8 @@ test "switch on tagged union" {...@@ -3442,8 +3442,8 @@ test "switch on tagged union" {
3442 Item.d => 8,3442 Item.d => 8,
3443 };3443 };
34443444
3445 assert(b == 6);3445 expect(b == 6);
3446 assert(a.c.x == 2);3446 expect(a.c.x == 2);
3447}3447}
3448 {#code_end#}3448 {#code_end#}
3449 {#see_also|comptime|enum|@compileError|Compile Variables#}3449 {#see_also|comptime|enum|@compileError|Compile Variables#}
...@@ -3477,7 +3477,7 @@ test "exhaustive switching" {...@@ -3477,7 +3477,7 @@ test "exhaustive switching" {
3477 </p>3477 </p>
3478 {#code_begin|test#}3478 {#code_begin|test#}
3479const std = @import("std");3479const std = @import("std");
3480const assert = std.debug.assert;3480const expect = std.testing.expect;
34813481
3482const Color = enum {3482const Color = enum {
3483 auto,3483 auto,
...@@ -3492,7 +3492,7 @@ test "enum literals with switch" {...@@ -3492,7 +3492,7 @@ test "enum literals with switch" {
3492 .on => false,3492 .on => false,
3493 .off => true,3493 .off => true,
3494 };3494 };
3495 assert(result);3495 expect(result);
3496}3496}
3497 {#code_end#}3497 {#code_end#}
3498 {#header_close#}3498 {#header_close#}
...@@ -3504,21 +3504,21 @@ test "enum literals with switch" {...@@ -3504,21 +3504,21 @@ test "enum literals with switch" {
3504 some condition is no longer true.3504 some condition is no longer true.
3505 </p>3505 </p>
3506 {#code_begin|test|while#}3506 {#code_begin|test|while#}
3507const assert = @import("std").debug.assert;3507const expect = @import("std").testing.expect;
35083508
3509test "while basic" {3509test "while basic" {
3510 var i: usize = 0;3510 var i: usize = 0;
3511 while (i < 10) {3511 while (i < 10) {
3512 i += 1;3512 i += 1;
3513 }3513 }
3514 assert(i == 10);3514 expect(i == 10);
3515}3515}
3516 {#code_end#}3516 {#code_end#}
3517 <p>3517 <p>
3518 Use {#syntax#}break{#endsyntax#} to exit a while loop early.3518 Use {#syntax#}break{#endsyntax#} to exit a while loop early.
3519 </p>3519 </p>
3520 {#code_begin|test|while#}3520 {#code_begin|test|while#}
3521const assert = @import("std").debug.assert;3521const expect = @import("std").testing.expect;
35223522
3523test "while break" {3523test "while break" {
3524 var i: usize = 0;3524 var i: usize = 0;
...@@ -3527,14 +3527,14 @@ test "while break" {...@@ -3527,14 +3527,14 @@ test "while break" {
3527 break;3527 break;
3528 i += 1;3528 i += 1;
3529 }3529 }
3530 assert(i == 10);3530 expect(i == 10);
3531}3531}
3532 {#code_end#}3532 {#code_end#}
3533 <p>3533 <p>
3534 Use {#syntax#}continue{#endsyntax#} to jump back to the beginning of the loop.3534 Use {#syntax#}continue{#endsyntax#} to jump back to the beginning of the loop.
3535 </p>3535 </p>
3536 {#code_begin|test|while#}3536 {#code_begin|test|while#}
3537const assert = @import("std").debug.assert;3537const expect = @import("std").testing.expect;
35383538
3539test "while continue" {3539test "while continue" {
3540 var i: usize = 0;3540 var i: usize = 0;
...@@ -3544,7 +3544,7 @@ test "while continue" {...@@ -3544,7 +3544,7 @@ test "while continue" {
3544 continue;3544 continue;
3545 break;3545 break;
3546 }3546 }
3547 assert(i == 10);3547 expect(i == 10);
3548}3548}
3549 {#code_end#}3549 {#code_end#}
3550 <p>3550 <p>
...@@ -3552,12 +3552,12 @@ test "while continue" {...@@ -3552,12 +3552,12 @@ test "while continue" {
3552 is continued. The {#syntax#}continue{#endsyntax#} keyword respects this expression.3552 is continued. The {#syntax#}continue{#endsyntax#} keyword respects this expression.
3553 </p>3553 </p>
3554 {#code_begin|test|while#}3554 {#code_begin|test|while#}
3555const assert = @import("std").debug.assert;3555const expect = @import("std").testing.expect;
35563556
3557test "while loop continue expression" {3557test "while loop continue expression" {
3558 var i: usize = 0;3558 var i: usize = 0;
3559 while (i < 10) : (i += 1) {}3559 while (i < 10) : (i += 1) {}
3560 assert(i == 10);3560 expect(i == 10);
3561}3561}
35623562
3563test "while loop continue expression, more complicated" {3563test "while loop continue expression, more complicated" {
...@@ -3565,7 +3565,7 @@ test "while loop continue expression, more complicated" {...@@ -3565,7 +3565,7 @@ test "while loop continue expression, more complicated" {
3565 var j: usize = 1;3565 var j: usize = 1;
3566 while (i * j < 2000) : ({ i *= 2; j *= 3; }) {3566 while (i * j < 2000) : ({ i *= 2; j *= 3; }) {
3567 const my_ij = i * j;3567 const my_ij = i * j;
3568 assert(my_ij < 2000);3568 expect(my_ij < 2000);
3569 }3569 }
3570}3570}
3571 {#code_end#}3571 {#code_end#}
...@@ -3581,11 +3581,11 @@ test "while loop continue expression, more complicated" {...@@ -3581,11 +3581,11 @@ test "while loop continue expression, more complicated" {
3581 evaluated.3581 evaluated.
3582 </p>3582 </p>
3583 {#code_begin|test|while#}3583 {#code_begin|test|while#}
3584const assert = @import("std").debug.assert;3584const expect = @import("std").testing.expect;
35853585
3586test "while else" {3586test "while else" {
3587 assert(rangeHasNumber(0, 10, 5));3587 expect(rangeHasNumber(0, 10, 5));
3588 assert(!rangeHasNumber(0, 10, 15));3588 expect(!rangeHasNumber(0, 10, 15));
3589}3589}
35903590
3591fn rangeHasNumber(begin: usize, end: usize, number: usize) bool {3591fn rangeHasNumber(begin: usize, end: usize, number: usize) bool {
...@@ -3634,7 +3634,7 @@ test "nested continue" {...@@ -3634,7 +3634,7 @@ test "nested continue" {
3634 be executed on the first null value encountered.3634 be executed on the first null value encountered.
3635 </p>3635 </p>
3636 {#code_begin|test|while#}3636 {#code_begin|test|while#}
3637const assert = @import("std").debug.assert;3637const expect = @import("std").testing.expect;
36383638
3639test "while null capture" {3639test "while null capture" {
3640 var sum1: u32 = 0;3640 var sum1: u32 = 0;
...@@ -3642,14 +3642,14 @@ test "while null capture" {...@@ -3642,14 +3642,14 @@ test "while null capture" {
3642 while (eventuallyNullSequence()) |value| {3642 while (eventuallyNullSequence()) |value| {
3643 sum1 += value;3643 sum1 += value;
3644 }3644 }
3645 assert(sum1 == 3);3645 expect(sum1 == 3);
36463646
3647 var sum2: u32 = 0;3647 var sum2: u32 = 0;
3648 numbers_left = 3;3648 numbers_left = 3;
3649 while (eventuallyNullSequence()) |value| {3649 while (eventuallyNullSequence()) |value| {
3650 sum2 += value;3650 sum2 += value;
3651 } else {3651 } else {
3652 assert(sum2 == 3);3652 expect(sum2 == 3);
3653 }3653 }
3654}3654}
36553655
...@@ -3676,7 +3676,7 @@ fn eventuallyNullSequence() ?u32 {...@@ -3676,7 +3676,7 @@ fn eventuallyNullSequence() ?u32 {
3676 the while condition must have an {#link|Error Union Type#}.3676 the while condition must have an {#link|Error Union Type#}.
3677 </p>3677 </p>
3678 {#code_begin|test|while#}3678 {#code_begin|test|while#}
3679const assert = @import("std").debug.assert;3679const expect = @import("std").testing.expect;
36803680
3681test "while error union capture" {3681test "while error union capture" {
3682 var sum1: u32 = 0;3682 var sum1: u32 = 0;
...@@ -3684,7 +3684,7 @@ test "while error union capture" {...@@ -3684,7 +3684,7 @@ test "while error union capture" {
3684 while (eventuallyErrorSequence()) |value| {3684 while (eventuallyErrorSequence()) |value| {
3685 sum1 += value;3685 sum1 += value;
3686 } else |err| {3686 } else |err| {
3687 assert(err == error.ReachedZero);3687 expect(err == error.ReachedZero);
3688 }3688 }
3689}3689}
36903690
...@@ -3706,7 +3706,7 @@ fn eventuallyErrorSequence() anyerror!u32 {...@@ -3706,7 +3706,7 @@ fn eventuallyErrorSequence() anyerror!u32 {
3706 such as use types as first class values.3706 such as use types as first class values.
3707 </p>3707 </p>
3708 {#code_begin|test#}3708 {#code_begin|test#}
3709const assert = @import("std").debug.assert;3709const expect = @import("std").testing.expect;
37103710
3711test "inline while loop" {3711test "inline while loop" {
3712 comptime var i = 0;3712 comptime var i = 0;
...@@ -3720,7 +3720,7 @@ test "inline while loop" {...@@ -3720,7 +3720,7 @@ test "inline while loop" {
3720 };3720 };
3721 sum += typeNameLength(T);3721 sum += typeNameLength(T);
3722 }3722 }
3723 assert(sum == 9);3723 expect(sum == 9);
3724}3724}
37253725
3726fn typeNameLength(comptime T: type) usize {3726fn typeNameLength(comptime T: type) usize {
...@@ -3741,7 +3741,7 @@ fn typeNameLength(comptime T: type) usize {...@@ -3741,7 +3741,7 @@ fn typeNameLength(comptime T: type) usize {
3741 {#header_close#}3741 {#header_close#}
3742 {#header_open|for#}3742 {#header_open|for#}
3743 {#code_begin|test|for#}3743 {#code_begin|test|for#}
3744const assert = @import("std").debug.assert;3744const expect = @import("std").testing.expect;
37453745
3746test "for basics" {3746test "for basics" {
3747 const items = [_]i32 { 4, 5, 3, 4, 0 };3747 const items = [_]i32 { 4, 5, 3, 4, 0 };
...@@ -3755,22 +3755,22 @@ test "for basics" {...@@ -3755,22 +3755,22 @@ test "for basics" {
3755 }3755 }
3756 sum += value;3756 sum += value;
3757 }3757 }
3758 assert(sum == 16);3758 expect(sum == 16);
37593759
3760 // To iterate over a portion of a slice, reslice.3760 // To iterate over a portion of a slice, reslice.
3761 for (items[0..1]) |value| {3761 for (items[0..1]) |value| {
3762 sum += value;3762 sum += value;
3763 }3763 }
3764 assert(sum == 20);3764 expect(sum == 20);
37653765
3766 // To access the index of iteration, specify a second capture value.3766 // To access the index of iteration, specify a second capture value.
3767 // This is zero-indexed.3767 // This is zero-indexed.
3768 var sum2: i32 = 0;3768 var sum2: i32 = 0;
3769 for (items) |value, i| {3769 for (items) |value, i| {
3770 assert(@TypeOf(i) == usize);3770 expect(@TypeOf(i) == usize);
3771 sum2 += @intCast(i32, i);3771 sum2 += @intCast(i32, i);
3772 }3772 }
3773 assert(sum2 == 10);3773 expect(sum2 == 10);
3774}3774}
37753775
3776test "for reference" {3776test "for reference" {
...@@ -3782,9 +3782,9 @@ test "for reference" {...@@ -3782,9 +3782,9 @@ test "for reference" {
3782 value.* += 1;3782 value.* += 1;
3783 }3783 }
37843784
3785 assert(items[0] == 4);3785 expect(items[0] == 4);
3786 assert(items[1] == 5);3786 expect(items[1] == 5);
3787 assert(items[2] == 3);3787 expect(items[2] == 3);
3788}3788}
37893789
3790test "for else" {3790test "for else" {
...@@ -3799,10 +3799,10 @@ test "for else" {...@@ -3799,10 +3799,10 @@ test "for else" {
3799 sum += value.?;3799 sum += value.?;
3800 }3800 }
3801 } else blk: {3801 } else blk: {
3802 assert(sum == 12);3802 expect(sum == 12);
3803 break :blk sum;3803 break :blk sum;
3804 };3804 };
3805 assert(result == 12);3805 expect(result == 12);
3806}3806}
3807 {#code_end#}3807 {#code_end#}
3808 {#header_open|Labeled for#}3808 {#header_open|Labeled for#}
...@@ -3810,7 +3810,7 @@ test "for else" {...@@ -3810,7 +3810,7 @@ test "for else" {
3810 or {#syntax#}continue{#endsyntax#} from within a nested loop:</p>3810 or {#syntax#}continue{#endsyntax#} from within a nested loop:</p>
3811 {#code_begin|test#}3811 {#code_begin|test#}
3812const std = @import("std");3812const std = @import("std");
3813const assert = std.debug.assert;3813const expect = std.testing.expect;
38143814
3815test "nested break" {3815test "nested break" {
3816 var count: usize = 0;3816 var count: usize = 0;
...@@ -3820,7 +3820,7 @@ test "nested break" {...@@ -3820,7 +3820,7 @@ test "nested break" {
3820 break :outer;3820 break :outer;
3821 }3821 }
3822 }3822 }
3823 assert(count == 1);3823 expect(count == 1);
3824}3824}
38253825
3826test "nested continue" {3826test "nested continue" {
...@@ -3832,7 +3832,7 @@ test "nested continue" {...@@ -3832,7 +3832,7 @@ test "nested continue" {
3832 }3832 }
3833 }3833 }
38343834
3835 assert(count == 8);3835 expect(count == 8);
3836}3836}
3837 {#code_end#}3837 {#code_end#}
3838 {#header_close#}3838 {#header_close#}
...@@ -3845,7 +3845,7 @@ test "nested continue" {...@@ -3845,7 +3845,7 @@ test "nested continue" {
3845 compile-time known.3845 compile-time known.
3846 </p>3846 </p>
3847 {#code_begin|test#}3847 {#code_begin|test#}
3848const assert = @import("std").debug.assert;3848const expect = @import("std").testing.expect;
38493849
3850test "inline for loop" {3850test "inline for loop" {
3851 const nums = [_]i32{2, 4, 6};3851 const nums = [_]i32{2, 4, 6};
...@@ -3859,7 +3859,7 @@ test "inline for loop" {...@@ -3859,7 +3859,7 @@ test "inline for loop" {
3859 };3859 };
3860 sum += typeNameLength(T);3860 sum += typeNameLength(T);
3861 }3861 }
3862 assert(sum == 9);3862 expect(sum == 9);
3863}3863}
38643864
3865fn typeNameLength(comptime T: type) usize {3865fn typeNameLength(comptime T: type) usize {
...@@ -3885,14 +3885,14 @@ fn typeNameLength(comptime T: type) usize {...@@ -3885,14 +3885,14 @@ fn typeNameLength(comptime T: type) usize {
3885// * ?T3885// * ?T
3886// * anyerror!T3886// * anyerror!T
38873887
3888const assert = @import("std").debug.assert;3888const expect = @import("std").testing.expect;
38893889
3890test "if expression" {3890test "if expression" {
3891 // If expressions are used instead of a ternary expression.3891 // If expressions are used instead of a ternary expression.
3892 const a: u32 = 5;3892 const a: u32 = 5;
3893 const b: u32 = 4;3893 const b: u32 = 4;
3894 const result = if (a != b) 47 else 3089;3894 const result = if (a != b) 47 else 3089;
3895 assert(result == 47);3895 expect(result == 47);
3896}3896}
38973897
3898test "if boolean" {3898test "if boolean" {
...@@ -3900,7 +3900,7 @@ test "if boolean" {...@@ -3900,7 +3900,7 @@ test "if boolean" {
3900 const a: u32 = 5;3900 const a: u32 = 5;
3901 const b: u32 = 4;3901 const b: u32 = 4;
3902 if (a != b) {3902 if (a != b) {
3903 assert(true);3903 expect(true);
3904 } else if (a == 9) {3904 } else if (a == 9) {
3905 unreachable;3905 unreachable;
3906 } else {3906 } else {
...@@ -3913,7 +3913,7 @@ test "if optional" {...@@ -3913,7 +3913,7 @@ test "if optional" {
39133913
3914 const a: ?u32 = 0;3914 const a: ?u32 = 0;
3915 if (a) |value| {3915 if (a) |value| {
3916 assert(value == 0);3916 expect(value == 0);
3917 } else {3917 } else {
3918 unreachable;3918 unreachable;
3919 }3919 }
...@@ -3922,17 +3922,17 @@ test "if optional" {...@@ -3922,17 +3922,17 @@ test "if optional" {
3922 if (b) |value| {3922 if (b) |value| {
3923 unreachable;3923 unreachable;
3924 } else {3924 } else {
3925 assert(true);3925 expect(true);
3926 }3926 }
39273927
3928 // The else is not required.3928 // The else is not required.
3929 if (a) |value| {3929 if (a) |value| {
3930 assert(value == 0);3930 expect(value == 0);
3931 }3931 }
39323932
3933 // To test against null only, use the binary equality operator.3933 // To test against null only, use the binary equality operator.
3934 if (b == null) {3934 if (b == null) {
3935 assert(true);3935 expect(true);
3936 }3936 }
39373937
3938 // Access the value by reference using a pointer capture.3938 // Access the value by reference using a pointer capture.
...@@ -3942,7 +3942,7 @@ test "if optional" {...@@ -3942,7 +3942,7 @@ test "if optional" {
3942 }3942 }
39433943
3944 if (c) |value| {3944 if (c) |value| {
3945 assert(value == 2);3945 expect(value == 2);
3946 } else {3946 } else {
3947 unreachable;3947 unreachable;
3948 }3948 }
...@@ -3954,7 +3954,7 @@ test "if error union" {...@@ -3954,7 +3954,7 @@ test "if error union" {
39543954
3955 const a: anyerror!u32 = 0;3955 const a: anyerror!u32 = 0;
3956 if (a) |value| {3956 if (a) |value| {
3957 assert(value == 0);3957 expect(value == 0);
3958 } else |err| {3958 } else |err| {
3959 unreachable;3959 unreachable;
3960 }3960 }
...@@ -3963,17 +3963,17 @@ test "if error union" {...@@ -3963,17 +3963,17 @@ test "if error union" {
3963 if (b) |value| {3963 if (b) |value| {
3964 unreachable;3964 unreachable;
3965 } else |err| {3965 } else |err| {
3966 assert(err == error.BadValue);3966 expect(err == error.BadValue);
3967 }3967 }
39683968
3969 // The else and |err| capture is strictly required.3969 // The else and |err| capture is strictly required.
3970 if (a) |value| {3970 if (a) |value| {
3971 assert(value == 0);3971 expect(value == 0);
3972 } else |_| {}3972 } else |_| {}
39733973
3974 // To check only the error value, use an empty block expression.3974 // To check only the error value, use an empty block expression.
3975 if (b) |_| {} else |err| {3975 if (b) |_| {} else |err| {
3976 assert(err == error.BadValue);3976 expect(err == error.BadValue);
3977 }3977 }
39783978
3979 // Access the value by reference using a pointer capture.3979 // Access the value by reference using a pointer capture.
...@@ -3985,7 +3985,7 @@ test "if error union" {...@@ -3985,7 +3985,7 @@ test "if error union" {
3985 }3985 }
39863986
3987 if (c) |value| {3987 if (c) |value| {
3988 assert(value == 9);3988 expect(value == 9);
3989 } else |err| {3989 } else |err| {
3990 unreachable;3990 unreachable;
3991 }3991 }
...@@ -3997,14 +3997,14 @@ test "if error union with optional" {...@@ -3997,14 +3997,14 @@ test "if error union with optional" {
39973997
3998 const a: anyerror!?u32 = 0;3998 const a: anyerror!?u32 = 0;
3999 if (a) |optional_value| {3999 if (a) |optional_value| {
4000 assert(optional_value.? == 0);4000 expect(optional_value.? == 0);
4001 } else |err| {4001 } else |err| {
4002 unreachable;4002 unreachable;
4003 }4003 }
40044004
4005 const b: anyerror!?u32 = null;4005 const b: anyerror!?u32 = null;
4006 if (b) |optional_value| {4006 if (b) |optional_value| {
4007 assert(optional_value == null);4007 expect(optional_value == null);
4008 } else |err| {4008 } else |err| {
4009 unreachable;4009 unreachable;
4010 }4010 }
...@@ -4013,7 +4013,7 @@ test "if error union with optional" {...@@ -4013,7 +4013,7 @@ test "if error union with optional" {
4013 if (c) |optional_value| {4013 if (c) |optional_value| {
4014 unreachable;4014 unreachable;
4015 } else |err| {4015 } else |err| {
4016 assert(err == error.BadValue);4016 expect(err == error.BadValue);
4017 }4017 }
40184018
4019 // Access the value by reference by using a pointer capture each time.4019 // Access the value by reference by using a pointer capture each time.
...@@ -4027,7 +4027,7 @@ test "if error union with optional" {...@@ -4027,7 +4027,7 @@ test "if error union with optional" {
4027 }4027 }
40284028
4029 if (d) |optional_value| {4029 if (d) |optional_value| {
4030 assert(optional_value.? == 9);4030 expect(optional_value.? == 9);
4031 } else |err| {4031 } else |err| {
4032 unreachable;4032 unreachable;
4033 }4033 }
...@@ -4038,7 +4038,7 @@ test "if error union with optional" {...@@ -4038,7 +4038,7 @@ test "if error union with optional" {
4038 {#header_open|defer#}4038 {#header_open|defer#}
4039 {#code_begin|test|defer#}4039 {#code_begin|test|defer#}
4040const std = @import("std");4040const std = @import("std");
4041const assert = std.debug.assert;4041const expect = std.testing.expect;
4042const print = std.debug.print;4042const print = std.debug.print;
40434043
4044// defer will execute an expression at the end of the current scope.4044// defer will execute an expression at the end of the current scope.
...@@ -4049,14 +4049,14 @@ fn deferExample() usize {...@@ -4049,14 +4049,14 @@ fn deferExample() usize {
4049 defer a = 2;4049 defer a = 2;
4050 a = 1;4050 a = 1;
4051 }4051 }
4052 assert(a == 2);4052 expect(a == 2);
40534053
4054 a = 5;4054 a = 5;
4055 return a;4055 return a;
4056}4056}
40574057
4058test "defer basics" {4058test "defer basics" {
4059 assert(deferExample() == 5);4059 expect(deferExample() == 5);
4060}4060}
40614061
4062// If multiple defer statements are specified, they will be executed in4062// If multiple defer statements are specified, they will be executed in
...@@ -4133,8 +4133,9 @@ test "basic math" {...@@ -4133,8 +4133,9 @@ test "basic math" {
4133 }4133 }
4134}4134}
4135 {#code_end#}4135 {#code_end#}
4136 <p>In fact, this is how assert is implemented:</p>4136 <p>In fact, this is how {#syntax#}std.debug.assert{#endsyntax#} is implemented:</p>
4137 {#code_begin|test_err#}4137 {#code_begin|test_err#}
4138// This is how std.debug.assert is implemented
4138fn assert(ok: bool) void {4139fn assert(ok: bool) void {
4139 if (!ok) unreachable; // assertion failure4140 if (!ok) unreachable; // assertion failure
4140}4141}
...@@ -4193,19 +4194,19 @@ pub extern "kernel32" fn ExitProcess(exit_code: c_uint) callconv(.Stdcall) noret...@@ -4193,19 +4194,19 @@ pub extern "kernel32" fn ExitProcess(exit_code: c_uint) callconv(.Stdcall) noret
41934194
4194test "foo" {4195test "foo" {
4195 const value = bar() catch ExitProcess(1);4196 const value = bar() catch ExitProcess(1);
4196 assert(value == 1234);4197 expect(value == 1234);
4197}4198}
41984199
4199fn bar() anyerror!u32 {4200fn bar() anyerror!u32 {
4200 return 1234;4201 return 1234;
4201}4202}
42024203
4203const assert = @import("std").debug.assert;4204const expect = @import("std").testing.expect;
4204 {#code_end#}4205 {#code_end#}
4205 {#header_close#}4206 {#header_close#}
4206 {#header_open|Functions#}4207 {#header_open|Functions#}
4207 {#code_begin|test|functions#}4208 {#code_begin|test|functions#}
4208const assert = @import("std").debug.assert;4209const expect = @import("std").testing.expect;
42094210
4210// Functions are declared like this4211// Functions are declared like this
4211fn add(a: i8, b: i8) i8 {4212fn add(a: i8, b: i8) i8 {
...@@ -4256,17 +4257,17 @@ fn do_op(fn_call: call2_op, op1: i8, op2: i8) i8 {...@@ -4256,17 +4257,17 @@ fn do_op(fn_call: call2_op, op1: i8, op2: i8) i8 {
4256}4257}
42574258
4258test "function" {4259test "function" {
4259 assert(do_op(add, 5, 6) == 11);4260 expect(do_op(add, 5, 6) == 11);
4260 assert(do_op(sub2, 5, 6) == -1);4261 expect(do_op(sub2, 5, 6) == -1);
4261}4262}
4262 {#code_end#}4263 {#code_end#}
4263 <p>Function values are like pointers:</p>4264 <p>Function values are like pointers:</p>
4264 {#code_begin|obj#}4265 {#code_begin|obj#}
4265const assert = @import("std").debug.assert;4266const expect = @import("std").testing.expect;
42664267
4267comptime {4268comptime {
4268 assert(@TypeOf(foo) == fn()void);4269 expect(@TypeOf(foo) == fn()void);
4269 assert(@sizeOf(fn()void) == @sizeOf(?fn()void));4270 expect(@sizeOf(fn()void) == @sizeOf(?fn()void));
4270}4271}
42714272
4272fn foo() void { }4273fn foo() void { }
...@@ -4298,10 +4299,10 @@ fn foo(point: Point) i32 {...@@ -4298,10 +4299,10 @@ fn foo(point: Point) i32 {
4298 return point.x + point.y;4299 return point.x + point.y;
4299}4300}
43004301
4301const assert = @import("std").debug.assert;4302const expect = @import("std").testing.expect;
43024303
4303test "pass struct to function" {4304test "pass struct to function" {
4304 assert(foo(Point{ .x = 1, .y = 2 }) == 3);4305 expect(foo(Point{ .x = 1, .y = 2 }) == 3);
4305}4306}
4306 {#code_end#}4307 {#code_end#}
4307 <p>4308 <p>
...@@ -4315,29 +4316,29 @@ test "pass struct to function" {...@@ -4315,29 +4316,29 @@ test "pass struct to function" {
4315 Use {#link|@TypeOf#} and {#link|@typeInfo#} to get information about the inferred type.4316 Use {#link|@TypeOf#} and {#link|@typeInfo#} to get information about the inferred type.
4316 </p>4317 </p>
4317 {#code_begin|test#}4318 {#code_begin|test#}
4318const assert = @import("std").debug.assert;4319const expect = @import("std").testing.expect;
43194320
4320fn addFortyTwo(x: anytype) @TypeOf(x) {4321fn addFortyTwo(x: anytype) @TypeOf(x) {
4321 return x + 42;4322 return x + 42;
4322}4323}
43234324
4324test "fn type inference" {4325test "fn type inference" {
4325 assert(addFortyTwo(1) == 43);4326 expect(addFortyTwo(1) == 43);
4326 assert(@TypeOf(addFortyTwo(1)) == comptime_int);4327 expect(@TypeOf(addFortyTwo(1)) == comptime_int);
4327 var y: i64 = 2;4328 var y: i64 = 2;
4328 assert(addFortyTwo(y) == 44);4329 expect(addFortyTwo(y) == 44);
4329 assert(@TypeOf(addFortyTwo(y)) == i64);4330 expect(@TypeOf(addFortyTwo(y)) == i64);
4330}4331}
4331 {#code_end#}4332 {#code_end#}
43324333
4333 {#header_close#}4334 {#header_close#}
4334 {#header_open|Function Reflection#}4335 {#header_open|Function Reflection#}
4335 {#code_begin|test#}4336 {#code_begin|test#}
4336const assert = @import("std").debug.assert;4337const expect = @import("std").testing.expect;
43374338
4338test "fn reflection" {4339test "fn reflection" {
4339 assert(@typeInfo(@TypeOf(assert)).Fn.return_type.? == void);4340 expect(@typeInfo(@TypeOf(expect)).Fn.return_type.? == void);
4340 assert(@typeInfo(@TypeOf(assert)).Fn.is_var_args == false);4341 expect(@typeInfo(@TypeOf(expect)).Fn.is_var_args == false);
4341}4342}
4342 {#code_end#}4343 {#code_end#}
4343 {#header_close#}4344 {#header_close#}
...@@ -4372,7 +4373,7 @@ const AllocationError = error {...@@ -4372,7 +4373,7 @@ const AllocationError = error {
43724373
4373test "coerce subset to superset" {4374test "coerce subset to superset" {
4374 const err = foo(AllocationError.OutOfMemory);4375 const err = foo(AllocationError.OutOfMemory);
4375 std.debug.assert(err == FileOpenError.OutOfMemory);4376 std.testing.expect(err == FileOpenError.OutOfMemory);
4376}4377}
43774378
4378fn foo(err: AllocationError) FileOpenError {4379fn foo(err: AllocationError) FileOpenError {
...@@ -4480,7 +4481,7 @@ fn charToDigit(c: u8) u8 {...@@ -4480,7 +4481,7 @@ fn charToDigit(c: u8) u8 {
44804481
4481test "parse u64" {4482test "parse u64" {
4482 const result = try parseU64("1234", 10);4483 const result = try parseU64("1234", 10);
4483 std.debug.assert(result == 1234);4484 std.testing.expect(result == 1234);
4484}4485}
4485 {#code_end#}4486 {#code_end#}
4486 <p>4487 <p>
...@@ -4625,7 +4626,7 @@ fn createFoo(param: i32) !Foo {...@@ -4625,7 +4626,7 @@ fn createFoo(param: i32) !Foo {
4625 <p>An error union is created with the {#syntax#}!{#endsyntax#} binary operator.4626 <p>An error union is created with the {#syntax#}!{#endsyntax#} binary operator.
4626 You can use compile-time reflection to access the child type of an error union:</p>4627 You can use compile-time reflection to access the child type of an error union:</p>
4627 {#code_begin|test#}4628 {#code_begin|test#}
4628const assert = @import("std").debug.assert;4629const expect = @import("std").testing.expect;
46294630
4630test "error union" {4631test "error union" {
4631 var foo: anyerror!i32 = undefined;4632 var foo: anyerror!i32 = undefined;
...@@ -4637,10 +4638,10 @@ test "error union" {...@@ -4637,10 +4638,10 @@ test "error union" {
4637 foo = error.SomeError;4638 foo = error.SomeError;
46384639
4639 // Use compile-time reflection to access the payload type of an error union:4640 // Use compile-time reflection to access the payload type of an error union:
4640 comptime assert(@typeInfo(@TypeOf(foo)).ErrorUnion.payload == i32);4641 comptime expect(@typeInfo(@TypeOf(foo)).ErrorUnion.payload == i32);
46414642
4642 // Use compile-time reflection to access the error set type of an error union:4643 // Use compile-time reflection to access the error set type of an error union:
4643 comptime assert(@typeInfo(@TypeOf(foo)).ErrorUnion.error_set == anyerror);4644 comptime expect(@typeInfo(@TypeOf(foo)).ErrorUnion.error_set == anyerror);
4644}4645}
4645 {#code_end#}4646 {#code_end#}
4646 {#header_open|Merging Error Sets#}4647 {#header_open|Merging Error Sets#}
...@@ -5007,7 +5008,7 @@ fn doAThing(optional_foo: ?*Foo) void {...@@ -5007,7 +5008,7 @@ fn doAThing(optional_foo: ?*Foo) void {
5007 <p>An optional is created by putting {#syntax#}?{#endsyntax#} in front of a type. You can use compile-time5008 <p>An optional is created by putting {#syntax#}?{#endsyntax#} in front of a type. You can use compile-time
5008 reflection to access the child type of an optional:</p>5009 reflection to access the child type of an optional:</p>
5009 {#code_begin|test#}5010 {#code_begin|test#}
5010const assert = @import("std").debug.assert;5011const expect = @import("std").testing.expect;
50115012
5012test "optional type" {5013test "optional type" {
5013 // Declare an optional and coerce from null:5014 // Declare an optional and coerce from null:
...@@ -5017,7 +5018,7 @@ test "optional type" {...@@ -5017,7 +5018,7 @@ test "optional type" {
5017 foo = 1234;5018 foo = 1234;
50185019
5019 // Use compile-time reflection to access the child type of the optional:5020 // Use compile-time reflection to access the child type of the optional:
5020 comptime assert(@typeInfo(@TypeOf(foo)).Optional.child == i32);5021 comptime expect(@typeInfo(@TypeOf(foo)).Optional.child == i32);
5021}5022}
5022 {#code_end#}5023 {#code_end#}
5023 {#header_close#}5024 {#header_close#}
...@@ -5034,7 +5035,7 @@ const optional_value: ?i32 = null;...@@ -5034,7 +5035,7 @@ const optional_value: ?i32 = null;
5034 <p>An optional pointer is guaranteed to be the same size as a pointer. The {#syntax#}null{#endsyntax#} of5035 <p>An optional pointer is guaranteed to be the same size as a pointer. The {#syntax#}null{#endsyntax#} of
5035 the optional is guaranteed to be address 0.</p>5036 the optional is guaranteed to be address 0.</p>
5036 {#code_begin|test#}5037 {#code_begin|test#}
5037const assert = @import("std").debug.assert;5038const expect = @import("std").testing.expect;
50385039
5039test "optional pointers" {5040test "optional pointers" {
5040 // Pointers cannot be null. If you want a null pointer, use the optional5041 // Pointers cannot be null. If you want a null pointer, use the optional
...@@ -5044,11 +5045,11 @@ test "optional pointers" {...@@ -5044,11 +5045,11 @@ test "optional pointers" {
5044 var x: i32 = 1;5045 var x: i32 = 1;
5045 ptr = &x;5046 ptr = &x;
50465047
5047 assert(ptr.?.* == 1);5048 expect(ptr.?.* == 1);
50485049
5049 // Optional pointers are the same size as normal pointers, because pointer5050 // Optional pointers are the same size as normal pointers, because pointer
5050 // value 0 is used as the null value.5051 // value 0 is used as the null value.
5051 assert(@sizeOf(?*i32) == @sizeOf(*i32));5052 expect(@sizeOf(?*i32) == @sizeOf(*i32));
5052}5053}
5053 {#code_end#}5054 {#code_end#}
5054 {#header_close#}5055 {#header_close#}
...@@ -5115,13 +5116,13 @@ fn foo(a: *const i32) void {}...@@ -5115,13 +5116,13 @@ fn foo(a: *const i32) void {}
5115 </p>5116 </p>
5116 {#code_begin|test#}5117 {#code_begin|test#}
5117const std = @import("std");5118const std = @import("std");
5118const assert = std.debug.assert;5119const expect = std.testing.expect;
5119const mem = std.mem;5120const mem = std.mem;
51205121
5121test "cast *[1][*]const u8 to [*]const ?[*]const u8" {5122test "cast *[1][*]const u8 to [*]const ?[*]const u8" {
5122 const window_name = [1][*]const u8{"window name"};5123 const window_name = [1][*]const u8{"window name"};
5123 const x: [*]const ?[*]const u8 = &window_name;5124 const x: [*]const ?[*]const u8 = &window_name;
5124 assert(mem.eql(u8, std.mem.spanZ(@ptrCast([*:0]const u8, x[0].?)), "window name"));5125 expect(mem.eql(u8, std.mem.spanZ(@ptrCast([*:0]const u8, x[0].?)), "window name"));
5125}5126}
5126 {#code_end#}5127 {#code_end#}
5127 {#header_close#}5128 {#header_close#}
...@@ -5132,7 +5133,7 @@ test "cast *[1][*]const u8 to [*]const ?[*]const u8" {...@@ -5132,7 +5133,7 @@ test "cast *[1][*]const u8 to [*]const ?[*]const u8" {
5132 </p>5133 </p>
5133 {#code_begin|test#}5134 {#code_begin|test#}
5134const std = @import("std");5135const std = @import("std");
5135const assert = std.debug.assert;5136const expect = std.testing.expect;
5136const mem = std.mem;5137const mem = std.mem;
51375138
5138test "integer widening" {5139test "integer widening" {
...@@ -5142,13 +5143,13 @@ test "integer widening" {...@@ -5142,13 +5143,13 @@ test "integer widening" {
5142 var d: u64 = c;5143 var d: u64 = c;
5143 var e: u64 = d;5144 var e: u64 = d;
5144 var f: u128 = e;5145 var f: u128 = e;
5145 assert(f == a);5146 expect(f == a);
5146}5147}
51475148
5148test "implicit unsigned integer to signed integer" {5149test "implicit unsigned integer to signed integer" {
5149 var a: u8 = 250;5150 var a: u8 = 250;
5150 var b: i16 = a;5151 var b: i16 = a;
5151 assert(b == 250);5152 expect(b == 250);
5152}5153}
51535154
5154test "float widening" {5155test "float widening" {
...@@ -5160,7 +5161,7 @@ test "float widening" {...@@ -5160,7 +5161,7 @@ test "float widening" {
5160 var b: f32 = a;5161 var b: f32 = a;
5161 var c: f64 = b;5162 var c: f64 = b;
5162 var d: f128 = c;5163 var d: f128 = c;
5163 assert(d == a);5164 expect(d == a);
5164}5165}
5165 {#code_end#}5166 {#code_end#}
5166 {#header_close#}5167 {#header_close#}
...@@ -5183,7 +5184,7 @@ test "implicit cast to comptime_int" {...@@ -5183,7 +5184,7 @@ test "implicit cast to comptime_int" {
5183 {#header_open|Type Coercion: Arrays and Pointers#}5184 {#header_open|Type Coercion: Arrays and Pointers#}
5184 {#code_begin|test|coerce_arrays_and_ptrs#}5185 {#code_begin|test|coerce_arrays_and_ptrs#}
5185const std = @import("std");5186const std = @import("std");
5186const assert = std.debug.assert;5187const expect = std.testing.expect;
51875188
5188// This cast exists primarily so that string literals can be5189// This cast exists primarily so that string literals can be
5189// passed to functions that accept const slices. However5190// passed to functions that accept const slices. However
...@@ -5192,41 +5193,41 @@ const assert = std.debug.assert;...@@ -5192,41 +5193,41 @@ const assert = std.debug.assert;
5192test "[N]T to []const T" {5193test "[N]T to []const T" {
5193 var x1: []const u8 = "hello";5194 var x1: []const u8 = "hello";
5194 var x2: []const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 };5195 var x2: []const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 };
5195 assert(std.mem.eql(u8, x1, x2));5196 expect(std.mem.eql(u8, x1, x2));
51965197
5197 var y: []const f32 = &[2]f32{ 1.2, 3.4 };5198 var y: []const f32 = &[2]f32{ 1.2, 3.4 };
5198 assert(y[0] == 1.2);5199 expect(y[0] == 1.2);
5199}5200}
52005201
5201// Likewise, it works when the destination type is an error union.5202// Likewise, it works when the destination type is an error union.
5202test "[N]T to E![]const T" {5203test "[N]T to E![]const T" {
5203 var x1: anyerror![]const u8 = "hello";5204 var x1: anyerror![]const u8 = "hello";
5204 var x2: anyerror![]const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 };5205 var x2: anyerror![]const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 };
5205 assert(std.mem.eql(u8, try x1, try x2));5206 expect(std.mem.eql(u8, try x1, try x2));
52065207
5207 var y: anyerror![]const f32 = &[2]f32{ 1.2, 3.4 };5208 var y: anyerror![]const f32 = &[2]f32{ 1.2, 3.4 };
5208 assert((try y)[0] == 1.2);5209 expect((try y)[0] == 1.2);
5209}5210}
52105211
5211// Likewise, it works when the destination type is an optional.5212// Likewise, it works when the destination type is an optional.
5212test "[N]T to ?[]const T" {5213test "[N]T to ?[]const T" {
5213 var x1: ?[]const u8 = "hello";5214 var x1: ?[]const u8 = "hello";
5214 var x2: ?[]const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 };5215 var x2: ?[]const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 };
5215 assert(std.mem.eql(u8, x1.?, x2.?));5216 expect(std.mem.eql(u8, x1.?, x2.?));
52165217
5217 var y: ?[]const f32 = &[2]f32{ 1.2, 3.4 };5218 var y: ?[]const f32 = &[2]f32{ 1.2, 3.4 };
5218 assert(y.?[0] == 1.2);5219 expect(y.?[0] == 1.2);
5219}5220}
52205221
5221// In this cast, the array length becomes the slice length.5222// In this cast, the array length becomes the slice length.
5222test "*[N]T to []T" {5223test "*[N]T to []T" {
5223 var buf: [5]u8 = "hello".*;5224 var buf: [5]u8 = "hello".*;
5224 const x: []u8 = &buf;5225 const x: []u8 = &buf;
5225 assert(std.mem.eql(u8, x, "hello"));5226 expect(std.mem.eql(u8, x, "hello"));
52265227
5227 const buf2 = [2]f32{ 1.2, 3.4 };5228 const buf2 = [2]f32{ 1.2, 3.4 };
5228 const x2: []const f32 = &buf2;5229 const x2: []const f32 = &buf2;
5229 assert(std.mem.eql(f32, x2, &[2]f32{ 1.2, 3.4 }));5230 expect(std.mem.eql(f32, x2, &[2]f32{ 1.2, 3.4 }));
5230}5231}
52315232
5232// Single-item pointers to arrays can be coerced to5233// Single-item pointers to arrays can be coerced to
...@@ -5234,7 +5235,7 @@ test "*[N]T to []T" {...@@ -5234,7 +5235,7 @@ test "*[N]T to []T" {
5234test "*[N]T to [*]T" {5235test "*[N]T to [*]T" {
5235 var buf: [5]u8 = "hello".*;5236 var buf: [5]u8 = "hello".*;
5236 const x: [*]u8 = &buf;5237 const x: [*]u8 = &buf;
5237 assert(x[4] == 'o');5238 expect(x[4] == 'o');
5238 // x[5] would be an uncaught out of bounds pointer dereference!5239 // x[5] would be an uncaught out of bounds pointer dereference!
5239}5240}
52405241
...@@ -5242,7 +5243,7 @@ test "*[N]T to [*]T" {...@@ -5242,7 +5243,7 @@ test "*[N]T to [*]T" {
5242test "*[N]T to ?[*]T" {5243test "*[N]T to ?[*]T" {
5243 var buf: [5]u8 = "hello".*;5244 var buf: [5]u8 = "hello".*;
5244 const x: ?[*]u8 = &buf;5245 const x: ?[*]u8 = &buf;
5245 assert(x.?[4] == 'o');5246 expect(x.?[4] == 'o');
5246}5247}
52475248
5248// Single-item pointers can be cast to len-1 single-item arrays.5249// Single-item pointers can be cast to len-1 single-item arrays.
...@@ -5250,7 +5251,7 @@ test "*T to *[1]T" {...@@ -5250,7 +5251,7 @@ test "*T to *[1]T" {
5250 var x: i32 = 1234;5251 var x: i32 = 1234;
5251 const y: *[1]i32 = &x;5252 const y: *[1]i32 = &x;
5252 const z: [*]i32 = y;5253 const z: [*]i32 = y;
5253 assert(z[0] == 1234);5254 expect(z[0] == 1234);
5254}5255}
5255 {#code_end#}5256 {#code_end#}
5256 {#see_also|C Pointers#}5257 {#see_also|C Pointers#}
...@@ -5261,27 +5262,27 @@ test "*T to *[1]T" {...@@ -5261,27 +5262,27 @@ test "*T to *[1]T" {
5261 </p>5262 </p>
5262 {#code_begin|test#}5263 {#code_begin|test#}
5263const std = @import("std");5264const std = @import("std");
5264const assert = std.debug.assert;5265const expect = std.testing.expect;
52655266
5266test "coerce to optionals" {5267test "coerce to optionals" {
5267 const x: ?i32 = 1234;5268 const x: ?i32 = 1234;
5268 const y: ?i32 = null;5269 const y: ?i32 = null;
52695270
5270 assert(x.? == 1234);5271 expect(x.? == 1234);
5271 assert(y == null);5272 expect(y == null);
5272}5273}
5273 {#code_end#}5274 {#code_end#}
5274 <p>It works nested inside the {#link|Error Union Type#}, too:</p>5275 <p>It works nested inside the {#link|Error Union Type#}, too:</p>
5275 {#code_begin|test#}5276 {#code_begin|test#}
5276const std = @import("std");5277const std = @import("std");
5277const assert = std.debug.assert;5278const expect = std.testing.expect;
52785279
5279test "coerce to optionals wrapped in error union" {5280test "coerce to optionals wrapped in error union" {
5280 const x: anyerror!?i32 = 1234;5281 const x: anyerror!?i32 = 1234;
5281 const y: anyerror!?i32 = null;5282 const y: anyerror!?i32 = null;
52825283
5283 assert((try x).? == 1234);5284 expect((try x).? == 1234);
5284 assert((try y) == null);5285 expect((try y) == null);
5285}5286}
5286 {#code_end#}5287 {#code_end#}
5287 {#header_close#}5288 {#header_close#}
...@@ -5291,13 +5292,13 @@ test "coerce to optionals wrapped in error union" {...@@ -5291,13 +5292,13 @@ test "coerce to optionals wrapped in error union" {
5291 </p>5292 </p>
5292 {#code_begin|test#}5293 {#code_begin|test#}
5293const std = @import("std");5294const std = @import("std");
5294const assert = std.debug.assert;5295const expect = std.testing.expect;
52955296
5296test "coercion to error unions" {5297test "coercion to error unions" {
5297 const x: anyerror!i32 = 1234;5298 const x: anyerror!i32 = 1234;
5298 const y: anyerror!i32 = error.Failure;5299 const y: anyerror!i32 = error.Failure;
52995300
5300 assert((try x) == 1234);5301 expect((try x) == 1234);
5301 std.testing.expectError(error.Failure, y);5302 std.testing.expectError(error.Failure, y);
5302}5303}
5303 {#code_end#}5304 {#code_end#}
...@@ -5308,12 +5309,12 @@ test "coercion to error unions" {...@@ -5308,12 +5309,12 @@ test "coercion to error unions" {
5308 </p>5309 </p>
5309 {#code_begin|test#}5310 {#code_begin|test#}
5310const std = @import("std");5311const std = @import("std");
5311const assert = std.debug.assert;5312const expect = std.testing.expect;
53125313
5313test "coercing large integer type to smaller one when value is comptime known to fit" {5314test "coercing large integer type to smaller one when value is comptime known to fit" {
5314 const x: u64 = 255;5315 const x: u64 = 255;
5315 const y: u8 = x;5316 const y: u8 = x;
5316 assert(y == 255);5317 expect(y == 255);
5317}5318}
5318 {#code_end#}5319 {#code_end#}
5319 {#header_close#}5320 {#header_close#}
...@@ -5324,7 +5325,7 @@ test "coercing large integer type to smaller one when value is comptime known to...@@ -5324,7 +5325,7 @@ test "coercing large integer type to smaller one when value is comptime known to
5324 </p>5325 </p>
5325 {#code_begin|test#}5326 {#code_begin|test#}
5326const std = @import("std");5327const std = @import("std");
5327const assert = std.debug.assert;5328const expect = std.testing.expect;
53285329
5329const E = enum {5330const E = enum {
5330 one,5331 one,
...@@ -5341,11 +5342,11 @@ const U = union(E) {...@@ -5341,11 +5342,11 @@ const U = union(E) {
5341test "coercion between unions and enums" {5342test "coercion between unions and enums" {
5342 var u = U{ .two = 12.34 };5343 var u = U{ .two = 12.34 };
5343 var e: E = u;5344 var e: E = u;
5344 assert(e == E.two);5345 expect(e == E.two);
53455346
5346 const three = E.three;5347 const three = E.three;
5347 var another_u: U = three;5348 var another_u: U = three;
5348 assert(another_u == E.three);5349 expect(another_u == E.three);
5349}5350}
5350 {#code_end#}5351 {#code_end#}
5351 {#see_also|union|enum#}5352 {#see_also|union|enum#}
...@@ -5411,22 +5412,22 @@ test "coercion of zero bit types" {...@@ -5411,22 +5412,22 @@ test "coercion of zero bit types" {
5411 </p>5412 </p>
5412 {#code_begin|test|peer_type_resolution#}5413 {#code_begin|test|peer_type_resolution#}
5413const std = @import("std");5414const std = @import("std");
5414const assert = std.debug.assert;5415const expect = std.testing.expect;
5415const mem = std.mem;5416const mem = std.mem;
54165417
5417test "peer resolve int widening" {5418test "peer resolve int widening" {
5418 var a: i8 = 12;5419 var a: i8 = 12;
5419 var b: i16 = 34;5420 var b: i16 = 34;
5420 var c = a + b;5421 var c = a + b;
5421 assert(c == 46);5422 expect(c == 46);
5422 assert(@TypeOf(c) == i16);5423 expect(@TypeOf(c) == i16);
5423}5424}
54245425
5425test "peer resolve arrays of different size to const slice" {5426test "peer resolve arrays of different size to const slice" {
5426 assert(mem.eql(u8, boolToStr(true), "true"));5427 expect(mem.eql(u8, boolToStr(true), "true"));
5427 assert(mem.eql(u8, boolToStr(false), "false"));5428 expect(mem.eql(u8, boolToStr(false), "false"));
5428 comptime assert(mem.eql(u8, boolToStr(true), "true"));5429 comptime expect(mem.eql(u8, boolToStr(true), "true"));
5429 comptime assert(mem.eql(u8, boolToStr(false), "false"));5430 comptime expect(mem.eql(u8, boolToStr(false), "false"));
5430}5431}
5431fn boolToStr(b: bool) []const u8 {5432fn boolToStr(b: bool) []const u8 {
5432 return if (b) "true" else "false";5433 return if (b) "true" else "false";
...@@ -5439,16 +5440,16 @@ test "peer resolve array and const slice" {...@@ -5439,16 +5440,16 @@ test "peer resolve array and const slice" {
5439fn testPeerResolveArrayConstSlice(b: bool) void {5440fn testPeerResolveArrayConstSlice(b: bool) void {
5440 const value1 = if (b) "aoeu" else @as([]const u8, "zz");5441 const value1 = if (b) "aoeu" else @as([]const u8, "zz");
5441 const value2 = if (b) @as([]const u8, "zz") else "aoeu";5442 const value2 = if (b) @as([]const u8, "zz") else "aoeu";
5442 assert(mem.eql(u8, value1, "aoeu"));5443 expect(mem.eql(u8, value1, "aoeu"));
5443 assert(mem.eql(u8, value2, "zz"));5444 expect(mem.eql(u8, value2, "zz"));
5444}5445}
54455446
5446test "peer type resolution: ?T and T" {5447test "peer type resolution: ?T and T" {
5447 assert(peerTypeTAndOptionalT(true, false).? == 0);5448 expect(peerTypeTAndOptionalT(true, false).? == 0);
5448 assert(peerTypeTAndOptionalT(false, false).? == 3);5449 expect(peerTypeTAndOptionalT(false, false).? == 3);
5449 comptime {5450 comptime {
5450 assert(peerTypeTAndOptionalT(true, false).? == 0);5451 expect(peerTypeTAndOptionalT(true, false).? == 0);
5451 assert(peerTypeTAndOptionalT(false, false).? == 3);5452 expect(peerTypeTAndOptionalT(false, false).? == 3);
5452 }5453 }
5453}5454}
5454fn peerTypeTAndOptionalT(c: bool, b: bool) ?usize {5455fn peerTypeTAndOptionalT(c: bool, b: bool) ?usize {
...@@ -5460,11 +5461,11 @@ fn peerTypeTAndOptionalT(c: bool, b: bool) ?usize {...@@ -5460,11 +5461,11 @@ fn peerTypeTAndOptionalT(c: bool, b: bool) ?usize {
5460}5461}
54615462
5462test "peer type resolution: *[0]u8 and []const u8" {5463test "peer type resolution: *[0]u8 and []const u8" {
5463 assert(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);5464 expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);
5464 assert(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);5465 expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);
5465 comptime {5466 comptime {
5466 assert(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);5467 expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);
5467 assert(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);5468 expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);
5468 }5469 }
5469}5470}
5470fn peerTypeEmptyArrayAndSlice(a: bool, slice: []const u8) []const u8 {5471fn peerTypeEmptyArrayAndSlice(a: bool, slice: []const u8) []const u8 {
...@@ -5478,14 +5479,14 @@ test "peer type resolution: *[0]u8, []const u8, and anyerror![]u8" {...@@ -5478,14 +5479,14 @@ test "peer type resolution: *[0]u8, []const u8, and anyerror![]u8" {
5478 {5479 {
5479 var data = "hi".*;5480 var data = "hi".*;
5480 const slice = data[0..];5481 const slice = data[0..];
5481 assert((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0);5482 expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0);
5482 assert((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1);5483 expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1);
5483 }5484 }
5484 comptime {5485 comptime {
5485 var data = "hi".*;5486 var data = "hi".*;
5486 const slice = data[0..];5487 const slice = data[0..];
5487 assert((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0);5488 expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0);
5488 assert((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1);5489 expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1);
5489 }5490 }
5490}5491}
5491fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) anyerror![]u8 {5492fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) anyerror![]u8 {
...@@ -5499,8 +5500,8 @@ fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) anyerror![]u8 {...@@ -5499,8 +5500,8 @@ fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) anyerror![]u8 {
5499test "peer type resolution: *const T and ?*T" {5500test "peer type resolution: *const T and ?*T" {
5500 const a = @intToPtr(*const usize, 0x123456780);5501 const a = @intToPtr(*const usize, 0x123456780);
5501 const b = @intToPtr(?*usize, 0x123456780);5502 const b = @intToPtr(?*usize, 0x123456780);
5502 assert(a == b);5503 expect(a == b);
5503 assert(b == a);5504 expect(b == a);
5504}5505}
5505 {#code_end#}5506 {#code_end#}
5506 {#header_close#}5507 {#header_close#}
...@@ -5547,7 +5548,7 @@ export fn entry() void {...@@ -5547,7 +5548,7 @@ export fn entry() void {
5547 </p>5548 </p>
5548 {#code_begin|test#}5549 {#code_begin|test#}
5549const std = @import("std");5550const std = @import("std");
5550const assert = std.debug.assert;5551const expect = std.testing.expect;
55515552
5552test "turn HashMap into a set with void" {5553test "turn HashMap into a set with void" {
5553 var map = std.AutoHashMap(i32, void).init(std.testing.allocator);5554 var map = std.AutoHashMap(i32, void).init(std.testing.allocator);
...@@ -5556,11 +5557,11 @@ test "turn HashMap into a set with void" {...@@ -5556,11 +5557,11 @@ test "turn HashMap into a set with void" {
5556 try map.put(1, {});5557 try map.put(1, {});
5557 try map.put(2, {});5558 try map.put(2, {});
55585559
5559 assert(map.contains(2));5560 expect(map.contains(2));
5560 assert(!map.contains(3));5561 expect(!map.contains(3));
55615562
5562 _ = map.remove(2);5563 _ = map.remove(2);
5563 assert(!map.contains(2));5564 expect(!map.contains(2));
5564}5565}
5565 {#code_end#}5566 {#code_end#}
5566 <p>Note that this is different from using a dummy value for the hash map value.5567 <p>Note that this is different from using a dummy value for the hash map value.
...@@ -5607,7 +5608,7 @@ fn foo() i32 {...@@ -5607,7 +5608,7 @@ fn foo() i32 {
5607 <p>Pointers to zero bit types also have zero bits. They always compare equal to each other:</p>5608 <p>Pointers to zero bit types also have zero bits. They always compare equal to each other:</p>
5608 {#code_begin|test#}5609 {#code_begin|test#}
5609const std = @import("std");5610const std = @import("std");
5610const assert = std.debug.assert;5611const expect = std.testing.expect;
56115612
5612test "pointer to empty struct" {5613test "pointer to empty struct" {
5613 const Empty = struct {};5614 const Empty = struct {};
...@@ -5615,7 +5616,7 @@ test "pointer to empty struct" {...@@ -5615,7 +5616,7 @@ test "pointer to empty struct" {
5615 var b = Empty{};5616 var b = Empty{};
5616 var ptr_a = &a;5617 var ptr_a = &a;
5617 var ptr_b = &b;5618 var ptr_b = &b;
5618 comptime assert(ptr_a == ptr_b);5619 comptime expect(ptr_a == ptr_b);
5619}5620}
5620 {#code_end#}5621 {#code_end#}
5621 <p>The type being pointed to can only ever be one value; therefore loads and stores are5622 <p>The type being pointed to can only ever be one value; therefore loads and stores are
...@@ -5650,7 +5651,7 @@ test "@intToPtr for pointer to zero bit type" {...@@ -5650,7 +5651,7 @@ test "@intToPtr for pointer to zero bit type" {
5650usingnamespace @import("std");5651usingnamespace @import("std");
56515652
5652test "using std namespace" {5653test "using std namespace" {
5653 debug.assert(true);5654 testing.expect(true);
5654}5655}
5655 {#code_end#}5656 {#code_end#}
5656 <p>5657 <p>
...@@ -5762,7 +5763,7 @@ fn max(comptime T: type, a: T, b: T) T {...@@ -5762,7 +5763,7 @@ fn max(comptime T: type, a: T, b: T) T {
5762 }5763 }
5763}5764}
5764test "try to compare bools" {5765test "try to compare bools" {
5765 @import("std").debug.assert(max(bool, false, true) == true);5766 @import("std").testing.expect(max(bool, false, true) == true);
5766}5767}
5767 {#code_end#}5768 {#code_end#}
5768 <p>5769 <p>
...@@ -5802,7 +5803,7 @@ fn max(a: bool, b: bool) bool {...@@ -5802,7 +5803,7 @@ fn max(a: bool, b: bool) bool {
5802 For example:5803 For example:
5803 </p>5804 </p>
5804 {#code_begin|test|comptime_vars#}5805 {#code_begin|test|comptime_vars#}
5805const assert = @import("std").debug.assert;5806const expect = @import("std").testing.expect;
58065807
5807const CmdFn = struct {5808const CmdFn = struct {
5808 name: []const u8,5809 name: []const u8,
...@@ -5830,9 +5831,9 @@ fn performFn(comptime prefix_char: u8, start_value: i32) i32 {...@@ -5830,9 +5831,9 @@ fn performFn(comptime prefix_char: u8, start_value: i32) i32 {
5830}5831}
58315832
5832test "perform fn" {5833test "perform fn" {
5833 assert(performFn('t', 1) == 6);5834 expect(performFn('t', 1) == 6);
5834 assert(performFn('o', 0) == 1);5835 expect(performFn('o', 0) == 1);
5835 assert(performFn('w', 99) == 99);5836 expect(performFn('w', 99) == 99);
5836}5837}
5837 {#code_end#}5838 {#code_end#}
5838 <p>5839 <p>
...@@ -5843,7 +5844,7 @@ test "perform fn" {...@@ -5843,7 +5844,7 @@ test "perform fn" {
5843 </p>5844 </p>
5844 {#code_begin|syntax#}5845 {#code_begin|syntax#}
5845// From the line:5846// From the line:
5846// assert(performFn('t', 1) == 6);5847// expect(performFn('t', 1) == 6);
5847fn performFn(start_value: i32) i32 {5848fn performFn(start_value: i32) i32 {
5848 var result: i32 = start_value;5849 var result: i32 = start_value;
5849 result = two(result);5850 result = two(result);
...@@ -5853,7 +5854,7 @@ fn performFn(start_value: i32) i32 {...@@ -5853,7 +5854,7 @@ fn performFn(start_value: i32) i32 {
5853 {#code_end#}5854 {#code_end#}
5854 {#code_begin|syntax#}5855 {#code_begin|syntax#}
5855// From the line:5856// From the line:
5856// assert(performFn('o', 0) == 1);5857// expect(performFn('o', 0) == 1);
5857fn performFn(start_value: i32) i32 {5858fn performFn(start_value: i32) i32 {
5858 var result: i32 = start_value;5859 var result: i32 = start_value;
5859 result = one(result);5860 result = one(result);
...@@ -5862,7 +5863,7 @@ fn performFn(start_value: i32) i32 {...@@ -5862,7 +5863,7 @@ fn performFn(start_value: i32) i32 {
5862 {#code_end#}5863 {#code_end#}
5863 {#code_begin|syntax#}5864 {#code_begin|syntax#}
5864// From the line:5865// From the line:
5865// assert(performFn('w', 99) == 99);5866// expect(performFn('w', 99) == 99);
5866fn performFn(start_value: i32) i32 {5867fn performFn(start_value: i32) i32 {
5867 var result: i32 = start_value;5868 var result: i32 = start_value;
5868 return result;5869 return result;
...@@ -5915,7 +5916,7 @@ test "foo" {...@@ -5915,7 +5916,7 @@ test "foo" {
5915 Let's look at an example:5916 Let's look at an example:
5916 </p>5917 </p>
5917 {#code_begin|test#}5918 {#code_begin|test#}
5918const assert = @import("std").debug.assert;5919const expect = @import("std").testing.expect;
59195920
5920fn fibonacci(index: u32) u32 {5921fn fibonacci(index: u32) u32 {
5921 if (index < 2) return index;5922 if (index < 2) return index;
...@@ -5924,11 +5925,11 @@ fn fibonacci(index: u32) u32 {...@@ -5924,11 +5925,11 @@ fn fibonacci(index: u32) u32 {
59245925
5925test "fibonacci" {5926test "fibonacci" {
5926 // test fibonacci at run-time5927 // test fibonacci at run-time
5927 assert(fibonacci(7) == 13);5928 expect(fibonacci(7) == 13);
59285929
5929 // test fibonacci at compile-time5930 // test fibonacci at compile-time
5930 comptime {5931 comptime {
5931 assert(fibonacci(7) == 13);5932 expect(fibonacci(7) == 13);
5932 }5933 }
5933}5934}
5934 {#code_end#}5935 {#code_end#}
...@@ -5936,7 +5937,7 @@ test "fibonacci" {...@@ -5936,7 +5937,7 @@ test "fibonacci" {
5936 Imagine if we had forgotten the base case of the recursive function and tried to run the tests:5937 Imagine if we had forgotten the base case of the recursive function and tried to run the tests:
5937 </p>5938 </p>
5938 {#code_begin|test_err|operation caused overflow#}5939 {#code_begin|test_err|operation caused overflow#}
5939const assert = @import("std").debug.assert;5940const expect = @import("std").testing.expect;
59405941
5941fn fibonacci(index: u32) u32 {5942fn fibonacci(index: u32) u32 {
5942 //if (index < 2) return index;5943 //if (index < 2) return index;
...@@ -5945,7 +5946,7 @@ fn fibonacci(index: u32) u32 {...@@ -5945,7 +5946,7 @@ fn fibonacci(index: u32) u32 {
59455946
5946test "fibonacci" {5947test "fibonacci" {
5947 comptime {5948 comptime {
5948 assert(fibonacci(7) == 13);5949 expect(fibonacci(7) == 13);
5949 }5950 }
5950}5951}
5951 {#code_end#}5952 {#code_end#}
...@@ -5959,7 +5960,7 @@ test "fibonacci" {...@@ -5959,7 +5960,7 @@ test "fibonacci" {
5959 But what would have happened if we used a signed integer?5960 But what would have happened if we used a signed integer?
5960 </p>5961 </p>
5961 {#code_begin|test_err|evaluation exceeded 1000 backwards branches#}5962 {#code_begin|test_err|evaluation exceeded 1000 backwards branches#}
5962const assert = @import("std").debug.assert;5963const expect = @import("std").testing.expect;
59635964
5964fn fibonacci(index: i32) i32 {5965fn fibonacci(index: i32) i32 {
5965 //if (index < 2) return index;5966 //if (index < 2) return index;
...@@ -5968,7 +5969,7 @@ fn fibonacci(index: i32) i32 {...@@ -5968,7 +5969,7 @@ fn fibonacci(index: i32) i32 {
59685969
5969test "fibonacci" {5970test "fibonacci" {
5970 comptime {5971 comptime {
5971 assert(fibonacci(7) == 13);5972 expect(fibonacci(7) == 13);
5972 }5973 }
5973}5974}
5974 {#code_end#}5975 {#code_end#}
...@@ -5979,10 +5980,10 @@ test "fibonacci" {...@@ -5979,10 +5980,10 @@ test "fibonacci" {
5979 {#link|@setEvalBranchQuota#} to change the default number 1000 to something else.5980 {#link|@setEvalBranchQuota#} to change the default number 1000 to something else.
5980 </p>5981 </p>
5981 <p>5982 <p>
5982 What if we fix the base case, but put the wrong value in the {#syntax#}assert{#endsyntax#} line?5983 What if we fix the base case, but put the wrong value in the {#syntax#}expect{#endsyntax#} line?
5983 </p>5984 </p>
5984 {#code_begin|test_err|unable to evaluate constant expression#}5985 {#code_begin|test_err|encountered @panic at compile-time#}
5985const assert = @import("std").debug.assert;5986const expect = @import("std").testing.expect;
59865987
5987fn fibonacci(index: i32) i32 {5988fn fibonacci(index: i32) i32 {
5988 if (index < 2) return index;5989 if (index < 2) return index;
...@@ -5991,16 +5992,15 @@ fn fibonacci(index: i32) i32 {...@@ -5991,16 +5992,15 @@ fn fibonacci(index: i32) i32 {
59915992
5992test "fibonacci" {5993test "fibonacci" {
5993 comptime {5994 comptime {
5994 assert(fibonacci(7) == 99999);5995 expect(fibonacci(7) == 99999);
5995 }5996 }
5996}5997}
5997 {#code_end#}5998 {#code_end#}
5998 <p>5999 <p>
5999 What happened is Zig started interpreting the {#syntax#}assert{#endsyntax#} function with the6000 What happened is Zig started interpreting the {#syntax#}expect{#endsyntax#} function with the
6000 parameter {#syntax#}ok{#endsyntax#} set to {#syntax#}false{#endsyntax#}. When the interpreter hit6001 parameter {#syntax#}ok{#endsyntax#} set to {#syntax#}false{#endsyntax#}. When the interpreter hit
6001 {#syntax#}unreachable{#endsyntax#} it emitted a compile error, because reaching unreachable6002 {#syntax#}@panic{#endsyntax#} it emitted a compile error because a panic during compile
6002 code is undefined behavior, and undefined behavior causes a compile error if it is detected6003 causes a compile error if it is detected at compile-time.
6003 at compile-time.
6004 </p>6004 </p>
60056005
6006 <p>6006 <p>
...@@ -6042,7 +6042,7 @@ fn sum(numbers: []const i32) i32 {...@@ -6042,7 +6042,7 @@ fn sum(numbers: []const i32) i32 {
6042}6042}
60436043
6044test "variable values" {6044test "variable values" {
6045 @import("std").debug.assert(sum_of_first_25_primes == 1060);6045 @import("std").testing.expect(sum_of_first_25_primes == 1060);
6046}6046}
6047 {#code_end#}6047 {#code_end#}
6048 <p>6048 <p>
...@@ -6435,7 +6435,7 @@ volatile (...@@ -6435,7 +6435,7 @@ volatile (
6435 {#code_begin|test|global-asm#}6435 {#code_begin|test|global-asm#}
6436 {#target_linux_x86_64#}6436 {#target_linux_x86_64#}
6437const std = @import("std");6437const std = @import("std");
6438const assert = std.debug.assert;6438const expect = std.testing.expect;
64396439
6440comptime {6440comptime {
6441 asm (6441 asm (
...@@ -6450,7 +6450,7 @@ comptime {...@@ -6450,7 +6450,7 @@ comptime {
6450extern fn my_func(a: i32, b: i32) i32;6450extern fn my_func(a: i32, b: i32) i32;
64516451
6452test "global assembly" {6452test "global assembly" {
6453 assert(my_func(12, 34) == 46);6453 expect(my_func(12, 34) == 46);
6454}6454}
6455 {#code_end#}6455 {#code_end#}
6456 {#header_close#}6456 {#header_close#}
...@@ -6485,13 +6485,13 @@ test "global assembly" {...@@ -6485,13 +6485,13 @@ test "global assembly" {
6485 </p>6485 </p>
6486 {#code_begin|test#}6486 {#code_begin|test#}
6487const std = @import("std");6487const std = @import("std");
6488const assert = std.debug.assert;6488const expect = std.testing.expect;
64896489
6490var x: i32 = 1;6490var x: i32 = 1;
64916491
6492test "suspend with no resume" {6492test "suspend with no resume" {
6493 var frame = async func();6493 var frame = async func();
6494 assert(x == 2);6494 expect(x == 2);
6495}6495}
64966496
6497fn func() void {6497fn func() void {
...@@ -6511,21 +6511,21 @@ fn func() void {...@@ -6511,21 +6511,21 @@ fn func() void {
6511 </p>6511 </p>
6512 {#code_begin|test#}6512 {#code_begin|test#}
6513const std = @import("std");6513const std = @import("std");
6514const assert = std.debug.assert;6514const expect = std.testing.expect;
65156515
6516var the_frame: anyframe = undefined;6516var the_frame: anyframe = undefined;
6517var result = false;6517var result = false;
65186518
6519test "async function suspend with block" {6519test "async function suspend with block" {
6520 _ = async testSuspendBlock();6520 _ = async testSuspendBlock();
6521 assert(!result);6521 expect(!result);
6522 resume the_frame;6522 resume the_frame;
6523 assert(result);6523 expect(result);
6524}6524}
65256525
6526fn testSuspendBlock() void {6526fn testSuspendBlock() void {
6527 suspend {6527 suspend {
6528 comptime assert(@TypeOf(@frame()) == *@Frame(testSuspendBlock));6528 comptime expect(@TypeOf(@frame()) == *@Frame(testSuspendBlock));
6529 the_frame = @frame();6529 the_frame = @frame();
6530 }6530 }
6531 result = true;6531 result = true;
...@@ -6549,12 +6549,12 @@ fn testSuspendBlock() void {...@@ -6549,12 +6549,12 @@ fn testSuspendBlock() void {
6549 </p>6549 </p>
6550 {#code_begin|test#}6550 {#code_begin|test#}
6551const std = @import("std");6551const std = @import("std");
6552const assert = std.debug.assert;6552const expect = std.testing.expect;
65536553
6554test "resume from suspend" {6554test "resume from suspend" {
6555 var my_result: i32 = 1;6555 var my_result: i32 = 1;
6556 _ = async testResumeFromSuspend(&my_result);6556 _ = async testResumeFromSuspend(&my_result);
6557 std.debug.assert(my_result == 2);6557 std.testing.expect(my_result == 2);
6558}6558}
6559fn testResumeFromSuspend(my_result: *i32) void {6559fn testResumeFromSuspend(my_result: *i32) void {
6560 suspend {6560 suspend {
...@@ -6578,7 +6578,7 @@ fn testResumeFromSuspend(my_result: *i32) void {...@@ -6578,7 +6578,7 @@ fn testResumeFromSuspend(my_result: *i32) void {
6578 </p>6578 </p>
6579 {#code_begin|test#}6579 {#code_begin|test#}
6580const std = @import("std");6580const std = @import("std");
6581const assert = std.debug.assert;6581const expect = std.testing.expect;
65826582
6583test "async and await" {6583test "async and await" {
6584 // Here we have an exception where we do not match an async6584 // Here we have an exception where we do not match an async
...@@ -6592,7 +6592,7 @@ test "async and await" {...@@ -6592,7 +6592,7 @@ test "async and await" {
65926592
6593fn amain() void {6593fn amain() void {
6594 var frame = async func();6594 var frame = async func();
6595 comptime assert(@TypeOf(frame) == @Frame(func));6595 comptime expect(@TypeOf(frame) == @Frame(func));
65966596
6597 const ptr: anyframe->void = &frame;6597 const ptr: anyframe->void = &frame;
6598 const any_ptr: anyframe = ptr;6598 const any_ptr: anyframe = ptr;
...@@ -6622,7 +6622,7 @@ fn func() void {...@@ -6622,7 +6622,7 @@ fn func() void {
6622 </p>6622 </p>
6623 {#code_begin|test#}6623 {#code_begin|test#}
6624const std = @import("std");6624const std = @import("std");
6625const assert = std.debug.assert;6625const expect = std.testing.expect;
66266626
6627var the_frame: anyframe = undefined;6627var the_frame: anyframe = undefined;
6628var final_result: i32 = 0;6628var final_result: i32 = 0;
...@@ -6633,8 +6633,8 @@ test "async function await" {...@@ -6633,8 +6633,8 @@ test "async function await" {
6633 seq('f');6633 seq('f');
6634 resume the_frame;6634 resume the_frame;
6635 seq('i');6635 seq('i');
6636 assert(final_result == 1234);6636 expect(final_result == 1234);
6637 assert(std.mem.eql(u8, &seq_points, "abcdefghi"));6637 expect(std.mem.eql(u8, &seq_points, "abcdefghi"));
6638}6638}
6639fn amain() void {6639fn amain() void {
6640 seq('b');6640 seq('b');
...@@ -6848,9 +6848,9 @@ fn readFile(allocator: *Allocator, filename: []const u8) ![]u8 {...@@ -6848,9 +6848,9 @@ fn readFile(allocator: *Allocator, filename: []const u8) ![]u8 {
6848 for the current target to match the C ABI. When the child type of a pointer has6848 for the current target to match the C ABI. When the child type of a pointer has
6849 this alignment, the alignment can be omitted from the type.6849 this alignment, the alignment can be omitted from the type.
6850 </p>6850 </p>
6851 <pre>{#syntax#}const assert = @import("std").debug.assert;6851 <pre>{#syntax#}const expect = @import("std").testing.expect;
6852comptime {6852comptime {
6853 assert(*u32 == *align(@alignOf(u32)) u32);6853 expect(*u32 == *align(@alignOf(u32)) u32);
6854}{#endsyntax#}</pre>6854}{#endsyntax#}</pre>
6855 <p>6855 <p>
6856 The result is a target-specific compile time constant. It is guaranteed to be6856 The result is a target-specific compile time constant. It is guaranteed to be
...@@ -6886,7 +6886,7 @@ comptime {...@@ -6886,7 +6886,7 @@ comptime {
6886 </p>6886 </p>
6887 {#code_begin|test#}6887 {#code_begin|test#}
6888const std = @import("std");6888const std = @import("std");
6889const assert = std.debug.assert;6889const expect = std.testing.expect;
68906890
6891test "async fn pointer in a struct field" {6891test "async fn pointer in a struct field" {
6892 var data: i32 = 1;6892 var data: i32 = 1;
...@@ -6896,9 +6896,9 @@ test "async fn pointer in a struct field" {...@@ -6896,9 +6896,9 @@ test "async fn pointer in a struct field" {
6896 var foo = Foo{ .bar = func };6896 var foo = Foo{ .bar = func };
6897 var bytes: [64]u8 align(@alignOf(@Frame(func))) = undefined;6897 var bytes: [64]u8 align(@alignOf(@Frame(func))) = undefined;
6898 const f = @asyncCall(&bytes, {}, foo.bar, .{&data});6898 const f = @asyncCall(&bytes, {}, foo.bar, .{&data});
6899 assert(data == 2);6899 expect(data == 2);
6900 resume f;6900 resume f;
6901 assert(data == 4);6901 expect(data == 4);
6902}6902}
69036903
6904fn func(y: *i32) void {6904fn func(y: *i32) void {
...@@ -7082,10 +7082,10 @@ fn func(y: *i32) void {...@@ -7082,10 +7082,10 @@ fn func(y: *i32) void {
7082 Calls a function, in the same way that invoking an expression with parentheses does:7082 Calls a function, in the same way that invoking an expression with parentheses does:
7083 </p>7083 </p>
7084 {#code_begin|test|call#}7084 {#code_begin|test|call#}
7085const assert = @import("std").debug.assert;7085const expect = @import("std").testing.expect;
70867086
7087test "noinline function call" {7087test "noinline function call" {
7088 assert(@call(.{}, add, .{3, 9}) == 12);7088 expect(@call(.{}, add, .{3, 9}) == 12);
7089}7089}
70907090
7091fn add(a: i32, b: i32) i32 {7091fn add(a: i32, b: i32) i32 {
...@@ -7544,14 +7544,14 @@ const Point = struct {...@@ -7544,14 +7544,14 @@ const Point = struct {
7544};7544};
75457545
7546test "field access by string" {7546test "field access by string" {
7547 const assert = std.debug.assert;7547 const expect = std.testing.expect;
7548 var p = Point {.x = 0, .y = 0};7548 var p = Point {.x = 0, .y = 0};
75497549
7550 @field(p, "x") = 4;7550 @field(p, "x") = 4;
7551 @field(p, "y") = @field(p, "x") + 1;7551 @field(p, "y") = @field(p, "x") + 1;
75527552
7553 assert(@field(p, "x") == 4);7553 expect(@field(p, "x") == 4);
7554 assert(@field(p, "y") == 5);7554 expect(@field(p, "y") == 5);
7555}7555}
7556 {#code_end#}7556 {#code_end#}
75577557
...@@ -7657,7 +7657,7 @@ fn func() void {...@@ -7657,7 +7657,7 @@ fn func() void {
7657 </p>7657 </p>
7658 {#code_begin|test#}7658 {#code_begin|test#}
7659const std = @import("std");7659const std = @import("std");
7660const assert = std.debug.assert;7660const expect = std.testing.expect;
76617661
7662const Foo = struct {7662const Foo = struct {
7663 nope: i32,7663 nope: i32,
...@@ -7667,16 +7667,16 @@ const Foo = struct {...@@ -7667,16 +7667,16 @@ const Foo = struct {
7667};7667};
76687668
7669test "@hasDecl" {7669test "@hasDecl" {
7670 assert(@hasDecl(Foo, "blah"));7670 expect(@hasDecl(Foo, "blah"));
76717671
7672 // Even though `hi` is private, @hasDecl returns true because this test is7672 // Even though `hi` is private, @hasDecl returns true because this test is
7673 // in the same file scope as Foo. It would return false if Foo was declared7673 // in the same file scope as Foo. It would return false if Foo was declared
7674 // in a different file.7674 // in a different file.
7675 assert(@hasDecl(Foo, "hi"));7675 expect(@hasDecl(Foo, "hi"));
76767676
7677 // @hasDecl is for declarations; not fields.7677 // @hasDecl is for declarations; not fields.
7678 assert(!@hasDecl(Foo, "nope"));7678 expect(!@hasDecl(Foo, "nope"));
7679 assert(!@hasDecl(Foo, "nope1234"));7679 expect(!@hasDecl(Foo, "nope1234"));
7680}7680}
7681 {#code_end#}7681 {#code_end#}
7682 {#see_also|@hasField#}7682 {#see_also|@hasField#}
...@@ -7851,14 +7851,14 @@ mem.set(u8, dest, c);{#endsyntax#}</pre>...@@ -7851,14 +7851,14 @@ mem.set(u8, dest, c);{#endsyntax#}</pre>
7851 {#code_begin|test#}7851 {#code_begin|test#}
7852const std = @import("std");7852const std = @import("std");
7853const builtin = @import("builtin");7853const builtin = @import("builtin");
7854const assert = std.debug.assert;7854const expect = std.testing.expect;
78557855
7856test "@wasmMemoryGrow" {7856test "@wasmMemoryGrow" {
7857 if (builtin.arch != .wasm32) return error.SkipZigTest;7857 if (builtin.arch != .wasm32) return error.SkipZigTest;
78587858
7859 var prev = @wasmMemorySize(0);7859 var prev = @wasmMemorySize(0);
7860 assert(prev == @wasmMemoryGrow(0, 1));7860 expect(prev == @wasmMemoryGrow(0, 1));
7861 assert(prev + 1 == @wasmMemorySize(0));7861 expect(prev + 1 == @wasmMemorySize(0));
7862}7862}
7863 {#code_end#}7863 {#code_end#}
7864 {#see_also|@wasmMemorySize#}7864 {#see_also|@wasmMemorySize#}
...@@ -8194,13 +8194,13 @@ test "@setRuntimeSafety" {...@@ -8194,13 +8194,13 @@ test "@setRuntimeSafety" {
8194 </p>8194 </p>
8195 {#code_begin|test#}8195 {#code_begin|test#}
8196const std = @import("std");8196const std = @import("std");
8197const assert = std.debug.assert;8197const expect = std.testing.expect;
81988198
8199test "vector @splat" {8199test "vector @splat" {
8200 const scalar: u32 = 5;8200 const scalar: u32 = 5;
8201 const result = @splat(4, scalar);8201 const result = @splat(4, scalar);
8202 comptime assert(@TypeOf(result) == std.meta.Vector(4, u32));8202 comptime expect(@TypeOf(result) == std.meta.Vector(4, u32));
8203 assert(std.mem.eql(u32, &@as([4]u32, result), &[_]u32{ 5, 5, 5, 5 }));8203 expect(std.mem.eql(u32, &@as([4]u32, result), &[_]u32{ 5, 5, 5, 5 }));
8204}8204}
8205 {#code_end#}8205 {#code_end#}
8206 <p>8206 <p>
...@@ -8410,12 +8410,12 @@ fn doTheTest() void {...@@ -8410,12 +8410,12 @@ fn doTheTest() void {
8410 </p>8410 </p>
8411 {#code_begin|test#}8411 {#code_begin|test#}
8412const std = @import("std");8412const std = @import("std");
8413const assert = std.debug.assert;8413const expect = std.testing.expect;
84148414
8415test "@This()" {8415test "@This()" {
8416 var items = [_]i32{ 1, 2, 3, 4 };8416 var items = [_]i32{ 1, 2, 3, 4 };
8417 const list = List(i32){ .items = items[0..] };8417 const list = List(i32){ .items = items[0..] };
8418 assert(list.length() == 4);8418 expect(list.length() == 4);
8419}8419}
84208420
8421fn List(comptime T: type) type {8421fn List(comptime T: type) type {
...@@ -8456,12 +8456,12 @@ test "integer cast panic" {...@@ -8456,12 +8456,12 @@ test "integer cast panic" {
8456 </p>8456 </p>
8457 {#code_begin|test|truncate#}8457 {#code_begin|test|truncate#}
8458const std = @import("std");8458const std = @import("std");
8459const assert = std.debug.assert;8459const expect = std.testing.expect;
84608460
8461test "integer truncation" {8461test "integer truncation" {
8462 var a: u16 = 0xabcd;8462 var a: u16 = 0xabcd;
8463 var b: u8 = @truncate(u8, a);8463 var b: u8 = @truncate(u8, a);
8464 assert(b == 0xcd);8464 expect(b == 0xcd);
8465}8465}
8466 {#code_end#}8466 {#code_end#}
8467 <p>8467 <p>
...@@ -8544,13 +8544,13 @@ test "integer truncation" {...@@ -8544,13 +8544,13 @@ test "integer truncation" {
8544 </p>8544 </p>
8545 {#code_begin|test#}8545 {#code_begin|test#}
8546const std = @import("std");8546const std = @import("std");
8547const assert = std.debug.assert;8547const expect = std.testing.expect;
85488548
8549test "no runtime side effects" {8549test "no runtime side effects" {
8550 var data: i32 = 0;8550 var data: i32 = 0;
8551 const T = @TypeOf(foo(i32, &data));8551 const T = @TypeOf(foo(i32, &data));
8552 comptime assert(T == i32);8552 comptime expect(T == i32);
8553 assert(data == 0);8553 expect(data == 0);
8554}8554}
85558555
8556fn foo(comptime T: type, ptr: *T) T {8556fn foo(comptime T: type, ptr: *T) T {
...@@ -8853,16 +8853,16 @@ pub fn main() void {...@@ -8853,16 +8853,16 @@ pub fn main() void {
8853 </ul>8853 </ul>
8854 {#code_begin|test#}8854 {#code_begin|test#}
8855const std = @import("std");8855const std = @import("std");
8856const assert = std.debug.assert;8856const expect = std.testing.expect;
8857const minInt = std.math.minInt;8857const minInt = std.math.minInt;
8858const maxInt = std.math.maxInt;8858const maxInt = std.math.maxInt;
88598859
8860test "wraparound addition and subtraction" {8860test "wraparound addition and subtraction" {
8861 const x: i32 = maxInt(i32);8861 const x: i32 = maxInt(i32);
8862 const min_val = x +% 1;8862 const min_val = x +% 1;
8863 assert(min_val == minInt(i32));8863 expect(min_val == minInt(i32));
8864 const max_val = min_val -% 1;8864 const max_val = min_val -% 1;
8865 assert(max_val == maxInt(i32));8865 expect(max_val == maxInt(i32));
8866}8866}
8867 {#code_end#}8867 {#code_end#}
8868 {#header_close#}8868 {#header_close#}
...@@ -9287,13 +9287,13 @@ pub fn main() void {...@@ -9287,13 +9287,13 @@ pub fn main() void {
9287 {#code_begin|test|allocator#}9287 {#code_begin|test|allocator#}
9288const std = @import("std");9288const std = @import("std");
9289const Allocator = std.mem.Allocator;9289const Allocator = std.mem.Allocator;
9290const assert = std.debug.assert;9290const expect = std.testing.expect;
92919291
9292test "using an allocator" {9292test "using an allocator" {
9293 var buffer: [100]u8 = undefined;9293 var buffer: [100]u8 = undefined;
9294 const allocator = &std.heap.FixedBufferAllocator.init(&buffer).allocator;9294 const allocator = &std.heap.FixedBufferAllocator.init(&buffer).allocator;
9295 const result = try concat(allocator, "foo", "bar");9295 const result = try concat(allocator, "foo", "bar");
9296 assert(std.mem.eql(u8, "foobar", result));9296 expect(std.mem.eql(u8, "foobar", result));
9297}9297}
92989298
9299fn concat(allocator: *Allocator, a: []const u8, b: []const u8) ![]u8 {9299fn concat(allocator: *Allocator, a: []const u8, b: []const u8) ![]u8 {
...@@ -9560,10 +9560,10 @@ const separator = if (builtin.os == builtin.Os.windows) '\\' else '/';...@@ -9560,10 +9560,10 @@ const separator = if (builtin.os == builtin.Os.windows) '\\' else '/';
9560 {#code_begin|test|detect_test#}9560 {#code_begin|test|detect_test#}
9561const std = @import("std");9561const std = @import("std");
9562const builtin = std.builtin;9562const builtin = std.builtin;
9563const assert = std.debug.assert;9563const expect = std.testing.expect;
95649564
9565test "builtin.is_test" {9565test "builtin.is_test" {
9566 assert(builtin.is_test);9566 expect(builtin.is_test);
9567}9567}
9568 {#code_end#}9568 {#code_end#}
9569 <p>9569 <p>
...@@ -9613,7 +9613,7 @@ test "assert in release fast mode" {...@@ -9613,7 +9613,7 @@ test "assert in release fast mode" {
9613const std = @import("std");9613const std = @import("std");
9614const expect = std.testing.expect;9614const expect = std.testing.expect;
96159615
9616test "assert in release fast mode" {9616test "expect in release fast mode" {
9617 expect(false);9617 expect(false);
9618}9618}
9619 {#code_end#}9619 {#code_end#}