| ... | @@ -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#} |
| 347 | const assert = @import("std").debug.assert; | 347 | const expect = @import("std").testing.expect; |
| 348 | | 348 | |
| 349 | test "comments" { | 349 | test "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. |
| 352 | | 352 | |
| 353 | //assert(false); | 353 | //expect(false); |
| 354 | | 354 | |
| 355 | const x = true; // another comment | 355 | 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#} |
| 698 | const assert = @import("std").debug.assert; | 698 | const expect = @import("std").testing.expect; |
| 699 | const mem = @import("std").mem; | 699 | const mem = @import("std").mem; |
| 700 | | 700 | |
| 701 | test "string literals" { | 701 | test "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#} |
| 803 | const assert = @import("std").debug.assert; | 803 | const expect = @import("std").testing.expect; |
| 804 | | 804 | |
| 805 | test "var" { | 805 | test "var" { |
| 806 | var y: i32 = 5678; | 806 | var y: i32 = 5678; |
| 807 | | 807 | |
| 808 | y += 1; | 808 | y += 1; |
| 809 | | 809 | |
| 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#} |
| 824 | const assert = @import("std").debug.assert; | 824 | const expect = @import("std").testing.expect; |
| 825 | | 825 | |
| 826 | test "init with undefined" { | 826 | test "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); |
| 868 | const x: i32 = add(12, 34); | 868 | const x: i32 = add(12, 34); |
| 869 | | 869 | |
| 870 | test "global variables" { | 870 | test "global variables" { |
| 871 | assert(x == 46); | 871 | expect(x == 46); |
| 872 | assert(y == 56); | 872 | expect(y == 56); |
| 873 | } | 873 | } |
| 874 | | 874 | |
| 875 | fn add(a: i32, b: i32) i32 { | 875 | fn 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 | } |
| 878 | | 878 | |
| 879 | const std = @import("std"); | 879 | const std = @import("std"); |
| 880 | const assert = std.debug.assert; | 880 | const 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#} |
| 886 | const std = @import("std"); | 886 | const std = @import("std"); |
| 887 | const assert = std.debug.assert; | 887 | const expect = std.testing.expect; |
| 888 | | 888 | |
| 889 | test "namespaced global variable" { | 889 | test "namespaced global variable" { |
| 890 | assert(foo() == 1235); | 890 | expect(foo() == 1235); |
| 891 | assert(foo() == 1236); | 891 | expect(foo() == 1236); |
| 892 | } | 892 | } |
| 893 | | 893 | |
| 894 | fn foo() i32 { | 894 | fn 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#} |
| 959 | const std = @import("std"); | 959 | const std = @import("std"); |
| 960 | const assert = std.debug.assert; | 960 | const expect = std.testing.expect; |
| 961 | | 961 | |
| 962 | test "comptime vars" { | 962 | test "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; |
| 968 | | 968 | |
| 969 | assert(x == 2); | 969 | expect(x == 2); |
| 970 | assert(y == 2); | 970 | expect(y == 2); |
| 971 | | 971 | |
| 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#} |
| 1760 | const assert = @import("std").debug.assert; | 1760 | const expect = @import("std").testing.expect; |
| 1761 | const mem = @import("std").mem; | 1761 | const mem = @import("std").mem; |
| 1762 | | 1762 | |
| 1763 | // array literal | 1763 | // 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' }; |
| 1765 | | 1765 | |
| 1766 | // get the size of an array | 1766 | // get the size of an array |
| 1767 | comptime { | 1767 | comptime { |
| 1768 | assert(message.len == 5); | 1768 | expect(message.len == 5); |
| 1769 | } | 1769 | } |
| 1770 | | 1770 | |
| 1771 | // A string literal is a pointer to an array literal. | 1771 | // A string literal is a pointer to an array literal. |
| 1772 | const same_message = "hello"; | 1772 | const same_message = "hello"; |
| 1773 | | 1773 | |
| 1774 | comptime { | 1774 | comptime { |
| 1775 | assert(mem.eql(u8, &message, same_message)); | 1775 | expect(mem.eql(u8, &message, same_message)); |
| 1776 | } | 1776 | } |
| 1777 | | 1777 | |
| 1778 | test "iterate over an array" { | 1778 | test "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 | } |
| 1785 | | 1785 | |
| 1786 | // modifiable array | 1786 | // 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 | } |
| 1796 | | 1796 | |
| 1797 | // array concatenation works if the values are known | 1797 | // 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 }; |
| 1800 | const part_two = [_]i32{ 5, 6, 7, 8 }; | 1800 | const part_two = [_]i32{ 5, 6, 7, 8 }; |
| 1801 | const all_of_it = part_one ++ part_two; | 1801 | const all_of_it = part_one ++ part_two; |
| 1802 | comptime { | 1802 | comptime { |
| 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 | } |
| 1805 | | 1805 | |
| 1806 | // remember that string literals are arrays | 1806 | // remember that string literals are arrays |
| ... | @@ -1808,21 +1808,21 @@ const hello = "hello"; | ... | @@ -1808,21 +1808,21 @@ const hello = "hello"; |
| 1808 | const world = "world"; | 1808 | const world = "world"; |
| 1809 | const hello_world = hello ++ " " ++ world; | 1809 | const hello_world = hello ++ " " ++ world; |
| 1810 | comptime { | 1810 | comptime { |
| 1811 | assert(mem.eql(u8, hello_world, "hello world")); | 1811 | expect(mem.eql(u8, hello_world, "hello world")); |
| 1812 | } | 1812 | } |
| 1813 | | 1813 | |
| 1814 | // ** does repeating patterns | 1814 | // ** does repeating patterns |
| 1815 | const pattern = "ab" ** 3; | 1815 | const pattern = "ab" ** 3; |
| 1816 | comptime { | 1816 | comptime { |
| 1817 | assert(mem.eql(u8, pattern, "ababab")); | 1817 | expect(mem.eql(u8, pattern, "ababab")); |
| 1818 | } | 1818 | } |
| 1819 | | 1819 | |
| 1820 | // initialize an array to zero | 1820 | // initialize an array to zero |
| 1821 | const all_zero = [_]u16{0} ** 10; | 1821 | const all_zero = [_]u16{0} ** 10; |
| 1822 | | 1822 | |
| 1823 | comptime { | 1823 | comptime { |
| 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 | } |
| 1827 | | 1827 | |
| 1828 | // use compile-time code to initialize an array | 1828 | // use compile-time code to initialize an array |
| ... | @@ -1842,8 +1842,8 @@ const Point = struct { | ... | @@ -1842,8 +1842,8 @@ const Point = struct { |
| 1842 | }; | 1842 | }; |
| 1843 | | 1843 | |
| 1844 | test "compile-time array initialization" { | 1844 | test "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 | } |
| 1848 | | 1848 | |
| 1849 | // call a function to initialize an array | 1849 | // 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 | } |
| 1857 | test "array initialization with function calls" { | 1857 | test "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#} |
| 1869 | const std = @import("std"); | 1869 | const std = @import("std"); |
| 1870 | const assert = std.debug.assert; | 1870 | const expect = std.testing.expect; |
| 1871 | | 1871 | |
| 1872 | test "anonymous list literal syntax" { | 1872 | test "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#} |
| 1885 | const std = @import("std"); | 1885 | const std = @import("std"); |
| 1886 | const assert = std.debug.assert; | 1886 | const expect = std.testing.expect; |
| 1887 | | 1887 | |
| 1888 | test "fully anonymous list literal" { | 1888 | test "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 | } |
| 1891 | | 1891 | |
| 1892 | fn dump(args: anytype) void { | 1892 | fn 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#} |
| 1907 | const std = @import("std"); | 1907 | const std = @import("std"); |
| 1908 | const assert = std.debug.assert; | 1908 | const expect = std.testing.expect; |
| 1909 | | 1909 | |
| 1910 | const mat4x4 = [4][4]f32{ | 1910 | const 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 | }; |
| 1916 | test "multidimensional arrays" { | 1916 | test "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); |
| 1919 | | 1919 | |
| 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#} |
| 1938 | const std = @import("std"); | 1938 | const std = @import("std"); |
| 1939 | const assert = std.debug.assert; | 1939 | const expect = std.testing.expect; |
| 1940 | | 1940 | |
| 1941 | test "null terminated array" { | 1941 | test "null terminated array" { |
| 1942 | const array = [_:0]u8 {1, 2, 3, 4}; | 1942 | const array = [_:0]u8 {1, 2, 3, 4}; |
| 1943 | | 1943 | |
| 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#} |
| 2016 | const assert = @import("std").debug.assert; | 2016 | const expect = @import("std").testing.expect; |
| 2017 | | 2017 | |
| 2018 | test "address of syntax" { | 2018 | test "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; |
| 2022 | | 2022 | |
| 2023 | // Dereference a pointer: | 2023 | // Dereference a pointer: |
| 2024 | assert(x_ptr.* == 1234); | 2024 | expect(x_ptr.* == 1234); |
| 2025 | | 2025 | |
| 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); |
| 2028 | | 2028 | |
| 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 | } |
| 2036 | | 2036 | |
| 2037 | test "pointer array access" { | 2037 | test "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); |
| 2044 | | 2044 | |
| 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#} |
| 2060 | const assert = @import("std").debug.assert; | 2060 | const expect = @import("std").testing.expect; |
| 2061 | | 2061 | |
| 2062 | test "pointer slicing" { | 2062 | test "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); |
| 2066 | | 2066 | |
| 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 on | 2072 | <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#} |
| 2075 | const assert = @import("std").debug.assert; | 2075 | const expect = @import("std").testing.expect; |
| 2076 | | 2076 | |
| 2077 | test "comptime pointers" { | 2077 | test "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#} |
| 2090 | const assert = @import("std").debug.assert; | 2090 | const expect = @import("std").testing.expect; |
| 2091 | | 2091 | |
| 2092 | test "@ptrToInt and @intToPtr" { | 2092 | test "@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 as | 2099 | <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#} |
| 2102 | const assert = @import("std").debug.assert; | 2102 | const expect = @import("std").testing.expect; |
| 2103 | | 2103 | |
| 2104 | test "comptime @intToPtr" { | 2104 | test "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 happen | 2119 | 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#} |
| 2122 | const assert = @import("std").debug.assert; | 2122 | const expect = @import("std").testing.expect; |
| 2123 | | 2123 | |
| 2124 | test "volatile" { | 2124 | test "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#} |
| 2141 | const std = @import("std"); | 2141 | const std = @import("std"); |
| 2142 | const assert = std.debug.assert; | 2142 | const expect = std.testing.expect; |
| 2143 | | 2143 | |
| 2144 | test "pointer casting" { | 2144 | test "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); |
| 2148 | | 2148 | |
| 2149 | // Even this example is contrived - there are better ways to do the above than | 2149 | // 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); |
| 2153 | | 2153 | |
| 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 | } |
| 2157 | | 2157 | |
| 2158 | test "pointer child type" { | 2158 | test "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#} |
| 2179 | const std = @import("std"); | 2179 | const std = @import("std"); |
| 2180 | const assert = std.debug.assert; | 2180 | const expect = std.testing.expect; |
| 2181 | | 2181 | |
| 2182 | test "variable alignment" { | 2182 | test "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#} |
| 2201 | const assert = @import("std").debug.assert; | 2201 | const expect = @import("std").testing.expect; |
| 2202 | | 2202 | |
| 2203 | var foo: u8 align(4) = 100; | 2203 | var foo: u8 align(4) = 100; |
| 2204 | | 2204 | |
| 2205 | test "global variable alignment" { | 2205 | test "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 | } |
| 2212 | | 2212 | |
| 2213 | fn derp() align(@sizeOf(usize) * 2) i32 { return 1234; } | 2213 | fn 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 {} |
| 2215 | fn noop4() align(4) void {} | 2215 | fn noop4() align(4) void {} |
| 2216 | | 2216 | |
| 2217 | test "function alignment" { | 2217 | test "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"); |
| 2234 | test "pointer alignment safety" { | 2234 | test "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 | } |
| 2239 | fn foo(bytes: []u8) u32 { | 2239 | fn 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#} |
| 2257 | const std = @import("std"); | 2257 | const std = @import("std"); |
| 2258 | const assert = std.debug.assert; | 2258 | const expect = std.testing.expect; |
| 2259 | | 2259 | |
| 2260 | test "allowzero" { | 2260 | test "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 { |
| 2292 | | 2292 | |
| 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#} |
| 2295 | const assert = @import("std").debug.assert; | 2295 | const expect = @import("std").testing.expect; |
| 2296 | | 2296 | |
| 2297 | test "basic slices" { | 2297 | test "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); |
| 2307 | | 2307 | |
| 2308 | // Using the address-of operator on a slice gives a pointer to a single | 2308 | // 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])); |
| 2313 | | 2313 | |
| 2314 | // Slices have array bounds checking. If you try to access something out | 2314 | // 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#} |
| 2324 | const std = @import("std"); | 2324 | const std = @import("std"); |
| 2325 | const assert = std.debug.assert; | 2325 | const expect = std.testing.expect; |
| 2326 | const mem = std.mem; | 2326 | const mem = std.mem; |
| 2327 | const fmt = std.fmt; | 2327 | const fmt = std.fmt; |
| 2328 | | 2328 | |
| ... | @@ -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 a | 2343 | // 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 need | 2344 | // 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 | } |
| 2348 | | 2348 | |
| 2349 | test "slice pointer" { | 2349 | test "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 start | 2358 | // 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); |
| 2361 | | 2361 | |
| 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#} |
| 2378 | const std = @import("std"); | 2378 | const std = @import("std"); |
| 2379 | const assert = std.debug.assert; | 2379 | const expect = std.testing.expect; |
| 2380 | | 2380 | |
| 2381 | test "null terminated slice" { | 2381 | test "null terminated slice" { |
| 2382 | const slice: [:0]const u8 = "hello"; | 2382 | const slice: [:0]const u8 = "hello"; |
| 2383 | | 2383 | |
| 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 | }; |
| 2442 | | 2442 | |
| 2443 | const assert = @import("std").debug.assert; | 2443 | const expect = @import("std").testing.expect; |
| 2444 | test "dot product" { | 2444 | test "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); |
| 2448 | | 2448 | |
| 2449 | // Other than being available to call with dot syntax, struct methods are | 2449 | // Other than being available to call with dot syntax, struct methods are |
| 2450 | // not special. You can reference them as any other declaration inside | 2450 | // 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 | } |
| 2454 | | 2454 | |
| 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 | }; |
| 2460 | test "struct namespaced variable" { | 2460 | test "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); |
| 2463 | | 2463 | |
| 2464 | // you can still instantiate an empty struct | 2464 | // 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 | } |
| 2482 | | 2482 | |
| 2483 | // You can return a struct from a function. This is how we do generics | 2483 | // 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 { |
| 2499 | test "linked list" { | 2499 | test "linked list" { |
| 2500 | // Functions called at compile-time are memoized. This means you can | 2500 | // 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)); |
| 2503 | | 2503 | |
| 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); |
| 2510 | | 2510 | |
| 2511 | // Since types are first class values you can instantiate the type | 2511 | // 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)); |
| 2515 | | 2515 | |
| 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#} |
| 2529 | | 2529 | |
| ... | @@ -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#} |
| 2585 | const std = @import("std"); | 2585 | const std = @import("std"); |
| 2586 | const builtin = std.builtin; | 2586 | const builtin = std.builtin; |
| 2587 | const assert = std.debug.assert; | 2587 | const expect = std.testing.expect; |
| 2588 | | 2588 | |
| 2589 | const Full = packed struct { | 2589 | const 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 | } |
| 2602 | | 2602 | |
| 2603 | fn doTheTest() void { | 2603 | fn 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#} |
| 2626 | const std = @import("std"); | 2626 | const std = @import("std"); |
| 2627 | const assert = std.debug.assert; | 2627 | const expect = std.testing.expect; |
| 2628 | | 2628 | |
| 2629 | const BitField = packed struct { | 2629 | const BitField = packed struct { |
| 2630 | a: u3, | 2630 | a: u3, |
| ... | @@ -2640,7 +2640,7 @@ var foo = BitField{ | ... | @@ -2640,7 +2640,7 @@ var foo = BitField{ |
| 2640 | | 2640 | |
| 2641 | test "pointer to non-byte-aligned field" { | 2641 | test "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#} |
| 2651 | const std = @import("std"); | 2651 | const std = @import("std"); |
| 2652 | const assert = std.debug.assert; | 2652 | const expect = std.testing.expect; |
| 2653 | | 2653 | |
| 2654 | const BitField = packed struct { | 2654 | const 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 | }; |
| 2665 | | 2665 | |
| 2666 | test "pointer to non-bit-aligned field" { | 2666 | test "pointer to non-bit-aligned field" { |
| 2667 | assert(bar(&bit_field.b) == 2); | 2667 | expect(bar(&bit_field.b) == 2); |
| 2668 | } | 2668 | } |
| 2669 | | 2669 | |
| 2670 | fn bar(x: *const u3) u3 { | 2670 | fn 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#} |
| 2682 | const std = @import("std"); | 2682 | const std = @import("std"); |
| 2683 | const assert = std.debug.assert; | 2683 | const expect = std.testing.expect; |
| 2684 | | 2684 | |
| 2685 | const BitField = packed struct { | 2685 | const 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 | }; |
| 2696 | | 2696 | |
| 2697 | test "pointer to non-bit-aligned field" { | 2697 | test "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#} |
| 2706 | const std = @import("std"); | 2706 | const std = @import("std"); |
| 2707 | const assert = std.debug.assert; | 2707 | const expect = std.testing.expect; |
| 2708 | | 2708 | |
| 2709 | const BitField = packed struct { | 2709 | const 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 { |
| 2714 | | 2714 | |
| 2715 | test "pointer to non-bit-aligned field" { | 2715 | test "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); |
| 2720 | | 2720 | |
| 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#} |
| 2793 | const std = @import("std"); | 2793 | const std = @import("std"); |
| 2794 | const assert = std.debug.assert; | 2794 | const expect = std.testing.expect; |
| 2795 | | 2795 | |
| 2796 | const Point = struct {x: i32, y: i32}; | 2796 | const Point = struct {x: i32, y: i32}; |
| 2797 | | 2797 | |
| ... | @@ -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#} |
| 2812 | const std = @import("std"); | 2812 | const std = @import("std"); |
| 2813 | const assert = std.debug.assert; | 2813 | const expect = std.testing.expect; |
| 2814 | | 2814 | |
| 2815 | test "fully anonymous struct" { | 2815 | test "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 | } |
| 2823 | | 2823 | |
| 2824 | fn dump(args: anytype) void { | 2824 | fn 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#} |
| 2837 | const assert = @import("std").debug.assert; | 2837 | const expect = @import("std").testing.expect; |
| 2838 | const mem = @import("std").mem; | 2838 | const mem = @import("std").mem; |
| 2839 | | 2839 | |
| 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. |
| 2859 | test "enum ordinal value" { | 2859 | test "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 | } |
| 2864 | | 2864 | |
| 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 | }; |
| 2871 | test "set enum ordinal value" { | 2871 | test "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 | } |
| 2876 | | 2876 | |
| 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 | }; |
| 2890 | test "enum method" { | 2890 | test "enum method" { |
| 2891 | const p = Suit.spades; | 2891 | const p = Suit.spades; |
| 2892 | assert(!p.isClubs()); | 2892 | expect(!p.isClubs()); |
| 2893 | } | 2893 | } |
| 2894 | | 2894 | |
| 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 | } |
| 2910 | | 2910 | |
| 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 | }; |
| 2918 | test "@TagType" { | 2918 | test "@TagType" { |
| 2919 | assert(@TagType(Small) == u2); | 2919 | expect(@TagType(Small) == u2); |
| 2920 | } | 2920 | } |
| 2921 | | 2921 | |
| 2922 | // @typeInfo tells us the field count and the fields names: | 2922 | // @typeInfo tells us the field count and the fields names: |
| 2923 | test "@typeInfo" { | 2923 | test "@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 | } |
| 2927 | | 2927 | |
| 2928 | // @tagName gives a []const u8 representation of an enum value: | 2928 | // @tagName gives a []const u8 representation of an enum value: |
| 2929 | test "@tagName" { | 2929 | test "@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#} |
| 2976 | const std = @import("std"); | 2976 | const std = @import("std"); |
| 2977 | const assert = std.debug.assert; | 2977 | const expect = std.testing.expect; |
| 2978 | | 2978 | |
| 2979 | const Color = enum { | 2979 | const Color = enum { |
| 2980 | auto, | 2980 | auto, |
| ... | @@ -2985,7 +2985,7 @@ const Color = enum { | ... | @@ -2985,7 +2985,7 @@ const Color = enum { |
| 2985 | test "enum literals" { | 2985 | test "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 | } |
| 2990 | | 2990 | |
| 2991 | test "switch using enum literals" { | 2991 | test "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#} |
| 3016 | const std = @import("std"); | 3016 | const std = @import("std"); |
| 3017 | const assert = std.debug.assert; | 3017 | const expect = std.testing.expect; |
| 3018 | | 3018 | |
| 3019 | const Number = enum(u8) { | 3019 | const 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#} |
| 3069 | const std = @import("std"); | 3069 | const std = @import("std"); |
| 3070 | const assert = std.debug.assert; | 3070 | const expect = std.testing.expect; |
| 3071 | | 3071 | |
| 3072 | const Payload = union { | 3072 | const Payload = union { |
| 3073 | int: i64, | 3073 | int: i64, |
| ... | @@ -3076,9 +3076,9 @@ const Payload = union { | ... | @@ -3076,9 +3076,9 @@ const Payload = union { |
| 3076 | }; | 3076 | }; |
| 3077 | test "simple union" { | 3077 | test "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#} |
| 3099 | const std = @import("std"); | 3099 | const std = @import("std"); |
| 3100 | const assert = std.debug.assert; | 3100 | const expect = std.testing.expect; |
| 3101 | | 3101 | |
| 3102 | const ComplexTypeTag = enum { | 3102 | const ComplexTypeTag = enum { |
| 3103 | ok, | 3103 | ok, |
| ... | @@ -3110,24 +3110,24 @@ const ComplexType = union(ComplexTypeTag) { | ... | @@ -3110,24 +3110,24 @@ const ComplexType = union(ComplexTypeTag) { |
| 3110 | | 3110 | |
| 3111 | test "switch on tagged union" { | 3111 | test "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); |
| 3114 | | 3114 | |
| 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 | } |
| 3120 | | 3120 | |
| 3121 | test "@TagType" { | 3121 | test "@TagType" { |
| 3122 | assert(@TagType(ComplexType) == ComplexTypeTag); | 3122 | expect(@TagType(ComplexType) == ComplexTypeTag); |
| 3123 | } | 3123 | } |
| 3124 | | 3124 | |
| 3125 | test "coerce to enum" { | 3125 | test "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; |
| 3128 | | 3128 | |
| 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#} |
| 3137 | const std = @import("std"); | 3137 | const std = @import("std"); |
| 3138 | const assert = std.debug.assert; | 3138 | const expect = std.testing.expect; |
| 3139 | | 3139 | |
| 3140 | const ComplexTypeTag = enum { | 3140 | const ComplexTypeTag = enum { |
| 3141 | ok, | 3141 | ok, |
| ... | @@ -3148,14 +3148,14 @@ const ComplexType = union(ComplexTypeTag) { | ... | @@ -3148,14 +3148,14 @@ const ComplexType = union(ComplexTypeTag) { |
| 3148 | | 3148 | |
| 3149 | test "modify tagged union in switch" { | 3149 | test "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); |
| 3152 | | 3152 | |
| 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 | } |
| 3157 | | 3157 | |
| 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#} |
| 3166 | const std = @import("std"); | 3166 | const std = @import("std"); |
| 3167 | const assert = std.debug.assert; | 3167 | const expect = std.testing.expect; |
| 3168 | | 3168 | |
| 3169 | const Variant = union(enum) { | 3169 | const 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 }; |
| 3188 | | 3188 | |
| 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#} |
| 3198 | const std = @import("std"); | 3198 | const std = @import("std"); |
| 3199 | const assert = std.debug.assert; | 3199 | const expect = std.testing.expect; |
| 3200 | | 3200 | |
| 3201 | const Small2 = union(enum) { | 3201 | const 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 | }; |
| 3206 | test "@tagName" { | 3206 | test "@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#} |
| 3229 | const std = @import("std"); | 3229 | const std = @import("std"); |
| 3230 | const assert = std.debug.assert; | 3230 | const expect = std.testing.expect; |
| 3231 | | 3231 | |
| 3232 | const Number = union { | 3232 | const Number = union { |
| 3233 | int: i32, | 3233 | int: i32, |
| ... | @@ -3237,8 +3237,8 @@ const Number = union { | ... | @@ -3237,8 +3237,8 @@ const Number = union { |
| 3237 | test "anonymous union literal syntax" { | 3237 | test "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 | } |
| 3243 | | 3243 | |
| 3244 | fn makeNumber() Number { | 3244 | fn 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#} |
| 3293 | const std = @import("std"); | 3293 | const std = @import("std"); |
| 3294 | const assert = std.debug.assert; | 3294 | const expect = std.testing.expect; |
| 3295 | | 3295 | |
| 3296 | test "labeled break from labeled block expression" { | 3296 | test "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#} |
| 3341 | const std = @import("std"); | 3341 | const std = @import("std"); |
| 3342 | const assert = std.debug.assert; | 3342 | const expect = std.testing.expect; |
| 3343 | | 3343 | |
| 3344 | test "switch simple" { | 3344 | test "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 | }; |
| 3381 | | 3381 | |
| 3382 | assert(b == 1); | 3382 | expect(b == 1); |
| 3383 | } | 3383 | } |
| 3384 | | 3384 | |
| 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#} |
| 3412 | const assert = @import("std").debug.assert; | 3412 | const expect = @import("std").testing.expect; |
| 3413 | | 3413 | |
| 3414 | test "switch on tagged union" { | 3414 | test "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 | }; |
| 3444 | | 3444 | |
| 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#} |
| 3479 | const std = @import("std"); | 3479 | const std = @import("std"); |
| 3480 | const assert = std.debug.assert; | 3480 | const expect = std.testing.expect; |
| 3481 | | 3481 | |
| 3482 | const Color = enum { | 3482 | const 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#} |
| 3507 | const assert = @import("std").debug.assert; | 3507 | const expect = @import("std").testing.expect; |
| 3508 | | 3508 | |
| 3509 | test "while basic" { | 3509 | test "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#} |
| 3521 | const assert = @import("std").debug.assert; | 3521 | const expect = @import("std").testing.expect; |
| 3522 | | 3522 | |
| 3523 | test "while break" { | 3523 | test "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#} |
| 3537 | const assert = @import("std").debug.assert; | 3537 | const expect = @import("std").testing.expect; |
| 3538 | | 3538 | |
| 3539 | test "while continue" { | 3539 | test "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#} |
| 3555 | const assert = @import("std").debug.assert; | 3555 | const expect = @import("std").testing.expect; |
| 3556 | | 3556 | |
| 3557 | test "while loop continue expression" { | 3557 | test "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 | } |
| 3562 | | 3562 | |
| 3563 | test "while loop continue expression, more complicated" { | 3563 | test "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#} |
| 3584 | const assert = @import("std").debug.assert; | 3584 | const expect = @import("std").testing.expect; |
| 3585 | | 3585 | |
| 3586 | test "while else" { | 3586 | test "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 | } |
| 3590 | | 3590 | |
| 3591 | fn rangeHasNumber(begin: usize, end: usize, number: usize) bool { | 3591 | fn 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#} |
| 3637 | const assert = @import("std").debug.assert; | 3637 | const expect = @import("std").testing.expect; |
| 3638 | | 3638 | |
| 3639 | test "while null capture" { | 3639 | test "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); |
| 3646 | | 3646 | |
| 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 | } |
| 3655 | | 3655 | |
| ... | @@ -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#} |
| 3679 | const assert = @import("std").debug.assert; | 3679 | const expect = @import("std").testing.expect; |
| 3680 | | 3680 | |
| 3681 | test "while error union capture" { | 3681 | test "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 | } |
| 3690 | | 3690 | |
| ... | @@ -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#} |
| 3709 | const assert = @import("std").debug.assert; | 3709 | const expect = @import("std").testing.expect; |
| 3710 | | 3710 | |
| 3711 | test "inline while loop" { | 3711 | test "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 | } |
| 3725 | | 3725 | |
| 3726 | fn typeNameLength(comptime T: type) usize { | 3726 | fn 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#} |
| 3744 | const assert = @import("std").debug.assert; | 3744 | const expect = @import("std").testing.expect; |
| 3745 | | 3745 | |
| 3746 | test "for basics" { | 3746 | test "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); |
| 3759 | | 3759 | |
| 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); |
| 3765 | | 3765 | |
| 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 | } |
| 3775 | | 3775 | |
| 3776 | test "for reference" { | 3776 | test "for reference" { |
| ... | @@ -3782,9 +3782,9 @@ test "for reference" { | ... | @@ -3782,9 +3782,9 @@ test "for reference" { |
| 3782 | value.* += 1; | 3782 | value.* += 1; |
| 3783 | } | 3783 | } |
| 3784 | | 3784 | |
| 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 | } |
| 3789 | | 3789 | |
| 3790 | test "for else" { | 3790 | test "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#} |
| 3812 | const std = @import("std"); | 3812 | const std = @import("std"); |
| 3813 | const assert = std.debug.assert; | 3813 | const expect = std.testing.expect; |
| 3814 | | 3814 | |
| 3815 | test "nested break" { | 3815 | test "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 | } |
| 3825 | | 3825 | |
| 3826 | test "nested continue" { | 3826 | test "nested continue" { |
| ... | @@ -3832,7 +3832,7 @@ test "nested continue" { | ... | @@ -3832,7 +3832,7 @@ test "nested continue" { |
| 3832 | } | 3832 | } |
| 3833 | } | 3833 | } |
| 3834 | | 3834 | |
| 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#} |
| 3848 | const assert = @import("std").debug.assert; | 3848 | const expect = @import("std").testing.expect; |
| 3849 | | 3849 | |
| 3850 | test "inline for loop" { | 3850 | test "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 | } |
| 3864 | | 3864 | |
| 3865 | fn typeNameLength(comptime T: type) usize { | 3865 | fn 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 | // * ?T | 3885 | // * ?T |
| 3886 | // * anyerror!T | 3886 | // * anyerror!T |
| 3887 | | 3887 | |
| 3888 | const assert = @import("std").debug.assert; | 3888 | const expect = @import("std").testing.expect; |
| 3889 | | 3889 | |
| 3890 | test "if expression" { | 3890 | test "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 | } |
| 3897 | | 3897 | |
| 3898 | test "if boolean" { | 3898 | test "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" { |
| 3913 | | 3913 | |
| 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 | } |
| 3927 | | 3927 | |
| 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 | } |
| 3932 | | 3932 | |
| 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 | } |
| 3937 | | 3937 | |
| 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 | } |
| 3943 | | 3943 | |
| 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" { |
| 3954 | | 3954 | |
| 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 | } |
| 3968 | | 3968 | |
| 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 |_| {} |
| 3973 | | 3973 | |
| 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 | } |
| 3978 | | 3978 | |
| 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 | } |
| 3986 | | 3986 | |
| 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" { |
| 3997 | | 3997 | |
| 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 | } |
| 4004 | | 4004 | |
| 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 | } |
| 4018 | | 4018 | |
| 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 | } |
| 4028 | | 4028 | |
| 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#} |
| 4040 | const std = @import("std"); | 4040 | const std = @import("std"); |
| 4041 | const assert = std.debug.assert; | 4041 | const expect = std.testing.expect; |
| 4042 | const print = std.debug.print; | 4042 | const print = std.debug.print; |
| 4043 | | 4043 | |
| 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); |
| 4053 | | 4053 | |
| 4054 | a = 5; | 4054 | a = 5; |
| 4055 | return a; | 4055 | return a; |
| 4056 | } | 4056 | } |
| 4057 | | 4057 | |
| 4058 | test "defer basics" { | 4058 | test "defer basics" { |
| 4059 | assert(deferExample() == 5); | 4059 | expect(deferExample() == 5); |
| 4060 | } | 4060 | } |
| 4061 | | 4061 | |
| 4062 | // If multiple defer statements are specified, they will be executed in | 4062 | // 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 |
| 4138 | fn assert(ok: bool) void { | 4139 | fn assert(ok: bool) void { |
| 4139 | if (!ok) unreachable; // assertion failure | 4140 | 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 |
| 4193 | | 4194 | |
| 4194 | test "foo" { | 4195 | test "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 | } |
| 4198 | | 4199 | |
| 4199 | fn bar() anyerror!u32 { | 4200 | fn bar() anyerror!u32 { |
| 4200 | return 1234; | 4201 | return 1234; |
| 4201 | } | 4202 | } |
| 4202 | | 4203 | |
| 4203 | const assert = @import("std").debug.assert; | 4204 | const 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#} |
| 4208 | const assert = @import("std").debug.assert; | 4209 | const expect = @import("std").testing.expect; |
| 4209 | | 4210 | |
| 4210 | // Functions are declared like this | 4211 | // Functions are declared like this |
| 4211 | fn add(a: i8, b: i8) i8 { | 4212 | fn 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 | } |
| 4257 | | 4258 | |
| 4258 | test "function" { | 4259 | test "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#} |
| 4265 | const assert = @import("std").debug.assert; | 4266 | const expect = @import("std").testing.expect; |
| 4266 | | 4267 | |
| 4267 | comptime { | 4268 | comptime { |
| 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 | } |
| 4271 | | 4272 | |
| 4272 | fn foo() void { } | 4273 | fn 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 | } |
| 4300 | | 4301 | |
| 4301 | const assert = @import("std").debug.assert; | 4302 | const expect = @import("std").testing.expect; |
| 4302 | | 4303 | |
| 4303 | test "pass struct to function" { | 4304 | test "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#} |
| 4318 | const assert = @import("std").debug.assert; | 4319 | const expect = @import("std").testing.expect; |
| 4319 | | 4320 | |
| 4320 | fn addFortyTwo(x: anytype) @TypeOf(x) { | 4321 | fn addFortyTwo(x: anytype) @TypeOf(x) { |
| 4321 | return x + 42; | 4322 | return x + 42; |
| 4322 | } | 4323 | } |
| 4323 | | 4324 | |
| 4324 | test "fn type inference" { | 4325 | test "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#} |
| 4332 | | 4333 | |
| 4333 | {#header_close#} | 4334 | {#header_close#} |
| 4334 | {#header_open|Function Reflection#} | 4335 | {#header_open|Function Reflection#} |
| 4335 | {#code_begin|test#} | 4336 | {#code_begin|test#} |
| 4336 | const assert = @import("std").debug.assert; | 4337 | const expect = @import("std").testing.expect; |
| 4337 | | 4338 | |
| 4338 | test "fn reflection" { | 4339 | test "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 { |
| 4372 | | 4373 | |
| 4373 | test "coerce subset to superset" { | 4374 | test "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 | } |
| 4377 | | 4378 | |
| 4378 | fn foo(err: AllocationError) FileOpenError { | 4379 | fn foo(err: AllocationError) FileOpenError { |
| ... | @@ -4480,7 +4481,7 @@ fn charToDigit(c: u8) u8 { | ... | @@ -4480,7 +4481,7 @@ fn charToDigit(c: u8) u8 { |
| 4480 | | 4481 | |
| 4481 | test "parse u64" { | 4482 | test "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#} |
| 4628 | const assert = @import("std").debug.assert; | 4629 | const expect = @import("std").testing.expect; |
| 4629 | | 4630 | |
| 4630 | test "error union" { | 4631 | test "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; |
| 4638 | | 4639 | |
| 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); |
| 4641 | | 4642 | |
| 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-time | 5008 | <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#} |
| 5010 | const assert = @import("std").debug.assert; | 5011 | const expect = @import("std").testing.expect; |
| 5011 | | 5012 | |
| 5012 | test "optional type" { | 5013 | test "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; |
| 5018 | | 5019 | |
| 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#} of | 5035 | <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#} |
| 5037 | const assert = @import("std").debug.assert; | 5038 | const expect = @import("std").testing.expect; |
| 5038 | | 5039 | |
| 5039 | test "optional pointers" { | 5040 | test "optional pointers" { |
| 5040 | // Pointers cannot be null. If you want a null pointer, use the optional | 5041 | // 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; |
| 5046 | | 5047 | |
| 5047 | assert(ptr.?.* == 1); | 5048 | expect(ptr.?.* == 1); |
| 5048 | | 5049 | |
| 5049 | // Optional pointers are the same size as normal pointers, because pointer | 5050 | // 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#} |
| 5117 | const std = @import("std"); | 5118 | const std = @import("std"); |
| 5118 | const assert = std.debug.assert; | 5119 | const expect = std.testing.expect; |
| 5119 | const mem = std.mem; | 5120 | const mem = std.mem; |
| 5120 | | 5121 | |
| 5121 | test "cast *[1][*]const u8 to [*]const ?[*]const u8" { | 5122 | test "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#} |
| 5134 | const std = @import("std"); | 5135 | const std = @import("std"); |
| 5135 | const assert = std.debug.assert; | 5136 | const expect = std.testing.expect; |
| 5136 | const mem = std.mem; | 5137 | const mem = std.mem; |
| 5137 | | 5138 | |
| 5138 | test "integer widening" { | 5139 | test "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 | } |
| 5147 | | 5148 | |
| 5148 | test "implicit unsigned integer to signed integer" { | 5149 | test "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 | } |
| 5153 | | 5154 | |
| 5154 | test "float widening" { | 5155 | test "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#} |
| 5185 | const std = @import("std"); | 5186 | const std = @import("std"); |
| 5186 | const assert = std.debug.assert; | 5187 | const expect = std.testing.expect; |
| 5187 | | 5188 | |
| 5188 | // This cast exists primarily so that string literals can be | 5189 | // This cast exists primarily so that string literals can be |
| 5189 | // passed to functions that accept const slices. However | 5190 | // 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; |
| 5192 | test "[N]T to []const T" { | 5193 | test "[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)); |
| 5196 | | 5197 | |
| 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 | } |
| 5200 | | 5201 | |
| 5201 | // Likewise, it works when the destination type is an error union. | 5202 | // Likewise, it works when the destination type is an error union. |
| 5202 | test "[N]T to E![]const T" { | 5203 | test "[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)); |
| 5206 | | 5207 | |
| 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 | } |
| 5210 | | 5211 | |
| 5211 | // Likewise, it works when the destination type is an optional. | 5212 | // Likewise, it works when the destination type is an optional. |
| 5212 | test "[N]T to ?[]const T" { | 5213 | test "[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.?)); |
| 5216 | | 5217 | |
| 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 | } |
| 5220 | | 5221 | |
| 5221 | // In this cast, the array length becomes the slice length. | 5222 | // In this cast, the array length becomes the slice length. |
| 5222 | test "*[N]T to []T" { | 5223 | test "*[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")); |
| 5226 | | 5227 | |
| 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 | } |
| 5231 | | 5232 | |
| 5232 | // Single-item pointers to arrays can be coerced to | 5233 | // 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" { |
| 5234 | test "*[N]T to [*]T" { | 5235 | test "*[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 | } |
| 5240 | | 5241 | |
| ... | @@ -5242,7 +5243,7 @@ test "*[N]T to [*]T" { | ... | @@ -5242,7 +5243,7 @@ test "*[N]T to [*]T" { |
| 5242 | test "*[N]T to ?[*]T" { | 5243 | test "*[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 | } |
| 5247 | | 5248 | |
| 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#} |
| 5263 | const std = @import("std"); | 5264 | const std = @import("std"); |
| 5264 | const assert = std.debug.assert; | 5265 | const expect = std.testing.expect; |
| 5265 | | 5266 | |
| 5266 | test "coerce to optionals" { | 5267 | test "coerce to optionals" { |
| 5267 | const x: ?i32 = 1234; | 5268 | const x: ?i32 = 1234; |
| 5268 | const y: ?i32 = null; | 5269 | const y: ?i32 = null; |
| 5269 | | 5270 | |
| 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#} |
| 5276 | const std = @import("std"); | 5277 | const std = @import("std"); |
| 5277 | const assert = std.debug.assert; | 5278 | const expect = std.testing.expect; |
| 5278 | | 5279 | |
| 5279 | test "coerce to optionals wrapped in error union" { | 5280 | test "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; |
| 5282 | | 5283 | |
| 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#} |
| 5293 | const std = @import("std"); | 5294 | const std = @import("std"); |
| 5294 | const assert = std.debug.assert; | 5295 | const expect = std.testing.expect; |
| 5295 | | 5296 | |
| 5296 | test "coercion to error unions" { | 5297 | test "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; |
| 5299 | | 5300 | |
| 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#} |
| 5310 | const std = @import("std"); | 5311 | const std = @import("std"); |
| 5311 | const assert = std.debug.assert; | 5312 | const expect = std.testing.expect; |
| 5312 | | 5313 | |
| 5313 | test "coercing large integer type to smaller one when value is comptime known to fit" { | 5314 | test "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#} |
| 5326 | const std = @import("std"); | 5327 | const std = @import("std"); |
| 5327 | const assert = std.debug.assert; | 5328 | const expect = std.testing.expect; |
| 5328 | | 5329 | |
| 5329 | const E = enum { | 5330 | const E = enum { |
| 5330 | one, | 5331 | one, |
| ... | @@ -5341,11 +5342,11 @@ const U = union(E) { | ... | @@ -5341,11 +5342,11 @@ const U = union(E) { |
| 5341 | test "coercion between unions and enums" { | 5342 | test "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); |
| 5345 | | 5346 | |
| 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#} |
| 5413 | const std = @import("std"); | 5414 | const std = @import("std"); |
| 5414 | const assert = std.debug.assert; | 5415 | const expect = std.testing.expect; |
| 5415 | const mem = std.mem; | 5416 | const mem = std.mem; |
| 5416 | | 5417 | |
| 5417 | test "peer resolve int widening" { | 5418 | test "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 | } |
| 5424 | | 5425 | |
| 5425 | test "peer resolve arrays of different size to const slice" { | 5426 | test "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 | } |
| 5431 | fn boolToStr(b: bool) []const u8 { | 5432 | fn 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" { |
| 5439 | fn testPeerResolveArrayConstSlice(b: bool) void { | 5440 | fn 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 | } |
| 5445 | | 5446 | |
| 5446 | test "peer type resolution: ?T and T" { | 5447 | test "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 | } |
| 5454 | fn peerTypeTAndOptionalT(c: bool, b: bool) ?usize { | 5455 | fn 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 | } |
| 5461 | | 5462 | |
| 5462 | test "peer type resolution: *[0]u8 and []const u8" { | 5463 | test "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 | } |
| 5470 | fn peerTypeEmptyArrayAndSlice(a: bool, slice: []const u8) []const u8 { | 5471 | fn 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 | } |
| 5491 | fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) anyerror![]u8 { | 5492 | fn 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 { |
| 5499 | test "peer type resolution: *const T and ?*T" { | 5500 | test "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#} |
| 5549 | const std = @import("std"); | 5550 | const std = @import("std"); |
| 5550 | const assert = std.debug.assert; | 5551 | const expect = std.testing.expect; |
| 5551 | | 5552 | |
| 5552 | test "turn HashMap into a set with void" { | 5553 | test "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, {}); |
| 5558 | | 5559 | |
| 5559 | assert(map.contains(2)); | 5560 | expect(map.contains(2)); |
| 5560 | assert(!map.contains(3)); | 5561 | expect(!map.contains(3)); |
| 5561 | | 5562 | |
| 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#} |
| 5609 | const std = @import("std"); | 5610 | const std = @import("std"); |
| 5610 | const assert = std.debug.assert; | 5611 | const expect = std.testing.expect; |
| 5611 | | 5612 | |
| 5612 | test "pointer to empty struct" { | 5613 | test "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 are | 5622 | <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" { |
| 5650 | usingnamespace @import("std"); | 5651 | usingnamespace @import("std"); |
| 5651 | | 5652 | |
| 5652 | test "using std namespace" { | 5653 | test "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 | } |
| 5764 | test "try to compare bools" { | 5765 | test "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#} |
| 5805 | const assert = @import("std").debug.assert; | 5806 | const expect = @import("std").testing.expect; |
| 5806 | | 5807 | |
| 5807 | const CmdFn = struct { | 5808 | const 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 | } |
| 5831 | | 5832 | |
| 5832 | test "perform fn" { | 5833 | test "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); |
| 5847 | fn performFn(start_value: i32) i32 { | 5848 | fn 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); |
| 5857 | fn performFn(start_value: i32) i32 { | 5858 | fn 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); |
| 5866 | fn performFn(start_value: i32) i32 { | 5867 | fn 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#} |
| 5918 | const assert = @import("std").debug.assert; | 5919 | const expect = @import("std").testing.expect; |
| 5919 | | 5920 | |
| 5920 | fn fibonacci(index: u32) u32 { | 5921 | fn 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 { |
| 5924 | | 5925 | |
| 5925 | test "fibonacci" { | 5926 | test "fibonacci" { |
| 5926 | // test fibonacci at run-time | 5927 | // test fibonacci at run-time |
| 5927 | assert(fibonacci(7) == 13); | 5928 | expect(fibonacci(7) == 13); |
| 5928 | | 5929 | |
| 5929 | // test fibonacci at compile-time | 5930 | // 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#} |
| 5939 | const assert = @import("std").debug.assert; | 5940 | const expect = @import("std").testing.expect; |
| 5940 | | 5941 | |
| 5941 | fn fibonacci(index: u32) u32 { | 5942 | fn 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 { |
| 5945 | | 5946 | |
| 5946 | test "fibonacci" { | 5947 | test "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#} |
| 5962 | const assert = @import("std").debug.assert; | 5963 | const expect = @import("std").testing.expect; |
| 5963 | | 5964 | |
| 5964 | fn fibonacci(index: i32) i32 { | 5965 | fn 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 { |
| 5968 | | 5969 | |
| 5969 | test "fibonacci" { | 5970 | test "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#} |
| 5985 | const assert = @import("std").debug.assert; | 5986 | const expect = @import("std").testing.expect; |
| 5986 | | 5987 | |
| 5987 | fn fibonacci(index: i32) i32 { | 5988 | fn 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 { |
| 5991 | | 5992 | |
| 5992 | test "fibonacci" { | 5993 | test "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 the | 6000 | 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 hit | 6001 | parameter {#syntax#}ok{#endsyntax#} set to {#syntax#}false{#endsyntax#}. When the interpreter hit |
| 6001 | {#syntax#}unreachable{#endsyntax#} it emitted a compile error, because reaching unreachable | 6002 | {#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 detected | 6003 | causes a compile error if it is detected at compile-time. |
| 6003 | at compile-time. | | |
| 6004 | </p> | 6004 | </p> |
| 6005 | | 6005 | |
| 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 | } |
| 6043 | | 6043 | |
| 6044 | test "variable values" { | 6044 | test "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#} |
| 6437 | const std = @import("std"); | 6437 | const std = @import("std"); |
| 6438 | const assert = std.debug.assert; | 6438 | const expect = std.testing.expect; |
| 6439 | | 6439 | |
| 6440 | comptime { | 6440 | comptime { |
| 6441 | asm ( | 6441 | asm ( |
| ... | @@ -6450,7 +6450,7 @@ comptime { | ... | @@ -6450,7 +6450,7 @@ comptime { |
| 6450 | extern fn my_func(a: i32, b: i32) i32; | 6450 | extern fn my_func(a: i32, b: i32) i32; |
| 6451 | | 6451 | |
| 6452 | test "global assembly" { | 6452 | test "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#} |
| 6487 | const std = @import("std"); | 6487 | const std = @import("std"); |
| 6488 | const assert = std.debug.assert; | 6488 | const expect = std.testing.expect; |
| 6489 | | 6489 | |
| 6490 | var x: i32 = 1; | 6490 | var x: i32 = 1; |
| 6491 | | 6491 | |
| 6492 | test "suspend with no resume" { | 6492 | test "suspend with no resume" { |
| 6493 | var frame = async func(); | 6493 | var frame = async func(); |
| 6494 | assert(x == 2); | 6494 | expect(x == 2); |
| 6495 | } | 6495 | } |
| 6496 | | 6496 | |
| 6497 | fn func() void { | 6497 | fn 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#} |
| 6513 | const std = @import("std"); | 6513 | const std = @import("std"); |
| 6514 | const assert = std.debug.assert; | 6514 | const expect = std.testing.expect; |
| 6515 | | 6515 | |
| 6516 | var the_frame: anyframe = undefined; | 6516 | var the_frame: anyframe = undefined; |
| 6517 | var result = false; | 6517 | var result = false; |
| 6518 | | 6518 | |
| 6519 | test "async function suspend with block" { | 6519 | test "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 | } |
| 6525 | | 6525 | |
| 6526 | fn testSuspendBlock() void { | 6526 | fn 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#} |
| 6551 | const std = @import("std"); | 6551 | const std = @import("std"); |
| 6552 | const assert = std.debug.assert; | 6552 | const expect = std.testing.expect; |
| 6553 | | 6553 | |
| 6554 | test "resume from suspend" { | 6554 | test "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 | } |
| 6559 | fn testResumeFromSuspend(my_result: *i32) void { | 6559 | fn 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#} |
| 6580 | const std = @import("std"); | 6580 | const std = @import("std"); |
| 6581 | const assert = std.debug.assert; | 6581 | const expect = std.testing.expect; |
| 6582 | | 6582 | |
| 6583 | test "async and await" { | 6583 | test "async and await" { |
| 6584 | // Here we have an exception where we do not match an async | 6584 | // 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" { |
| 6592 | | 6592 | |
| 6593 | fn amain() void { | 6593 | fn 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)); |
| 6596 | | 6596 | |
| 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#} |
| 6624 | const std = @import("std"); | 6624 | const std = @import("std"); |
| 6625 | const assert = std.debug.assert; | 6625 | const expect = std.testing.expect; |
| 6626 | | 6626 | |
| 6627 | var the_frame: anyframe = undefined; | 6627 | var the_frame: anyframe = undefined; |
| 6628 | var final_result: i32 = 0; | 6628 | var 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 | } |
| 6639 | fn amain() void { | 6639 | fn 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 has | 6848 | 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; |
| 6852 | comptime { | 6852 | comptime { |
| 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 be | 6856 | 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#} |
| 6888 | const std = @import("std"); | 6888 | const std = @import("std"); |
| 6889 | const assert = std.debug.assert; | 6889 | const expect = std.testing.expect; |
| 6890 | | 6890 | |
| 6891 | test "async fn pointer in a struct field" { | 6891 | test "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 | } |
| 6903 | | 6903 | |
| 6904 | fn func(y: *i32) void { | 6904 | fn 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#} |
| 7085 | const assert = @import("std").debug.assert; | 7085 | const expect = @import("std").testing.expect; |
| 7086 | | 7086 | |
| 7087 | test "noinline function call" { | 7087 | test "noinline function call" { |
| 7088 | assert(@call(.{}, add, .{3, 9}) == 12); | 7088 | expect(@call(.{}, add, .{3, 9}) == 12); |
| 7089 | } | 7089 | } |
| 7090 | | 7090 | |
| 7091 | fn add(a: i32, b: i32) i32 { | 7091 | fn add(a: i32, b: i32) i32 { |
| ... | @@ -7544,14 +7544,14 @@ const Point = struct { | ... | @@ -7544,14 +7544,14 @@ const Point = struct { |
| 7544 | }; | 7544 | }; |
| 7545 | | 7545 | |
| 7546 | test "field access by string" { | 7546 | test "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}; |
| 7549 | | 7549 | |
| 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; |
| 7552 | | 7552 | |
| 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#} |
| 7557 | | 7557 | |
| ... | @@ -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#} |
| 7659 | const std = @import("std"); | 7659 | const std = @import("std"); |
| 7660 | const assert = std.debug.assert; | 7660 | const expect = std.testing.expect; |
| 7661 | | 7661 | |
| 7662 | const Foo = struct { | 7662 | const Foo = struct { |
| 7663 | nope: i32, | 7663 | nope: i32, |
| ... | @@ -7667,16 +7667,16 @@ const Foo = struct { | ... | @@ -7667,16 +7667,16 @@ const Foo = struct { |
| 7667 | }; | 7667 | }; |
| 7668 | | 7668 | |
| 7669 | test "@hasDecl" { | 7669 | test "@hasDecl" { |
| 7670 | assert(@hasDecl(Foo, "blah")); | 7670 | expect(@hasDecl(Foo, "blah")); |
| 7671 | | 7671 | |
| 7672 | // Even though `hi` is private, @hasDecl returns true because this test is | 7672 | // 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 declared | 7673 | // 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")); |
| 7676 | | 7676 | |
| 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#} |
| 7852 | const std = @import("std"); | 7852 | const std = @import("std"); |
| 7853 | const builtin = @import("builtin"); | 7853 | const builtin = @import("builtin"); |
| 7854 | const assert = std.debug.assert; | 7854 | const expect = std.testing.expect; |
| 7855 | | 7855 | |
| 7856 | test "@wasmMemoryGrow" { | 7856 | test "@wasmMemoryGrow" { |
| 7857 | if (builtin.arch != .wasm32) return error.SkipZigTest; | 7857 | if (builtin.arch != .wasm32) return error.SkipZigTest; |
| 7858 | | 7858 | |
| 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#} |
| 8196 | const std = @import("std"); | 8196 | const std = @import("std"); |
| 8197 | const assert = std.debug.assert; | 8197 | const expect = std.testing.expect; |
| 8198 | | 8198 | |
| 8199 | test "vector @splat" { | 8199 | test "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#} |
| 8412 | const std = @import("std"); | 8412 | const std = @import("std"); |
| 8413 | const assert = std.debug.assert; | 8413 | const expect = std.testing.expect; |
| 8414 | | 8414 | |
| 8415 | test "@This()" { | 8415 | test "@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 | } |
| 8420 | | 8420 | |
| 8421 | fn List(comptime T: type) type { | 8421 | fn 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#} |
| 8458 | const std = @import("std"); | 8458 | const std = @import("std"); |
| 8459 | const assert = std.debug.assert; | 8459 | const expect = std.testing.expect; |
| 8460 | | 8460 | |
| 8461 | test "integer truncation" { | 8461 | test "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#} |
| 8546 | const std = @import("std"); | 8546 | const std = @import("std"); |
| 8547 | const assert = std.debug.assert; | 8547 | const expect = std.testing.expect; |
| 8548 | | 8548 | |
| 8549 | test "no runtime side effects" { | 8549 | test "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 | } |
| 8555 | | 8555 | |
| 8556 | fn foo(comptime T: type, ptr: *T) T { | 8556 | fn 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#} |
| 8855 | const std = @import("std"); | 8855 | const std = @import("std"); |
| 8856 | const assert = std.debug.assert; | 8856 | const expect = std.testing.expect; |
| 8857 | const minInt = std.math.minInt; | 8857 | const minInt = std.math.minInt; |
| 8858 | const maxInt = std.math.maxInt; | 8858 | const maxInt = std.math.maxInt; |
| 8859 | | 8859 | |
| 8860 | test "wraparound addition and subtraction" { | 8860 | test "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#} |
| 9288 | const std = @import("std"); | 9288 | const std = @import("std"); |
| 9289 | const Allocator = std.mem.Allocator; | 9289 | const Allocator = std.mem.Allocator; |
| 9290 | const assert = std.debug.assert; | 9290 | const expect = std.testing.expect; |
| 9291 | | 9291 | |
| 9292 | test "using an allocator" { | 9292 | test "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 | } |
| 9298 | | 9298 | |
| 9299 | fn concat(allocator: *Allocator, a: []const u8, b: []const u8) ![]u8 { | 9299 | fn 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#} |
| 9561 | const std = @import("std"); | 9561 | const std = @import("std"); |
| 9562 | const builtin = std.builtin; | 9562 | const builtin = std.builtin; |
| 9563 | const assert = std.debug.assert; | 9563 | const expect = std.testing.expect; |
| 9564 | | 9564 | |
| 9565 | test "builtin.is_test" { | 9565 | test "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" { |
| 9613 | const std = @import("std"); | 9613 | const std = @import("std"); |
| 9614 | const expect = std.testing.expect; | 9614 | const expect = std.testing.expect; |
| 9615 | | 9615 | |
| 9616 | test "assert in release fast mode" { | 9616 | test "expect in release fast mode" { |
| 9617 | expect(false); | 9617 | expect(false); |
| 9618 | } | 9618 | } |
| 9619 | {#code_end#} | 9619 | {#code_end#} |