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" {
358358 //expect(false);
359359
360360 const x = true; // another comment
361 expect(x);
361 try expect(x);
362362}
363363 {#code_end#}
364364 <p>
......@@ -718,15 +718,15 @@ const mem = @import("std").mem;
718718
719719test "string literals" {
720720 const bytes = "hello";
721 expect(@TypeOf(bytes) == *const [5:0]u8);
722 expect(bytes.len == 5);
723 expect(bytes[1] == 'e');
724 expect(bytes[5] == 0);
725 expect('e' == '\x65');
726 expect('\u{1f4a9}' == 128169);
727 expect('πŸ’―' == 128175);
728 expect(mem.eql(u8, "hello", "h\x65llo"));
729 expect("\xff"[0] == 0xff); // non-UTF-8 strings are possible with \xNN notation.
721 try expect(@TypeOf(bytes) == *const [5:0]u8);
722 try expect(bytes.len == 5);
723 try expect(bytes[1] == 'e');
724 try expect(bytes[5] == 0);
725 try expect('e' == '\x65');
726 try expect('\u{1f4a9}' == 128169);
727 try expect('πŸ’―' == 128175);
728 try expect(mem.eql(u8, "hello", "h\x65llo"));
729 try expect("\xff"[0] == 0xff); // non-UTF-8 strings are possible with \xNN notation.
730730}
731731 {#code_end#}
732732 {#see_also|Arrays|Zig Test|Source Encoding#}
......@@ -826,7 +826,7 @@ test "var" {
826826
827827 y += 1;
828828
829 expect(y == 5679);
829 try expect(y == 5679);
830830}
831831 {#code_end#}
832832 <p>Variables must be initialized:</p>
......@@ -845,7 +845,7 @@ const expect = @import("std").testing.expect;
845845test "init with undefined" {
846846 var x: i32 = undefined;
847847 x = 1;
848 expect(x == 1);
848 try expect(x == 1);
849849}
850850 {#code_end#}
851851 <p>
......@@ -887,8 +887,8 @@ var y: i32 = add(10, x);
887887const x: i32 = add(12, 34);
888888
889889test "global variables" {
890 expect(x == 46);
891 expect(y == 56);
890 try expect(x == 46);
891 try expect(y == 56);
892892}
893893
894894fn add(a: i32, b: i32) i32 {
......@@ -906,8 +906,8 @@ const std = @import("std");
906906const expect = std.testing.expect;
907907
908908test "namespaced global variable" {
909 expect(foo() == 1235);
910 expect(foo() == 1236);
909 try expect(foo() == 1235);
910 try expect(foo() == 1236);
911911}
912912
913913fn foo() i32 {
......@@ -985,8 +985,8 @@ test "comptime vars" {
985985 x += 1;
986986 y += 1;
987987
988 expect(x == 2);
989 expect(y == 2);
988 try expect(x == 2);
989 try expect(y == 2);
990990
991991 if (y != 2) {
992992 // This compile error never triggers because y is a comptime variable,
......@@ -1777,6 +1777,7 @@ orelse catch
17771777 {#header_open|Arrays#}
17781778 {#code_begin|test|arrays#}
17791779const expect = @import("std").testing.expect;
1780const assert = @import("std").debug.assert;
17801781const mem = @import("std").mem;
17811782
17821783// array literal
......@@ -1784,14 +1785,14 @@ const message = [_]u8{ 'h', 'e', 'l', 'l', 'o' };
17841785
17851786// get the size of an array
17861787comptime {
1787 expect(message.len == 5);
1788 assert(message.len == 5);
17881789}
17891790
17901791// A string literal is a single-item pointer to an array literal.
17911792const same_message = "hello";
17921793
17931794comptime {
1794 expect(mem.eql(u8, &message, same_message));
1795 assert(mem.eql(u8, &message, same_message));
17951796}
17961797
17971798test "iterate over an array" {
......@@ -1799,7 +1800,7 @@ test "iterate over an array" {
17991800 for (message) |byte| {
18001801 sum += byte;
18011802 }
1802 expect(sum == 'h' + 'e' + 'l' * 2 + 'o');
1803 try expect(sum == 'h' + 'e' + 'l' * 2 + 'o');
18031804}
18041805
18051806// modifiable array
......@@ -1809,8 +1810,8 @@ test "modify an array" {
18091810 for (some_integers) |*item, i| {
18101811 item.* = @intCast(i32, i);
18111812 }
1812 expect(some_integers[10] == 10);
1813 expect(some_integers[99] == 99);
1813 try expect(some_integers[10] == 10);
1814 try expect(some_integers[99] == 99);
18141815}
18151816
18161817// array concatenation works if the values are known
......@@ -1819,7 +1820,7 @@ const part_one = [_]i32{ 1, 2, 3, 4 };
18191820const part_two = [_]i32{ 5, 6, 7, 8 };
18201821const all_of_it = part_one ++ part_two;
18211822comptime {
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 }));
18231824}
18241825
18251826// remember that string literals are arrays
......@@ -1827,21 +1828,21 @@ const hello = "hello";
18271828const world = "world";
18281829const hello_world = hello ++ " " ++ world;
18291830comptime {
1830 expect(mem.eql(u8, hello_world, "hello world"));
1831 assert(mem.eql(u8, hello_world, "hello world"));
18311832}
18321833
18331834// ** does repeating patterns
18341835const pattern = "ab" ** 3;
18351836comptime {
1836 expect(mem.eql(u8, pattern, "ababab"));
1837 assert(mem.eql(u8, pattern, "ababab"));
18371838}
18381839
18391840// initialize an array to zero
18401841const all_zero = [_]u16{0} ** 10;
18411842
18421843comptime {
1843 expect(all_zero.len == 10);
1844 expect(all_zero[5] == 0);
1844 assert(all_zero.len == 10);
1845 assert(all_zero[5] == 0);
18451846}
18461847
18471848// use compile-time code to initialize an array
......@@ -1861,8 +1862,8 @@ const Point = struct {
18611862};
18621863
18631864test "compile-time array initialization" {
1864 expect(fancy_array[4].x == 4);
1865 expect(fancy_array[4].y == 8);
1865 try expect(fancy_array[4].x == 4);
1866 try expect(fancy_array[4].y == 8);
18661867}
18671868
18681869// call a function to initialize an array
......@@ -1874,9 +1875,9 @@ fn makePoint(x: i32) Point {
18741875 };
18751876}
18761877test "array initialization with function calls" {
1877 expect(more_points[4].x == 3);
1878 expect(more_points[4].y == 6);
1879 expect(more_points.len == 10);
1878 try expect(more_points[4].x == 3);
1879 try expect(more_points[4].y == 6);
1880 try expect(more_points.len == 10);
18801881}
18811882 {#code_end#}
18821883 {#see_also|for|Slices#}
......@@ -1890,10 +1891,10 @@ const expect = std.testing.expect;
18901891
18911892test "anonymous list literal syntax" {
18921893 var array: [4]u8 = .{11, 22, 33, 44};
1893 expect(array[0] == 11);
1894 expect(array[1] == 22);
1895 expect(array[2] == 33);
1896 expect(array[3] == 44);
1894 try expect(array[0] == 11);
1895 try expect(array[1] == 22);
1896 try expect(array[2] == 33);
1897 try expect(array[3] == 44);
18971898}
18981899 {#code_end#}
18991900 <p>
......@@ -1905,15 +1906,15 @@ const std = @import("std");
19051906const expect = std.testing.expect;
19061907
19071908test "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"});
19091910}
19101911
1911fn dump(args: anytype) void {
1912 expect(args.@"0" == 1234);
1913 expect(args.@"1" == 12.34);
1914 expect(args.@"2");
1915 expect(args.@"3"[0] == 'h');
1916 expect(args.@"3"[1] == 'i');
1912fn dump(args: anytype) !void {
1913 try expect(args.@"0" == 1234);
1914 try expect(args.@"1" == 12.34);
1915 try expect(args.@"2");
1916 try expect(args.@"3"[0] == 'h');
1917 try expect(args.@"3"[1] == 'i');
19171918}
19181919 {#code_end#}
19191920 {#header_close#}
......@@ -1934,13 +1935,13 @@ const mat4x4 = [4][4]f32{
19341935};
19351936test "multidimensional arrays" {
19361937 // 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
19391940 // Here we iterate with for loops.
19401941 for (mat4x4) |row, row_index| {
19411942 for (row) |cell, column_index| {
19421943 if (row_index == column_index) {
1943 expect(cell == 1.0);
1944 try expect(cell == 1.0);
19441945 }
19451946 }
19461947 }
......@@ -1960,9 +1961,9 @@ const expect = std.testing.expect;
19601961test "null terminated array" {
19611962 const array = [_:0]u8 {1, 2, 3, 4};
19621963
1963 expect(@TypeOf(array) == [4:0]u8);
1964 expect(array.len == 4);
1965 expect(array[4] == 0);
1964 try expect(@TypeOf(array) == [4:0]u8);
1965 try expect(array.len == 4);
1966 try expect(array[4] == 0);
19661967}
19671968 {#code_end#}
19681969 {#see_also|Sentinel-Terminated Pointers|Sentinel-Terminated Slices#}
......@@ -2040,17 +2041,17 @@ test "address of syntax" {
20402041 const x_ptr = &x;
20412042
20422043 // Dereference a pointer:
2043 expect(x_ptr.* == 1234);
2044 try expect(x_ptr.* == 1234);
20442045
20452046 // 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
20482049 // If you want to mutate the value, you'd need an address of a mutable variable:
20492050 var y: i32 = 5678;
20502051 const y_ptr = &y;
2051 expect(@TypeOf(y_ptr) == *i32);
2052 try expect(@TypeOf(y_ptr) == *i32);
20522053 y_ptr.* += 1;
2053 expect(y_ptr.* == 5679);
2054 try expect(y_ptr.* == 5679);
20542055}
20552056
20562057test "pointer array access" {
......@@ -2059,11 +2060,11 @@ test "pointer array access" {
20592060 // does not support pointer arithmetic.
20602061 var array = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
20612062 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);
20652066 ptr.* += 1;
2066 expect(array[2] == 4);
2067 try expect(array[2] == 4);
20672068}
20682069 {#code_end#}
20692070 <p>
......@@ -2081,11 +2082,11 @@ const expect = @import("std").testing.expect;
20812082test "pointer slicing" {
20822083 var array = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
20832084 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);
20872088 slice[1] += 1;
2088 expect(array[3] == 5);
2089 try expect(array[3] == 5);
20892090}
20902091 {#code_end#}
20912092 <p>Pointers work at compile-time too, as long as the code does not depend on
......@@ -2099,7 +2100,7 @@ test "comptime pointers" {
20992100 const ptr = &x;
21002101 ptr.* += 1;
21012102 x += 1;
2102 expect(ptr.* == 3);
2103 try expect(ptr.* == 3);
21032104 }
21042105}
21052106 {#code_end#}
......@@ -2111,8 +2112,8 @@ const expect = @import("std").testing.expect;
21112112test "@ptrToInt and @intToPtr" {
21122113 const ptr = @intToPtr(*i32, 0xdeadbee0);
21132114 const addr = @ptrToInt(ptr);
2114 expect(@TypeOf(addr) == usize);
2115 expect(addr == 0xdeadbee0);
2115 try expect(@TypeOf(addr) == usize);
2116 try expect(addr == 0xdeadbee0);
21162117}
21172118 {#code_end#}
21182119 <p>Zig is able to preserve memory addresses in comptime code, as long as
......@@ -2126,8 +2127,8 @@ test "comptime @intToPtr" {
21262127 // ptr is never dereferenced.
21272128 const ptr = @intToPtr(*i32, 0xdeadbee0);
21282129 const addr = @ptrToInt(ptr);
2129 expect(@TypeOf(addr) == usize);
2130 expect(addr == 0xdeadbee0);
2130 try expect(@TypeOf(addr) == usize);
2131 try expect(addr == 0xdeadbee0);
21312132 }
21322133}
21332134 {#code_end#}
......@@ -2142,7 +2143,7 @@ const expect = @import("std").testing.expect;
21422143
21432144test "volatile" {
21442145 const mmio_ptr = @intToPtr(*volatile u8, 0x12345678);
2145 expect(@TypeOf(mmio_ptr) == *volatile u8);
2146 try expect(@TypeOf(mmio_ptr) == *volatile u8);
21462147}
21472148 {#code_end#}
21482149 <p>
......@@ -2163,20 +2164,20 @@ const expect = std.testing.expect;
21632164test "pointer casting" {
21642165 const bytes align(@alignOf(u32)) = [_]u8{ 0x12, 0x12, 0x12, 0x12 };
21652166 const u32_ptr = @ptrCast(*const u32, &bytes);
2166 expect(u32_ptr.* == 0x12121212);
2167 try expect(u32_ptr.* == 0x12121212);
21672168
21682169 // Even this example is contrived - there are better ways to do the above than
21692170 // pointer casting. For example, using a slice narrowing cast:
21702171 const u32_value = std.mem.bytesAsSlice(u32, bytes[0..])[0];
2171 expect(u32_value == 0x12121212);
2172 try expect(u32_value == 0x12121212);
21722173
21732174 // And even another way, the most straightforward way to do it:
2174 expect(@bitCast(u32, bytes) == 0x12121212);
2175 try expect(@bitCast(u32, bytes) == 0x12121212);
21752176}
21762177
21772178test "pointer child type" {
21782179 // 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);
21802181}
21812182 {#code_end#}
21822183 {#header_open|Alignment#}
......@@ -2201,10 +2202,10 @@ const expect = std.testing.expect;
22012202test "variable alignment" {
22022203 var x: i32 = 1234;
22032204 const align_of_i32 = @alignOf(@TypeOf(x));
2204 expect(@TypeOf(&x) == *i32);
2205 expect(*i32 == *align(align_of_i32) i32);
2205 try expect(@TypeOf(&x) == *i32);
2206 try expect(*i32 == *align(align_of_i32) i32);
22062207 if (std.Target.current.cpu.arch == .x86_64) {
2207 expect(@typeInfo(*i32).Pointer.alignment == 4);
2208 try expect(@typeInfo(*i32).Pointer.alignment == 4);
22082209 }
22092210}
22102211 {#code_end#}
......@@ -2222,11 +2223,11 @@ const expect = @import("std").testing.expect;
22222223var foo: u8 align(4) = 100;
22232224
22242225test "global variable alignment" {
2225 expect(@typeInfo(@TypeOf(&foo)).Pointer.alignment == 4);
2226 expect(@TypeOf(&foo) == *align(4) u8);
2226 try expect(@typeInfo(@TypeOf(&foo)).Pointer.alignment == 4);
2227 try expect(@TypeOf(&foo) == *align(4) u8);
22272228 const as_pointer_to_array: *[1]u8 = &foo;
22282229 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);
22302231}
22312232
22322233fn derp() align(@sizeOf(usize) * 2) i32 { return 1234; }
......@@ -2234,9 +2235,9 @@ fn noop1() align(1) void {}
22342235fn noop4() align(4) void {}
22352236
22362237test "function alignment" {
2237 expect(derp() == 1234);
2238 expect(@TypeOf(noop1) == fn() align(1) void);
2239 expect(@TypeOf(noop4) == fn() align(4) void);
2238 try expect(derp() == 1234);
2239 try expect(@TypeOf(noop1) == fn() align(1) void);
2240 try expect(@TypeOf(noop4) == fn() align(4) void);
22402241 noop1();
22412242 noop4();
22422243}
......@@ -2253,7 +2254,7 @@ const std = @import("std");
22532254test "pointer alignment safety" {
22542255 var array align(4) = [_]u32{ 0x11111111, 0x11111111 };
22552256 const bytes = std.mem.sliceAsBytes(array[0..]);
2256 std.testing.expect(foo(bytes) == 0x11111111);
2257 try std.testing.expect(foo(bytes) == 0x11111111);
22572258}
22582259fn foo(bytes: []u8) u32 {
22592260 const slice4 = bytes[1..5];
......@@ -2279,7 +2280,7 @@ const expect = std.testing.expect;
22792280test "allowzero" {
22802281 var zero: usize = 0;
22812282 var ptr = @intToPtr(*allowzero i32, zero);
2282 expect(@ptrToInt(ptr) == 0);
2283 try expect(@ptrToInt(ptr) == 0);
22832284}
22842285 {#code_end#}
22852286 {#header_close#}
......@@ -2321,14 +2322,14 @@ test "basic slices" {
23212322 // Both can be accessed with the `len` field.
23222323 var known_at_runtime_zero: usize = 0;
23232324 const slice = array[known_at_runtime_zero..array.len];
2324 expect(&slice[0] == &array[0]);
2325 expect(slice.len == array.len);
2325 try expect(&slice[0] == &array[0]);
2326 try expect(slice.len == array.len);
23262327
23272328 // Using the address-of operator on a slice gives a single-item pointer,
23282329 // while using the `ptr` field gives a many-item pointer.
2329 expect(@TypeOf(slice.ptr) == [*]i32);
2330 expect(@TypeOf(&slice[0]) == *i32);
2331 expect(@ptrToInt(slice.ptr) == @ptrToInt(&slice[0]));
2330 try expect(@TypeOf(slice.ptr) == [*]i32);
2331 try expect(@TypeOf(&slice[0]) == *i32);
2332 try expect(@ptrToInt(slice.ptr) == @ptrToInt(&slice[0]));
23322333
23332334 // Slices have array bounds checking. If you try to access something out
23342335 // of bounds, you'll get a safety check failure:
......@@ -2362,7 +2363,7 @@ test "using slices for strings" {
23622363 // Generally, you can use UTF-8 and not worry about whether something is a
23632364 // string. If you don't need to deal with individual characters, no need
23642365 // to decode.
2365 expect(mem.eql(u8, hello_world, "hello δΈ–η•Œ"));
2366 try expect(mem.eql(u8, hello_world, "hello δΈ–η•Œ"));
23662367}
23672368
23682369test "slice pointer" {
......@@ -2372,16 +2373,16 @@ test "slice pointer" {
23722373 // You can use slicing syntax to convert a pointer into a slice:
23732374 const slice = ptr[0..5];
23742375 slice[2] = 3;
2375 expect(slice[2] == 3);
2376 try expect(slice[2] == 3);
23762377 // The slice is mutable because we sliced a mutable pointer.
23772378 // Furthermore, it is actually a pointer to an array, since the start
23782379 // and end indexes were both comptime-known.
2379 expect(@TypeOf(slice) == *[5]u8);
2380 try expect(@TypeOf(slice) == *[5]u8);
23802381
23812382 // You can also slice a slice:
23822383 const slice2 = slice[2..3];
2383 expect(slice2.len == 1);
2384 expect(slice2[0] == 3);
2384 try expect(slice2.len == 1);
2385 try expect(slice2[0] == 3);
23852386}
23862387 {#code_end#}
23872388 {#see_also|Pointers|for|Arrays#}
......@@ -2400,8 +2401,8 @@ const expect = std.testing.expect;
24002401test "null terminated slice" {
24012402 const slice: [:0]const u8 = "hello";
24022403
2403 expect(slice.len == 5);
2404 expect(slice[5] == 0);
2404 try expect(slice.len == 5);
2405 try expect(slice[5] == 0);
24052406}
24062407 {#code_end#}
24072408 {#see_also|Sentinel-Terminated Pointers|Sentinel-Terminated Arrays#}
......@@ -2463,12 +2464,12 @@ const expect = @import("std").testing.expect;
24632464test "dot product" {
24642465 const v1 = Vec3.init(1.0, 0.0, 0.0);
24652466 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
24682469 // Other than being available to call with dot syntax, struct methods are
24692470 // not special. You can reference them as any other declaration inside
24702471 // the struct:
2471 expect(Vec3.dot(v1, v2) == 0.0);
2472 try expect(Vec3.dot(v1, v2) == 0.0);
24722473}
24732474
24742475// Structs can have global declarations.
......@@ -2477,8 +2478,8 @@ const Empty = struct {
24772478 pub const PI = 3.14;
24782479};
24792480test "struct namespaced variable" {
2480 expect(Empty.PI == 3.14);
2481 expect(@sizeOf(Empty) == 0);
2481 try expect(Empty.PI == 3.14);
2482 try expect(@sizeOf(Empty) == 0);
24822483
24832484 // you can still instantiate an empty struct
24842485 const does_nothing = Empty {};
......@@ -2496,7 +2497,7 @@ test "field parent pointer" {
24962497 .y = 0.5678,
24972498 };
24982499 setYBasedOnX(&point.x, 0.9);
2499 expect(point.y == 0.9);
2500 try expect(point.y == 0.9);
25002501}
25012502
25022503// You can return a struct from a function. This is how we do generics
......@@ -2518,19 +2519,19 @@ fn LinkedList(comptime T: type) type {
25182519test "linked list" {
25192520 // Functions called at compile-time are memoized. This means you can
25202521 // do this:
2521 expect(LinkedList(i32) == LinkedList(i32));
2522 try expect(LinkedList(i32) == LinkedList(i32));
25222523
25232524 var list = LinkedList(i32) {
25242525 .first = null,
25252526 .last = null,
25262527 .len = 0,
25272528 };
2528 expect(list.len == 0);
2529 try expect(list.len == 0);
25292530
25302531 // Since types are first class values you can instantiate the type
25312532 // by assigning it to a variable:
25322533 const ListOfInts = LinkedList(i32);
2533 expect(ListOfInts == LinkedList(i32));
2534 try expect(ListOfInts == LinkedList(i32));
25342535
25352536 var node = ListOfInts.Node {
25362537 .prev = null,
......@@ -2542,7 +2543,7 @@ test "linked list" {
25422543 .last = &node,
25432544 .len = 1,
25442545 };
2545 expect(list2.first.?.data == 1234);
2546 try expect(list2.first.?.data == 1234);
25462547}
25472548 {#code_end#}
25482549
......@@ -2615,25 +2616,25 @@ const Divided = packed struct {
26152616};
26162617
26172618test "@bitCast between packed structs" {
2618 doTheTest();
2619 comptime doTheTest();
2619 try doTheTest();
2620 comptime try doTheTest();
26202621}
26212622
2622fn doTheTest() void {
2623 expect(@sizeOf(Full) == 2);
2624 expect(@sizeOf(Divided) == 2);
2623fn doTheTest() !void {
2624 try expect(@sizeOf(Full) == 2);
2625 try expect(@sizeOf(Divided) == 2);
26252626 var full = Full{ .number = 0x1234 };
26262627 var divided = @bitCast(Divided, full);
26272628 switch (builtin.endian) {
26282629 .Big => {
2629 expect(divided.half1 == 0x12);
2630 expect(divided.quarter3 == 0x3);
2631 expect(divided.quarter4 == 0x4);
2630 try expect(divided.half1 == 0x12);
2631 try expect(divided.quarter3 == 0x3);
2632 try expect(divided.quarter4 == 0x4);
26322633 },
26332634 .Little => {
2634 expect(divided.half1 == 0x34);
2635 expect(divided.quarter3 == 0x2);
2636 expect(divided.quarter4 == 0x1);
2635 try expect(divided.half1 == 0x34);
2636 try expect(divided.quarter3 == 0x2);
2637 try expect(divided.quarter4 == 0x1);
26372638 },
26382639 }
26392640}
......@@ -2659,7 +2660,7 @@ var foo = BitField{
26592660
26602661test "pointer to non-byte-aligned field" {
26612662 const ptr = &foo.b;
2662 expect(ptr.* == 2);
2663 try expect(ptr.* == 2);
26632664}
26642665 {#code_end#}
26652666 <p>
......@@ -2683,7 +2684,7 @@ var bit_field = BitField{
26832684};
26842685
26852686test "pointer to non-bit-aligned field" {
2686 expect(bar(&bit_field.b) == 2);
2687 try expect(bar(&bit_field.b) == 2);
26872688}
26882689
26892690fn bar(x: *const u3) u3 {
......@@ -2714,8 +2715,8 @@ var bit_field = BitField{
27142715};
27152716
27162717test "pointer to non-bit-aligned field" {
2717 expect(@ptrToInt(&bit_field.a) == @ptrToInt(&bit_field.b));
2718 expect(@ptrToInt(&bit_field.a) == @ptrToInt(&bit_field.c));
2718 try expect(@ptrToInt(&bit_field.a) == @ptrToInt(&bit_field.b));
2719 try expect(@ptrToInt(&bit_field.a) == @ptrToInt(&bit_field.c));
27192720}
27202721 {#code_end#}
27212722 <p>
......@@ -2733,13 +2734,13 @@ const BitField = packed struct {
27332734
27342735test "pointer to non-bit-aligned field" {
27352736 comptime {
2736 expect(@bitOffsetOf(BitField, "a") == 0);
2737 expect(@bitOffsetOf(BitField, "b") == 3);
2738 expect(@bitOffsetOf(BitField, "c") == 6);
2737 try expect(@bitOffsetOf(BitField, "a") == 0);
2738 try expect(@bitOffsetOf(BitField, "b") == 3);
2739 try expect(@bitOffsetOf(BitField, "c") == 6);
27392740
2740 expect(@byteOffsetOf(BitField, "a") == 0);
2741 expect(@byteOffsetOf(BitField, "b") == 0);
2742 expect(@byteOffsetOf(BitField, "c") == 0);
2741 try expect(@byteOffsetOf(BitField, "a") == 0);
2742 try expect(@byteOffsetOf(BitField, "b") == 0);
2743 try expect(@byteOffsetOf(BitField, "c") == 0);
27432744 }
27442745}
27452746 {#code_end#}
......@@ -2776,9 +2777,9 @@ test "aligned struct fields" {
27762777 };
27772778 var foo = S{ .a = 1, .b = 2 };
27782779
2779 expectEqual(64, @alignOf(S));
2780 expectEqual(*align(2) u32, @TypeOf(&foo.a));
2781 expectEqual(*align(64) u32, @TypeOf(&foo.b));
2780 try expectEqual(64, @alignOf(S));
2781 try expectEqual(*align(2) u32, @TypeOf(&foo.a));
2782 try expectEqual(*align(64) u32, @TypeOf(&foo.b));
27822783}
27832784 {#code_end#}
27842785 <p>
......@@ -2834,8 +2835,8 @@ test "anonymous struct literal" {
28342835 .x = 13,
28352836 .y = 67,
28362837 };
2837 expect(pt.x == 13);
2838 expect(pt.y == 67);
2838 try expect(pt.x == 13);
2839 try expect(pt.y == 67);
28392840}
28402841 {#code_end#}
28412842 <p>
......@@ -2847,7 +2848,7 @@ const std = @import("std");
28472848const expect = std.testing.expect;
28482849
28492850test "fully anonymous struct" {
2850 dump(.{
2851 try dump(.{
28512852 .int = @as(u32, 1234),
28522853 .float = @as(f64, 12.34),
28532854 .b = true,
......@@ -2855,12 +2856,12 @@ test "fully anonymous struct" {
28552856 });
28562857}
28572858
2858fn dump(args: anytype) void {
2859 expect(args.int == 1234);
2860 expect(args.float == 12.34);
2861 expect(args.b);
2862 expect(args.s[0] == 'h');
2863 expect(args.s[1] == 'i');
2859fn dump(args: anytype) !void {
2860 try expect(args.int == 1234);
2861 try expect(args.float == 12.34);
2862 try expect(args.b);
2863 try expect(args.s[0] == 'h');
2864 try expect(args.s[1] == 'i');
28642865}
28652866 {#code_end#}
28662867 <p>
......@@ -2884,14 +2885,14 @@ test "tuple" {
28842885 true,
28852886 "hi",
28862887 } ++ .{false} ** 2;
2887 expect(values[0] == 1234);
2888 expect(values[4] == false);
2888 try expect(values[0] == 1234);
2889 try expect(values[4] == false);
28892890 inline for (values) |v, i| {
28902891 if (i != 2) continue;
2891 expect(v);
2892 try expect(v);
28922893 }
2893 expect(values.len == 6);
2894 expect(values.@"3"[0] == 'h');
2894 try expect(values.len == 6);
2895 try expect(values.@"3"[0] == 'h');
28952896}
28962897 {#code_end#}
28972898 {#header_close#}
......@@ -2922,9 +2923,9 @@ const Value = enum(u2) {
29222923// Now you can cast between u2 and Value.
29232924// The ordinal value starts from 0, counting up for each member.
29242925test "enum ordinal value" {
2925 expect(@enumToInt(Value.zero) == 0);
2926 expect(@enumToInt(Value.one) == 1);
2927 expect(@enumToInt(Value.two) == 2);
2926 try expect(@enumToInt(Value.zero) == 0);
2927 try expect(@enumToInt(Value.one) == 1);
2928 try expect(@enumToInt(Value.two) == 2);
29282929}
29292930
29302931// You can override the ordinal value for an enum.
......@@ -2934,9 +2935,9 @@ const Value2 = enum(u32) {
29342935 million = 1000000,
29352936};
29362937test "set enum ordinal value" {
2937 expect(@enumToInt(Value2.hundred) == 100);
2938 expect(@enumToInt(Value2.thousand) == 1000);
2939 expect(@enumToInt(Value2.million) == 1000000);
2938 try expect(@enumToInt(Value2.hundred) == 100);
2939 try expect(@enumToInt(Value2.thousand) == 1000);
2940 try expect(@enumToInt(Value2.million) == 1000000);
29402941}
29412942
29422943// Enums can have methods, the same as structs and unions.
......@@ -2954,7 +2955,7 @@ const Suit = enum {
29542955};
29552956test "enum method" {
29562957 const p = Suit.spades;
2957 expect(!p.isClubs());
2958 try expect(!p.isClubs());
29582959}
29592960
29602961// An enum variant of different types can be switched upon.
......@@ -2970,7 +2971,7 @@ test "enum variant switch" {
29702971 Foo.number => "this is a number",
29712972 Foo.none => "this is a none",
29722973 };
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"));
29742975}
29752976
29762977// @typeInfo can be used to access the integer tag type of an enum.
......@@ -2981,18 +2982,18 @@ const Small = enum {
29812982 four,
29822983};
29832984test "std.meta.Tag" {
2984 expect(@typeInfo(Small).Enum.tag_type == u2);
2985 try expect(@typeInfo(Small).Enum.tag_type == u2);
29852986}
29862987
29872988// @typeInfo tells us the field count and the fields names:
29882989test "@typeInfo" {
2989 expect(@typeInfo(Small).Enum.fields.len == 4);
2990 expect(mem.eql(u8, @typeInfo(Small).Enum.fields[1].name, "two"));
2990 try expect(@typeInfo(Small).Enum.fields.len == 4);
2991 try expect(mem.eql(u8, @typeInfo(Small).Enum.fields[1].name, "two"));
29912992}
29922993
29932994// @tagName gives a []const u8 representation of an enum value:
29942995test "@tagName" {
2995 expect(mem.eql(u8, @tagName(Small.three), "three"));
2996 try expect(mem.eql(u8, @tagName(Small.three), "three"));
29962997}
29972998 {#code_end#}
29982999 {#see_also|@typeInfo|@tagName|@sizeOf#}
......@@ -3027,7 +3028,7 @@ test "packed enum" {
30273028 two,
30283029 three,
30293030 };
3030 std.testing.expect(@sizeOf(Number) == @sizeOf(u8));
3031 try std.testing.expect(@sizeOf(Number) == @sizeOf(u8));
30313032}
30323033 {#code_end#}
30333034 <p>This makes the enum eligible to be in a {#link|packed struct#}.</p>
......@@ -3050,7 +3051,7 @@ const Color = enum {
30503051test "enum literals" {
30513052 const color1: Color = .auto;
30523053 const color2 = Color.auto;
3053 expect(color1 == color2);
3054 try expect(color1 == color2);
30543055}
30553056
30563057test "switch using enum literals" {
......@@ -3060,7 +3061,7 @@ test "switch using enum literals" {
30603061 .on => true,
30613062 .off => false,
30623063 };
3063 expect(result);
3064 try expect(result);
30643065}
30653066 {#code_end#}
30663067 {#header_close#}
......@@ -3096,12 +3097,12 @@ test "switch on non-exhaustive enum" {
30963097 .three => false,
30973098 _ => false,
30983099 };
3099 expect(result);
3100 try expect(result);
31003101 const is_one = switch (number) {
31013102 .one => true,
31023103 else => false,
31033104 };
3104 expect(is_one);
3105 try expect(is_one);
31053106}
31063107 {#code_end#}
31073108 {#header_close#}
......@@ -3141,9 +3142,9 @@ const Payload = union {
31413142};
31423143test "simple union" {
31433144 var payload = Payload{ .int = 1234 };
3144 expect(payload.int == 1234);
3145 try expect(payload.int == 1234);
31453146 payload = Payload{ .float = 12.34 };
3146 expect(payload.float == 12.34);
3147 try expect(payload.float == 12.34);
31473148}
31483149 {#code_end#}
31493150 <p>
......@@ -3174,24 +3175,24 @@ const ComplexType = union(ComplexTypeTag) {
31743175
31753176test "switch on tagged union" {
31763177 const c = ComplexType{ .ok = 42 };
3177 expect(@as(ComplexTypeTag, c) == ComplexTypeTag.ok);
3178 try expect(@as(ComplexTypeTag, c) == ComplexTypeTag.ok);
31783179
31793180 switch (c) {
3180 ComplexTypeTag.ok => |value| expect(value == 42),
3181 ComplexTypeTag.ok => |value| try expect(value == 42),
31813182 ComplexTypeTag.not_ok => unreachable,
31823183 }
31833184}
31843185
31853186test "get tag type" {
3186 expect(std.meta.Tag(ComplexType) == ComplexTypeTag);
3187 try expect(std.meta.Tag(ComplexType) == ComplexTypeTag);
31873188}
31883189
31893190test "coerce to enum" {
31903191 const c1 = ComplexType{ .ok = 42 };
31913192 const c2 = ComplexType.not_ok;
31923193
3193 expect(c1 == .ok);
3194 expect(c2 == .not_ok);
3194 try expect(c1 == .ok);
3195 try expect(c2 == .not_ok);
31953196}
31963197 {#code_end#}
31973198 <p>In order to modify the payload of a tagged union in a switch expression,
......@@ -3212,14 +3213,14 @@ const ComplexType = union(ComplexTypeTag) {
32123213
32133214test "modify tagged union in switch" {
32143215 var c = ComplexType{ .ok = 42 };
3215 expect(@as(ComplexTypeTag, c) == ComplexTypeTag.ok);
3216 try expect(@as(ComplexTypeTag, c) == ComplexTypeTag.ok);
32163217
32173218 switch (c) {
32183219 ComplexTypeTag.ok => |*value| value.* += 1,
32193220 ComplexTypeTag.not_ok => unreachable,
32203221 }
32213222
3222 expect(c.ok == 43);
3223 try expect(c.ok == 43);
32233224}
32243225 {#code_end#}
32253226 <p>
......@@ -3250,8 +3251,8 @@ test "union method" {
32503251 var v1 = Variant{ .int = 1 };
32513252 var v2 = Variant{ .boolean = false };
32523253
3253 expect(v1.truthy());
3254 expect(!v2.truthy());
3254 try expect(v1.truthy());
3255 try expect(!v2.truthy());
32553256}
32563257 {#code_end#}
32573258 <p>
......@@ -3268,7 +3269,7 @@ const Small2 = union(enum) {
32683269 c: u8,
32693270};
32703271test "@tagName" {
3271 expect(std.mem.eql(u8, @tagName(Small2.a), "a"));
3272 try expect(std.mem.eql(u8, @tagName(Small2.a), "a"));
32723273}
32733274 {#code_end#}
32743275 {#header_close#}
......@@ -3301,8 +3302,8 @@ const Number = union {
33013302test "anonymous union literal syntax" {
33023303 var i: Number = .{.int = 42};
33033304 var f = makeNumber();
3304 expect(i.int == 42);
3305 expect(f.float == 12.34);
3305 try expect(i.int == 42);
3306 try expect(f.float == 12.34);
33063307}
33073308
33083309fn makeNumber() Number {
......@@ -3364,8 +3365,8 @@ test "labeled break from labeled block expression" {
33643365 y += 1;
33653366 break :blk y;
33663367 };
3367 expect(x == 124);
3368 expect(y == 124);
3368 try expect(x == 124);
3369 try expect(y == 124);
33693370}
33703371 {#code_end#}
33713372 <p>Here, {#syntax#}blk{#endsyntax#} can be any name.</p>
......@@ -3443,7 +3444,7 @@ test "switch simple" {
34433444 else => 9,
34443445 };
34453446
3446 expect(b == 1);
3447 try expect(b == 1);
34473448}
34483449
34493450// Switch expressions can be used outside a function:
......@@ -3506,8 +3507,8 @@ test "switch on tagged union" {
35063507 Item.d => 8,
35073508 };
35083509
3509 expect(b == 6);
3510 expect(a.c.x == 2);
3510 try expect(b == 6);
3511 try expect(a.c.x == 2);
35113512}
35123513 {#code_end#}
35133514 {#see_also|comptime|enum|@compileError|Compile Variables#}
......@@ -3556,7 +3557,7 @@ test "enum literals with switch" {
35563557 .on => false,
35573558 .off => true,
35583559 };
3559 expect(result);
3560 try expect(result);
35603561}
35613562 {#code_end#}
35623563 {#header_close#}
......@@ -3575,7 +3576,7 @@ test "while basic" {
35753576 while (i < 10) {
35763577 i += 1;
35773578 }
3578 expect(i == 10);
3579 try expect(i == 10);
35793580}
35803581 {#code_end#}
35813582 <p>
......@@ -3591,7 +3592,7 @@ test "while break" {
35913592 break;
35923593 i += 1;
35933594 }
3594 expect(i == 10);
3595 try expect(i == 10);
35953596}
35963597 {#code_end#}
35973598 <p>
......@@ -3608,7 +3609,7 @@ test "while continue" {
36083609 continue;
36093610 break;
36103611 }
3611 expect(i == 10);
3612 try expect(i == 10);
36123613}
36133614 {#code_end#}
36143615 <p>
......@@ -3621,7 +3622,7 @@ const expect = @import("std").testing.expect;
36213622test "while loop continue expression" {
36223623 var i: usize = 0;
36233624 while (i < 10) : (i += 1) {}
3624 expect(i == 10);
3625 try expect(i == 10);
36253626}
36263627
36273628test "while loop continue expression, more complicated" {
......@@ -3629,7 +3630,7 @@ test "while loop continue expression, more complicated" {
36293630 var j: usize = 1;
36303631 while (i * j < 2000) : ({ i *= 2; j *= 3; }) {
36313632 const my_ij = i * j;
3632 expect(my_ij < 2000);
3633 try expect(my_ij < 2000);
36333634 }
36343635}
36353636 {#code_end#}
......@@ -3648,8 +3649,8 @@ test "while loop continue expression, more complicated" {
36483649const expect = @import("std").testing.expect;
36493650
36503651test "while else" {
3651 expect(rangeHasNumber(0, 10, 5));
3652 expect(!rangeHasNumber(0, 10, 15));
3652 try expect(rangeHasNumber(0, 10, 5));
3653 try expect(!rangeHasNumber(0, 10, 15));
36533654}
36543655
36553656fn rangeHasNumber(begin: usize, end: usize, number: usize) bool {
......@@ -3706,14 +3707,14 @@ test "while null capture" {
37063707 while (eventuallyNullSequence()) |value| {
37073708 sum1 += value;
37083709 }
3709 expect(sum1 == 3);
3710 try expect(sum1 == 3);
37103711
37113712 var sum2: u32 = 0;
37123713 numbers_left = 3;
37133714 while (eventuallyNullSequence()) |value| {
37143715 sum2 += value;
37153716 } else {
3716 expect(sum2 == 3);
3717 try expect(sum2 == 3);
37173718 }
37183719}
37193720
......@@ -3748,7 +3749,7 @@ test "while error union capture" {
37483749 while (eventuallyErrorSequence()) |value| {
37493750 sum1 += value;
37503751 } else |err| {
3751 expect(err == error.ReachedZero);
3752 try expect(err == error.ReachedZero);
37523753 }
37533754}
37543755
......@@ -3784,7 +3785,7 @@ test "inline while loop" {
37843785 };
37853786 sum += typeNameLength(T);
37863787 }
3787 expect(sum == 9);
3788 try expect(sum == 9);
37883789}
37893790
37903791fn typeNameLength(comptime T: type) usize {
......@@ -3819,22 +3820,22 @@ test "for basics" {
38193820 }
38203821 sum += value;
38213822 }
3822 expect(sum == 16);
3823 try expect(sum == 16);
38233824
38243825 // To iterate over a portion of a slice, reslice.
38253826 for (items[0..1]) |value| {
38263827 sum += value;
38273828 }
3828 expect(sum == 20);
3829 try expect(sum == 20);
38293830
38303831 // To access the index of iteration, specify a second capture value.
38313832 // This is zero-indexed.
38323833 var sum2: i32 = 0;
38333834 for (items) |value, i| {
3834 expect(@TypeOf(i) == usize);
3835 try expect(@TypeOf(i) == usize);
38353836 sum2 += @intCast(i32, i);
38363837 }
3837 expect(sum2 == 10);
3838 try expect(sum2 == 10);
38383839}
38393840
38403841test "for reference" {
......@@ -3846,9 +3847,9 @@ test "for reference" {
38463847 value.* += 1;
38473848 }
38483849
3849 expect(items[0] == 4);
3850 expect(items[1] == 5);
3851 expect(items[2] == 3);
3850 try expect(items[0] == 4);
3851 try expect(items[1] == 5);
3852 try expect(items[2] == 3);
38523853}
38533854
38543855test "for else" {
......@@ -3863,10 +3864,10 @@ test "for else" {
38633864 sum += value.?;
38643865 }
38653866 } else blk: {
3866 expect(sum == 12);
3867 try expect(sum == 12);
38673868 break :blk sum;
38683869 };
3869 expect(result == 12);
3870 try expect(result == 12);
38703871}
38713872 {#code_end#}
38723873 {#header_open|Labeled for#}
......@@ -3884,7 +3885,7 @@ test "nested break" {
38843885 break :outer;
38853886 }
38863887 }
3887 expect(count == 1);
3888 try expect(count == 1);
38883889}
38893890
38903891test "nested continue" {
......@@ -3896,7 +3897,7 @@ test "nested continue" {
38963897 }
38973898 }
38983899
3899 expect(count == 8);
3900 try expect(count == 8);
39003901}
39013902 {#code_end#}
39023903 {#header_close#}
......@@ -3923,7 +3924,7 @@ test "inline for loop" {
39233924 };
39243925 sum += typeNameLength(T);
39253926 }
3926 expect(sum == 9);
3927 try expect(sum == 9);
39273928}
39283929
39293930fn typeNameLength(comptime T: type) usize {
......@@ -3956,7 +3957,7 @@ test "if expression" {
39563957 const a: u32 = 5;
39573958 const b: u32 = 4;
39583959 const result = if (a != b) 47 else 3089;
3959 expect(result == 47);
3960 try expect(result == 47);
39603961}
39613962
39623963test "if boolean" {
......@@ -3964,7 +3965,7 @@ test "if boolean" {
39643965 const a: u32 = 5;
39653966 const b: u32 = 4;
39663967 if (a != b) {
3967 expect(true);
3968 try expect(true);
39683969 } else if (a == 9) {
39693970 unreachable;
39703971 } else {
......@@ -3977,7 +3978,7 @@ test "if optional" {
39773978
39783979 const a: ?u32 = 0;
39793980 if (a) |value| {
3980 expect(value == 0);
3981 try expect(value == 0);
39813982 } else {
39823983 unreachable;
39833984 }
......@@ -3986,17 +3987,17 @@ test "if optional" {
39863987 if (b) |value| {
39873988 unreachable;
39883989 } else {
3989 expect(true);
3990 try expect(true);
39903991 }
39913992
39923993 // The else is not required.
39933994 if (a) |value| {
3994 expect(value == 0);
3995 try expect(value == 0);
39953996 }
39963997
39973998 // To test against null only, use the binary equality operator.
39983999 if (b == null) {
3999 expect(true);
4000 try expect(true);
40004001 }
40014002
40024003 // Access the value by reference using a pointer capture.
......@@ -4006,7 +4007,7 @@ test "if optional" {
40064007 }
40074008
40084009 if (c) |value| {
4009 expect(value == 2);
4010 try expect(value == 2);
40104011 } else {
40114012 unreachable;
40124013 }
......@@ -4018,7 +4019,7 @@ test "if error union" {
40184019
40194020 const a: anyerror!u32 = 0;
40204021 if (a) |value| {
4021 expect(value == 0);
4022 try expect(value == 0);
40224023 } else |err| {
40234024 unreachable;
40244025 }
......@@ -4027,17 +4028,17 @@ test "if error union" {
40274028 if (b) |value| {
40284029 unreachable;
40294030 } else |err| {
4030 expect(err == error.BadValue);
4031 try expect(err == error.BadValue);
40314032 }
40324033
40334034 // The else and |err| capture is strictly required.
40344035 if (a) |value| {
4035 expect(value == 0);
4036 try expect(value == 0);
40364037 } else |_| {}
40374038
40384039 // To check only the error value, use an empty block expression.
40394040 if (b) |_| {} else |err| {
4040 expect(err == error.BadValue);
4041 try expect(err == error.BadValue);
40414042 }
40424043
40434044 // Access the value by reference using a pointer capture.
......@@ -4049,7 +4050,7 @@ test "if error union" {
40494050 }
40504051
40514052 if (c) |value| {
4052 expect(value == 9);
4053 try expect(value == 9);
40534054 } else |err| {
40544055 unreachable;
40554056 }
......@@ -4061,14 +4062,14 @@ test "if error union with optional" {
40614062
40624063 const a: anyerror!?u32 = 0;
40634064 if (a) |optional_value| {
4064 expect(optional_value.? == 0);
4065 try expect(optional_value.? == 0);
40654066 } else |err| {
40664067 unreachable;
40674068 }
40684069
40694070 const b: anyerror!?u32 = null;
40704071 if (b) |optional_value| {
4071 expect(optional_value == null);
4072 try expect(optional_value == null);
40724073 } else |err| {
40734074 unreachable;
40744075 }
......@@ -4077,7 +4078,7 @@ test "if error union with optional" {
40774078 if (c) |optional_value| {
40784079 unreachable;
40794080 } else |err| {
4080 expect(err == error.BadValue);
4081 try expect(err == error.BadValue);
40814082 }
40824083
40834084 // Access the value by reference by using a pointer capture each time.
......@@ -4091,7 +4092,7 @@ test "if error union with optional" {
40914092 }
40924093
40934094 if (d) |optional_value| {
4094 expect(optional_value.? == 9);
4095 try expect(optional_value.? == 9);
40954096 } else |err| {
40964097 unreachable;
40974098 }
......@@ -4106,21 +4107,21 @@ const expect = std.testing.expect;
41064107const print = std.debug.print;
41074108
41084109// defer will execute an expression at the end of the current scope.
4109fn deferExample() usize {
4110fn deferExample() !usize {
41104111 var a: usize = 1;
41114112
41124113 {
41134114 defer a = 2;
41144115 a = 1;
41154116 }
4116 expect(a == 2);
4117 try expect(a == 2);
41174118
41184119 a = 5;
41194120 return a;
41204121}
41214122
41224123test "defer basics" {
4123 expect(deferExample() == 5);
4124 try expect((try deferExample()) == 5);
41244125}
41254126
41264127// 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
42584259
42594260test "foo" {
42604261 const value = bar() catch ExitProcess(1);
4261 expect(value == 1234);
4262 try expect(value == 1234);
42624263}
42634264
42644265fn bar() anyerror!u32 {
......@@ -4321,17 +4322,17 @@ fn do_op(fn_call: call2_op, op1: i8, op2: i8) i8 {
43214322}
43224323
43234324test "function" {
4324 expect(do_op(add, 5, 6) == 11);
4325 expect(do_op(sub2, 5, 6) == -1);
4325 try expect(do_op(add, 5, 6) == 11);
4326 try expect(do_op(sub2, 5, 6) == -1);
43264327}
43274328 {#code_end#}
43284329 <p>Function values are like pointers:</p>
43294330 {#code_begin|obj#}
4330const expect = @import("std").testing.expect;
4331const assert = @import("std").debug.assert;
43314332
43324333comptime {
4333 expect(@TypeOf(foo) == fn()void);
4334 expect(@sizeOf(fn()void) == @sizeOf(?fn()void));
4334 assert(@TypeOf(foo) == fn()void);
4335 assert(@sizeOf(fn()void) == @sizeOf(?fn()void));
43354336}
43364337
43374338fn foo() void { }
......@@ -4366,7 +4367,7 @@ fn foo(point: Point) i32 {
43664367const expect = @import("std").testing.expect;
43674368
43684369test "pass struct to function" {
4369 expect(foo(Point{ .x = 1, .y = 2 }) == 3);
4370 try expect(foo(Point{ .x = 1, .y = 2 }) == 3);
43704371}
43714372 {#code_end#}
43724373 <p>
......@@ -4387,11 +4388,11 @@ fn addFortyTwo(x: anytype) @TypeOf(x) {
43874388}
43884389
43894390test "fn type inference" {
4390 expect(addFortyTwo(1) == 43);
4391 expect(@TypeOf(addFortyTwo(1)) == comptime_int);
4391 try expect(addFortyTwo(1) == 43);
4392 try expect(@TypeOf(addFortyTwo(1)) == comptime_int);
43924393 var y: i64 = 2;
4393 expect(addFortyTwo(y) == 44);
4394 expect(@TypeOf(addFortyTwo(y)) == i64);
4394 try expect(addFortyTwo(y) == 44);
4395 try expect(@TypeOf(addFortyTwo(y)) == i64);
43954396}
43964397 {#code_end#}
43974398
......@@ -4401,8 +4402,8 @@ test "fn type inference" {
44014402const expect = @import("std").testing.expect;
44024403
44034404test "fn reflection" {
4404 expect(@typeInfo(@TypeOf(expect)).Fn.return_type.? == void);
4405 expect(@typeInfo(@TypeOf(expect)).Fn.is_var_args == false);
4405 try expect(@typeInfo(@TypeOf(expect)).Fn.args[0].arg_type.? == bool);
4406 try expect(@typeInfo(@TypeOf(expect)).Fn.is_var_args == false);
44064407}
44074408 {#code_end#}
44084409 {#header_close#}
......@@ -4437,7 +4438,7 @@ const AllocationError = error {
44374438
44384439test "coerce subset to superset" {
44394440 const err = foo(AllocationError.OutOfMemory);
4440 std.testing.expect(err == FileOpenError.OutOfMemory);
4441 try std.testing.expect(err == FileOpenError.OutOfMemory);
44414442}
44424443
44434444fn foo(err: AllocationError) FileOpenError {
......@@ -4545,7 +4546,7 @@ fn charToDigit(c: u8) u8 {
45454546
45464547test "parse u64" {
45474548 const result = try parseU64("1234", 10);
4548 std.testing.expect(result == 1234);
4549 try std.testing.expect(result == 1234);
45494550}
45504551 {#code_end#}
45514552 <p>
......@@ -4702,10 +4703,10 @@ test "error union" {
47024703 foo = error.SomeError;
47034704
47044705 // 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
47074708 // 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);
47094710}
47104711 {#code_end#}
47114712 {#header_open|Merging Error Sets#}
......@@ -5082,7 +5083,7 @@ test "optional type" {
50825083 foo = 1234;
50835084
50845085 // 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);
50865087}
50875088 {#code_end#}
50885089 {#header_close#}
......@@ -5109,11 +5110,11 @@ test "optional pointers" {
51095110 var x: i32 = 1;
51105111 ptr = &x;
51115112
5112 expect(ptr.?.* == 1);
5113 try expect(ptr.?.* == 1);
51135114
51145115 // Optional pointers are the same size as normal pointers, because pointer
51155116 // value 0 is used as the null value.
5116 expect(@sizeOf(?*i32) == @sizeOf(*i32));
5117 try expect(@sizeOf(?*i32) == @sizeOf(*i32));
51175118}
51185119 {#code_end#}
51195120 {#header_close#}
......@@ -5186,7 +5187,7 @@ const mem = std.mem;
51865187test "cast *[1][*]const u8 to [*]const ?[*]const u8" {
51875188 const window_name = [1][*]const u8{"window name"};
51885189 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"));
51905191}
51915192 {#code_end#}
51925193 {#header_close#}
......@@ -5207,13 +5208,13 @@ test "integer widening" {
52075208 var d: u64 = c;
52085209 var e: u64 = d;
52095210 var f: u128 = e;
5210 expect(f == a);
5211 try expect(f == a);
52115212}
52125213
52135214test "implicit unsigned integer to signed integer" {
52145215 var a: u8 = 250;
52155216 var b: i16 = a;
5216 expect(b == 250);
5217 try expect(b == 250);
52175218}
52185219
52195220test "float widening" {
......@@ -5225,7 +5226,7 @@ test "float widening" {
52255226 var b: f32 = a;
52265227 var c: f64 = b;
52275228 var d: f128 = c;
5228 expect(d == a);
5229 try expect(d == a);
52295230}
52305231 {#code_end#}
52315232 {#header_close#}
......@@ -5257,48 +5258,48 @@ const expect = std.testing.expect;
52575258test "[N]T to []const T" {
52585259 var x1: []const u8 = "hello";
52595260 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
52625263 var y: []const f32 = &[2]f32{ 1.2, 3.4 };
5263 expect(y[0] == 1.2);
5264 try expect(y[0] == 1.2);
52645265}
52655266
52665267// Likewise, it works when the destination type is an error union.
52675268test "[N]T to E![]const T" {
52685269 var x1: anyerror![]const u8 = "hello";
52695270 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
52725273 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);
52745275}
52755276
52765277// Likewise, it works when the destination type is an optional.
52775278test "[N]T to ?[]const T" {
52785279 var x1: ?[]const u8 = "hello";
52795280 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
52825283 var y: ?[]const f32 = &[2]f32{ 1.2, 3.4 };
5283 expect(y.?[0] == 1.2);
5284 try expect(y.?[0] == 1.2);
52845285}
52855286
52865287// In this cast, the array length becomes the slice length.
52875288test "*[N]T to []T" {
52885289 var buf: [5]u8 = "hello".*;
52895290 const x: []u8 = &buf;
5290 expect(std.mem.eql(u8, x, "hello"));
5291 try expect(std.mem.eql(u8, x, "hello"));
52915292
52925293 const buf2 = [2]f32{ 1.2, 3.4 };
52935294 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 }));
52955296}
52965297
52975298// Single-item pointers to arrays can be coerced to many-item pointers.
52985299test "*[N]T to [*]T" {
52995300 var buf: [5]u8 = "hello".*;
53005301 const x: [*]u8 = &buf;
5301 expect(x[4] == 'o');
5302 try expect(x[4] == 'o');
53025303 // x[5] would be an uncaught out of bounds pointer dereference!
53035304}
53045305
......@@ -5306,7 +5307,7 @@ test "*[N]T to [*]T" {
53065307test "*[N]T to ?[*]T" {
53075308 var buf: [5]u8 = "hello".*;
53085309 const x: ?[*]u8 = &buf;
5309 expect(x.?[4] == 'o');
5310 try expect(x.?[4] == 'o');
53105311}
53115312
53125313// Single-item pointers can be cast to len-1 single-item arrays.
......@@ -5314,7 +5315,7 @@ test "*T to *[1]T" {
53145315 var x: i32 = 1234;
53155316 const y: *[1]i32 = &x;
53165317 const z: [*]i32 = y;
5317 expect(z[0] == 1234);
5318 try expect(z[0] == 1234);
53185319}
53195320 {#code_end#}
53205321 {#see_also|C Pointers#}
......@@ -5331,8 +5332,8 @@ test "coerce to optionals" {
53315332 const x: ?i32 = 1234;
53325333 const y: ?i32 = null;
53335334
5334 expect(x.? == 1234);
5335 expect(y == null);
5335 try expect(x.? == 1234);
5336 try expect(y == null);
53365337}
53375338 {#code_end#}
53385339 <p>It works nested inside the {#link|Error Union Type#}, too:</p>
......@@ -5344,8 +5345,8 @@ test "coerce to optionals wrapped in error union" {
53445345 const x: anyerror!?i32 = 1234;
53455346 const y: anyerror!?i32 = null;
53465347
5347 expect((try x).? == 1234);
5348 expect((try y) == null);
5348 try expect((try x).? == 1234);
5349 try expect((try y) == null);
53495350}
53505351 {#code_end#}
53515352 {#header_close#}
......@@ -5361,8 +5362,8 @@ test "coercion to error unions" {
53615362 const x: anyerror!i32 = 1234;
53625363 const y: anyerror!i32 = error.Failure;
53635364
5364 expect((try x) == 1234);
5365 std.testing.expectError(error.Failure, y);
5365 try expect((try x) == 1234);
5366 try std.testing.expectError(error.Failure, y);
53665367}
53675368 {#code_end#}
53685369 {#header_close#}
......@@ -5377,7 +5378,7 @@ const expect = std.testing.expect;
53775378test "coercing large integer type to smaller one when value is comptime known to fit" {
53785379 const x: u64 = 255;
53795380 const y: u8 = x;
5380 expect(y == 255);
5381 try expect(y == 255);
53815382}
53825383 {#code_end#}
53835384 {#header_close#}
......@@ -5405,11 +5406,11 @@ const U = union(E) {
54055406test "coercion between unions and enums" {
54065407 var u = U{ .two = 12.34 };
54075408 var e: E = u;
5408 expect(e == E.two);
5409 try expect(e == E.two);
54095410
54105411 const three = E.three;
54115412 var another_u: U = three;
5412 expect(another_u == E.three);
5413 try expect(another_u == E.three);
54135414}
54145415 {#code_end#}
54155416 {#see_also|union|enum#}
......@@ -5482,37 +5483,37 @@ test "peer resolve int widening" {
54825483 var a: i8 = 12;
54835484 var b: i16 = 34;
54845485 var c = a + b;
5485 expect(c == 46);
5486 expect(@TypeOf(c) == i16);
5486 try expect(c == 46);
5487 try expect(@TypeOf(c) == i16);
54875488}
54885489
54895490test "peer resolve arrays of different size to const slice" {
5490 expect(mem.eql(u8, boolToStr(true), "true"));
5491 expect(mem.eql(u8, boolToStr(false), "false"));
5492 comptime expect(mem.eql(u8, boolToStr(true), "true"));
5493 comptime expect(mem.eql(u8, boolToStr(false), "false"));
5491 try expect(mem.eql(u8, boolToStr(true), "true"));
5492 try expect(mem.eql(u8, boolToStr(false), "false"));
5493 comptime try expect(mem.eql(u8, boolToStr(true), "true"));
5494 comptime try expect(mem.eql(u8, boolToStr(false), "false"));
54945495}
54955496fn boolToStr(b: bool) []const u8 {
54965497 return if (b) "true" else "false";
54975498}
54985499
54995500test "peer resolve array and const slice" {
5500 testPeerResolveArrayConstSlice(true);
5501 comptime testPeerResolveArrayConstSlice(true);
5501 try testPeerResolveArrayConstSlice(true);
5502 comptime try testPeerResolveArrayConstSlice(true);
55025503}
5503fn testPeerResolveArrayConstSlice(b: bool) void {
5504fn testPeerResolveArrayConstSlice(b: bool) !void {
55045505 const value1 = if (b) "aoeu" else @as([]const u8, "zz");
55055506 const value2 = if (b) @as([]const u8, "zz") else "aoeu";
5506 expect(mem.eql(u8, value1, "aoeu"));
5507 expect(mem.eql(u8, value2, "zz"));
5507 try expect(mem.eql(u8, value1, "aoeu"));
5508 try expect(mem.eql(u8, value2, "zz"));
55085509}
55095510
55105511test "peer type resolution: ?T and T" {
5511 expect(peerTypeTAndOptionalT(true, false).? == 0);
5512 expect(peerTypeTAndOptionalT(false, false).? == 3);
5512 try expect(peerTypeTAndOptionalT(true, false).? == 0);
5513 try expect(peerTypeTAndOptionalT(false, false).? == 3);
55135514 comptime {
5514 expect(peerTypeTAndOptionalT(true, false).? == 0);
5515 expect(peerTypeTAndOptionalT(false, false).? == 3);
5515 try expect(peerTypeTAndOptionalT(true, false).? == 0);
5516 try expect(peerTypeTAndOptionalT(false, false).? == 3);
55165517 }
55175518}
55185519fn peerTypeTAndOptionalT(c: bool, b: bool) ?usize {
......@@ -5524,11 +5525,11 @@ fn peerTypeTAndOptionalT(c: bool, b: bool) ?usize {
55245525}
55255526
55265527test "peer type resolution: *[0]u8 and []const u8" {
5527 expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);
5528 expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);
5528 try expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);
5529 try expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);
55295530 comptime {
5530 expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);
5531 expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);
5531 try expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);
5532 try expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);
55325533 }
55335534}
55345535fn peerTypeEmptyArrayAndSlice(a: bool, slice: []const u8) []const u8 {
......@@ -5542,14 +5543,14 @@ test "peer type resolution: *[0]u8, []const u8, and anyerror![]u8" {
55425543 {
55435544 var data = "hi".*;
55445545 const slice = data[0..];
5545 expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0);
5546 expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1);
5546 try expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0);
5547 try expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1);
55475548 }
55485549 comptime {
55495550 var data = "hi".*;
55505551 const slice = data[0..];
5551 expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0);
5552 expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1);
5552 try expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0);
5553 try expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1);
55535554 }
55545555}
55555556fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) anyerror![]u8 {
......@@ -5563,8 +5564,8 @@ fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) anyerror![]u8 {
55635564test "peer type resolution: *const T and ?*T" {
55645565 const a = @intToPtr(*const usize, 0x123456780);
55655566 const b = @intToPtr(?*usize, 0x123456780);
5566 expect(a == b);
5567 expect(b == a);
5567 try expect(a == b);
5568 try expect(b == a);
55685569}
55695570 {#code_end#}
55705571 {#header_close#}
......@@ -5620,11 +5621,11 @@ test "turn HashMap into a set with void" {
56205621 try map.put(1, {});
56215622 try map.put(2, {});
56225623
5623 expect(map.contains(2));
5624 expect(!map.contains(3));
5624 try expect(map.contains(2));
5625 try expect(!map.contains(3));
56255626
56265627 _ = map.remove(2);
5627 expect(!map.contains(2));
5628 try expect(!map.contains(2));
56285629}
56295630 {#code_end#}
56305631 <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" {
56795680 var b = Empty{};
56805681 var ptr_a = &a;
56815682 var ptr_b = &b;
5682 comptime expect(ptr_a == ptr_b);
5683 comptime try expect(ptr_a == ptr_b);
56835684}
56845685 {#code_end#}
56855686 <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" {
57145715usingnamespace @import("std");
57155716
57165717test "using std namespace" {
5717 testing.expect(true);
5718 try testing.expect(true);
57185719}
57195720 {#code_end#}
57205721 <p>
......@@ -5826,7 +5827,7 @@ fn max(comptime T: type, a: T, b: T) T {
58265827 }
58275828}
58285829test "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);
58305831}
58315832 {#code_end#}
58325833 <p>
......@@ -5894,9 +5895,9 @@ fn performFn(comptime prefix_char: u8, start_value: i32) i32 {
58945895}
58955896
58965897test "perform fn" {
5897 expect(performFn('t', 1) == 6);
5898 expect(performFn('o', 0) == 1);
5899 expect(performFn('w', 99) == 99);
5898 try expect(performFn('t', 1) == 6);
5899 try expect(performFn('o', 0) == 1);
5900 try expect(performFn('w', 99) == 99);
59005901}
59015902 {#code_end#}
59025903 <p>
......@@ -5988,11 +5989,11 @@ fn fibonacci(index: u32) u32 {
59885989
59895990test "fibonacci" {
59905991 // test fibonacci at run-time
5991 expect(fibonacci(7) == 13);
5992 try expect(fibonacci(7) == 13);
59925993
59935994 // test fibonacci at compile-time
59945995 comptime {
5995 expect(fibonacci(7) == 13);
5996 try expect(fibonacci(7) == 13);
59965997 }
59975998}
59985999 {#code_end#}
......@@ -6009,7 +6010,7 @@ fn fibonacci(index: u32) u32 {
60096010
60106011test "fibonacci" {
60116012 comptime {
6012 expect(fibonacci(7) == 13);
6013 try expect(fibonacci(7) == 13);
60136014 }
60146015}
60156016 {#code_end#}
......@@ -6032,7 +6033,7 @@ fn fibonacci(index: i32) i32 {
60326033
60336034test "fibonacci" {
60346035 comptime {
6035 expect(fibonacci(7) == 13);
6036 try expect(fibonacci(7) == 13);
60366037 }
60376038}
60386039 {#code_end#}
......@@ -6045,7 +6046,7 @@ test "fibonacci" {
60456046 <p>
60466047 What if we fix the base case, but put the wrong value in the {#syntax#}expect{#endsyntax#} line?
60476048 </p>
6048 {#code_begin|test_err|encountered @panic at compile-time#}
6049 {#code_begin|test_err|test "fibonacci"... FAIL (TestUnexpectedResult)#}
60496050const expect = @import("std").testing.expect;
60506051
60516052fn fibonacci(index: i32) i32 {
......@@ -6055,7 +6056,7 @@ fn fibonacci(index: i32) i32 {
60556056
60566057test "fibonacci" {
60576058 comptime {
6058 expect(fibonacci(7) == 99999);
6059 try expect(fibonacci(7) == 99999);
60596060 }
60606061}
60616062 {#code_end#}
......@@ -6105,7 +6106,7 @@ fn sum(numbers: []const i32) i32 {
61056106}
61066107
61076108test "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);
61096110}
61106111 {#code_end#}
61116112 <p>
......@@ -6513,7 +6514,7 @@ comptime {
65136514extern fn my_func(a: i32, b: i32) i32;
65146515
65156516test "global assembly" {
6516 expect(my_func(12, 34) == 46);
6517 try expect(my_func(12, 34) == 46);
65176518}
65186519 {#code_end#}
65196520 {#header_close#}
......@@ -6554,7 +6555,7 @@ var x: i32 = 1;
65546555
65556556test "suspend with no resume" {
65566557 var frame = async func();
6557 expect(x == 2);
6558 try expect(x == 2);
65586559}
65596560
65606561fn func() void {
......@@ -6581,14 +6582,14 @@ var result = false;
65816582
65826583test "async function suspend with block" {
65836584 _ = async testSuspendBlock();
6584 expect(!result);
6585 try expect(!result);
65856586 resume the_frame;
6586 expect(result);
6587 try expect(result);
65876588}
65886589
65896590fn testSuspendBlock() void {
65906591 suspend {
6591 comptime expect(@TypeOf(@frame()) == *@Frame(testSuspendBlock));
6592 comptime try expect(@TypeOf(@frame()) == *@Frame(testSuspendBlock));
65926593 the_frame = @frame();
65936594 }
65946595 result = true;
......@@ -6617,7 +6618,7 @@ const expect = std.testing.expect;
66176618test "resume from suspend" {
66186619 var my_result: i32 = 1;
66196620 _ = async testResumeFromSuspend(&my_result);
6620 std.testing.expect(my_result == 2);
6621 try std.testing.expect(my_result == 2);
66216622}
66226623fn testResumeFromSuspend(my_result: *i32) void {
66236624 suspend {
......@@ -6653,7 +6654,7 @@ test "async and await" {
66536654
66546655fn amain() void {
66556656 var frame = async func();
6656 comptime expect(@TypeOf(frame) == @Frame(func));
6657 comptime try expect(@TypeOf(frame) == @Frame(func));
66576658
66586659 const ptr: anyframe->void = &frame;
66596660 const any_ptr: anyframe = ptr;
......@@ -6694,8 +6695,8 @@ test "async function await" {
66946695 seq('f');
66956696 resume the_frame;
66966697 seq('i');
6697 expect(final_result == 1234);
6698 expect(std.mem.eql(u8, &seq_points, "abcdefghi"));
6698 try expect(final_result == 1234);
6699 try expect(std.mem.eql(u8, &seq_points, "abcdefghi"));
66996700}
67006701fn amain() void {
67016702 seq('b');
......@@ -6909,9 +6910,9 @@ fn readFile(allocator: *Allocator, filename: []const u8) ![]u8 {
69096910 for the current target to match the C ABI. When the child type of a pointer has
69106911 this alignment, the alignment can be omitted from the type.
69116912 </p>
6912 <pre>{#syntax#}const expect = @import("std").testing.expect;
6913 <pre>{#syntax#}const expect = @import("std").debug.assert;
69136914comptime {
6914 expect(*u32 == *align(@alignOf(u32)) u32);
6915 assert(*u32 == *align(@alignOf(u32)) u32);
69156916}{#endsyntax#}</pre>
69166917 <p>
69176918 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" {
69576958 var foo = Foo{ .bar = func };
69586959 var bytes: [64]u8 align(@alignOf(@Frame(func))) = undefined;
69596960 const f = @asyncCall(&bytes, {}, foo.bar, .{&data});
6960 expect(data == 2);
6961 try expect(data == 2);
69616962 resume f;
6962 expect(data == 4);
6963 try expect(data == 4);
69636964}
69646965
69656966fn func(y: *i32) void {
......@@ -7146,7 +7147,7 @@ fn func(y: *i32) void {
71467147const expect = @import("std").testing.expect;
71477148
71487149test "noinline function call" {
7149 expect(@call(.{}, add, .{3, 9}) == 12);
7150 try expect(@call(.{}, add, .{3, 9}) == 12);
71507151}
71517152
71527153fn add(a: i32, b: i32) i32 {
......@@ -7621,17 +7622,17 @@ test "field access by string" {
76217622 @field(p, "x") = 4;
76227623 @field(p, "y") = @field(p, "x") + 1;
76237624
7624 expect(@field(p, "x") == 4);
7625 expect(@field(p, "y") == 5);
7625 try expect(@field(p, "x") == 4);
7626 try expect(@field(p, "y") == 5);
76267627}
76277628
76287629test "decl access by string" {
76297630 const expect = std.testing.expect;
76307631
7631 expect(@field(Point, "z") == 1);
7632 try expect(@field(Point, "z") == 1);
76327633
76337634 @field(Point, "z") = 2;
7634 expect(@field(Point, "z") == 2);
7635 try expect(@field(Point, "z") == 2);
76357636}
76367637 {#code_end#}
76377638
......@@ -7747,16 +7748,16 @@ const Foo = struct {
77477748};
77487749
77497750test "@hasDecl" {
7750 expect(@hasDecl(Foo, "blah"));
7751 try expect(@hasDecl(Foo, "blah"));
77517752
77527753 // Even though `hi` is private, @hasDecl returns true because this test is
77537754 // in the same file scope as Foo. It would return false if Foo was declared
77547755 // in a different file.
7755 expect(@hasDecl(Foo, "hi"));
7756 try expect(@hasDecl(Foo, "hi"));
77567757
77577758 // @hasDecl is for declarations; not fields.
7758 expect(!@hasDecl(Foo, "nope"));
7759 expect(!@hasDecl(Foo, "nope1234"));
7759 try expect(!@hasDecl(Foo, "nope"));
7760 try expect(!@hasDecl(Foo, "nope1234"));
77607761}
77617762 {#code_end#}
77627763 {#see_also|@hasField#}
......@@ -7937,8 +7938,8 @@ test "@wasmMemoryGrow" {
79377938 if (builtin.arch != .wasm32) return error.SkipZigTest;
79387939
79397940 var prev = @wasmMemorySize(0);
7940 expect(prev == @wasmMemoryGrow(0, 1));
7941 expect(prev + 1 == @wasmMemorySize(0));
7941 try expect(prev == @wasmMemoryGrow(0, 1));
7942 try expect(prev + 1 == @wasmMemorySize(0));
79427943}
79437944 {#code_end#}
79447945 {#see_also|@wasmMemorySize#}
......@@ -8279,8 +8280,8 @@ const expect = std.testing.expect;
82798280test "vector @splat" {
82808281 const scalar: u32 = 5;
82818282 const result = @splat(4, scalar);
8282 comptime expect(@TypeOf(result) == std.meta.Vector(4, u32));
8283 expect(std.mem.eql(u32, &@as([4]u32, result), &[_]u32{ 5, 5, 5, 5 }));
8283 comptime try expect(@TypeOf(result) == std.meta.Vector(4, u32));
8284 try expect(std.mem.eql(u32, &@as([4]u32, result), &[_]u32{ 5, 5, 5, 5 }));
82848285}
82858286 {#code_end#}
82868287 <p>
......@@ -8322,10 +8323,10 @@ test "vector @reduce" {
83228323 const value: std.meta.Vector(4, i32) = [_]i32{ 1, -1, 1, -1 };
83238324 const result = value > @splat(4, @as(i32, 0));
83248325 // 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));
83268327 const is_all_true = @reduce(.And, result);
8327 comptime expect(@TypeOf(is_all_true) == bool);
8328 expect(is_all_true == false);
8328 comptime try expect(@TypeOf(is_all_true) == bool);
8329 try expect(is_all_true == false);
83298330}
83308331 {#code_end#}
83318332 {#see_also|Vectors|@setFloatMode#}
......@@ -8341,16 +8342,16 @@ const std = @import("std");
83418342const expect = std.testing.expect;
83428343
83438344test "@src" {
8344 doTheTest();
8345 try doTheTest();
83458346}
83468347
8347fn doTheTest() void {
8348fn doTheTest() !void {
83488349 const src = @src();
83498350
8350 expect(src.line == 9);
8351 expect(src.column == 17);
8352 expect(std.mem.endsWith(u8, src.fn_name, "doTheTest"));
8353 expect(std.mem.endsWith(u8, src.file, "test.zig"));
8351 try expect(src.line == 9);
8352 try expect(src.column == 17);
8353 try expect(std.mem.endsWith(u8, src.fn_name, "doTheTest"));
8354 try expect(std.mem.endsWith(u8, src.file, "test.zig"));
83548355}
83558356 {#code_end#}
83568357 {#header_close#}
......@@ -8527,7 +8528,7 @@ const expect = std.testing.expect;
85278528test "@This()" {
85288529 var items = [_]i32{ 1, 2, 3, 4 };
85298530 const list = List(i32){ .items = items[0..] };
8530 expect(list.length() == 4);
8531 try expect(list.length() == 4);
85318532}
85328533
85338534fn List(comptime T: type) type {
......@@ -8573,7 +8574,7 @@ const expect = std.testing.expect;
85738574test "integer truncation" {
85748575 var a: u16 = 0xabcd;
85758576 var b: u8 = @truncate(u8, a);
8576 expect(b == 0xcd);
8577 try expect(b == 0xcd);
85778578}
85788579 {#code_end#}
85798580 <p>
......@@ -8661,8 +8662,8 @@ const expect = std.testing.expect;
86618662test "no runtime side effects" {
86628663 var data: i32 = 0;
86638664 const T = @TypeOf(foo(i32, &data));
8664 comptime expect(T == i32);
8665 expect(data == 0);
8665 comptime try expect(T == i32);
8666 try expect(data == 0);
86668667}
86678668
86688669fn foo(comptime T: type, ptr: *T) T {
......@@ -8972,9 +8973,9 @@ const maxInt = std.math.maxInt;
89728973test "wraparound addition and subtraction" {
89738974 const x: i32 = maxInt(i32);
89748975 const min_val = x +% 1;
8975 expect(min_val == minInt(i32));
8976 try expect(min_val == minInt(i32));
89768977 const max_val = min_val -% 1;
8977 expect(max_val == maxInt(i32));
8978 try expect(max_val == maxInt(i32));
89788979}
89798980 {#code_end#}
89808981 {#header_close#}
......@@ -9405,7 +9406,7 @@ test "using an allocator" {
94059406 var buffer: [100]u8 = undefined;
94069407 const allocator = &std.heap.FixedBufferAllocator.init(&buffer).allocator;
94079408 const result = try concat(allocator, "foo", "bar");
9408 expect(std.mem.eql(u8, "foobar", result));
9409 try expect(std.mem.eql(u8, "foobar", result));
94099410}
94109411
94119412fn concat(allocator: *Allocator, a: []const u8, b: []const u8) ![]u8 {
......@@ -9675,7 +9676,7 @@ const builtin = std.builtin;
96759676const expect = std.testing.expect;
96769677
96779678test "builtin.is_test" {
9678 expect(builtin.is_test);
9679 try expect(builtin.is_test);
96799680}
96809681 {#code_end#}
96819682 <p>
......@@ -9720,13 +9721,13 @@ test "assert in release fast mode" {
97209721 <p>
97219722 Better practice for checking the output when testing is to use {#syntax#}std.testing.expect{#endsyntax#}:
97229723 </p>
9723 {#code_begin|test_err|test failure#}
9724 {#code_begin|test_err|test "expect in release fast mode"... FAIL (TestUnexpectedResult)#}
97249725 {#code_release_fast#}
97259726const std = @import("std");
97269727const expect = std.testing.expect;
97279728
97289729test "expect in release fast mode" {
9729 expect(false);
9730 try expect(false);
97309731}
97319732 {#code_end#}
97329733 <p>See the rest of the {#syntax#}std.testing{#endsyntax#} namespace for more available functions.</p>