| ... | ... | @@ -344,16 +344,16 @@ pub fn main() void { |
| 344 | 344 | {#header_close#} |
| 345 | 345 | {#header_open|Comments#} |
| 346 | 346 | {#code_begin|test|comments#} |
| 347 | | const assert = @import("std").debug.assert; |
| 347 | const expect = @import("std").testing.expect; |
| 348 | 348 | |
| 349 | 349 | test "comments" { |
| 350 | 350 | // Comments in Zig start with "//" and end at the next LF byte (end of line). |
| 351 | 351 | // The below line is a comment, and won't be executed. |
| 352 | 352 | |
| 353 | | //assert(false); |
| 353 | //expect(false); |
| 354 | 354 | |
| 355 | 355 | const x = true; // another comment |
| 356 | | assert(x); |
| 356 | expect(x); |
| 357 | 357 | } |
| 358 | 358 | {#code_end#} |
| 359 | 359 | <p> |
| ... | ... | @@ -695,19 +695,19 @@ pub fn main() void { |
| 695 | 695 | and character literals. |
| 696 | 696 | </p> |
| 697 | 697 | {#code_begin|test#} |
| 698 | | const assert = @import("std").debug.assert; |
| 698 | const expect = @import("std").testing.expect; |
| 699 | 699 | const mem = @import("std").mem; |
| 700 | 700 | |
| 701 | 701 | test "string literals" { |
| 702 | 702 | const bytes = "hello"; |
| 703 | | assert(@TypeOf(bytes) == *const [5:0]u8); |
| 704 | | assert(bytes.len == 5); |
| 705 | | assert(bytes[1] == 'e'); |
| 706 | | assert(bytes[5] == 0); |
| 707 | | assert('e' == '\x65'); |
| 708 | | assert('\u{1f4a9}' == 128169); |
| 709 | | assert('π―' == 128175); |
| 710 | | assert(mem.eql(u8, "hello", "h\x65llo")); |
| 703 | expect(@TypeOf(bytes) == *const [5:0]u8); |
| 704 | expect(bytes.len == 5); |
| 705 | expect(bytes[1] == 'e'); |
| 706 | expect(bytes[5] == 0); |
| 707 | expect('e' == '\x65'); |
| 708 | expect('\u{1f4a9}' == 128169); |
| 709 | expect('π―' == 128175); |
| 710 | expect(mem.eql(u8, "hello", "h\x65llo")); |
| 711 | 711 | } |
| 712 | 712 | {#code_end#} |
| 713 | 713 | {#see_also|Arrays|Zig Test|Source Encoding#} |
| ... | ... | @@ -800,14 +800,14 @@ test "assignment" { |
| 800 | 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 | 801 | <p>If you need a variable that you can modify, use the {#syntax#}var{#endsyntax#} keyword:</p> |
| 802 | 802 | {#code_begin|test#} |
| 803 | | const assert = @import("std").debug.assert; |
| 803 | const expect = @import("std").testing.expect; |
| 804 | 804 | |
| 805 | 805 | test "var" { |
| 806 | 806 | var y: i32 = 5678; |
| 807 | 807 | |
| 808 | 808 | y += 1; |
| 809 | 809 | |
| 810 | | assert(y == 5679); |
| 810 | expect(y == 5679); |
| 811 | 811 | } |
| 812 | 812 | {#code_end#} |
| 813 | 813 | <p>Variables must be initialized:</p> |
| ... | ... | @@ -821,12 +821,12 @@ test "initialization" { |
| 821 | 821 | {#header_open|undefined#} |
| 822 | 822 | <p>Use {#syntax#}undefined{#endsyntax#} to leave variables uninitialized:</p> |
| 823 | 823 | {#code_begin|test#} |
| 824 | | const assert = @import("std").debug.assert; |
| 824 | const expect = @import("std").testing.expect; |
| 825 | 825 | |
| 826 | 826 | test "init with undefined" { |
| 827 | 827 | var x: i32 = undefined; |
| 828 | 828 | x = 1; |
| 829 | | assert(x == 1); |
| 829 | expect(x == 1); |
| 830 | 830 | } |
| 831 | 831 | {#code_end#} |
| 832 | 832 | <p> |
| ... | ... | @@ -868,8 +868,8 @@ var y: i32 = add(10, x); |
| 868 | 868 | const x: i32 = add(12, 34); |
| 869 | 869 | |
| 870 | 870 | test "global variables" { |
| 871 | | assert(x == 46); |
| 872 | | assert(y == 56); |
| 871 | expect(x == 46); |
| 872 | expect(y == 56); |
| 873 | 873 | } |
| 874 | 874 | |
| 875 | 875 | fn add(a: i32, b: i32) i32 { |
| ... | ... | @@ -877,18 +877,18 @@ fn add(a: i32, b: i32) i32 { |
| 877 | 877 | } |
| 878 | 878 | |
| 879 | 879 | const std = @import("std"); |
| 880 | | const assert = std.debug.assert; |
| 880 | const expect = std.testing.expect; |
| 881 | 881 | {#code_end#} |
| 882 | 882 | <p> |
| 883 | 883 | Global variables may be declared inside a {#link|struct#}, {#link|union#}, or {#link|enum#}: |
| 884 | 884 | </p> |
| 885 | 885 | {#code_begin|test|namespaced_global#} |
| 886 | 886 | const std = @import("std"); |
| 887 | | const assert = std.debug.assert; |
| 887 | const expect = std.testing.expect; |
| 888 | 888 | |
| 889 | 889 | test "namespaced global variable" { |
| 890 | | assert(foo() == 1235); |
| 891 | | assert(foo() == 1236); |
| 890 | expect(foo() == 1235); |
| 891 | expect(foo() == 1236); |
| 892 | 892 | } |
| 893 | 893 | |
| 894 | 894 | fn foo() i32 { |
| ... | ... | @@ -957,7 +957,7 @@ fn testTls(context: void) void { |
| 957 | 957 | </p> |
| 958 | 958 | {#code_begin|test|comptime_vars#} |
| 959 | 959 | const std = @import("std"); |
| 960 | | const assert = std.debug.assert; |
| 960 | const expect = std.testing.expect; |
| 961 | 961 | |
| 962 | 962 | test "comptime vars" { |
| 963 | 963 | var x: i32 = 1; |
| ... | ... | @@ -966,8 +966,8 @@ test "comptime vars" { |
| 966 | 966 | x += 1; |
| 967 | 967 | y += 1; |
| 968 | 968 | |
| 969 | | assert(x == 2); |
| 970 | | assert(y == 2); |
| 969 | expect(x == 2); |
| 970 | expect(y == 2); |
| 971 | 971 | |
| 972 | 972 | if (y != 2) { |
| 973 | 973 | // This compile error never triggers because y is a comptime variable, |
| ... | ... | @@ -1757,7 +1757,7 @@ orelse catch |
| 1757 | 1757 | {#header_close#} |
| 1758 | 1758 | {#header_open|Arrays#} |
| 1759 | 1759 | {#code_begin|test|arrays#} |
| 1760 | | const assert = @import("std").debug.assert; |
| 1760 | const expect = @import("std").testing.expect; |
| 1761 | 1761 | const mem = @import("std").mem; |
| 1762 | 1762 | |
| 1763 | 1763 | // array literal |
| ... | ... | @@ -1765,14 +1765,14 @@ const message = [_]u8{ 'h', 'e', 'l', 'l', 'o' }; |
| 1765 | 1765 | |
| 1766 | 1766 | // get the size of an array |
| 1767 | 1767 | comptime { |
| 1768 | | assert(message.len == 5); |
| 1768 | expect(message.len == 5); |
| 1769 | 1769 | } |
| 1770 | 1770 | |
| 1771 | 1771 | // A string literal is a pointer to an array literal. |
| 1772 | 1772 | const same_message = "hello"; |
| 1773 | 1773 | |
| 1774 | 1774 | comptime { |
| 1775 | | assert(mem.eql(u8, &message, same_message)); |
| 1775 | expect(mem.eql(u8, &message, same_message)); |
| 1776 | 1776 | } |
| 1777 | 1777 | |
| 1778 | 1778 | test "iterate over an array" { |
| ... | ... | @@ -1780,7 +1780,7 @@ test "iterate over an array" { |
| 1780 | 1780 | for (message) |byte| { |
| 1781 | 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 | 1786 | // modifiable array |
| ... | ... | @@ -1790,8 +1790,8 @@ test "modify an array" { |
| 1790 | 1790 | for (some_integers) |*item, i| { |
| 1791 | 1791 | item.* = @intCast(i32, i); |
| 1792 | 1792 | } |
| 1793 | | assert(some_integers[10] == 10); |
| 1794 | | assert(some_integers[99] == 99); |
| 1793 | expect(some_integers[10] == 10); |
| 1794 | expect(some_integers[99] == 99); |
| 1795 | 1795 | } |
| 1796 | 1796 | |
| 1797 | 1797 | // array concatenation works if the values are known |
| ... | ... | @@ -1800,7 +1800,7 @@ const part_one = [_]i32{ 1, 2, 3, 4 }; |
| 1800 | 1800 | const part_two = [_]i32{ 5, 6, 7, 8 }; |
| 1801 | 1801 | const all_of_it = part_one ++ part_two; |
| 1802 | 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 | 1806 | // remember that string literals are arrays |
| ... | ... | @@ -1808,21 +1808,21 @@ const hello = "hello"; |
| 1808 | 1808 | const world = "world"; |
| 1809 | 1809 | const hello_world = hello ++ " " ++ world; |
| 1810 | 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 | 1814 | // ** does repeating patterns |
| 1815 | 1815 | const pattern = "ab" ** 3; |
| 1816 | 1816 | comptime { |
| 1817 | | assert(mem.eql(u8, pattern, "ababab")); |
| 1817 | expect(mem.eql(u8, pattern, "ababab")); |
| 1818 | 1818 | } |
| 1819 | 1819 | |
| 1820 | 1820 | // initialize an array to zero |
| 1821 | 1821 | const all_zero = [_]u16{0} ** 10; |
| 1822 | 1822 | |
| 1823 | 1823 | comptime { |
| 1824 | | assert(all_zero.len == 10); |
| 1825 | | assert(all_zero[5] == 0); |
| 1824 | expect(all_zero.len == 10); |
| 1825 | expect(all_zero[5] == 0); |
| 1826 | 1826 | } |
| 1827 | 1827 | |
| 1828 | 1828 | // use compile-time code to initialize an array |
| ... | ... | @@ -1842,8 +1842,8 @@ const Point = struct { |
| 1842 | 1842 | }; |
| 1843 | 1843 | |
| 1844 | 1844 | test "compile-time array initialization" { |
| 1845 | | assert(fancy_array[4].x == 4); |
| 1846 | | assert(fancy_array[4].y == 8); |
| 1845 | expect(fancy_array[4].x == 4); |
| 1846 | expect(fancy_array[4].y == 8); |
| 1847 | 1847 | } |
| 1848 | 1848 | |
| 1849 | 1849 | // call a function to initialize an array |
| ... | ... | @@ -1855,9 +1855,9 @@ fn makePoint(x: i32) Point { |
| 1855 | 1855 | }; |
| 1856 | 1856 | } |
| 1857 | 1857 | test "array initialization with function calls" { |
| 1858 | | assert(more_points[4].x == 3); |
| 1859 | | assert(more_points[4].y == 6); |
| 1860 | | assert(more_points.len == 10); |
| 1858 | expect(more_points[4].x == 3); |
| 1859 | expect(more_points[4].y == 6); |
| 1860 | expect(more_points.len == 10); |
| 1861 | 1861 | } |
| 1862 | 1862 | {#code_end#} |
| 1863 | 1863 | {#see_also|for|Slices#} |
| ... | ... | @@ -1867,14 +1867,14 @@ test "array initialization with function calls" { |
| 1867 | 1867 | the type can be omitted from array literals:</p> |
| 1868 | 1868 | {#code_begin|test|anon_list#} |
| 1869 | 1869 | const std = @import("std"); |
| 1870 | | const assert = std.debug.assert; |
| 1870 | const expect = std.testing.expect; |
| 1871 | 1871 | |
| 1872 | 1872 | test "anonymous list literal syntax" { |
| 1873 | 1873 | var array: [4]u8 = .{11, 22, 33, 44}; |
| 1874 | | assert(array[0] == 11); |
| 1875 | | assert(array[1] == 22); |
| 1876 | | assert(array[2] == 33); |
| 1877 | | assert(array[3] == 44); |
| 1874 | expect(array[0] == 11); |
| 1875 | expect(array[1] == 22); |
| 1876 | expect(array[2] == 33); |
| 1877 | expect(array[3] == 44); |
| 1878 | 1878 | } |
| 1879 | 1879 | {#code_end#} |
| 1880 | 1880 | <p> |
| ... | ... | @@ -1883,18 +1883,18 @@ test "anonymous list literal syntax" { |
| 1883 | 1883 | </p> |
| 1884 | 1884 | {#code_begin|test|infer_list_literal#} |
| 1885 | 1885 | const std = @import("std"); |
| 1886 | | const assert = std.debug.assert; |
| 1886 | const expect = std.testing.expect; |
| 1887 | 1887 | |
| 1888 | 1888 | test "fully anonymous list literal" { |
| 1889 | 1889 | dump(.{ @as(u32, 1234), @as(f64, 12.34), true, "hi"}); |
| 1890 | 1890 | } |
| 1891 | 1891 | |
| 1892 | 1892 | fn dump(args: anytype) void { |
| 1893 | | assert(args.@"0" == 1234); |
| 1894 | | assert(args.@"1" == 12.34); |
| 1895 | | assert(args.@"2"); |
| 1896 | | assert(args.@"3"[0] == 'h'); |
| 1897 | | assert(args.@"3"[1] == 'i'); |
| 1893 | expect(args.@"0" == 1234); |
| 1894 | expect(args.@"1" == 12.34); |
| 1895 | expect(args.@"2"); |
| 1896 | expect(args.@"3"[0] == 'h'); |
| 1897 | expect(args.@"3"[1] == 'i'); |
| 1898 | 1898 | } |
| 1899 | 1899 | {#code_end#} |
| 1900 | 1900 | {#header_close#} |
| ... | ... | @@ -1905,7 +1905,7 @@ fn dump(args: anytype) void { |
| 1905 | 1905 | </p> |
| 1906 | 1906 | {#code_begin|test|multidimensional#} |
| 1907 | 1907 | const std = @import("std"); |
| 1908 | | const assert = std.debug.assert; |
| 1908 | const expect = std.testing.expect; |
| 1909 | 1909 | |
| 1910 | 1910 | const mat4x4 = [4][4]f32{ |
| 1911 | 1911 | [_]f32{ 1.0, 0.0, 0.0, 0.0 }, |
| ... | ... | @@ -1915,13 +1915,13 @@ const mat4x4 = [4][4]f32{ |
| 1915 | 1915 | }; |
| 1916 | 1916 | test "multidimensional arrays" { |
| 1917 | 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 | 1920 | // Here we iterate with for loops. |
| 1921 | 1921 | for (mat4x4) |row, row_index| { |
| 1922 | 1922 | for (row) |cell, column_index| { |
| 1923 | 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 | 1936 | </p> |
| 1937 | 1937 | {#code_begin|test|null_terminated_array#} |
| 1938 | 1938 | const std = @import("std"); |
| 1939 | | const assert = std.debug.assert; |
| 1939 | const expect = std.testing.expect; |
| 1940 | 1940 | |
| 1941 | 1941 | test "null terminated array" { |
| 1942 | 1942 | const array = [_:0]u8 {1, 2, 3, 4}; |
| 1943 | 1943 | |
| 1944 | | assert(@TypeOf(array) == [4:0]u8); |
| 1945 | | assert(array.len == 4); |
| 1946 | | assert(array[4] == 0); |
| 1944 | expect(@TypeOf(array) == [4:0]u8); |
| 1945 | expect(array.len == 4); |
| 1946 | expect(array[4] == 0); |
| 1947 | 1947 | } |
| 1948 | 1948 | {#code_end#} |
| 1949 | 1949 | {#see_also|Sentinel-Terminated Pointers|Sentinel-Terminated Slices#} |
| ... | ... | @@ -2013,7 +2013,7 @@ test "null terminated array" { |
| 2013 | 2013 | </ul> |
| 2014 | 2014 | <p>Use {#syntax#}&x{#endsyntax#} to obtain a single-item pointer:</p> |
| 2015 | 2015 | {#code_begin|test#} |
| 2016 | | const assert = @import("std").debug.assert; |
| 2016 | const expect = @import("std").testing.expect; |
| 2017 | 2017 | |
| 2018 | 2018 | test "address of syntax" { |
| 2019 | 2019 | // Get the address of a variable: |
| ... | ... | @@ -2021,17 +2021,17 @@ test "address of syntax" { |
| 2021 | 2021 | const x_ptr = &x; |
| 2022 | 2022 | |
| 2023 | 2023 | // Dereference a pointer: |
| 2024 | | assert(x_ptr.* == 1234); |
| 2024 | expect(x_ptr.* == 1234); |
| 2025 | 2025 | |
| 2026 | 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 | 2029 | // If you want to mutate the value, you'd need an address of a mutable variable: |
| 2030 | 2030 | var y: i32 = 5678; |
| 2031 | 2031 | const y_ptr = &y; |
| 2032 | | assert(@TypeOf(y_ptr) == *i32); |
| 2032 | expect(@TypeOf(y_ptr) == *i32); |
| 2033 | 2033 | y_ptr.* += 1; |
| 2034 | | assert(y_ptr.* == 5679); |
| 2034 | expect(y_ptr.* == 5679); |
| 2035 | 2035 | } |
| 2036 | 2036 | |
| 2037 | 2037 | test "pointer array access" { |
| ... | ... | @@ -2040,11 +2040,11 @@ test "pointer array access" { |
| 2040 | 2040 | // does not support pointer arithmetic. |
| 2041 | 2041 | var array = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; |
| 2042 | 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 | 2046 | ptr.* += 1; |
| 2047 | | assert(array[2] == 4); |
| 2047 | expect(array[2] == 4); |
| 2048 | 2048 | } |
| 2049 | 2049 | {#code_end#} |
| 2050 | 2050 | <p> |
| ... | ... | @@ -2057,22 +2057,22 @@ test "pointer array access" { |
| 2057 | 2057 | we prefer slices to pointers. |
| 2058 | 2058 | </p> |
| 2059 | 2059 | {#code_begin|test#} |
| 2060 | | const assert = @import("std").debug.assert; |
| 2060 | const expect = @import("std").testing.expect; |
| 2061 | 2061 | |
| 2062 | 2062 | test "pointer slicing" { |
| 2063 | 2063 | var array = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; |
| 2064 | 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 | 2068 | slice[1] += 1; |
| 2069 | | assert(array[3] == 5); |
| 2069 | expect(array[3] == 5); |
| 2070 | 2070 | } |
| 2071 | 2071 | {#code_end#} |
| 2072 | 2072 | <p>Pointers work at compile-time too, as long as the code does not depend on |
| 2073 | 2073 | an undefined memory layout:</p> |
| 2074 | 2074 | {#code_begin|test#} |
| 2075 | | const assert = @import("std").debug.assert; |
| 2075 | const expect = @import("std").testing.expect; |
| 2076 | 2076 | |
| 2077 | 2077 | test "comptime pointers" { |
| 2078 | 2078 | comptime { |
| ... | ... | @@ -2080,26 +2080,26 @@ test "comptime pointers" { |
| 2080 | 2080 | const ptr = &x; |
| 2081 | 2081 | ptr.* += 1; |
| 2082 | 2082 | x += 1; |
| 2083 | | assert(ptr.* == 3); |
| 2083 | expect(ptr.* == 3); |
| 2084 | 2084 | } |
| 2085 | 2085 | } |
| 2086 | 2086 | {#code_end#} |
| 2087 | 2087 | <p>To convert an integer address into a pointer, use {#syntax#}@intToPtr{#endsyntax#}. |
| 2088 | 2088 | To convert a pointer to an integer, use {#syntax#}@ptrToInt{#endsyntax#}:</p> |
| 2089 | 2089 | {#code_begin|test#} |
| 2090 | | const assert = @import("std").debug.assert; |
| 2090 | const expect = @import("std").testing.expect; |
| 2091 | 2091 | |
| 2092 | 2092 | test "@ptrToInt and @intToPtr" { |
| 2093 | 2093 | const ptr = @intToPtr(*i32, 0xdeadbee0); |
| 2094 | 2094 | const addr = @ptrToInt(ptr); |
| 2095 | | assert(@TypeOf(addr) == usize); |
| 2096 | | assert(addr == 0xdeadbee0); |
| 2095 | expect(@TypeOf(addr) == usize); |
| 2096 | expect(addr == 0xdeadbee0); |
| 2097 | 2097 | } |
| 2098 | 2098 | {#code_end#} |
| 2099 | 2099 | <p>Zig is able to preserve memory addresses in comptime code, as long as |
| 2100 | 2100 | the pointer is never dereferenced:</p> |
| 2101 | 2101 | {#code_begin|test#} |
| 2102 | | const assert = @import("std").debug.assert; |
| 2102 | const expect = @import("std").testing.expect; |
| 2103 | 2103 | |
| 2104 | 2104 | test "comptime @intToPtr" { |
| 2105 | 2105 | comptime { |
| ... | ... | @@ -2107,8 +2107,8 @@ test "comptime @intToPtr" { |
| 2107 | 2107 | // ptr is never dereferenced. |
| 2108 | 2108 | const ptr = @intToPtr(*i32, 0xdeadbee0); |
| 2109 | 2109 | const addr = @ptrToInt(ptr); |
| 2110 | | assert(@TypeOf(addr) == usize); |
| 2111 | | assert(addr == 0xdeadbee0); |
| 2110 | expect(@TypeOf(addr) == usize); |
| 2111 | expect(addr == 0xdeadbee0); |
| 2112 | 2112 | } |
| 2113 | 2113 | } |
| 2114 | 2114 | {#code_end#} |
| ... | ... | @@ -2119,11 +2119,11 @@ test "comptime @intToPtr" { |
| 2119 | 2119 | In the following code, loads and stores with {#syntax#}mmio_ptr{#endsyntax#} are guaranteed to all happen |
| 2120 | 2120 | and in the same order as in source code:</p> |
| 2121 | 2121 | {#code_begin|test#} |
| 2122 | | const assert = @import("std").debug.assert; |
| 2122 | const expect = @import("std").testing.expect; |
| 2123 | 2123 | |
| 2124 | 2124 | test "volatile" { |
| 2125 | 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 | 2128 | {#code_end#} |
| 2129 | 2129 | <p> |
| ... | ... | @@ -2139,25 +2139,25 @@ test "volatile" { |
| 2139 | 2139 | </p> |
| 2140 | 2140 | {#code_begin|test#} |
| 2141 | 2141 | const std = @import("std"); |
| 2142 | | const assert = std.debug.assert; |
| 2142 | const expect = std.testing.expect; |
| 2143 | 2143 | |
| 2144 | 2144 | test "pointer casting" { |
| 2145 | 2145 | const bytes align(@alignOf(u32)) = [_]u8{ 0x12, 0x12, 0x12, 0x12 }; |
| 2146 | 2146 | const u32_ptr = @ptrCast(*const u32, &bytes); |
| 2147 | | assert(u32_ptr.* == 0x12121212); |
| 2147 | expect(u32_ptr.* == 0x12121212); |
| 2148 | 2148 | |
| 2149 | 2149 | // Even this example is contrived - there are better ways to do the above than |
| 2150 | 2150 | // pointer casting. For example, using a slice narrowing cast: |
| 2151 | 2151 | const u32_value = std.mem.bytesAsSlice(u32, bytes[0..])[0]; |
| 2152 | | assert(u32_value == 0x12121212); |
| 2152 | expect(u32_value == 0x12121212); |
| 2153 | 2153 | |
| 2154 | 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 | 2158 | test "pointer child type" { |
| 2159 | 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 | 2162 | {#code_end#} |
| 2163 | 2163 | {#header_open|Alignment#} |
| ... | ... | @@ -2177,15 +2177,15 @@ test "pointer child type" { |
| 2177 | 2177 | </p> |
| 2178 | 2178 | {#code_begin|test#} |
| 2179 | 2179 | const std = @import("std"); |
| 2180 | | const assert = std.debug.assert; |
| 2180 | const expect = std.testing.expect; |
| 2181 | 2181 | |
| 2182 | 2182 | test "variable alignment" { |
| 2183 | 2183 | var x: i32 = 1234; |
| 2184 | 2184 | const align_of_i32 = @alignOf(@TypeOf(x)); |
| 2185 | | assert(@TypeOf(&x) == *i32); |
| 2186 | | assert(*i32 == *align(align_of_i32) i32); |
| 2185 | expect(@TypeOf(&x) == *i32); |
| 2186 | expect(*i32 == *align(align_of_i32) i32); |
| 2187 | 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 | 2191 | {#code_end#} |
| ... | ... | @@ -2198,16 +2198,16 @@ test "variable alignment" { |
| 2198 | 2198 | pointers to them get the specified alignment: |
| 2199 | 2199 | </p> |
| 2200 | 2200 | {#code_begin|test#} |
| 2201 | | const assert = @import("std").debug.assert; |
| 2201 | const expect = @import("std").testing.expect; |
| 2202 | 2202 | |
| 2203 | 2203 | var foo: u8 align(4) = 100; |
| 2204 | 2204 | |
| 2205 | 2205 | test "global variable alignment" { |
| 2206 | | assert(@typeInfo(@TypeOf(&foo)).Pointer.alignment == 4); |
| 2207 | | assert(@TypeOf(&foo) == *align(4) u8); |
| 2206 | expect(@typeInfo(@TypeOf(&foo)).Pointer.alignment == 4); |
| 2207 | expect(@TypeOf(&foo) == *align(4) u8); |
| 2208 | 2208 | const as_pointer_to_array: *[1]u8 = &foo; |
| 2209 | 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 | 2213 | fn derp() align(@sizeOf(usize) * 2) i32 { return 1234; } |
| ... | ... | @@ -2215,9 +2215,9 @@ fn noop1() align(1) void {} |
| 2215 | 2215 | fn noop4() align(4) void {} |
| 2216 | 2216 | |
| 2217 | 2217 | test "function alignment" { |
| 2218 | | assert(derp() == 1234); |
| 2219 | | assert(@TypeOf(noop1) == fn() align(1) void); |
| 2220 | | assert(@TypeOf(noop4) == fn() align(4) void); |
| 2218 | expect(derp() == 1234); |
| 2219 | expect(@TypeOf(noop1) == fn() align(1) void); |
| 2220 | expect(@TypeOf(noop4) == fn() align(4) void); |
| 2221 | 2221 | noop1(); |
| 2222 | 2222 | noop4(); |
| 2223 | 2223 | } |
| ... | ... | @@ -2234,7 +2234,7 @@ const std = @import("std"); |
| 2234 | 2234 | test "pointer alignment safety" { |
| 2235 | 2235 | var array align(4) = [_]u32{ 0x11111111, 0x11111111 }; |
| 2236 | 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 | 2239 | fn foo(bytes: []u8) u32 { |
| 2240 | 2240 | const slice4 = bytes[1..5]; |
| ... | ... | @@ -2255,12 +2255,12 @@ fn foo(bytes: []u8) u32 { |
| 2255 | 2255 | </p> |
| 2256 | 2256 | {#code_begin|test|allowzero#} |
| 2257 | 2257 | const std = @import("std"); |
| 2258 | | const assert = std.debug.assert; |
| 2258 | const expect = std.testing.expect; |
| 2259 | 2259 | |
| 2260 | 2260 | test "allowzero" { |
| 2261 | 2261 | var zero: usize = 0; |
| 2262 | 2262 | var ptr = @intToPtr(*allowzero i32, zero); |
| 2263 | | assert(@ptrToInt(ptr) == 0); |
| 2263 | expect(@ptrToInt(ptr) == 0); |
| 2264 | 2264 | } |
| 2265 | 2265 | {#code_end#} |
| 2266 | 2266 | {#header_close#} |
| ... | ... | @@ -2292,7 +2292,7 @@ pub fn main() anyerror!void { |
| 2292 | 2292 | |
| 2293 | 2293 | {#header_open|Slices#} |
| 2294 | 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 | 2297 | test "basic slices" { |
| 2298 | 2298 | var array = [_]i32{ 1, 2, 3, 4 }; |
| ... | ... | @@ -2302,14 +2302,14 @@ test "basic slices" { |
| 2302 | 2302 | // Both can be accessed with the `len` field. |
| 2303 | 2303 | var known_at_runtime_zero: usize = 0; |
| 2304 | 2304 | const slice = array[known_at_runtime_zero..array.len]; |
| 2305 | | assert(&slice[0] == &array[0]); |
| 2306 | | assert(slice.len == array.len); |
| 2305 | expect(&slice[0] == &array[0]); |
| 2306 | expect(slice.len == array.len); |
| 2307 | 2307 | |
| 2308 | 2308 | // Using the address-of operator on a slice gives a pointer to a single |
| 2309 | 2309 | // item, while using the `ptr` field gives an unknown length pointer. |
| 2310 | | assert(@TypeOf(slice.ptr) == [*]i32); |
| 2311 | | assert(@TypeOf(&slice[0]) == *i32); |
| 2312 | | assert(@ptrToInt(slice.ptr) == @ptrToInt(&slice[0])); |
| 2310 | expect(@TypeOf(slice.ptr) == [*]i32); |
| 2311 | expect(@TypeOf(&slice[0]) == *i32); |
| 2312 | expect(@ptrToInt(slice.ptr) == @ptrToInt(&slice[0])); |
| 2313 | 2313 | |
| 2314 | 2314 | // Slices have array bounds checking. If you try to access something out |
| 2315 | 2315 | // of bounds, you'll get a safety check failure: |
| ... | ... | @@ -2322,7 +2322,7 @@ test "basic slices" { |
| 2322 | 2322 | <p>This is one reason we prefer slices to pointers.</p> |
| 2323 | 2323 | {#code_begin|test|slices#} |
| 2324 | 2324 | const std = @import("std"); |
| 2325 | | const assert = std.debug.assert; |
| 2325 | const expect = std.testing.expect; |
| 2326 | 2326 | const mem = std.mem; |
| 2327 | 2327 | const fmt = std.fmt; |
| 2328 | 2328 | |
| ... | ... | @@ -2343,7 +2343,7 @@ test "using slices for strings" { |
| 2343 | 2343 | // Generally, you can use UTF-8 and not worry about whether something is a |
| 2344 | 2344 | // string. If you don't need to deal with individual characters, no need |
| 2345 | 2345 | // to decode. |
| 2346 | | assert(mem.eql(u8, hello_world, "hello δΈη")); |
| 2346 | expect(mem.eql(u8, hello_world, "hello δΈη")); |
| 2347 | 2347 | } |
| 2348 | 2348 | |
| 2349 | 2349 | test "slice pointer" { |
| ... | ... | @@ -2353,16 +2353,16 @@ test "slice pointer" { |
| 2353 | 2353 | // You can use slicing syntax to convert a pointer into a slice: |
| 2354 | 2354 | const slice = ptr[0..5]; |
| 2355 | 2355 | slice[2] = 3; |
| 2356 | | assert(slice[2] == 3); |
| 2356 | expect(slice[2] == 3); |
| 2357 | 2357 | // The slice is mutable because we sliced a mutable pointer. |
| 2358 | 2358 | // Furthermore, it is actually a pointer to an array, since the start |
| 2359 | 2359 | // and end indexes were both comptime-known. |
| 2360 | | assert(@TypeOf(slice) == *[5]u8); |
| 2360 | expect(@TypeOf(slice) == *[5]u8); |
| 2361 | 2361 | |
| 2362 | 2362 | // You can also slice a slice: |
| 2363 | 2363 | const slice2 = slice[2..3]; |
| 2364 | | assert(slice2.len == 1); |
| 2365 | | assert(slice2[0] == 3); |
| 2364 | expect(slice2.len == 1); |
| 2365 | expect(slice2[0] == 3); |
| 2366 | 2366 | } |
| 2367 | 2367 | {#code_end#} |
| 2368 | 2368 | {#see_also|Pointers|for|Arrays#} |
| ... | ... | @@ -2376,13 +2376,13 @@ test "slice pointer" { |
| 2376 | 2376 | </p> |
| 2377 | 2377 | {#code_begin|test|null_terminated_slice#} |
| 2378 | 2378 | const std = @import("std"); |
| 2379 | | const assert = std.debug.assert; |
| 2379 | const expect = std.testing.expect; |
| 2380 | 2380 | |
| 2381 | 2381 | test "null terminated slice" { |
| 2382 | 2382 | const slice: [:0]const u8 = "hello"; |
| 2383 | 2383 | |
| 2384 | | assert(slice.len == 5); |
| 2385 | | assert(slice[5] == 0); |
| 2384 | expect(slice.len == 5); |
| 2385 | expect(slice[5] == 0); |
| 2386 | 2386 | } |
| 2387 | 2387 | {#code_end#} |
| 2388 | 2388 | {#see_also|Sentinel-Terminated Pointers|Sentinel-Terminated Arrays#} |
| ... | ... | @@ -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 | 2444 | test "dot product" { |
| 2445 | 2445 | const v1 = Vec3.init(1.0, 0.0, 0.0); |
| 2446 | 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 | 2449 | // Other than being available to call with dot syntax, struct methods are |
| 2450 | 2450 | // not special. You can reference them as any other declaration inside |
| 2451 | 2451 | // the struct: |
| 2452 | | assert(Vec3.dot(v1, v2) == 0.0); |
| 2452 | expect(Vec3.dot(v1, v2) == 0.0); |
| 2453 | 2453 | } |
| 2454 | 2454 | |
| 2455 | 2455 | // Structs can have global declarations. |
| ... | ... | @@ -2458,8 +2458,8 @@ const Empty = struct { |
| 2458 | 2458 | pub const PI = 3.14; |
| 2459 | 2459 | }; |
| 2460 | 2460 | test "struct namespaced variable" { |
| 2461 | | assert(Empty.PI == 3.14); |
| 2462 | | assert(@sizeOf(Empty) == 0); |
| 2461 | expect(Empty.PI == 3.14); |
| 2462 | expect(@sizeOf(Empty) == 0); |
| 2463 | 2463 | |
| 2464 | 2464 | // you can still instantiate an empty struct |
| 2465 | 2465 | const does_nothing = Empty {}; |
| ... | ... | @@ -2477,7 +2477,7 @@ test "field parent pointer" { |
| 2477 | 2477 | .y = 0.5678, |
| 2478 | 2478 | }; |
| 2479 | 2479 | setYBasedOnX(&point.x, 0.9); |
| 2480 | | assert(point.y == 0.9); |
| 2480 | expect(point.y == 0.9); |
| 2481 | 2481 | } |
| 2482 | 2482 | |
| 2483 | 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 | 2499 | test "linked list" { |
| 2500 | 2500 | // Functions called at compile-time are memoized. This means you can |
| 2501 | 2501 | // do this: |
| 2502 | | assert(LinkedList(i32) == LinkedList(i32)); |
| 2502 | expect(LinkedList(i32) == LinkedList(i32)); |
| 2503 | 2503 | |
| 2504 | 2504 | var list = LinkedList(i32) { |
| 2505 | 2505 | .first = null, |
| 2506 | 2506 | .last = null, |
| 2507 | 2507 | .len = 0, |
| 2508 | 2508 | }; |
| 2509 | | assert(list.len == 0); |
| 2509 | expect(list.len == 0); |
| 2510 | 2510 | |
| 2511 | 2511 | // Since types are first class values you can instantiate the type |
| 2512 | 2512 | // by assigning it to a variable: |
| 2513 | 2513 | const ListOfInts = LinkedList(i32); |
| 2514 | | assert(ListOfInts == LinkedList(i32)); |
| 2514 | expect(ListOfInts == LinkedList(i32)); |
| 2515 | 2515 | |
| 2516 | 2516 | var node = ListOfInts.Node { |
| 2517 | 2517 | .prev = null, |
| ... | ... | @@ -2523,7 +2523,7 @@ test "linked list" { |
| 2523 | 2523 | .last = &node, |
| 2524 | 2524 | .len = 1, |
| 2525 | 2525 | }; |
| 2526 | | assert(list2.first.?.data == 1234); |
| 2526 | expect(list2.first.?.data == 1234); |
| 2527 | 2527 | } |
| 2528 | 2528 | {#code_end#} |
| 2529 | 2529 | |
| ... | ... | @@ -2584,7 +2584,7 @@ test "default struct initialization fields" { |
| 2584 | 2584 | {#code_begin|test#} |
| 2585 | 2585 | const std = @import("std"); |
| 2586 | 2586 | const builtin = std.builtin; |
| 2587 | | const assert = std.debug.assert; |
| 2587 | const expect = std.testing.expect; |
| 2588 | 2588 | |
| 2589 | 2589 | const Full = packed struct { |
| 2590 | 2590 | number: u16, |
| ... | ... | @@ -2601,20 +2601,20 @@ test "@bitCast between packed structs" { |
| 2601 | 2601 | } |
| 2602 | 2602 | |
| 2603 | 2603 | fn doTheTest() void { |
| 2604 | | assert(@sizeOf(Full) == 2); |
| 2605 | | assert(@sizeOf(Divided) == 2); |
| 2604 | expect(@sizeOf(Full) == 2); |
| 2605 | expect(@sizeOf(Divided) == 2); |
| 2606 | 2606 | var full = Full{ .number = 0x1234 }; |
| 2607 | 2607 | var divided = @bitCast(Divided, full); |
| 2608 | 2608 | switch (builtin.endian) { |
| 2609 | 2609 | .Big => { |
| 2610 | | assert(divided.half1 == 0x12); |
| 2611 | | assert(divided.quarter3 == 0x3); |
| 2612 | | assert(divided.quarter4 == 0x4); |
| 2610 | expect(divided.half1 == 0x12); |
| 2611 | expect(divided.quarter3 == 0x3); |
| 2612 | expect(divided.quarter4 == 0x4); |
| 2613 | 2613 | }, |
| 2614 | 2614 | .Little => { |
| 2615 | | assert(divided.half1 == 0x34); |
| 2616 | | assert(divided.quarter3 == 0x2); |
| 2617 | | assert(divided.quarter4 == 0x1); |
| 2615 | expect(divided.half1 == 0x34); |
| 2616 | expect(divided.quarter3 == 0x2); |
| 2617 | expect(divided.quarter4 == 0x1); |
| 2618 | 2618 | }, |
| 2619 | 2619 | } |
| 2620 | 2620 | } |
| ... | ... | @@ -2624,7 +2624,7 @@ fn doTheTest() void { |
| 2624 | 2624 | </p> |
| 2625 | 2625 | {#code_begin|test#} |
| 2626 | 2626 | const std = @import("std"); |
| 2627 | | const assert = std.debug.assert; |
| 2627 | const expect = std.testing.expect; |
| 2628 | 2628 | |
| 2629 | 2629 | const BitField = packed struct { |
| 2630 | 2630 | a: u3, |
| ... | ... | @@ -2640,7 +2640,7 @@ var foo = BitField{ |
| 2640 | 2640 | |
| 2641 | 2641 | test "pointer to non-byte-aligned field" { |
| 2642 | 2642 | const ptr = &foo.b; |
| 2643 | | assert(ptr.* == 2); |
| 2643 | expect(ptr.* == 2); |
| 2644 | 2644 | } |
| 2645 | 2645 | {#code_end#} |
| 2646 | 2646 | <p> |
| ... | ... | @@ -2649,7 +2649,7 @@ test "pointer to non-byte-aligned field" { |
| 2649 | 2649 | </p> |
| 2650 | 2650 | {#code_begin|test_err|expected type#} |
| 2651 | 2651 | const std = @import("std"); |
| 2652 | | const assert = std.debug.assert; |
| 2652 | const expect = std.testing.expect; |
| 2653 | 2653 | |
| 2654 | 2654 | const BitField = packed struct { |
| 2655 | 2655 | a: u3, |
| ... | ... | @@ -2664,7 +2664,7 @@ var bit_field = BitField{ |
| 2664 | 2664 | }; |
| 2665 | 2665 | |
| 2666 | 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 | 2670 | fn bar(x: *const u3) u3 { |
| ... | ... | @@ -2680,7 +2680,7 @@ fn bar(x: *const u3) u3 { |
| 2680 | 2680 | </p> |
| 2681 | 2681 | {#code_begin|test#} |
| 2682 | 2682 | const std = @import("std"); |
| 2683 | | const assert = std.debug.assert; |
| 2683 | const expect = std.testing.expect; |
| 2684 | 2684 | |
| 2685 | 2685 | const BitField = packed struct { |
| 2686 | 2686 | a: u3, |
| ... | ... | @@ -2695,8 +2695,8 @@ var bit_field = BitField{ |
| 2695 | 2695 | }; |
| 2696 | 2696 | |
| 2697 | 2697 | test "pointer to non-bit-aligned field" { |
| 2698 | | assert(@ptrToInt(&bit_field.a) == @ptrToInt(&bit_field.b)); |
| 2699 | | assert(@ptrToInt(&bit_field.a) == @ptrToInt(&bit_field.c)); |
| 2698 | expect(@ptrToInt(&bit_field.a) == @ptrToInt(&bit_field.b)); |
| 2699 | expect(@ptrToInt(&bit_field.a) == @ptrToInt(&bit_field.c)); |
| 2700 | 2700 | } |
| 2701 | 2701 | {#code_end#} |
| 2702 | 2702 | <p> |
| ... | ... | @@ -2704,7 +2704,7 @@ test "pointer to non-bit-aligned field" { |
| 2704 | 2704 | </p> |
| 2705 | 2705 | {#code_begin|test#} |
| 2706 | 2706 | const std = @import("std"); |
| 2707 | | const assert = std.debug.assert; |
| 2707 | const expect = std.testing.expect; |
| 2708 | 2708 | |
| 2709 | 2709 | const BitField = packed struct { |
| 2710 | 2710 | a: u3, |
| ... | ... | @@ -2714,13 +2714,13 @@ const BitField = packed struct { |
| 2714 | 2714 | |
| 2715 | 2715 | test "pointer to non-bit-aligned field" { |
| 2716 | 2716 | comptime { |
| 2717 | | assert(@bitOffsetOf(BitField, "a") == 0); |
| 2718 | | assert(@bitOffsetOf(BitField, "b") == 3); |
| 2719 | | assert(@bitOffsetOf(BitField, "c") == 6); |
| 2717 | expect(@bitOffsetOf(BitField, "a") == 0); |
| 2718 | expect(@bitOffsetOf(BitField, "b") == 3); |
| 2719 | expect(@bitOffsetOf(BitField, "c") == 6); |
| 2720 | 2720 | |
| 2721 | | assert(@byteOffsetOf(BitField, "a") == 0); |
| 2722 | | assert(@byteOffsetOf(BitField, "b") == 0); |
| 2723 | | assert(@byteOffsetOf(BitField, "c") == 0); |
| 2721 | expect(@byteOffsetOf(BitField, "a") == 0); |
| 2722 | expect(@byteOffsetOf(BitField, "b") == 0); |
| 2723 | expect(@byteOffsetOf(BitField, "c") == 0); |
| 2724 | 2724 | } |
| 2725 | 2725 | } |
| 2726 | 2726 | {#code_end#} |
| ... | ... | @@ -2791,7 +2791,7 @@ fn List(comptime T: type) type { |
| 2791 | 2791 | </p> |
| 2792 | 2792 | {#code_begin|test|struct_result#} |
| 2793 | 2793 | const std = @import("std"); |
| 2794 | | const assert = std.debug.assert; |
| 2794 | const expect = std.testing.expect; |
| 2795 | 2795 | |
| 2796 | 2796 | const Point = struct {x: i32, y: i32}; |
| 2797 | 2797 | |
| ... | ... | @@ -2800,8 +2800,8 @@ test "anonymous struct literal" { |
| 2800 | 2800 | .x = 13, |
| 2801 | 2801 | .y = 67, |
| 2802 | 2802 | }; |
| 2803 | | assert(pt.x == 13); |
| 2804 | | assert(pt.y == 67); |
| 2803 | expect(pt.x == 13); |
| 2804 | expect(pt.y == 67); |
| 2805 | 2805 | } |
| 2806 | 2806 | {#code_end#} |
| 2807 | 2807 | <p> |
| ... | ... | @@ -2810,7 +2810,7 @@ test "anonymous struct literal" { |
| 2810 | 2810 | </p> |
| 2811 | 2811 | {#code_begin|test|struct_anon#} |
| 2812 | 2812 | const std = @import("std"); |
| 2813 | | const assert = std.debug.assert; |
| 2813 | const expect = std.testing.expect; |
| 2814 | 2814 | |
| 2815 | 2815 | test "fully anonymous struct" { |
| 2816 | 2816 | dump(.{ |
| ... | ... | @@ -2822,11 +2822,11 @@ test "fully anonymous struct" { |
| 2822 | 2822 | } |
| 2823 | 2823 | |
| 2824 | 2824 | fn dump(args: anytype) void { |
| 2825 | | assert(args.int == 1234); |
| 2826 | | assert(args.float == 12.34); |
| 2827 | | assert(args.b); |
| 2828 | | assert(args.s[0] == 'h'); |
| 2829 | | assert(args.s[1] == 'i'); |
| 2825 | expect(args.int == 1234); |
| 2826 | expect(args.float == 12.34); |
| 2827 | expect(args.b); |
| 2828 | expect(args.s[0] == 'h'); |
| 2829 | expect(args.s[1] == 'i'); |
| 2830 | 2830 | } |
| 2831 | 2831 | {#code_end#} |
| 2832 | 2832 | {#header_close#} |
| ... | ... | @@ -2834,7 +2834,7 @@ fn dump(args: anytype) void { |
| 2834 | 2834 | {#header_close#} |
| 2835 | 2835 | {#header_open|enum#} |
| 2836 | 2836 | {#code_begin|test|enums#} |
| 2837 | | const assert = @import("std").debug.assert; |
| 2837 | const expect = @import("std").testing.expect; |
| 2838 | 2838 | const mem = @import("std").mem; |
| 2839 | 2839 | |
| 2840 | 2840 | // Declare an enum. |
| ... | ... | @@ -2857,9 +2857,9 @@ const Value = enum(u2) { |
| 2857 | 2857 | // Now you can cast between u2 and Value. |
| 2858 | 2858 | // The ordinal value starts from 0, counting up for each member. |
| 2859 | 2859 | test "enum ordinal value" { |
| 2860 | | assert(@enumToInt(Value.zero) == 0); |
| 2861 | | assert(@enumToInt(Value.one) == 1); |
| 2862 | | assert(@enumToInt(Value.two) == 2); |
| 2860 | expect(@enumToInt(Value.zero) == 0); |
| 2861 | expect(@enumToInt(Value.one) == 1); |
| 2862 | expect(@enumToInt(Value.two) == 2); |
| 2863 | 2863 | } |
| 2864 | 2864 | |
| 2865 | 2865 | // You can override the ordinal value for an enum. |
| ... | ... | @@ -2869,9 +2869,9 @@ const Value2 = enum(u32) { |
| 2869 | 2869 | million = 1000000, |
| 2870 | 2870 | }; |
| 2871 | 2871 | test "set enum ordinal value" { |
| 2872 | | assert(@enumToInt(Value2.hundred) == 100); |
| 2873 | | assert(@enumToInt(Value2.thousand) == 1000); |
| 2874 | | assert(@enumToInt(Value2.million) == 1000000); |
| 2872 | expect(@enumToInt(Value2.hundred) == 100); |
| 2873 | expect(@enumToInt(Value2.thousand) == 1000); |
| 2874 | expect(@enumToInt(Value2.million) == 1000000); |
| 2875 | 2875 | } |
| 2876 | 2876 | |
| 2877 | 2877 | // Enums can have methods, the same as structs and unions. |
| ... | ... | @@ -2889,7 +2889,7 @@ const Suit = enum { |
| 2889 | 2889 | }; |
| 2890 | 2890 | test "enum method" { |
| 2891 | 2891 | const p = Suit.spades; |
| 2892 | | assert(!p.isClubs()); |
| 2892 | expect(!p.isClubs()); |
| 2893 | 2893 | } |
| 2894 | 2894 | |
| 2895 | 2895 | // An enum variant of different types can be switched upon. |
| ... | ... | @@ -2905,7 +2905,7 @@ test "enum variant switch" { |
| 2905 | 2905 | Foo.number => "this is a number", |
| 2906 | 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 | 2911 | // @TagType can be used to access the integer tag type of an enum. |
| ... | ... | @@ -2916,18 +2916,18 @@ const Small = enum { |
| 2916 | 2916 | four, |
| 2917 | 2917 | }; |
| 2918 | 2918 | test "@TagType" { |
| 2919 | | assert(@TagType(Small) == u2); |
| 2919 | expect(@TagType(Small) == u2); |
| 2920 | 2920 | } |
| 2921 | 2921 | |
| 2922 | 2922 | // @typeInfo tells us the field count and the fields names: |
| 2923 | 2923 | test "@typeInfo" { |
| 2924 | | assert(@typeInfo(Small).Enum.fields.len == 4); |
| 2925 | | assert(mem.eql(u8, @typeInfo(Small).Enum.fields[1].name, "two")); |
| 2924 | expect(@typeInfo(Small).Enum.fields.len == 4); |
| 2925 | expect(mem.eql(u8, @typeInfo(Small).Enum.fields[1].name, "two")); |
| 2926 | 2926 | } |
| 2927 | 2927 | |
| 2928 | 2928 | // @tagName gives a []const u8 representation of an enum value: |
| 2929 | 2929 | test "@tagName" { |
| 2930 | | assert(mem.eql(u8, @tagName(Small.three), "three")); |
| 2930 | expect(mem.eql(u8, @tagName(Small.three), "three")); |
| 2931 | 2931 | } |
| 2932 | 2932 | {#code_end#} |
| 2933 | 2933 | {#see_also|@typeInfo|@tagName|@sizeOf#} |
| ... | ... | @@ -2962,7 +2962,7 @@ test "packed enum" { |
| 2962 | 2962 | two, |
| 2963 | 2963 | three, |
| 2964 | 2964 | }; |
| 2965 | | std.debug.assert(@sizeOf(Number) == @sizeOf(u8)); |
| 2965 | std.testing.expect(@sizeOf(Number) == @sizeOf(u8)); |
| 2966 | 2966 | } |
| 2967 | 2967 | {#code_end#} |
| 2968 | 2968 | <p>This makes the enum eligible to be in a {#link|packed struct#}.</p> |
| ... | ... | @@ -2974,7 +2974,7 @@ test "packed enum" { |
| 2974 | 2974 | </p> |
| 2975 | 2975 | {#code_begin|test#} |
| 2976 | 2976 | const std = @import("std"); |
| 2977 | | const assert = std.debug.assert; |
| 2977 | const expect = std.testing.expect; |
| 2978 | 2978 | |
| 2979 | 2979 | const Color = enum { |
| 2980 | 2980 | auto, |
| ... | ... | @@ -2985,7 +2985,7 @@ const Color = enum { |
| 2985 | 2985 | test "enum literals" { |
| 2986 | 2986 | const color1: Color = .auto; |
| 2987 | 2987 | const color2 = Color.auto; |
| 2988 | | assert(color1 == color2); |
| 2988 | expect(color1 == color2); |
| 2989 | 2989 | } |
| 2990 | 2990 | |
| 2991 | 2991 | test "switch using enum literals" { |
| ... | ... | @@ -2995,7 +2995,7 @@ test "switch using enum literals" { |
| 2995 | 2995 | .on => true, |
| 2996 | 2996 | .off => false, |
| 2997 | 2997 | }; |
| 2998 | | assert(result); |
| 2998 | expect(result); |
| 2999 | 2999 | } |
| 3000 | 3000 | {#code_end#} |
| 3001 | 3001 | {#header_close#} |
| ... | ... | @@ -3014,7 +3014,7 @@ test "switch using enum literals" { |
| 3014 | 3014 | </p> |
| 3015 | 3015 | {#code_begin|test#} |
| 3016 | 3016 | const std = @import("std"); |
| 3017 | | const assert = std.debug.assert; |
| 3017 | const expect = std.testing.expect; |
| 3018 | 3018 | |
| 3019 | 3019 | const Number = enum(u8) { |
| 3020 | 3020 | one, |
| ... | ... | @@ -3031,12 +3031,12 @@ test "switch on non-exhaustive enum" { |
| 3031 | 3031 | .three => false, |
| 3032 | 3032 | _ => false, |
| 3033 | 3033 | }; |
| 3034 | | assert(result); |
| 3034 | expect(result); |
| 3035 | 3035 | const is_one = switch (number) { |
| 3036 | 3036 | .one => true, |
| 3037 | 3037 | else => false, |
| 3038 | 3038 | }; |
| 3039 | | assert(is_one); |
| 3039 | expect(is_one); |
| 3040 | 3040 | } |
| 3041 | 3041 | {#code_end#} |
| 3042 | 3042 | {#header_close#} |
| ... | ... | @@ -3067,7 +3067,7 @@ test "simple union" { |
| 3067 | 3067 | <p>You can activate another field by assigning the entire union:</p> |
| 3068 | 3068 | {#code_begin|test#} |
| 3069 | 3069 | const std = @import("std"); |
| 3070 | | const assert = std.debug.assert; |
| 3070 | const expect = std.testing.expect; |
| 3071 | 3071 | |
| 3072 | 3072 | const Payload = union { |
| 3073 | 3073 | int: i64, |
| ... | ... | @@ -3076,9 +3076,9 @@ const Payload = union { |
| 3076 | 3076 | }; |
| 3077 | 3077 | test "simple union" { |
| 3078 | 3078 | var payload = Payload{ .int = 1234 }; |
| 3079 | | assert(payload.int == 1234); |
| 3079 | expect(payload.int == 1234); |
| 3080 | 3080 | payload = Payload{ .float = 12.34 }; |
| 3081 | | assert(payload.float == 12.34); |
| 3081 | expect(payload.float == 12.34); |
| 3082 | 3082 | } |
| 3083 | 3083 | {#code_end#} |
| 3084 | 3084 | <p> |
| ... | ... | @@ -3097,7 +3097,7 @@ test "simple union" { |
| 3097 | 3097 | </p> |
| 3098 | 3098 | {#code_begin|test#} |
| 3099 | 3099 | const std = @import("std"); |
| 3100 | | const assert = std.debug.assert; |
| 3100 | const expect = std.testing.expect; |
| 3101 | 3101 | |
| 3102 | 3102 | const ComplexTypeTag = enum { |
| 3103 | 3103 | ok, |
| ... | ... | @@ -3110,24 +3110,24 @@ const ComplexType = union(ComplexTypeTag) { |
| 3110 | 3110 | |
| 3111 | 3111 | test "switch on tagged union" { |
| 3112 | 3112 | const c = ComplexType{ .ok = 42 }; |
| 3113 | | assert(@as(ComplexTypeTag, c) == ComplexTypeTag.ok); |
| 3113 | expect(@as(ComplexTypeTag, c) == ComplexTypeTag.ok); |
| 3114 | 3114 | |
| 3115 | 3115 | switch (c) { |
| 3116 | | ComplexTypeTag.ok => |value| assert(value == 42), |
| 3116 | ComplexTypeTag.ok => |value| expect(value == 42), |
| 3117 | 3117 | ComplexTypeTag.not_ok => unreachable, |
| 3118 | 3118 | } |
| 3119 | 3119 | } |
| 3120 | 3120 | |
| 3121 | 3121 | test "@TagType" { |
| 3122 | | assert(@TagType(ComplexType) == ComplexTypeTag); |
| 3122 | expect(@TagType(ComplexType) == ComplexTypeTag); |
| 3123 | 3123 | } |
| 3124 | 3124 | |
| 3125 | 3125 | test "coerce to enum" { |
| 3126 | 3126 | const c1 = ComplexType{ .ok = 42 }; |
| 3127 | 3127 | const c2 = ComplexType.not_ok; |
| 3128 | 3128 | |
| 3129 | | assert(c1 == .ok); |
| 3130 | | assert(c2 == .not_ok); |
| 3129 | expect(c1 == .ok); |
| 3130 | expect(c2 == .not_ok); |
| 3131 | 3131 | } |
| 3132 | 3132 | {#code_end#} |
| 3133 | 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 | 3135 | </p> |
| 3136 | 3136 | {#code_begin|test#} |
| 3137 | 3137 | const std = @import("std"); |
| 3138 | | const assert = std.debug.assert; |
| 3138 | const expect = std.testing.expect; |
| 3139 | 3139 | |
| 3140 | 3140 | const ComplexTypeTag = enum { |
| 3141 | 3141 | ok, |
| ... | ... | @@ -3148,14 +3148,14 @@ const ComplexType = union(ComplexTypeTag) { |
| 3148 | 3148 | |
| 3149 | 3149 | test "modify tagged union in switch" { |
| 3150 | 3150 | var c = ComplexType{ .ok = 42 }; |
| 3151 | | assert(@as(ComplexTypeTag, c) == ComplexTypeTag.ok); |
| 3151 | expect(@as(ComplexTypeTag, c) == ComplexTypeTag.ok); |
| 3152 | 3152 | |
| 3153 | 3153 | switch (c) { |
| 3154 | 3154 | ComplexTypeTag.ok => |*value| value.* += 1, |
| 3155 | 3155 | ComplexTypeTag.not_ok => unreachable, |
| 3156 | 3156 | } |
| 3157 | 3157 | |
| 3158 | | assert(c.ok == 43); |
| 3158 | expect(c.ok == 43); |
| 3159 | 3159 | } |
| 3160 | 3160 | {#code_end#} |
| 3161 | 3161 | <p> |
| ... | ... | @@ -3164,7 +3164,7 @@ test "modify tagged union in switch" { |
| 3164 | 3164 | </p> |
| 3165 | 3165 | {#code_begin|test#} |
| 3166 | 3166 | const std = @import("std"); |
| 3167 | | const assert = std.debug.assert; |
| 3167 | const expect = std.testing.expect; |
| 3168 | 3168 | |
| 3169 | 3169 | const Variant = union(enum) { |
| 3170 | 3170 | int: i32, |
| ... | ... | @@ -3186,8 +3186,8 @@ test "union method" { |
| 3186 | 3186 | var v1 = Variant{ .int = 1 }; |
| 3187 | 3187 | var v2 = Variant{ .boolean = false }; |
| 3188 | 3188 | |
| 3189 | | assert(v1.truthy()); |
| 3190 | | assert(!v2.truthy()); |
| 3189 | expect(v1.truthy()); |
| 3190 | expect(!v2.truthy()); |
| 3191 | 3191 | } |
| 3192 | 3192 | {#code_end#} |
| 3193 | 3193 | <p> |
| ... | ... | @@ -3196,7 +3196,7 @@ test "union method" { |
| 3196 | 3196 | </p> |
| 3197 | 3197 | {#code_begin|test#} |
| 3198 | 3198 | const std = @import("std"); |
| 3199 | | const assert = std.debug.assert; |
| 3199 | const expect = std.testing.expect; |
| 3200 | 3200 | |
| 3201 | 3201 | const Small2 = union(enum) { |
| 3202 | 3202 | a: i32, |
| ... | ... | @@ -3204,7 +3204,7 @@ const Small2 = union(enum) { |
| 3204 | 3204 | c: u8, |
| 3205 | 3205 | }; |
| 3206 | 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 | 3209 | {#code_end#} |
| 3210 | 3210 | {#header_close#} |
| ... | ... | @@ -3227,7 +3227,7 @@ test "@tagName" { |
| 3227 | 3227 | the type:</p> |
| 3228 | 3228 | {#code_begin|test|anon_union#} |
| 3229 | 3229 | const std = @import("std"); |
| 3230 | | const assert = std.debug.assert; |
| 3230 | const expect = std.testing.expect; |
| 3231 | 3231 | |
| 3232 | 3232 | const Number = union { |
| 3233 | 3233 | int: i32, |
| ... | ... | @@ -3237,8 +3237,8 @@ const Number = union { |
| 3237 | 3237 | test "anonymous union literal syntax" { |
| 3238 | 3238 | var i: Number = .{.int = 42}; |
| 3239 | 3239 | var f = makeNumber(); |
| 3240 | | assert(i.int == 42); |
| 3241 | | assert(f.float == 12.34); |
| 3240 | expect(i.int == 42); |
| 3241 | expect(f.float == 12.34); |
| 3242 | 3242 | } |
| 3243 | 3243 | |
| 3244 | 3244 | fn makeNumber() Number { |
| ... | ... | @@ -3291,7 +3291,7 @@ test "access variable after block scope" { |
| 3291 | 3291 | </p> |
| 3292 | 3292 | {#code_begin|test#} |
| 3293 | 3293 | const std = @import("std"); |
| 3294 | | const assert = std.debug.assert; |
| 3294 | const expect = std.testing.expect; |
| 3295 | 3295 | |
| 3296 | 3296 | test "labeled break from labeled block expression" { |
| 3297 | 3297 | var y: i32 = 123; |
| ... | ... | @@ -3300,8 +3300,8 @@ test "labeled break from labeled block expression" { |
| 3300 | 3300 | y += 1; |
| 3301 | 3301 | break :blk y; |
| 3302 | 3302 | }; |
| 3303 | | assert(x == 124); |
| 3304 | | assert(y == 124); |
| 3303 | expect(x == 124); |
| 3304 | expect(y == 124); |
| 3305 | 3305 | } |
| 3306 | 3306 | {#code_end#} |
| 3307 | 3307 | <p>Here, {#syntax#}blk{#endsyntax#} can be any name.</p> |
| ... | ... | @@ -3339,7 +3339,7 @@ test "separate scopes" { |
| 3339 | 3339 | {#header_open|switch#} |
| 3340 | 3340 | {#code_begin|test|switch#} |
| 3341 | 3341 | const std = @import("std"); |
| 3342 | | const assert = std.debug.assert; |
| 3342 | const expect = std.testing.expect; |
| 3343 | 3343 | |
| 3344 | 3344 | test "switch simple" { |
| 3345 | 3345 | const a: u64 = 10; |
| ... | ... | @@ -3379,7 +3379,7 @@ test "switch simple" { |
| 3379 | 3379 | else => 9, |
| 3380 | 3380 | }; |
| 3381 | 3381 | |
| 3382 | | assert(b == 1); |
| 3382 | expect(b == 1); |
| 3383 | 3383 | } |
| 3384 | 3384 | |
| 3385 | 3385 | // Switch expressions can be used outside a function: |
| ... | ... | @@ -3409,7 +3409,7 @@ test "switch inside function" { |
| 3409 | 3409 | turning it into a pointer. |
| 3410 | 3410 | </p> |
| 3411 | 3411 | {#code_begin|test#} |
| 3412 | | const assert = @import("std").debug.assert; |
| 3412 | const expect = @import("std").testing.expect; |
| 3413 | 3413 | |
| 3414 | 3414 | test "switch on tagged union" { |
| 3415 | 3415 | const Point = struct { |
| ... | ... | @@ -3442,8 +3442,8 @@ test "switch on tagged union" { |
| 3442 | 3442 | Item.d => 8, |
| 3443 | 3443 | }; |
| 3444 | 3444 | |
| 3445 | | assert(b == 6); |
| 3446 | | assert(a.c.x == 2); |
| 3445 | expect(b == 6); |
| 3446 | expect(a.c.x == 2); |
| 3447 | 3447 | } |
| 3448 | 3448 | {#code_end#} |
| 3449 | 3449 | {#see_also|comptime|enum|@compileError|Compile Variables#} |
| ... | ... | @@ -3477,7 +3477,7 @@ test "exhaustive switching" { |
| 3477 | 3477 | </p> |
| 3478 | 3478 | {#code_begin|test#} |
| 3479 | 3479 | const std = @import("std"); |
| 3480 | | const assert = std.debug.assert; |
| 3480 | const expect = std.testing.expect; |
| 3481 | 3481 | |
| 3482 | 3482 | const Color = enum { |
| 3483 | 3483 | auto, |
| ... | ... | @@ -3492,7 +3492,7 @@ test "enum literals with switch" { |
| 3492 | 3492 | .on => false, |
| 3493 | 3493 | .off => true, |
| 3494 | 3494 | }; |
| 3495 | | assert(result); |
| 3495 | expect(result); |
| 3496 | 3496 | } |
| 3497 | 3497 | {#code_end#} |
| 3498 | 3498 | {#header_close#} |
| ... | ... | @@ -3504,21 +3504,21 @@ test "enum literals with switch" { |
| 3504 | 3504 | some condition is no longer true. |
| 3505 | 3505 | </p> |
| 3506 | 3506 | {#code_begin|test|while#} |
| 3507 | | const assert = @import("std").debug.assert; |
| 3507 | const expect = @import("std").testing.expect; |
| 3508 | 3508 | |
| 3509 | 3509 | test "while basic" { |
| 3510 | 3510 | var i: usize = 0; |
| 3511 | 3511 | while (i < 10) { |
| 3512 | 3512 | i += 1; |
| 3513 | 3513 | } |
| 3514 | | assert(i == 10); |
| 3514 | expect(i == 10); |
| 3515 | 3515 | } |
| 3516 | 3516 | {#code_end#} |
| 3517 | 3517 | <p> |
| 3518 | 3518 | Use {#syntax#}break{#endsyntax#} to exit a while loop early. |
| 3519 | 3519 | </p> |
| 3520 | 3520 | {#code_begin|test|while#} |
| 3521 | | const assert = @import("std").debug.assert; |
| 3521 | const expect = @import("std").testing.expect; |
| 3522 | 3522 | |
| 3523 | 3523 | test "while break" { |
| 3524 | 3524 | var i: usize = 0; |
| ... | ... | @@ -3527,14 +3527,14 @@ test "while break" { |
| 3527 | 3527 | break; |
| 3528 | 3528 | i += 1; |
| 3529 | 3529 | } |
| 3530 | | assert(i == 10); |
| 3530 | expect(i == 10); |
| 3531 | 3531 | } |
| 3532 | 3532 | {#code_end#} |
| 3533 | 3533 | <p> |
| 3534 | 3534 | Use {#syntax#}continue{#endsyntax#} to jump back to the beginning of the loop. |
| 3535 | 3535 | </p> |
| 3536 | 3536 | {#code_begin|test|while#} |
| 3537 | | const assert = @import("std").debug.assert; |
| 3537 | const expect = @import("std").testing.expect; |
| 3538 | 3538 | |
| 3539 | 3539 | test "while continue" { |
| 3540 | 3540 | var i: usize = 0; |
| ... | ... | @@ -3544,7 +3544,7 @@ test "while continue" { |
| 3544 | 3544 | continue; |
| 3545 | 3545 | break; |
| 3546 | 3546 | } |
| 3547 | | assert(i == 10); |
| 3547 | expect(i == 10); |
| 3548 | 3548 | } |
| 3549 | 3549 | {#code_end#} |
| 3550 | 3550 | <p> |
| ... | ... | @@ -3552,12 +3552,12 @@ test "while continue" { |
| 3552 | 3552 | is continued. The {#syntax#}continue{#endsyntax#} keyword respects this expression. |
| 3553 | 3553 | </p> |
| 3554 | 3554 | {#code_begin|test|while#} |
| 3555 | | const assert = @import("std").debug.assert; |
| 3555 | const expect = @import("std").testing.expect; |
| 3556 | 3556 | |
| 3557 | 3557 | test "while loop continue expression" { |
| 3558 | 3558 | var i: usize = 0; |
| 3559 | 3559 | while (i < 10) : (i += 1) {} |
| 3560 | | assert(i == 10); |
| 3560 | expect(i == 10); |
| 3561 | 3561 | } |
| 3562 | 3562 | |
| 3563 | 3563 | test "while loop continue expression, more complicated" { |
| ... | ... | @@ -3565,7 +3565,7 @@ test "while loop continue expression, more complicated" { |
| 3565 | 3565 | var j: usize = 1; |
| 3566 | 3566 | while (i * j < 2000) : ({ i *= 2; j *= 3; }) { |
| 3567 | 3567 | const my_ij = i * j; |
| 3568 | | assert(my_ij < 2000); |
| 3568 | expect(my_ij < 2000); |
| 3569 | 3569 | } |
| 3570 | 3570 | } |
| 3571 | 3571 | {#code_end#} |
| ... | ... | @@ -3581,11 +3581,11 @@ test "while loop continue expression, more complicated" { |
| 3581 | 3581 | evaluated. |
| 3582 | 3582 | </p> |
| 3583 | 3583 | {#code_begin|test|while#} |
| 3584 | | const assert = @import("std").debug.assert; |
| 3584 | const expect = @import("std").testing.expect; |
| 3585 | 3585 | |
| 3586 | 3586 | test "while else" { |
| 3587 | | assert(rangeHasNumber(0, 10, 5)); |
| 3588 | | assert(!rangeHasNumber(0, 10, 15)); |
| 3587 | expect(rangeHasNumber(0, 10, 5)); |
| 3588 | expect(!rangeHasNumber(0, 10, 15)); |
| 3589 | 3589 | } |
| 3590 | 3590 | |
| 3591 | 3591 | fn rangeHasNumber(begin: usize, end: usize, number: usize) bool { |
| ... | ... | @@ -3634,7 +3634,7 @@ test "nested continue" { |
| 3634 | 3634 | be executed on the first null value encountered. |
| 3635 | 3635 | </p> |
| 3636 | 3636 | {#code_begin|test|while#} |
| 3637 | | const assert = @import("std").debug.assert; |
| 3637 | const expect = @import("std").testing.expect; |
| 3638 | 3638 | |
| 3639 | 3639 | test "while null capture" { |
| 3640 | 3640 | var sum1: u32 = 0; |
| ... | ... | @@ -3642,14 +3642,14 @@ test "while null capture" { |
| 3642 | 3642 | while (eventuallyNullSequence()) |value| { |
| 3643 | 3643 | sum1 += value; |
| 3644 | 3644 | } |
| 3645 | | assert(sum1 == 3); |
| 3645 | expect(sum1 == 3); |
| 3646 | 3646 | |
| 3647 | 3647 | var sum2: u32 = 0; |
| 3648 | 3648 | numbers_left = 3; |
| 3649 | 3649 | while (eventuallyNullSequence()) |value| { |
| 3650 | 3650 | sum2 += value; |
| 3651 | 3651 | } else { |
| 3652 | | assert(sum2 == 3); |
| 3652 | expect(sum2 == 3); |
| 3653 | 3653 | } |
| 3654 | 3654 | } |
| 3655 | 3655 | |
| ... | ... | @@ -3676,7 +3676,7 @@ fn eventuallyNullSequence() ?u32 { |
| 3676 | 3676 | the while condition must have an {#link|Error Union Type#}. |
| 3677 | 3677 | </p> |
| 3678 | 3678 | {#code_begin|test|while#} |
| 3679 | | const assert = @import("std").debug.assert; |
| 3679 | const expect = @import("std").testing.expect; |
| 3680 | 3680 | |
| 3681 | 3681 | test "while error union capture" { |
| 3682 | 3682 | var sum1: u32 = 0; |
| ... | ... | @@ -3684,7 +3684,7 @@ test "while error union capture" { |
| 3684 | 3684 | while (eventuallyErrorSequence()) |value| { |
| 3685 | 3685 | sum1 += value; |
| 3686 | 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 | 3706 | such as use types as first class values. |
| 3707 | 3707 | </p> |
| 3708 | 3708 | {#code_begin|test#} |
| 3709 | | const assert = @import("std").debug.assert; |
| 3709 | const expect = @import("std").testing.expect; |
| 3710 | 3710 | |
| 3711 | 3711 | test "inline while loop" { |
| 3712 | 3712 | comptime var i = 0; |
| ... | ... | @@ -3720,7 +3720,7 @@ test "inline while loop" { |
| 3720 | 3720 | }; |
| 3721 | 3721 | sum += typeNameLength(T); |
| 3722 | 3722 | } |
| 3723 | | assert(sum == 9); |
| 3723 | expect(sum == 9); |
| 3724 | 3724 | } |
| 3725 | 3725 | |
| 3726 | 3726 | fn typeNameLength(comptime T: type) usize { |
| ... | ... | @@ -3741,7 +3741,7 @@ fn typeNameLength(comptime T: type) usize { |
| 3741 | 3741 | {#header_close#} |
| 3742 | 3742 | {#header_open|for#} |
| 3743 | 3743 | {#code_begin|test|for#} |
| 3744 | | const assert = @import("std").debug.assert; |
| 3744 | const expect = @import("std").testing.expect; |
| 3745 | 3745 | |
| 3746 | 3746 | test "for basics" { |
| 3747 | 3747 | const items = [_]i32 { 4, 5, 3, 4, 0 }; |
| ... | ... | @@ -3755,22 +3755,22 @@ test "for basics" { |
| 3755 | 3755 | } |
| 3756 | 3756 | sum += value; |
| 3757 | 3757 | } |
| 3758 | | assert(sum == 16); |
| 3758 | expect(sum == 16); |
| 3759 | 3759 | |
| 3760 | 3760 | // To iterate over a portion of a slice, reslice. |
| 3761 | 3761 | for (items[0..1]) |value| { |
| 3762 | 3762 | sum += value; |
| 3763 | 3763 | } |
| 3764 | | assert(sum == 20); |
| 3764 | expect(sum == 20); |
| 3765 | 3765 | |
| 3766 | 3766 | // To access the index of iteration, specify a second capture value. |
| 3767 | 3767 | // This is zero-indexed. |
| 3768 | 3768 | var sum2: i32 = 0; |
| 3769 | 3769 | for (items) |value, i| { |
| 3770 | | assert(@TypeOf(i) == usize); |
| 3770 | expect(@TypeOf(i) == usize); |
| 3771 | 3771 | sum2 += @intCast(i32, i); |
| 3772 | 3772 | } |
| 3773 | | assert(sum2 == 10); |
| 3773 | expect(sum2 == 10); |
| 3774 | 3774 | } |
| 3775 | 3775 | |
| 3776 | 3776 | test "for reference" { |
| ... | ... | @@ -3782,9 +3782,9 @@ test "for reference" { |
| 3782 | 3782 | value.* += 1; |
| 3783 | 3783 | } |
| 3784 | 3784 | |
| 3785 | | assert(items[0] == 4); |
| 3786 | | assert(items[1] == 5); |
| 3787 | | assert(items[2] == 3); |
| 3785 | expect(items[0] == 4); |
| 3786 | expect(items[1] == 5); |
| 3787 | expect(items[2] == 3); |
| 3788 | 3788 | } |
| 3789 | 3789 | |
| 3790 | 3790 | test "for else" { |
| ... | ... | @@ -3799,10 +3799,10 @@ test "for else" { |
| 3799 | 3799 | sum += value.?; |
| 3800 | 3800 | } |
| 3801 | 3801 | } else blk: { |
| 3802 | | assert(sum == 12); |
| 3802 | expect(sum == 12); |
| 3803 | 3803 | break :blk sum; |
| 3804 | 3804 | }; |
| 3805 | | assert(result == 12); |
| 3805 | expect(result == 12); |
| 3806 | 3806 | } |
| 3807 | 3807 | {#code_end#} |
| 3808 | 3808 | {#header_open|Labeled for#} |
| ... | ... | @@ -3810,7 +3810,7 @@ test "for else" { |
| 3810 | 3810 | or {#syntax#}continue{#endsyntax#} from within a nested loop:</p> |
| 3811 | 3811 | {#code_begin|test#} |
| 3812 | 3812 | const std = @import("std"); |
| 3813 | | const assert = std.debug.assert; |
| 3813 | const expect = std.testing.expect; |
| 3814 | 3814 | |
| 3815 | 3815 | test "nested break" { |
| 3816 | 3816 | var count: usize = 0; |
| ... | ... | @@ -3820,7 +3820,7 @@ test "nested break" { |
| 3820 | 3820 | break :outer; |
| 3821 | 3821 | } |
| 3822 | 3822 | } |
| 3823 | | assert(count == 1); |
| 3823 | expect(count == 1); |
| 3824 | 3824 | } |
| 3825 | 3825 | |
| 3826 | 3826 | 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 | 3837 | {#code_end#} |
| 3838 | 3838 | {#header_close#} |
| ... | ... | @@ -3845,7 +3845,7 @@ test "nested continue" { |
| 3845 | 3845 | compile-time known. |
| 3846 | 3846 | </p> |
| 3847 | 3847 | {#code_begin|test#} |
| 3848 | | const assert = @import("std").debug.assert; |
| 3848 | const expect = @import("std").testing.expect; |
| 3849 | 3849 | |
| 3850 | 3850 | test "inline for loop" { |
| 3851 | 3851 | const nums = [_]i32{2, 4, 6}; |
| ... | ... | @@ -3859,7 +3859,7 @@ test "inline for loop" { |
| 3859 | 3859 | }; |
| 3860 | 3860 | sum += typeNameLength(T); |
| 3861 | 3861 | } |
| 3862 | | assert(sum == 9); |
| 3862 | expect(sum == 9); |
| 3863 | 3863 | } |
| 3864 | 3864 | |
| 3865 | 3865 | fn typeNameLength(comptime T: type) usize { |
| ... | ... | @@ -3885,14 +3885,14 @@ fn typeNameLength(comptime T: type) usize { |
| 3885 | 3885 | // * ?T |
| 3886 | 3886 | // * anyerror!T |
| 3887 | 3887 | |
| 3888 | | const assert = @import("std").debug.assert; |
| 3888 | const expect = @import("std").testing.expect; |
| 3889 | 3889 | |
| 3890 | 3890 | test "if expression" { |
| 3891 | 3891 | // If expressions are used instead of a ternary expression. |
| 3892 | 3892 | const a: u32 = 5; |
| 3893 | 3893 | const b: u32 = 4; |
| 3894 | 3894 | const result = if (a != b) 47 else 3089; |
| 3895 | | assert(result == 47); |
| 3895 | expect(result == 47); |
| 3896 | 3896 | } |
| 3897 | 3897 | |
| 3898 | 3898 | test "if boolean" { |
| ... | ... | @@ -3900,7 +3900,7 @@ test "if boolean" { |
| 3900 | 3900 | const a: u32 = 5; |
| 3901 | 3901 | const b: u32 = 4; |
| 3902 | 3902 | if (a != b) { |
| 3903 | | assert(true); |
| 3903 | expect(true); |
| 3904 | 3904 | } else if (a == 9) { |
| 3905 | 3905 | unreachable; |
| 3906 | 3906 | } else { |
| ... | ... | @@ -3913,7 +3913,7 @@ test "if optional" { |
| 3913 | 3913 | |
| 3914 | 3914 | const a: ?u32 = 0; |
| 3915 | 3915 | if (a) |value| { |
| 3916 | | assert(value == 0); |
| 3916 | expect(value == 0); |
| 3917 | 3917 | } else { |
| 3918 | 3918 | unreachable; |
| 3919 | 3919 | } |
| ... | ... | @@ -3922,17 +3922,17 @@ test "if optional" { |
| 3922 | 3922 | if (b) |value| { |
| 3923 | 3923 | unreachable; |
| 3924 | 3924 | } else { |
| 3925 | | assert(true); |
| 3925 | expect(true); |
| 3926 | 3926 | } |
| 3927 | 3927 | |
| 3928 | 3928 | // The else is not required. |
| 3929 | 3929 | if (a) |value| { |
| 3930 | | assert(value == 0); |
| 3930 | expect(value == 0); |
| 3931 | 3931 | } |
| 3932 | 3932 | |
| 3933 | 3933 | // To test against null only, use the binary equality operator. |
| 3934 | 3934 | if (b == null) { |
| 3935 | | assert(true); |
| 3935 | expect(true); |
| 3936 | 3936 | } |
| 3937 | 3937 | |
| 3938 | 3938 | // Access the value by reference using a pointer capture. |
| ... | ... | @@ -3942,7 +3942,7 @@ test "if optional" { |
| 3942 | 3942 | } |
| 3943 | 3943 | |
| 3944 | 3944 | if (c) |value| { |
| 3945 | | assert(value == 2); |
| 3945 | expect(value == 2); |
| 3946 | 3946 | } else { |
| 3947 | 3947 | unreachable; |
| 3948 | 3948 | } |
| ... | ... | @@ -3954,7 +3954,7 @@ test "if error union" { |
| 3954 | 3954 | |
| 3955 | 3955 | const a: anyerror!u32 = 0; |
| 3956 | 3956 | if (a) |value| { |
| 3957 | | assert(value == 0); |
| 3957 | expect(value == 0); |
| 3958 | 3958 | } else |err| { |
| 3959 | 3959 | unreachable; |
| 3960 | 3960 | } |
| ... | ... | @@ -3963,17 +3963,17 @@ test "if error union" { |
| 3963 | 3963 | if (b) |value| { |
| 3964 | 3964 | unreachable; |
| 3965 | 3965 | } else |err| { |
| 3966 | | assert(err == error.BadValue); |
| 3966 | expect(err == error.BadValue); |
| 3967 | 3967 | } |
| 3968 | 3968 | |
| 3969 | 3969 | // The else and |err| capture is strictly required. |
| 3970 | 3970 | if (a) |value| { |
| 3971 | | assert(value == 0); |
| 3971 | expect(value == 0); |
| 3972 | 3972 | } else |_| {} |
| 3973 | 3973 | |
| 3974 | 3974 | // To check only the error value, use an empty block expression. |
| 3975 | 3975 | if (b) |_| {} else |err| { |
| 3976 | | assert(err == error.BadValue); |
| 3976 | expect(err == error.BadValue); |
| 3977 | 3977 | } |
| 3978 | 3978 | |
| 3979 | 3979 | // Access the value by reference using a pointer capture. |
| ... | ... | @@ -3985,7 +3985,7 @@ test "if error union" { |
| 3985 | 3985 | } |
| 3986 | 3986 | |
| 3987 | 3987 | if (c) |value| { |
| 3988 | | assert(value == 9); |
| 3988 | expect(value == 9); |
| 3989 | 3989 | } else |err| { |
| 3990 | 3990 | unreachable; |
| 3991 | 3991 | } |
| ... | ... | @@ -3997,14 +3997,14 @@ test "if error union with optional" { |
| 3997 | 3997 | |
| 3998 | 3998 | const a: anyerror!?u32 = 0; |
| 3999 | 3999 | if (a) |optional_value| { |
| 4000 | | assert(optional_value.? == 0); |
| 4000 | expect(optional_value.? == 0); |
| 4001 | 4001 | } else |err| { |
| 4002 | 4002 | unreachable; |
| 4003 | 4003 | } |
| 4004 | 4004 | |
| 4005 | 4005 | const b: anyerror!?u32 = null; |
| 4006 | 4006 | if (b) |optional_value| { |
| 4007 | | assert(optional_value == null); |
| 4007 | expect(optional_value == null); |
| 4008 | 4008 | } else |err| { |
| 4009 | 4009 | unreachable; |
| 4010 | 4010 | } |
| ... | ... | @@ -4013,7 +4013,7 @@ test "if error union with optional" { |
| 4013 | 4013 | if (c) |optional_value| { |
| 4014 | 4014 | unreachable; |
| 4015 | 4015 | } else |err| { |
| 4016 | | assert(err == error.BadValue); |
| 4016 | expect(err == error.BadValue); |
| 4017 | 4017 | } |
| 4018 | 4018 | |
| 4019 | 4019 | // Access the value by reference by using a pointer capture each time. |
| ... | ... | @@ -4027,7 +4027,7 @@ test "if error union with optional" { |
| 4027 | 4027 | } |
| 4028 | 4028 | |
| 4029 | 4029 | if (d) |optional_value| { |
| 4030 | | assert(optional_value.? == 9); |
| 4030 | expect(optional_value.? == 9); |
| 4031 | 4031 | } else |err| { |
| 4032 | 4032 | unreachable; |
| 4033 | 4033 | } |
| ... | ... | @@ -4038,7 +4038,7 @@ test "if error union with optional" { |
| 4038 | 4038 | {#header_open|defer#} |
| 4039 | 4039 | {#code_begin|test|defer#} |
| 4040 | 4040 | const std = @import("std"); |
| 4041 | | const assert = std.debug.assert; |
| 4041 | const expect = std.testing.expect; |
| 4042 | 4042 | const print = std.debug.print; |
| 4043 | 4043 | |
| 4044 | 4044 | // defer will execute an expression at the end of the current scope. |
| ... | ... | @@ -4049,14 +4049,14 @@ fn deferExample() usize { |
| 4049 | 4049 | defer a = 2; |
| 4050 | 4050 | a = 1; |
| 4051 | 4051 | } |
| 4052 | | assert(a == 2); |
| 4052 | expect(a == 2); |
| 4053 | 4053 | |
| 4054 | 4054 | a = 5; |
| 4055 | 4055 | return a; |
| 4056 | 4056 | } |
| 4057 | 4057 | |
| 4058 | 4058 | test "defer basics" { |
| 4059 | | assert(deferExample() == 5); |
| 4059 | expect(deferExample() == 5); |
| 4060 | 4060 | } |
| 4061 | 4061 | |
| 4062 | 4062 | // If multiple defer statements are specified, they will be executed in |
| ... | ... | @@ -4133,8 +4133,9 @@ test "basic math" { |
| 4133 | 4133 | } |
| 4134 | 4134 | } |
| 4135 | 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 | 4137 | {#code_begin|test_err#} |
| 4138 | // This is how std.debug.assert is implemented |
| 4138 | 4139 | fn assert(ok: bool) void { |
| 4139 | 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 | 4194 | |
| 4194 | 4195 | test "foo" { |
| 4195 | 4196 | const value = bar() catch ExitProcess(1); |
| 4196 | | assert(value == 1234); |
| 4197 | expect(value == 1234); |
| 4197 | 4198 | } |
| 4198 | 4199 | |
| 4199 | 4200 | fn bar() anyerror!u32 { |
| 4200 | 4201 | return 1234; |
| 4201 | 4202 | } |
| 4202 | 4203 | |
| 4203 | | const assert = @import("std").debug.assert; |
| 4204 | const expect = @import("std").testing.expect; |
| 4204 | 4205 | {#code_end#} |
| 4205 | 4206 | {#header_close#} |
| 4206 | 4207 | {#header_open|Functions#} |
| 4207 | 4208 | {#code_begin|test|functions#} |
| 4208 | | const assert = @import("std").debug.assert; |
| 4209 | const expect = @import("std").testing.expect; |
| 4209 | 4210 | |
| 4210 | 4211 | // Functions are declared like this |
| 4211 | 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 | 4257 | } |
| 4257 | 4258 | |
| 4258 | 4259 | test "function" { |
| 4259 | | assert(do_op(add, 5, 6) == 11); |
| 4260 | | assert(do_op(sub2, 5, 6) == -1); |
| 4260 | expect(do_op(add, 5, 6) == 11); |
| 4261 | expect(do_op(sub2, 5, 6) == -1); |
| 4261 | 4262 | } |
| 4262 | 4263 | {#code_end#} |
| 4263 | 4264 | <p>Function values are like pointers:</p> |
| 4264 | 4265 | {#code_begin|obj#} |
| 4265 | | const assert = @import("std").debug.assert; |
| 4266 | const expect = @import("std").testing.expect; |
| 4266 | 4267 | |
| 4267 | 4268 | comptime { |
| 4268 | | assert(@TypeOf(foo) == fn()void); |
| 4269 | | assert(@sizeOf(fn()void) == @sizeOf(?fn()void)); |
| 4269 | expect(@TypeOf(foo) == fn()void); |
| 4270 | expect(@sizeOf(fn()void) == @sizeOf(?fn()void)); |
| 4270 | 4271 | } |
| 4271 | 4272 | |
| 4272 | 4273 | fn foo() void { } |
| ... | ... | @@ -4298,10 +4299,10 @@ fn foo(point: Point) i32 { |
| 4298 | 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 | 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 | 4307 | {#code_end#} |
| 4307 | 4308 | <p> |
| ... | ... | @@ -4315,29 +4316,29 @@ test "pass struct to function" { |
| 4315 | 4316 | Use {#link|@TypeOf#} and {#link|@typeInfo#} to get information about the inferred type. |
| 4316 | 4317 | </p> |
| 4317 | 4318 | {#code_begin|test#} |
| 4318 | | const assert = @import("std").debug.assert; |
| 4319 | const expect = @import("std").testing.expect; |
| 4319 | 4320 | |
| 4320 | 4321 | fn addFortyTwo(x: anytype) @TypeOf(x) { |
| 4321 | 4322 | return x + 42; |
| 4322 | 4323 | } |
| 4323 | 4324 | |
| 4324 | 4325 | test "fn type inference" { |
| 4325 | | assert(addFortyTwo(1) == 43); |
| 4326 | | assert(@TypeOf(addFortyTwo(1)) == comptime_int); |
| 4326 | expect(addFortyTwo(1) == 43); |
| 4327 | expect(@TypeOf(addFortyTwo(1)) == comptime_int); |
| 4327 | 4328 | var y: i64 = 2; |
| 4328 | | assert(addFortyTwo(y) == 44); |
| 4329 | | assert(@TypeOf(addFortyTwo(y)) == i64); |
| 4329 | expect(addFortyTwo(y) == 44); |
| 4330 | expect(@TypeOf(addFortyTwo(y)) == i64); |
| 4330 | 4331 | } |
| 4331 | 4332 | {#code_end#} |
| 4332 | 4333 | |
| 4333 | 4334 | {#header_close#} |
| 4334 | 4335 | {#header_open|Function Reflection#} |
| 4335 | 4336 | {#code_begin|test#} |
| 4336 | | const assert = @import("std").debug.assert; |
| 4337 | const expect = @import("std").testing.expect; |
| 4337 | 4338 | |
| 4338 | 4339 | test "fn reflection" { |
| 4339 | | assert(@typeInfo(@TypeOf(assert)).Fn.return_type.? == void); |
| 4340 | | assert(@typeInfo(@TypeOf(assert)).Fn.is_var_args == false); |
| 4340 | expect(@typeInfo(@TypeOf(expect)).Fn.return_type.? == void); |
| 4341 | expect(@typeInfo(@TypeOf(expect)).Fn.is_var_args == false); |
| 4341 | 4342 | } |
| 4342 | 4343 | {#code_end#} |
| 4343 | 4344 | {#header_close#} |
| ... | ... | @@ -4372,7 +4373,7 @@ const AllocationError = error { |
| 4372 | 4373 | |
| 4373 | 4374 | test "coerce subset to superset" { |
| 4374 | 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 | 4379 | fn foo(err: AllocationError) FileOpenError { |
| ... | ... | @@ -4480,7 +4481,7 @@ fn charToDigit(c: u8) u8 { |
| 4480 | 4481 | |
| 4481 | 4482 | test "parse u64" { |
| 4482 | 4483 | const result = try parseU64("1234", 10); |
| 4483 | | std.debug.assert(result == 1234); |
| 4484 | std.testing.expect(result == 1234); |
| 4484 | 4485 | } |
| 4485 | 4486 | {#code_end#} |
| 4486 | 4487 | <p> |
| ... | ... | @@ -4625,7 +4626,7 @@ fn createFoo(param: i32) !Foo { |
| 4625 | 4626 | <p>An error union is created with the {#syntax#}!{#endsyntax#} binary operator. |
| 4626 | 4627 | You can use compile-time reflection to access the child type of an error union:</p> |
| 4627 | 4628 | {#code_begin|test#} |
| 4628 | | const assert = @import("std").debug.assert; |
| 4629 | const expect = @import("std").testing.expect; |
| 4629 | 4630 | |
| 4630 | 4631 | test "error union" { |
| 4631 | 4632 | var foo: anyerror!i32 = undefined; |
| ... | ... | @@ -4637,10 +4638,10 @@ test "error union" { |
| 4637 | 4638 | foo = error.SomeError; |
| 4638 | 4639 | |
| 4639 | 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 | 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 | 4646 | {#code_end#} |
| 4646 | 4647 | {#header_open|Merging Error Sets#} |
| ... | ... | @@ -5007,7 +5008,7 @@ fn doAThing(optional_foo: ?*Foo) void { |
| 5007 | 5008 | <p>An optional is created by putting {#syntax#}?{#endsyntax#} in front of a type. You can use compile-time |
| 5008 | 5009 | reflection to access the child type of an optional:</p> |
| 5009 | 5010 | {#code_begin|test#} |
| 5010 | | const assert = @import("std").debug.assert; |
| 5011 | const expect = @import("std").testing.expect; |
| 5011 | 5012 | |
| 5012 | 5013 | test "optional type" { |
| 5013 | 5014 | // Declare an optional and coerce from null: |
| ... | ... | @@ -5017,7 +5018,7 @@ test "optional type" { |
| 5017 | 5018 | foo = 1234; |
| 5018 | 5019 | |
| 5019 | 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 | 5023 | {#code_end#} |
| 5023 | 5024 | {#header_close#} |
| ... | ... | @@ -5034,7 +5035,7 @@ const optional_value: ?i32 = null; |
| 5034 | 5035 | <p>An optional pointer is guaranteed to be the same size as a pointer. The {#syntax#}null{#endsyntax#} of |
| 5035 | 5036 | the optional is guaranteed to be address 0.</p> |
| 5036 | 5037 | {#code_begin|test#} |
| 5037 | | const assert = @import("std").debug.assert; |
| 5038 | const expect = @import("std").testing.expect; |
| 5038 | 5039 | |
| 5039 | 5040 | test "optional pointers" { |
| 5040 | 5041 | // Pointers cannot be null. If you want a null pointer, use the optional |
| ... | ... | @@ -5044,11 +5045,11 @@ test "optional pointers" { |
| 5044 | 5045 | var x: i32 = 1; |
| 5045 | 5046 | ptr = &x; |
| 5046 | 5047 | |
| 5047 | | assert(ptr.?.* == 1); |
| 5048 | expect(ptr.?.* == 1); |
| 5048 | 5049 | |
| 5049 | 5050 | // Optional pointers are the same size as normal pointers, because pointer |
| 5050 | 5051 | // value 0 is used as the null value. |
| 5051 | | assert(@sizeOf(?*i32) == @sizeOf(*i32)); |
| 5052 | expect(@sizeOf(?*i32) == @sizeOf(*i32)); |
| 5052 | 5053 | } |
| 5053 | 5054 | {#code_end#} |
| 5054 | 5055 | {#header_close#} |
| ... | ... | @@ -5115,13 +5116,13 @@ fn foo(a: *const i32) void {} |
| 5115 | 5116 | </p> |
| 5116 | 5117 | {#code_begin|test#} |
| 5117 | 5118 | const std = @import("std"); |
| 5118 | | const assert = std.debug.assert; |
| 5119 | const expect = std.testing.expect; |
| 5119 | 5120 | const mem = std.mem; |
| 5120 | 5121 | |
| 5121 | 5122 | test "cast *[1][*]const u8 to [*]const ?[*]const u8" { |
| 5122 | 5123 | const window_name = [1][*]const u8{"window name"}; |
| 5123 | 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 | 5127 | {#code_end#} |
| 5127 | 5128 | {#header_close#} |
| ... | ... | @@ -5132,7 +5133,7 @@ test "cast *[1][*]const u8 to [*]const ?[*]const u8" { |
| 5132 | 5133 | </p> |
| 5133 | 5134 | {#code_begin|test#} |
| 5134 | 5135 | const std = @import("std"); |
| 5135 | | const assert = std.debug.assert; |
| 5136 | const expect = std.testing.expect; |
| 5136 | 5137 | const mem = std.mem; |
| 5137 | 5138 | |
| 5138 | 5139 | test "integer widening" { |
| ... | ... | @@ -5142,13 +5143,13 @@ test "integer widening" { |
| 5142 | 5143 | var d: u64 = c; |
| 5143 | 5144 | var e: u64 = d; |
| 5144 | 5145 | var f: u128 = e; |
| 5145 | | assert(f == a); |
| 5146 | expect(f == a); |
| 5146 | 5147 | } |
| 5147 | 5148 | |
| 5148 | 5149 | test "implicit unsigned integer to signed integer" { |
| 5149 | 5150 | var a: u8 = 250; |
| 5150 | 5151 | var b: i16 = a; |
| 5151 | | assert(b == 250); |
| 5152 | expect(b == 250); |
| 5152 | 5153 | } |
| 5153 | 5154 | |
| 5154 | 5155 | test "float widening" { |
| ... | ... | @@ -5160,7 +5161,7 @@ test "float widening" { |
| 5160 | 5161 | var b: f32 = a; |
| 5161 | 5162 | var c: f64 = b; |
| 5162 | 5163 | var d: f128 = c; |
| 5163 | | assert(d == a); |
| 5164 | expect(d == a); |
| 5164 | 5165 | } |
| 5165 | 5166 | {#code_end#} |
| 5166 | 5167 | {#header_close#} |
| ... | ... | @@ -5183,7 +5184,7 @@ test "implicit cast to comptime_int" { |
| 5183 | 5184 | {#header_open|Type Coercion: Arrays and Pointers#} |
| 5184 | 5185 | {#code_begin|test|coerce_arrays_and_ptrs#} |
| 5185 | 5186 | const std = @import("std"); |
| 5186 | | const assert = std.debug.assert; |
| 5187 | const expect = std.testing.expect; |
| 5187 | 5188 | |
| 5188 | 5189 | // This cast exists primarily so that string literals can be |
| 5189 | 5190 | // passed to functions that accept const slices. However |
| ... | ... | @@ -5192,41 +5193,41 @@ const assert = std.debug.assert; |
| 5192 | 5193 | test "[N]T to []const T" { |
| 5193 | 5194 | var x1: []const u8 = "hello"; |
| 5194 | 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 | 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 | 5202 | // Likewise, it works when the destination type is an error union. |
| 5202 | 5203 | test "[N]T to E![]const T" { |
| 5203 | 5204 | var x1: anyerror![]const u8 = "hello"; |
| 5204 | 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 | 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 | 5212 | // Likewise, it works when the destination type is an optional. |
| 5212 | 5213 | test "[N]T to ?[]const T" { |
| 5213 | 5214 | var x1: ?[]const u8 = "hello"; |
| 5214 | 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 | 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 | 5222 | // In this cast, the array length becomes the slice length. |
| 5222 | 5223 | test "*[N]T to []T" { |
| 5223 | 5224 | var buf: [5]u8 = "hello".*; |
| 5224 | 5225 | const x: []u8 = &buf; |
| 5225 | | assert(std.mem.eql(u8, x, "hello")); |
| 5226 | expect(std.mem.eql(u8, x, "hello")); |
| 5226 | 5227 | |
| 5227 | 5228 | const buf2 = [2]f32{ 1.2, 3.4 }; |
| 5228 | 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 | 5233 | // Single-item pointers to arrays can be coerced to |
| ... | ... | @@ -5234,7 +5235,7 @@ test "*[N]T to []T" { |
| 5234 | 5235 | test "*[N]T to [*]T" { |
| 5235 | 5236 | var buf: [5]u8 = "hello".*; |
| 5236 | 5237 | const x: [*]u8 = &buf; |
| 5237 | | assert(x[4] == 'o'); |
| 5238 | expect(x[4] == 'o'); |
| 5238 | 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 | 5243 | test "*[N]T to ?[*]T" { |
| 5243 | 5244 | var buf: [5]u8 = "hello".*; |
| 5244 | 5245 | const x: ?[*]u8 = &buf; |
| 5245 | | assert(x.?[4] == 'o'); |
| 5246 | expect(x.?[4] == 'o'); |
| 5246 | 5247 | } |
| 5247 | 5248 | |
| 5248 | 5249 | // Single-item pointers can be cast to len-1 single-item arrays. |
| ... | ... | @@ -5250,7 +5251,7 @@ test "*T to *[1]T" { |
| 5250 | 5251 | var x: i32 = 1234; |
| 5251 | 5252 | const y: *[1]i32 = &x; |
| 5252 | 5253 | const z: [*]i32 = y; |
| 5253 | | assert(z[0] == 1234); |
| 5254 | expect(z[0] == 1234); |
| 5254 | 5255 | } |
| 5255 | 5256 | {#code_end#} |
| 5256 | 5257 | {#see_also|C Pointers#} |
| ... | ... | @@ -5261,27 +5262,27 @@ test "*T to *[1]T" { |
| 5261 | 5262 | </p> |
| 5262 | 5263 | {#code_begin|test#} |
| 5263 | 5264 | const std = @import("std"); |
| 5264 | | const assert = std.debug.assert; |
| 5265 | const expect = std.testing.expect; |
| 5265 | 5266 | |
| 5266 | 5267 | test "coerce to optionals" { |
| 5267 | 5268 | const x: ?i32 = 1234; |
| 5268 | 5269 | const y: ?i32 = null; |
| 5269 | 5270 | |
| 5270 | | assert(x.? == 1234); |
| 5271 | | assert(y == null); |
| 5271 | expect(x.? == 1234); |
| 5272 | expect(y == null); |
| 5272 | 5273 | } |
| 5273 | 5274 | {#code_end#} |
| 5274 | 5275 | <p>It works nested inside the {#link|Error Union Type#}, too:</p> |
| 5275 | 5276 | {#code_begin|test#} |
| 5276 | 5277 | const std = @import("std"); |
| 5277 | | const assert = std.debug.assert; |
| 5278 | const expect = std.testing.expect; |
| 5278 | 5279 | |
| 5279 | 5280 | test "coerce to optionals wrapped in error union" { |
| 5280 | 5281 | const x: anyerror!?i32 = 1234; |
| 5281 | 5282 | const y: anyerror!?i32 = null; |
| 5282 | 5283 | |
| 5283 | | assert((try x).? == 1234); |
| 5284 | | assert((try y) == null); |
| 5284 | expect((try x).? == 1234); |
| 5285 | expect((try y) == null); |
| 5285 | 5286 | } |
| 5286 | 5287 | {#code_end#} |
| 5287 | 5288 | {#header_close#} |
| ... | ... | @@ -5291,13 +5292,13 @@ test "coerce to optionals wrapped in error union" { |
| 5291 | 5292 | </p> |
| 5292 | 5293 | {#code_begin|test#} |
| 5293 | 5294 | const std = @import("std"); |
| 5294 | | const assert = std.debug.assert; |
| 5295 | const expect = std.testing.expect; |
| 5295 | 5296 | |
| 5296 | 5297 | test "coercion to error unions" { |
| 5297 | 5298 | const x: anyerror!i32 = 1234; |
| 5298 | 5299 | const y: anyerror!i32 = error.Failure; |
| 5299 | 5300 | |
| 5300 | | assert((try x) == 1234); |
| 5301 | expect((try x) == 1234); |
| 5301 | 5302 | std.testing.expectError(error.Failure, y); |
| 5302 | 5303 | } |
| 5303 | 5304 | {#code_end#} |
| ... | ... | @@ -5308,12 +5309,12 @@ test "coercion to error unions" { |
| 5308 | 5309 | </p> |
| 5309 | 5310 | {#code_begin|test#} |
| 5310 | 5311 | const std = @import("std"); |
| 5311 | | const assert = std.debug.assert; |
| 5312 | const expect = std.testing.expect; |
| 5312 | 5313 | |
| 5313 | 5314 | test "coercing large integer type to smaller one when value is comptime known to fit" { |
| 5314 | 5315 | const x: u64 = 255; |
| 5315 | 5316 | const y: u8 = x; |
| 5316 | | assert(y == 255); |
| 5317 | expect(y == 255); |
| 5317 | 5318 | } |
| 5318 | 5319 | {#code_end#} |
| 5319 | 5320 | {#header_close#} |
| ... | ... | @@ -5324,7 +5325,7 @@ test "coercing large integer type to smaller one when value is comptime known to |
| 5324 | 5325 | </p> |
| 5325 | 5326 | {#code_begin|test#} |
| 5326 | 5327 | const std = @import("std"); |
| 5327 | | const assert = std.debug.assert; |
| 5328 | const expect = std.testing.expect; |
| 5328 | 5329 | |
| 5329 | 5330 | const E = enum { |
| 5330 | 5331 | one, |
| ... | ... | @@ -5341,11 +5342,11 @@ const U = union(E) { |
| 5341 | 5342 | test "coercion between unions and enums" { |
| 5342 | 5343 | var u = U{ .two = 12.34 }; |
| 5343 | 5344 | var e: E = u; |
| 5344 | | assert(e == E.two); |
| 5345 | expect(e == E.two); |
| 5345 | 5346 | |
| 5346 | 5347 | const three = E.three; |
| 5347 | 5348 | var another_u: U = three; |
| 5348 | | assert(another_u == E.three); |
| 5349 | expect(another_u == E.three); |
| 5349 | 5350 | } |
| 5350 | 5351 | {#code_end#} |
| 5351 | 5352 | {#see_also|union|enum#} |
| ... | ... | @@ -5411,22 +5412,22 @@ test "coercion of zero bit types" { |
| 5411 | 5412 | </p> |
| 5412 | 5413 | {#code_begin|test|peer_type_resolution#} |
| 5413 | 5414 | const std = @import("std"); |
| 5414 | | const assert = std.debug.assert; |
| 5415 | const expect = std.testing.expect; |
| 5415 | 5416 | const mem = std.mem; |
| 5416 | 5417 | |
| 5417 | 5418 | test "peer resolve int widening" { |
| 5418 | 5419 | var a: i8 = 12; |
| 5419 | 5420 | var b: i16 = 34; |
| 5420 | 5421 | var c = a + b; |
| 5421 | | assert(c == 46); |
| 5422 | | assert(@TypeOf(c) == i16); |
| 5422 | expect(c == 46); |
| 5423 | expect(@TypeOf(c) == i16); |
| 5423 | 5424 | } |
| 5424 | 5425 | |
| 5425 | 5426 | test "peer resolve arrays of different size to const slice" { |
| 5426 | | assert(mem.eql(u8, boolToStr(true), "true")); |
| 5427 | | assert(mem.eql(u8, boolToStr(false), "false")); |
| 5428 | | comptime assert(mem.eql(u8, boolToStr(true), "true")); |
| 5429 | | comptime assert(mem.eql(u8, boolToStr(false), "false")); |
| 5427 | expect(mem.eql(u8, boolToStr(true), "true")); |
| 5428 | expect(mem.eql(u8, boolToStr(false), "false")); |
| 5429 | comptime expect(mem.eql(u8, boolToStr(true), "true")); |
| 5430 | comptime expect(mem.eql(u8, boolToStr(false), "false")); |
| 5430 | 5431 | } |
| 5431 | 5432 | fn boolToStr(b: bool) []const u8 { |
| 5432 | 5433 | return if (b) "true" else "false"; |
| ... | ... | @@ -5439,16 +5440,16 @@ test "peer resolve array and const slice" { |
| 5439 | 5440 | fn testPeerResolveArrayConstSlice(b: bool) void { |
| 5440 | 5441 | const value1 = if (b) "aoeu" else @as([]const u8, "zz"); |
| 5441 | 5442 | const value2 = if (b) @as([]const u8, "zz") else "aoeu"; |
| 5442 | | assert(mem.eql(u8, value1, "aoeu")); |
| 5443 | | assert(mem.eql(u8, value2, "zz")); |
| 5443 | expect(mem.eql(u8, value1, "aoeu")); |
| 5444 | expect(mem.eql(u8, value2, "zz")); |
| 5444 | 5445 | } |
| 5445 | 5446 | |
| 5446 | 5447 | test "peer type resolution: ?T and T" { |
| 5447 | | assert(peerTypeTAndOptionalT(true, false).? == 0); |
| 5448 | | assert(peerTypeTAndOptionalT(false, false).? == 3); |
| 5448 | expect(peerTypeTAndOptionalT(true, false).? == 0); |
| 5449 | expect(peerTypeTAndOptionalT(false, false).? == 3); |
| 5449 | 5450 | comptime { |
| 5450 | | assert(peerTypeTAndOptionalT(true, false).? == 0); |
| 5451 | | assert(peerTypeTAndOptionalT(false, false).? == 3); |
| 5451 | expect(peerTypeTAndOptionalT(true, false).? == 0); |
| 5452 | expect(peerTypeTAndOptionalT(false, false).? == 3); |
| 5452 | 5453 | } |
| 5453 | 5454 | } |
| 5454 | 5455 | fn peerTypeTAndOptionalT(c: bool, b: bool) ?usize { |
| ... | ... | @@ -5460,11 +5461,11 @@ fn peerTypeTAndOptionalT(c: bool, b: bool) ?usize { |
| 5460 | 5461 | } |
| 5461 | 5462 | |
| 5462 | 5463 | test "peer type resolution: *[0]u8 and []const u8" { |
| 5463 | | assert(peerTypeEmptyArrayAndSlice(true, "hi").len == 0); |
| 5464 | | assert(peerTypeEmptyArrayAndSlice(false, "hi").len == 1); |
| 5464 | expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0); |
| 5465 | expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1); |
| 5465 | 5466 | comptime { |
| 5466 | | assert(peerTypeEmptyArrayAndSlice(true, "hi").len == 0); |
| 5467 | | assert(peerTypeEmptyArrayAndSlice(false, "hi").len == 1); |
| 5467 | expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0); |
| 5468 | expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1); |
| 5468 | 5469 | } |
| 5469 | 5470 | } |
| 5470 | 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 | 5479 | { |
| 5479 | 5480 | var data = "hi".*; |
| 5480 | 5481 | const slice = data[0..]; |
| 5481 | | assert((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0); |
| 5482 | | assert((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1); |
| 5482 | expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0); |
| 5483 | expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1); |
| 5483 | 5484 | } |
| 5484 | 5485 | comptime { |
| 5485 | 5486 | var data = "hi".*; |
| 5486 | 5487 | const slice = data[0..]; |
| 5487 | | assert((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0); |
| 5488 | | assert((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1); |
| 5488 | expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0); |
| 5489 | expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1); |
| 5489 | 5490 | } |
| 5490 | 5491 | } |
| 5491 | 5492 | fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) anyerror![]u8 { |
| ... | ... | @@ -5499,8 +5500,8 @@ fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) anyerror![]u8 { |
| 5499 | 5500 | test "peer type resolution: *const T and ?*T" { |
| 5500 | 5501 | const a = @intToPtr(*const usize, 0x123456780); |
| 5501 | 5502 | const b = @intToPtr(?*usize, 0x123456780); |
| 5502 | | assert(a == b); |
| 5503 | | assert(b == a); |
| 5503 | expect(a == b); |
| 5504 | expect(b == a); |
| 5504 | 5505 | } |
| 5505 | 5506 | {#code_end#} |
| 5506 | 5507 | {#header_close#} |
| ... | ... | @@ -5547,7 +5548,7 @@ export fn entry() void { |
| 5547 | 5548 | </p> |
| 5548 | 5549 | {#code_begin|test#} |
| 5549 | 5550 | const std = @import("std"); |
| 5550 | | const assert = std.debug.assert; |
| 5551 | const expect = std.testing.expect; |
| 5551 | 5552 | |
| 5552 | 5553 | test "turn HashMap into a set with void" { |
| 5553 | 5554 | var map = std.AutoHashMap(i32, void).init(std.testing.allocator); |
| ... | ... | @@ -5556,11 +5557,11 @@ test "turn HashMap into a set with void" { |
| 5556 | 5557 | try map.put(1, {}); |
| 5557 | 5558 | try map.put(2, {}); |
| 5558 | 5559 | |
| 5559 | | assert(map.contains(2)); |
| 5560 | | assert(!map.contains(3)); |
| 5560 | expect(map.contains(2)); |
| 5561 | expect(!map.contains(3)); |
| 5561 | 5562 | |
| 5562 | 5563 | _ = map.remove(2); |
| 5563 | | assert(!map.contains(2)); |
| 5564 | expect(!map.contains(2)); |
| 5564 | 5565 | } |
| 5565 | 5566 | {#code_end#} |
| 5566 | 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 | 5608 | <p>Pointers to zero bit types also have zero bits. They always compare equal to each other:</p> |
| 5608 | 5609 | {#code_begin|test#} |
| 5609 | 5610 | const std = @import("std"); |
| 5610 | | const assert = std.debug.assert; |
| 5611 | const expect = std.testing.expect; |
| 5611 | 5612 | |
| 5612 | 5613 | test "pointer to empty struct" { |
| 5613 | 5614 | const Empty = struct {}; |
| ... | ... | @@ -5615,7 +5616,7 @@ test "pointer to empty struct" { |
| 5615 | 5616 | var b = Empty{}; |
| 5616 | 5617 | var ptr_a = &a; |
| 5617 | 5618 | var ptr_b = &b; |
| 5618 | | comptime assert(ptr_a == ptr_b); |
| 5619 | comptime expect(ptr_a == ptr_b); |
| 5619 | 5620 | } |
| 5620 | 5621 | {#code_end#} |
| 5621 | 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 | 5651 | usingnamespace @import("std"); |
| 5651 | 5652 | |
| 5652 | 5653 | test "using std namespace" { |
| 5653 | | debug.assert(true); |
| 5654 | testing.expect(true); |
| 5654 | 5655 | } |
| 5655 | 5656 | {#code_end#} |
| 5656 | 5657 | <p> |
| ... | ... | @@ -5762,7 +5763,7 @@ fn max(comptime T: type, a: T, b: T) T { |
| 5762 | 5763 | } |
| 5763 | 5764 | } |
| 5764 | 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 | 5768 | {#code_end#} |
| 5768 | 5769 | <p> |
| ... | ... | @@ -5802,7 +5803,7 @@ fn max(a: bool, b: bool) bool { |
| 5802 | 5803 | For example: |
| 5803 | 5804 | </p> |
| 5804 | 5805 | {#code_begin|test|comptime_vars#} |
| 5805 | | const assert = @import("std").debug.assert; |
| 5806 | const expect = @import("std").testing.expect; |
| 5806 | 5807 | |
| 5807 | 5808 | const CmdFn = struct { |
| 5808 | 5809 | name: []const u8, |
| ... | ... | @@ -5830,9 +5831,9 @@ fn performFn(comptime prefix_char: u8, start_value: i32) i32 { |
| 5830 | 5831 | } |
| 5831 | 5832 | |
| 5832 | 5833 | test "perform fn" { |
| 5833 | | assert(performFn('t', 1) == 6); |
| 5834 | | assert(performFn('o', 0) == 1); |
| 5835 | | assert(performFn('w', 99) == 99); |
| 5834 | expect(performFn('t', 1) == 6); |
| 5835 | expect(performFn('o', 0) == 1); |
| 5836 | expect(performFn('w', 99) == 99); |
| 5836 | 5837 | } |
| 5837 | 5838 | {#code_end#} |
| 5838 | 5839 | <p> |
| ... | ... | @@ -5843,7 +5844,7 @@ test "perform fn" { |
| 5843 | 5844 | </p> |
| 5844 | 5845 | {#code_begin|syntax#} |
| 5845 | 5846 | // From the line: |
| 5846 | | // assert(performFn('t', 1) == 6); |
| 5847 | // expect(performFn('t', 1) == 6); |
| 5847 | 5848 | fn performFn(start_value: i32) i32 { |
| 5848 | 5849 | var result: i32 = start_value; |
| 5849 | 5850 | result = two(result); |
| ... | ... | @@ -5853,7 +5854,7 @@ fn performFn(start_value: i32) i32 { |
| 5853 | 5854 | {#code_end#} |
| 5854 | 5855 | {#code_begin|syntax#} |
| 5855 | 5856 | // From the line: |
| 5856 | | // assert(performFn('o', 0) == 1); |
| 5857 | // expect(performFn('o', 0) == 1); |
| 5857 | 5858 | fn performFn(start_value: i32) i32 { |
| 5858 | 5859 | var result: i32 = start_value; |
| 5859 | 5860 | result = one(result); |
| ... | ... | @@ -5862,7 +5863,7 @@ fn performFn(start_value: i32) i32 { |
| 5862 | 5863 | {#code_end#} |
| 5863 | 5864 | {#code_begin|syntax#} |
| 5864 | 5865 | // From the line: |
| 5865 | | // assert(performFn('w', 99) == 99); |
| 5866 | // expect(performFn('w', 99) == 99); |
| 5866 | 5867 | fn performFn(start_value: i32) i32 { |
| 5867 | 5868 | var result: i32 = start_value; |
| 5868 | 5869 | return result; |
| ... | ... | @@ -5915,7 +5916,7 @@ test "foo" { |
| 5915 | 5916 | Let's look at an example: |
| 5916 | 5917 | </p> |
| 5917 | 5918 | {#code_begin|test#} |
| 5918 | | const assert = @import("std").debug.assert; |
| 5919 | const expect = @import("std").testing.expect; |
| 5919 | 5920 | |
| 5920 | 5921 | fn fibonacci(index: u32) u32 { |
| 5921 | 5922 | if (index < 2) return index; |
| ... | ... | @@ -5924,11 +5925,11 @@ fn fibonacci(index: u32) u32 { |
| 5924 | 5925 | |
| 5925 | 5926 | test "fibonacci" { |
| 5926 | 5927 | // test fibonacci at run-time |
| 5927 | | assert(fibonacci(7) == 13); |
| 5928 | expect(fibonacci(7) == 13); |
| 5928 | 5929 | |
| 5929 | 5930 | // test fibonacci at compile-time |
| 5930 | 5931 | comptime { |
| 5931 | | assert(fibonacci(7) == 13); |
| 5932 | expect(fibonacci(7) == 13); |
| 5932 | 5933 | } |
| 5933 | 5934 | } |
| 5934 | 5935 | {#code_end#} |
| ... | ... | @@ -5936,7 +5937,7 @@ test "fibonacci" { |
| 5936 | 5937 | Imagine if we had forgotten the base case of the recursive function and tried to run the tests: |
| 5937 | 5938 | </p> |
| 5938 | 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 | 5942 | fn fibonacci(index: u32) u32 { |
| 5942 | 5943 | //if (index < 2) return index; |
| ... | ... | @@ -5945,7 +5946,7 @@ fn fibonacci(index: u32) u32 { |
| 5945 | 5946 | |
| 5946 | 5947 | test "fibonacci" { |
| 5947 | 5948 | comptime { |
| 5948 | | assert(fibonacci(7) == 13); |
| 5949 | expect(fibonacci(7) == 13); |
| 5949 | 5950 | } |
| 5950 | 5951 | } |
| 5951 | 5952 | {#code_end#} |
| ... | ... | @@ -5959,7 +5960,7 @@ test "fibonacci" { |
| 5959 | 5960 | But what would have happened if we used a signed integer? |
| 5960 | 5961 | </p> |
| 5961 | 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 | 5965 | fn fibonacci(index: i32) i32 { |
| 5965 | 5966 | //if (index < 2) return index; |
| ... | ... | @@ -5968,7 +5969,7 @@ fn fibonacci(index: i32) i32 { |
| 5968 | 5969 | |
| 5969 | 5970 | test "fibonacci" { |
| 5970 | 5971 | comptime { |
| 5971 | | assert(fibonacci(7) == 13); |
| 5972 | expect(fibonacci(7) == 13); |
| 5972 | 5973 | } |
| 5973 | 5974 | } |
| 5974 | 5975 | {#code_end#} |
| ... | ... | @@ -5979,10 +5980,10 @@ test "fibonacci" { |
| 5979 | 5980 | {#link|@setEvalBranchQuota#} to change the default number 1000 to something else. |
| 5980 | 5981 | </p> |
| 5981 | 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 | 5984 | </p> |
| 5984 | | {#code_begin|test_err|unable to evaluate constant expression#} |
| 5985 | | const assert = @import("std").debug.assert; |
| 5985 | {#code_begin|test_err|encountered @panic at compile-time#} |
| 5986 | const expect = @import("std").testing.expect; |
| 5986 | 5987 | |
| 5987 | 5988 | fn fibonacci(index: i32) i32 { |
| 5988 | 5989 | if (index < 2) return index; |
| ... | ... | @@ -5991,16 +5992,15 @@ fn fibonacci(index: i32) i32 { |
| 5991 | 5992 | |
| 5992 | 5993 | test "fibonacci" { |
| 5993 | 5994 | comptime { |
| 5994 | | assert(fibonacci(7) == 99999); |
| 5995 | expect(fibonacci(7) == 99999); |
| 5995 | 5996 | } |
| 5996 | 5997 | } |
| 5997 | 5998 | {#code_end#} |
| 5998 | 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 | 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 | | code is undefined behavior, and undefined behavior causes a compile error if it is detected |
| 6003 | | at compile-time. |
| 6002 | {#syntax#}@panic{#endsyntax#} it emitted a compile error because a panic during compile |
| 6003 | causes a compile error if it is detected at compile-time. |
| 6004 | 6004 | </p> |
| 6005 | 6005 | |
| 6006 | 6006 | <p> |
| ... | ... | @@ -6042,7 +6042,7 @@ fn sum(numbers: []const i32) i32 { |
| 6042 | 6042 | } |
| 6043 | 6043 | |
| 6044 | 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 | 6047 | {#code_end#} |
| 6048 | 6048 | <p> |
| ... | ... | @@ -6435,7 +6435,7 @@ volatile ( |
| 6435 | 6435 | {#code_begin|test|global-asm#} |
| 6436 | 6436 | {#target_linux_x86_64#} |
| 6437 | 6437 | const std = @import("std"); |
| 6438 | | const assert = std.debug.assert; |
| 6438 | const expect = std.testing.expect; |
| 6439 | 6439 | |
| 6440 | 6440 | comptime { |
| 6441 | 6441 | asm ( |
| ... | ... | @@ -6450,7 +6450,7 @@ comptime { |
| 6450 | 6450 | extern fn my_func(a: i32, b: i32) i32; |
| 6451 | 6451 | |
| 6452 | 6452 | test "global assembly" { |
| 6453 | | assert(my_func(12, 34) == 46); |
| 6453 | expect(my_func(12, 34) == 46); |
| 6454 | 6454 | } |
| 6455 | 6455 | {#code_end#} |
| 6456 | 6456 | {#header_close#} |
| ... | ... | @@ -6485,13 +6485,13 @@ test "global assembly" { |
| 6485 | 6485 | </p> |
| 6486 | 6486 | {#code_begin|test#} |
| 6487 | 6487 | const std = @import("std"); |
| 6488 | | const assert = std.debug.assert; |
| 6488 | const expect = std.testing.expect; |
| 6489 | 6489 | |
| 6490 | 6490 | var x: i32 = 1; |
| 6491 | 6491 | |
| 6492 | 6492 | test "suspend with no resume" { |
| 6493 | 6493 | var frame = async func(); |
| 6494 | | assert(x == 2); |
| 6494 | expect(x == 2); |
| 6495 | 6495 | } |
| 6496 | 6496 | |
| 6497 | 6497 | fn func() void { |
| ... | ... | @@ -6511,21 +6511,21 @@ fn func() void { |
| 6511 | 6511 | </p> |
| 6512 | 6512 | {#code_begin|test#} |
| 6513 | 6513 | const std = @import("std"); |
| 6514 | | const assert = std.debug.assert; |
| 6514 | const expect = std.testing.expect; |
| 6515 | 6515 | |
| 6516 | 6516 | var the_frame: anyframe = undefined; |
| 6517 | 6517 | var result = false; |
| 6518 | 6518 | |
| 6519 | 6519 | test "async function suspend with block" { |
| 6520 | 6520 | _ = async testSuspendBlock(); |
| 6521 | | assert(!result); |
| 6521 | expect(!result); |
| 6522 | 6522 | resume the_frame; |
| 6523 | | assert(result); |
| 6523 | expect(result); |
| 6524 | 6524 | } |
| 6525 | 6525 | |
| 6526 | 6526 | fn testSuspendBlock() void { |
| 6527 | 6527 | suspend { |
| 6528 | | comptime assert(@TypeOf(@frame()) == *@Frame(testSuspendBlock)); |
| 6528 | comptime expect(@TypeOf(@frame()) == *@Frame(testSuspendBlock)); |
| 6529 | 6529 | the_frame = @frame(); |
| 6530 | 6530 | } |
| 6531 | 6531 | result = true; |
| ... | ... | @@ -6549,12 +6549,12 @@ fn testSuspendBlock() void { |
| 6549 | 6549 | </p> |
| 6550 | 6550 | {#code_begin|test#} |
| 6551 | 6551 | const std = @import("std"); |
| 6552 | | const assert = std.debug.assert; |
| 6552 | const expect = std.testing.expect; |
| 6553 | 6553 | |
| 6554 | 6554 | test "resume from suspend" { |
| 6555 | 6555 | var my_result: i32 = 1; |
| 6556 | 6556 | _ = async testResumeFromSuspend(&my_result); |
| 6557 | | std.debug.assert(my_result == 2); |
| 6557 | std.testing.expect(my_result == 2); |
| 6558 | 6558 | } |
| 6559 | 6559 | fn testResumeFromSuspend(my_result: *i32) void { |
| 6560 | 6560 | suspend { |
| ... | ... | @@ -6578,7 +6578,7 @@ fn testResumeFromSuspend(my_result: *i32) void { |
| 6578 | 6578 | </p> |
| 6579 | 6579 | {#code_begin|test#} |
| 6580 | 6580 | const std = @import("std"); |
| 6581 | | const assert = std.debug.assert; |
| 6581 | const expect = std.testing.expect; |
| 6582 | 6582 | |
| 6583 | 6583 | test "async and await" { |
| 6584 | 6584 | // Here we have an exception where we do not match an async |
| ... | ... | @@ -6592,7 +6592,7 @@ test "async and await" { |
| 6592 | 6592 | |
| 6593 | 6593 | fn amain() void { |
| 6594 | 6594 | var frame = async func(); |
| 6595 | | comptime assert(@TypeOf(frame) == @Frame(func)); |
| 6595 | comptime expect(@TypeOf(frame) == @Frame(func)); |
| 6596 | 6596 | |
| 6597 | 6597 | const ptr: anyframe->void = &frame; |
| 6598 | 6598 | const any_ptr: anyframe = ptr; |
| ... | ... | @@ -6622,7 +6622,7 @@ fn func() void { |
| 6622 | 6622 | </p> |
| 6623 | 6623 | {#code_begin|test#} |
| 6624 | 6624 | const std = @import("std"); |
| 6625 | | const assert = std.debug.assert; |
| 6625 | const expect = std.testing.expect; |
| 6626 | 6626 | |
| 6627 | 6627 | var the_frame: anyframe = undefined; |
| 6628 | 6628 | var final_result: i32 = 0; |
| ... | ... | @@ -6633,8 +6633,8 @@ test "async function await" { |
| 6633 | 6633 | seq('f'); |
| 6634 | 6634 | resume the_frame; |
| 6635 | 6635 | seq('i'); |
| 6636 | | assert(final_result == 1234); |
| 6637 | | assert(std.mem.eql(u8, &seq_points, "abcdefghi")); |
| 6636 | expect(final_result == 1234); |
| 6637 | expect(std.mem.eql(u8, &seq_points, "abcdefghi")); |
| 6638 | 6638 | } |
| 6639 | 6639 | fn amain() void { |
| 6640 | 6640 | seq('b'); |
| ... | ... | @@ -6848,9 +6848,9 @@ fn readFile(allocator: *Allocator, filename: []const u8) ![]u8 { |
| 6848 | 6848 | for the current target to match the C ABI. When the child type of a pointer has |
| 6849 | 6849 | this alignment, the alignment can be omitted from the type. |
| 6850 | 6850 | </p> |
| 6851 | | <pre>{#syntax#}const assert = @import("std").debug.assert; |
| 6851 | <pre>{#syntax#}const expect = @import("std").testing.expect; |
| 6852 | 6852 | comptime { |
| 6853 | | assert(*u32 == *align(@alignOf(u32)) u32); |
| 6853 | expect(*u32 == *align(@alignOf(u32)) u32); |
| 6854 | 6854 | }{#endsyntax#}</pre> |
| 6855 | 6855 | <p> |
| 6856 | 6856 | The result is a target-specific compile time constant. It is guaranteed to be |
| ... | ... | @@ -6886,7 +6886,7 @@ comptime { |
| 6886 | 6886 | </p> |
| 6887 | 6887 | {#code_begin|test#} |
| 6888 | 6888 | const std = @import("std"); |
| 6889 | | const assert = std.debug.assert; |
| 6889 | const expect = std.testing.expect; |
| 6890 | 6890 | |
| 6891 | 6891 | test "async fn pointer in a struct field" { |
| 6892 | 6892 | var data: i32 = 1; |
| ... | ... | @@ -6896,9 +6896,9 @@ test "async fn pointer in a struct field" { |
| 6896 | 6896 | var foo = Foo{ .bar = func }; |
| 6897 | 6897 | var bytes: [64]u8 align(@alignOf(@Frame(func))) = undefined; |
| 6898 | 6898 | const f = @asyncCall(&bytes, {}, foo.bar, .{&data}); |
| 6899 | | assert(data == 2); |
| 6899 | expect(data == 2); |
| 6900 | 6900 | resume f; |
| 6901 | | assert(data == 4); |
| 6901 | expect(data == 4); |
| 6902 | 6902 | } |
| 6903 | 6903 | |
| 6904 | 6904 | fn func(y: *i32) void { |
| ... | ... | @@ -7082,10 +7082,10 @@ fn func(y: *i32) void { |
| 7082 | 7082 | Calls a function, in the same way that invoking an expression with parentheses does: |
| 7083 | 7083 | </p> |
| 7084 | 7084 | {#code_begin|test|call#} |
| 7085 | | const assert = @import("std").debug.assert; |
| 7085 | const expect = @import("std").testing.expect; |
| 7086 | 7086 | |
| 7087 | 7087 | test "noinline function call" { |
| 7088 | | assert(@call(.{}, add, .{3, 9}) == 12); |
| 7088 | expect(@call(.{}, add, .{3, 9}) == 12); |
| 7089 | 7089 | } |
| 7090 | 7090 | |
| 7091 | 7091 | fn add(a: i32, b: i32) i32 { |
| ... | ... | @@ -7544,14 +7544,14 @@ const Point = struct { |
| 7544 | 7544 | }; |
| 7545 | 7545 | |
| 7546 | 7546 | test "field access by string" { |
| 7547 | | const assert = std.debug.assert; |
| 7547 | const expect = std.testing.expect; |
| 7548 | 7548 | var p = Point {.x = 0, .y = 0}; |
| 7549 | 7549 | |
| 7550 | 7550 | @field(p, "x") = 4; |
| 7551 | 7551 | @field(p, "y") = @field(p, "x") + 1; |
| 7552 | 7552 | |
| 7553 | | assert(@field(p, "x") == 4); |
| 7554 | | assert(@field(p, "y") == 5); |
| 7553 | expect(@field(p, "x") == 4); |
| 7554 | expect(@field(p, "y") == 5); |
| 7555 | 7555 | } |
| 7556 | 7556 | {#code_end#} |
| 7557 | 7557 | |
| ... | ... | @@ -7657,7 +7657,7 @@ fn func() void { |
| 7657 | 7657 | </p> |
| 7658 | 7658 | {#code_begin|test#} |
| 7659 | 7659 | const std = @import("std"); |
| 7660 | | const assert = std.debug.assert; |
| 7660 | const expect = std.testing.expect; |
| 7661 | 7661 | |
| 7662 | 7662 | const Foo = struct { |
| 7663 | 7663 | nope: i32, |
| ... | ... | @@ -7667,16 +7667,16 @@ const Foo = struct { |
| 7667 | 7667 | }; |
| 7668 | 7668 | |
| 7669 | 7669 | test "@hasDecl" { |
| 7670 | | assert(@hasDecl(Foo, "blah")); |
| 7670 | expect(@hasDecl(Foo, "blah")); |
| 7671 | 7671 | |
| 7672 | 7672 | // Even though `hi` is private, @hasDecl returns true because this test is |
| 7673 | 7673 | // in the same file scope as Foo. It would return false if Foo was declared |
| 7674 | 7674 | // in a different file. |
| 7675 | | assert(@hasDecl(Foo, "hi")); |
| 7675 | expect(@hasDecl(Foo, "hi")); |
| 7676 | 7676 | |
| 7677 | 7677 | // @hasDecl is for declarations; not fields. |
| 7678 | | assert(!@hasDecl(Foo, "nope")); |
| 7679 | | assert(!@hasDecl(Foo, "nope1234")); |
| 7678 | expect(!@hasDecl(Foo, "nope")); |
| 7679 | expect(!@hasDecl(Foo, "nope1234")); |
| 7680 | 7680 | } |
| 7681 | 7681 | {#code_end#} |
| 7682 | 7682 | {#see_also|@hasField#} |
| ... | ... | @@ -7851,14 +7851,14 @@ mem.set(u8, dest, c);{#endsyntax#}</pre> |
| 7851 | 7851 | {#code_begin|test#} |
| 7852 | 7852 | const std = @import("std"); |
| 7853 | 7853 | const builtin = @import("builtin"); |
| 7854 | | const assert = std.debug.assert; |
| 7854 | const expect = std.testing.expect; |
| 7855 | 7855 | |
| 7856 | 7856 | test "@wasmMemoryGrow" { |
| 7857 | 7857 | if (builtin.arch != .wasm32) return error.SkipZigTest; |
| 7858 | 7858 | |
| 7859 | 7859 | var prev = @wasmMemorySize(0); |
| 7860 | | assert(prev == @wasmMemoryGrow(0, 1)); |
| 7861 | | assert(prev + 1 == @wasmMemorySize(0)); |
| 7860 | expect(prev == @wasmMemoryGrow(0, 1)); |
| 7861 | expect(prev + 1 == @wasmMemorySize(0)); |
| 7862 | 7862 | } |
| 7863 | 7863 | {#code_end#} |
| 7864 | 7864 | {#see_also|@wasmMemorySize#} |
| ... | ... | @@ -8194,13 +8194,13 @@ test "@setRuntimeSafety" { |
| 8194 | 8194 | </p> |
| 8195 | 8195 | {#code_begin|test#} |
| 8196 | 8196 | const std = @import("std"); |
| 8197 | | const assert = std.debug.assert; |
| 8197 | const expect = std.testing.expect; |
| 8198 | 8198 | |
| 8199 | 8199 | test "vector @splat" { |
| 8200 | 8200 | const scalar: u32 = 5; |
| 8201 | 8201 | const result = @splat(4, scalar); |
| 8202 | | comptime assert(@TypeOf(result) == std.meta.Vector(4, u32)); |
| 8203 | | assert(std.mem.eql(u32, &@as([4]u32, result), &[_]u32{ 5, 5, 5, 5 })); |
| 8202 | comptime expect(@TypeOf(result) == std.meta.Vector(4, u32)); |
| 8203 | expect(std.mem.eql(u32, &@as([4]u32, result), &[_]u32{ 5, 5, 5, 5 })); |
| 8204 | 8204 | } |
| 8205 | 8205 | {#code_end#} |
| 8206 | 8206 | <p> |
| ... | ... | @@ -8410,12 +8410,12 @@ fn doTheTest() void { |
| 8410 | 8410 | </p> |
| 8411 | 8411 | {#code_begin|test#} |
| 8412 | 8412 | const std = @import("std"); |
| 8413 | | const assert = std.debug.assert; |
| 8413 | const expect = std.testing.expect; |
| 8414 | 8414 | |
| 8415 | 8415 | test "@This()" { |
| 8416 | 8416 | var items = [_]i32{ 1, 2, 3, 4 }; |
| 8417 | 8417 | const list = List(i32){ .items = items[0..] }; |
| 8418 | | assert(list.length() == 4); |
| 8418 | expect(list.length() == 4); |
| 8419 | 8419 | } |
| 8420 | 8420 | |
| 8421 | 8421 | fn List(comptime T: type) type { |
| ... | ... | @@ -8456,12 +8456,12 @@ test "integer cast panic" { |
| 8456 | 8456 | </p> |
| 8457 | 8457 | {#code_begin|test|truncate#} |
| 8458 | 8458 | const std = @import("std"); |
| 8459 | | const assert = std.debug.assert; |
| 8459 | const expect = std.testing.expect; |
| 8460 | 8460 | |
| 8461 | 8461 | test "integer truncation" { |
| 8462 | 8462 | var a: u16 = 0xabcd; |
| 8463 | 8463 | var b: u8 = @truncate(u8, a); |
| 8464 | | assert(b == 0xcd); |
| 8464 | expect(b == 0xcd); |
| 8465 | 8465 | } |
| 8466 | 8466 | {#code_end#} |
| 8467 | 8467 | <p> |
| ... | ... | @@ -8544,13 +8544,13 @@ test "integer truncation" { |
| 8544 | 8544 | </p> |
| 8545 | 8545 | {#code_begin|test#} |
| 8546 | 8546 | const std = @import("std"); |
| 8547 | | const assert = std.debug.assert; |
| 8547 | const expect = std.testing.expect; |
| 8548 | 8548 | |
| 8549 | 8549 | test "no runtime side effects" { |
| 8550 | 8550 | var data: i32 = 0; |
| 8551 | 8551 | const T = @TypeOf(foo(i32, &data)); |
| 8552 | | comptime assert(T == i32); |
| 8553 | | assert(data == 0); |
| 8552 | comptime expect(T == i32); |
| 8553 | expect(data == 0); |
| 8554 | 8554 | } |
| 8555 | 8555 | |
| 8556 | 8556 | fn foo(comptime T: type, ptr: *T) T { |
| ... | ... | @@ -8853,16 +8853,16 @@ pub fn main() void { |
| 8853 | 8853 | </ul> |
| 8854 | 8854 | {#code_begin|test#} |
| 8855 | 8855 | const std = @import("std"); |
| 8856 | | const assert = std.debug.assert; |
| 8856 | const expect = std.testing.expect; |
| 8857 | 8857 | const minInt = std.math.minInt; |
| 8858 | 8858 | const maxInt = std.math.maxInt; |
| 8859 | 8859 | |
| 8860 | 8860 | test "wraparound addition and subtraction" { |
| 8861 | 8861 | const x: i32 = maxInt(i32); |
| 8862 | 8862 | const min_val = x +% 1; |
| 8863 | | assert(min_val == minInt(i32)); |
| 8863 | expect(min_val == minInt(i32)); |
| 8864 | 8864 | const max_val = min_val -% 1; |
| 8865 | | assert(max_val == maxInt(i32)); |
| 8865 | expect(max_val == maxInt(i32)); |
| 8866 | 8866 | } |
| 8867 | 8867 | {#code_end#} |
| 8868 | 8868 | {#header_close#} |
| ... | ... | @@ -9287,13 +9287,13 @@ pub fn main() void { |
| 9287 | 9287 | {#code_begin|test|allocator#} |
| 9288 | 9288 | const std = @import("std"); |
| 9289 | 9289 | const Allocator = std.mem.Allocator; |
| 9290 | | const assert = std.debug.assert; |
| 9290 | const expect = std.testing.expect; |
| 9291 | 9291 | |
| 9292 | 9292 | test "using an allocator" { |
| 9293 | 9293 | var buffer: [100]u8 = undefined; |
| 9294 | 9294 | const allocator = &std.heap.FixedBufferAllocator.init(&buffer).allocator; |
| 9295 | 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 | 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 | 9560 | {#code_begin|test|detect_test#} |
| 9561 | 9561 | const std = @import("std"); |
| 9562 | 9562 | const builtin = std.builtin; |
| 9563 | | const assert = std.debug.assert; |
| 9563 | const expect = std.testing.expect; |
| 9564 | 9564 | |
| 9565 | 9565 | test "builtin.is_test" { |
| 9566 | | assert(builtin.is_test); |
| 9566 | expect(builtin.is_test); |
| 9567 | 9567 | } |
| 9568 | 9568 | {#code_end#} |
| 9569 | 9569 | <p> |
| ... | ... | @@ -9613,7 +9613,7 @@ test "assert in release fast mode" { |
| 9613 | 9613 | const std = @import("std"); |
| 9614 | 9614 | const expect = std.testing.expect; |
| 9615 | 9615 | |
| 9616 | | test "assert in release fast mode" { |
| 9616 | test "expect in release fast mode" { |
| 9617 | 9617 | expect(false); |
| 9618 | 9618 | } |
| 9619 | 9619 | {#code_end#} |