authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-03-13 15:56:09-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-03-13 15:56:09-07:00
loge5a95c6af123d614522de57f875b620deb152cc0
treebab31574cd2b31cf723d6d68749391b52f534fc4
parentd029e0e972dc34788e9674d0cb72e964b64e17c4

std: promote tests to doctests

Now these show up as "example usage" in generated documentation.

26 files changed, 313 insertions(+), 290 deletions(-)

lib/std/ascii.zig+9-9
......@@ -147,7 +147,7 @@ pub fn isWhitespace(c: u8) bool {
147147/// See also: `isWhitespace`
148148pub const whitespace = [_]u8{ ' ', '\t', '\n', '\r', control_code.vt, control_code.ff };
149149
150test "whitespace" {
150test whitespace {
151151 for (whitespace) |char| try std.testing.expect(isWhitespace(char));
152152
153153 var i: u8 = 0;
......@@ -278,7 +278,7 @@ pub fn lowerString(output: []u8, ascii_string: []const u8) []u8 {
278278 return output[0..ascii_string.len];
279279}
280280
281test "lowerString" {
281test lowerString {
282282 var buf: [1024]u8 = undefined;
283283 const result = lowerString(&buf, "aBcDeFgHiJkLmNOPqrst0234+πŸ’©!");
284284 try std.testing.expectEqualStrings("abcdefghijklmnopqrst0234+πŸ’©!", result);
......@@ -291,7 +291,7 @@ pub fn allocLowerString(allocator: std.mem.Allocator, ascii_string: []const u8)
291291 return lowerString(result, ascii_string);
292292}
293293
294test "allocLowerString" {
294test allocLowerString {
295295 const result = try allocLowerString(std.testing.allocator, "aBcDeFgHiJkLmNOPqrst0234+πŸ’©!");
296296 defer std.testing.allocator.free(result);
297297 try std.testing.expectEqualStrings("abcdefghijklmnopqrst0234+πŸ’©!", result);
......@@ -307,7 +307,7 @@ pub fn upperString(output: []u8, ascii_string: []const u8) []u8 {
307307 return output[0..ascii_string.len];
308308}
309309
310test "upperString" {
310test upperString {
311311 var buf: [1024]u8 = undefined;
312312 const result = upperString(&buf, "aBcDeFgHiJkLmNOPqrst0234+πŸ’©!");
313313 try std.testing.expectEqualStrings("ABCDEFGHIJKLMNOPQRST0234+πŸ’©!", result);
......@@ -320,7 +320,7 @@ pub fn allocUpperString(allocator: std.mem.Allocator, ascii_string: []const u8)
320320 return upperString(result, ascii_string);
321321}
322322
323test "allocUpperString" {
323test allocUpperString {
324324 const result = try allocUpperString(std.testing.allocator, "aBcDeFgHiJkLmNOPqrst0234+πŸ’©!");
325325 defer std.testing.allocator.free(result);
326326 try std.testing.expectEqualStrings("ABCDEFGHIJKLMNOPQRST0234+πŸ’©!", result);
......@@ -335,7 +335,7 @@ pub fn eqlIgnoreCase(a: []const u8, b: []const u8) bool {
335335 return true;
336336}
337337
338test "eqlIgnoreCase" {
338test eqlIgnoreCase {
339339 try std.testing.expect(eqlIgnoreCase("HElπŸ’©Lo!", "helπŸ’©lo!"));
340340 try std.testing.expect(!eqlIgnoreCase("hElLo!", "hello! "));
341341 try std.testing.expect(!eqlIgnoreCase("hElLo!", "helro!"));
......@@ -345,7 +345,7 @@ pub fn startsWithIgnoreCase(haystack: []const u8, needle: []const u8) bool {
345345 return if (needle.len > haystack.len) false else eqlIgnoreCase(haystack[0..needle.len], needle);
346346}
347347
348test "startsWithIgnoreCase" {
348test startsWithIgnoreCase {
349349 try std.testing.expect(startsWithIgnoreCase("boB", "Bo"));
350350 try std.testing.expect(!startsWithIgnoreCase("Needle in hAyStAcK", "haystack"));
351351}
......@@ -354,7 +354,7 @@ pub fn endsWithIgnoreCase(haystack: []const u8, needle: []const u8) bool {
354354 return if (needle.len > haystack.len) false else eqlIgnoreCase(haystack[haystack.len - needle.len ..], needle);
355355}
356356
357test "endsWithIgnoreCase" {
357test endsWithIgnoreCase {
358358 try std.testing.expect(endsWithIgnoreCase("Needle in HaYsTaCk", "haystack"));
359359 try std.testing.expect(!endsWithIgnoreCase("BoB", "Bo"));
360360}
......@@ -409,7 +409,7 @@ fn boyerMooreHorspoolPreprocessIgnoreCase(pattern: []const u8, table: *[256]usiz
409409 }
410410}
411411
412test "indexOfIgnoreCase" {
412test indexOfIgnoreCase {
413413 try std.testing.expect(indexOfIgnoreCase("one Two Three Four", "foUr").? == 14);
414414 try std.testing.expect(indexOfIgnoreCase("one two three FouR", "gOur") == null);
415415 try std.testing.expect(indexOfIgnoreCase("foO", "Foo").? == 0);
lib/std/bit_set.zig+5-5
......@@ -1648,7 +1648,7 @@ fn testStaticBitSet(comptime Set: type) !void {
16481648 try testPureBitSet(Set);
16491649}
16501650
1651test "IntegerBitSet" {
1651test IntegerBitSet {
16521652 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
16531653
16541654 try testStaticBitSet(IntegerBitSet(0));
......@@ -1661,7 +1661,7 @@ test "IntegerBitSet" {
16611661 try testStaticBitSet(IntegerBitSet(127));
16621662}
16631663
1664test "ArrayBitSet" {
1664test ArrayBitSet {
16651665 inline for (.{ 0, 1, 2, 31, 32, 33, 63, 64, 65, 254, 500, 3000 }) |size| {
16661666 try testStaticBitSet(ArrayBitSet(u8, size));
16671667 try testStaticBitSet(ArrayBitSet(u16, size));
......@@ -1671,7 +1671,7 @@ test "ArrayBitSet" {
16711671 }
16721672}
16731673
1674test "DynamicBitSetUnmanaged" {
1674test DynamicBitSetUnmanaged {
16751675 const allocator = std.testing.allocator;
16761676 var a = try DynamicBitSetUnmanaged.initEmpty(allocator, 300);
16771677 try testing.expectEqual(@as(usize, 0), a.count());
......@@ -1724,7 +1724,7 @@ test "DynamicBitSetUnmanaged" {
17241724 }
17251725}
17261726
1727test "DynamicBitSet" {
1727test DynamicBitSet {
17281728 const allocator = std.testing.allocator;
17291729 var a = try DynamicBitSet.initEmpty(allocator, 300);
17301730 try testing.expectEqual(@as(usize, 0), a.count());
......@@ -1765,7 +1765,7 @@ test "DynamicBitSet" {
17651765 }
17661766}
17671767
1768test "StaticBitSet" {
1768test StaticBitSet {
17691769 try testing.expectEqual(IntegerBitSet(0), StaticBitSet(0));
17701770 try testing.expectEqual(IntegerBitSet(5), StaticBitSet(5));
17711771 try testing.expectEqual(IntegerBitSet(@bitSizeOf(usize)), StaticBitSet(@bitSizeOf(usize)));
lib/std/bounded_array.zig+1-1
......@@ -287,7 +287,7 @@ pub fn BoundedArrayAligned(
287287 };
288288}
289289
290test "BoundedArray" {
290test BoundedArray {
291291 var a = try BoundedArray(u8, 64).init(32);
292292
293293 try testing.expectEqual(a.capacity(), 64);
lib/std/child_process.zig+3-3
......@@ -1230,7 +1230,7 @@ fn windowsCreateProcessSupportsExtension(ext: []const u16) ?CreateProcessSupport
12301230 return null;
12311231}
12321232
1233test "windowsCreateProcessSupportsExtension" {
1233test windowsCreateProcessSupportsExtension {
12341234 try std.testing.expectEqual(CreateProcessSupportedExtension.exe, windowsCreateProcessSupportsExtension(&[_]u16{ '.', 'e', 'X', 'e' }).?);
12351235 try std.testing.expect(windowsCreateProcessSupportsExtension(&[_]u16{ '.', 'e', 'X', 'e', 'c' }) == null);
12361236}
......@@ -1321,7 +1321,7 @@ pub fn argvToCommandLineWindows(
13211321 return try unicode.wtf8ToWtf16LeAllocZ(allocator, buf.items);
13221322}
13231323
1324test "argvToCommandLineWindows" {
1324test argvToCommandLineWindows {
13251325 const t = testArgvToCommandLineWindows;
13261326
13271327 try t(&.{
......@@ -1555,7 +1555,7 @@ pub fn createNullDelimitedEnvMap(arena: mem.Allocator, env_map: *const EnvMap) !
15551555 return envp_buf;
15561556}
15571557
1558test "createNullDelimitedEnvMap" {
1558test createNullDelimitedEnvMap {
15591559 const testing = std.testing;
15601560 const allocator = testing.allocator;
15611561 var envmap = EnvMap.init(allocator);
lib/std/crypto/utils.zig+3-3
......@@ -138,7 +138,7 @@ pub inline fn secureZero(comptime T: type, s: []T) void {
138138 @memset(@as([]volatile T, s), 0);
139139}
140140
141test "timingSafeEql" {
141test timingSafeEql {
142142 var a: [100]u8 = undefined;
143143 var b: [100]u8 = undefined;
144144 random.bytes(a[0..]);
......@@ -162,7 +162,7 @@ test "timingSafeEql (vectors)" {
162162 try testing.expect(timingSafeEql(@Vector(100, u8), v1, v3));
163163}
164164
165test "timingSafeCompare" {
165test timingSafeCompare {
166166 var a = [_]u8{10} ** 32;
167167 var b = [_]u8{10} ** 32;
168168 try testing.expectEqual(timingSafeCompare(u8, &a, &b, .big), .eq);
......@@ -195,7 +195,7 @@ test "timingSafe{Add,Sub}" {
195195 }
196196}
197197
198test "secureZero" {
198test secureZero {
199199 var a = [_]u8{0xfe} ** 8;
200200 var b = [_]u8{0xfe} ** 8;
201201
lib/std/debug.zig+2-2
......@@ -908,7 +908,7 @@ fn machoSearchSymbols(symbols: []const MachoSymbol, address: usize) ?*const Mach
908908 return null;
909909}
910910
911test "machoSearchSymbols" {
911test machoSearchSymbols {
912912 const symbols = [_]MachoSymbol{
913913 .{ .addr = 100, .strx = undefined, .size = undefined, .ofile = undefined },
914914 .{ .addr = 200, .strx = undefined, .size = undefined, .ofile = undefined },
......@@ -1502,7 +1502,7 @@ fn printLineFromFileAnyOs(out_stream: anytype, line_info: LineInfo) !void {
15021502 }
15031503}
15041504
1505test "printLineFromFileAnyOs" {
1505test printLineFromFileAnyOs {
15061506 var output = std.ArrayList(u8).init(std.testing.allocator);
15071507 defer output.deinit();
15081508 const output_stream = output.writer();
lib/std/fifo.zig+1-1
......@@ -507,7 +507,7 @@ test "LinearFifo(u8, .Dynamic)" {
507507 }
508508}
509509
510test "LinearFifo" {
510test LinearFifo {
511511 inline for ([_]type{ u1, u8, u16, u64 }) |T| {
512512 inline for ([_]LinearFifoBufferType{ LinearFifoBufferType{ .Static = 32 }, .Slice, .Dynamic }) |bt| {
513513 const FifoType = LinearFifo(T, bt);
lib/std/fmt.zig+9-9
......@@ -1300,7 +1300,7 @@ pub fn fmtDuration(ns: u64) Formatter(formatDuration) {
13001300 return .{ .data = data };
13011301}
13021302
1303test "fmtDuration" {
1303test fmtDuration {
13041304 var buf: [24]u8 = undefined;
13051305 inline for (.{
13061306 .{ .s = "0ns", .d = 0 },
......@@ -1364,7 +1364,7 @@ pub fn fmtDurationSigned(ns: i64) Formatter(formatDurationSigned) {
13641364 return .{ .data = ns };
13651365}
13661366
1367test "fmtDurationSigned" {
1367test fmtDurationSigned {
13681368 var buf: [24]u8 = undefined;
13691369 inline for (.{
13701370 .{ .s = "0ns", .d = 0 },
......@@ -1494,7 +1494,7 @@ pub fn parseInt(comptime T: type, buf: []const u8, base: u8) ParseIntError!T {
14941494 return parseWithSign(T, buf, base, .pos);
14951495}
14961496
1497test "parseInt" {
1497test parseInt {
14981498 try std.testing.expect((try parseInt(i32, "-10", 10)) == -10);
14991499 try std.testing.expect((try parseInt(i32, "+10", 10)) == 10);
15001500 try std.testing.expect((try parseInt(u32, "+10", 10)) == 10);
......@@ -1636,7 +1636,7 @@ pub fn parseUnsigned(comptime T: type, buf: []const u8, base: u8) ParseIntError!
16361636 return parseWithSign(T, buf, base, .pos);
16371637}
16381638
1639test "parseUnsigned" {
1639test parseUnsigned {
16401640 try std.testing.expect((try parseUnsigned(u16, "050124", 10)) == 50124);
16411641 try std.testing.expect((try parseUnsigned(u16, "65535", 10)) == 65535);
16421642 try std.testing.expect((try parseUnsigned(u16, "65_535", 10)) == 65535);
......@@ -1710,7 +1710,7 @@ pub fn parseIntSizeSuffix(buf: []const u8, digit_base: u8) ParseIntError!usize {
17101710 return math.mul(usize, number, multiplier);
17111711}
17121712
1713test "parseIntSizeSuffix" {
1713test parseIntSizeSuffix {
17141714 try std.testing.expect(try parseIntSizeSuffix("2", 10) == 2);
17151715 try std.testing.expect(try parseIntSizeSuffix("2B", 10) == 2);
17161716 try std.testing.expect(try parseIntSizeSuffix("2kB", 10) == 2000);
......@@ -1793,7 +1793,7 @@ pub fn allocPrintZ(allocator: mem.Allocator, comptime fmt: []const u8, args: any
17931793 return result[0 .. result.len - 1 :0];
17941794}
17951795
1796test "bufPrintInt" {
1796test bufPrintIntToSlice {
17971797 var buffer: [100]u8 = undefined;
17981798 const buf = buffer[0..];
17991799
......@@ -1827,7 +1827,7 @@ pub inline fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [cou
18271827 }
18281828}
18291829
1830test "comptimePrint" {
1830test comptimePrint {
18311831 @setEvalBranchQuota(2000);
18321832 try std.testing.expectEqual(*const [3:0]u8, @TypeOf(comptimePrint("{}", .{100})));
18331833 try std.testing.expectEqualSlices(u8, "100", comptimePrint("{}", .{100}));
......@@ -2442,14 +2442,14 @@ pub fn hexToBytes(out: []u8, input: []const u8) ![]u8 {
24422442 return out[0 .. in_i / 2];
24432443}
24442444
2445test "bytesToHex" {
2445test bytesToHex {
24462446 const input = "input slice";
24472447 const encoded = bytesToHex(input, .lower);
24482448 var decoded: [input.len]u8 = undefined;
24492449 try std.testing.expectEqualSlices(u8, input, try hexToBytes(&decoded, &encoded));
24502450}
24512451
2452test "hexToBytes" {
2452test hexToBytes {
24532453 var buf: [32]u8 = undefined;
24542454 try expectFmt("90" ** 32, "{s}", .{fmtSliceHexUpper(try hexToBytes(&buf, "90" ** 32))});
24552455 try expectFmt("ABCD", "{s}", .{fmtSliceHexUpper(try hexToBytes(&buf, "ABCD"))});
lib/std/fs/get_app_data_dir.zig+1-1
......@@ -60,7 +60,7 @@ pub fn getAppDataDir(allocator: mem.Allocator, appname: []const u8) GetAppDataDi
6060 }
6161}
6262
63test "getAppDataDir" {
63test getAppDataDir {
6464 if (builtin.os.tag == .wasi) return error.SkipZigTest;
6565
6666 // We can't actually validate the result
lib/std/fs/path.zig+10-10
......@@ -180,7 +180,7 @@ fn testJoinMaybeZPosix(paths: []const []const u8, expected: []const u8, zero: bo
180180 try testing.expectEqualSlices(u8, expected, if (zero) actual[0 .. actual.len - 1 :0] else actual);
181181}
182182
183test "join" {
183test join {
184184 {
185185 const actual: []u8 = try join(testing.allocator, &[_][]const u8{});
186186 defer testing.allocator.free(actual);
......@@ -303,7 +303,7 @@ pub fn isAbsolutePosixZ(path_c: [*:0]const u8) bool {
303303 return isAbsolutePosix(mem.sliceTo(path_c, 0));
304304}
305305
306test "isAbsoluteWindows" {
306test isAbsoluteWindows {
307307 try testIsAbsoluteWindows("", false);
308308 try testIsAbsoluteWindows("/", true);
309309 try testIsAbsoluteWindows("//", true);
......@@ -326,7 +326,7 @@ test "isAbsoluteWindows" {
326326 try testIsAbsoluteWindows("/usr/local", true);
327327}
328328
329test "isAbsolutePosix" {
329test isAbsolutePosix {
330330 try testIsAbsolutePosix("", false);
331331 try testIsAbsolutePosix("/home/foo", true);
332332 try testIsAbsolutePosix("/home/foo/..", true);
......@@ -400,7 +400,7 @@ pub fn windowsParsePath(path: []const u8) WindowsPath {
400400 return relative_path;
401401}
402402
403test "windowsParsePath" {
403test windowsParsePath {
404404 {
405405 const parsed = windowsParsePath("//a/b");
406406 try testing.expect(parsed.is_abs);
......@@ -884,7 +884,7 @@ pub fn dirnamePosix(path: []const u8) ?[]const u8 {
884884 return path[0..end_index];
885885}
886886
887test "dirnamePosix" {
887test dirnamePosix {
888888 try testDirnamePosix("/a/b/c", "/a/b");
889889 try testDirnamePosix("/a/b/c///", "/a/b");
890890 try testDirnamePosix("/a", "/");
......@@ -898,7 +898,7 @@ test "dirnamePosix" {
898898 try testDirnamePosix("a//", null);
899899}
900900
901test "dirnameWindows" {
901test dirnameWindows {
902902 try testDirnameWindows("c:\\", null);
903903 try testDirnameWindows("c:\\foo", "c:\\");
904904 try testDirnameWindows("c:\\foo\\", "c:\\");
......@@ -1011,7 +1011,7 @@ pub fn basenameWindows(path: []const u8) []const u8 {
10111011 return path[start_index + 1 .. end_index];
10121012}
10131013
1014test "basename" {
1014test basename {
10151015 try testBasename("", "");
10161016 try testBasename("/", "");
10171017 try testBasename("/dir/basename.ext", "basename.ext");
......@@ -1186,7 +1186,7 @@ pub fn relativePosix(allocator: Allocator, from: []const u8, to: []const u8) ![]
11861186 return [_]u8{};
11871187}
11881188
1189test "relative" {
1189test relative {
11901190 try testRelativeWindows("c:/blah\\blah", "d:/games", "D:\\games");
11911191 try testRelativeWindows("c:/aaaa/bbbb", "c:/aaaa", "..");
11921192 try testRelativeWindows("c:/aaaa/bbbb", "c:/cccc", "..\\..\\cccc");
......@@ -1271,7 +1271,7 @@ fn testExtension(path: []const u8, expected: []const u8) !void {
12711271 try testing.expectEqualStrings(expected, extension(path));
12721272}
12731273
1274test "extension" {
1274test extension {
12751275 try testExtension("", "");
12761276 try testExtension(".", "");
12771277 try testExtension("a.", ".");
......@@ -1328,7 +1328,7 @@ fn testStem(path: []const u8, expected: []const u8) !void {
13281328 try testing.expectEqualStrings(expected, stem(path));
13291329}
13301330
1331test "stem" {
1331test stem {
13321332 try testStem("hello/world/lib.tar.gz", "lib.tar");
13331333 try testStem("hello/world/lib.tar", "lib");
13341334 try testStem("hello/world/lib", "lib");
lib/std/io.zig+1-1
......@@ -425,7 +425,7 @@ fn dummyWrite(context: void, data: []const u8) error{}!usize {
425425 return data.len;
426426}
427427
428test "null_writer" {
428test null_writer {
429429 null_writer.writeAll("yay" ** 10) catch |err| switch (err) {};
430430}
431431
lib/std/math.zig+91-70
......@@ -161,28 +161,51 @@ pub fn approxEqRel(comptime T: type, x: T, y: T, tolerance: T) bool {
161161 return @abs(x - y) <= @max(@abs(x), @abs(y)) * tolerance;
162162}
163163
164test "approxEqAbs and approxEqRel" {
164test approxEqAbs {
165165 inline for ([_]type{ f16, f32, f64, f128 }) |T| {
166166 const eps_value = comptime floatEps(T);
167 const sqrt_eps_value = comptime sqrt(eps_value);
168 const nan_value = comptime nan(T);
169 const inf_value = comptime inf(T);
170167 const min_value = comptime floatMin(T);
171168
172169 try testing.expect(approxEqAbs(T, 0.0, 0.0, eps_value));
173170 try testing.expect(approxEqAbs(T, -0.0, -0.0, eps_value));
174171 try testing.expect(approxEqAbs(T, 0.0, -0.0, eps_value));
175 try testing.expect(approxEqRel(T, 1.0, 1.0, sqrt_eps_value));
176 try testing.expect(!approxEqRel(T, 1.0, 0.0, sqrt_eps_value));
177172 try testing.expect(!approxEqAbs(T, 1.0 + 2 * eps_value, 1.0, eps_value));
178173 try testing.expect(approxEqAbs(T, 1.0 + 1 * eps_value, 1.0, eps_value));
174 try testing.expect(approxEqAbs(T, min_value, 0.0, eps_value * 2));
175 try testing.expect(approxEqAbs(T, -min_value, 0.0, eps_value * 2));
176 }
177
178 comptime {
179 // `comptime_float` is guaranteed to have the same precision and operations of
180 // the largest other floating point type, which is f128 but it doesn't have a
181 // defined layout so we can't rely on `@bitCast` to construct the smallest
182 // possible epsilon value like we do in the tests above. In the same vein, we
183 // also can't represent a max/min, `NaN` or `Inf` values.
184 const eps_value = 1e-4;
185
186 try testing.expect(approxEqAbs(comptime_float, 0.0, 0.0, eps_value));
187 try testing.expect(approxEqAbs(comptime_float, -0.0, -0.0, eps_value));
188 try testing.expect(approxEqAbs(comptime_float, 0.0, -0.0, eps_value));
189 try testing.expect(!approxEqAbs(comptime_float, 1.0 + 2 * eps_value, 1.0, eps_value));
190 try testing.expect(approxEqAbs(comptime_float, 1.0 + 1 * eps_value, 1.0, eps_value));
191 }
192}
193
194test approxEqRel {
195 inline for ([_]type{ f16, f32, f64, f128 }) |T| {
196 const eps_value = comptime floatEps(T);
197 const sqrt_eps_value = comptime sqrt(eps_value);
198 const nan_value = comptime nan(T);
199 const inf_value = comptime inf(T);
200 const min_value = comptime floatMin(T);
201
202 try testing.expect(approxEqRel(T, 1.0, 1.0, sqrt_eps_value));
203 try testing.expect(!approxEqRel(T, 1.0, 0.0, sqrt_eps_value));
179204 try testing.expect(!approxEqRel(T, 1.0, nan_value, sqrt_eps_value));
180205 try testing.expect(!approxEqRel(T, nan_value, nan_value, sqrt_eps_value));
181206 try testing.expect(approxEqRel(T, inf_value, inf_value, sqrt_eps_value));
182207 try testing.expect(approxEqRel(T, min_value, min_value, sqrt_eps_value));
183208 try testing.expect(approxEqRel(T, -min_value, -min_value, sqrt_eps_value));
184 try testing.expect(approxEqAbs(T, min_value, 0.0, eps_value * 2));
185 try testing.expect(approxEqAbs(T, -min_value, 0.0, eps_value * 2));
186209 }
187210
188211 comptime {
......@@ -194,13 +217,8 @@ test "approxEqAbs and approxEqRel" {
194217 const eps_value = 1e-4;
195218 const sqrt_eps_value = sqrt(eps_value);
196219
197 try testing.expect(approxEqAbs(comptime_float, 0.0, 0.0, eps_value));
198 try testing.expect(approxEqAbs(comptime_float, -0.0, -0.0, eps_value));
199 try testing.expect(approxEqAbs(comptime_float, 0.0, -0.0, eps_value));
200220 try testing.expect(approxEqRel(comptime_float, 1.0, 1.0, sqrt_eps_value));
201221 try testing.expect(!approxEqRel(comptime_float, 1.0, 0.0, sqrt_eps_value));
202 try testing.expect(!approxEqAbs(comptime_float, 1.0 + 2 * eps_value, 1.0, eps_value));
203 try testing.expect(approxEqAbs(comptime_float, 1.0 + 1 * eps_value, 1.0, eps_value));
204222 }
205223}
206224
......@@ -300,7 +318,7 @@ pub fn radiansToDegrees(comptime T: type, angle_in_radians: T) T {
300318 return angle_in_radians * 180.0 / pi;
301319}
302320
303test "radiansToDegrees" {
321test radiansToDegrees {
304322 try std.testing.expectApproxEqAbs(@as(f32, 0), radiansToDegrees(f32, 0), 1e-6);
305323 try std.testing.expectApproxEqAbs(@as(f32, 90), radiansToDegrees(f32, pi / 2.0), 1e-6);
306324 try std.testing.expectApproxEqAbs(@as(f32, -45), radiansToDegrees(f32, -pi / 4.0), 1e-6);
......@@ -315,7 +333,7 @@ pub fn degreesToRadians(comptime T: type, angle_in_degrees: T) T {
315333 return angle_in_degrees * pi / 180.0;
316334}
317335
318test "degreesToRadians" {
336test degreesToRadians {
319337 try std.testing.expectApproxEqAbs(@as(f32, pi / 2.0), degreesToRadians(f32, 90), 1e-6);
320338 try std.testing.expectApproxEqAbs(@as(f32, -3 * pi / 2.0), degreesToRadians(f32, -270), 1e-6);
321339 try std.testing.expectApproxEqAbs(@as(f32, 2 * pi), degreesToRadians(f32, 360), 1e-6);
......@@ -464,7 +482,7 @@ pub fn wrap(x: anytype, r: anytype) @TypeOf(x) {
464482 },
465483 }
466484}
467test "wrap" {
485test wrap {
468486 // Within range
469487 try testing.expect(wrap(@as(i32, -75), @as(i32, 180)) == -75);
470488 try testing.expect(wrap(@as(i32, -75), @as(i32, -180)) == -75);
......@@ -501,8 +519,7 @@ test "wrap" {
501519 var i: i32 = 1;
502520 _ = &i;
503521 try testing.expect(wrap(i, 10) == 1);
504}
505test wrap {
522
506523 const limit: i32 = 180;
507524 // Within range
508525 try testing.expect(wrap(@as(i32, -75), limit) == -75);
......@@ -527,7 +544,7 @@ pub fn clamp(val: anytype, lower: anytype, upper: anytype) @TypeOf(val, lower, u
527544 assert(lower <= upper);
528545 return @max(lower, @min(val, upper));
529546}
530test "clamp" {
547test clamp {
531548 // Within range
532549 try testing.expect(std.math.clamp(@as(i32, -1), @as(i32, -4), @as(i32, 7)) == -1);
533550 // Below
......@@ -608,7 +625,7 @@ pub fn shl(comptime T: type, a: T, shift_amt: anytype) T {
608625 return a << casted_shift_amt;
609626}
610627
611test "shl" {
628test shl {
612629 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) {
613630 // https://github.com/ziglang/zig/issues/12012
614631 return error.SkipZigTest;
......@@ -653,7 +670,7 @@ pub fn shr(comptime T: type, a: T, shift_amt: anytype) T {
653670 return a >> casted_shift_amt;
654671}
655672
656test "shr" {
673test shr {
657674 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) {
658675 // https://github.com/ziglang/zig/issues/12012
659676 return error.SkipZigTest;
......@@ -699,7 +716,7 @@ pub fn rotr(comptime T: type, x: T, r: anytype) T {
699716 }
700717}
701718
702test "rotr" {
719test rotr {
703720 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) {
704721 // https://github.com/ziglang/zig/issues/12012
705722 return error.SkipZigTest;
......@@ -744,7 +761,7 @@ pub fn rotl(comptime T: type, x: T, r: anytype) T {
744761 }
745762}
746763
747test "rotl" {
764test rotl {
748765 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) {
749766 // https://github.com/ziglang/zig/issues/12012
750767 return error.SkipZigTest;
......@@ -806,7 +823,7 @@ pub fn IntFittingRange(comptime from: comptime_int, comptime to: comptime_int) t
806823 return std.meta.Int(signedness, magnitude_bits);
807824}
808825
809test "IntFittingRange" {
826test IntFittingRange {
810827 try testing.expect(IntFittingRange(0, 0) == u0);
811828 try testing.expect(IntFittingRange(0, 1) == u1);
812829 try testing.expect(IntFittingRange(0, 2) == u2);
......@@ -874,7 +891,7 @@ pub fn divTrunc(comptime T: type, numerator: T, denominator: T) !T {
874891 return @divTrunc(numerator, denominator);
875892}
876893
877test "divTrunc" {
894test divTrunc {
878895 try testDivTrunc();
879896 try comptime testDivTrunc();
880897}
......@@ -898,7 +915,7 @@ pub fn divFloor(comptime T: type, numerator: T, denominator: T) !T {
898915 return @divFloor(numerator, denominator);
899916}
900917
901test "divFloor" {
918test divFloor {
902919 try testDivFloor();
903920 try comptime testDivFloor();
904921}
......@@ -935,7 +952,7 @@ pub fn divCeil(comptime T: type, numerator: T, denominator: T) !T {
935952 }
936953}
937954
938test "divCeil" {
955test divCeil {
939956 try testDivCeil();
940957 try comptime testDivCeil();
941958}
......@@ -979,7 +996,7 @@ pub fn divExact(comptime T: type, numerator: T, denominator: T) !T {
979996 return result;
980997}
981998
982test "divExact" {
999test divExact {
9831000 try testDivExact();
9841001 try comptime testDivExact();
9851002}
......@@ -1005,7 +1022,7 @@ pub fn mod(comptime T: type, numerator: T, denominator: T) !T {
10051022 return @mod(numerator, denominator);
10061023}
10071024
1008test "mod" {
1025test mod {
10091026 try testMod();
10101027 try comptime testMod();
10111028}
......@@ -1031,7 +1048,7 @@ pub fn rem(comptime T: type, numerator: T, denominator: T) !T {
10311048 return @rem(numerator, denominator);
10321049}
10331050
1034test "rem" {
1051test rem {
10351052 try testRem();
10361053 try comptime testRem();
10371054}
......@@ -1060,7 +1077,7 @@ pub fn negateCast(x: anytype) !std.meta.Int(.signed, @bitSizeOf(@TypeOf(x))) {
10601077 return -@as(int, @intCast(x));
10611078}
10621079
1063test "negateCast" {
1080test negateCast {
10641081 try testing.expect((negateCast(@as(u32, 999)) catch unreachable) == -999);
10651082 try testing.expect(@TypeOf(negateCast(@as(u32, 999)) catch unreachable) == i32);
10661083
......@@ -1085,7 +1102,7 @@ pub fn cast(comptime T: type, x: anytype) ?T {
10851102 }
10861103}
10871104
1088test "cast" {
1105test cast {
10891106 try testing.expect(cast(u8, 300) == null);
10901107 try testing.expect(cast(u8, @as(u32, 300)) == null);
10911108 try testing.expect(cast(i8, -200) == null);
......@@ -1144,7 +1161,7 @@ pub fn ByteAlignedInt(comptime T: type) type {
11441161 return extended_type;
11451162}
11461163
1147test "ByteAlignedInt" {
1164test ByteAlignedInt {
11481165 try testing.expect(ByteAlignedInt(u0) == u0);
11491166 try testing.expect(ByteAlignedInt(i0) == i0);
11501167 try testing.expect(ByteAlignedInt(u3) == u8);
......@@ -1182,7 +1199,7 @@ pub fn floorPowerOfTwo(comptime T: type, value: T) T {
11821199 return @as(T, 1) << log2_int(uT, @as(uT, @intCast(value)));
11831200}
11841201
1185test "floorPowerOfTwo" {
1202test floorPowerOfTwo {
11861203 try testFloorPowerOfTwo();
11871204 try comptime testFloorPowerOfTwo();
11881205}
......@@ -1244,7 +1261,7 @@ pub fn ceilPowerOfTwoAssert(comptime T: type, value: T) T {
12441261 return ceilPowerOfTwo(T, value) catch unreachable;
12451262}
12461263
1247test "ceilPowerOfTwoPromote" {
1264test ceilPowerOfTwoPromote {
12481265 try testCeilPowerOfTwoPromote();
12491266 try comptime testCeilPowerOfTwoPromote();
12501267}
......@@ -1261,7 +1278,7 @@ fn testCeilPowerOfTwoPromote() !void {
12611278 try testing.expectEqual(@as(u5, 16), ceilPowerOfTwoPromote(u4, 9));
12621279}
12631280
1264test "ceilPowerOfTwo" {
1281test ceilPowerOfTwo {
12651282 try testCeilPowerOfTwo();
12661283 try comptime testCeilPowerOfTwo();
12671284}
......@@ -1354,7 +1371,7 @@ pub fn lossyCast(comptime T: type, value: anytype) T {
13541371 }
13551372}
13561373
1357test "lossyCast" {
1374test lossyCast {
13581375 try testing.expect(lossyCast(i16, 70000.0) == @as(i16, 32767));
13591376 try testing.expect(lossyCast(u32, @as(i16, -255)) == @as(u32, 0));
13601377 try testing.expect(lossyCast(i9, @as(u32, 200)) == @as(i9, 200));
......@@ -1383,7 +1400,7 @@ pub fn lerp(a: anytype, b: anytype, t: anytype) @TypeOf(a, b, t) {
13831400 return @mulAdd(Type, b - a, t, a);
13841401}
13851402
1386test "lerp" {
1403test lerp {
13871404 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // https://github.com/ziglang/zig/issues/17884
13881405 if (builtin.zig_backend == .stage2_x86_64 and
13891406 !comptime std.Target.x86.featureSetHas(builtin.cpu.features, .fma)) return error.SkipZigTest;
......@@ -1437,7 +1454,7 @@ pub fn minInt(comptime T: type) comptime_int {
14371454 return -(1 << (bit_count - 1));
14381455}
14391456
1440test "minInt and maxInt" {
1457test maxInt {
14411458 try testing.expect(maxInt(u0) == 0);
14421459 try testing.expect(maxInt(u1) == 1);
14431460 try testing.expect(maxInt(u8) == 255);
......@@ -1454,7 +1471,9 @@ test "minInt and maxInt" {
14541471 try testing.expect(maxInt(i63) == 4611686018427387903);
14551472 try testing.expect(maxInt(i64) == 9223372036854775807);
14561473 try testing.expect(maxInt(i128) == 170141183460469231731687303715884105727);
1474}
14571475
1476test minInt {
14581477 try testing.expect(minInt(u0) == 0);
14591478 try testing.expect(minInt(u1) == 0);
14601479 try testing.expect(minInt(u8) == 0);
......@@ -1492,7 +1511,7 @@ pub fn mulWide(comptime T: type, a: T, b: T) std.meta.Int(
14921511 return @as(ResultInt, a) * @as(ResultInt, b);
14931512}
14941513
1495test "mulWide" {
1514test mulWide {
14961515 try testing.expect(mulWide(u8, 5, 5) == 25);
14971516 try testing.expect(mulWide(i8, 5, -5) == -25);
14981517 try testing.expect(mulWide(u8, 100, 100) == 10000);
......@@ -1517,6 +1536,12 @@ pub const Order = enum {
15171536 };
15181537 }
15191538
1539 test invert {
1540 try testing.expect(Order.invert(order(0, 0)) == .eq);
1541 try testing.expect(Order.invert(order(1, 0)) == .lt);
1542 try testing.expect(Order.invert(order(-1, 0)) == .gt);
1543 }
1544
15201545 pub fn compare(self: Order, op: CompareOperator) bool {
15211546 return switch (self) {
15221547 .lt => switch (op) {
......@@ -1545,6 +1570,19 @@ pub const Order = enum {
15451570 },
15461571 };
15471572 }
1573
1574 // TODO flaw in the language, can't make this a doctest due to ambiguous reference
1575 // also: can't currently add a doctest from an outside scope
1576 test "compare" {
1577 try testing.expect(order(-1, 0).compare(.lt));
1578 try testing.expect(order(-1, 0).compare(.lte));
1579 try testing.expect(order(0, 0).compare(.lte));
1580 try testing.expect(order(0, 0).compare(.eq));
1581 try testing.expect(order(0, 0).compare(.gte));
1582 try testing.expect(order(1, 0).compare(.gte));
1583 try testing.expect(order(1, 0).compare(.gt));
1584 try testing.expect(order(1, 0).compare(.neq));
1585 }
15481586};
15491587
15501588/// Given two numbers, this function returns the order they are with respect to each other.
......@@ -1587,6 +1625,15 @@ pub const CompareOperator = enum {
15871625 .neq => .neq,
15881626 };
15891627 }
1628
1629 test reverse {
1630 inline for (@typeInfo(CompareOperator).Enum.fields) |op_field| {
1631 const op = @as(CompareOperator, @enumFromInt(op_field.value));
1632 try testing.expect(compare(2, op, 3) == compare(3, op.reverse(), 2));
1633 try testing.expect(compare(3, op, 3) == compare(3, op.reverse(), 3));
1634 try testing.expect(compare(4, op, 3) == compare(3, op.reverse(), 4));
1635 }
1636 }
15901637};
15911638
15921639/// This function does the same thing as comparison operators, however the
......@@ -1603,7 +1650,7 @@ pub fn compare(a: anytype, op: CompareOperator, b: anytype) bool {
16031650 };
16041651}
16051652
1606test "compare between signed and unsigned" {
1653test compare {
16071654 try testing.expect(compare(@as(i8, -1), .lt, @as(u8, 255)));
16081655 try testing.expect(compare(@as(i8, 2), .gt, @as(u8, 1)));
16091656 try testing.expect(!compare(@as(i8, -1), .gte, @as(u8, 255)));
......@@ -1623,38 +1670,12 @@ test "compare between signed and unsigned" {
16231670 try testing.expect(compare(@as(u8, 1), .eq, @as(u8, 1)));
16241671}
16251672
1626test "order" {
1673test order {
16271674 try testing.expect(order(0, 0) == .eq);
16281675 try testing.expect(order(1, 0) == .gt);
16291676 try testing.expect(order(-1, 0) == .lt);
16301677}
16311678
1632test "order.invert" {
1633 try testing.expect(Order.invert(order(0, 0)) == .eq);
1634 try testing.expect(Order.invert(order(1, 0)) == .lt);
1635 try testing.expect(Order.invert(order(-1, 0)) == .gt);
1636}
1637
1638test "order.compare" {
1639 try testing.expect(order(-1, 0).compare(.lt));
1640 try testing.expect(order(-1, 0).compare(.lte));
1641 try testing.expect(order(0, 0).compare(.lte));
1642 try testing.expect(order(0, 0).compare(.eq));
1643 try testing.expect(order(0, 0).compare(.gte));
1644 try testing.expect(order(1, 0).compare(.gte));
1645 try testing.expect(order(1, 0).compare(.gt));
1646 try testing.expect(order(1, 0).compare(.neq));
1647}
1648
1649test "compare.reverse" {
1650 inline for (@typeInfo(CompareOperator).Enum.fields) |op_field| {
1651 const op = @as(CompareOperator, @enumFromInt(op_field.value));
1652 try testing.expect(compare(2, op, 3) == compare(3, op.reverse(), 2));
1653 try testing.expect(compare(3, op, 3) == compare(3, op.reverse(), 3));
1654 try testing.expect(compare(4, op, 3) == compare(3, op.reverse(), 4));
1655 }
1656}
1657
16581679/// Returns a mask of all ones if value is true,
16591680/// and a mask of all zeroes if value is false.
16601681/// Compiles to one instruction for register sized integers.
......@@ -1676,7 +1697,7 @@ pub inline fn boolMask(comptime MaskInt: type, value: bool) MaskInt {
16761697 return -%@as(MaskInt, @intCast(@intFromBool(value)));
16771698}
16781699
1679test "boolMask" {
1700test boolMask {
16801701 const runTest = struct {
16811702 fn runTest() !void {
16821703 try testing.expectEqual(@as(u1, 0), boolMask(u1, false));
......@@ -1824,7 +1845,7 @@ fn testSign() !void {
18241845 try std.testing.expectEqual(0.0, sign(0.0));
18251846}
18261847
1827test "sign" {
1848test sign {
18281849 if (builtin.zig_backend == .stage2_llvm) {
18291850 // https://github.com/ziglang/zig/issues/12012
18301851 return error.SkipZigTest;
lib/std/mem.zig+66-64
......@@ -306,7 +306,7 @@ pub fn zeroes(comptime T: type) T {
306306 }
307307}
308308
309test "zeroes" {
309test zeroes {
310310 const C_struct = extern struct {
311311 x: u32,
312312 y: u32 align(128),
......@@ -475,7 +475,7 @@ pub fn zeroInit(comptime T: type, init: anytype) T {
475475 }
476476}
477477
478test "zeroInit" {
478test zeroInit {
479479 const I = struct {
480480 d: f64,
481481 };
......@@ -606,16 +606,19 @@ pub fn orderZ(comptime T: type, lhs: [*:0]const T, rhs: [*:0]const T) math.Order
606606 return math.order(lhs[i], rhs[i]);
607607}
608608
609test "order and orderZ" {
609test order {
610610 try testing.expect(order(u8, "abcd", "bee") == .lt);
611 try testing.expect(orderZ(u8, "abcd", "bee") == .lt);
612611 try testing.expect(order(u8, "abc", "abc") == .eq);
613 try testing.expect(orderZ(u8, "abc", "abc") == .eq);
614612 try testing.expect(order(u8, "abc", "abc0") == .lt);
615 try testing.expect(orderZ(u8, "abc", "abc0") == .lt);
616613 try testing.expect(order(u8, "", "") == .eq);
617 try testing.expect(orderZ(u8, "", "") == .eq);
618614 try testing.expect(order(u8, "", "a") == .lt);
615}
616
617test orderZ {
618 try testing.expect(orderZ(u8, "abcd", "bee") == .lt);
619 try testing.expect(orderZ(u8, "abc", "abc") == .eq);
620 try testing.expect(orderZ(u8, "abc", "abc0") == .lt);
621 try testing.expect(orderZ(u8, "", "") == .eq);
619622 try testing.expect(orderZ(u8, "", "a") == .lt);
620623}
621624
......@@ -624,7 +627,7 @@ pub fn lessThan(comptime T: type, lhs: []const T, rhs: []const T) bool {
624627 return order(T, lhs, rhs) == .lt;
625628}
626629
627test "lessThan" {
630test lessThan {
628631 try testing.expect(lessThan(u8, "abcd", "bee"));
629632 try testing.expect(!lessThan(u8, "abc", "abc"));
630633 try testing.expect(lessThan(u8, "abc", "abc0"));
......@@ -726,7 +729,7 @@ pub fn indexOfDiff(comptime T: type, a: []const T, b: []const T) ?usize {
726729 return if (a.len == b.len) null else shortest;
727730}
728731
729test "indexOfDiff" {
732test indexOfDiff {
730733 try testing.expectEqual(indexOfDiff(u8, "one", "one"), null);
731734 try testing.expectEqual(indexOfDiff(u8, "one two", "one"), 3);
732735 try testing.expectEqual(indexOfDiff(u8, "one", "one two"), 3);
......@@ -759,7 +762,7 @@ fn Span(comptime T: type) type {
759762 @compileError("invalid type given to std.mem.span: " ++ @typeName(T));
760763}
761764
762test "Span" {
765test Span {
763766 try testing.expect(Span([*:1]u16) == [:1]u16);
764767 try testing.expect(Span(?[*:1]u16) == ?[:1]u16);
765768 try testing.expect(Span([*:1]const u8) == [:1]const u8);
......@@ -793,7 +796,7 @@ pub fn span(ptr: anytype) Span(@TypeOf(ptr)) {
793796 }
794797}
795798
796test "span" {
799test span {
797800 var array: [5]u16 = [_]u16{ 1, 2, 3, 4, 5 };
798801 const ptr = @as([*:3]u16, array[0..2 :3]);
799802 try testing.expect(eql(u16, span(ptr), &[_]u16{ 1, 2 }));
......@@ -877,7 +880,7 @@ pub fn sliceTo(ptr: anytype, comptime end: std.meta.Elem(@TypeOf(ptr))) SliceTo(
877880 }
878881}
879882
880test "sliceTo" {
883test sliceTo {
881884 try testing.expectEqualSlices(u8, "aoeu", sliceTo("aoeu", 0));
882885
883886 {
......@@ -963,7 +966,7 @@ fn lenSliceTo(ptr: anytype, comptime end: std.meta.Elem(@TypeOf(ptr))) usize {
963966 @compileError("invalid type given to std.mem.sliceTo: " ++ @typeName(@TypeOf(ptr)));
964967}
965968
966test "lenSliceTo" {
969test lenSliceTo {
967970 try testing.expect(lenSliceTo("aoeu", 0) == 4);
968971
969972 {
......@@ -1018,7 +1021,7 @@ pub fn len(value: anytype) usize {
10181021 }
10191022}
10201023
1021test "len" {
1024test len {
10221025 var array: [5]u16 = [_]u16{ 1, 2, 0, 4, 5 };
10231026 const ptr = @as([*:4]u16, array[0..3 :4]);
10241027 try testing.expect(len(ptr) == 3);
......@@ -1157,7 +1160,7 @@ pub fn trim(comptime T: type, slice: []const T, values_to_strip: []const T) []co
11571160 return slice[begin..end];
11581161}
11591162
1160test "trim" {
1163test trim {
11611164 try testing.expectEqualSlices(u8, "foo\n ", trimLeft(u8, " foo\n ", " \n"));
11621165 try testing.expectEqualSlices(u8, " foo", trimRight(u8, " foo\n ", " \n"));
11631166 try testing.expectEqualSlices(u8, "foo", trim(u8, " foo\n ", " \n"));
......@@ -1240,7 +1243,7 @@ pub fn indexOfScalarPos(comptime T: type, slice: []const T, start_index: usize,
12401243 return null;
12411244}
12421245
1243test "indexOfScalarPos" {
1246test indexOfScalarPos {
12441247 const Types = [_]type{ u8, u16, u32, u64 };
12451248
12461249 inline for (Types) |T| {
......@@ -1316,7 +1319,7 @@ pub fn indexOfNonePos(comptime T: type, slice: []const T, start_index: usize, va
13161319 return null;
13171320}
13181321
1319test "indexOfNone" {
1322test indexOfNone {
13201323 try testing.expect(indexOfNone(u8, "abc123", "123").? == 0);
13211324 try testing.expect(lastIndexOfNone(u8, "abc123", "123").? == 2);
13221325 try testing.expect(indexOfNone(u8, "123abc", "123").? == 3);
......@@ -1460,7 +1463,7 @@ pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, nee
14601463 return null;
14611464}
14621465
1463test "indexOf" {
1466test indexOf {
14641467 try testing.expect(indexOf(u8, "one two three four five six seven eight nine ten eleven", "three four").? == 8);
14651468 try testing.expect(lastIndexOf(u8, "one two three four five six seven eight nine ten eleven", "three four").? == 8);
14661469 try testing.expect(indexOf(u8, "one two three four five six seven eight nine ten eleven", "two two") == null);
......@@ -1533,7 +1536,7 @@ pub fn count(comptime T: type, haystack: []const T, needle: []const T) usize {
15331536 return found;
15341537}
15351538
1536test "count" {
1539test count {
15371540 try testing.expect(count(u8, "", "h") == 0);
15381541 try testing.expect(count(u8, "h", "h") == 1);
15391542 try testing.expect(count(u8, "hh", "h") == 2);
......@@ -1565,7 +1568,7 @@ pub fn containsAtLeast(comptime T: type, haystack: []const T, expected_count: us
15651568 return false;
15661569}
15671570
1568test "containsAtLeast" {
1571test containsAtLeast {
15691572 try testing.expect(containsAtLeast(u8, "aa", 0, "a"));
15701573 try testing.expect(containsAtLeast(u8, "aa", 1, "a"));
15711574 try testing.expect(containsAtLeast(u8, "aa", 2, "a"));
......@@ -1698,6 +1701,9 @@ test readInt {
16981701
16991702 try testing.expect(readInt(i16, &[_]u8{ 0xff, 0xfd }, .big) == -3);
17001703 try testing.expect(readInt(i16, &[_]u8{ 0xfc, 0xff }, .little) == -4);
1704
1705 try moreReadIntTests();
1706 try comptime moreReadIntTests();
17011707}
17021708
17031709fn readPackedIntLittle(comptime T: type, bytes: []const u8, bit_offset: usize) T {
......@@ -2027,7 +2033,7 @@ pub fn byteSwapAllFields(comptime S: type, ptr: *S) void {
20272033 }
20282034}
20292035
2030test "byteSwapAllFields" {
2036test byteSwapAllFields {
20312037 const T = extern struct {
20322038 f0: u8,
20332039 f1: u16,
......@@ -2136,7 +2142,7 @@ pub fn tokenizeScalar(comptime T: type, buffer: []const T, delimiter: T) TokenIt
21362142 };
21372143}
21382144
2139test "tokenizeScalar" {
2145test tokenizeScalar {
21402146 var it = tokenizeScalar(u8, " abc def ghi ", ' ');
21412147 try testing.expect(eql(u8, it.next().?, "abc"));
21422148 try testing.expect(eql(u8, it.peek().?, "def"));
......@@ -2177,7 +2183,7 @@ test "tokenizeScalar" {
21772183 try testing.expect(it16.next() == null);
21782184}
21792185
2180test "tokenizeAny" {
2186test tokenizeAny {
21812187 var it = tokenizeAny(u8, "a|b,c/d e", " /,|");
21822188 try testing.expect(eql(u8, it.next().?, "a"));
21832189 try testing.expect(eql(u8, it.peek().?, "b"));
......@@ -2205,7 +2211,7 @@ test "tokenizeAny" {
22052211 try testing.expect(it16.next() == null);
22062212}
22072213
2208test "tokenizeSequence" {
2214test tokenizeSequence {
22092215 var it = tokenizeSequence(u8, "a<>b<><>c><>d><", "<>");
22102216 try testing.expectEqualStrings("a", it.next().?);
22112217 try testing.expectEqualStrings("b", it.peek().?);
......@@ -2334,7 +2340,7 @@ pub fn splitScalar(comptime T: type, buffer: []const T, delimiter: T) SplitItera
23342340 };
23352341}
23362342
2337test "splitScalar" {
2343test splitScalar {
23382344 var it = splitScalar(u8, "abc|def||ghi", '|');
23392345 try testing.expectEqualSlices(u8, it.rest(), "abc|def||ghi");
23402346 try testing.expectEqualSlices(u8, it.first(), "abc");
......@@ -2377,7 +2383,7 @@ test "splitScalar" {
23772383 try testing.expect(it16.next() == null);
23782384}
23792385
2380test "splitSequence" {
2386test splitSequence {
23812387 var it = splitSequence(u8, "a, b ,, c, d, e", ", ");
23822388 try testing.expectEqualSlices(u8, it.first(), "a");
23832389 try testing.expectEqualSlices(u8, it.rest(), "b ,, c, d, e");
......@@ -2400,7 +2406,7 @@ test "splitSequence" {
24002406 try testing.expect(it16.next() == null);
24012407}
24022408
2403test "splitAny" {
2409test splitAny {
24042410 var it = splitAny(u8, "a,b, c d e", ", ");
24052411 try testing.expectEqualSlices(u8, it.first(), "a");
24062412 try testing.expectEqualSlices(u8, it.rest(), "b, c d e");
......@@ -2536,7 +2542,7 @@ pub fn splitBackwardsScalar(comptime T: type, buffer: []const T, delimiter: T) S
25362542 };
25372543}
25382544
2539test "splitBackwardsScalar" {
2545test splitBackwardsScalar {
25402546 var it = splitBackwardsScalar(u8, "abc|def||ghi", '|');
25412547 try testing.expectEqualSlices(u8, it.rest(), "abc|def||ghi");
25422548 try testing.expectEqualSlices(u8, it.first(), "ghi");
......@@ -2575,7 +2581,7 @@ test "splitBackwardsScalar" {
25752581 try testing.expect(it16.next() == null);
25762582}
25772583
2578test "splitBackwardsSequence" {
2584test splitBackwardsSequence {
25792585 var it = splitBackwardsSequence(u8, "a, b ,, c, d, e", ", ");
25802586 try testing.expectEqualSlices(u8, it.rest(), "a, b ,, c, d, e");
25812587 try testing.expectEqualSlices(u8, it.first(), "e");
......@@ -2608,7 +2614,7 @@ test "splitBackwardsSequence" {
26082614 try testing.expect(it16.next() == null);
26092615}
26102616
2611test "splitBackwardsAny" {
2617test splitBackwardsAny {
26122618 var it = splitBackwardsAny(u8, "a,b, c d e", ", ");
26132619 try testing.expectEqualSlices(u8, it.rest(), "a,b, c d e");
26142620 try testing.expectEqualSlices(u8, it.first(), "e");
......@@ -2715,7 +2721,7 @@ pub fn window(comptime T: type, buffer: []const T, size: usize, advance: usize)
27152721 };
27162722}
27172723
2718test "window" {
2724test window {
27192725 {
27202726 // moving average size 3
27212727 var it = window(u8, "abcdefg", 3, 1);
......@@ -2841,7 +2847,7 @@ pub fn startsWith(comptime T: type, haystack: []const T, needle: []const T) bool
28412847 return if (needle.len > haystack.len) false else eql(T, haystack[0..needle.len], needle);
28422848}
28432849
2844test "startsWith" {
2850test startsWith {
28452851 try testing.expect(startsWith(u8, "Bob", "Bo"));
28462852 try testing.expect(!startsWith(u8, "Needle in haystack", "haystack"));
28472853}
......@@ -2850,7 +2856,7 @@ pub fn endsWith(comptime T: type, haystack: []const T, needle: []const T) bool {
28502856 return if (needle.len > haystack.len) false else eql(T, haystack[haystack.len - needle.len ..], needle);
28512857}
28522858
2853test "endsWith" {
2859test endsWith {
28542860 try testing.expect(endsWith(u8, "Needle in haystack", "haystack"));
28552861 try testing.expect(!endsWith(u8, "Bob", "Bo"));
28562862}
......@@ -3086,7 +3092,7 @@ fn joinMaybeZ(allocator: Allocator, separator: []const u8, slices: []const []con
30863092 return buf;
30873093}
30883094
3089test "join" {
3095test join {
30903096 {
30913097 const str = try join(testing.allocator, ",", &[_][]const u8{});
30923098 defer testing.allocator.free(str);
......@@ -3109,7 +3115,7 @@ test "join" {
31093115 }
31103116}
31113117
3112test "joinZ" {
3118test joinZ {
31133119 {
31143120 const str = try joinZ(testing.allocator, ",", &[_][]const u8{});
31153121 defer testing.allocator.free(str);
......@@ -3181,7 +3187,7 @@ pub fn concatMaybeSentinel(allocator: Allocator, comptime T: type, slices: []con
31813187 return buf;
31823188}
31833189
3184test "concat" {
3190test concat {
31853191 {
31863192 const str = try concat(testing.allocator, u8, &[_][]const u8{ "abc", "def", "ghi" });
31873193 defer testing.allocator.free(str);
......@@ -3219,17 +3225,13 @@ test "concat" {
32193225 }
32203226}
32213227
3222test "testStringEquality" {
3228test eql {
32233229 try testing.expect(eql(u8, "abcd", "abcd"));
32243230 try testing.expect(!eql(u8, "abcdef", "abZdef"));
32253231 try testing.expect(!eql(u8, "abcdefg", "abcdef"));
32263232}
32273233
3228test "testReadInt" {
3229 try testReadIntImpl();
3230 try comptime testReadIntImpl();
3231}
3232fn testReadIntImpl() !void {
3234fn moreReadIntTests() !void {
32333235 {
32343236 const bytes = [_]u8{
32353237 0x12,
......@@ -3287,7 +3289,7 @@ pub fn min(comptime T: type, slice: []const T) T {
32873289 return best;
32883290}
32893291
3290test "min" {
3292test min {
32913293 try testing.expectEqual(min(u8, "abcdefg"), 'a');
32923294 try testing.expectEqual(min(u8, "bcdefga"), 'a');
32933295 try testing.expectEqual(min(u8, "a"), 'a');
......@@ -3304,7 +3306,7 @@ pub fn max(comptime T: type, slice: []const T) T {
33043306 return best;
33053307}
33063308
3307test "max" {
3309test max {
33083310 try testing.expectEqual(max(u8, "abcdefg"), 'g');
33093311 try testing.expectEqual(max(u8, "gabcdef"), 'g');
33103312 try testing.expectEqual(max(u8, "g"), 'g');
......@@ -3357,7 +3359,7 @@ pub fn indexOfMin(comptime T: type, slice: []const T) usize {
33573359 return index;
33583360}
33593361
3360test "indexOfMin" {
3362test indexOfMin {
33613363 try testing.expectEqual(indexOfMin(u8, "abcdefg"), 0);
33623364 try testing.expectEqual(indexOfMin(u8, "bcdefga"), 6);
33633365 try testing.expectEqual(indexOfMin(u8, "a"), 0);
......@@ -3378,7 +3380,7 @@ pub fn indexOfMax(comptime T: type, slice: []const T) usize {
33783380 return index;
33793381}
33803382
3381test "indexOfMax" {
3383test indexOfMax {
33823384 try testing.expectEqual(indexOfMax(u8, "abcdefg"), 6);
33833385 try testing.expectEqual(indexOfMax(u8, "gabcdef"), 0);
33843386 try testing.expectEqual(indexOfMax(u8, "a"), 0);
......@@ -3408,7 +3410,7 @@ pub fn indexOfMinMax(comptime T: type, slice: []const T) IndexOfMinMaxResult {
34083410
34093411pub const IndexOfMinMaxResult = struct { index_min: usize, index_max: usize };
34103412
3411test "indexOfMinMax" {
3413test indexOfMinMax {
34123414 try testing.expectEqual(IndexOfMinMaxResult{ .index_min = 0, .index_max = 6 }, indexOfMinMax(u8, "abcdefg"));
34133415 try testing.expectEqual(IndexOfMinMaxResult{ .index_min = 1, .index_max = 0 }, indexOfMinMax(u8, "gabcdef"));
34143416 try testing.expectEqual(IndexOfMinMaxResult{ .index_min = 0, .index_max = 0 }, indexOfMinMax(u8, "a"));
......@@ -3429,7 +3431,7 @@ pub fn reverse(comptime T: type, items: []T) void {
34293431 }
34303432}
34313433
3432test "reverse" {
3434test reverse {
34333435 var arr = [_]i32{ 5, 3, 1, 2, 4 };
34343436 reverse(i32, arr[0..]);
34353437
......@@ -3490,7 +3492,7 @@ pub fn reverseIterator(slice: anytype) ReverseIterator(@TypeOf(slice)) {
34903492 return .{ .ptr = slice.ptr, .index = slice.len };
34913493}
34923494
3493test "reverseIterator" {
3495test reverseIterator {
34943496 {
34953497 var it = reverseIterator("abc");
34963498 try testing.expectEqual(@as(?u8, 'c'), it.next());
......@@ -3548,7 +3550,7 @@ pub fn rotate(comptime T: type, items: []T, amount: usize) void {
35483550 reverse(T, items);
35493551}
35503552
3551test "rotate" {
3553test rotate {
35523554 var arr = [_]i32{ 5, 3, 1, 2, 4 };
35533555 rotate(i32, arr[0..], 2);
35543556
......@@ -3582,7 +3584,7 @@ pub fn replace(comptime T: type, input: []const T, needle: []const T, replacemen
35823584 return replacements;
35833585}
35843586
3585test "replace" {
3587test replace {
35863588 var output: [29]u8 = undefined;
35873589 var replacements = replace(u8, "All your base are belong to us", "base", "Zig", output[0..]);
35883590 var expected: []const u8 = "All your Zig are belong to us";
......@@ -3645,7 +3647,7 @@ fn testCollapseRepeats(str: []const u8, elem: u8, expected: []const u8) !void {
36453647 defer std.testing.allocator.free(mutable);
36463648 try testing.expect(std.mem.eql(u8, collapseRepeats(u8, mutable, elem), expected));
36473649}
3648test "collapseRepeats" {
3650test collapseRepeats {
36493651 try testCollapseRepeats("", '/', "");
36503652 try testCollapseRepeats("a", '/', "a");
36513653 try testCollapseRepeats("/", '/', "/");
......@@ -3679,7 +3681,7 @@ pub fn replacementSize(comptime T: type, input: []const T, needle: []const T, re
36793681 return size;
36803682}
36813683
3682test "replacementSize" {
3684test replacementSize {
36833685 try testing.expect(replacementSize(u8, "All your base are belong to us", "base", "Zig") == 29);
36843686 try testing.expect(replacementSize(u8, "Favor reading code over writing code.", "code", "") == 29);
36853687 try testing.expect(replacementSize(u8, "Only one obvious way to do things.", "things.", "things in Zig.") == 41);
......@@ -3699,7 +3701,7 @@ pub fn replaceOwned(comptime T: type, allocator: Allocator, input: []const T, ne
36993701 return output;
37003702}
37013703
3702test "replaceOwned" {
3704test replaceOwned {
37033705 const gpa = std.testing.allocator;
37043706
37053707 const base_replace = replaceOwned(u8, gpa, "All your base are belong to us", "base", "Zig") catch @panic("out of memory");
......@@ -3803,7 +3805,7 @@ pub fn alignPointer(ptr: anytype, align_to: usize) ?@TypeOf(ptr) {
38033805 return @alignCast(ptr + adjust_off);
38043806}
38053807
3806test "alignPointer" {
3808test alignPointer {
38073809 const S = struct {
38083810 fn checkAlign(comptime T: type, base: usize, align_to: usize, expected: usize) !void {
38093811 const ptr: T = @ptrFromInt(base);
......@@ -3852,7 +3854,7 @@ pub fn asBytes(ptr: anytype) AsBytesReturnType(@TypeOf(ptr)) {
38523854 return @ptrCast(@alignCast(ptr));
38533855}
38543856
3855test "asBytes" {
3857test asBytes {
38563858 const deadbeef = @as(u32, 0xDEADBEEF);
38573859 const deadbeef_bytes = switch (native_endian) {
38583860 .big => "\xDE\xAD\xBE\xEF",
......@@ -3912,7 +3914,7 @@ pub fn toBytes(value: anytype) [@sizeOf(@TypeOf(value))]u8 {
39123914 return asBytes(&value).*;
39133915}
39143916
3915test "toBytes" {
3917test toBytes {
39163918 var my_bytes = toBytes(@as(u32, 0x12345678));
39173919 switch (native_endian) {
39183920 .big => try testing.expect(eql(u8, &my_bytes, "\x12\x34\x56\x78")),
......@@ -3936,7 +3938,7 @@ pub fn bytesAsValue(comptime T: type, bytes: anytype) BytesAsValueReturnType(T,
39363938 return @ptrCast(bytes);
39373939}
39383940
3939test "bytesAsValue" {
3941test bytesAsValue {
39403942 const deadbeef = @as(u32, 0xDEADBEEF);
39413943 const deadbeef_bytes = switch (native_endian) {
39423944 .big => "\xDE\xAD\xBE\xEF",
......@@ -3995,7 +3997,7 @@ test "bytesAsValue preserves pointer attributes" {
39953997pub fn bytesToValue(comptime T: type, bytes: anytype) T {
39963998 return bytesAsValue(T, bytes).*;
39973999}
3998test "bytesToValue" {
4000test bytesToValue {
39994001 const deadbeef_bytes = switch (native_endian) {
40004002 .big => "\xDE\xAD\xBE\xEF",
40014003 .little => "\xEF\xBE\xAD\xDE",
......@@ -4023,7 +4025,7 @@ pub fn bytesAsSlice(comptime T: type, bytes: anytype) BytesAsSliceReturnType(T,
40234025 return @as(cast_target, @ptrCast(bytes))[0..@divExact(bytes.len, @sizeOf(T))];
40244026}
40254027
4026test "bytesAsSlice" {
4028test bytesAsSlice {
40274029 {
40284030 const bytes = [_]u8{ 0xDE, 0xAD, 0xBE, 0xEF };
40294031 const slice = bytesAsSlice(u16, bytes[0..]);
......@@ -4112,7 +4114,7 @@ pub fn sliceAsBytes(slice: anytype) SliceAsBytesReturnType(@TypeOf(slice)) {
41124114 return @as(cast_target, @ptrCast(slice))[0 .. slice.len * @sizeOf(std.meta.Elem(Slice))];
41134115}
41144116
4115test "sliceAsBytes" {
4117test sliceAsBytes {
41164118 const bytes = [_]u16{ 0xDEAD, 0xBEEF };
41174119 const slice = sliceAsBytes(bytes[0..]);
41184120 try testing.expect(slice.len == 4);
......@@ -4270,7 +4272,7 @@ fn doNotOptimizeAwayC(ptr: anytype) void {
42704272 dest.* = 0;
42714273}
42724274
4273test "doNotOptimizeAway" {
4275test doNotOptimizeAway {
42744276 comptime doNotOptimizeAway("test");
42754277
42764278 doNotOptimizeAway(null);
......@@ -4295,7 +4297,7 @@ test "doNotOptimizeAway" {
42954297 doNotOptimizeAway(@as(std.builtin.Endian, .little));
42964298}
42974299
4298test "alignForward" {
4300test alignForward {
42994301 try testing.expect(alignForward(usize, 1, 1) == 1);
43004302 try testing.expect(alignForward(usize, 2, 1) == 2);
43014303 try testing.expect(alignForward(usize, 1, 2) == 2);
......@@ -4364,7 +4366,7 @@ pub fn isAlignedGeneric(comptime T: type, addr: T, alignment: T) bool {
43644366 return alignBackward(T, addr, alignment) == addr;
43654367}
43664368
4367test "isAligned" {
4369test isAligned {
43684370 try testing.expect(isAligned(0, 4));
43694371 try testing.expect(isAligned(1, 1));
43704372 try testing.expect(isAligned(2, 1));
lib/std/meta/trailer_flags.zig+1-1
......@@ -132,7 +132,7 @@ pub fn TrailerFlags(comptime Fields: type) type {
132132 };
133133}
134134
135test "TrailerFlags" {
135test TrailerFlags {
136136 const Flags = TrailerFlags(struct {
137137 a: i32,
138138 b: bool,
lib/std/os/windows.zig+4-4
......@@ -1077,7 +1077,7 @@ pub fn QueryObjectName(
10771077 else => |e| return unexpectedStatus(e),
10781078 }
10791079}
1080test "QueryObjectName" {
1080test QueryObjectName {
10811081 if (builtin.os.tag != .windows)
10821082 return;
10831083
......@@ -1245,7 +1245,7 @@ pub fn GetFinalPathNameByHandle(
12451245 }
12461246}
12471247
1248test "GetFinalPathNameByHandle" {
1248test GetFinalPathNameByHandle {
12491249 if (builtin.os.tag != .windows)
12501250 return;
12511251
......@@ -2467,7 +2467,7 @@ pub fn ntToWin32Namespace(path: []const u16) !PathSpace {
24672467 }
24682468}
24692469
2470test "ntToWin32Namespace" {
2470test ntToWin32Namespace {
24712471 const L = std.unicode.utf8ToUtf16LeStringLiteral;
24722472
24732473 try testNtToWin32Namespace(L("UNC"), L("\\??\\UNC"));
......@@ -3386,7 +3386,7 @@ pub const GUID = extern struct {
33863386 }
33873387};
33883388
3389test "GUID" {
3389test GUID {
33903390 try std.testing.expectEqual(
33913391 GUID{
33923392 .Data1 = 0x01234567,
lib/std/process.zig+3-3
......@@ -209,7 +209,7 @@ pub const EnvMap = struct {
209209 }
210210};
211211
212test "EnvMap" {
212test EnvMap {
213213 var env = EnvMap.init(testing.allocator);
214214 defer env.deinit();
215215
......@@ -371,7 +371,7 @@ pub fn getEnvMap(allocator: Allocator) GetEnvMapError!EnvMap {
371371 }
372372}
373373
374test "getEnvMap" {
374test getEnvMap {
375375 var env = try getEnvMap(testing.allocator);
376376 defer env.deinit();
377377}
......@@ -1132,7 +1132,7 @@ pub fn argsFree(allocator: Allocator, args_alloc: []const [:0]u8) void {
11321132 return allocator.free(aligned_allocated_buf);
11331133}
11341134
1135test "ArgIteratorWindows" {
1135test ArgIteratorWindows {
11361136 const t = testArgIteratorWindows;
11371137
11381138 try t(
lib/std/sort.zig+9-9
......@@ -430,7 +430,7 @@ pub fn binarySearch(
430430 return null;
431431}
432432
433test "binarySearch" {
433test binarySearch {
434434 const S = struct {
435435 fn order_u32(context: void, lhs: u32, rhs: u32) math.Order {
436436 _ = context;
......@@ -537,7 +537,7 @@ pub fn lowerBound(
537537 return left;
538538}
539539
540test "lowerBound" {
540test lowerBound {
541541 const S = struct {
542542 fn lower_u32(context: void, lhs: u32, rhs: u32) bool {
543543 _ = context;
......@@ -627,7 +627,7 @@ pub fn upperBound(
627627 return left;
628628}
629629
630test "upperBound" {
630test upperBound {
631631 const S = struct {
632632 fn lower_u32(context: void, lhs: u32, rhs: u32) bool {
633633 _ = context;
......@@ -712,7 +712,7 @@ pub fn equalRange(
712712 };
713713}
714714
715test "equalRange" {
715test equalRange {
716716 const S = struct {
717717 fn lower_u32(context: void, lhs: u32, rhs: u32) bool {
718718 _ = context;
......@@ -792,7 +792,7 @@ pub fn argMin(
792792 return smallest_index;
793793}
794794
795test "argMin" {
795test argMin {
796796 try testing.expectEqual(@as(?usize, null), argMin(i32, &[_]i32{}, {}, asc_i32));
797797 try testing.expectEqual(@as(?usize, 0), argMin(i32, &[_]i32{1}, {}, asc_i32));
798798 try testing.expectEqual(@as(?usize, 0), argMin(i32, &[_]i32{ 1, 2, 3, 4, 5 }, {}, asc_i32));
......@@ -812,7 +812,7 @@ pub fn min(
812812 return items[i];
813813}
814814
815test "min" {
815test min {
816816 try testing.expectEqual(@as(?i32, null), min(i32, &[_]i32{}, {}, asc_i32));
817817 try testing.expectEqual(@as(?i32, 1), min(i32, &[_]i32{1}, {}, asc_i32));
818818 try testing.expectEqual(@as(?i32, 1), min(i32, &[_]i32{ 1, 2, 3, 4, 5 }, {}, asc_i32));
......@@ -844,7 +844,7 @@ pub fn argMax(
844844 return biggest_index;
845845}
846846
847test "argMax" {
847test argMax {
848848 try testing.expectEqual(@as(?usize, null), argMax(i32, &[_]i32{}, {}, asc_i32));
849849 try testing.expectEqual(@as(?usize, 0), argMax(i32, &[_]i32{1}, {}, asc_i32));
850850 try testing.expectEqual(@as(?usize, 4), argMax(i32, &[_]i32{ 1, 2, 3, 4, 5 }, {}, asc_i32));
......@@ -864,7 +864,7 @@ pub fn max(
864864 return items[i];
865865}
866866
867test "max" {
867test max {
868868 try testing.expectEqual(@as(?i32, null), max(i32, &[_]i32{}, {}, asc_i32));
869869 try testing.expectEqual(@as(?i32, 1), max(i32, &[_]i32{1}, {}, asc_i32));
870870 try testing.expectEqual(@as(?i32, 5), max(i32, &[_]i32{ 1, 2, 3, 4, 5 }, {}, asc_i32));
......@@ -890,7 +890,7 @@ pub fn isSorted(
890890 return true;
891891}
892892
893test "isSorted" {
893test isSorted {
894894 try testing.expect(isSorted(i32, &[_]i32{}, {}, asc_i32));
895895 try testing.expect(isSorted(i32, &[_]i32{10}, {}, asc_i32));
896896 try testing.expect(isSorted(i32, &[_]i32{ 1, 2, 3, 4, 5 }, {}, asc_i32));
lib/std/tar.zig+2-2
......@@ -676,7 +676,7 @@ fn stripComponents(path: []const u8, count: u32) []const u8 {
676676 return path[i..];
677677}
678678
679test "stripComponents" {
679test stripComponents {
680680 const expectEqualStrings = testing.expectEqualStrings;
681681 try expectEqualStrings("a/b/c", stripComponents("a/b/c", 0));
682682 try expectEqualStrings("b/c", stripComponents("a/b/c", 1));
......@@ -685,7 +685,7 @@ test "stripComponents" {
685685 try expectEqualStrings("", stripComponents("a/b/c", 4));
686686}
687687
688test "PaxIterator" {
688test PaxIterator {
689689 const Attr = struct {
690690 kind: PaxAttributeKind,
691691 value: []const u8 = undefined,
lib/std/testing.zig+2-2
......@@ -242,7 +242,7 @@ fn expectApproxEqAbsInner(comptime T: type, expected: T, actual: T, tolerance: T
242242 }
243243}
244244
245test "expectApproxEqAbs" {
245test expectApproxEqAbs {
246246 inline for ([_]type{ f16, f32, f64, f128 }) |T| {
247247 const pos_x: T = 12.0;
248248 const pos_y: T = 12.06;
......@@ -278,7 +278,7 @@ fn expectApproxEqRelInner(comptime T: type, expected: T, actual: T, tolerance: T
278278 }
279279}
280280
281test "expectApproxEqRel" {
281test expectApproxEqRel {
282282 inline for ([_]type{ f16, f32, f64, f128 }) |T| {
283283 const eps_value = comptime math.floatEps(T);
284284 const sqrt_eps_value = comptime @sqrt(eps_value);
lib/std/time.zig+3-3
......@@ -52,7 +52,7 @@ pub fn sleep(nanoseconds: u64) void {
5252 std.os.nanosleep(s, ns);
5353}
5454
55test "sleep" {
55test sleep {
5656 sleep(1);
5757}
5858
......@@ -122,7 +122,7 @@ pub fn nanoTimestamp() i128 {
122122 }
123123}
124124
125test "timestamp" {
125test milliTimestamp {
126126 const margin = ns_per_ms * 50;
127127
128128 const time_0 = milliTimestamp();
......@@ -326,7 +326,7 @@ pub const Timer = struct {
326326 }
327327};
328328
329test "Timer + Instant" {
329test Timer {
330330 const margin = ns_per_ms * 150;
331331
332332 var timer = try Timer.start();
lib/std/time/epoch.zig+1-1
......@@ -53,7 +53,7 @@ pub fn isLeapYear(year: Year) bool {
5353 return (0 == @mod(year, 400));
5454}
5555
56test "isLeapYear" {
56test isLeapYear {
5757 try testing.expectEqual(false, isLeapYear(2095));
5858 try testing.expectEqual(true, isLeapYear(2096));
5959 try testing.expectEqual(false, isLeapYear(2100));
lib/std/unicode.zig+4-4
......@@ -907,7 +907,7 @@ pub fn fmtUtf8(utf8: []const u8) std.fmt.Formatter(formatUtf8) {
907907 return .{ .data = utf8 };
908908}
909909
910test "fmtUtf8" {
910test fmtUtf8 {
911911 const expectFmt = testing.expectFmt;
912912 try expectFmt("", "{}", .{fmtUtf8("")});
913913 try expectFmt("foo", "{}", .{fmtUtf8("foo")});
......@@ -1249,7 +1249,7 @@ pub fn utf8ToUtf16LeImpl(utf16le: []u16, utf8: []const u8, comptime surrogates:
12491249 return dest_index;
12501250}
12511251
1252test "utf8ToUtf16Le" {
1252test utf8ToUtf16Le {
12531253 var utf16le: [128]u16 = undefined;
12541254 {
12551255 const length = try utf8ToUtf16Le(utf16le[0..], "𐐷");
......@@ -1430,7 +1430,7 @@ pub fn fmtUtf16Le(utf16le: []const u16) std.fmt.Formatter(formatUtf16Le) {
14301430 return .{ .data = utf16le };
14311431}
14321432
1433test "fmtUtf16Le" {
1433test fmtUtf16Le {
14341434 const expectFmt = testing.expectFmt;
14351435 try expectFmt("", "{}", .{fmtUtf16Le(utf8ToUtf16LeStringLiteral(""))});
14361436 try expectFmt("foo", "{}", .{fmtUtf16Le(utf8ToUtf16LeStringLiteral("foo"))});
......@@ -1443,7 +1443,7 @@ test "fmtUtf16Le" {
14431443 try expectFmt("ξ€€", "{}", .{fmtUtf16Le(&[_]u16{mem.readInt(u16, "\x00\xe0", native_endian)})});
14441444}
14451445
1446test "utf8ToUtf16LeStringLiteral" {
1446test utf8ToUtf16LeStringLiteral {
14471447 {
14481448 const bytes = [_:0]u16{
14491449 mem.nativeToLittle(u16, 0x41),
lib/std/valgrind/memcheck.zig+2-2
......@@ -137,7 +137,7 @@ pub fn countLeaks() CountResult {
137137 return res;
138138}
139139
140test "countLeaks" {
140test countLeaks {
141141 try testing.expectEqual(
142142 @as(CountResult, .{
143143 .leaked = 0,
......@@ -167,7 +167,7 @@ pub fn countLeakBlocks() CountResult {
167167 return res;
168168}
169169
170test "countLeakBlocks" {
170test countLeakBlocks {
171171 try testing.expectEqual(
172172 @as(CountResult, .{
173173 .leaked = 0,
lib/std/zig/ErrorBundle.zig+77-77
......@@ -642,96 +642,96 @@ pub const Wip = struct {
642642 i += 1;
643643 }
644644 }
645};
646645
647test "addBundleAsRoots" {
648 var bundle = bundle: {
649 var wip: ErrorBundle.Wip = undefined;
650 try wip.init(std.testing.allocator);
651 errdefer wip.deinit();
652
653 var ref_traces: [3]ReferenceTrace = undefined;
654 for (&ref_traces, 0..) |*ref_trace, i| {
655 if (i == ref_traces.len - 1) {
656 // sentinel reference trace
657 ref_trace.* = .{
658 .decl_name = 3, // signifies 3 hidden references
659 .src_loc = .none,
660 };
661 } else {
662 ref_trace.* = .{
663 .decl_name = try wip.addString("foo"),
664 .src_loc = try wip.addSourceLocation(.{
665 .src_path = try wip.addString("foo"),
666 .line = 1,
667 .column = 2,
668 .span_start = 3,
669 .span_main = 4,
670 .span_end = 5,
671 .source_line = 0,
672 }),
673 };
646 test addBundleAsRoots {
647 var bundle = bundle: {
648 var wip: ErrorBundle.Wip = undefined;
649 try wip.init(std.testing.allocator);
650 errdefer wip.deinit();
651
652 var ref_traces: [3]ReferenceTrace = undefined;
653 for (&ref_traces, 0..) |*ref_trace, i| {
654 if (i == ref_traces.len - 1) {
655 // sentinel reference trace
656 ref_trace.* = .{
657 .decl_name = 3, // signifies 3 hidden references
658 .src_loc = .none,
659 };
660 } else {
661 ref_trace.* = .{
662 .decl_name = try wip.addString("foo"),
663 .src_loc = try wip.addSourceLocation(.{
664 .src_path = try wip.addString("foo"),
665 .line = 1,
666 .column = 2,
667 .span_start = 3,
668 .span_main = 4,
669 .span_end = 5,
670 .source_line = 0,
671 }),
672 };
673 }
674674 }
675 }
676675
677 const src_loc = try wip.addSourceLocation(.{
678 .src_path = try wip.addString("foo"),
679 .line = 1,
680 .column = 2,
681 .span_start = 3,
682 .span_main = 4,
683 .span_end = 5,
684 .source_line = try wip.addString("some source code"),
685 .reference_trace_len = ref_traces.len,
686 });
687 for (&ref_traces) |ref_trace| {
688 try wip.addReferenceTrace(ref_trace);
689 }
690
691 try wip.addRootErrorMessage(ErrorMessage{
692 .msg = try wip.addString("hello world"),
693 .src_loc = src_loc,
694 .notes_len = 1,
695 });
696 const i = try wip.reserveNotes(1);
697 const note_index = @intFromEnum(wip.addErrorMessageAssumeCapacity(.{
698 .msg = try wip.addString("this is a note"),
699 .src_loc = try wip.addSourceLocation(.{
700 .src_path = try wip.addString("bar"),
676 const src_loc = try wip.addSourceLocation(.{
677 .src_path = try wip.addString("foo"),
701678 .line = 1,
702679 .column = 2,
703680 .span_start = 3,
704681 .span_main = 4,
705682 .span_end = 5,
706 .source_line = try wip.addString("another line of source"),
707 }),
708 }));
709 wip.extra.items[i] = note_index;
683 .source_line = try wip.addString("some source code"),
684 .reference_trace_len = ref_traces.len,
685 });
686 for (&ref_traces) |ref_trace| {
687 try wip.addReferenceTrace(ref_trace);
688 }
710689
711 break :bundle try wip.toOwnedBundle("");
712 };
713 defer bundle.deinit(std.testing.allocator);
690 try wip.addRootErrorMessage(ErrorMessage{
691 .msg = try wip.addString("hello world"),
692 .src_loc = src_loc,
693 .notes_len = 1,
694 });
695 const i = try wip.reserveNotes(1);
696 const note_index = @intFromEnum(wip.addErrorMessageAssumeCapacity(.{
697 .msg = try wip.addString("this is a note"),
698 .src_loc = try wip.addSourceLocation(.{
699 .src_path = try wip.addString("bar"),
700 .line = 1,
701 .column = 2,
702 .span_start = 3,
703 .span_main = 4,
704 .span_end = 5,
705 .source_line = try wip.addString("another line of source"),
706 }),
707 }));
708 wip.extra.items[i] = note_index;
709
710 break :bundle try wip.toOwnedBundle("");
711 };
712 defer bundle.deinit(std.testing.allocator);
714713
715 const ttyconf: std.io.tty.Config = .no_color;
714 const ttyconf: std.io.tty.Config = .no_color;
716715
717 var bundle_buf = std.ArrayList(u8).init(std.testing.allocator);
718 defer bundle_buf.deinit();
719 try bundle.renderToWriter(.{ .ttyconf = ttyconf }, bundle_buf.writer());
716 var bundle_buf = std.ArrayList(u8).init(std.testing.allocator);
717 defer bundle_buf.deinit();
718 try bundle.renderToWriter(.{ .ttyconf = ttyconf }, bundle_buf.writer());
720719
721 var copy = copy: {
722 var wip: ErrorBundle.Wip = undefined;
723 try wip.init(std.testing.allocator);
724 errdefer wip.deinit();
720 var copy = copy: {
721 var wip: ErrorBundle.Wip = undefined;
722 try wip.init(std.testing.allocator);
723 errdefer wip.deinit();
725724
726 try wip.addBundleAsRoots(bundle);
725 try wip.addBundleAsRoots(bundle);
727726
728 break :copy try wip.toOwnedBundle("");
729 };
730 defer copy.deinit(std.testing.allocator);
727 break :copy try wip.toOwnedBundle("");
728 };
729 defer copy.deinit(std.testing.allocator);
731730
732 var copy_buf = std.ArrayList(u8).init(std.testing.allocator);
733 defer copy_buf.deinit();
734 try copy.renderToWriter(.{ .ttyconf = ttyconf }, copy_buf.writer());
731 var copy_buf = std.ArrayList(u8).init(std.testing.allocator);
732 defer copy_buf.deinit();
733 try copy.renderToWriter(.{ .ttyconf = ttyconf }, copy_buf.writer());
735734
736 try std.testing.expectEqualStrings(bundle_buf.items, copy_buf.items);
737}
735 try std.testing.expectEqualStrings(bundle_buf.items, copy_buf.items);
736 }
737};
lib/std/zig/primitives.zig+1-1
......@@ -51,7 +51,7 @@ pub fn isPrimitive(name: []const u8) bool {
5151 return true;
5252}
5353
54test "isPrimitive" {
54test isPrimitive {
5555 const expect = std.testing.expect;
5656 try expect(!isPrimitive(""));
5757 try expect(!isPrimitive("_"));
lib/std/zig/string_literal.zig+2-2
......@@ -148,7 +148,7 @@ pub fn parseEscapeSequence(slice: []const u8, offset: *usize) ParsedCharLiteral
148148 }
149149}
150150
151test "parseCharLiteral" {
151test parseCharLiteral {
152152 try std.testing.expectEqual(
153153 ParsedCharLiteral{ .success = 'a' },
154154 parseCharLiteral("'a'"),
......@@ -281,7 +281,7 @@ pub fn parseAlloc(allocator: std.mem.Allocator, bytes: []const u8) ParseError![]
281281 }
282282}
283283
284test "parse" {
284test parseAlloc {
285285 const expect = std.testing.expect;
286286 const expectError = std.testing.expectError;
287287 const eql = std.mem.eql;