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 {...@@ -147,7 +147,7 @@ pub fn isWhitespace(c: u8) bool {
147/// See also: `isWhitespace`147/// See also: `isWhitespace`
148pub const whitespace = [_]u8{ ' ', '\t', '\n', '\r', control_code.vt, control_code.ff };148pub const whitespace = [_]u8{ ' ', '\t', '\n', '\r', control_code.vt, control_code.ff };
149149
150test "whitespace" {150test whitespace {
151 for (whitespace) |char| try std.testing.expect(isWhitespace(char));151 for (whitespace) |char| try std.testing.expect(isWhitespace(char));
152152
153 var i: u8 = 0;153 var i: u8 = 0;
...@@ -278,7 +278,7 @@ pub fn lowerString(output: []u8, ascii_string: []const u8) []u8 {...@@ -278,7 +278,7 @@ pub fn lowerString(output: []u8, ascii_string: []const u8) []u8 {
278 return output[0..ascii_string.len];278 return output[0..ascii_string.len];
279}279}
280280
281test "lowerString" {281test lowerString {
282 var buf: [1024]u8 = undefined;282 var buf: [1024]u8 = undefined;
283 const result = lowerString(&buf, "aBcDeFgHiJkLmNOPqrst0234+πŸ’©!");283 const result = lowerString(&buf, "aBcDeFgHiJkLmNOPqrst0234+πŸ’©!");
284 try std.testing.expectEqualStrings("abcdefghijklmnopqrst0234+πŸ’©!", result);284 try std.testing.expectEqualStrings("abcdefghijklmnopqrst0234+πŸ’©!", result);
...@@ -291,7 +291,7 @@ pub fn allocLowerString(allocator: std.mem.Allocator, ascii_string: []const u8)...@@ -291,7 +291,7 @@ pub fn allocLowerString(allocator: std.mem.Allocator, ascii_string: []const u8)
291 return lowerString(result, ascii_string);291 return lowerString(result, ascii_string);
292}292}
293293
294test "allocLowerString" {294test allocLowerString {
295 const result = try allocLowerString(std.testing.allocator, "aBcDeFgHiJkLmNOPqrst0234+πŸ’©!");295 const result = try allocLowerString(std.testing.allocator, "aBcDeFgHiJkLmNOPqrst0234+πŸ’©!");
296 defer std.testing.allocator.free(result);296 defer std.testing.allocator.free(result);
297 try std.testing.expectEqualStrings("abcdefghijklmnopqrst0234+πŸ’©!", result);297 try std.testing.expectEqualStrings("abcdefghijklmnopqrst0234+πŸ’©!", result);
...@@ -307,7 +307,7 @@ pub fn upperString(output: []u8, ascii_string: []const u8) []u8 {...@@ -307,7 +307,7 @@ pub fn upperString(output: []u8, ascii_string: []const u8) []u8 {
307 return output[0..ascii_string.len];307 return output[0..ascii_string.len];
308}308}
309309
310test "upperString" {310test upperString {
311 var buf: [1024]u8 = undefined;311 var buf: [1024]u8 = undefined;
312 const result = upperString(&buf, "aBcDeFgHiJkLmNOPqrst0234+πŸ’©!");312 const result = upperString(&buf, "aBcDeFgHiJkLmNOPqrst0234+πŸ’©!");
313 try std.testing.expectEqualStrings("ABCDEFGHIJKLMNOPQRST0234+πŸ’©!", result);313 try std.testing.expectEqualStrings("ABCDEFGHIJKLMNOPQRST0234+πŸ’©!", result);
...@@ -320,7 +320,7 @@ pub fn allocUpperString(allocator: std.mem.Allocator, ascii_string: []const u8)...@@ -320,7 +320,7 @@ pub fn allocUpperString(allocator: std.mem.Allocator, ascii_string: []const u8)
320 return upperString(result, ascii_string);320 return upperString(result, ascii_string);
321}321}
322322
323test "allocUpperString" {323test allocUpperString {
324 const result = try allocUpperString(std.testing.allocator, "aBcDeFgHiJkLmNOPqrst0234+πŸ’©!");324 const result = try allocUpperString(std.testing.allocator, "aBcDeFgHiJkLmNOPqrst0234+πŸ’©!");
325 defer std.testing.allocator.free(result);325 defer std.testing.allocator.free(result);
326 try std.testing.expectEqualStrings("ABCDEFGHIJKLMNOPQRST0234+πŸ’©!", result);326 try std.testing.expectEqualStrings("ABCDEFGHIJKLMNOPQRST0234+πŸ’©!", result);
...@@ -335,7 +335,7 @@ pub fn eqlIgnoreCase(a: []const u8, b: []const u8) bool {...@@ -335,7 +335,7 @@ pub fn eqlIgnoreCase(a: []const u8, b: []const u8) bool {
335 return true;335 return true;
336}336}
337337
338test "eqlIgnoreCase" {338test eqlIgnoreCase {
339 try std.testing.expect(eqlIgnoreCase("HElπŸ’©Lo!", "helπŸ’©lo!"));339 try std.testing.expect(eqlIgnoreCase("HElπŸ’©Lo!", "helπŸ’©lo!"));
340 try std.testing.expect(!eqlIgnoreCase("hElLo!", "hello! "));340 try std.testing.expect(!eqlIgnoreCase("hElLo!", "hello! "));
341 try std.testing.expect(!eqlIgnoreCase("hElLo!", "helro!"));341 try std.testing.expect(!eqlIgnoreCase("hElLo!", "helro!"));
...@@ -345,7 +345,7 @@ pub fn startsWithIgnoreCase(haystack: []const u8, needle: []const u8) bool {...@@ -345,7 +345,7 @@ pub fn startsWithIgnoreCase(haystack: []const u8, needle: []const u8) bool {
345 return if (needle.len > haystack.len) false else eqlIgnoreCase(haystack[0..needle.len], needle);345 return if (needle.len > haystack.len) false else eqlIgnoreCase(haystack[0..needle.len], needle);
346}346}
347347
348test "startsWithIgnoreCase" {348test startsWithIgnoreCase {
349 try std.testing.expect(startsWithIgnoreCase("boB", "Bo"));349 try std.testing.expect(startsWithIgnoreCase("boB", "Bo"));
350 try std.testing.expect(!startsWithIgnoreCase("Needle in hAyStAcK", "haystack"));350 try std.testing.expect(!startsWithIgnoreCase("Needle in hAyStAcK", "haystack"));
351}351}
...@@ -354,7 +354,7 @@ pub fn endsWithIgnoreCase(haystack: []const u8, needle: []const u8) bool {...@@ -354,7 +354,7 @@ pub fn endsWithIgnoreCase(haystack: []const u8, needle: []const u8) bool {
354 return if (needle.len > haystack.len) false else eqlIgnoreCase(haystack[haystack.len - needle.len ..], needle);354 return if (needle.len > haystack.len) false else eqlIgnoreCase(haystack[haystack.len - needle.len ..], needle);
355}355}
356356
357test "endsWithIgnoreCase" {357test endsWithIgnoreCase {
358 try std.testing.expect(endsWithIgnoreCase("Needle in HaYsTaCk", "haystack"));358 try std.testing.expect(endsWithIgnoreCase("Needle in HaYsTaCk", "haystack"));
359 try std.testing.expect(!endsWithIgnoreCase("BoB", "Bo"));359 try std.testing.expect(!endsWithIgnoreCase("BoB", "Bo"));
360}360}
...@@ -409,7 +409,7 @@ fn boyerMooreHorspoolPreprocessIgnoreCase(pattern: []const u8, table: *[256]usiz...@@ -409,7 +409,7 @@ fn boyerMooreHorspoolPreprocessIgnoreCase(pattern: []const u8, table: *[256]usiz
409 }409 }
410}410}
411411
412test "indexOfIgnoreCase" {412test indexOfIgnoreCase {
413 try std.testing.expect(indexOfIgnoreCase("one Two Three Four", "foUr").? == 14);413 try std.testing.expect(indexOfIgnoreCase("one Two Three Four", "foUr").? == 14);
414 try std.testing.expect(indexOfIgnoreCase("one two three FouR", "gOur") == null);414 try std.testing.expect(indexOfIgnoreCase("one two three FouR", "gOur") == null);
415 try std.testing.expect(indexOfIgnoreCase("foO", "Foo").? == 0);415 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 {...@@ -1648,7 +1648,7 @@ fn testStaticBitSet(comptime Set: type) !void {
1648 try testPureBitSet(Set);1648 try testPureBitSet(Set);
1649}1649}
16501650
1651test "IntegerBitSet" {1651test IntegerBitSet {
1652 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;1652 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
16531653
1654 try testStaticBitSet(IntegerBitSet(0));1654 try testStaticBitSet(IntegerBitSet(0));
...@@ -1661,7 +1661,7 @@ test "IntegerBitSet" {...@@ -1661,7 +1661,7 @@ test "IntegerBitSet" {
1661 try testStaticBitSet(IntegerBitSet(127));1661 try testStaticBitSet(IntegerBitSet(127));
1662}1662}
16631663
1664test "ArrayBitSet" {1664test ArrayBitSet {
1665 inline for (.{ 0, 1, 2, 31, 32, 33, 63, 64, 65, 254, 500, 3000 }) |size| {1665 inline for (.{ 0, 1, 2, 31, 32, 33, 63, 64, 65, 254, 500, 3000 }) |size| {
1666 try testStaticBitSet(ArrayBitSet(u8, size));1666 try testStaticBitSet(ArrayBitSet(u8, size));
1667 try testStaticBitSet(ArrayBitSet(u16, size));1667 try testStaticBitSet(ArrayBitSet(u16, size));
...@@ -1671,7 +1671,7 @@ test "ArrayBitSet" {...@@ -1671,7 +1671,7 @@ test "ArrayBitSet" {
1671 }1671 }
1672}1672}
16731673
1674test "DynamicBitSetUnmanaged" {1674test DynamicBitSetUnmanaged {
1675 const allocator = std.testing.allocator;1675 const allocator = std.testing.allocator;
1676 var a = try DynamicBitSetUnmanaged.initEmpty(allocator, 300);1676 var a = try DynamicBitSetUnmanaged.initEmpty(allocator, 300);
1677 try testing.expectEqual(@as(usize, 0), a.count());1677 try testing.expectEqual(@as(usize, 0), a.count());
...@@ -1724,7 +1724,7 @@ test "DynamicBitSetUnmanaged" {...@@ -1724,7 +1724,7 @@ test "DynamicBitSetUnmanaged" {
1724 }1724 }
1725}1725}
17261726
1727test "DynamicBitSet" {1727test DynamicBitSet {
1728 const allocator = std.testing.allocator;1728 const allocator = std.testing.allocator;
1729 var a = try DynamicBitSet.initEmpty(allocator, 300);1729 var a = try DynamicBitSet.initEmpty(allocator, 300);
1730 try testing.expectEqual(@as(usize, 0), a.count());1730 try testing.expectEqual(@as(usize, 0), a.count());
...@@ -1765,7 +1765,7 @@ test "DynamicBitSet" {...@@ -1765,7 +1765,7 @@ test "DynamicBitSet" {
1765 }1765 }
1766}1766}
17671767
1768test "StaticBitSet" {1768test StaticBitSet {
1769 try testing.expectEqual(IntegerBitSet(0), StaticBitSet(0));1769 try testing.expectEqual(IntegerBitSet(0), StaticBitSet(0));
1770 try testing.expectEqual(IntegerBitSet(5), StaticBitSet(5));1770 try testing.expectEqual(IntegerBitSet(5), StaticBitSet(5));
1771 try testing.expectEqual(IntegerBitSet(@bitSizeOf(usize)), StaticBitSet(@bitSizeOf(usize)));1771 try testing.expectEqual(IntegerBitSet(@bitSizeOf(usize)), StaticBitSet(@bitSizeOf(usize)));
lib/std/bounded_array.zig+1-1
...@@ -287,7 +287,7 @@ pub fn BoundedArrayAligned(...@@ -287,7 +287,7 @@ pub fn BoundedArrayAligned(
287 };287 };
288}288}
289289
290test "BoundedArray" {290test BoundedArray {
291 var a = try BoundedArray(u8, 64).init(32);291 var a = try BoundedArray(u8, 64).init(32);
292292
293 try testing.expectEqual(a.capacity(), 64);293 try testing.expectEqual(a.capacity(), 64);
lib/std/child_process.zig+3-3
...@@ -1230,7 +1230,7 @@ fn windowsCreateProcessSupportsExtension(ext: []const u16) ?CreateProcessSupport...@@ -1230,7 +1230,7 @@ fn windowsCreateProcessSupportsExtension(ext: []const u16) ?CreateProcessSupport
1230 return null;1230 return null;
1231}1231}
12321232
1233test "windowsCreateProcessSupportsExtension" {1233test windowsCreateProcessSupportsExtension {
1234 try std.testing.expectEqual(CreateProcessSupportedExtension.exe, windowsCreateProcessSupportsExtension(&[_]u16{ '.', 'e', 'X', 'e' }).?);1234 try std.testing.expectEqual(CreateProcessSupportedExtension.exe, windowsCreateProcessSupportsExtension(&[_]u16{ '.', 'e', 'X', 'e' }).?);
1235 try std.testing.expect(windowsCreateProcessSupportsExtension(&[_]u16{ '.', 'e', 'X', 'e', 'c' }) == null);1235 try std.testing.expect(windowsCreateProcessSupportsExtension(&[_]u16{ '.', 'e', 'X', 'e', 'c' }) == null);
1236}1236}
...@@ -1321,7 +1321,7 @@ pub fn argvToCommandLineWindows(...@@ -1321,7 +1321,7 @@ pub fn argvToCommandLineWindows(
1321 return try unicode.wtf8ToWtf16LeAllocZ(allocator, buf.items);1321 return try unicode.wtf8ToWtf16LeAllocZ(allocator, buf.items);
1322}1322}
13231323
1324test "argvToCommandLineWindows" {1324test argvToCommandLineWindows {
1325 const t = testArgvToCommandLineWindows;1325 const t = testArgvToCommandLineWindows;
13261326
1327 try t(&.{1327 try t(&.{
...@@ -1555,7 +1555,7 @@ pub fn createNullDelimitedEnvMap(arena: mem.Allocator, env_map: *const EnvMap) !...@@ -1555,7 +1555,7 @@ pub fn createNullDelimitedEnvMap(arena: mem.Allocator, env_map: *const EnvMap) !
1555 return envp_buf;1555 return envp_buf;
1556}1556}
15571557
1558test "createNullDelimitedEnvMap" {1558test createNullDelimitedEnvMap {
1559 const testing = std.testing;1559 const testing = std.testing;
1560 const allocator = testing.allocator;1560 const allocator = testing.allocator;
1561 var envmap = EnvMap.init(allocator);1561 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 {...@@ -138,7 +138,7 @@ pub inline fn secureZero(comptime T: type, s: []T) void {
138 @memset(@as([]volatile T, s), 0);138 @memset(@as([]volatile T, s), 0);
139}139}
140140
141test "timingSafeEql" {141test timingSafeEql {
142 var a: [100]u8 = undefined;142 var a: [100]u8 = undefined;
143 var b: [100]u8 = undefined;143 var b: [100]u8 = undefined;
144 random.bytes(a[0..]);144 random.bytes(a[0..]);
...@@ -162,7 +162,7 @@ test "timingSafeEql (vectors)" {...@@ -162,7 +162,7 @@ test "timingSafeEql (vectors)" {
162 try testing.expect(timingSafeEql(@Vector(100, u8), v1, v3));162 try testing.expect(timingSafeEql(@Vector(100, u8), v1, v3));
163}163}
164164
165test "timingSafeCompare" {165test timingSafeCompare {
166 var a = [_]u8{10} ** 32;166 var a = [_]u8{10} ** 32;
167 var b = [_]u8{10} ** 32;167 var b = [_]u8{10} ** 32;
168 try testing.expectEqual(timingSafeCompare(u8, &a, &b, .big), .eq);168 try testing.expectEqual(timingSafeCompare(u8, &a, &b, .big), .eq);
...@@ -195,7 +195,7 @@ test "timingSafe{Add,Sub}" {...@@ -195,7 +195,7 @@ test "timingSafe{Add,Sub}" {
195 }195 }
196}196}
197197
198test "secureZero" {198test secureZero {
199 var a = [_]u8{0xfe} ** 8;199 var a = [_]u8{0xfe} ** 8;
200 var b = [_]u8{0xfe} ** 8;200 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...@@ -908,7 +908,7 @@ fn machoSearchSymbols(symbols: []const MachoSymbol, address: usize) ?*const Mach
908 return null;908 return null;
909}909}
910910
911test "machoSearchSymbols" {911test machoSearchSymbols {
912 const symbols = [_]MachoSymbol{912 const symbols = [_]MachoSymbol{
913 .{ .addr = 100, .strx = undefined, .size = undefined, .ofile = undefined },913 .{ .addr = 100, .strx = undefined, .size = undefined, .ofile = undefined },
914 .{ .addr = 200, .strx = undefined, .size = undefined, .ofile = undefined },914 .{ .addr = 200, .strx = undefined, .size = undefined, .ofile = undefined },
...@@ -1502,7 +1502,7 @@ fn printLineFromFileAnyOs(out_stream: anytype, line_info: LineInfo) !void {...@@ -1502,7 +1502,7 @@ fn printLineFromFileAnyOs(out_stream: anytype, line_info: LineInfo) !void {
1502 }1502 }
1503}1503}
15041504
1505test "printLineFromFileAnyOs" {1505test printLineFromFileAnyOs {
1506 var output = std.ArrayList(u8).init(std.testing.allocator);1506 var output = std.ArrayList(u8).init(std.testing.allocator);
1507 defer output.deinit();1507 defer output.deinit();
1508 const output_stream = output.writer();1508 const output_stream = output.writer();
lib/std/fifo.zig+1-1
...@@ -507,7 +507,7 @@ test "LinearFifo(u8, .Dynamic)" {...@@ -507,7 +507,7 @@ test "LinearFifo(u8, .Dynamic)" {
507 }507 }
508}508}
509509
510test "LinearFifo" {510test LinearFifo {
511 inline for ([_]type{ u1, u8, u16, u64 }) |T| {511 inline for ([_]type{ u1, u8, u16, u64 }) |T| {
512 inline for ([_]LinearFifoBufferType{ LinearFifoBufferType{ .Static = 32 }, .Slice, .Dynamic }) |bt| {512 inline for ([_]LinearFifoBufferType{ LinearFifoBufferType{ .Static = 32 }, .Slice, .Dynamic }) |bt| {
513 const FifoType = LinearFifo(T, bt);513 const FifoType = LinearFifo(T, bt);
lib/std/fmt.zig+9-9
...@@ -1300,7 +1300,7 @@ pub fn fmtDuration(ns: u64) Formatter(formatDuration) {...@@ -1300,7 +1300,7 @@ pub fn fmtDuration(ns: u64) Formatter(formatDuration) {
1300 return .{ .data = data };1300 return .{ .data = data };
1301}1301}
13021302
1303test "fmtDuration" {1303test fmtDuration {
1304 var buf: [24]u8 = undefined;1304 var buf: [24]u8 = undefined;
1305 inline for (.{1305 inline for (.{
1306 .{ .s = "0ns", .d = 0 },1306 .{ .s = "0ns", .d = 0 },
...@@ -1364,7 +1364,7 @@ pub fn fmtDurationSigned(ns: i64) Formatter(formatDurationSigned) {...@@ -1364,7 +1364,7 @@ pub fn fmtDurationSigned(ns: i64) Formatter(formatDurationSigned) {
1364 return .{ .data = ns };1364 return .{ .data = ns };
1365}1365}
13661366
1367test "fmtDurationSigned" {1367test fmtDurationSigned {
1368 var buf: [24]u8 = undefined;1368 var buf: [24]u8 = undefined;
1369 inline for (.{1369 inline for (.{
1370 .{ .s = "0ns", .d = 0 },1370 .{ .s = "0ns", .d = 0 },
...@@ -1494,7 +1494,7 @@ pub fn parseInt(comptime T: type, buf: []const u8, base: u8) ParseIntError!T {...@@ -1494,7 +1494,7 @@ pub fn parseInt(comptime T: type, buf: []const u8, base: u8) ParseIntError!T {
1494 return parseWithSign(T, buf, base, .pos);1494 return parseWithSign(T, buf, base, .pos);
1495}1495}
14961496
1497test "parseInt" {1497test parseInt {
1498 try std.testing.expect((try parseInt(i32, "-10", 10)) == -10);1498 try std.testing.expect((try parseInt(i32, "-10", 10)) == -10);
1499 try std.testing.expect((try parseInt(i32, "+10", 10)) == 10);1499 try std.testing.expect((try parseInt(i32, "+10", 10)) == 10);
1500 try std.testing.expect((try parseInt(u32, "+10", 10)) == 10);1500 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!...@@ -1636,7 +1636,7 @@ pub fn parseUnsigned(comptime T: type, buf: []const u8, base: u8) ParseIntError!
1636 return parseWithSign(T, buf, base, .pos);1636 return parseWithSign(T, buf, base, .pos);
1637}1637}
16381638
1639test "parseUnsigned" {1639test parseUnsigned {
1640 try std.testing.expect((try parseUnsigned(u16, "050124", 10)) == 50124);1640 try std.testing.expect((try parseUnsigned(u16, "050124", 10)) == 50124);
1641 try std.testing.expect((try parseUnsigned(u16, "65535", 10)) == 65535);1641 try std.testing.expect((try parseUnsigned(u16, "65535", 10)) == 65535);
1642 try std.testing.expect((try parseUnsigned(u16, "65_535", 10)) == 65535);1642 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 {...@@ -1710,7 +1710,7 @@ pub fn parseIntSizeSuffix(buf: []const u8, digit_base: u8) ParseIntError!usize {
1710 return math.mul(usize, number, multiplier);1710 return math.mul(usize, number, multiplier);
1711}1711}
17121712
1713test "parseIntSizeSuffix" {1713test parseIntSizeSuffix {
1714 try std.testing.expect(try parseIntSizeSuffix("2", 10) == 2);1714 try std.testing.expect(try parseIntSizeSuffix("2", 10) == 2);
1715 try std.testing.expect(try parseIntSizeSuffix("2B", 10) == 2);1715 try std.testing.expect(try parseIntSizeSuffix("2B", 10) == 2);
1716 try std.testing.expect(try parseIntSizeSuffix("2kB", 10) == 2000);1716 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...@@ -1793,7 +1793,7 @@ pub fn allocPrintZ(allocator: mem.Allocator, comptime fmt: []const u8, args: any
1793 return result[0 .. result.len - 1 :0];1793 return result[0 .. result.len - 1 :0];
1794}1794}
17951795
1796test "bufPrintInt" {1796test bufPrintIntToSlice {
1797 var buffer: [100]u8 = undefined;1797 var buffer: [100]u8 = undefined;
1798 const buf = buffer[0..];1798 const buf = buffer[0..];
17991799
...@@ -1827,7 +1827,7 @@ pub inline fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [cou...@@ -1827,7 +1827,7 @@ pub inline fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [cou
1827 }1827 }
1828}1828}
18291829
1830test "comptimePrint" {1830test comptimePrint {
1831 @setEvalBranchQuota(2000);1831 @setEvalBranchQuota(2000);
1832 try std.testing.expectEqual(*const [3:0]u8, @TypeOf(comptimePrint("{}", .{100})));1832 try std.testing.expectEqual(*const [3:0]u8, @TypeOf(comptimePrint("{}", .{100})));
1833 try std.testing.expectEqualSlices(u8, "100", comptimePrint("{}", .{100}));1833 try std.testing.expectEqualSlices(u8, "100", comptimePrint("{}", .{100}));
...@@ -2442,14 +2442,14 @@ pub fn hexToBytes(out: []u8, input: []const u8) ![]u8 {...@@ -2442,14 +2442,14 @@ pub fn hexToBytes(out: []u8, input: []const u8) ![]u8 {
2442 return out[0 .. in_i / 2];2442 return out[0 .. in_i / 2];
2443}2443}
24442444
2445test "bytesToHex" {2445test bytesToHex {
2446 const input = "input slice";2446 const input = "input slice";
2447 const encoded = bytesToHex(input, .lower);2447 const encoded = bytesToHex(input, .lower);
2448 var decoded: [input.len]u8 = undefined;2448 var decoded: [input.len]u8 = undefined;
2449 try std.testing.expectEqualSlices(u8, input, try hexToBytes(&decoded, &encoded));2449 try std.testing.expectEqualSlices(u8, input, try hexToBytes(&decoded, &encoded));
2450}2450}
24512451
2452test "hexToBytes" {2452test hexToBytes {
2453 var buf: [32]u8 = undefined;2453 var buf: [32]u8 = undefined;
2454 try expectFmt("90" ** 32, "{s}", .{fmtSliceHexUpper(try hexToBytes(&buf, "90" ** 32))});2454 try expectFmt("90" ** 32, "{s}", .{fmtSliceHexUpper(try hexToBytes(&buf, "90" ** 32))});
2455 try expectFmt("ABCD", "{s}", .{fmtSliceHexUpper(try hexToBytes(&buf, "ABCD"))});2455 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...@@ -60,7 +60,7 @@ pub fn getAppDataDir(allocator: mem.Allocator, appname: []const u8) GetAppDataDi
60 }60 }
61}61}
6262
63test "getAppDataDir" {63test getAppDataDir {
64 if (builtin.os.tag == .wasi) return error.SkipZigTest;64 if (builtin.os.tag == .wasi) return error.SkipZigTest;
6565
66 // We can't actually validate the result66 // 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...@@ -180,7 +180,7 @@ fn testJoinMaybeZPosix(paths: []const []const u8, expected: []const u8, zero: bo
180 try testing.expectEqualSlices(u8, expected, if (zero) actual[0 .. actual.len - 1 :0] else actual);180 try testing.expectEqualSlices(u8, expected, if (zero) actual[0 .. actual.len - 1 :0] else actual);
181}181}
182182
183test "join" {183test join {
184 {184 {
185 const actual: []u8 = try join(testing.allocator, &[_][]const u8{});185 const actual: []u8 = try join(testing.allocator, &[_][]const u8{});
186 defer testing.allocator.free(actual);186 defer testing.allocator.free(actual);
...@@ -303,7 +303,7 @@ pub fn isAbsolutePosixZ(path_c: [*:0]const u8) bool {...@@ -303,7 +303,7 @@ pub fn isAbsolutePosixZ(path_c: [*:0]const u8) bool {
303 return isAbsolutePosix(mem.sliceTo(path_c, 0));303 return isAbsolutePosix(mem.sliceTo(path_c, 0));
304}304}
305305
306test "isAbsoluteWindows" {306test isAbsoluteWindows {
307 try testIsAbsoluteWindows("", false);307 try testIsAbsoluteWindows("", false);
308 try testIsAbsoluteWindows("/", true);308 try testIsAbsoluteWindows("/", true);
309 try testIsAbsoluteWindows("//", true);309 try testIsAbsoluteWindows("//", true);
...@@ -326,7 +326,7 @@ test "isAbsoluteWindows" {...@@ -326,7 +326,7 @@ test "isAbsoluteWindows" {
326 try testIsAbsoluteWindows("/usr/local", true);326 try testIsAbsoluteWindows("/usr/local", true);
327}327}
328328
329test "isAbsolutePosix" {329test isAbsolutePosix {
330 try testIsAbsolutePosix("", false);330 try testIsAbsolutePosix("", false);
331 try testIsAbsolutePosix("/home/foo", true);331 try testIsAbsolutePosix("/home/foo", true);
332 try testIsAbsolutePosix("/home/foo/..", true);332 try testIsAbsolutePosix("/home/foo/..", true);
...@@ -400,7 +400,7 @@ pub fn windowsParsePath(path: []const u8) WindowsPath {...@@ -400,7 +400,7 @@ pub fn windowsParsePath(path: []const u8) WindowsPath {
400 return relative_path;400 return relative_path;
401}401}
402402
403test "windowsParsePath" {403test windowsParsePath {
404 {404 {
405 const parsed = windowsParsePath("//a/b");405 const parsed = windowsParsePath("//a/b");
406 try testing.expect(parsed.is_abs);406 try testing.expect(parsed.is_abs);
...@@ -884,7 +884,7 @@ pub fn dirnamePosix(path: []const u8) ?[]const u8 {...@@ -884,7 +884,7 @@ pub fn dirnamePosix(path: []const u8) ?[]const u8 {
884 return path[0..end_index];884 return path[0..end_index];
885}885}
886886
887test "dirnamePosix" {887test dirnamePosix {
888 try testDirnamePosix("/a/b/c", "/a/b");888 try testDirnamePosix("/a/b/c", "/a/b");
889 try testDirnamePosix("/a/b/c///", "/a/b");889 try testDirnamePosix("/a/b/c///", "/a/b");
890 try testDirnamePosix("/a", "/");890 try testDirnamePosix("/a", "/");
...@@ -898,7 +898,7 @@ test "dirnamePosix" {...@@ -898,7 +898,7 @@ test "dirnamePosix" {
898 try testDirnamePosix("a//", null);898 try testDirnamePosix("a//", null);
899}899}
900900
901test "dirnameWindows" {901test dirnameWindows {
902 try testDirnameWindows("c:\\", null);902 try testDirnameWindows("c:\\", null);
903 try testDirnameWindows("c:\\foo", "c:\\");903 try testDirnameWindows("c:\\foo", "c:\\");
904 try testDirnameWindows("c:\\foo\\", "c:\\");904 try testDirnameWindows("c:\\foo\\", "c:\\");
...@@ -1011,7 +1011,7 @@ pub fn basenameWindows(path: []const u8) []const u8 {...@@ -1011,7 +1011,7 @@ pub fn basenameWindows(path: []const u8) []const u8 {
1011 return path[start_index + 1 .. end_index];1011 return path[start_index + 1 .. end_index];
1012}1012}
10131013
1014test "basename" {1014test basename {
1015 try testBasename("", "");1015 try testBasename("", "");
1016 try testBasename("/", "");1016 try testBasename("/", "");
1017 try testBasename("/dir/basename.ext", "basename.ext");1017 try testBasename("/dir/basename.ext", "basename.ext");
...@@ -1186,7 +1186,7 @@ pub fn relativePosix(allocator: Allocator, from: []const u8, to: []const u8) ![]...@@ -1186,7 +1186,7 @@ pub fn relativePosix(allocator: Allocator, from: []const u8, to: []const u8) ![]
1186 return [_]u8{};1186 return [_]u8{};
1187}1187}
11881188
1189test "relative" {1189test relative {
1190 try testRelativeWindows("c:/blah\\blah", "d:/games", "D:\\games");1190 try testRelativeWindows("c:/blah\\blah", "d:/games", "D:\\games");
1191 try testRelativeWindows("c:/aaaa/bbbb", "c:/aaaa", "..");1191 try testRelativeWindows("c:/aaaa/bbbb", "c:/aaaa", "..");
1192 try testRelativeWindows("c:/aaaa/bbbb", "c:/cccc", "..\\..\\cccc");1192 try testRelativeWindows("c:/aaaa/bbbb", "c:/cccc", "..\\..\\cccc");
...@@ -1271,7 +1271,7 @@ fn testExtension(path: []const u8, expected: []const u8) !void {...@@ -1271,7 +1271,7 @@ fn testExtension(path: []const u8, expected: []const u8) !void {
1271 try testing.expectEqualStrings(expected, extension(path));1271 try testing.expectEqualStrings(expected, extension(path));
1272}1272}
12731273
1274test "extension" {1274test extension {
1275 try testExtension("", "");1275 try testExtension("", "");
1276 try testExtension(".", "");1276 try testExtension(".", "");
1277 try testExtension("a.", ".");1277 try testExtension("a.", ".");
...@@ -1328,7 +1328,7 @@ fn testStem(path: []const u8, expected: []const u8) !void {...@@ -1328,7 +1328,7 @@ fn testStem(path: []const u8, expected: []const u8) !void {
1328 try testing.expectEqualStrings(expected, stem(path));1328 try testing.expectEqualStrings(expected, stem(path));
1329}1329}
13301330
1331test "stem" {1331test stem {
1332 try testStem("hello/world/lib.tar.gz", "lib.tar");1332 try testStem("hello/world/lib.tar.gz", "lib.tar");
1333 try testStem("hello/world/lib.tar", "lib");1333 try testStem("hello/world/lib.tar", "lib");
1334 try testStem("hello/world/lib", "lib");1334 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 {...@@ -425,7 +425,7 @@ fn dummyWrite(context: void, data: []const u8) error{}!usize {
425 return data.len;425 return data.len;
426}426}
427427
428test "null_writer" {428test null_writer {
429 null_writer.writeAll("yay" ** 10) catch |err| switch (err) {};429 null_writer.writeAll("yay" ** 10) catch |err| switch (err) {};
430}430}
431431
lib/std/math.zig+91-70
...@@ -161,28 +161,51 @@ pub fn approxEqRel(comptime T: type, x: T, y: T, tolerance: T) bool {...@@ -161,28 +161,51 @@ pub fn approxEqRel(comptime T: type, x: T, y: T, tolerance: T) bool {
161 return @abs(x - y) <= @max(@abs(x), @abs(y)) * tolerance;161 return @abs(x - y) <= @max(@abs(x), @abs(y)) * tolerance;
162}162}
163163
164test "approxEqAbs and approxEqRel" {164test approxEqAbs {
165 inline for ([_]type{ f16, f32, f64, f128 }) |T| {165 inline for ([_]type{ f16, f32, f64, f128 }) |T| {
166 const eps_value = comptime floatEps(T);166 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);
170 const min_value = comptime floatMin(T);167 const min_value = comptime floatMin(T);
171168
172 try testing.expect(approxEqAbs(T, 0.0, 0.0, eps_value));169 try testing.expect(approxEqAbs(T, 0.0, 0.0, eps_value));
173 try testing.expect(approxEqAbs(T, -0.0, -0.0, eps_value));170 try testing.expect(approxEqAbs(T, -0.0, -0.0, eps_value));
174 try testing.expect(approxEqAbs(T, 0.0, -0.0, eps_value));171 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));
177 try testing.expect(!approxEqAbs(T, 1.0 + 2 * eps_value, 1.0, eps_value));172 try testing.expect(!approxEqAbs(T, 1.0 + 2 * eps_value, 1.0, eps_value));
178 try testing.expect(approxEqAbs(T, 1.0 + 1 * eps_value, 1.0, eps_value));173 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));
179 try testing.expect(!approxEqRel(T, 1.0, nan_value, sqrt_eps_value));204 try testing.expect(!approxEqRel(T, 1.0, nan_value, sqrt_eps_value));
180 try testing.expect(!approxEqRel(T, nan_value, nan_value, sqrt_eps_value));205 try testing.expect(!approxEqRel(T, nan_value, nan_value, sqrt_eps_value));
181 try testing.expect(approxEqRel(T, inf_value, inf_value, sqrt_eps_value));206 try testing.expect(approxEqRel(T, inf_value, inf_value, sqrt_eps_value));
182 try testing.expect(approxEqRel(T, min_value, min_value, sqrt_eps_value));207 try testing.expect(approxEqRel(T, min_value, min_value, sqrt_eps_value));
183 try testing.expect(approxEqRel(T, -min_value, -min_value, sqrt_eps_value));208 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));
186 }209 }
187210
188 comptime {211 comptime {
...@@ -194,13 +217,8 @@ test "approxEqAbs and approxEqRel" {...@@ -194,13 +217,8 @@ test "approxEqAbs and approxEqRel" {
194 const eps_value = 1e-4;217 const eps_value = 1e-4;
195 const sqrt_eps_value = sqrt(eps_value);218 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));
200 try testing.expect(approxEqRel(comptime_float, 1.0, 1.0, sqrt_eps_value));220 try testing.expect(approxEqRel(comptime_float, 1.0, 1.0, sqrt_eps_value));
201 try testing.expect(!approxEqRel(comptime_float, 1.0, 0.0, sqrt_eps_value));221 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));
204 }222 }
205}223}
206224
...@@ -300,7 +318,7 @@ pub fn radiansToDegrees(comptime T: type, angle_in_radians: T) T {...@@ -300,7 +318,7 @@ pub fn radiansToDegrees(comptime T: type, angle_in_radians: T) T {
300 return angle_in_radians * 180.0 / pi;318 return angle_in_radians * 180.0 / pi;
301}319}
302320
303test "radiansToDegrees" {321test radiansToDegrees {
304 try std.testing.expectApproxEqAbs(@as(f32, 0), radiansToDegrees(f32, 0), 1e-6);322 try std.testing.expectApproxEqAbs(@as(f32, 0), radiansToDegrees(f32, 0), 1e-6);
305 try std.testing.expectApproxEqAbs(@as(f32, 90), radiansToDegrees(f32, pi / 2.0), 1e-6);323 try std.testing.expectApproxEqAbs(@as(f32, 90), radiansToDegrees(f32, pi / 2.0), 1e-6);
306 try std.testing.expectApproxEqAbs(@as(f32, -45), radiansToDegrees(f32, -pi / 4.0), 1e-6);324 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 {...@@ -315,7 +333,7 @@ pub fn degreesToRadians(comptime T: type, angle_in_degrees: T) T {
315 return angle_in_degrees * pi / 180.0;333 return angle_in_degrees * pi / 180.0;
316}334}
317335
318test "degreesToRadians" {336test degreesToRadians {
319 try std.testing.expectApproxEqAbs(@as(f32, pi / 2.0), degreesToRadians(f32, 90), 1e-6);337 try std.testing.expectApproxEqAbs(@as(f32, pi / 2.0), degreesToRadians(f32, 90), 1e-6);
320 try std.testing.expectApproxEqAbs(@as(f32, -3 * pi / 2.0), degreesToRadians(f32, -270), 1e-6);338 try std.testing.expectApproxEqAbs(@as(f32, -3 * pi / 2.0), degreesToRadians(f32, -270), 1e-6);
321 try std.testing.expectApproxEqAbs(@as(f32, 2 * pi), degreesToRadians(f32, 360), 1e-6);339 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) {...@@ -464,7 +482,7 @@ pub fn wrap(x: anytype, r: anytype) @TypeOf(x) {
464 },482 },
465 }483 }
466}484}
467test "wrap" {485test wrap {
468 // Within range486 // Within range
469 try testing.expect(wrap(@as(i32, -75), @as(i32, 180)) == -75);487 try testing.expect(wrap(@as(i32, -75), @as(i32, 180)) == -75);
470 try testing.expect(wrap(@as(i32, -75), @as(i32, -180)) == -75);488 try testing.expect(wrap(@as(i32, -75), @as(i32, -180)) == -75);
...@@ -501,8 +519,7 @@ test "wrap" {...@@ -501,8 +519,7 @@ test "wrap" {
501 var i: i32 = 1;519 var i: i32 = 1;
502 _ = &i;520 _ = &i;
503 try testing.expect(wrap(i, 10) == 1);521 try testing.expect(wrap(i, 10) == 1);
504}522
505test wrap {
506 const limit: i32 = 180;523 const limit: i32 = 180;
507 // Within range524 // Within range
508 try testing.expect(wrap(@as(i32, -75), limit) == -75);525 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...@@ -527,7 +544,7 @@ pub fn clamp(val: anytype, lower: anytype, upper: anytype) @TypeOf(val, lower, u
527 assert(lower <= upper);544 assert(lower <= upper);
528 return @max(lower, @min(val, upper));545 return @max(lower, @min(val, upper));
529}546}
530test "clamp" {547test clamp {
531 // Within range548 // Within range
532 try testing.expect(std.math.clamp(@as(i32, -1), @as(i32, -4), @as(i32, 7)) == -1);549 try testing.expect(std.math.clamp(@as(i32, -1), @as(i32, -4), @as(i32, 7)) == -1);
533 // Below550 // Below
...@@ -608,7 +625,7 @@ pub fn shl(comptime T: type, a: T, shift_amt: anytype) T {...@@ -608,7 +625,7 @@ pub fn shl(comptime T: type, a: T, shift_amt: anytype) T {
608 return a << casted_shift_amt;625 return a << casted_shift_amt;
609}626}
610627
611test "shl" {628test shl {
612 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) {629 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) {
613 // https://github.com/ziglang/zig/issues/12012630 // https://github.com/ziglang/zig/issues/12012
614 return error.SkipZigTest;631 return error.SkipZigTest;
...@@ -653,7 +670,7 @@ pub fn shr(comptime T: type, a: T, shift_amt: anytype) T {...@@ -653,7 +670,7 @@ pub fn shr(comptime T: type, a: T, shift_amt: anytype) T {
653 return a >> casted_shift_amt;670 return a >> casted_shift_amt;
654}671}
655672
656test "shr" {673test shr {
657 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) {674 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) {
658 // https://github.com/ziglang/zig/issues/12012675 // https://github.com/ziglang/zig/issues/12012
659 return error.SkipZigTest;676 return error.SkipZigTest;
...@@ -699,7 +716,7 @@ pub fn rotr(comptime T: type, x: T, r: anytype) T {...@@ -699,7 +716,7 @@ pub fn rotr(comptime T: type, x: T, r: anytype) T {
699 }716 }
700}717}
701718
702test "rotr" {719test rotr {
703 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) {720 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) {
704 // https://github.com/ziglang/zig/issues/12012721 // https://github.com/ziglang/zig/issues/12012
705 return error.SkipZigTest;722 return error.SkipZigTest;
...@@ -744,7 +761,7 @@ pub fn rotl(comptime T: type, x: T, r: anytype) T {...@@ -744,7 +761,7 @@ pub fn rotl(comptime T: type, x: T, r: anytype) T {
744 }761 }
745}762}
746763
747test "rotl" {764test rotl {
748 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) {765 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) {
749 // https://github.com/ziglang/zig/issues/12012766 // https://github.com/ziglang/zig/issues/12012
750 return error.SkipZigTest;767 return error.SkipZigTest;
...@@ -806,7 +823,7 @@ pub fn IntFittingRange(comptime from: comptime_int, comptime to: comptime_int) t...@@ -806,7 +823,7 @@ pub fn IntFittingRange(comptime from: comptime_int, comptime to: comptime_int) t
806 return std.meta.Int(signedness, magnitude_bits);823 return std.meta.Int(signedness, magnitude_bits);
807}824}
808825
809test "IntFittingRange" {826test IntFittingRange {
810 try testing.expect(IntFittingRange(0, 0) == u0);827 try testing.expect(IntFittingRange(0, 0) == u0);
811 try testing.expect(IntFittingRange(0, 1) == u1);828 try testing.expect(IntFittingRange(0, 1) == u1);
812 try testing.expect(IntFittingRange(0, 2) == u2);829 try testing.expect(IntFittingRange(0, 2) == u2);
...@@ -874,7 +891,7 @@ pub fn divTrunc(comptime T: type, numerator: T, denominator: T) !T {...@@ -874,7 +891,7 @@ pub fn divTrunc(comptime T: type, numerator: T, denominator: T) !T {
874 return @divTrunc(numerator, denominator);891 return @divTrunc(numerator, denominator);
875}892}
876893
877test "divTrunc" {894test divTrunc {
878 try testDivTrunc();895 try testDivTrunc();
879 try comptime testDivTrunc();896 try comptime testDivTrunc();
880}897}
...@@ -898,7 +915,7 @@ pub fn divFloor(comptime T: type, numerator: T, denominator: T) !T {...@@ -898,7 +915,7 @@ pub fn divFloor(comptime T: type, numerator: T, denominator: T) !T {
898 return @divFloor(numerator, denominator);915 return @divFloor(numerator, denominator);
899}916}
900917
901test "divFloor" {918test divFloor {
902 try testDivFloor();919 try testDivFloor();
903 try comptime testDivFloor();920 try comptime testDivFloor();
904}921}
...@@ -935,7 +952,7 @@ pub fn divCeil(comptime T: type, numerator: T, denominator: T) !T {...@@ -935,7 +952,7 @@ pub fn divCeil(comptime T: type, numerator: T, denominator: T) !T {
935 }952 }
936}953}
937954
938test "divCeil" {955test divCeil {
939 try testDivCeil();956 try testDivCeil();
940 try comptime testDivCeil();957 try comptime testDivCeil();
941}958}
...@@ -979,7 +996,7 @@ pub fn divExact(comptime T: type, numerator: T, denominator: T) !T {...@@ -979,7 +996,7 @@ pub fn divExact(comptime T: type, numerator: T, denominator: T) !T {
979 return result;996 return result;
980}997}
981998
982test "divExact" {999test divExact {
983 try testDivExact();1000 try testDivExact();
984 try comptime testDivExact();1001 try comptime testDivExact();
985}1002}
...@@ -1005,7 +1022,7 @@ pub fn mod(comptime T: type, numerator: T, denominator: T) !T {...@@ -1005,7 +1022,7 @@ pub fn mod(comptime T: type, numerator: T, denominator: T) !T {
1005 return @mod(numerator, denominator);1022 return @mod(numerator, denominator);
1006}1023}
10071024
1008test "mod" {1025test mod {
1009 try testMod();1026 try testMod();
1010 try comptime testMod();1027 try comptime testMod();
1011}1028}
...@@ -1031,7 +1048,7 @@ pub fn rem(comptime T: type, numerator: T, denominator: T) !T {...@@ -1031,7 +1048,7 @@ pub fn rem(comptime T: type, numerator: T, denominator: T) !T {
1031 return @rem(numerator, denominator);1048 return @rem(numerator, denominator);
1032}1049}
10331050
1034test "rem" {1051test rem {
1035 try testRem();1052 try testRem();
1036 try comptime testRem();1053 try comptime testRem();
1037}1054}
...@@ -1060,7 +1077,7 @@ pub fn negateCast(x: anytype) !std.meta.Int(.signed, @bitSizeOf(@TypeOf(x))) {...@@ -1060,7 +1077,7 @@ pub fn negateCast(x: anytype) !std.meta.Int(.signed, @bitSizeOf(@TypeOf(x))) {
1060 return -@as(int, @intCast(x));1077 return -@as(int, @intCast(x));
1061}1078}
10621079
1063test "negateCast" {1080test negateCast {
1064 try testing.expect((negateCast(@as(u32, 999)) catch unreachable) == -999);1081 try testing.expect((negateCast(@as(u32, 999)) catch unreachable) == -999);
1065 try testing.expect(@TypeOf(negateCast(@as(u32, 999)) catch unreachable) == i32);1082 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 {...@@ -1085,7 +1102,7 @@ pub fn cast(comptime T: type, x: anytype) ?T {
1085 }1102 }
1086}1103}
10871104
1088test "cast" {1105test cast {
1089 try testing.expect(cast(u8, 300) == null);1106 try testing.expect(cast(u8, 300) == null);
1090 try testing.expect(cast(u8, @as(u32, 300)) == null);1107 try testing.expect(cast(u8, @as(u32, 300)) == null);
1091 try testing.expect(cast(i8, -200) == null);1108 try testing.expect(cast(i8, -200) == null);
...@@ -1144,7 +1161,7 @@ pub fn ByteAlignedInt(comptime T: type) type {...@@ -1144,7 +1161,7 @@ pub fn ByteAlignedInt(comptime T: type) type {
1144 return extended_type;1161 return extended_type;
1145}1162}
11461163
1147test "ByteAlignedInt" {1164test ByteAlignedInt {
1148 try testing.expect(ByteAlignedInt(u0) == u0);1165 try testing.expect(ByteAlignedInt(u0) == u0);
1149 try testing.expect(ByteAlignedInt(i0) == i0);1166 try testing.expect(ByteAlignedInt(i0) == i0);
1150 try testing.expect(ByteAlignedInt(u3) == u8);1167 try testing.expect(ByteAlignedInt(u3) == u8);
...@@ -1182,7 +1199,7 @@ pub fn floorPowerOfTwo(comptime T: type, value: T) T {...@@ -1182,7 +1199,7 @@ pub fn floorPowerOfTwo(comptime T: type, value: T) T {
1182 return @as(T, 1) << log2_int(uT, @as(uT, @intCast(value)));1199 return @as(T, 1) << log2_int(uT, @as(uT, @intCast(value)));
1183}1200}
11841201
1185test "floorPowerOfTwo" {1202test floorPowerOfTwo {
1186 try testFloorPowerOfTwo();1203 try testFloorPowerOfTwo();
1187 try comptime testFloorPowerOfTwo();1204 try comptime testFloorPowerOfTwo();
1188}1205}
...@@ -1244,7 +1261,7 @@ pub fn ceilPowerOfTwoAssert(comptime T: type, value: T) T {...@@ -1244,7 +1261,7 @@ pub fn ceilPowerOfTwoAssert(comptime T: type, value: T) T {
1244 return ceilPowerOfTwo(T, value) catch unreachable;1261 return ceilPowerOfTwo(T, value) catch unreachable;
1245}1262}
12461263
1247test "ceilPowerOfTwoPromote" {1264test ceilPowerOfTwoPromote {
1248 try testCeilPowerOfTwoPromote();1265 try testCeilPowerOfTwoPromote();
1249 try comptime testCeilPowerOfTwoPromote();1266 try comptime testCeilPowerOfTwoPromote();
1250}1267}
...@@ -1261,7 +1278,7 @@ fn testCeilPowerOfTwoPromote() !void {...@@ -1261,7 +1278,7 @@ fn testCeilPowerOfTwoPromote() !void {
1261 try testing.expectEqual(@as(u5, 16), ceilPowerOfTwoPromote(u4, 9));1278 try testing.expectEqual(@as(u5, 16), ceilPowerOfTwoPromote(u4, 9));
1262}1279}
12631280
1264test "ceilPowerOfTwo" {1281test ceilPowerOfTwo {
1265 try testCeilPowerOfTwo();1282 try testCeilPowerOfTwo();
1266 try comptime testCeilPowerOfTwo();1283 try comptime testCeilPowerOfTwo();
1267}1284}
...@@ -1354,7 +1371,7 @@ pub fn lossyCast(comptime T: type, value: anytype) T {...@@ -1354,7 +1371,7 @@ pub fn lossyCast(comptime T: type, value: anytype) T {
1354 }1371 }
1355}1372}
13561373
1357test "lossyCast" {1374test lossyCast {
1358 try testing.expect(lossyCast(i16, 70000.0) == @as(i16, 32767));1375 try testing.expect(lossyCast(i16, 70000.0) == @as(i16, 32767));
1359 try testing.expect(lossyCast(u32, @as(i16, -255)) == @as(u32, 0));1376 try testing.expect(lossyCast(u32, @as(i16, -255)) == @as(u32, 0));
1360 try testing.expect(lossyCast(i9, @as(u32, 200)) == @as(i9, 200));1377 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) {...@@ -1383,7 +1400,7 @@ pub fn lerp(a: anytype, b: anytype, t: anytype) @TypeOf(a, b, t) {
1383 return @mulAdd(Type, b - a, t, a);1400 return @mulAdd(Type, b - a, t, a);
1384}1401}
13851402
1386test "lerp" {1403test lerp {
1387 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // https://github.com/ziglang/zig/issues/178841404 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // https://github.com/ziglang/zig/issues/17884
1388 if (builtin.zig_backend == .stage2_x86_64 and1405 if (builtin.zig_backend == .stage2_x86_64 and
1389 !comptime std.Target.x86.featureSetHas(builtin.cpu.features, .fma)) return error.SkipZigTest;1406 !comptime std.Target.x86.featureSetHas(builtin.cpu.features, .fma)) return error.SkipZigTest;
...@@ -1437,7 +1454,7 @@ pub fn minInt(comptime T: type) comptime_int {...@@ -1437,7 +1454,7 @@ pub fn minInt(comptime T: type) comptime_int {
1437 return -(1 << (bit_count - 1));1454 return -(1 << (bit_count - 1));
1438}1455}
14391456
1440test "minInt and maxInt" {1457test maxInt {
1441 try testing.expect(maxInt(u0) == 0);1458 try testing.expect(maxInt(u0) == 0);
1442 try testing.expect(maxInt(u1) == 1);1459 try testing.expect(maxInt(u1) == 1);
1443 try testing.expect(maxInt(u8) == 255);1460 try testing.expect(maxInt(u8) == 255);
...@@ -1454,7 +1471,9 @@ test "minInt and maxInt" {...@@ -1454,7 +1471,9 @@ test "minInt and maxInt" {
1454 try testing.expect(maxInt(i63) == 4611686018427387903);1471 try testing.expect(maxInt(i63) == 4611686018427387903);
1455 try testing.expect(maxInt(i64) == 9223372036854775807);1472 try testing.expect(maxInt(i64) == 9223372036854775807);
1456 try testing.expect(maxInt(i128) == 170141183460469231731687303715884105727);1473 try testing.expect(maxInt(i128) == 170141183460469231731687303715884105727);
1474}
14571475
1476test minInt {
1458 try testing.expect(minInt(u0) == 0);1477 try testing.expect(minInt(u0) == 0);
1459 try testing.expect(minInt(u1) == 0);1478 try testing.expect(minInt(u1) == 0);
1460 try testing.expect(minInt(u8) == 0);1479 try testing.expect(minInt(u8) == 0);
...@@ -1492,7 +1511,7 @@ pub fn mulWide(comptime T: type, a: T, b: T) std.meta.Int(...@@ -1492,7 +1511,7 @@ pub fn mulWide(comptime T: type, a: T, b: T) std.meta.Int(
1492 return @as(ResultInt, a) * @as(ResultInt, b);1511 return @as(ResultInt, a) * @as(ResultInt, b);
1493}1512}
14941513
1495test "mulWide" {1514test mulWide {
1496 try testing.expect(mulWide(u8, 5, 5) == 25);1515 try testing.expect(mulWide(u8, 5, 5) == 25);
1497 try testing.expect(mulWide(i8, 5, -5) == -25);1516 try testing.expect(mulWide(i8, 5, -5) == -25);
1498 try testing.expect(mulWide(u8, 100, 100) == 10000);1517 try testing.expect(mulWide(u8, 100, 100) == 10000);
...@@ -1517,6 +1536,12 @@ pub const Order = enum {...@@ -1517,6 +1536,12 @@ pub const Order = enum {
1517 };1536 };
1518 }1537 }
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
1520 pub fn compare(self: Order, op: CompareOperator) bool {1545 pub fn compare(self: Order, op: CompareOperator) bool {
1521 return switch (self) {1546 return switch (self) {
1522 .lt => switch (op) {1547 .lt => switch (op) {
...@@ -1545,6 +1570,19 @@ pub const Order = enum {...@@ -1545,6 +1570,19 @@ pub const Order = enum {
1545 },1570 },
1546 };1571 };
1547 }1572 }
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 }
1548};1586};
15491587
1550/// Given two numbers, this function returns the order they are with respect to each other.1588/// Given two numbers, this function returns the order they are with respect to each other.
...@@ -1587,6 +1625,15 @@ pub const CompareOperator = enum {...@@ -1587,6 +1625,15 @@ pub const CompareOperator = enum {
1587 .neq => .neq,1625 .neq => .neq,
1588 };1626 };
1589 }1627 }
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 }
1590};1637};
15911638
1592/// This function does the same thing as comparison operators, however the1639/// 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 {...@@ -1603,7 +1650,7 @@ pub fn compare(a: anytype, op: CompareOperator, b: anytype) bool {
1603 };1650 };
1604}1651}
16051652
1606test "compare between signed and unsigned" {1653test compare {
1607 try testing.expect(compare(@as(i8, -1), .lt, @as(u8, 255)));1654 try testing.expect(compare(@as(i8, -1), .lt, @as(u8, 255)));
1608 try testing.expect(compare(@as(i8, 2), .gt, @as(u8, 1)));1655 try testing.expect(compare(@as(i8, 2), .gt, @as(u8, 1)));
1609 try testing.expect(!compare(@as(i8, -1), .gte, @as(u8, 255)));1656 try testing.expect(!compare(@as(i8, -1), .gte, @as(u8, 255)));
...@@ -1623,38 +1670,12 @@ test "compare between signed and unsigned" {...@@ -1623,38 +1670,12 @@ test "compare between signed and unsigned" {
1623 try testing.expect(compare(@as(u8, 1), .eq, @as(u8, 1)));1670 try testing.expect(compare(@as(u8, 1), .eq, @as(u8, 1)));
1624}1671}
16251672
1626test "order" {1673test order {
1627 try testing.expect(order(0, 0) == .eq);1674 try testing.expect(order(0, 0) == .eq);
1628 try testing.expect(order(1, 0) == .gt);1675 try testing.expect(order(1, 0) == .gt);
1629 try testing.expect(order(-1, 0) == .lt);1676 try testing.expect(order(-1, 0) == .lt);
1630}1677}
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
1658/// Returns a mask of all ones if value is true,1679/// Returns a mask of all ones if value is true,
1659/// and a mask of all zeroes if value is false.1680/// and a mask of all zeroes if value is false.
1660/// Compiles to one instruction for register sized integers.1681/// Compiles to one instruction for register sized integers.
...@@ -1676,7 +1697,7 @@ pub inline fn boolMask(comptime MaskInt: type, value: bool) MaskInt {...@@ -1676,7 +1697,7 @@ pub inline fn boolMask(comptime MaskInt: type, value: bool) MaskInt {
1676 return -%@as(MaskInt, @intCast(@intFromBool(value)));1697 return -%@as(MaskInt, @intCast(@intFromBool(value)));
1677}1698}
16781699
1679test "boolMask" {1700test boolMask {
1680 const runTest = struct {1701 const runTest = struct {
1681 fn runTest() !void {1702 fn runTest() !void {
1682 try testing.expectEqual(@as(u1, 0), boolMask(u1, false));1703 try testing.expectEqual(@as(u1, 0), boolMask(u1, false));
...@@ -1824,7 +1845,7 @@ fn testSign() !void {...@@ -1824,7 +1845,7 @@ fn testSign() !void {
1824 try std.testing.expectEqual(0.0, sign(0.0));1845 try std.testing.expectEqual(0.0, sign(0.0));
1825}1846}
18261847
1827test "sign" {1848test sign {
1828 if (builtin.zig_backend == .stage2_llvm) {1849 if (builtin.zig_backend == .stage2_llvm) {
1829 // https://github.com/ziglang/zig/issues/120121850 // https://github.com/ziglang/zig/issues/12012
1830 return error.SkipZigTest;1851 return error.SkipZigTest;
lib/std/mem.zig+66-64
...@@ -306,7 +306,7 @@ pub fn zeroes(comptime T: type) T {...@@ -306,7 +306,7 @@ pub fn zeroes(comptime T: type) T {
306 }306 }
307}307}
308308
309test "zeroes" {309test zeroes {
310 const C_struct = extern struct {310 const C_struct = extern struct {
311 x: u32,311 x: u32,
312 y: u32 align(128),312 y: u32 align(128),
...@@ -475,7 +475,7 @@ pub fn zeroInit(comptime T: type, init: anytype) T {...@@ -475,7 +475,7 @@ pub fn zeroInit(comptime T: type, init: anytype) T {
475 }475 }
476}476}
477477
478test "zeroInit" {478test zeroInit {
479 const I = struct {479 const I = struct {
480 d: f64,480 d: f64,
481 };481 };
...@@ -606,16 +606,19 @@ pub fn orderZ(comptime T: type, lhs: [*:0]const T, rhs: [*:0]const T) math.Order...@@ -606,16 +606,19 @@ pub fn orderZ(comptime T: type, lhs: [*:0]const T, rhs: [*:0]const T) math.Order
606 return math.order(lhs[i], rhs[i]);606 return math.order(lhs[i], rhs[i]);
607}607}
608608
609test "order and orderZ" {609test order {
610 try testing.expect(order(u8, "abcd", "bee") == .lt);610 try testing.expect(order(u8, "abcd", "bee") == .lt);
611 try testing.expect(orderZ(u8, "abcd", "bee") == .lt);
612 try testing.expect(order(u8, "abc", "abc") == .eq);611 try testing.expect(order(u8, "abc", "abc") == .eq);
613 try testing.expect(orderZ(u8, "abc", "abc") == .eq);
614 try testing.expect(order(u8, "abc", "abc0") == .lt);612 try testing.expect(order(u8, "abc", "abc0") == .lt);
615 try testing.expect(orderZ(u8, "abc", "abc0") == .lt);
616 try testing.expect(order(u8, "", "") == .eq);613 try testing.expect(order(u8, "", "") == .eq);
617 try testing.expect(orderZ(u8, "", "") == .eq);
618 try testing.expect(order(u8, "", "a") == .lt);614 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);
619 try testing.expect(orderZ(u8, "", "a") == .lt);622 try testing.expect(orderZ(u8, "", "a") == .lt);
620}623}
621624
...@@ -624,7 +627,7 @@ pub fn lessThan(comptime T: type, lhs: []const T, rhs: []const T) bool {...@@ -624,7 +627,7 @@ pub fn lessThan(comptime T: type, lhs: []const T, rhs: []const T) bool {
624 return order(T, lhs, rhs) == .lt;627 return order(T, lhs, rhs) == .lt;
625}628}
626629
627test "lessThan" {630test lessThan {
628 try testing.expect(lessThan(u8, "abcd", "bee"));631 try testing.expect(lessThan(u8, "abcd", "bee"));
629 try testing.expect(!lessThan(u8, "abc", "abc"));632 try testing.expect(!lessThan(u8, "abc", "abc"));
630 try testing.expect(lessThan(u8, "abc", "abc0"));633 try testing.expect(lessThan(u8, "abc", "abc0"));
...@@ -726,7 +729,7 @@ pub fn indexOfDiff(comptime T: type, a: []const T, b: []const T) ?usize {...@@ -726,7 +729,7 @@ pub fn indexOfDiff(comptime T: type, a: []const T, b: []const T) ?usize {
726 return if (a.len == b.len) null else shortest;729 return if (a.len == b.len) null else shortest;
727}730}
728731
729test "indexOfDiff" {732test indexOfDiff {
730 try testing.expectEqual(indexOfDiff(u8, "one", "one"), null);733 try testing.expectEqual(indexOfDiff(u8, "one", "one"), null);
731 try testing.expectEqual(indexOfDiff(u8, "one two", "one"), 3);734 try testing.expectEqual(indexOfDiff(u8, "one two", "one"), 3);
732 try testing.expectEqual(indexOfDiff(u8, "one", "one two"), 3);735 try testing.expectEqual(indexOfDiff(u8, "one", "one two"), 3);
...@@ -759,7 +762,7 @@ fn Span(comptime T: type) type {...@@ -759,7 +762,7 @@ fn Span(comptime T: type) type {
759 @compileError("invalid type given to std.mem.span: " ++ @typeName(T));762 @compileError("invalid type given to std.mem.span: " ++ @typeName(T));
760}763}
761764
762test "Span" {765test Span {
763 try testing.expect(Span([*:1]u16) == [:1]u16);766 try testing.expect(Span([*:1]u16) == [:1]u16);
764 try testing.expect(Span(?[*:1]u16) == ?[:1]u16);767 try testing.expect(Span(?[*:1]u16) == ?[:1]u16);
765 try testing.expect(Span([*:1]const u8) == [:1]const u8);768 try testing.expect(Span([*:1]const u8) == [:1]const u8);
...@@ -793,7 +796,7 @@ pub fn span(ptr: anytype) Span(@TypeOf(ptr)) {...@@ -793,7 +796,7 @@ pub fn span(ptr: anytype) Span(@TypeOf(ptr)) {
793 }796 }
794}797}
795798
796test "span" {799test span {
797 var array: [5]u16 = [_]u16{ 1, 2, 3, 4, 5 };800 var array: [5]u16 = [_]u16{ 1, 2, 3, 4, 5 };
798 const ptr = @as([*:3]u16, array[0..2 :3]);801 const ptr = @as([*:3]u16, array[0..2 :3]);
799 try testing.expect(eql(u16, span(ptr), &[_]u16{ 1, 2 }));802 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(...@@ -877,7 +880,7 @@ pub fn sliceTo(ptr: anytype, comptime end: std.meta.Elem(@TypeOf(ptr))) SliceTo(
877 }880 }
878}881}
879882
880test "sliceTo" {883test sliceTo {
881 try testing.expectEqualSlices(u8, "aoeu", sliceTo("aoeu", 0));884 try testing.expectEqualSlices(u8, "aoeu", sliceTo("aoeu", 0));
882885
883 {886 {
...@@ -963,7 +966,7 @@ fn lenSliceTo(ptr: anytype, comptime end: std.meta.Elem(@TypeOf(ptr))) usize {...@@ -963,7 +966,7 @@ fn lenSliceTo(ptr: anytype, comptime end: std.meta.Elem(@TypeOf(ptr))) usize {
963 @compileError("invalid type given to std.mem.sliceTo: " ++ @typeName(@TypeOf(ptr)));966 @compileError("invalid type given to std.mem.sliceTo: " ++ @typeName(@TypeOf(ptr)));
964}967}
965968
966test "lenSliceTo" {969test lenSliceTo {
967 try testing.expect(lenSliceTo("aoeu", 0) == 4);970 try testing.expect(lenSliceTo("aoeu", 0) == 4);
968971
969 {972 {
...@@ -1018,7 +1021,7 @@ pub fn len(value: anytype) usize {...@@ -1018,7 +1021,7 @@ pub fn len(value: anytype) usize {
1018 }1021 }
1019}1022}
10201023
1021test "len" {1024test len {
1022 var array: [5]u16 = [_]u16{ 1, 2, 0, 4, 5 };1025 var array: [5]u16 = [_]u16{ 1, 2, 0, 4, 5 };
1023 const ptr = @as([*:4]u16, array[0..3 :4]);1026 const ptr = @as([*:4]u16, array[0..3 :4]);
1024 try testing.expect(len(ptr) == 3);1027 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...@@ -1157,7 +1160,7 @@ pub fn trim(comptime T: type, slice: []const T, values_to_strip: []const T) []co
1157 return slice[begin..end];1160 return slice[begin..end];
1158}1161}
11591162
1160test "trim" {1163test trim {
1161 try testing.expectEqualSlices(u8, "foo\n ", trimLeft(u8, " foo\n ", " \n"));1164 try testing.expectEqualSlices(u8, "foo\n ", trimLeft(u8, " foo\n ", " \n"));
1162 try testing.expectEqualSlices(u8, " foo", trimRight(u8, " foo\n ", " \n"));1165 try testing.expectEqualSlices(u8, " foo", trimRight(u8, " foo\n ", " \n"));
1163 try testing.expectEqualSlices(u8, "foo", trim(u8, " foo\n ", " \n"));1166 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,...@@ -1240,7 +1243,7 @@ pub fn indexOfScalarPos(comptime T: type, slice: []const T, start_index: usize,
1240 return null;1243 return null;
1241}1244}
12421245
1243test "indexOfScalarPos" {1246test indexOfScalarPos {
1244 const Types = [_]type{ u8, u16, u32, u64 };1247 const Types = [_]type{ u8, u16, u32, u64 };
12451248
1246 inline for (Types) |T| {1249 inline for (Types) |T| {
...@@ -1316,7 +1319,7 @@ pub fn indexOfNonePos(comptime T: type, slice: []const T, start_index: usize, va...@@ -1316,7 +1319,7 @@ pub fn indexOfNonePos(comptime T: type, slice: []const T, start_index: usize, va
1316 return null;1319 return null;
1317}1320}
13181321
1319test "indexOfNone" {1322test indexOfNone {
1320 try testing.expect(indexOfNone(u8, "abc123", "123").? == 0);1323 try testing.expect(indexOfNone(u8, "abc123", "123").? == 0);
1321 try testing.expect(lastIndexOfNone(u8, "abc123", "123").? == 2);1324 try testing.expect(lastIndexOfNone(u8, "abc123", "123").? == 2);
1322 try testing.expect(indexOfNone(u8, "123abc", "123").? == 3);1325 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...@@ -1460,7 +1463,7 @@ pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, nee
1460 return null;1463 return null;
1461}1464}
14621465
1463test "indexOf" {1466test indexOf {
1464 try testing.expect(indexOf(u8, "one two three four five six seven eight nine ten eleven", "three four").? == 8);1467 try testing.expect(indexOf(u8, "one two three four five six seven eight nine ten eleven", "three four").? == 8);
1465 try testing.expect(lastIndexOf(u8, "one two three four five six seven eight nine ten eleven", "three four").? == 8);1468 try testing.expect(lastIndexOf(u8, "one two three four five six seven eight nine ten eleven", "three four").? == 8);
1466 try testing.expect(indexOf(u8, "one two three four five six seven eight nine ten eleven", "two two") == null);1469 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 {...@@ -1533,7 +1536,7 @@ pub fn count(comptime T: type, haystack: []const T, needle: []const T) usize {
1533 return found;1536 return found;
1534}1537}
15351538
1536test "count" {1539test count {
1537 try testing.expect(count(u8, "", "h") == 0);1540 try testing.expect(count(u8, "", "h") == 0);
1538 try testing.expect(count(u8, "h", "h") == 1);1541 try testing.expect(count(u8, "h", "h") == 1);
1539 try testing.expect(count(u8, "hh", "h") == 2);1542 try testing.expect(count(u8, "hh", "h") == 2);
...@@ -1565,7 +1568,7 @@ pub fn containsAtLeast(comptime T: type, haystack: []const T, expected_count: us...@@ -1565,7 +1568,7 @@ pub fn containsAtLeast(comptime T: type, haystack: []const T, expected_count: us
1565 return false;1568 return false;
1566}1569}
15671570
1568test "containsAtLeast" {1571test containsAtLeast {
1569 try testing.expect(containsAtLeast(u8, "aa", 0, "a"));1572 try testing.expect(containsAtLeast(u8, "aa", 0, "a"));
1570 try testing.expect(containsAtLeast(u8, "aa", 1, "a"));1573 try testing.expect(containsAtLeast(u8, "aa", 1, "a"));
1571 try testing.expect(containsAtLeast(u8, "aa", 2, "a"));1574 try testing.expect(containsAtLeast(u8, "aa", 2, "a"));
...@@ -1698,6 +1701,9 @@ test readInt {...@@ -1698,6 +1701,9 @@ test readInt {
16981701
1699 try testing.expect(readInt(i16, &[_]u8{ 0xff, 0xfd }, .big) == -3);1702 try testing.expect(readInt(i16, &[_]u8{ 0xff, 0xfd }, .big) == -3);
1700 try testing.expect(readInt(i16, &[_]u8{ 0xfc, 0xff }, .little) == -4);1703 try testing.expect(readInt(i16, &[_]u8{ 0xfc, 0xff }, .little) == -4);
1704
1705 try moreReadIntTests();
1706 try comptime moreReadIntTests();
1701}1707}
17021708
1703fn readPackedIntLittle(comptime T: type, bytes: []const u8, bit_offset: usize) T {1709fn readPackedIntLittle(comptime T: type, bytes: []const u8, bit_offset: usize) T {
...@@ -2027,7 +2033,7 @@ pub fn byteSwapAllFields(comptime S: type, ptr: *S) void {...@@ -2027,7 +2033,7 @@ pub fn byteSwapAllFields(comptime S: type, ptr: *S) void {
2027 }2033 }
2028}2034}
20292035
2030test "byteSwapAllFields" {2036test byteSwapAllFields {
2031 const T = extern struct {2037 const T = extern struct {
2032 f0: u8,2038 f0: u8,
2033 f1: u16,2039 f1: u16,
...@@ -2136,7 +2142,7 @@ pub fn tokenizeScalar(comptime T: type, buffer: []const T, delimiter: T) TokenIt...@@ -2136,7 +2142,7 @@ pub fn tokenizeScalar(comptime T: type, buffer: []const T, delimiter: T) TokenIt
2136 };2142 };
2137}2143}
21382144
2139test "tokenizeScalar" {2145test tokenizeScalar {
2140 var it = tokenizeScalar(u8, " abc def ghi ", ' ');2146 var it = tokenizeScalar(u8, " abc def ghi ", ' ');
2141 try testing.expect(eql(u8, it.next().?, "abc"));2147 try testing.expect(eql(u8, it.next().?, "abc"));
2142 try testing.expect(eql(u8, it.peek().?, "def"));2148 try testing.expect(eql(u8, it.peek().?, "def"));
...@@ -2177,7 +2183,7 @@ test "tokenizeScalar" {...@@ -2177,7 +2183,7 @@ test "tokenizeScalar" {
2177 try testing.expect(it16.next() == null);2183 try testing.expect(it16.next() == null);
2178}2184}
21792185
2180test "tokenizeAny" {2186test tokenizeAny {
2181 var it = tokenizeAny(u8, "a|b,c/d e", " /,|");2187 var it = tokenizeAny(u8, "a|b,c/d e", " /,|");
2182 try testing.expect(eql(u8, it.next().?, "a"));2188 try testing.expect(eql(u8, it.next().?, "a"));
2183 try testing.expect(eql(u8, it.peek().?, "b"));2189 try testing.expect(eql(u8, it.peek().?, "b"));
...@@ -2205,7 +2211,7 @@ test "tokenizeAny" {...@@ -2205,7 +2211,7 @@ test "tokenizeAny" {
2205 try testing.expect(it16.next() == null);2211 try testing.expect(it16.next() == null);
2206}2212}
22072213
2208test "tokenizeSequence" {2214test tokenizeSequence {
2209 var it = tokenizeSequence(u8, "a<>b<><>c><>d><", "<>");2215 var it = tokenizeSequence(u8, "a<>b<><>c><>d><", "<>");
2210 try testing.expectEqualStrings("a", it.next().?);2216 try testing.expectEqualStrings("a", it.next().?);
2211 try testing.expectEqualStrings("b", it.peek().?);2217 try testing.expectEqualStrings("b", it.peek().?);
...@@ -2334,7 +2340,7 @@ pub fn splitScalar(comptime T: type, buffer: []const T, delimiter: T) SplitItera...@@ -2334,7 +2340,7 @@ pub fn splitScalar(comptime T: type, buffer: []const T, delimiter: T) SplitItera
2334 };2340 };
2335}2341}
23362342
2337test "splitScalar" {2343test splitScalar {
2338 var it = splitScalar(u8, "abc|def||ghi", '|');2344 var it = splitScalar(u8, "abc|def||ghi", '|');
2339 try testing.expectEqualSlices(u8, it.rest(), "abc|def||ghi");2345 try testing.expectEqualSlices(u8, it.rest(), "abc|def||ghi");
2340 try testing.expectEqualSlices(u8, it.first(), "abc");2346 try testing.expectEqualSlices(u8, it.first(), "abc");
...@@ -2377,7 +2383,7 @@ test "splitScalar" {...@@ -2377,7 +2383,7 @@ test "splitScalar" {
2377 try testing.expect(it16.next() == null);2383 try testing.expect(it16.next() == null);
2378}2384}
23792385
2380test "splitSequence" {2386test splitSequence {
2381 var it = splitSequence(u8, "a, b ,, c, d, e", ", ");2387 var it = splitSequence(u8, "a, b ,, c, d, e", ", ");
2382 try testing.expectEqualSlices(u8, it.first(), "a");2388 try testing.expectEqualSlices(u8, it.first(), "a");
2383 try testing.expectEqualSlices(u8, it.rest(), "b ,, c, d, e");2389 try testing.expectEqualSlices(u8, it.rest(), "b ,, c, d, e");
...@@ -2400,7 +2406,7 @@ test "splitSequence" {...@@ -2400,7 +2406,7 @@ test "splitSequence" {
2400 try testing.expect(it16.next() == null);2406 try testing.expect(it16.next() == null);
2401}2407}
24022408
2403test "splitAny" {2409test splitAny {
2404 var it = splitAny(u8, "a,b, c d e", ", ");2410 var it = splitAny(u8, "a,b, c d e", ", ");
2405 try testing.expectEqualSlices(u8, it.first(), "a");2411 try testing.expectEqualSlices(u8, it.first(), "a");
2406 try testing.expectEqualSlices(u8, it.rest(), "b, c d e");2412 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...@@ -2536,7 +2542,7 @@ pub fn splitBackwardsScalar(comptime T: type, buffer: []const T, delimiter: T) S
2536 };2542 };
2537}2543}
25382544
2539test "splitBackwardsScalar" {2545test splitBackwardsScalar {
2540 var it = splitBackwardsScalar(u8, "abc|def||ghi", '|');2546 var it = splitBackwardsScalar(u8, "abc|def||ghi", '|');
2541 try testing.expectEqualSlices(u8, it.rest(), "abc|def||ghi");2547 try testing.expectEqualSlices(u8, it.rest(), "abc|def||ghi");
2542 try testing.expectEqualSlices(u8, it.first(), "ghi");2548 try testing.expectEqualSlices(u8, it.first(), "ghi");
...@@ -2575,7 +2581,7 @@ test "splitBackwardsScalar" {...@@ -2575,7 +2581,7 @@ test "splitBackwardsScalar" {
2575 try testing.expect(it16.next() == null);2581 try testing.expect(it16.next() == null);
2576}2582}
25772583
2578test "splitBackwardsSequence" {2584test splitBackwardsSequence {
2579 var it = splitBackwardsSequence(u8, "a, b ,, c, d, e", ", ");2585 var it = splitBackwardsSequence(u8, "a, b ,, c, d, e", ", ");
2580 try testing.expectEqualSlices(u8, it.rest(), "a, b ,, c, d, e");2586 try testing.expectEqualSlices(u8, it.rest(), "a, b ,, c, d, e");
2581 try testing.expectEqualSlices(u8, it.first(), "e");2587 try testing.expectEqualSlices(u8, it.first(), "e");
...@@ -2608,7 +2614,7 @@ test "splitBackwardsSequence" {...@@ -2608,7 +2614,7 @@ test "splitBackwardsSequence" {
2608 try testing.expect(it16.next() == null);2614 try testing.expect(it16.next() == null);
2609}2615}
26102616
2611test "splitBackwardsAny" {2617test splitBackwardsAny {
2612 var it = splitBackwardsAny(u8, "a,b, c d e", ", ");2618 var it = splitBackwardsAny(u8, "a,b, c d e", ", ");
2613 try testing.expectEqualSlices(u8, it.rest(), "a,b, c d e");2619 try testing.expectEqualSlices(u8, it.rest(), "a,b, c d e");
2614 try testing.expectEqualSlices(u8, it.first(), "e");2620 try testing.expectEqualSlices(u8, it.first(), "e");
...@@ -2715,7 +2721,7 @@ pub fn window(comptime T: type, buffer: []const T, size: usize, advance: usize)...@@ -2715,7 +2721,7 @@ pub fn window(comptime T: type, buffer: []const T, size: usize, advance: usize)
2715 };2721 };
2716}2722}
27172723
2718test "window" {2724test window {
2719 {2725 {
2720 // moving average size 32726 // moving average size 3
2721 var it = window(u8, "abcdefg", 3, 1);2727 var it = window(u8, "abcdefg", 3, 1);
...@@ -2841,7 +2847,7 @@ pub fn startsWith(comptime T: type, haystack: []const T, needle: []const T) bool...@@ -2841,7 +2847,7 @@ pub fn startsWith(comptime T: type, haystack: []const T, needle: []const T) bool
2841 return if (needle.len > haystack.len) false else eql(T, haystack[0..needle.len], needle);2847 return if (needle.len > haystack.len) false else eql(T, haystack[0..needle.len], needle);
2842}2848}
28432849
2844test "startsWith" {2850test startsWith {
2845 try testing.expect(startsWith(u8, "Bob", "Bo"));2851 try testing.expect(startsWith(u8, "Bob", "Bo"));
2846 try testing.expect(!startsWith(u8, "Needle in haystack", "haystack"));2852 try testing.expect(!startsWith(u8, "Needle in haystack", "haystack"));
2847}2853}
...@@ -2850,7 +2856,7 @@ pub fn endsWith(comptime T: type, haystack: []const T, needle: []const T) bool {...@@ -2850,7 +2856,7 @@ pub fn endsWith(comptime T: type, haystack: []const T, needle: []const T) bool {
2850 return if (needle.len > haystack.len) false else eql(T, haystack[haystack.len - needle.len ..], needle);2856 return if (needle.len > haystack.len) false else eql(T, haystack[haystack.len - needle.len ..], needle);
2851}2857}
28522858
2853test "endsWith" {2859test endsWith {
2854 try testing.expect(endsWith(u8, "Needle in haystack", "haystack"));2860 try testing.expect(endsWith(u8, "Needle in haystack", "haystack"));
2855 try testing.expect(!endsWith(u8, "Bob", "Bo"));2861 try testing.expect(!endsWith(u8, "Bob", "Bo"));
2856}2862}
...@@ -3086,7 +3092,7 @@ fn joinMaybeZ(allocator: Allocator, separator: []const u8, slices: []const []con...@@ -3086,7 +3092,7 @@ fn joinMaybeZ(allocator: Allocator, separator: []const u8, slices: []const []con
3086 return buf;3092 return buf;
3087}3093}
30883094
3089test "join" {3095test join {
3090 {3096 {
3091 const str = try join(testing.allocator, ",", &[_][]const u8{});3097 const str = try join(testing.allocator, ",", &[_][]const u8{});
3092 defer testing.allocator.free(str);3098 defer testing.allocator.free(str);
...@@ -3109,7 +3115,7 @@ test "join" {...@@ -3109,7 +3115,7 @@ test "join" {
3109 }3115 }
3110}3116}
31113117
3112test "joinZ" {3118test joinZ {
3113 {3119 {
3114 const str = try joinZ(testing.allocator, ",", &[_][]const u8{});3120 const str = try joinZ(testing.allocator, ",", &[_][]const u8{});
3115 defer testing.allocator.free(str);3121 defer testing.allocator.free(str);
...@@ -3181,7 +3187,7 @@ pub fn concatMaybeSentinel(allocator: Allocator, comptime T: type, slices: []con...@@ -3181,7 +3187,7 @@ pub fn concatMaybeSentinel(allocator: Allocator, comptime T: type, slices: []con
3181 return buf;3187 return buf;
3182}3188}
31833189
3184test "concat" {3190test concat {
3185 {3191 {
3186 const str = try concat(testing.allocator, u8, &[_][]const u8{ "abc", "def", "ghi" });3192 const str = try concat(testing.allocator, u8, &[_][]const u8{ "abc", "def", "ghi" });
3187 defer testing.allocator.free(str);3193 defer testing.allocator.free(str);
...@@ -3219,17 +3225,13 @@ test "concat" {...@@ -3219,17 +3225,13 @@ test "concat" {
3219 }3225 }
3220}3226}
32213227
3222test "testStringEquality" {3228test eql {
3223 try testing.expect(eql(u8, "abcd", "abcd"));3229 try testing.expect(eql(u8, "abcd", "abcd"));
3224 try testing.expect(!eql(u8, "abcdef", "abZdef"));3230 try testing.expect(!eql(u8, "abcdef", "abZdef"));
3225 try testing.expect(!eql(u8, "abcdefg", "abcdef"));3231 try testing.expect(!eql(u8, "abcdefg", "abcdef"));
3226}3232}
32273233
3228test "testReadInt" {3234fn moreReadIntTests() !void {
3229 try testReadIntImpl();
3230 try comptime testReadIntImpl();
3231}
3232fn testReadIntImpl() !void {
3233 {3235 {
3234 const bytes = [_]u8{3236 const bytes = [_]u8{
3235 0x12,3237 0x12,
...@@ -3287,7 +3289,7 @@ pub fn min(comptime T: type, slice: []const T) T {...@@ -3287,7 +3289,7 @@ pub fn min(comptime T: type, slice: []const T) T {
3287 return best;3289 return best;
3288}3290}
32893291
3290test "min" {3292test min {
3291 try testing.expectEqual(min(u8, "abcdefg"), 'a');3293 try testing.expectEqual(min(u8, "abcdefg"), 'a');
3292 try testing.expectEqual(min(u8, "bcdefga"), 'a');3294 try testing.expectEqual(min(u8, "bcdefga"), 'a');
3293 try testing.expectEqual(min(u8, "a"), 'a');3295 try testing.expectEqual(min(u8, "a"), 'a');
...@@ -3304,7 +3306,7 @@ pub fn max(comptime T: type, slice: []const T) T {...@@ -3304,7 +3306,7 @@ pub fn max(comptime T: type, slice: []const T) T {
3304 return best;3306 return best;
3305}3307}
33063308
3307test "max" {3309test max {
3308 try testing.expectEqual(max(u8, "abcdefg"), 'g');3310 try testing.expectEqual(max(u8, "abcdefg"), 'g');
3309 try testing.expectEqual(max(u8, "gabcdef"), 'g');3311 try testing.expectEqual(max(u8, "gabcdef"), 'g');
3310 try testing.expectEqual(max(u8, "g"), 'g');3312 try testing.expectEqual(max(u8, "g"), 'g');
...@@ -3357,7 +3359,7 @@ pub fn indexOfMin(comptime T: type, slice: []const T) usize {...@@ -3357,7 +3359,7 @@ pub fn indexOfMin(comptime T: type, slice: []const T) usize {
3357 return index;3359 return index;
3358}3360}
33593361
3360test "indexOfMin" {3362test indexOfMin {
3361 try testing.expectEqual(indexOfMin(u8, "abcdefg"), 0);3363 try testing.expectEqual(indexOfMin(u8, "abcdefg"), 0);
3362 try testing.expectEqual(indexOfMin(u8, "bcdefga"), 6);3364 try testing.expectEqual(indexOfMin(u8, "bcdefga"), 6);
3363 try testing.expectEqual(indexOfMin(u8, "a"), 0);3365 try testing.expectEqual(indexOfMin(u8, "a"), 0);
...@@ -3378,7 +3380,7 @@ pub fn indexOfMax(comptime T: type, slice: []const T) usize {...@@ -3378,7 +3380,7 @@ pub fn indexOfMax(comptime T: type, slice: []const T) usize {
3378 return index;3380 return index;
3379}3381}
33803382
3381test "indexOfMax" {3383test indexOfMax {
3382 try testing.expectEqual(indexOfMax(u8, "abcdefg"), 6);3384 try testing.expectEqual(indexOfMax(u8, "abcdefg"), 6);
3383 try testing.expectEqual(indexOfMax(u8, "gabcdef"), 0);3385 try testing.expectEqual(indexOfMax(u8, "gabcdef"), 0);
3384 try testing.expectEqual(indexOfMax(u8, "a"), 0);3386 try testing.expectEqual(indexOfMax(u8, "a"), 0);
...@@ -3408,7 +3410,7 @@ pub fn indexOfMinMax(comptime T: type, slice: []const T) IndexOfMinMaxResult {...@@ -3408,7 +3410,7 @@ pub fn indexOfMinMax(comptime T: type, slice: []const T) IndexOfMinMaxResult {
34083410
3409pub const IndexOfMinMaxResult = struct { index_min: usize, index_max: usize };3411pub const IndexOfMinMaxResult = struct { index_min: usize, index_max: usize };
34103412
3411test "indexOfMinMax" {3413test indexOfMinMax {
3412 try testing.expectEqual(IndexOfMinMaxResult{ .index_min = 0, .index_max = 6 }, indexOfMinMax(u8, "abcdefg"));3414 try testing.expectEqual(IndexOfMinMaxResult{ .index_min = 0, .index_max = 6 }, indexOfMinMax(u8, "abcdefg"));
3413 try testing.expectEqual(IndexOfMinMaxResult{ .index_min = 1, .index_max = 0 }, indexOfMinMax(u8, "gabcdef"));3415 try testing.expectEqual(IndexOfMinMaxResult{ .index_min = 1, .index_max = 0 }, indexOfMinMax(u8, "gabcdef"));
3414 try testing.expectEqual(IndexOfMinMaxResult{ .index_min = 0, .index_max = 0 }, indexOfMinMax(u8, "a"));3416 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 {...@@ -3429,7 +3431,7 @@ pub fn reverse(comptime T: type, items: []T) void {
3429 }3431 }
3430}3432}
34313433
3432test "reverse" {3434test reverse {
3433 var arr = [_]i32{ 5, 3, 1, 2, 4 };3435 var arr = [_]i32{ 5, 3, 1, 2, 4 };
3434 reverse(i32, arr[0..]);3436 reverse(i32, arr[0..]);
34353437
...@@ -3490,7 +3492,7 @@ pub fn reverseIterator(slice: anytype) ReverseIterator(@TypeOf(slice)) {...@@ -3490,7 +3492,7 @@ pub fn reverseIterator(slice: anytype) ReverseIterator(@TypeOf(slice)) {
3490 return .{ .ptr = slice.ptr, .index = slice.len };3492 return .{ .ptr = slice.ptr, .index = slice.len };
3491}3493}
34923494
3493test "reverseIterator" {3495test reverseIterator {
3494 {3496 {
3495 var it = reverseIterator("abc");3497 var it = reverseIterator("abc");
3496 try testing.expectEqual(@as(?u8, 'c'), it.next());3498 try testing.expectEqual(@as(?u8, 'c'), it.next());
...@@ -3548,7 +3550,7 @@ pub fn rotate(comptime T: type, items: []T, amount: usize) void {...@@ -3548,7 +3550,7 @@ pub fn rotate(comptime T: type, items: []T, amount: usize) void {
3548 reverse(T, items);3550 reverse(T, items);
3549}3551}
35503552
3551test "rotate" {3553test rotate {
3552 var arr = [_]i32{ 5, 3, 1, 2, 4 };3554 var arr = [_]i32{ 5, 3, 1, 2, 4 };
3553 rotate(i32, arr[0..], 2);3555 rotate(i32, arr[0..], 2);
35543556
...@@ -3582,7 +3584,7 @@ pub fn replace(comptime T: type, input: []const T, needle: []const T, replacemen...@@ -3582,7 +3584,7 @@ pub fn replace(comptime T: type, input: []const T, needle: []const T, replacemen
3582 return replacements;3584 return replacements;
3583}3585}
35843586
3585test "replace" {3587test replace {
3586 var output: [29]u8 = undefined;3588 var output: [29]u8 = undefined;
3587 var replacements = replace(u8, "All your base are belong to us", "base", "Zig", output[0..]);3589 var replacements = replace(u8, "All your base are belong to us", "base", "Zig", output[0..]);
3588 var expected: []const u8 = "All your Zig are belong to us";3590 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 {...@@ -3645,7 +3647,7 @@ fn testCollapseRepeats(str: []const u8, elem: u8, expected: []const u8) !void {
3645 defer std.testing.allocator.free(mutable);3647 defer std.testing.allocator.free(mutable);
3646 try testing.expect(std.mem.eql(u8, collapseRepeats(u8, mutable, elem), expected));3648 try testing.expect(std.mem.eql(u8, collapseRepeats(u8, mutable, elem), expected));
3647}3649}
3648test "collapseRepeats" {3650test collapseRepeats {
3649 try testCollapseRepeats("", '/', "");3651 try testCollapseRepeats("", '/', "");
3650 try testCollapseRepeats("a", '/', "a");3652 try testCollapseRepeats("a", '/', "a");
3651 try testCollapseRepeats("/", '/', "/");3653 try testCollapseRepeats("/", '/', "/");
...@@ -3679,7 +3681,7 @@ pub fn replacementSize(comptime T: type, input: []const T, needle: []const T, re...@@ -3679,7 +3681,7 @@ pub fn replacementSize(comptime T: type, input: []const T, needle: []const T, re
3679 return size;3681 return size;
3680}3682}
36813683
3682test "replacementSize" {3684test replacementSize {
3683 try testing.expect(replacementSize(u8, "All your base are belong to us", "base", "Zig") == 29);3685 try testing.expect(replacementSize(u8, "All your base are belong to us", "base", "Zig") == 29);
3684 try testing.expect(replacementSize(u8, "Favor reading code over writing code.", "code", "") == 29);3686 try testing.expect(replacementSize(u8, "Favor reading code over writing code.", "code", "") == 29);
3685 try testing.expect(replacementSize(u8, "Only one obvious way to do things.", "things.", "things in Zig.") == 41);3687 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...@@ -3699,7 +3701,7 @@ pub fn replaceOwned(comptime T: type, allocator: Allocator, input: []const T, ne
3699 return output;3701 return output;
3700}3702}
37013703
3702test "replaceOwned" {3704test replaceOwned {
3703 const gpa = std.testing.allocator;3705 const gpa = std.testing.allocator;
37043706
3705 const base_replace = replaceOwned(u8, gpa, "All your base are belong to us", "base", "Zig") catch @panic("out of memory");3707 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) {...@@ -3803,7 +3805,7 @@ pub fn alignPointer(ptr: anytype, align_to: usize) ?@TypeOf(ptr) {
3803 return @alignCast(ptr + adjust_off);3805 return @alignCast(ptr + adjust_off);
3804}3806}
38053807
3806test "alignPointer" {3808test alignPointer {
3807 const S = struct {3809 const S = struct {
3808 fn checkAlign(comptime T: type, base: usize, align_to: usize, expected: usize) !void {3810 fn checkAlign(comptime T: type, base: usize, align_to: usize, expected: usize) !void {
3809 const ptr: T = @ptrFromInt(base);3811 const ptr: T = @ptrFromInt(base);
...@@ -3852,7 +3854,7 @@ pub fn asBytes(ptr: anytype) AsBytesReturnType(@TypeOf(ptr)) {...@@ -3852,7 +3854,7 @@ pub fn asBytes(ptr: anytype) AsBytesReturnType(@TypeOf(ptr)) {
3852 return @ptrCast(@alignCast(ptr));3854 return @ptrCast(@alignCast(ptr));
3853}3855}
38543856
3855test "asBytes" {3857test asBytes {
3856 const deadbeef = @as(u32, 0xDEADBEEF);3858 const deadbeef = @as(u32, 0xDEADBEEF);
3857 const deadbeef_bytes = switch (native_endian) {3859 const deadbeef_bytes = switch (native_endian) {
3858 .big => "\xDE\xAD\xBE\xEF",3860 .big => "\xDE\xAD\xBE\xEF",
...@@ -3912,7 +3914,7 @@ pub fn toBytes(value: anytype) [@sizeOf(@TypeOf(value))]u8 {...@@ -3912,7 +3914,7 @@ pub fn toBytes(value: anytype) [@sizeOf(@TypeOf(value))]u8 {
3912 return asBytes(&value).*;3914 return asBytes(&value).*;
3913}3915}
39143916
3915test "toBytes" {3917test toBytes {
3916 var my_bytes = toBytes(@as(u32, 0x12345678));3918 var my_bytes = toBytes(@as(u32, 0x12345678));
3917 switch (native_endian) {3919 switch (native_endian) {
3918 .big => try testing.expect(eql(u8, &my_bytes, "\x12\x34\x56\x78")),3920 .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,...@@ -3936,7 +3938,7 @@ pub fn bytesAsValue(comptime T: type, bytes: anytype) BytesAsValueReturnType(T,
3936 return @ptrCast(bytes);3938 return @ptrCast(bytes);
3937}3939}
39383940
3939test "bytesAsValue" {3941test bytesAsValue {
3940 const deadbeef = @as(u32, 0xDEADBEEF);3942 const deadbeef = @as(u32, 0xDEADBEEF);
3941 const deadbeef_bytes = switch (native_endian) {3943 const deadbeef_bytes = switch (native_endian) {
3942 .big => "\xDE\xAD\xBE\xEF",3944 .big => "\xDE\xAD\xBE\xEF",
...@@ -3995,7 +3997,7 @@ test "bytesAsValue preserves pointer attributes" {...@@ -3995,7 +3997,7 @@ test "bytesAsValue preserves pointer attributes" {
3995pub fn bytesToValue(comptime T: type, bytes: anytype) T {3997pub fn bytesToValue(comptime T: type, bytes: anytype) T {
3996 return bytesAsValue(T, bytes).*;3998 return bytesAsValue(T, bytes).*;
3997}3999}
3998test "bytesToValue" {4000test bytesToValue {
3999 const deadbeef_bytes = switch (native_endian) {4001 const deadbeef_bytes = switch (native_endian) {
4000 .big => "\xDE\xAD\xBE\xEF",4002 .big => "\xDE\xAD\xBE\xEF",
4001 .little => "\xEF\xBE\xAD\xDE",4003 .little => "\xEF\xBE\xAD\xDE",
...@@ -4023,7 +4025,7 @@ pub fn bytesAsSlice(comptime T: type, bytes: anytype) BytesAsSliceReturnType(T,...@@ -4023,7 +4025,7 @@ pub fn bytesAsSlice(comptime T: type, bytes: anytype) BytesAsSliceReturnType(T,
4023 return @as(cast_target, @ptrCast(bytes))[0..@divExact(bytes.len, @sizeOf(T))];4025 return @as(cast_target, @ptrCast(bytes))[0..@divExact(bytes.len, @sizeOf(T))];
4024}4026}
40254027
4026test "bytesAsSlice" {4028test bytesAsSlice {
4027 {4029 {
4028 const bytes = [_]u8{ 0xDE, 0xAD, 0xBE, 0xEF };4030 const bytes = [_]u8{ 0xDE, 0xAD, 0xBE, 0xEF };
4029 const slice = bytesAsSlice(u16, bytes[0..]);4031 const slice = bytesAsSlice(u16, bytes[0..]);
...@@ -4112,7 +4114,7 @@ pub fn sliceAsBytes(slice: anytype) SliceAsBytesReturnType(@TypeOf(slice)) {...@@ -4112,7 +4114,7 @@ pub fn sliceAsBytes(slice: anytype) SliceAsBytesReturnType(@TypeOf(slice)) {
4112 return @as(cast_target, @ptrCast(slice))[0 .. slice.len * @sizeOf(std.meta.Elem(Slice))];4114 return @as(cast_target, @ptrCast(slice))[0 .. slice.len * @sizeOf(std.meta.Elem(Slice))];
4113}4115}
41144116
4115test "sliceAsBytes" {4117test sliceAsBytes {
4116 const bytes = [_]u16{ 0xDEAD, 0xBEEF };4118 const bytes = [_]u16{ 0xDEAD, 0xBEEF };
4117 const slice = sliceAsBytes(bytes[0..]);4119 const slice = sliceAsBytes(bytes[0..]);
4118 try testing.expect(slice.len == 4);4120 try testing.expect(slice.len == 4);
...@@ -4270,7 +4272,7 @@ fn doNotOptimizeAwayC(ptr: anytype) void {...@@ -4270,7 +4272,7 @@ fn doNotOptimizeAwayC(ptr: anytype) void {
4270 dest.* = 0;4272 dest.* = 0;
4271}4273}
42724274
4273test "doNotOptimizeAway" {4275test doNotOptimizeAway {
4274 comptime doNotOptimizeAway("test");4276 comptime doNotOptimizeAway("test");
42754277
4276 doNotOptimizeAway(null);4278 doNotOptimizeAway(null);
...@@ -4295,7 +4297,7 @@ test "doNotOptimizeAway" {...@@ -4295,7 +4297,7 @@ test "doNotOptimizeAway" {
4295 doNotOptimizeAway(@as(std.builtin.Endian, .little));4297 doNotOptimizeAway(@as(std.builtin.Endian, .little));
4296}4298}
42974299
4298test "alignForward" {4300test alignForward {
4299 try testing.expect(alignForward(usize, 1, 1) == 1);4301 try testing.expect(alignForward(usize, 1, 1) == 1);
4300 try testing.expect(alignForward(usize, 2, 1) == 2);4302 try testing.expect(alignForward(usize, 2, 1) == 2);
4301 try testing.expect(alignForward(usize, 1, 2) == 2);4303 try testing.expect(alignForward(usize, 1, 2) == 2);
...@@ -4364,7 +4366,7 @@ pub fn isAlignedGeneric(comptime T: type, addr: T, alignment: T) bool {...@@ -4364,7 +4366,7 @@ pub fn isAlignedGeneric(comptime T: type, addr: T, alignment: T) bool {
4364 return alignBackward(T, addr, alignment) == addr;4366 return alignBackward(T, addr, alignment) == addr;
4365}4367}
43664368
4367test "isAligned" {4369test isAligned {
4368 try testing.expect(isAligned(0, 4));4370 try testing.expect(isAligned(0, 4));
4369 try testing.expect(isAligned(1, 1));4371 try testing.expect(isAligned(1, 1));
4370 try testing.expect(isAligned(2, 1));4372 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 {...@@ -132,7 +132,7 @@ pub fn TrailerFlags(comptime Fields: type) type {
132 };132 };
133}133}
134134
135test "TrailerFlags" {135test TrailerFlags {
136 const Flags = TrailerFlags(struct {136 const Flags = TrailerFlags(struct {
137 a: i32,137 a: i32,
138 b: bool,138 b: bool,
lib/std/os/windows.zig+4-4
...@@ -1077,7 +1077,7 @@ pub fn QueryObjectName(...@@ -1077,7 +1077,7 @@ pub fn QueryObjectName(
1077 else => |e| return unexpectedStatus(e),1077 else => |e| return unexpectedStatus(e),
1078 }1078 }
1079}1079}
1080test "QueryObjectName" {1080test QueryObjectName {
1081 if (builtin.os.tag != .windows)1081 if (builtin.os.tag != .windows)
1082 return;1082 return;
10831083
...@@ -1245,7 +1245,7 @@ pub fn GetFinalPathNameByHandle(...@@ -1245,7 +1245,7 @@ pub fn GetFinalPathNameByHandle(
1245 }1245 }
1246}1246}
12471247
1248test "GetFinalPathNameByHandle" {1248test GetFinalPathNameByHandle {
1249 if (builtin.os.tag != .windows)1249 if (builtin.os.tag != .windows)
1250 return;1250 return;
12511251
...@@ -2467,7 +2467,7 @@ pub fn ntToWin32Namespace(path: []const u16) !PathSpace {...@@ -2467,7 +2467,7 @@ pub fn ntToWin32Namespace(path: []const u16) !PathSpace {
2467 }2467 }
2468}2468}
24692469
2470test "ntToWin32Namespace" {2470test ntToWin32Namespace {
2471 const L = std.unicode.utf8ToUtf16LeStringLiteral;2471 const L = std.unicode.utf8ToUtf16LeStringLiteral;
24722472
2473 try testNtToWin32Namespace(L("UNC"), L("\\??\\UNC"));2473 try testNtToWin32Namespace(L("UNC"), L("\\??\\UNC"));
...@@ -3386,7 +3386,7 @@ pub const GUID = extern struct {...@@ -3386,7 +3386,7 @@ pub const GUID = extern struct {
3386 }3386 }
3387};3387};
33883388
3389test "GUID" {3389test GUID {
3390 try std.testing.expectEqual(3390 try std.testing.expectEqual(
3391 GUID{3391 GUID{
3392 .Data1 = 0x01234567,3392 .Data1 = 0x01234567,
lib/std/process.zig+3-3
...@@ -209,7 +209,7 @@ pub const EnvMap = struct {...@@ -209,7 +209,7 @@ pub const EnvMap = struct {
209 }209 }
210};210};
211211
212test "EnvMap" {212test EnvMap {
213 var env = EnvMap.init(testing.allocator);213 var env = EnvMap.init(testing.allocator);
214 defer env.deinit();214 defer env.deinit();
215215
...@@ -371,7 +371,7 @@ pub fn getEnvMap(allocator: Allocator) GetEnvMapError!EnvMap {...@@ -371,7 +371,7 @@ pub fn getEnvMap(allocator: Allocator) GetEnvMapError!EnvMap {
371 }371 }
372}372}
373373
374test "getEnvMap" {374test getEnvMap {
375 var env = try getEnvMap(testing.allocator);375 var env = try getEnvMap(testing.allocator);
376 defer env.deinit();376 defer env.deinit();
377}377}
...@@ -1132,7 +1132,7 @@ pub fn argsFree(allocator: Allocator, args_alloc: []const [:0]u8) void {...@@ -1132,7 +1132,7 @@ pub fn argsFree(allocator: Allocator, args_alloc: []const [:0]u8) void {
1132 return allocator.free(aligned_allocated_buf);1132 return allocator.free(aligned_allocated_buf);
1133}1133}
11341134
1135test "ArgIteratorWindows" {1135test ArgIteratorWindows {
1136 const t = testArgIteratorWindows;1136 const t = testArgIteratorWindows;
11371137
1138 try t(1138 try t(
lib/std/sort.zig+9-9
...@@ -430,7 +430,7 @@ pub fn binarySearch(...@@ -430,7 +430,7 @@ pub fn binarySearch(
430 return null;430 return null;
431}431}
432432
433test "binarySearch" {433test binarySearch {
434 const S = struct {434 const S = struct {
435 fn order_u32(context: void, lhs: u32, rhs: u32) math.Order {435 fn order_u32(context: void, lhs: u32, rhs: u32) math.Order {
436 _ = context;436 _ = context;
...@@ -537,7 +537,7 @@ pub fn lowerBound(...@@ -537,7 +537,7 @@ pub fn lowerBound(
537 return left;537 return left;
538}538}
539539
540test "lowerBound" {540test lowerBound {
541 const S = struct {541 const S = struct {
542 fn lower_u32(context: void, lhs: u32, rhs: u32) bool {542 fn lower_u32(context: void, lhs: u32, rhs: u32) bool {
543 _ = context;543 _ = context;
...@@ -627,7 +627,7 @@ pub fn upperBound(...@@ -627,7 +627,7 @@ pub fn upperBound(
627 return left;627 return left;
628}628}
629629
630test "upperBound" {630test upperBound {
631 const S = struct {631 const S = struct {
632 fn lower_u32(context: void, lhs: u32, rhs: u32) bool {632 fn lower_u32(context: void, lhs: u32, rhs: u32) bool {
633 _ = context;633 _ = context;
...@@ -712,7 +712,7 @@ pub fn equalRange(...@@ -712,7 +712,7 @@ pub fn equalRange(
712 };712 };
713}713}
714714
715test "equalRange" {715test equalRange {
716 const S = struct {716 const S = struct {
717 fn lower_u32(context: void, lhs: u32, rhs: u32) bool {717 fn lower_u32(context: void, lhs: u32, rhs: u32) bool {
718 _ = context;718 _ = context;
...@@ -792,7 +792,7 @@ pub fn argMin(...@@ -792,7 +792,7 @@ pub fn argMin(
792 return smallest_index;792 return smallest_index;
793}793}
794794
795test "argMin" {795test argMin {
796 try testing.expectEqual(@as(?usize, null), argMin(i32, &[_]i32{}, {}, asc_i32));796 try testing.expectEqual(@as(?usize, null), argMin(i32, &[_]i32{}, {}, asc_i32));
797 try testing.expectEqual(@as(?usize, 0), argMin(i32, &[_]i32{1}, {}, asc_i32));797 try testing.expectEqual(@as(?usize, 0), argMin(i32, &[_]i32{1}, {}, asc_i32));
798 try testing.expectEqual(@as(?usize, 0), argMin(i32, &[_]i32{ 1, 2, 3, 4, 5 }, {}, asc_i32));798 try testing.expectEqual(@as(?usize, 0), argMin(i32, &[_]i32{ 1, 2, 3, 4, 5 }, {}, asc_i32));
...@@ -812,7 +812,7 @@ pub fn min(...@@ -812,7 +812,7 @@ pub fn min(
812 return items[i];812 return items[i];
813}813}
814814
815test "min" {815test min {
816 try testing.expectEqual(@as(?i32, null), min(i32, &[_]i32{}, {}, asc_i32));816 try testing.expectEqual(@as(?i32, null), min(i32, &[_]i32{}, {}, asc_i32));
817 try testing.expectEqual(@as(?i32, 1), min(i32, &[_]i32{1}, {}, asc_i32));817 try testing.expectEqual(@as(?i32, 1), min(i32, &[_]i32{1}, {}, asc_i32));
818 try testing.expectEqual(@as(?i32, 1), min(i32, &[_]i32{ 1, 2, 3, 4, 5 }, {}, asc_i32));818 try testing.expectEqual(@as(?i32, 1), min(i32, &[_]i32{ 1, 2, 3, 4, 5 }, {}, asc_i32));
...@@ -844,7 +844,7 @@ pub fn argMax(...@@ -844,7 +844,7 @@ pub fn argMax(
844 return biggest_index;844 return biggest_index;
845}845}
846846
847test "argMax" {847test argMax {
848 try testing.expectEqual(@as(?usize, null), argMax(i32, &[_]i32{}, {}, asc_i32));848 try testing.expectEqual(@as(?usize, null), argMax(i32, &[_]i32{}, {}, asc_i32));
849 try testing.expectEqual(@as(?usize, 0), argMax(i32, &[_]i32{1}, {}, asc_i32));849 try testing.expectEqual(@as(?usize, 0), argMax(i32, &[_]i32{1}, {}, asc_i32));
850 try testing.expectEqual(@as(?usize, 4), argMax(i32, &[_]i32{ 1, 2, 3, 4, 5 }, {}, asc_i32));850 try testing.expectEqual(@as(?usize, 4), argMax(i32, &[_]i32{ 1, 2, 3, 4, 5 }, {}, asc_i32));
...@@ -864,7 +864,7 @@ pub fn max(...@@ -864,7 +864,7 @@ pub fn max(
864 return items[i];864 return items[i];
865}865}
866866
867test "max" {867test max {
868 try testing.expectEqual(@as(?i32, null), max(i32, &[_]i32{}, {}, asc_i32));868 try testing.expectEqual(@as(?i32, null), max(i32, &[_]i32{}, {}, asc_i32));
869 try testing.expectEqual(@as(?i32, 1), max(i32, &[_]i32{1}, {}, asc_i32));869 try testing.expectEqual(@as(?i32, 1), max(i32, &[_]i32{1}, {}, asc_i32));
870 try testing.expectEqual(@as(?i32, 5), max(i32, &[_]i32{ 1, 2, 3, 4, 5 }, {}, asc_i32));870 try testing.expectEqual(@as(?i32, 5), max(i32, &[_]i32{ 1, 2, 3, 4, 5 }, {}, asc_i32));
...@@ -890,7 +890,7 @@ pub fn isSorted(...@@ -890,7 +890,7 @@ pub fn isSorted(
890 return true;890 return true;
891}891}
892892
893test "isSorted" {893test isSorted {
894 try testing.expect(isSorted(i32, &[_]i32{}, {}, asc_i32));894 try testing.expect(isSorted(i32, &[_]i32{}, {}, asc_i32));
895 try testing.expect(isSorted(i32, &[_]i32{10}, {}, asc_i32));895 try testing.expect(isSorted(i32, &[_]i32{10}, {}, asc_i32));
896 try testing.expect(isSorted(i32, &[_]i32{ 1, 2, 3, 4, 5 }, {}, asc_i32));896 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 {...@@ -676,7 +676,7 @@ fn stripComponents(path: []const u8, count: u32) []const u8 {
676 return path[i..];676 return path[i..];
677}677}
678678
679test "stripComponents" {679test stripComponents {
680 const expectEqualStrings = testing.expectEqualStrings;680 const expectEqualStrings = testing.expectEqualStrings;
681 try expectEqualStrings("a/b/c", stripComponents("a/b/c", 0));681 try expectEqualStrings("a/b/c", stripComponents("a/b/c", 0));
682 try expectEqualStrings("b/c", stripComponents("a/b/c", 1));682 try expectEqualStrings("b/c", stripComponents("a/b/c", 1));
...@@ -685,7 +685,7 @@ test "stripComponents" {...@@ -685,7 +685,7 @@ test "stripComponents" {
685 try expectEqualStrings("", stripComponents("a/b/c", 4));685 try expectEqualStrings("", stripComponents("a/b/c", 4));
686}686}
687687
688test "PaxIterator" {688test PaxIterator {
689 const Attr = struct {689 const Attr = struct {
690 kind: PaxAttributeKind,690 kind: PaxAttributeKind,
691 value: []const u8 = undefined,691 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...@@ -242,7 +242,7 @@ fn expectApproxEqAbsInner(comptime T: type, expected: T, actual: T, tolerance: T
242 }242 }
243}243}
244244
245test "expectApproxEqAbs" {245test expectApproxEqAbs {
246 inline for ([_]type{ f16, f32, f64, f128 }) |T| {246 inline for ([_]type{ f16, f32, f64, f128 }) |T| {
247 const pos_x: T = 12.0;247 const pos_x: T = 12.0;
248 const pos_y: T = 12.06;248 const pos_y: T = 12.06;
...@@ -278,7 +278,7 @@ fn expectApproxEqRelInner(comptime T: type, expected: T, actual: T, tolerance: T...@@ -278,7 +278,7 @@ fn expectApproxEqRelInner(comptime T: type, expected: T, actual: T, tolerance: T
278 }278 }
279}279}
280280
281test "expectApproxEqRel" {281test expectApproxEqRel {
282 inline for ([_]type{ f16, f32, f64, f128 }) |T| {282 inline for ([_]type{ f16, f32, f64, f128 }) |T| {
283 const eps_value = comptime math.floatEps(T);283 const eps_value = comptime math.floatEps(T);
284 const sqrt_eps_value = comptime @sqrt(eps_value);284 const sqrt_eps_value = comptime @sqrt(eps_value);
lib/std/time.zig+3-3
...@@ -52,7 +52,7 @@ pub fn sleep(nanoseconds: u64) void {...@@ -52,7 +52,7 @@ pub fn sleep(nanoseconds: u64) void {
52 std.os.nanosleep(s, ns);52 std.os.nanosleep(s, ns);
53}53}
5454
55test "sleep" {55test sleep {
56 sleep(1);56 sleep(1);
57}57}
5858
...@@ -122,7 +122,7 @@ pub fn nanoTimestamp() i128 {...@@ -122,7 +122,7 @@ pub fn nanoTimestamp() i128 {
122 }122 }
123}123}
124124
125test "timestamp" {125test milliTimestamp {
126 const margin = ns_per_ms * 50;126 const margin = ns_per_ms * 50;
127127
128 const time_0 = milliTimestamp();128 const time_0 = milliTimestamp();
...@@ -326,7 +326,7 @@ pub const Timer = struct {...@@ -326,7 +326,7 @@ pub const Timer = struct {
326 }326 }
327};327};
328328
329test "Timer + Instant" {329test Timer {
330 const margin = ns_per_ms * 150;330 const margin = ns_per_ms * 150;
331331
332 var timer = try Timer.start();332 var timer = try Timer.start();
lib/std/time/epoch.zig+1-1
...@@ -53,7 +53,7 @@ pub fn isLeapYear(year: Year) bool {...@@ -53,7 +53,7 @@ pub fn isLeapYear(year: Year) bool {
53 return (0 == @mod(year, 400));53 return (0 == @mod(year, 400));
54}54}
5555
56test "isLeapYear" {56test isLeapYear {
57 try testing.expectEqual(false, isLeapYear(2095));57 try testing.expectEqual(false, isLeapYear(2095));
58 try testing.expectEqual(true, isLeapYear(2096));58 try testing.expectEqual(true, isLeapYear(2096));
59 try testing.expectEqual(false, isLeapYear(2100));59 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) {...@@ -907,7 +907,7 @@ pub fn fmtUtf8(utf8: []const u8) std.fmt.Formatter(formatUtf8) {
907 return .{ .data = utf8 };907 return .{ .data = utf8 };
908}908}
909909
910test "fmtUtf8" {910test fmtUtf8 {
911 const expectFmt = testing.expectFmt;911 const expectFmt = testing.expectFmt;
912 try expectFmt("", "{}", .{fmtUtf8("")});912 try expectFmt("", "{}", .{fmtUtf8("")});
913 try expectFmt("foo", "{}", .{fmtUtf8("foo")});913 try expectFmt("foo", "{}", .{fmtUtf8("foo")});
...@@ -1249,7 +1249,7 @@ pub fn utf8ToUtf16LeImpl(utf16le: []u16, utf8: []const u8, comptime surrogates:...@@ -1249,7 +1249,7 @@ pub fn utf8ToUtf16LeImpl(utf16le: []u16, utf8: []const u8, comptime surrogates:
1249 return dest_index;1249 return dest_index;
1250}1250}
12511251
1252test "utf8ToUtf16Le" {1252test utf8ToUtf16Le {
1253 var utf16le: [128]u16 = undefined;1253 var utf16le: [128]u16 = undefined;
1254 {1254 {
1255 const length = try utf8ToUtf16Le(utf16le[0..], "𐐷");1255 const length = try utf8ToUtf16Le(utf16le[0..], "𐐷");
...@@ -1430,7 +1430,7 @@ pub fn fmtUtf16Le(utf16le: []const u16) std.fmt.Formatter(formatUtf16Le) {...@@ -1430,7 +1430,7 @@ pub fn fmtUtf16Le(utf16le: []const u16) std.fmt.Formatter(formatUtf16Le) {
1430 return .{ .data = utf16le };1430 return .{ .data = utf16le };
1431}1431}
14321432
1433test "fmtUtf16Le" {1433test fmtUtf16Le {
1434 const expectFmt = testing.expectFmt;1434 const expectFmt = testing.expectFmt;
1435 try expectFmt("", "{}", .{fmtUtf16Le(utf8ToUtf16LeStringLiteral(""))});1435 try expectFmt("", "{}", .{fmtUtf16Le(utf8ToUtf16LeStringLiteral(""))});
1436 try expectFmt("foo", "{}", .{fmtUtf16Le(utf8ToUtf16LeStringLiteral("foo"))});1436 try expectFmt("foo", "{}", .{fmtUtf16Le(utf8ToUtf16LeStringLiteral("foo"))});
...@@ -1443,7 +1443,7 @@ test "fmtUtf16Le" {...@@ -1443,7 +1443,7 @@ test "fmtUtf16Le" {
1443 try expectFmt("ξ€€", "{}", .{fmtUtf16Le(&[_]u16{mem.readInt(u16, "\x00\xe0", native_endian)})});1443 try expectFmt("ξ€€", "{}", .{fmtUtf16Le(&[_]u16{mem.readInt(u16, "\x00\xe0", native_endian)})});
1444}1444}
14451445
1446test "utf8ToUtf16LeStringLiteral" {1446test utf8ToUtf16LeStringLiteral {
1447 {1447 {
1448 const bytes = [_:0]u16{1448 const bytes = [_:0]u16{
1449 mem.nativeToLittle(u16, 0x41),1449 mem.nativeToLittle(u16, 0x41),
lib/std/valgrind/memcheck.zig+2-2
...@@ -137,7 +137,7 @@ pub fn countLeaks() CountResult {...@@ -137,7 +137,7 @@ pub fn countLeaks() CountResult {
137 return res;137 return res;
138}138}
139139
140test "countLeaks" {140test countLeaks {
141 try testing.expectEqual(141 try testing.expectEqual(
142 @as(CountResult, .{142 @as(CountResult, .{
143 .leaked = 0,143 .leaked = 0,
...@@ -167,7 +167,7 @@ pub fn countLeakBlocks() CountResult {...@@ -167,7 +167,7 @@ pub fn countLeakBlocks() CountResult {
167 return res;167 return res;
168}168}
169169
170test "countLeakBlocks" {170test countLeakBlocks {
171 try testing.expectEqual(171 try testing.expectEqual(
172 @as(CountResult, .{172 @as(CountResult, .{
173 .leaked = 0,173 .leaked = 0,
lib/std/zig/ErrorBundle.zig+77-77
...@@ -642,96 +642,96 @@ pub const Wip = struct {...@@ -642,96 +642,96 @@ pub const Wip = struct {
642 i += 1;642 i += 1;
643 }643 }
644 }644 }
645};
646645
647test "addBundleAsRoots" {646 test addBundleAsRoots {
648 var bundle = bundle: {647 var bundle = bundle: {
649 var wip: ErrorBundle.Wip = undefined;648 var wip: ErrorBundle.Wip = undefined;
650 try wip.init(std.testing.allocator);649 try wip.init(std.testing.allocator);
651 errdefer wip.deinit();650 errdefer wip.deinit();
652651
653 var ref_traces: [3]ReferenceTrace = undefined;652 var ref_traces: [3]ReferenceTrace = undefined;
654 for (&ref_traces, 0..) |*ref_trace, i| {653 for (&ref_traces, 0..) |*ref_trace, i| {
655 if (i == ref_traces.len - 1) {654 if (i == ref_traces.len - 1) {
656 // sentinel reference trace655 // sentinel reference trace
657 ref_trace.* = .{656 ref_trace.* = .{
658 .decl_name = 3, // signifies 3 hidden references657 .decl_name = 3, // signifies 3 hidden references
659 .src_loc = .none,658 .src_loc = .none,
660 };659 };
661 } else {660 } else {
662 ref_trace.* = .{661 ref_trace.* = .{
663 .decl_name = try wip.addString("foo"),662 .decl_name = try wip.addString("foo"),
664 .src_loc = try wip.addSourceLocation(.{663 .src_loc = try wip.addSourceLocation(.{
665 .src_path = try wip.addString("foo"),664 .src_path = try wip.addString("foo"),
666 .line = 1,665 .line = 1,
667 .column = 2,666 .column = 2,
668 .span_start = 3,667 .span_start = 3,
669 .span_main = 4,668 .span_main = 4,
670 .span_end = 5,669 .span_end = 5,
671 .source_line = 0,670 .source_line = 0,
672 }),671 }),
673 };672 };
673 }
674 }674 }
675 }
676675
677 const src_loc = try wip.addSourceLocation(.{676 const src_loc = try wip.addSourceLocation(.{
678 .src_path = try wip.addString("foo"),677 .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"),
701 .line = 1,678 .line = 1,
702 .column = 2,679 .column = 2,
703 .span_start = 3,680 .span_start = 3,
704 .span_main = 4,681 .span_main = 4,
705 .span_end = 5,682 .span_end = 5,
706 .source_line = try wip.addString("another line of source"),683 .source_line = try wip.addString("some source code"),
707 }),684 .reference_trace_len = ref_traces.len,
708 }));685 });
709 wip.extra.items[i] = note_index;686 for (&ref_traces) |ref_trace| {
687 try wip.addReferenceTrace(ref_trace);
688 }
710689
711 break :bundle try wip.toOwnedBundle("");690 try wip.addRootErrorMessage(ErrorMessage{
712 };691 .msg = try wip.addString("hello world"),
713 defer bundle.deinit(std.testing.allocator);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);716 var bundle_buf = std.ArrayList(u8).init(std.testing.allocator);
718 defer bundle_buf.deinit();717 defer bundle_buf.deinit();
719 try bundle.renderToWriter(.{ .ttyconf = ttyconf }, bundle_buf.writer());718 try bundle.renderToWriter(.{ .ttyconf = ttyconf }, bundle_buf.writer());
720719
721 var copy = copy: {720 var copy = copy: {
722 var wip: ErrorBundle.Wip = undefined;721 var wip: ErrorBundle.Wip = undefined;
723 try wip.init(std.testing.allocator);722 try wip.init(std.testing.allocator);
724 errdefer wip.deinit();723 errdefer wip.deinit();
725724
726 try wip.addBundleAsRoots(bundle);725 try wip.addBundleAsRoots(bundle);
727726
728 break :copy try wip.toOwnedBundle("");727 break :copy try wip.toOwnedBundle("");
729 };728 };
730 defer copy.deinit(std.testing.allocator);729 defer copy.deinit(std.testing.allocator);
731730
732 var copy_buf = std.ArrayList(u8).init(std.testing.allocator);731 var copy_buf = std.ArrayList(u8).init(std.testing.allocator);
733 defer copy_buf.deinit();732 defer copy_buf.deinit();
734 try copy.renderToWriter(.{ .ttyconf = ttyconf }, copy_buf.writer());733 try copy.renderToWriter(.{ .ttyconf = ttyconf }, copy_buf.writer());
735734
736 try std.testing.expectEqualStrings(bundle_buf.items, copy_buf.items);735 try std.testing.expectEqualStrings(bundle_buf.items, copy_buf.items);
737}736 }
737};
lib/std/zig/primitives.zig+1-1
...@@ -51,7 +51,7 @@ pub fn isPrimitive(name: []const u8) bool {...@@ -51,7 +51,7 @@ pub fn isPrimitive(name: []const u8) bool {
51 return true;51 return true;
52}52}
5353
54test "isPrimitive" {54test isPrimitive {
55 const expect = std.testing.expect;55 const expect = std.testing.expect;
56 try expect(!isPrimitive(""));56 try expect(!isPrimitive(""));
57 try expect(!isPrimitive("_"));57 try expect(!isPrimitive("_"));
lib/std/zig/string_literal.zig+2-2
...@@ -148,7 +148,7 @@ pub fn parseEscapeSequence(slice: []const u8, offset: *usize) ParsedCharLiteral...@@ -148,7 +148,7 @@ pub fn parseEscapeSequence(slice: []const u8, offset: *usize) ParsedCharLiteral
148 }148 }
149}149}
150150
151test "parseCharLiteral" {151test parseCharLiteral {
152 try std.testing.expectEqual(152 try std.testing.expectEqual(
153 ParsedCharLiteral{ .success = 'a' },153 ParsedCharLiteral{ .success = 'a' },
154 parseCharLiteral("'a'"),154 parseCharLiteral("'a'"),
...@@ -281,7 +281,7 @@ pub fn parseAlloc(allocator: std.mem.Allocator, bytes: []const u8) ParseError![]...@@ -281,7 +281,7 @@ pub fn parseAlloc(allocator: std.mem.Allocator, bytes: []const u8) ParseError![]
281 }281 }
282}282}
283283
284test "parse" {284test parseAlloc {
285 const expect = std.testing.expect;285 const expect = std.testing.expect;
286 const expectError = std.testing.expectError;286 const expectError = std.testing.expectError;
287 const eql = std.mem.eql;287 const eql = std.mem.eql;