| ... | ... | @@ -358,7 +358,7 @@ test "comments" { |
| 358 | 358 | //expect(false); |
| 359 | 359 | |
| 360 | 360 | const x = true; // another comment |
| 361 | | expect(x); |
| 361 | try expect(x); |
| 362 | 362 | } |
| 363 | 363 | {#code_end#} |
| 364 | 364 | <p> |
| ... | ... | @@ -718,15 +718,15 @@ const mem = @import("std").mem; |
| 718 | 718 | |
| 719 | 719 | test "string literals" { |
| 720 | 720 | const bytes = "hello"; |
| 721 | | expect(@TypeOf(bytes) == *const [5:0]u8); |
| 722 | | expect(bytes.len == 5); |
| 723 | | expect(bytes[1] == 'e'); |
| 724 | | expect(bytes[5] == 0); |
| 725 | | expect('e' == '\x65'); |
| 726 | | expect('\u{1f4a9}' == 128169); |
| 727 | | expect('π―' == 128175); |
| 728 | | expect(mem.eql(u8, "hello", "h\x65llo")); |
| 729 | | expect("\xff"[0] == 0xff); // non-UTF-8 strings are possible with \xNN notation. |
| 721 | try expect(@TypeOf(bytes) == *const [5:0]u8); |
| 722 | try expect(bytes.len == 5); |
| 723 | try expect(bytes[1] == 'e'); |
| 724 | try expect(bytes[5] == 0); |
| 725 | try expect('e' == '\x65'); |
| 726 | try expect('\u{1f4a9}' == 128169); |
| 727 | try expect('π―' == 128175); |
| 728 | try expect(mem.eql(u8, "hello", "h\x65llo")); |
| 729 | try expect("\xff"[0] == 0xff); // non-UTF-8 strings are possible with \xNN notation. |
| 730 | 730 | } |
| 731 | 731 | {#code_end#} |
| 732 | 732 | {#see_also|Arrays|Zig Test|Source Encoding#} |
| ... | ... | @@ -826,7 +826,7 @@ test "var" { |
| 826 | 826 | |
| 827 | 827 | y += 1; |
| 828 | 828 | |
| 829 | | expect(y == 5679); |
| 829 | try expect(y == 5679); |
| 830 | 830 | } |
| 831 | 831 | {#code_end#} |
| 832 | 832 | <p>Variables must be initialized:</p> |
| ... | ... | @@ -845,7 +845,7 @@ const expect = @import("std").testing.expect; |
| 845 | 845 | test "init with undefined" { |
| 846 | 846 | var x: i32 = undefined; |
| 847 | 847 | x = 1; |
| 848 | | expect(x == 1); |
| 848 | try expect(x == 1); |
| 849 | 849 | } |
| 850 | 850 | {#code_end#} |
| 851 | 851 | <p> |
| ... | ... | @@ -887,8 +887,8 @@ var y: i32 = add(10, x); |
| 887 | 887 | const x: i32 = add(12, 34); |
| 888 | 888 | |
| 889 | 889 | test "global variables" { |
| 890 | | expect(x == 46); |
| 891 | | expect(y == 56); |
| 890 | try expect(x == 46); |
| 891 | try expect(y == 56); |
| 892 | 892 | } |
| 893 | 893 | |
| 894 | 894 | fn add(a: i32, b: i32) i32 { |
| ... | ... | @@ -906,8 +906,8 @@ const std = @import("std"); |
| 906 | 906 | const expect = std.testing.expect; |
| 907 | 907 | |
| 908 | 908 | test "namespaced global variable" { |
| 909 | | expect(foo() == 1235); |
| 910 | | expect(foo() == 1236); |
| 909 | try expect(foo() == 1235); |
| 910 | try expect(foo() == 1236); |
| 911 | 911 | } |
| 912 | 912 | |
| 913 | 913 | fn foo() i32 { |
| ... | ... | @@ -985,8 +985,8 @@ test "comptime vars" { |
| 985 | 985 | x += 1; |
| 986 | 986 | y += 1; |
| 987 | 987 | |
| 988 | | expect(x == 2); |
| 989 | | expect(y == 2); |
| 988 | try expect(x == 2); |
| 989 | try expect(y == 2); |
| 990 | 990 | |
| 991 | 991 | if (y != 2) { |
| 992 | 992 | // This compile error never triggers because y is a comptime variable, |
| ... | ... | @@ -1777,6 +1777,7 @@ orelse catch |
| 1777 | 1777 | {#header_open|Arrays#} |
| 1778 | 1778 | {#code_begin|test|arrays#} |
| 1779 | 1779 | const expect = @import("std").testing.expect; |
| 1780 | const assert = @import("std").debug.assert; |
| 1780 | 1781 | const mem = @import("std").mem; |
| 1781 | 1782 | |
| 1782 | 1783 | // array literal |
| ... | ... | @@ -1784,14 +1785,14 @@ const message = [_]u8{ 'h', 'e', 'l', 'l', 'o' }; |
| 1784 | 1785 | |
| 1785 | 1786 | // get the size of an array |
| 1786 | 1787 | comptime { |
| 1787 | | expect(message.len == 5); |
| 1788 | assert(message.len == 5); |
| 1788 | 1789 | } |
| 1789 | 1790 | |
| 1790 | 1791 | // A string literal is a single-item pointer to an array literal. |
| 1791 | 1792 | const same_message = "hello"; |
| 1792 | 1793 | |
| 1793 | 1794 | comptime { |
| 1794 | | expect(mem.eql(u8, &message, same_message)); |
| 1795 | assert(mem.eql(u8, &message, same_message)); |
| 1795 | 1796 | } |
| 1796 | 1797 | |
| 1797 | 1798 | test "iterate over an array" { |
| ... | ... | @@ -1799,7 +1800,7 @@ test "iterate over an array" { |
| 1799 | 1800 | for (message) |byte| { |
| 1800 | 1801 | sum += byte; |
| 1801 | 1802 | } |
| 1802 | | expect(sum == 'h' + 'e' + 'l' * 2 + 'o'); |
| 1803 | try expect(sum == 'h' + 'e' + 'l' * 2 + 'o'); |
| 1803 | 1804 | } |
| 1804 | 1805 | |
| 1805 | 1806 | // modifiable array |
| ... | ... | @@ -1809,8 +1810,8 @@ test "modify an array" { |
| 1809 | 1810 | for (some_integers) |*item, i| { |
| 1810 | 1811 | item.* = @intCast(i32, i); |
| 1811 | 1812 | } |
| 1812 | | expect(some_integers[10] == 10); |
| 1813 | | expect(some_integers[99] == 99); |
| 1813 | try expect(some_integers[10] == 10); |
| 1814 | try expect(some_integers[99] == 99); |
| 1814 | 1815 | } |
| 1815 | 1816 | |
| 1816 | 1817 | // array concatenation works if the values are known |
| ... | ... | @@ -1819,7 +1820,7 @@ const part_one = [_]i32{ 1, 2, 3, 4 }; |
| 1819 | 1820 | const part_two = [_]i32{ 5, 6, 7, 8 }; |
| 1820 | 1821 | const all_of_it = part_one ++ part_two; |
| 1821 | 1822 | comptime { |
| 1822 | | expect(mem.eql(i32, &all_of_it, &[_]i32{ 1, 2, 3, 4, 5, 6, 7, 8 })); |
| 1823 | assert(mem.eql(i32, &all_of_it, &[_]i32{ 1, 2, 3, 4, 5, 6, 7, 8 })); |
| 1823 | 1824 | } |
| 1824 | 1825 | |
| 1825 | 1826 | // remember that string literals are arrays |
| ... | ... | @@ -1827,21 +1828,21 @@ const hello = "hello"; |
| 1827 | 1828 | const world = "world"; |
| 1828 | 1829 | const hello_world = hello ++ " " ++ world; |
| 1829 | 1830 | comptime { |
| 1830 | | expect(mem.eql(u8, hello_world, "hello world")); |
| 1831 | assert(mem.eql(u8, hello_world, "hello world")); |
| 1831 | 1832 | } |
| 1832 | 1833 | |
| 1833 | 1834 | // ** does repeating patterns |
| 1834 | 1835 | const pattern = "ab" ** 3; |
| 1835 | 1836 | comptime { |
| 1836 | | expect(mem.eql(u8, pattern, "ababab")); |
| 1837 | assert(mem.eql(u8, pattern, "ababab")); |
| 1837 | 1838 | } |
| 1838 | 1839 | |
| 1839 | 1840 | // initialize an array to zero |
| 1840 | 1841 | const all_zero = [_]u16{0} ** 10; |
| 1841 | 1842 | |
| 1842 | 1843 | comptime { |
| 1843 | | expect(all_zero.len == 10); |
| 1844 | | expect(all_zero[5] == 0); |
| 1844 | assert(all_zero.len == 10); |
| 1845 | assert(all_zero[5] == 0); |
| 1845 | 1846 | } |
| 1846 | 1847 | |
| 1847 | 1848 | // use compile-time code to initialize an array |
| ... | ... | @@ -1861,8 +1862,8 @@ const Point = struct { |
| 1861 | 1862 | }; |
| 1862 | 1863 | |
| 1863 | 1864 | test "compile-time array initialization" { |
| 1864 | | expect(fancy_array[4].x == 4); |
| 1865 | | expect(fancy_array[4].y == 8); |
| 1865 | try expect(fancy_array[4].x == 4); |
| 1866 | try expect(fancy_array[4].y == 8); |
| 1866 | 1867 | } |
| 1867 | 1868 | |
| 1868 | 1869 | // call a function to initialize an array |
| ... | ... | @@ -1874,9 +1875,9 @@ fn makePoint(x: i32) Point { |
| 1874 | 1875 | }; |
| 1875 | 1876 | } |
| 1876 | 1877 | test "array initialization with function calls" { |
| 1877 | | expect(more_points[4].x == 3); |
| 1878 | | expect(more_points[4].y == 6); |
| 1879 | | expect(more_points.len == 10); |
| 1878 | try expect(more_points[4].x == 3); |
| 1879 | try expect(more_points[4].y == 6); |
| 1880 | try expect(more_points.len == 10); |
| 1880 | 1881 | } |
| 1881 | 1882 | {#code_end#} |
| 1882 | 1883 | {#see_also|for|Slices#} |
| ... | ... | @@ -1890,10 +1891,10 @@ const expect = std.testing.expect; |
| 1890 | 1891 | |
| 1891 | 1892 | test "anonymous list literal syntax" { |
| 1892 | 1893 | var array: [4]u8 = .{11, 22, 33, 44}; |
| 1893 | | expect(array[0] == 11); |
| 1894 | | expect(array[1] == 22); |
| 1895 | | expect(array[2] == 33); |
| 1896 | | expect(array[3] == 44); |
| 1894 | try expect(array[0] == 11); |
| 1895 | try expect(array[1] == 22); |
| 1896 | try expect(array[2] == 33); |
| 1897 | try expect(array[3] == 44); |
| 1897 | 1898 | } |
| 1898 | 1899 | {#code_end#} |
| 1899 | 1900 | <p> |
| ... | ... | @@ -1905,15 +1906,15 @@ const std = @import("std"); |
| 1905 | 1906 | const expect = std.testing.expect; |
| 1906 | 1907 | |
| 1907 | 1908 | test "fully anonymous list literal" { |
| 1908 | | dump(.{ @as(u32, 1234), @as(f64, 12.34), true, "hi"}); |
| 1909 | try dump(.{ @as(u32, 1234), @as(f64, 12.34), true, "hi"}); |
| 1909 | 1910 | } |
| 1910 | 1911 | |
| 1911 | | fn dump(args: anytype) void { |
| 1912 | | expect(args.@"0" == 1234); |
| 1913 | | expect(args.@"1" == 12.34); |
| 1914 | | expect(args.@"2"); |
| 1915 | | expect(args.@"3"[0] == 'h'); |
| 1916 | | expect(args.@"3"[1] == 'i'); |
| 1912 | fn dump(args: anytype) !void { |
| 1913 | try expect(args.@"0" == 1234); |
| 1914 | try expect(args.@"1" == 12.34); |
| 1915 | try expect(args.@"2"); |
| 1916 | try expect(args.@"3"[0] == 'h'); |
| 1917 | try expect(args.@"3"[1] == 'i'); |
| 1917 | 1918 | } |
| 1918 | 1919 | {#code_end#} |
| 1919 | 1920 | {#header_close#} |
| ... | ... | @@ -1934,13 +1935,13 @@ const mat4x4 = [4][4]f32{ |
| 1934 | 1935 | }; |
| 1935 | 1936 | test "multidimensional arrays" { |
| 1936 | 1937 | // Access the 2D array by indexing the outer array, and then the inner array. |
| 1937 | | expect(mat4x4[1][1] == 1.0); |
| 1938 | try expect(mat4x4[1][1] == 1.0); |
| 1938 | 1939 | |
| 1939 | 1940 | // Here we iterate with for loops. |
| 1940 | 1941 | for (mat4x4) |row, row_index| { |
| 1941 | 1942 | for (row) |cell, column_index| { |
| 1942 | 1943 | if (row_index == column_index) { |
| 1943 | | expect(cell == 1.0); |
| 1944 | try expect(cell == 1.0); |
| 1944 | 1945 | } |
| 1945 | 1946 | } |
| 1946 | 1947 | } |
| ... | ... | @@ -1960,9 +1961,9 @@ const expect = std.testing.expect; |
| 1960 | 1961 | test "null terminated array" { |
| 1961 | 1962 | const array = [_:0]u8 {1, 2, 3, 4}; |
| 1962 | 1963 | |
| 1963 | | expect(@TypeOf(array) == [4:0]u8); |
| 1964 | | expect(array.len == 4); |
| 1965 | | expect(array[4] == 0); |
| 1964 | try expect(@TypeOf(array) == [4:0]u8); |
| 1965 | try expect(array.len == 4); |
| 1966 | try expect(array[4] == 0); |
| 1966 | 1967 | } |
| 1967 | 1968 | {#code_end#} |
| 1968 | 1969 | {#see_also|Sentinel-Terminated Pointers|Sentinel-Terminated Slices#} |
| ... | ... | @@ -2040,17 +2041,17 @@ test "address of syntax" { |
| 2040 | 2041 | const x_ptr = &x; |
| 2041 | 2042 | |
| 2042 | 2043 | // Dereference a pointer: |
| 2043 | | expect(x_ptr.* == 1234); |
| 2044 | try expect(x_ptr.* == 1234); |
| 2044 | 2045 | |
| 2045 | 2046 | // When you get the address of a const variable, you get a const single-item pointer. |
| 2046 | | expect(@TypeOf(x_ptr) == *const i32); |
| 2047 | try expect(@TypeOf(x_ptr) == *const i32); |
| 2047 | 2048 | |
| 2048 | 2049 | // If you want to mutate the value, you'd need an address of a mutable variable: |
| 2049 | 2050 | var y: i32 = 5678; |
| 2050 | 2051 | const y_ptr = &y; |
| 2051 | | expect(@TypeOf(y_ptr) == *i32); |
| 2052 | try expect(@TypeOf(y_ptr) == *i32); |
| 2052 | 2053 | y_ptr.* += 1; |
| 2053 | | expect(y_ptr.* == 5679); |
| 2054 | try expect(y_ptr.* == 5679); |
| 2054 | 2055 | } |
| 2055 | 2056 | |
| 2056 | 2057 | test "pointer array access" { |
| ... | ... | @@ -2059,11 +2060,11 @@ test "pointer array access" { |
| 2059 | 2060 | // does not support pointer arithmetic. |
| 2060 | 2061 | var array = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; |
| 2061 | 2062 | const ptr = &array[2]; |
| 2062 | | expect(@TypeOf(ptr) == *u8); |
| 2063 | try expect(@TypeOf(ptr) == *u8); |
| 2063 | 2064 | |
| 2064 | | expect(array[2] == 3); |
| 2065 | try expect(array[2] == 3); |
| 2065 | 2066 | ptr.* += 1; |
| 2066 | | expect(array[2] == 4); |
| 2067 | try expect(array[2] == 4); |
| 2067 | 2068 | } |
| 2068 | 2069 | {#code_end#} |
| 2069 | 2070 | <p> |
| ... | ... | @@ -2081,11 +2082,11 @@ const expect = @import("std").testing.expect; |
| 2081 | 2082 | test "pointer slicing" { |
| 2082 | 2083 | var array = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; |
| 2083 | 2084 | const slice = array[2..4]; |
| 2084 | | expect(slice.len == 2); |
| 2085 | try expect(slice.len == 2); |
| 2085 | 2086 | |
| 2086 | | expect(array[3] == 4); |
| 2087 | try expect(array[3] == 4); |
| 2087 | 2088 | slice[1] += 1; |
| 2088 | | expect(array[3] == 5); |
| 2089 | try expect(array[3] == 5); |
| 2089 | 2090 | } |
| 2090 | 2091 | {#code_end#} |
| 2091 | 2092 | <p>Pointers work at compile-time too, as long as the code does not depend on |
| ... | ... | @@ -2099,7 +2100,7 @@ test "comptime pointers" { |
| 2099 | 2100 | const ptr = &x; |
| 2100 | 2101 | ptr.* += 1; |
| 2101 | 2102 | x += 1; |
| 2102 | | expect(ptr.* == 3); |
| 2103 | try expect(ptr.* == 3); |
| 2103 | 2104 | } |
| 2104 | 2105 | } |
| 2105 | 2106 | {#code_end#} |
| ... | ... | @@ -2111,8 +2112,8 @@ const expect = @import("std").testing.expect; |
| 2111 | 2112 | test "@ptrToInt and @intToPtr" { |
| 2112 | 2113 | const ptr = @intToPtr(*i32, 0xdeadbee0); |
| 2113 | 2114 | const addr = @ptrToInt(ptr); |
| 2114 | | expect(@TypeOf(addr) == usize); |
| 2115 | | expect(addr == 0xdeadbee0); |
| 2115 | try expect(@TypeOf(addr) == usize); |
| 2116 | try expect(addr == 0xdeadbee0); |
| 2116 | 2117 | } |
| 2117 | 2118 | {#code_end#} |
| 2118 | 2119 | <p>Zig is able to preserve memory addresses in comptime code, as long as |
| ... | ... | @@ -2126,8 +2127,8 @@ test "comptime @intToPtr" { |
| 2126 | 2127 | // ptr is never dereferenced. |
| 2127 | 2128 | const ptr = @intToPtr(*i32, 0xdeadbee0); |
| 2128 | 2129 | const addr = @ptrToInt(ptr); |
| 2129 | | expect(@TypeOf(addr) == usize); |
| 2130 | | expect(addr == 0xdeadbee0); |
| 2130 | try expect(@TypeOf(addr) == usize); |
| 2131 | try expect(addr == 0xdeadbee0); |
| 2131 | 2132 | } |
| 2132 | 2133 | } |
| 2133 | 2134 | {#code_end#} |
| ... | ... | @@ -2142,7 +2143,7 @@ const expect = @import("std").testing.expect; |
| 2142 | 2143 | |
| 2143 | 2144 | test "volatile" { |
| 2144 | 2145 | const mmio_ptr = @intToPtr(*volatile u8, 0x12345678); |
| 2145 | | expect(@TypeOf(mmio_ptr) == *volatile u8); |
| 2146 | try expect(@TypeOf(mmio_ptr) == *volatile u8); |
| 2146 | 2147 | } |
| 2147 | 2148 | {#code_end#} |
| 2148 | 2149 | <p> |
| ... | ... | @@ -2163,20 +2164,20 @@ const expect = std.testing.expect; |
| 2163 | 2164 | test "pointer casting" { |
| 2164 | 2165 | const bytes align(@alignOf(u32)) = [_]u8{ 0x12, 0x12, 0x12, 0x12 }; |
| 2165 | 2166 | const u32_ptr = @ptrCast(*const u32, &bytes); |
| 2166 | | expect(u32_ptr.* == 0x12121212); |
| 2167 | try expect(u32_ptr.* == 0x12121212); |
| 2167 | 2168 | |
| 2168 | 2169 | // Even this example is contrived - there are better ways to do the above than |
| 2169 | 2170 | // pointer casting. For example, using a slice narrowing cast: |
| 2170 | 2171 | const u32_value = std.mem.bytesAsSlice(u32, bytes[0..])[0]; |
| 2171 | | expect(u32_value == 0x12121212); |
| 2172 | try expect(u32_value == 0x12121212); |
| 2172 | 2173 | |
| 2173 | 2174 | // And even another way, the most straightforward way to do it: |
| 2174 | | expect(@bitCast(u32, bytes) == 0x12121212); |
| 2175 | try expect(@bitCast(u32, bytes) == 0x12121212); |
| 2175 | 2176 | } |
| 2176 | 2177 | |
| 2177 | 2178 | test "pointer child type" { |
| 2178 | 2179 | // pointer types have a `child` field which tells you the type they point to. |
| 2179 | | expect(@typeInfo(*u32).Pointer.child == u32); |
| 2180 | try expect(@typeInfo(*u32).Pointer.child == u32); |
| 2180 | 2181 | } |
| 2181 | 2182 | {#code_end#} |
| 2182 | 2183 | {#header_open|Alignment#} |
| ... | ... | @@ -2201,10 +2202,10 @@ const expect = std.testing.expect; |
| 2201 | 2202 | test "variable alignment" { |
| 2202 | 2203 | var x: i32 = 1234; |
| 2203 | 2204 | const align_of_i32 = @alignOf(@TypeOf(x)); |
| 2204 | | expect(@TypeOf(&x) == *i32); |
| 2205 | | expect(*i32 == *align(align_of_i32) i32); |
| 2205 | try expect(@TypeOf(&x) == *i32); |
| 2206 | try expect(*i32 == *align(align_of_i32) i32); |
| 2206 | 2207 | if (std.Target.current.cpu.arch == .x86_64) { |
| 2207 | | expect(@typeInfo(*i32).Pointer.alignment == 4); |
| 2208 | try expect(@typeInfo(*i32).Pointer.alignment == 4); |
| 2208 | 2209 | } |
| 2209 | 2210 | } |
| 2210 | 2211 | {#code_end#} |
| ... | ... | @@ -2222,11 +2223,11 @@ const expect = @import("std").testing.expect; |
| 2222 | 2223 | var foo: u8 align(4) = 100; |
| 2223 | 2224 | |
| 2224 | 2225 | test "global variable alignment" { |
| 2225 | | expect(@typeInfo(@TypeOf(&foo)).Pointer.alignment == 4); |
| 2226 | | expect(@TypeOf(&foo) == *align(4) u8); |
| 2226 | try expect(@typeInfo(@TypeOf(&foo)).Pointer.alignment == 4); |
| 2227 | try expect(@TypeOf(&foo) == *align(4) u8); |
| 2227 | 2228 | const as_pointer_to_array: *[1]u8 = &foo; |
| 2228 | 2229 | const as_slice: []u8 = as_pointer_to_array; |
| 2229 | | expect(@TypeOf(as_slice) == []align(4) u8); |
| 2230 | try expect(@TypeOf(as_slice) == []align(4) u8); |
| 2230 | 2231 | } |
| 2231 | 2232 | |
| 2232 | 2233 | fn derp() align(@sizeOf(usize) * 2) i32 { return 1234; } |
| ... | ... | @@ -2234,9 +2235,9 @@ fn noop1() align(1) void {} |
| 2234 | 2235 | fn noop4() align(4) void {} |
| 2235 | 2236 | |
| 2236 | 2237 | test "function alignment" { |
| 2237 | | expect(derp() == 1234); |
| 2238 | | expect(@TypeOf(noop1) == fn() align(1) void); |
| 2239 | | expect(@TypeOf(noop4) == fn() align(4) void); |
| 2238 | try expect(derp() == 1234); |
| 2239 | try expect(@TypeOf(noop1) == fn() align(1) void); |
| 2240 | try expect(@TypeOf(noop4) == fn() align(4) void); |
| 2240 | 2241 | noop1(); |
| 2241 | 2242 | noop4(); |
| 2242 | 2243 | } |
| ... | ... | @@ -2253,7 +2254,7 @@ const std = @import("std"); |
| 2253 | 2254 | test "pointer alignment safety" { |
| 2254 | 2255 | var array align(4) = [_]u32{ 0x11111111, 0x11111111 }; |
| 2255 | 2256 | const bytes = std.mem.sliceAsBytes(array[0..]); |
| 2256 | | std.testing.expect(foo(bytes) == 0x11111111); |
| 2257 | try std.testing.expect(foo(bytes) == 0x11111111); |
| 2257 | 2258 | } |
| 2258 | 2259 | fn foo(bytes: []u8) u32 { |
| 2259 | 2260 | const slice4 = bytes[1..5]; |
| ... | ... | @@ -2279,7 +2280,7 @@ const expect = std.testing.expect; |
| 2279 | 2280 | test "allowzero" { |
| 2280 | 2281 | var zero: usize = 0; |
| 2281 | 2282 | var ptr = @intToPtr(*allowzero i32, zero); |
| 2282 | | expect(@ptrToInt(ptr) == 0); |
| 2283 | try expect(@ptrToInt(ptr) == 0); |
| 2283 | 2284 | } |
| 2284 | 2285 | {#code_end#} |
| 2285 | 2286 | {#header_close#} |
| ... | ... | @@ -2321,14 +2322,14 @@ test "basic slices" { |
| 2321 | 2322 | // Both can be accessed with the `len` field. |
| 2322 | 2323 | var known_at_runtime_zero: usize = 0; |
| 2323 | 2324 | const slice = array[known_at_runtime_zero..array.len]; |
| 2324 | | expect(&slice[0] == &array[0]); |
| 2325 | | expect(slice.len == array.len); |
| 2325 | try expect(&slice[0] == &array[0]); |
| 2326 | try expect(slice.len == array.len); |
| 2326 | 2327 | |
| 2327 | 2328 | // Using the address-of operator on a slice gives a single-item pointer, |
| 2328 | 2329 | // while using the `ptr` field gives a many-item pointer. |
| 2329 | | expect(@TypeOf(slice.ptr) == [*]i32); |
| 2330 | | expect(@TypeOf(&slice[0]) == *i32); |
| 2331 | | expect(@ptrToInt(slice.ptr) == @ptrToInt(&slice[0])); |
| 2330 | try expect(@TypeOf(slice.ptr) == [*]i32); |
| 2331 | try expect(@TypeOf(&slice[0]) == *i32); |
| 2332 | try expect(@ptrToInt(slice.ptr) == @ptrToInt(&slice[0])); |
| 2332 | 2333 | |
| 2333 | 2334 | // Slices have array bounds checking. If you try to access something out |
| 2334 | 2335 | // of bounds, you'll get a safety check failure: |
| ... | ... | @@ -2362,7 +2363,7 @@ test "using slices for strings" { |
| 2362 | 2363 | // Generally, you can use UTF-8 and not worry about whether something is a |
| 2363 | 2364 | // string. If you don't need to deal with individual characters, no need |
| 2364 | 2365 | // to decode. |
| 2365 | | expect(mem.eql(u8, hello_world, "hello δΈη")); |
| 2366 | try expect(mem.eql(u8, hello_world, "hello δΈη")); |
| 2366 | 2367 | } |
| 2367 | 2368 | |
| 2368 | 2369 | test "slice pointer" { |
| ... | ... | @@ -2372,16 +2373,16 @@ test "slice pointer" { |
| 2372 | 2373 | // You can use slicing syntax to convert a pointer into a slice: |
| 2373 | 2374 | const slice = ptr[0..5]; |
| 2374 | 2375 | slice[2] = 3; |
| 2375 | | expect(slice[2] == 3); |
| 2376 | try expect(slice[2] == 3); |
| 2376 | 2377 | // The slice is mutable because we sliced a mutable pointer. |
| 2377 | 2378 | // Furthermore, it is actually a pointer to an array, since the start |
| 2378 | 2379 | // and end indexes were both comptime-known. |
| 2379 | | expect(@TypeOf(slice) == *[5]u8); |
| 2380 | try expect(@TypeOf(slice) == *[5]u8); |
| 2380 | 2381 | |
| 2381 | 2382 | // You can also slice a slice: |
| 2382 | 2383 | const slice2 = slice[2..3]; |
| 2383 | | expect(slice2.len == 1); |
| 2384 | | expect(slice2[0] == 3); |
| 2384 | try expect(slice2.len == 1); |
| 2385 | try expect(slice2[0] == 3); |
| 2385 | 2386 | } |
| 2386 | 2387 | {#code_end#} |
| 2387 | 2388 | {#see_also|Pointers|for|Arrays#} |
| ... | ... | @@ -2400,8 +2401,8 @@ const expect = std.testing.expect; |
| 2400 | 2401 | test "null terminated slice" { |
| 2401 | 2402 | const slice: [:0]const u8 = "hello"; |
| 2402 | 2403 | |
| 2403 | | expect(slice.len == 5); |
| 2404 | | expect(slice[5] == 0); |
| 2404 | try expect(slice.len == 5); |
| 2405 | try expect(slice[5] == 0); |
| 2405 | 2406 | } |
| 2406 | 2407 | {#code_end#} |
| 2407 | 2408 | {#see_also|Sentinel-Terminated Pointers|Sentinel-Terminated Arrays#} |
| ... | ... | @@ -2463,12 +2464,12 @@ const expect = @import("std").testing.expect; |
| 2463 | 2464 | test "dot product" { |
| 2464 | 2465 | const v1 = Vec3.init(1.0, 0.0, 0.0); |
| 2465 | 2466 | const v2 = Vec3.init(0.0, 1.0, 0.0); |
| 2466 | | expect(v1.dot(v2) == 0.0); |
| 2467 | try expect(v1.dot(v2) == 0.0); |
| 2467 | 2468 | |
| 2468 | 2469 | // Other than being available to call with dot syntax, struct methods are |
| 2469 | 2470 | // not special. You can reference them as any other declaration inside |
| 2470 | 2471 | // the struct: |
| 2471 | | expect(Vec3.dot(v1, v2) == 0.0); |
| 2472 | try expect(Vec3.dot(v1, v2) == 0.0); |
| 2472 | 2473 | } |
| 2473 | 2474 | |
| 2474 | 2475 | // Structs can have global declarations. |
| ... | ... | @@ -2477,8 +2478,8 @@ const Empty = struct { |
| 2477 | 2478 | pub const PI = 3.14; |
| 2478 | 2479 | }; |
| 2479 | 2480 | test "struct namespaced variable" { |
| 2480 | | expect(Empty.PI == 3.14); |
| 2481 | | expect(@sizeOf(Empty) == 0); |
| 2481 | try expect(Empty.PI == 3.14); |
| 2482 | try expect(@sizeOf(Empty) == 0); |
| 2482 | 2483 | |
| 2483 | 2484 | // you can still instantiate an empty struct |
| 2484 | 2485 | const does_nothing = Empty {}; |
| ... | ... | @@ -2496,7 +2497,7 @@ test "field parent pointer" { |
| 2496 | 2497 | .y = 0.5678, |
| 2497 | 2498 | }; |
| 2498 | 2499 | setYBasedOnX(&point.x, 0.9); |
| 2499 | | expect(point.y == 0.9); |
| 2500 | try expect(point.y == 0.9); |
| 2500 | 2501 | } |
| 2501 | 2502 | |
| 2502 | 2503 | // You can return a struct from a function. This is how we do generics |
| ... | ... | @@ -2518,19 +2519,19 @@ fn LinkedList(comptime T: type) type { |
| 2518 | 2519 | test "linked list" { |
| 2519 | 2520 | // Functions called at compile-time are memoized. This means you can |
| 2520 | 2521 | // do this: |
| 2521 | | expect(LinkedList(i32) == LinkedList(i32)); |
| 2522 | try expect(LinkedList(i32) == LinkedList(i32)); |
| 2522 | 2523 | |
| 2523 | 2524 | var list = LinkedList(i32) { |
| 2524 | 2525 | .first = null, |
| 2525 | 2526 | .last = null, |
| 2526 | 2527 | .len = 0, |
| 2527 | 2528 | }; |
| 2528 | | expect(list.len == 0); |
| 2529 | try expect(list.len == 0); |
| 2529 | 2530 | |
| 2530 | 2531 | // Since types are first class values you can instantiate the type |
| 2531 | 2532 | // by assigning it to a variable: |
| 2532 | 2533 | const ListOfInts = LinkedList(i32); |
| 2533 | | expect(ListOfInts == LinkedList(i32)); |
| 2534 | try expect(ListOfInts == LinkedList(i32)); |
| 2534 | 2535 | |
| 2535 | 2536 | var node = ListOfInts.Node { |
| 2536 | 2537 | .prev = null, |
| ... | ... | @@ -2542,7 +2543,7 @@ test "linked list" { |
| 2542 | 2543 | .last = &node, |
| 2543 | 2544 | .len = 1, |
| 2544 | 2545 | }; |
| 2545 | | expect(list2.first.?.data == 1234); |
| 2546 | try expect(list2.first.?.data == 1234); |
| 2546 | 2547 | } |
| 2547 | 2548 | {#code_end#} |
| 2548 | 2549 | |
| ... | ... | @@ -2615,25 +2616,25 @@ const Divided = packed struct { |
| 2615 | 2616 | }; |
| 2616 | 2617 | |
| 2617 | 2618 | test "@bitCast between packed structs" { |
| 2618 | | doTheTest(); |
| 2619 | | comptime doTheTest(); |
| 2619 | try doTheTest(); |
| 2620 | comptime try doTheTest(); |
| 2620 | 2621 | } |
| 2621 | 2622 | |
| 2622 | | fn doTheTest() void { |
| 2623 | | expect(@sizeOf(Full) == 2); |
| 2624 | | expect(@sizeOf(Divided) == 2); |
| 2623 | fn doTheTest() !void { |
| 2624 | try expect(@sizeOf(Full) == 2); |
| 2625 | try expect(@sizeOf(Divided) == 2); |
| 2625 | 2626 | var full = Full{ .number = 0x1234 }; |
| 2626 | 2627 | var divided = @bitCast(Divided, full); |
| 2627 | 2628 | switch (builtin.endian) { |
| 2628 | 2629 | .Big => { |
| 2629 | | expect(divided.half1 == 0x12); |
| 2630 | | expect(divided.quarter3 == 0x3); |
| 2631 | | expect(divided.quarter4 == 0x4); |
| 2630 | try expect(divided.half1 == 0x12); |
| 2631 | try expect(divided.quarter3 == 0x3); |
| 2632 | try expect(divided.quarter4 == 0x4); |
| 2632 | 2633 | }, |
| 2633 | 2634 | .Little => { |
| 2634 | | expect(divided.half1 == 0x34); |
| 2635 | | expect(divided.quarter3 == 0x2); |
| 2636 | | expect(divided.quarter4 == 0x1); |
| 2635 | try expect(divided.half1 == 0x34); |
| 2636 | try expect(divided.quarter3 == 0x2); |
| 2637 | try expect(divided.quarter4 == 0x1); |
| 2637 | 2638 | }, |
| 2638 | 2639 | } |
| 2639 | 2640 | } |
| ... | ... | @@ -2659,7 +2660,7 @@ var foo = BitField{ |
| 2659 | 2660 | |
| 2660 | 2661 | test "pointer to non-byte-aligned field" { |
| 2661 | 2662 | const ptr = &foo.b; |
| 2662 | | expect(ptr.* == 2); |
| 2663 | try expect(ptr.* == 2); |
| 2663 | 2664 | } |
| 2664 | 2665 | {#code_end#} |
| 2665 | 2666 | <p> |
| ... | ... | @@ -2683,7 +2684,7 @@ var bit_field = BitField{ |
| 2683 | 2684 | }; |
| 2684 | 2685 | |
| 2685 | 2686 | test "pointer to non-bit-aligned field" { |
| 2686 | | expect(bar(&bit_field.b) == 2); |
| 2687 | try expect(bar(&bit_field.b) == 2); |
| 2687 | 2688 | } |
| 2688 | 2689 | |
| 2689 | 2690 | fn bar(x: *const u3) u3 { |
| ... | ... | @@ -2714,8 +2715,8 @@ var bit_field = BitField{ |
| 2714 | 2715 | }; |
| 2715 | 2716 | |
| 2716 | 2717 | test "pointer to non-bit-aligned field" { |
| 2717 | | expect(@ptrToInt(&bit_field.a) == @ptrToInt(&bit_field.b)); |
| 2718 | | expect(@ptrToInt(&bit_field.a) == @ptrToInt(&bit_field.c)); |
| 2718 | try expect(@ptrToInt(&bit_field.a) == @ptrToInt(&bit_field.b)); |
| 2719 | try expect(@ptrToInt(&bit_field.a) == @ptrToInt(&bit_field.c)); |
| 2719 | 2720 | } |
| 2720 | 2721 | {#code_end#} |
| 2721 | 2722 | <p> |
| ... | ... | @@ -2733,13 +2734,13 @@ const BitField = packed struct { |
| 2733 | 2734 | |
| 2734 | 2735 | test "pointer to non-bit-aligned field" { |
| 2735 | 2736 | comptime { |
| 2736 | | expect(@bitOffsetOf(BitField, "a") == 0); |
| 2737 | | expect(@bitOffsetOf(BitField, "b") == 3); |
| 2738 | | expect(@bitOffsetOf(BitField, "c") == 6); |
| 2737 | try expect(@bitOffsetOf(BitField, "a") == 0); |
| 2738 | try expect(@bitOffsetOf(BitField, "b") == 3); |
| 2739 | try expect(@bitOffsetOf(BitField, "c") == 6); |
| 2739 | 2740 | |
| 2740 | | expect(@byteOffsetOf(BitField, "a") == 0); |
| 2741 | | expect(@byteOffsetOf(BitField, "b") == 0); |
| 2742 | | expect(@byteOffsetOf(BitField, "c") == 0); |
| 2741 | try expect(@byteOffsetOf(BitField, "a") == 0); |
| 2742 | try expect(@byteOffsetOf(BitField, "b") == 0); |
| 2743 | try expect(@byteOffsetOf(BitField, "c") == 0); |
| 2743 | 2744 | } |
| 2744 | 2745 | } |
| 2745 | 2746 | {#code_end#} |
| ... | ... | @@ -2776,9 +2777,9 @@ test "aligned struct fields" { |
| 2776 | 2777 | }; |
| 2777 | 2778 | var foo = S{ .a = 1, .b = 2 }; |
| 2778 | 2779 | |
| 2779 | | expectEqual(64, @alignOf(S)); |
| 2780 | | expectEqual(*align(2) u32, @TypeOf(&foo.a)); |
| 2781 | | expectEqual(*align(64) u32, @TypeOf(&foo.b)); |
| 2780 | try expectEqual(64, @alignOf(S)); |
| 2781 | try expectEqual(*align(2) u32, @TypeOf(&foo.a)); |
| 2782 | try expectEqual(*align(64) u32, @TypeOf(&foo.b)); |
| 2782 | 2783 | } |
| 2783 | 2784 | {#code_end#} |
| 2784 | 2785 | <p> |
| ... | ... | @@ -2834,8 +2835,8 @@ test "anonymous struct literal" { |
| 2834 | 2835 | .x = 13, |
| 2835 | 2836 | .y = 67, |
| 2836 | 2837 | }; |
| 2837 | | expect(pt.x == 13); |
| 2838 | | expect(pt.y == 67); |
| 2838 | try expect(pt.x == 13); |
| 2839 | try expect(pt.y == 67); |
| 2839 | 2840 | } |
| 2840 | 2841 | {#code_end#} |
| 2841 | 2842 | <p> |
| ... | ... | @@ -2847,7 +2848,7 @@ const std = @import("std"); |
| 2847 | 2848 | const expect = std.testing.expect; |
| 2848 | 2849 | |
| 2849 | 2850 | test "fully anonymous struct" { |
| 2850 | | dump(.{ |
| 2851 | try dump(.{ |
| 2851 | 2852 | .int = @as(u32, 1234), |
| 2852 | 2853 | .float = @as(f64, 12.34), |
| 2853 | 2854 | .b = true, |
| ... | ... | @@ -2855,12 +2856,12 @@ test "fully anonymous struct" { |
| 2855 | 2856 | }); |
| 2856 | 2857 | } |
| 2857 | 2858 | |
| 2858 | | fn dump(args: anytype) void { |
| 2859 | | expect(args.int == 1234); |
| 2860 | | expect(args.float == 12.34); |
| 2861 | | expect(args.b); |
| 2862 | | expect(args.s[0] == 'h'); |
| 2863 | | expect(args.s[1] == 'i'); |
| 2859 | fn dump(args: anytype) !void { |
| 2860 | try expect(args.int == 1234); |
| 2861 | try expect(args.float == 12.34); |
| 2862 | try expect(args.b); |
| 2863 | try expect(args.s[0] == 'h'); |
| 2864 | try expect(args.s[1] == 'i'); |
| 2864 | 2865 | } |
| 2865 | 2866 | {#code_end#} |
| 2866 | 2867 | <p> |
| ... | ... | @@ -2884,14 +2885,14 @@ test "tuple" { |
| 2884 | 2885 | true, |
| 2885 | 2886 | "hi", |
| 2886 | 2887 | } ++ .{false} ** 2; |
| 2887 | | expect(values[0] == 1234); |
| 2888 | | expect(values[4] == false); |
| 2888 | try expect(values[0] == 1234); |
| 2889 | try expect(values[4] == false); |
| 2889 | 2890 | inline for (values) |v, i| { |
| 2890 | 2891 | if (i != 2) continue; |
| 2891 | | expect(v); |
| 2892 | try expect(v); |
| 2892 | 2893 | } |
| 2893 | | expect(values.len == 6); |
| 2894 | | expect(values.@"3"[0] == 'h'); |
| 2894 | try expect(values.len == 6); |
| 2895 | try expect(values.@"3"[0] == 'h'); |
| 2895 | 2896 | } |
| 2896 | 2897 | {#code_end#} |
| 2897 | 2898 | {#header_close#} |
| ... | ... | @@ -2922,9 +2923,9 @@ const Value = enum(u2) { |
| 2922 | 2923 | // Now you can cast between u2 and Value. |
| 2923 | 2924 | // The ordinal value starts from 0, counting up for each member. |
| 2924 | 2925 | test "enum ordinal value" { |
| 2925 | | expect(@enumToInt(Value.zero) == 0); |
| 2926 | | expect(@enumToInt(Value.one) == 1); |
| 2927 | | expect(@enumToInt(Value.two) == 2); |
| 2926 | try expect(@enumToInt(Value.zero) == 0); |
| 2927 | try expect(@enumToInt(Value.one) == 1); |
| 2928 | try expect(@enumToInt(Value.two) == 2); |
| 2928 | 2929 | } |
| 2929 | 2930 | |
| 2930 | 2931 | // You can override the ordinal value for an enum. |
| ... | ... | @@ -2934,9 +2935,9 @@ const Value2 = enum(u32) { |
| 2934 | 2935 | million = 1000000, |
| 2935 | 2936 | }; |
| 2936 | 2937 | test "set enum ordinal value" { |
| 2937 | | expect(@enumToInt(Value2.hundred) == 100); |
| 2938 | | expect(@enumToInt(Value2.thousand) == 1000); |
| 2939 | | expect(@enumToInt(Value2.million) == 1000000); |
| 2938 | try expect(@enumToInt(Value2.hundred) == 100); |
| 2939 | try expect(@enumToInt(Value2.thousand) == 1000); |
| 2940 | try expect(@enumToInt(Value2.million) == 1000000); |
| 2940 | 2941 | } |
| 2941 | 2942 | |
| 2942 | 2943 | // Enums can have methods, the same as structs and unions. |
| ... | ... | @@ -2954,7 +2955,7 @@ const Suit = enum { |
| 2954 | 2955 | }; |
| 2955 | 2956 | test "enum method" { |
| 2956 | 2957 | const p = Suit.spades; |
| 2957 | | expect(!p.isClubs()); |
| 2958 | try expect(!p.isClubs()); |
| 2958 | 2959 | } |
| 2959 | 2960 | |
| 2960 | 2961 | // An enum variant of different types can be switched upon. |
| ... | ... | @@ -2970,7 +2971,7 @@ test "enum variant switch" { |
| 2970 | 2971 | Foo.number => "this is a number", |
| 2971 | 2972 | Foo.none => "this is a none", |
| 2972 | 2973 | }; |
| 2973 | | expect(mem.eql(u8, what_is_it, "this is a number")); |
| 2974 | try expect(mem.eql(u8, what_is_it, "this is a number")); |
| 2974 | 2975 | } |
| 2975 | 2976 | |
| 2976 | 2977 | // @typeInfo can be used to access the integer tag type of an enum. |
| ... | ... | @@ -2981,18 +2982,18 @@ const Small = enum { |
| 2981 | 2982 | four, |
| 2982 | 2983 | }; |
| 2983 | 2984 | test "std.meta.Tag" { |
| 2984 | | expect(@typeInfo(Small).Enum.tag_type == u2); |
| 2985 | try expect(@typeInfo(Small).Enum.tag_type == u2); |
| 2985 | 2986 | } |
| 2986 | 2987 | |
| 2987 | 2988 | // @typeInfo tells us the field count and the fields names: |
| 2988 | 2989 | test "@typeInfo" { |
| 2989 | | expect(@typeInfo(Small).Enum.fields.len == 4); |
| 2990 | | expect(mem.eql(u8, @typeInfo(Small).Enum.fields[1].name, "two")); |
| 2990 | try expect(@typeInfo(Small).Enum.fields.len == 4); |
| 2991 | try expect(mem.eql(u8, @typeInfo(Small).Enum.fields[1].name, "two")); |
| 2991 | 2992 | } |
| 2992 | 2993 | |
| 2993 | 2994 | // @tagName gives a []const u8 representation of an enum value: |
| 2994 | 2995 | test "@tagName" { |
| 2995 | | expect(mem.eql(u8, @tagName(Small.three), "three")); |
| 2996 | try expect(mem.eql(u8, @tagName(Small.three), "three")); |
| 2996 | 2997 | } |
| 2997 | 2998 | {#code_end#} |
| 2998 | 2999 | {#see_also|@typeInfo|@tagName|@sizeOf#} |
| ... | ... | @@ -3027,7 +3028,7 @@ test "packed enum" { |
| 3027 | 3028 | two, |
| 3028 | 3029 | three, |
| 3029 | 3030 | }; |
| 3030 | | std.testing.expect(@sizeOf(Number) == @sizeOf(u8)); |
| 3031 | try std.testing.expect(@sizeOf(Number) == @sizeOf(u8)); |
| 3031 | 3032 | } |
| 3032 | 3033 | {#code_end#} |
| 3033 | 3034 | <p>This makes the enum eligible to be in a {#link|packed struct#}.</p> |
| ... | ... | @@ -3050,7 +3051,7 @@ const Color = enum { |
| 3050 | 3051 | test "enum literals" { |
| 3051 | 3052 | const color1: Color = .auto; |
| 3052 | 3053 | const color2 = Color.auto; |
| 3053 | | expect(color1 == color2); |
| 3054 | try expect(color1 == color2); |
| 3054 | 3055 | } |
| 3055 | 3056 | |
| 3056 | 3057 | test "switch using enum literals" { |
| ... | ... | @@ -3060,7 +3061,7 @@ test "switch using enum literals" { |
| 3060 | 3061 | .on => true, |
| 3061 | 3062 | .off => false, |
| 3062 | 3063 | }; |
| 3063 | | expect(result); |
| 3064 | try expect(result); |
| 3064 | 3065 | } |
| 3065 | 3066 | {#code_end#} |
| 3066 | 3067 | {#header_close#} |
| ... | ... | @@ -3096,12 +3097,12 @@ test "switch on non-exhaustive enum" { |
| 3096 | 3097 | .three => false, |
| 3097 | 3098 | _ => false, |
| 3098 | 3099 | }; |
| 3099 | | expect(result); |
| 3100 | try expect(result); |
| 3100 | 3101 | const is_one = switch (number) { |
| 3101 | 3102 | .one => true, |
| 3102 | 3103 | else => false, |
| 3103 | 3104 | }; |
| 3104 | | expect(is_one); |
| 3105 | try expect(is_one); |
| 3105 | 3106 | } |
| 3106 | 3107 | {#code_end#} |
| 3107 | 3108 | {#header_close#} |
| ... | ... | @@ -3141,9 +3142,9 @@ const Payload = union { |
| 3141 | 3142 | }; |
| 3142 | 3143 | test "simple union" { |
| 3143 | 3144 | var payload = Payload{ .int = 1234 }; |
| 3144 | | expect(payload.int == 1234); |
| 3145 | try expect(payload.int == 1234); |
| 3145 | 3146 | payload = Payload{ .float = 12.34 }; |
| 3146 | | expect(payload.float == 12.34); |
| 3147 | try expect(payload.float == 12.34); |
| 3147 | 3148 | } |
| 3148 | 3149 | {#code_end#} |
| 3149 | 3150 | <p> |
| ... | ... | @@ -3174,24 +3175,24 @@ const ComplexType = union(ComplexTypeTag) { |
| 3174 | 3175 | |
| 3175 | 3176 | test "switch on tagged union" { |
| 3176 | 3177 | const c = ComplexType{ .ok = 42 }; |
| 3177 | | expect(@as(ComplexTypeTag, c) == ComplexTypeTag.ok); |
| 3178 | try expect(@as(ComplexTypeTag, c) == ComplexTypeTag.ok); |
| 3178 | 3179 | |
| 3179 | 3180 | switch (c) { |
| 3180 | | ComplexTypeTag.ok => |value| expect(value == 42), |
| 3181 | ComplexTypeTag.ok => |value| try expect(value == 42), |
| 3181 | 3182 | ComplexTypeTag.not_ok => unreachable, |
| 3182 | 3183 | } |
| 3183 | 3184 | } |
| 3184 | 3185 | |
| 3185 | 3186 | test "get tag type" { |
| 3186 | | expect(std.meta.Tag(ComplexType) == ComplexTypeTag); |
| 3187 | try expect(std.meta.Tag(ComplexType) == ComplexTypeTag); |
| 3187 | 3188 | } |
| 3188 | 3189 | |
| 3189 | 3190 | test "coerce to enum" { |
| 3190 | 3191 | const c1 = ComplexType{ .ok = 42 }; |
| 3191 | 3192 | const c2 = ComplexType.not_ok; |
| 3192 | 3193 | |
| 3193 | | expect(c1 == .ok); |
| 3194 | | expect(c2 == .not_ok); |
| 3194 | try expect(c1 == .ok); |
| 3195 | try expect(c2 == .not_ok); |
| 3195 | 3196 | } |
| 3196 | 3197 | {#code_end#} |
| 3197 | 3198 | <p>In order to modify the payload of a tagged union in a switch expression, |
| ... | ... | @@ -3212,14 +3213,14 @@ const ComplexType = union(ComplexTypeTag) { |
| 3212 | 3213 | |
| 3213 | 3214 | test "modify tagged union in switch" { |
| 3214 | 3215 | var c = ComplexType{ .ok = 42 }; |
| 3215 | | expect(@as(ComplexTypeTag, c) == ComplexTypeTag.ok); |
| 3216 | try expect(@as(ComplexTypeTag, c) == ComplexTypeTag.ok); |
| 3216 | 3217 | |
| 3217 | 3218 | switch (c) { |
| 3218 | 3219 | ComplexTypeTag.ok => |*value| value.* += 1, |
| 3219 | 3220 | ComplexTypeTag.not_ok => unreachable, |
| 3220 | 3221 | } |
| 3221 | 3222 | |
| 3222 | | expect(c.ok == 43); |
| 3223 | try expect(c.ok == 43); |
| 3223 | 3224 | } |
| 3224 | 3225 | {#code_end#} |
| 3225 | 3226 | <p> |
| ... | ... | @@ -3250,8 +3251,8 @@ test "union method" { |
| 3250 | 3251 | var v1 = Variant{ .int = 1 }; |
| 3251 | 3252 | var v2 = Variant{ .boolean = false }; |
| 3252 | 3253 | |
| 3253 | | expect(v1.truthy()); |
| 3254 | | expect(!v2.truthy()); |
| 3254 | try expect(v1.truthy()); |
| 3255 | try expect(!v2.truthy()); |
| 3255 | 3256 | } |
| 3256 | 3257 | {#code_end#} |
| 3257 | 3258 | <p> |
| ... | ... | @@ -3268,7 +3269,7 @@ const Small2 = union(enum) { |
| 3268 | 3269 | c: u8, |
| 3269 | 3270 | }; |
| 3270 | 3271 | test "@tagName" { |
| 3271 | | expect(std.mem.eql(u8, @tagName(Small2.a), "a")); |
| 3272 | try expect(std.mem.eql(u8, @tagName(Small2.a), "a")); |
| 3272 | 3273 | } |
| 3273 | 3274 | {#code_end#} |
| 3274 | 3275 | {#header_close#} |
| ... | ... | @@ -3301,8 +3302,8 @@ const Number = union { |
| 3301 | 3302 | test "anonymous union literal syntax" { |
| 3302 | 3303 | var i: Number = .{.int = 42}; |
| 3303 | 3304 | var f = makeNumber(); |
| 3304 | | expect(i.int == 42); |
| 3305 | | expect(f.float == 12.34); |
| 3305 | try expect(i.int == 42); |
| 3306 | try expect(f.float == 12.34); |
| 3306 | 3307 | } |
| 3307 | 3308 | |
| 3308 | 3309 | fn makeNumber() Number { |
| ... | ... | @@ -3364,8 +3365,8 @@ test "labeled break from labeled block expression" { |
| 3364 | 3365 | y += 1; |
| 3365 | 3366 | break :blk y; |
| 3366 | 3367 | }; |
| 3367 | | expect(x == 124); |
| 3368 | | expect(y == 124); |
| 3368 | try expect(x == 124); |
| 3369 | try expect(y == 124); |
| 3369 | 3370 | } |
| 3370 | 3371 | {#code_end#} |
| 3371 | 3372 | <p>Here, {#syntax#}blk{#endsyntax#} can be any name.</p> |
| ... | ... | @@ -3443,7 +3444,7 @@ test "switch simple" { |
| 3443 | 3444 | else => 9, |
| 3444 | 3445 | }; |
| 3445 | 3446 | |
| 3446 | | expect(b == 1); |
| 3447 | try expect(b == 1); |
| 3447 | 3448 | } |
| 3448 | 3449 | |
| 3449 | 3450 | // Switch expressions can be used outside a function: |
| ... | ... | @@ -3506,8 +3507,8 @@ test "switch on tagged union" { |
| 3506 | 3507 | Item.d => 8, |
| 3507 | 3508 | }; |
| 3508 | 3509 | |
| 3509 | | expect(b == 6); |
| 3510 | | expect(a.c.x == 2); |
| 3510 | try expect(b == 6); |
| 3511 | try expect(a.c.x == 2); |
| 3511 | 3512 | } |
| 3512 | 3513 | {#code_end#} |
| 3513 | 3514 | {#see_also|comptime|enum|@compileError|Compile Variables#} |
| ... | ... | @@ -3556,7 +3557,7 @@ test "enum literals with switch" { |
| 3556 | 3557 | .on => false, |
| 3557 | 3558 | .off => true, |
| 3558 | 3559 | }; |
| 3559 | | expect(result); |
| 3560 | try expect(result); |
| 3560 | 3561 | } |
| 3561 | 3562 | {#code_end#} |
| 3562 | 3563 | {#header_close#} |
| ... | ... | @@ -3575,7 +3576,7 @@ test "while basic" { |
| 3575 | 3576 | while (i < 10) { |
| 3576 | 3577 | i += 1; |
| 3577 | 3578 | } |
| 3578 | | expect(i == 10); |
| 3579 | try expect(i == 10); |
| 3579 | 3580 | } |
| 3580 | 3581 | {#code_end#} |
| 3581 | 3582 | <p> |
| ... | ... | @@ -3591,7 +3592,7 @@ test "while break" { |
| 3591 | 3592 | break; |
| 3592 | 3593 | i += 1; |
| 3593 | 3594 | } |
| 3594 | | expect(i == 10); |
| 3595 | try expect(i == 10); |
| 3595 | 3596 | } |
| 3596 | 3597 | {#code_end#} |
| 3597 | 3598 | <p> |
| ... | ... | @@ -3608,7 +3609,7 @@ test "while continue" { |
| 3608 | 3609 | continue; |
| 3609 | 3610 | break; |
| 3610 | 3611 | } |
| 3611 | | expect(i == 10); |
| 3612 | try expect(i == 10); |
| 3612 | 3613 | } |
| 3613 | 3614 | {#code_end#} |
| 3614 | 3615 | <p> |
| ... | ... | @@ -3621,7 +3622,7 @@ const expect = @import("std").testing.expect; |
| 3621 | 3622 | test "while loop continue expression" { |
| 3622 | 3623 | var i: usize = 0; |
| 3623 | 3624 | while (i < 10) : (i += 1) {} |
| 3624 | | expect(i == 10); |
| 3625 | try expect(i == 10); |
| 3625 | 3626 | } |
| 3626 | 3627 | |
| 3627 | 3628 | test "while loop continue expression, more complicated" { |
| ... | ... | @@ -3629,7 +3630,7 @@ test "while loop continue expression, more complicated" { |
| 3629 | 3630 | var j: usize = 1; |
| 3630 | 3631 | while (i * j < 2000) : ({ i *= 2; j *= 3; }) { |
| 3631 | 3632 | const my_ij = i * j; |
| 3632 | | expect(my_ij < 2000); |
| 3633 | try expect(my_ij < 2000); |
| 3633 | 3634 | } |
| 3634 | 3635 | } |
| 3635 | 3636 | {#code_end#} |
| ... | ... | @@ -3648,8 +3649,8 @@ test "while loop continue expression, more complicated" { |
| 3648 | 3649 | const expect = @import("std").testing.expect; |
| 3649 | 3650 | |
| 3650 | 3651 | test "while else" { |
| 3651 | | expect(rangeHasNumber(0, 10, 5)); |
| 3652 | | expect(!rangeHasNumber(0, 10, 15)); |
| 3652 | try expect(rangeHasNumber(0, 10, 5)); |
| 3653 | try expect(!rangeHasNumber(0, 10, 15)); |
| 3653 | 3654 | } |
| 3654 | 3655 | |
| 3655 | 3656 | fn rangeHasNumber(begin: usize, end: usize, number: usize) bool { |
| ... | ... | @@ -3706,14 +3707,14 @@ test "while null capture" { |
| 3706 | 3707 | while (eventuallyNullSequence()) |value| { |
| 3707 | 3708 | sum1 += value; |
| 3708 | 3709 | } |
| 3709 | | expect(sum1 == 3); |
| 3710 | try expect(sum1 == 3); |
| 3710 | 3711 | |
| 3711 | 3712 | var sum2: u32 = 0; |
| 3712 | 3713 | numbers_left = 3; |
| 3713 | 3714 | while (eventuallyNullSequence()) |value| { |
| 3714 | 3715 | sum2 += value; |
| 3715 | 3716 | } else { |
| 3716 | | expect(sum2 == 3); |
| 3717 | try expect(sum2 == 3); |
| 3717 | 3718 | } |
| 3718 | 3719 | } |
| 3719 | 3720 | |
| ... | ... | @@ -3748,7 +3749,7 @@ test "while error union capture" { |
| 3748 | 3749 | while (eventuallyErrorSequence()) |value| { |
| 3749 | 3750 | sum1 += value; |
| 3750 | 3751 | } else |err| { |
| 3751 | | expect(err == error.ReachedZero); |
| 3752 | try expect(err == error.ReachedZero); |
| 3752 | 3753 | } |
| 3753 | 3754 | } |
| 3754 | 3755 | |
| ... | ... | @@ -3784,7 +3785,7 @@ test "inline while loop" { |
| 3784 | 3785 | }; |
| 3785 | 3786 | sum += typeNameLength(T); |
| 3786 | 3787 | } |
| 3787 | | expect(sum == 9); |
| 3788 | try expect(sum == 9); |
| 3788 | 3789 | } |
| 3789 | 3790 | |
| 3790 | 3791 | fn typeNameLength(comptime T: type) usize { |
| ... | ... | @@ -3819,22 +3820,22 @@ test "for basics" { |
| 3819 | 3820 | } |
| 3820 | 3821 | sum += value; |
| 3821 | 3822 | } |
| 3822 | | expect(sum == 16); |
| 3823 | try expect(sum == 16); |
| 3823 | 3824 | |
| 3824 | 3825 | // To iterate over a portion of a slice, reslice. |
| 3825 | 3826 | for (items[0..1]) |value| { |
| 3826 | 3827 | sum += value; |
| 3827 | 3828 | } |
| 3828 | | expect(sum == 20); |
| 3829 | try expect(sum == 20); |
| 3829 | 3830 | |
| 3830 | 3831 | // To access the index of iteration, specify a second capture value. |
| 3831 | 3832 | // This is zero-indexed. |
| 3832 | 3833 | var sum2: i32 = 0; |
| 3833 | 3834 | for (items) |value, i| { |
| 3834 | | expect(@TypeOf(i) == usize); |
| 3835 | try expect(@TypeOf(i) == usize); |
| 3835 | 3836 | sum2 += @intCast(i32, i); |
| 3836 | 3837 | } |
| 3837 | | expect(sum2 == 10); |
| 3838 | try expect(sum2 == 10); |
| 3838 | 3839 | } |
| 3839 | 3840 | |
| 3840 | 3841 | test "for reference" { |
| ... | ... | @@ -3846,9 +3847,9 @@ test "for reference" { |
| 3846 | 3847 | value.* += 1; |
| 3847 | 3848 | } |
| 3848 | 3849 | |
| 3849 | | expect(items[0] == 4); |
| 3850 | | expect(items[1] == 5); |
| 3851 | | expect(items[2] == 3); |
| 3850 | try expect(items[0] == 4); |
| 3851 | try expect(items[1] == 5); |
| 3852 | try expect(items[2] == 3); |
| 3852 | 3853 | } |
| 3853 | 3854 | |
| 3854 | 3855 | test "for else" { |
| ... | ... | @@ -3863,10 +3864,10 @@ test "for else" { |
| 3863 | 3864 | sum += value.?; |
| 3864 | 3865 | } |
| 3865 | 3866 | } else blk: { |
| 3866 | | expect(sum == 12); |
| 3867 | try expect(sum == 12); |
| 3867 | 3868 | break :blk sum; |
| 3868 | 3869 | }; |
| 3869 | | expect(result == 12); |
| 3870 | try expect(result == 12); |
| 3870 | 3871 | } |
| 3871 | 3872 | {#code_end#} |
| 3872 | 3873 | {#header_open|Labeled for#} |
| ... | ... | @@ -3884,7 +3885,7 @@ test "nested break" { |
| 3884 | 3885 | break :outer; |
| 3885 | 3886 | } |
| 3886 | 3887 | } |
| 3887 | | expect(count == 1); |
| 3888 | try expect(count == 1); |
| 3888 | 3889 | } |
| 3889 | 3890 | |
| 3890 | 3891 | test "nested continue" { |
| ... | ... | @@ -3896,7 +3897,7 @@ test "nested continue" { |
| 3896 | 3897 | } |
| 3897 | 3898 | } |
| 3898 | 3899 | |
| 3899 | | expect(count == 8); |
| 3900 | try expect(count == 8); |
| 3900 | 3901 | } |
| 3901 | 3902 | {#code_end#} |
| 3902 | 3903 | {#header_close#} |
| ... | ... | @@ -3923,7 +3924,7 @@ test "inline for loop" { |
| 3923 | 3924 | }; |
| 3924 | 3925 | sum += typeNameLength(T); |
| 3925 | 3926 | } |
| 3926 | | expect(sum == 9); |
| 3927 | try expect(sum == 9); |
| 3927 | 3928 | } |
| 3928 | 3929 | |
| 3929 | 3930 | fn typeNameLength(comptime T: type) usize { |
| ... | ... | @@ -3956,7 +3957,7 @@ test "if expression" { |
| 3956 | 3957 | const a: u32 = 5; |
| 3957 | 3958 | const b: u32 = 4; |
| 3958 | 3959 | const result = if (a != b) 47 else 3089; |
| 3959 | | expect(result == 47); |
| 3960 | try expect(result == 47); |
| 3960 | 3961 | } |
| 3961 | 3962 | |
| 3962 | 3963 | test "if boolean" { |
| ... | ... | @@ -3964,7 +3965,7 @@ test "if boolean" { |
| 3964 | 3965 | const a: u32 = 5; |
| 3965 | 3966 | const b: u32 = 4; |
| 3966 | 3967 | if (a != b) { |
| 3967 | | expect(true); |
| 3968 | try expect(true); |
| 3968 | 3969 | } else if (a == 9) { |
| 3969 | 3970 | unreachable; |
| 3970 | 3971 | } else { |
| ... | ... | @@ -3977,7 +3978,7 @@ test "if optional" { |
| 3977 | 3978 | |
| 3978 | 3979 | const a: ?u32 = 0; |
| 3979 | 3980 | if (a) |value| { |
| 3980 | | expect(value == 0); |
| 3981 | try expect(value == 0); |
| 3981 | 3982 | } else { |
| 3982 | 3983 | unreachable; |
| 3983 | 3984 | } |
| ... | ... | @@ -3986,17 +3987,17 @@ test "if optional" { |
| 3986 | 3987 | if (b) |value| { |
| 3987 | 3988 | unreachable; |
| 3988 | 3989 | } else { |
| 3989 | | expect(true); |
| 3990 | try expect(true); |
| 3990 | 3991 | } |
| 3991 | 3992 | |
| 3992 | 3993 | // The else is not required. |
| 3993 | 3994 | if (a) |value| { |
| 3994 | | expect(value == 0); |
| 3995 | try expect(value == 0); |
| 3995 | 3996 | } |
| 3996 | 3997 | |
| 3997 | 3998 | // To test against null only, use the binary equality operator. |
| 3998 | 3999 | if (b == null) { |
| 3999 | | expect(true); |
| 4000 | try expect(true); |
| 4000 | 4001 | } |
| 4001 | 4002 | |
| 4002 | 4003 | // Access the value by reference using a pointer capture. |
| ... | ... | @@ -4006,7 +4007,7 @@ test "if optional" { |
| 4006 | 4007 | } |
| 4007 | 4008 | |
| 4008 | 4009 | if (c) |value| { |
| 4009 | | expect(value == 2); |
| 4010 | try expect(value == 2); |
| 4010 | 4011 | } else { |
| 4011 | 4012 | unreachable; |
| 4012 | 4013 | } |
| ... | ... | @@ -4018,7 +4019,7 @@ test "if error union" { |
| 4018 | 4019 | |
| 4019 | 4020 | const a: anyerror!u32 = 0; |
| 4020 | 4021 | if (a) |value| { |
| 4021 | | expect(value == 0); |
| 4022 | try expect(value == 0); |
| 4022 | 4023 | } else |err| { |
| 4023 | 4024 | unreachable; |
| 4024 | 4025 | } |
| ... | ... | @@ -4027,17 +4028,17 @@ test "if error union" { |
| 4027 | 4028 | if (b) |value| { |
| 4028 | 4029 | unreachable; |
| 4029 | 4030 | } else |err| { |
| 4030 | | expect(err == error.BadValue); |
| 4031 | try expect(err == error.BadValue); |
| 4031 | 4032 | } |
| 4032 | 4033 | |
| 4033 | 4034 | // The else and |err| capture is strictly required. |
| 4034 | 4035 | if (a) |value| { |
| 4035 | | expect(value == 0); |
| 4036 | try expect(value == 0); |
| 4036 | 4037 | } else |_| {} |
| 4037 | 4038 | |
| 4038 | 4039 | // To check only the error value, use an empty block expression. |
| 4039 | 4040 | if (b) |_| {} else |err| { |
| 4040 | | expect(err == error.BadValue); |
| 4041 | try expect(err == error.BadValue); |
| 4041 | 4042 | } |
| 4042 | 4043 | |
| 4043 | 4044 | // Access the value by reference using a pointer capture. |
| ... | ... | @@ -4049,7 +4050,7 @@ test "if error union" { |
| 4049 | 4050 | } |
| 4050 | 4051 | |
| 4051 | 4052 | if (c) |value| { |
| 4052 | | expect(value == 9); |
| 4053 | try expect(value == 9); |
| 4053 | 4054 | } else |err| { |
| 4054 | 4055 | unreachable; |
| 4055 | 4056 | } |
| ... | ... | @@ -4061,14 +4062,14 @@ test "if error union with optional" { |
| 4061 | 4062 | |
| 4062 | 4063 | const a: anyerror!?u32 = 0; |
| 4063 | 4064 | if (a) |optional_value| { |
| 4064 | | expect(optional_value.? == 0); |
| 4065 | try expect(optional_value.? == 0); |
| 4065 | 4066 | } else |err| { |
| 4066 | 4067 | unreachable; |
| 4067 | 4068 | } |
| 4068 | 4069 | |
| 4069 | 4070 | const b: anyerror!?u32 = null; |
| 4070 | 4071 | if (b) |optional_value| { |
| 4071 | | expect(optional_value == null); |
| 4072 | try expect(optional_value == null); |
| 4072 | 4073 | } else |err| { |
| 4073 | 4074 | unreachable; |
| 4074 | 4075 | } |
| ... | ... | @@ -4077,7 +4078,7 @@ test "if error union with optional" { |
| 4077 | 4078 | if (c) |optional_value| { |
| 4078 | 4079 | unreachable; |
| 4079 | 4080 | } else |err| { |
| 4080 | | expect(err == error.BadValue); |
| 4081 | try expect(err == error.BadValue); |
| 4081 | 4082 | } |
| 4082 | 4083 | |
| 4083 | 4084 | // Access the value by reference by using a pointer capture each time. |
| ... | ... | @@ -4091,7 +4092,7 @@ test "if error union with optional" { |
| 4091 | 4092 | } |
| 4092 | 4093 | |
| 4093 | 4094 | if (d) |optional_value| { |
| 4094 | | expect(optional_value.? == 9); |
| 4095 | try expect(optional_value.? == 9); |
| 4095 | 4096 | } else |err| { |
| 4096 | 4097 | unreachable; |
| 4097 | 4098 | } |
| ... | ... | @@ -4106,21 +4107,21 @@ const expect = std.testing.expect; |
| 4106 | 4107 | const print = std.debug.print; |
| 4107 | 4108 | |
| 4108 | 4109 | // defer will execute an expression at the end of the current scope. |
| 4109 | | fn deferExample() usize { |
| 4110 | fn deferExample() !usize { |
| 4110 | 4111 | var a: usize = 1; |
| 4111 | 4112 | |
| 4112 | 4113 | { |
| 4113 | 4114 | defer a = 2; |
| 4114 | 4115 | a = 1; |
| 4115 | 4116 | } |
| 4116 | | expect(a == 2); |
| 4117 | try expect(a == 2); |
| 4117 | 4118 | |
| 4118 | 4119 | a = 5; |
| 4119 | 4120 | return a; |
| 4120 | 4121 | } |
| 4121 | 4122 | |
| 4122 | 4123 | test "defer basics" { |
| 4123 | | expect(deferExample() == 5); |
| 4124 | try expect((try deferExample()) == 5); |
| 4124 | 4125 | } |
| 4125 | 4126 | |
| 4126 | 4127 | // If multiple defer statements are specified, they will be executed in |
| ... | ... | @@ -4258,7 +4259,7 @@ pub extern "kernel32" fn ExitProcess(exit_code: c_uint) callconv(if (@import("bu |
| 4258 | 4259 | |
| 4259 | 4260 | test "foo" { |
| 4260 | 4261 | const value = bar() catch ExitProcess(1); |
| 4261 | | expect(value == 1234); |
| 4262 | try expect(value == 1234); |
| 4262 | 4263 | } |
| 4263 | 4264 | |
| 4264 | 4265 | fn bar() anyerror!u32 { |
| ... | ... | @@ -4321,17 +4322,17 @@ fn do_op(fn_call: call2_op, op1: i8, op2: i8) i8 { |
| 4321 | 4322 | } |
| 4322 | 4323 | |
| 4323 | 4324 | test "function" { |
| 4324 | | expect(do_op(add, 5, 6) == 11); |
| 4325 | | expect(do_op(sub2, 5, 6) == -1); |
| 4325 | try expect(do_op(add, 5, 6) == 11); |
| 4326 | try expect(do_op(sub2, 5, 6) == -1); |
| 4326 | 4327 | } |
| 4327 | 4328 | {#code_end#} |
| 4328 | 4329 | <p>Function values are like pointers:</p> |
| 4329 | 4330 | {#code_begin|obj#} |
| 4330 | | const expect = @import("std").testing.expect; |
| 4331 | const assert = @import("std").debug.assert; |
| 4331 | 4332 | |
| 4332 | 4333 | comptime { |
| 4333 | | expect(@TypeOf(foo) == fn()void); |
| 4334 | | expect(@sizeOf(fn()void) == @sizeOf(?fn()void)); |
| 4334 | assert(@TypeOf(foo) == fn()void); |
| 4335 | assert(@sizeOf(fn()void) == @sizeOf(?fn()void)); |
| 4335 | 4336 | } |
| 4336 | 4337 | |
| 4337 | 4338 | fn foo() void { } |
| ... | ... | @@ -4366,7 +4367,7 @@ fn foo(point: Point) i32 { |
| 4366 | 4367 | const expect = @import("std").testing.expect; |
| 4367 | 4368 | |
| 4368 | 4369 | test "pass struct to function" { |
| 4369 | | expect(foo(Point{ .x = 1, .y = 2 }) == 3); |
| 4370 | try expect(foo(Point{ .x = 1, .y = 2 }) == 3); |
| 4370 | 4371 | } |
| 4371 | 4372 | {#code_end#} |
| 4372 | 4373 | <p> |
| ... | ... | @@ -4387,11 +4388,11 @@ fn addFortyTwo(x: anytype) @TypeOf(x) { |
| 4387 | 4388 | } |
| 4388 | 4389 | |
| 4389 | 4390 | test "fn type inference" { |
| 4390 | | expect(addFortyTwo(1) == 43); |
| 4391 | | expect(@TypeOf(addFortyTwo(1)) == comptime_int); |
| 4391 | try expect(addFortyTwo(1) == 43); |
| 4392 | try expect(@TypeOf(addFortyTwo(1)) == comptime_int); |
| 4392 | 4393 | var y: i64 = 2; |
| 4393 | | expect(addFortyTwo(y) == 44); |
| 4394 | | expect(@TypeOf(addFortyTwo(y)) == i64); |
| 4394 | try expect(addFortyTwo(y) == 44); |
| 4395 | try expect(@TypeOf(addFortyTwo(y)) == i64); |
| 4395 | 4396 | } |
| 4396 | 4397 | {#code_end#} |
| 4397 | 4398 | |
| ... | ... | @@ -4401,8 +4402,8 @@ test "fn type inference" { |
| 4401 | 4402 | const expect = @import("std").testing.expect; |
| 4402 | 4403 | |
| 4403 | 4404 | test "fn reflection" { |
| 4404 | | expect(@typeInfo(@TypeOf(expect)).Fn.return_type.? == void); |
| 4405 | | expect(@typeInfo(@TypeOf(expect)).Fn.is_var_args == false); |
| 4405 | try expect(@typeInfo(@TypeOf(expect)).Fn.args[0].arg_type.? == bool); |
| 4406 | try expect(@typeInfo(@TypeOf(expect)).Fn.is_var_args == false); |
| 4406 | 4407 | } |
| 4407 | 4408 | {#code_end#} |
| 4408 | 4409 | {#header_close#} |
| ... | ... | @@ -4437,7 +4438,7 @@ const AllocationError = error { |
| 4437 | 4438 | |
| 4438 | 4439 | test "coerce subset to superset" { |
| 4439 | 4440 | const err = foo(AllocationError.OutOfMemory); |
| 4440 | | std.testing.expect(err == FileOpenError.OutOfMemory); |
| 4441 | try std.testing.expect(err == FileOpenError.OutOfMemory); |
| 4441 | 4442 | } |
| 4442 | 4443 | |
| 4443 | 4444 | fn foo(err: AllocationError) FileOpenError { |
| ... | ... | @@ -4545,7 +4546,7 @@ fn charToDigit(c: u8) u8 { |
| 4545 | 4546 | |
| 4546 | 4547 | test "parse u64" { |
| 4547 | 4548 | const result = try parseU64("1234", 10); |
| 4548 | | std.testing.expect(result == 1234); |
| 4549 | try std.testing.expect(result == 1234); |
| 4549 | 4550 | } |
| 4550 | 4551 | {#code_end#} |
| 4551 | 4552 | <p> |
| ... | ... | @@ -4702,10 +4703,10 @@ test "error union" { |
| 4702 | 4703 | foo = error.SomeError; |
| 4703 | 4704 | |
| 4704 | 4705 | // Use compile-time reflection to access the payload type of an error union: |
| 4705 | | comptime expect(@typeInfo(@TypeOf(foo)).ErrorUnion.payload == i32); |
| 4706 | comptime try expect(@typeInfo(@TypeOf(foo)).ErrorUnion.payload == i32); |
| 4706 | 4707 | |
| 4707 | 4708 | // Use compile-time reflection to access the error set type of an error union: |
| 4708 | | comptime expect(@typeInfo(@TypeOf(foo)).ErrorUnion.error_set == anyerror); |
| 4709 | comptime try expect(@typeInfo(@TypeOf(foo)).ErrorUnion.error_set == anyerror); |
| 4709 | 4710 | } |
| 4710 | 4711 | {#code_end#} |
| 4711 | 4712 | {#header_open|Merging Error Sets#} |
| ... | ... | @@ -5082,7 +5083,7 @@ test "optional type" { |
| 5082 | 5083 | foo = 1234; |
| 5083 | 5084 | |
| 5084 | 5085 | // Use compile-time reflection to access the child type of the optional: |
| 5085 | | comptime expect(@typeInfo(@TypeOf(foo)).Optional.child == i32); |
| 5086 | comptime try expect(@typeInfo(@TypeOf(foo)).Optional.child == i32); |
| 5086 | 5087 | } |
| 5087 | 5088 | {#code_end#} |
| 5088 | 5089 | {#header_close#} |
| ... | ... | @@ -5109,11 +5110,11 @@ test "optional pointers" { |
| 5109 | 5110 | var x: i32 = 1; |
| 5110 | 5111 | ptr = &x; |
| 5111 | 5112 | |
| 5112 | | expect(ptr.?.* == 1); |
| 5113 | try expect(ptr.?.* == 1); |
| 5113 | 5114 | |
| 5114 | 5115 | // Optional pointers are the same size as normal pointers, because pointer |
| 5115 | 5116 | // value 0 is used as the null value. |
| 5116 | | expect(@sizeOf(?*i32) == @sizeOf(*i32)); |
| 5117 | try expect(@sizeOf(?*i32) == @sizeOf(*i32)); |
| 5117 | 5118 | } |
| 5118 | 5119 | {#code_end#} |
| 5119 | 5120 | {#header_close#} |
| ... | ... | @@ -5186,7 +5187,7 @@ const mem = std.mem; |
| 5186 | 5187 | test "cast *[1][*]const u8 to [*]const ?[*]const u8" { |
| 5187 | 5188 | const window_name = [1][*]const u8{"window name"}; |
| 5188 | 5189 | const x: [*]const ?[*]const u8 = &window_name; |
| 5189 | | expect(mem.eql(u8, std.mem.spanZ(@ptrCast([*:0]const u8, x[0].?)), "window name")); |
| 5190 | try expect(mem.eql(u8, std.mem.spanZ(@ptrCast([*:0]const u8, x[0].?)), "window name")); |
| 5190 | 5191 | } |
| 5191 | 5192 | {#code_end#} |
| 5192 | 5193 | {#header_close#} |
| ... | ... | @@ -5207,13 +5208,13 @@ test "integer widening" { |
| 5207 | 5208 | var d: u64 = c; |
| 5208 | 5209 | var e: u64 = d; |
| 5209 | 5210 | var f: u128 = e; |
| 5210 | | expect(f == a); |
| 5211 | try expect(f == a); |
| 5211 | 5212 | } |
| 5212 | 5213 | |
| 5213 | 5214 | test "implicit unsigned integer to signed integer" { |
| 5214 | 5215 | var a: u8 = 250; |
| 5215 | 5216 | var b: i16 = a; |
| 5216 | | expect(b == 250); |
| 5217 | try expect(b == 250); |
| 5217 | 5218 | } |
| 5218 | 5219 | |
| 5219 | 5220 | test "float widening" { |
| ... | ... | @@ -5225,7 +5226,7 @@ test "float widening" { |
| 5225 | 5226 | var b: f32 = a; |
| 5226 | 5227 | var c: f64 = b; |
| 5227 | 5228 | var d: f128 = c; |
| 5228 | | expect(d == a); |
| 5229 | try expect(d == a); |
| 5229 | 5230 | } |
| 5230 | 5231 | {#code_end#} |
| 5231 | 5232 | {#header_close#} |
| ... | ... | @@ -5257,48 +5258,48 @@ const expect = std.testing.expect; |
| 5257 | 5258 | test "[N]T to []const T" { |
| 5258 | 5259 | var x1: []const u8 = "hello"; |
| 5259 | 5260 | var x2: []const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 }; |
| 5260 | | expect(std.mem.eql(u8, x1, x2)); |
| 5261 | try expect(std.mem.eql(u8, x1, x2)); |
| 5261 | 5262 | |
| 5262 | 5263 | var y: []const f32 = &[2]f32{ 1.2, 3.4 }; |
| 5263 | | expect(y[0] == 1.2); |
| 5264 | try expect(y[0] == 1.2); |
| 5264 | 5265 | } |
| 5265 | 5266 | |
| 5266 | 5267 | // Likewise, it works when the destination type is an error union. |
| 5267 | 5268 | test "[N]T to E![]const T" { |
| 5268 | 5269 | var x1: anyerror![]const u8 = "hello"; |
| 5269 | 5270 | var x2: anyerror![]const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 }; |
| 5270 | | expect(std.mem.eql(u8, try x1, try x2)); |
| 5271 | try expect(std.mem.eql(u8, try x1, try x2)); |
| 5271 | 5272 | |
| 5272 | 5273 | var y: anyerror![]const f32 = &[2]f32{ 1.2, 3.4 }; |
| 5273 | | expect((try y)[0] == 1.2); |
| 5274 | try expect((try y)[0] == 1.2); |
| 5274 | 5275 | } |
| 5275 | 5276 | |
| 5276 | 5277 | // Likewise, it works when the destination type is an optional. |
| 5277 | 5278 | test "[N]T to ?[]const T" { |
| 5278 | 5279 | var x1: ?[]const u8 = "hello"; |
| 5279 | 5280 | var x2: ?[]const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 }; |
| 5280 | | expect(std.mem.eql(u8, x1.?, x2.?)); |
| 5281 | try expect(std.mem.eql(u8, x1.?, x2.?)); |
| 5281 | 5282 | |
| 5282 | 5283 | var y: ?[]const f32 = &[2]f32{ 1.2, 3.4 }; |
| 5283 | | expect(y.?[0] == 1.2); |
| 5284 | try expect(y.?[0] == 1.2); |
| 5284 | 5285 | } |
| 5285 | 5286 | |
| 5286 | 5287 | // In this cast, the array length becomes the slice length. |
| 5287 | 5288 | test "*[N]T to []T" { |
| 5288 | 5289 | var buf: [5]u8 = "hello".*; |
| 5289 | 5290 | const x: []u8 = &buf; |
| 5290 | | expect(std.mem.eql(u8, x, "hello")); |
| 5291 | try expect(std.mem.eql(u8, x, "hello")); |
| 5291 | 5292 | |
| 5292 | 5293 | const buf2 = [2]f32{ 1.2, 3.4 }; |
| 5293 | 5294 | const x2: []const f32 = &buf2; |
| 5294 | | expect(std.mem.eql(f32, x2, &[2]f32{ 1.2, 3.4 })); |
| 5295 | try expect(std.mem.eql(f32, x2, &[2]f32{ 1.2, 3.4 })); |
| 5295 | 5296 | } |
| 5296 | 5297 | |
| 5297 | 5298 | // Single-item pointers to arrays can be coerced to many-item pointers. |
| 5298 | 5299 | test "*[N]T to [*]T" { |
| 5299 | 5300 | var buf: [5]u8 = "hello".*; |
| 5300 | 5301 | const x: [*]u8 = &buf; |
| 5301 | | expect(x[4] == 'o'); |
| 5302 | try expect(x[4] == 'o'); |
| 5302 | 5303 | // x[5] would be an uncaught out of bounds pointer dereference! |
| 5303 | 5304 | } |
| 5304 | 5305 | |
| ... | ... | @@ -5306,7 +5307,7 @@ test "*[N]T to [*]T" { |
| 5306 | 5307 | test "*[N]T to ?[*]T" { |
| 5307 | 5308 | var buf: [5]u8 = "hello".*; |
| 5308 | 5309 | const x: ?[*]u8 = &buf; |
| 5309 | | expect(x.?[4] == 'o'); |
| 5310 | try expect(x.?[4] == 'o'); |
| 5310 | 5311 | } |
| 5311 | 5312 | |
| 5312 | 5313 | // Single-item pointers can be cast to len-1 single-item arrays. |
| ... | ... | @@ -5314,7 +5315,7 @@ test "*T to *[1]T" { |
| 5314 | 5315 | var x: i32 = 1234; |
| 5315 | 5316 | const y: *[1]i32 = &x; |
| 5316 | 5317 | const z: [*]i32 = y; |
| 5317 | | expect(z[0] == 1234); |
| 5318 | try expect(z[0] == 1234); |
| 5318 | 5319 | } |
| 5319 | 5320 | {#code_end#} |
| 5320 | 5321 | {#see_also|C Pointers#} |
| ... | ... | @@ -5331,8 +5332,8 @@ test "coerce to optionals" { |
| 5331 | 5332 | const x: ?i32 = 1234; |
| 5332 | 5333 | const y: ?i32 = null; |
| 5333 | 5334 | |
| 5334 | | expect(x.? == 1234); |
| 5335 | | expect(y == null); |
| 5335 | try expect(x.? == 1234); |
| 5336 | try expect(y == null); |
| 5336 | 5337 | } |
| 5337 | 5338 | {#code_end#} |
| 5338 | 5339 | <p>It works nested inside the {#link|Error Union Type#}, too:</p> |
| ... | ... | @@ -5344,8 +5345,8 @@ test "coerce to optionals wrapped in error union" { |
| 5344 | 5345 | const x: anyerror!?i32 = 1234; |
| 5345 | 5346 | const y: anyerror!?i32 = null; |
| 5346 | 5347 | |
| 5347 | | expect((try x).? == 1234); |
| 5348 | | expect((try y) == null); |
| 5348 | try expect((try x).? == 1234); |
| 5349 | try expect((try y) == null); |
| 5349 | 5350 | } |
| 5350 | 5351 | {#code_end#} |
| 5351 | 5352 | {#header_close#} |
| ... | ... | @@ -5361,8 +5362,8 @@ test "coercion to error unions" { |
| 5361 | 5362 | const x: anyerror!i32 = 1234; |
| 5362 | 5363 | const y: anyerror!i32 = error.Failure; |
| 5363 | 5364 | |
| 5364 | | expect((try x) == 1234); |
| 5365 | | std.testing.expectError(error.Failure, y); |
| 5365 | try expect((try x) == 1234); |
| 5366 | try std.testing.expectError(error.Failure, y); |
| 5366 | 5367 | } |
| 5367 | 5368 | {#code_end#} |
| 5368 | 5369 | {#header_close#} |
| ... | ... | @@ -5377,7 +5378,7 @@ const expect = std.testing.expect; |
| 5377 | 5378 | test "coercing large integer type to smaller one when value is comptime known to fit" { |
| 5378 | 5379 | const x: u64 = 255; |
| 5379 | 5380 | const y: u8 = x; |
| 5380 | | expect(y == 255); |
| 5381 | try expect(y == 255); |
| 5381 | 5382 | } |
| 5382 | 5383 | {#code_end#} |
| 5383 | 5384 | {#header_close#} |
| ... | ... | @@ -5405,11 +5406,11 @@ const U = union(E) { |
| 5405 | 5406 | test "coercion between unions and enums" { |
| 5406 | 5407 | var u = U{ .two = 12.34 }; |
| 5407 | 5408 | var e: E = u; |
| 5408 | | expect(e == E.two); |
| 5409 | try expect(e == E.two); |
| 5409 | 5410 | |
| 5410 | 5411 | const three = E.three; |
| 5411 | 5412 | var another_u: U = three; |
| 5412 | | expect(another_u == E.three); |
| 5413 | try expect(another_u == E.three); |
| 5413 | 5414 | } |
| 5414 | 5415 | {#code_end#} |
| 5415 | 5416 | {#see_also|union|enum#} |
| ... | ... | @@ -5482,37 +5483,37 @@ test "peer resolve int widening" { |
| 5482 | 5483 | var a: i8 = 12; |
| 5483 | 5484 | var b: i16 = 34; |
| 5484 | 5485 | var c = a + b; |
| 5485 | | expect(c == 46); |
| 5486 | | expect(@TypeOf(c) == i16); |
| 5486 | try expect(c == 46); |
| 5487 | try expect(@TypeOf(c) == i16); |
| 5487 | 5488 | } |
| 5488 | 5489 | |
| 5489 | 5490 | test "peer resolve arrays of different size to const slice" { |
| 5490 | | expect(mem.eql(u8, boolToStr(true), "true")); |
| 5491 | | expect(mem.eql(u8, boolToStr(false), "false")); |
| 5492 | | comptime expect(mem.eql(u8, boolToStr(true), "true")); |
| 5493 | | comptime expect(mem.eql(u8, boolToStr(false), "false")); |
| 5491 | try expect(mem.eql(u8, boolToStr(true), "true")); |
| 5492 | try expect(mem.eql(u8, boolToStr(false), "false")); |
| 5493 | comptime try expect(mem.eql(u8, boolToStr(true), "true")); |
| 5494 | comptime try expect(mem.eql(u8, boolToStr(false), "false")); |
| 5494 | 5495 | } |
| 5495 | 5496 | fn boolToStr(b: bool) []const u8 { |
| 5496 | 5497 | return if (b) "true" else "false"; |
| 5497 | 5498 | } |
| 5498 | 5499 | |
| 5499 | 5500 | test "peer resolve array and const slice" { |
| 5500 | | testPeerResolveArrayConstSlice(true); |
| 5501 | | comptime testPeerResolveArrayConstSlice(true); |
| 5501 | try testPeerResolveArrayConstSlice(true); |
| 5502 | comptime try testPeerResolveArrayConstSlice(true); |
| 5502 | 5503 | } |
| 5503 | | fn testPeerResolveArrayConstSlice(b: bool) void { |
| 5504 | fn testPeerResolveArrayConstSlice(b: bool) !void { |
| 5504 | 5505 | const value1 = if (b) "aoeu" else @as([]const u8, "zz"); |
| 5505 | 5506 | const value2 = if (b) @as([]const u8, "zz") else "aoeu"; |
| 5506 | | expect(mem.eql(u8, value1, "aoeu")); |
| 5507 | | expect(mem.eql(u8, value2, "zz")); |
| 5507 | try expect(mem.eql(u8, value1, "aoeu")); |
| 5508 | try expect(mem.eql(u8, value2, "zz")); |
| 5508 | 5509 | } |
| 5509 | 5510 | |
| 5510 | 5511 | test "peer type resolution: ?T and T" { |
| 5511 | | expect(peerTypeTAndOptionalT(true, false).? == 0); |
| 5512 | | expect(peerTypeTAndOptionalT(false, false).? == 3); |
| 5512 | try expect(peerTypeTAndOptionalT(true, false).? == 0); |
| 5513 | try expect(peerTypeTAndOptionalT(false, false).? == 3); |
| 5513 | 5514 | comptime { |
| 5514 | | expect(peerTypeTAndOptionalT(true, false).? == 0); |
| 5515 | | expect(peerTypeTAndOptionalT(false, false).? == 3); |
| 5515 | try expect(peerTypeTAndOptionalT(true, false).? == 0); |
| 5516 | try expect(peerTypeTAndOptionalT(false, false).? == 3); |
| 5516 | 5517 | } |
| 5517 | 5518 | } |
| 5518 | 5519 | fn peerTypeTAndOptionalT(c: bool, b: bool) ?usize { |
| ... | ... | @@ -5524,11 +5525,11 @@ fn peerTypeTAndOptionalT(c: bool, b: bool) ?usize { |
| 5524 | 5525 | } |
| 5525 | 5526 | |
| 5526 | 5527 | test "peer type resolution: *[0]u8 and []const u8" { |
| 5527 | | expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0); |
| 5528 | | expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1); |
| 5528 | try expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0); |
| 5529 | try expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1); |
| 5529 | 5530 | comptime { |
| 5530 | | expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0); |
| 5531 | | expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1); |
| 5531 | try expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0); |
| 5532 | try expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1); |
| 5532 | 5533 | } |
| 5533 | 5534 | } |
| 5534 | 5535 | fn peerTypeEmptyArrayAndSlice(a: bool, slice: []const u8) []const u8 { |
| ... | ... | @@ -5542,14 +5543,14 @@ test "peer type resolution: *[0]u8, []const u8, and anyerror![]u8" { |
| 5542 | 5543 | { |
| 5543 | 5544 | var data = "hi".*; |
| 5544 | 5545 | const slice = data[0..]; |
| 5545 | | expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0); |
| 5546 | | expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1); |
| 5546 | try expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0); |
| 5547 | try expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1); |
| 5547 | 5548 | } |
| 5548 | 5549 | comptime { |
| 5549 | 5550 | var data = "hi".*; |
| 5550 | 5551 | const slice = data[0..]; |
| 5551 | | expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0); |
| 5552 | | expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1); |
| 5552 | try expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0); |
| 5553 | try expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1); |
| 5553 | 5554 | } |
| 5554 | 5555 | } |
| 5555 | 5556 | fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) anyerror![]u8 { |
| ... | ... | @@ -5563,8 +5564,8 @@ fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) anyerror![]u8 { |
| 5563 | 5564 | test "peer type resolution: *const T and ?*T" { |
| 5564 | 5565 | const a = @intToPtr(*const usize, 0x123456780); |
| 5565 | 5566 | const b = @intToPtr(?*usize, 0x123456780); |
| 5566 | | expect(a == b); |
| 5567 | | expect(b == a); |
| 5567 | try expect(a == b); |
| 5568 | try expect(b == a); |
| 5568 | 5569 | } |
| 5569 | 5570 | {#code_end#} |
| 5570 | 5571 | {#header_close#} |
| ... | ... | @@ -5620,11 +5621,11 @@ test "turn HashMap into a set with void" { |
| 5620 | 5621 | try map.put(1, {}); |
| 5621 | 5622 | try map.put(2, {}); |
| 5622 | 5623 | |
| 5623 | | expect(map.contains(2)); |
| 5624 | | expect(!map.contains(3)); |
| 5624 | try expect(map.contains(2)); |
| 5625 | try expect(!map.contains(3)); |
| 5625 | 5626 | |
| 5626 | 5627 | _ = map.remove(2); |
| 5627 | | expect(!map.contains(2)); |
| 5628 | try expect(!map.contains(2)); |
| 5628 | 5629 | } |
| 5629 | 5630 | {#code_end#} |
| 5630 | 5631 | <p>Note that this is different from using a dummy value for the hash map value. |
| ... | ... | @@ -5679,7 +5680,7 @@ test "pointer to empty struct" { |
| 5679 | 5680 | var b = Empty{}; |
| 5680 | 5681 | var ptr_a = &a; |
| 5681 | 5682 | var ptr_b = &b; |
| 5682 | | comptime expect(ptr_a == ptr_b); |
| 5683 | comptime try expect(ptr_a == ptr_b); |
| 5683 | 5684 | } |
| 5684 | 5685 | {#code_end#} |
| 5685 | 5686 | <p>The type being pointed to can only ever be one value; therefore loads and stores are |
| ... | ... | @@ -5714,7 +5715,7 @@ test "@intToPtr for pointer to zero bit type" { |
| 5714 | 5715 | usingnamespace @import("std"); |
| 5715 | 5716 | |
| 5716 | 5717 | test "using std namespace" { |
| 5717 | | testing.expect(true); |
| 5718 | try testing.expect(true); |
| 5718 | 5719 | } |
| 5719 | 5720 | {#code_end#} |
| 5720 | 5721 | <p> |
| ... | ... | @@ -5826,7 +5827,7 @@ fn max(comptime T: type, a: T, b: T) T { |
| 5826 | 5827 | } |
| 5827 | 5828 | } |
| 5828 | 5829 | test "try to compare bools" { |
| 5829 | | @import("std").testing.expect(max(bool, false, true) == true); |
| 5830 | try @import("std").testing.expect(max(bool, false, true) == true); |
| 5830 | 5831 | } |
| 5831 | 5832 | {#code_end#} |
| 5832 | 5833 | <p> |
| ... | ... | @@ -5894,9 +5895,9 @@ fn performFn(comptime prefix_char: u8, start_value: i32) i32 { |
| 5894 | 5895 | } |
| 5895 | 5896 | |
| 5896 | 5897 | test "perform fn" { |
| 5897 | | expect(performFn('t', 1) == 6); |
| 5898 | | expect(performFn('o', 0) == 1); |
| 5899 | | expect(performFn('w', 99) == 99); |
| 5898 | try expect(performFn('t', 1) == 6); |
| 5899 | try expect(performFn('o', 0) == 1); |
| 5900 | try expect(performFn('w', 99) == 99); |
| 5900 | 5901 | } |
| 5901 | 5902 | {#code_end#} |
| 5902 | 5903 | <p> |
| ... | ... | @@ -5988,11 +5989,11 @@ fn fibonacci(index: u32) u32 { |
| 5988 | 5989 | |
| 5989 | 5990 | test "fibonacci" { |
| 5990 | 5991 | // test fibonacci at run-time |
| 5991 | | expect(fibonacci(7) == 13); |
| 5992 | try expect(fibonacci(7) == 13); |
| 5992 | 5993 | |
| 5993 | 5994 | // test fibonacci at compile-time |
| 5994 | 5995 | comptime { |
| 5995 | | expect(fibonacci(7) == 13); |
| 5996 | try expect(fibonacci(7) == 13); |
| 5996 | 5997 | } |
| 5997 | 5998 | } |
| 5998 | 5999 | {#code_end#} |
| ... | ... | @@ -6009,7 +6010,7 @@ fn fibonacci(index: u32) u32 { |
| 6009 | 6010 | |
| 6010 | 6011 | test "fibonacci" { |
| 6011 | 6012 | comptime { |
| 6012 | | expect(fibonacci(7) == 13); |
| 6013 | try expect(fibonacci(7) == 13); |
| 6013 | 6014 | } |
| 6014 | 6015 | } |
| 6015 | 6016 | {#code_end#} |
| ... | ... | @@ -6032,7 +6033,7 @@ fn fibonacci(index: i32) i32 { |
| 6032 | 6033 | |
| 6033 | 6034 | test "fibonacci" { |
| 6034 | 6035 | comptime { |
| 6035 | | expect(fibonacci(7) == 13); |
| 6036 | try expect(fibonacci(7) == 13); |
| 6036 | 6037 | } |
| 6037 | 6038 | } |
| 6038 | 6039 | {#code_end#} |
| ... | ... | @@ -6045,7 +6046,7 @@ test "fibonacci" { |
| 6045 | 6046 | <p> |
| 6046 | 6047 | What if we fix the base case, but put the wrong value in the {#syntax#}expect{#endsyntax#} line? |
| 6047 | 6048 | </p> |
| 6048 | | {#code_begin|test_err|encountered @panic at compile-time#} |
| 6049 | {#code_begin|test_err|test "fibonacci"... FAIL (TestUnexpectedResult)#} |
| 6049 | 6050 | const expect = @import("std").testing.expect; |
| 6050 | 6051 | |
| 6051 | 6052 | fn fibonacci(index: i32) i32 { |
| ... | ... | @@ -6055,7 +6056,7 @@ fn fibonacci(index: i32) i32 { |
| 6055 | 6056 | |
| 6056 | 6057 | test "fibonacci" { |
| 6057 | 6058 | comptime { |
| 6058 | | expect(fibonacci(7) == 99999); |
| 6059 | try expect(fibonacci(7) == 99999); |
| 6059 | 6060 | } |
| 6060 | 6061 | } |
| 6061 | 6062 | {#code_end#} |
| ... | ... | @@ -6105,7 +6106,7 @@ fn sum(numbers: []const i32) i32 { |
| 6105 | 6106 | } |
| 6106 | 6107 | |
| 6107 | 6108 | test "variable values" { |
| 6108 | | @import("std").testing.expect(sum_of_first_25_primes == 1060); |
| 6109 | try @import("std").testing.expect(sum_of_first_25_primes == 1060); |
| 6109 | 6110 | } |
| 6110 | 6111 | {#code_end#} |
| 6111 | 6112 | <p> |
| ... | ... | @@ -6513,7 +6514,7 @@ comptime { |
| 6513 | 6514 | extern fn my_func(a: i32, b: i32) i32; |
| 6514 | 6515 | |
| 6515 | 6516 | test "global assembly" { |
| 6516 | | expect(my_func(12, 34) == 46); |
| 6517 | try expect(my_func(12, 34) == 46); |
| 6517 | 6518 | } |
| 6518 | 6519 | {#code_end#} |
| 6519 | 6520 | {#header_close#} |
| ... | ... | @@ -6554,7 +6555,7 @@ var x: i32 = 1; |
| 6554 | 6555 | |
| 6555 | 6556 | test "suspend with no resume" { |
| 6556 | 6557 | var frame = async func(); |
| 6557 | | expect(x == 2); |
| 6558 | try expect(x == 2); |
| 6558 | 6559 | } |
| 6559 | 6560 | |
| 6560 | 6561 | fn func() void { |
| ... | ... | @@ -6581,14 +6582,14 @@ var result = false; |
| 6581 | 6582 | |
| 6582 | 6583 | test "async function suspend with block" { |
| 6583 | 6584 | _ = async testSuspendBlock(); |
| 6584 | | expect(!result); |
| 6585 | try expect(!result); |
| 6585 | 6586 | resume the_frame; |
| 6586 | | expect(result); |
| 6587 | try expect(result); |
| 6587 | 6588 | } |
| 6588 | 6589 | |
| 6589 | 6590 | fn testSuspendBlock() void { |
| 6590 | 6591 | suspend { |
| 6591 | | comptime expect(@TypeOf(@frame()) == *@Frame(testSuspendBlock)); |
| 6592 | comptime try expect(@TypeOf(@frame()) == *@Frame(testSuspendBlock)); |
| 6592 | 6593 | the_frame = @frame(); |
| 6593 | 6594 | } |
| 6594 | 6595 | result = true; |
| ... | ... | @@ -6617,7 +6618,7 @@ const expect = std.testing.expect; |
| 6617 | 6618 | test "resume from suspend" { |
| 6618 | 6619 | var my_result: i32 = 1; |
| 6619 | 6620 | _ = async testResumeFromSuspend(&my_result); |
| 6620 | | std.testing.expect(my_result == 2); |
| 6621 | try std.testing.expect(my_result == 2); |
| 6621 | 6622 | } |
| 6622 | 6623 | fn testResumeFromSuspend(my_result: *i32) void { |
| 6623 | 6624 | suspend { |
| ... | ... | @@ -6653,7 +6654,7 @@ test "async and await" { |
| 6653 | 6654 | |
| 6654 | 6655 | fn amain() void { |
| 6655 | 6656 | var frame = async func(); |
| 6656 | | comptime expect(@TypeOf(frame) == @Frame(func)); |
| 6657 | comptime try expect(@TypeOf(frame) == @Frame(func)); |
| 6657 | 6658 | |
| 6658 | 6659 | const ptr: anyframe->void = &frame; |
| 6659 | 6660 | const any_ptr: anyframe = ptr; |
| ... | ... | @@ -6694,8 +6695,8 @@ test "async function await" { |
| 6694 | 6695 | seq('f'); |
| 6695 | 6696 | resume the_frame; |
| 6696 | 6697 | seq('i'); |
| 6697 | | expect(final_result == 1234); |
| 6698 | | expect(std.mem.eql(u8, &seq_points, "abcdefghi")); |
| 6698 | try expect(final_result == 1234); |
| 6699 | try expect(std.mem.eql(u8, &seq_points, "abcdefghi")); |
| 6699 | 6700 | } |
| 6700 | 6701 | fn amain() void { |
| 6701 | 6702 | seq('b'); |
| ... | ... | @@ -6909,9 +6910,9 @@ fn readFile(allocator: *Allocator, filename: []const u8) ![]u8 { |
| 6909 | 6910 | for the current target to match the C ABI. When the child type of a pointer has |
| 6910 | 6911 | this alignment, the alignment can be omitted from the type. |
| 6911 | 6912 | </p> |
| 6912 | | <pre>{#syntax#}const expect = @import("std").testing.expect; |
| 6913 | <pre>{#syntax#}const expect = @import("std").debug.assert; |
| 6913 | 6914 | comptime { |
| 6914 | | expect(*u32 == *align(@alignOf(u32)) u32); |
| 6915 | assert(*u32 == *align(@alignOf(u32)) u32); |
| 6915 | 6916 | }{#endsyntax#}</pre> |
| 6916 | 6917 | <p> |
| 6917 | 6918 | The result is a target-specific compile time constant. It is guaranteed to be |
| ... | ... | @@ -6957,9 +6958,9 @@ test "async fn pointer in a struct field" { |
| 6957 | 6958 | var foo = Foo{ .bar = func }; |
| 6958 | 6959 | var bytes: [64]u8 align(@alignOf(@Frame(func))) = undefined; |
| 6959 | 6960 | const f = @asyncCall(&bytes, {}, foo.bar, .{&data}); |
| 6960 | | expect(data == 2); |
| 6961 | try expect(data == 2); |
| 6961 | 6962 | resume f; |
| 6962 | | expect(data == 4); |
| 6963 | try expect(data == 4); |
| 6963 | 6964 | } |
| 6964 | 6965 | |
| 6965 | 6966 | fn func(y: *i32) void { |
| ... | ... | @@ -7146,7 +7147,7 @@ fn func(y: *i32) void { |
| 7146 | 7147 | const expect = @import("std").testing.expect; |
| 7147 | 7148 | |
| 7148 | 7149 | test "noinline function call" { |
| 7149 | | expect(@call(.{}, add, .{3, 9}) == 12); |
| 7150 | try expect(@call(.{}, add, .{3, 9}) == 12); |
| 7150 | 7151 | } |
| 7151 | 7152 | |
| 7152 | 7153 | fn add(a: i32, b: i32) i32 { |
| ... | ... | @@ -7621,17 +7622,17 @@ test "field access by string" { |
| 7621 | 7622 | @field(p, "x") = 4; |
| 7622 | 7623 | @field(p, "y") = @field(p, "x") + 1; |
| 7623 | 7624 | |
| 7624 | | expect(@field(p, "x") == 4); |
| 7625 | | expect(@field(p, "y") == 5); |
| 7625 | try expect(@field(p, "x") == 4); |
| 7626 | try expect(@field(p, "y") == 5); |
| 7626 | 7627 | } |
| 7627 | 7628 | |
| 7628 | 7629 | test "decl access by string" { |
| 7629 | 7630 | const expect = std.testing.expect; |
| 7630 | 7631 | |
| 7631 | | expect(@field(Point, "z") == 1); |
| 7632 | try expect(@field(Point, "z") == 1); |
| 7632 | 7633 | |
| 7633 | 7634 | @field(Point, "z") = 2; |
| 7634 | | expect(@field(Point, "z") == 2); |
| 7635 | try expect(@field(Point, "z") == 2); |
| 7635 | 7636 | } |
| 7636 | 7637 | {#code_end#} |
| 7637 | 7638 | |
| ... | ... | @@ -7747,16 +7748,16 @@ const Foo = struct { |
| 7747 | 7748 | }; |
| 7748 | 7749 | |
| 7749 | 7750 | test "@hasDecl" { |
| 7750 | | expect(@hasDecl(Foo, "blah")); |
| 7751 | try expect(@hasDecl(Foo, "blah")); |
| 7751 | 7752 | |
| 7752 | 7753 | // Even though `hi` is private, @hasDecl returns true because this test is |
| 7753 | 7754 | // in the same file scope as Foo. It would return false if Foo was declared |
| 7754 | 7755 | // in a different file. |
| 7755 | | expect(@hasDecl(Foo, "hi")); |
| 7756 | try expect(@hasDecl(Foo, "hi")); |
| 7756 | 7757 | |
| 7757 | 7758 | // @hasDecl is for declarations; not fields. |
| 7758 | | expect(!@hasDecl(Foo, "nope")); |
| 7759 | | expect(!@hasDecl(Foo, "nope1234")); |
| 7759 | try expect(!@hasDecl(Foo, "nope")); |
| 7760 | try expect(!@hasDecl(Foo, "nope1234")); |
| 7760 | 7761 | } |
| 7761 | 7762 | {#code_end#} |
| 7762 | 7763 | {#see_also|@hasField#} |
| ... | ... | @@ -7937,8 +7938,8 @@ test "@wasmMemoryGrow" { |
| 7937 | 7938 | if (builtin.arch != .wasm32) return error.SkipZigTest; |
| 7938 | 7939 | |
| 7939 | 7940 | var prev = @wasmMemorySize(0); |
| 7940 | | expect(prev == @wasmMemoryGrow(0, 1)); |
| 7941 | | expect(prev + 1 == @wasmMemorySize(0)); |
| 7941 | try expect(prev == @wasmMemoryGrow(0, 1)); |
| 7942 | try expect(prev + 1 == @wasmMemorySize(0)); |
| 7942 | 7943 | } |
| 7943 | 7944 | {#code_end#} |
| 7944 | 7945 | {#see_also|@wasmMemorySize#} |
| ... | ... | @@ -8279,8 +8280,8 @@ const expect = std.testing.expect; |
| 8279 | 8280 | test "vector @splat" { |
| 8280 | 8281 | const scalar: u32 = 5; |
| 8281 | 8282 | const result = @splat(4, scalar); |
| 8282 | | comptime expect(@TypeOf(result) == std.meta.Vector(4, u32)); |
| 8283 | | expect(std.mem.eql(u32, &@as([4]u32, result), &[_]u32{ 5, 5, 5, 5 })); |
| 8283 | comptime try expect(@TypeOf(result) == std.meta.Vector(4, u32)); |
| 8284 | try expect(std.mem.eql(u32, &@as([4]u32, result), &[_]u32{ 5, 5, 5, 5 })); |
| 8284 | 8285 | } |
| 8285 | 8286 | {#code_end#} |
| 8286 | 8287 | <p> |
| ... | ... | @@ -8322,10 +8323,10 @@ test "vector @reduce" { |
| 8322 | 8323 | const value: std.meta.Vector(4, i32) = [_]i32{ 1, -1, 1, -1 }; |
| 8323 | 8324 | const result = value > @splat(4, @as(i32, 0)); |
| 8324 | 8325 | // result is { true, false, true, false }; |
| 8325 | | comptime expect(@TypeOf(result) == std.meta.Vector(4, bool)); |
| 8326 | comptime try expect(@TypeOf(result) == std.meta.Vector(4, bool)); |
| 8326 | 8327 | const is_all_true = @reduce(.And, result); |
| 8327 | | comptime expect(@TypeOf(is_all_true) == bool); |
| 8328 | | expect(is_all_true == false); |
| 8328 | comptime try expect(@TypeOf(is_all_true) == bool); |
| 8329 | try expect(is_all_true == false); |
| 8329 | 8330 | } |
| 8330 | 8331 | {#code_end#} |
| 8331 | 8332 | {#see_also|Vectors|@setFloatMode#} |
| ... | ... | @@ -8341,16 +8342,16 @@ const std = @import("std"); |
| 8341 | 8342 | const expect = std.testing.expect; |
| 8342 | 8343 | |
| 8343 | 8344 | test "@src" { |
| 8344 | | doTheTest(); |
| 8345 | try doTheTest(); |
| 8345 | 8346 | } |
| 8346 | 8347 | |
| 8347 | | fn doTheTest() void { |
| 8348 | fn doTheTest() !void { |
| 8348 | 8349 | const src = @src(); |
| 8349 | 8350 | |
| 8350 | | expect(src.line == 9); |
| 8351 | | expect(src.column == 17); |
| 8352 | | expect(std.mem.endsWith(u8, src.fn_name, "doTheTest")); |
| 8353 | | expect(std.mem.endsWith(u8, src.file, "test.zig")); |
| 8351 | try expect(src.line == 9); |
| 8352 | try expect(src.column == 17); |
| 8353 | try expect(std.mem.endsWith(u8, src.fn_name, "doTheTest")); |
| 8354 | try expect(std.mem.endsWith(u8, src.file, "test.zig")); |
| 8354 | 8355 | } |
| 8355 | 8356 | {#code_end#} |
| 8356 | 8357 | {#header_close#} |
| ... | ... | @@ -8527,7 +8528,7 @@ const expect = std.testing.expect; |
| 8527 | 8528 | test "@This()" { |
| 8528 | 8529 | var items = [_]i32{ 1, 2, 3, 4 }; |
| 8529 | 8530 | const list = List(i32){ .items = items[0..] }; |
| 8530 | | expect(list.length() == 4); |
| 8531 | try expect(list.length() == 4); |
| 8531 | 8532 | } |
| 8532 | 8533 | |
| 8533 | 8534 | fn List(comptime T: type) type { |
| ... | ... | @@ -8573,7 +8574,7 @@ const expect = std.testing.expect; |
| 8573 | 8574 | test "integer truncation" { |
| 8574 | 8575 | var a: u16 = 0xabcd; |
| 8575 | 8576 | var b: u8 = @truncate(u8, a); |
| 8576 | | expect(b == 0xcd); |
| 8577 | try expect(b == 0xcd); |
| 8577 | 8578 | } |
| 8578 | 8579 | {#code_end#} |
| 8579 | 8580 | <p> |
| ... | ... | @@ -8661,8 +8662,8 @@ const expect = std.testing.expect; |
| 8661 | 8662 | test "no runtime side effects" { |
| 8662 | 8663 | var data: i32 = 0; |
| 8663 | 8664 | const T = @TypeOf(foo(i32, &data)); |
| 8664 | | comptime expect(T == i32); |
| 8665 | | expect(data == 0); |
| 8665 | comptime try expect(T == i32); |
| 8666 | try expect(data == 0); |
| 8666 | 8667 | } |
| 8667 | 8668 | |
| 8668 | 8669 | fn foo(comptime T: type, ptr: *T) T { |
| ... | ... | @@ -8972,9 +8973,9 @@ const maxInt = std.math.maxInt; |
| 8972 | 8973 | test "wraparound addition and subtraction" { |
| 8973 | 8974 | const x: i32 = maxInt(i32); |
| 8974 | 8975 | const min_val = x +% 1; |
| 8975 | | expect(min_val == minInt(i32)); |
| 8976 | try expect(min_val == minInt(i32)); |
| 8976 | 8977 | const max_val = min_val -% 1; |
| 8977 | | expect(max_val == maxInt(i32)); |
| 8978 | try expect(max_val == maxInt(i32)); |
| 8978 | 8979 | } |
| 8979 | 8980 | {#code_end#} |
| 8980 | 8981 | {#header_close#} |
| ... | ... | @@ -9405,7 +9406,7 @@ test "using an allocator" { |
| 9405 | 9406 | var buffer: [100]u8 = undefined; |
| 9406 | 9407 | const allocator = &std.heap.FixedBufferAllocator.init(&buffer).allocator; |
| 9407 | 9408 | const result = try concat(allocator, "foo", "bar"); |
| 9408 | | expect(std.mem.eql(u8, "foobar", result)); |
| 9409 | try expect(std.mem.eql(u8, "foobar", result)); |
| 9409 | 9410 | } |
| 9410 | 9411 | |
| 9411 | 9412 | fn concat(allocator: *Allocator, a: []const u8, b: []const u8) ![]u8 { |
| ... | ... | @@ -9675,7 +9676,7 @@ const builtin = std.builtin; |
| 9675 | 9676 | const expect = std.testing.expect; |
| 9676 | 9677 | |
| 9677 | 9678 | test "builtin.is_test" { |
| 9678 | | expect(builtin.is_test); |
| 9679 | try expect(builtin.is_test); |
| 9679 | 9680 | } |
| 9680 | 9681 | {#code_end#} |
| 9681 | 9682 | <p> |
| ... | ... | @@ -9720,13 +9721,13 @@ test "assert in release fast mode" { |
| 9720 | 9721 | <p> |
| 9721 | 9722 | Better practice for checking the output when testing is to use {#syntax#}std.testing.expect{#endsyntax#}: |
| 9722 | 9723 | </p> |
| 9723 | | {#code_begin|test_err|test failure#} |
| 9724 | {#code_begin|test_err|test "expect in release fast mode"... FAIL (TestUnexpectedResult)#} |
| 9724 | 9725 | {#code_release_fast#} |
| 9725 | 9726 | const std = @import("std"); |
| 9726 | 9727 | const expect = std.testing.expect; |
| 9727 | 9728 | |
| 9728 | 9729 | test "expect in release fast mode" { |
| 9729 | | expect(false); |
| 9730 | try expect(false); |
| 9730 | 9731 | } |
| 9731 | 9732 | {#code_end#} |
| 9732 | 9733 | <p>See the rest of the {#syntax#}std.testing{#endsyntax#} namespace for more available functions.</p> |