authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-05-08 10:45:19+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-05-08 15:16:05+03:00
log08b6baca122698db0d8543c8953d543f318c6422
treed07d093582fcc16b8900c3d10fe0f6dd19b2d394
parent7fe39c4e9680870f0bf30368c9f5b345b262b0eb

update usage of std.testing in langref.html


1 files changed, 349 insertions(+), 348 deletions(-)

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