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-21 14:11:46-07:00
log12191c8a220ca5594b278b5077bff98e8fecac08
treece6552d68161c363dd18826220febbaa7f733c62
parent5ae838d10596a272644da314937b01aa1a271a73

std: promote tests to doctests

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

26 files changed, 311 insertions(+), 289 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
...@@ -1231,7 +1231,7 @@ fn windowsCreateProcessSupportsExtension(ext: []const u16) ?CreateProcessSupport...@@ -1231,7 +1231,7 @@ fn windowsCreateProcessSupportsExtension(ext: []const u16) ?CreateProcessSupport
1231 return null;1231 return null;
1232}1232}
12331233
1234test "windowsCreateProcessSupportsExtension" {1234test windowsCreateProcessSupportsExtension {
1235 try std.testing.expectEqual(CreateProcessSupportedExtension.exe, windowsCreateProcessSupportsExtension(&[_]u16{ '.', 'e', 'X', 'e' }).?);1235 try std.testing.expectEqual(CreateProcessSupportedExtension.exe, windowsCreateProcessSupportsExtension(&[_]u16{ '.', 'e', 'X', 'e' }).?);
1236 try std.testing.expect(windowsCreateProcessSupportsExtension(&[_]u16{ '.', 'e', 'X', 'e', 'c' }) == null);1236 try std.testing.expect(windowsCreateProcessSupportsExtension(&[_]u16{ '.', 'e', 'X', 'e', 'c' }) == null);
1237}1237}
...@@ -1322,7 +1322,7 @@ pub fn argvToCommandLineWindows(...@@ -1322,7 +1322,7 @@ pub fn argvToCommandLineWindows(
1322 return try unicode.wtf8ToWtf16LeAllocZ(allocator, buf.items);1322 return try unicode.wtf8ToWtf16LeAllocZ(allocator, buf.items);
1323}1323}
13241324
1325test "argvToCommandLineWindows" {1325test argvToCommandLineWindows {
1326 const t = testArgvToCommandLineWindows;1326 const t = testArgvToCommandLineWindows;
13271327
1328 try t(&.{1328 try t(&.{
...@@ -1556,7 +1556,7 @@ pub fn createNullDelimitedEnvMap(arena: mem.Allocator, env_map: *const EnvMap) !...@@ -1556,7 +1556,7 @@ pub fn createNullDelimitedEnvMap(arena: mem.Allocator, env_map: *const EnvMap) !
1556 return envp_buf;1556 return envp_buf;
1557}1557}
15581558
1559test "createNullDelimitedEnvMap" {1559test createNullDelimitedEnvMap {
1560 const testing = std.testing;1560 const testing = std.testing;
1561 const allocator = testing.allocator;1561 const allocator = testing.allocator;
1562 var envmap = EnvMap.init(allocator);1562 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
...@@ -905,7 +905,7 @@ fn machoSearchSymbols(symbols: []const MachoSymbol, address: usize) ?*const Mach...@@ -905,7 +905,7 @@ fn machoSearchSymbols(symbols: []const MachoSymbol, address: usize) ?*const Mach
905 return null;905 return null;
906}906}
907907
908test "machoSearchSymbols" {908test machoSearchSymbols {
909 const symbols = [_]MachoSymbol{909 const symbols = [_]MachoSymbol{
910 .{ .addr = 100, .strx = undefined, .size = undefined, .ofile = undefined },910 .{ .addr = 100, .strx = undefined, .size = undefined, .ofile = undefined },
911 .{ .addr = 200, .strx = undefined, .size = undefined, .ofile = undefined },911 .{ .addr = 200, .strx = undefined, .size = undefined, .ofile = undefined },
...@@ -1504,7 +1504,7 @@ fn printLineFromFileAnyOs(out_stream: anytype, line_info: LineInfo) !void {...@@ -1504,7 +1504,7 @@ fn printLineFromFileAnyOs(out_stream: anytype, line_info: LineInfo) !void {
1504 }1504 }
1505}1505}
15061506
1507test "printLineFromFileAnyOs" {1507test printLineFromFileAnyOs {
1508 var output = std.ArrayList(u8).init(std.testing.allocator);1508 var output = std.ArrayList(u8).init(std.testing.allocator);
1509 defer output.deinit();1509 defer output.deinit();
1510 const output_stream = output.writer();1510 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
...@@ -1303,7 +1303,7 @@ pub fn fmtDuration(ns: u64) Formatter(formatDuration) {...@@ -1303,7 +1303,7 @@ pub fn fmtDuration(ns: u64) Formatter(formatDuration) {
1303 return .{ .data = data };1303 return .{ .data = data };
1304}1304}
13051305
1306test "fmtDuration" {1306test fmtDuration {
1307 var buf: [24]u8 = undefined;1307 var buf: [24]u8 = undefined;
1308 inline for (.{1308 inline for (.{
1309 .{ .s = "0ns", .d = 0 },1309 .{ .s = "0ns", .d = 0 },
...@@ -1367,7 +1367,7 @@ pub fn fmtDurationSigned(ns: i64) Formatter(formatDurationSigned) {...@@ -1367,7 +1367,7 @@ pub fn fmtDurationSigned(ns: i64) Formatter(formatDurationSigned) {
1367 return .{ .data = ns };1367 return .{ .data = ns };
1368}1368}
13691369
1370test "fmtDurationSigned" {1370test fmtDurationSigned {
1371 var buf: [24]u8 = undefined;1371 var buf: [24]u8 = undefined;
1372 inline for (.{1372 inline for (.{
1373 .{ .s = "0ns", .d = 0 },1373 .{ .s = "0ns", .d = 0 },
...@@ -1497,7 +1497,7 @@ pub fn parseInt(comptime T: type, buf: []const u8, base: u8) ParseIntError!T {...@@ -1497,7 +1497,7 @@ pub fn parseInt(comptime T: type, buf: []const u8, base: u8) ParseIntError!T {
1497 return parseWithSign(T, buf, base, .pos);1497 return parseWithSign(T, buf, base, .pos);
1498}1498}
14991499
1500test "parseInt" {1500test parseInt {
1501 try std.testing.expect((try parseInt(i32, "-10", 10)) == -10);1501 try std.testing.expect((try parseInt(i32, "-10", 10)) == -10);
1502 try std.testing.expect((try parseInt(i32, "+10", 10)) == 10);1502 try std.testing.expect((try parseInt(i32, "+10", 10)) == 10);
1503 try std.testing.expect((try parseInt(u32, "+10", 10)) == 10);1503 try std.testing.expect((try parseInt(u32, "+10", 10)) == 10);
...@@ -1639,7 +1639,7 @@ pub fn parseUnsigned(comptime T: type, buf: []const u8, base: u8) ParseIntError!...@@ -1639,7 +1639,7 @@ pub fn parseUnsigned(comptime T: type, buf: []const u8, base: u8) ParseIntError!
1639 return parseWithSign(T, buf, base, .pos);1639 return parseWithSign(T, buf, base, .pos);
1640}1640}
16411641
1642test "parseUnsigned" {1642test parseUnsigned {
1643 try std.testing.expect((try parseUnsigned(u16, "050124", 10)) == 50124);1643 try std.testing.expect((try parseUnsigned(u16, "050124", 10)) == 50124);
1644 try std.testing.expect((try parseUnsigned(u16, "65535", 10)) == 65535);1644 try std.testing.expect((try parseUnsigned(u16, "65535", 10)) == 65535);
1645 try std.testing.expect((try parseUnsigned(u16, "65_535", 10)) == 65535);1645 try std.testing.expect((try parseUnsigned(u16, "65_535", 10)) == 65535);
...@@ -1713,7 +1713,7 @@ pub fn parseIntSizeSuffix(buf: []const u8, digit_base: u8) ParseIntError!usize {...@@ -1713,7 +1713,7 @@ pub fn parseIntSizeSuffix(buf: []const u8, digit_base: u8) ParseIntError!usize {
1713 return math.mul(usize, number, multiplier);1713 return math.mul(usize, number, multiplier);
1714}1714}
17151715
1716test "parseIntSizeSuffix" {1716test parseIntSizeSuffix {
1717 try std.testing.expect(try parseIntSizeSuffix("2", 10) == 2);1717 try std.testing.expect(try parseIntSizeSuffix("2", 10) == 2);
1718 try std.testing.expect(try parseIntSizeSuffix("2B", 10) == 2);1718 try std.testing.expect(try parseIntSizeSuffix("2B", 10) == 2);
1719 try std.testing.expect(try parseIntSizeSuffix("2kB", 10) == 2000);1719 try std.testing.expect(try parseIntSizeSuffix("2kB", 10) == 2000);
...@@ -1796,7 +1796,7 @@ pub fn allocPrintZ(allocator: mem.Allocator, comptime fmt: []const u8, args: any...@@ -1796,7 +1796,7 @@ pub fn allocPrintZ(allocator: mem.Allocator, comptime fmt: []const u8, args: any
1796 return result[0 .. result.len - 1 :0];1796 return result[0 .. result.len - 1 :0];
1797}1797}
17981798
1799test "bufPrintInt" {1799test bufPrintIntToSlice {
1800 var buffer: [100]u8 = undefined;1800 var buffer: [100]u8 = undefined;
1801 const buf = buffer[0..];1801 const buf = buffer[0..];
18021802
...@@ -1830,7 +1830,7 @@ pub inline fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [cou...@@ -1830,7 +1830,7 @@ pub inline fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [cou
1830 }1830 }
1831}1831}
18321832
1833test "comptimePrint" {1833test comptimePrint {
1834 @setEvalBranchQuota(2000);1834 @setEvalBranchQuota(2000);
1835 try std.testing.expectEqual(*const [3:0]u8, @TypeOf(comptimePrint("{}", .{100})));1835 try std.testing.expectEqual(*const [3:0]u8, @TypeOf(comptimePrint("{}", .{100})));
1836 try std.testing.expectEqualSlices(u8, "100", comptimePrint("{}", .{100}));1836 try std.testing.expectEqualSlices(u8, "100", comptimePrint("{}", .{100}));
...@@ -2445,14 +2445,14 @@ pub fn hexToBytes(out: []u8, input: []const u8) ![]u8 {...@@ -2445,14 +2445,14 @@ pub fn hexToBytes(out: []u8, input: []const u8) ![]u8 {
2445 return out[0 .. in_i / 2];2445 return out[0 .. in_i / 2];
2446}2446}
24472447
2448test "bytesToHex" {2448test bytesToHex {
2449 const input = "input slice";2449 const input = "input slice";
2450 const encoded = bytesToHex(input, .lower);2450 const encoded = bytesToHex(input, .lower);
2451 var decoded: [input.len]u8 = undefined;2451 var decoded: [input.len]u8 = undefined;
2452 try std.testing.expectEqualSlices(u8, input, try hexToBytes(&decoded, &encoded));2452 try std.testing.expectEqualSlices(u8, input, try hexToBytes(&decoded, &encoded));
2453}2453}
24542454
2455test "hexToBytes" {2455test hexToBytes {
2456 var buf: [32]u8 = undefined;2456 var buf: [32]u8 = undefined;
2457 try expectFmt("90" ** 32, "{s}", .{fmtSliceHexUpper(try hexToBytes(&buf, "90" ** 32))});2457 try expectFmt("90" ** 32, "{s}", .{fmtSliceHexUpper(try hexToBytes(&buf, "90" ** 32))});
2458 try expectFmt("ABCD", "{s}", .{fmtSliceHexUpper(try hexToBytes(&buf, "ABCD"))});2458 try expectFmt("ABCD", "{s}", .{fmtSliceHexUpper(try hexToBytes(&buf, "ABCD"))});
lib/std/fs/get_app_data_dir.zig+1-1
...@@ -61,7 +61,7 @@ pub fn getAppDataDir(allocator: mem.Allocator, appname: []const u8) GetAppDataDi...@@ -61,7 +61,7 @@ pub fn getAppDataDir(allocator: mem.Allocator, appname: []const u8) GetAppDataDi
61 }61 }
62}62}
6363
64test "getAppDataDir" {64test getAppDataDir {
65 if (native_os == .wasi) return error.SkipZigTest;65 if (native_os == .wasi) return error.SkipZigTest;
6666
67 // We can't actually validate the result67 // 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
...@@ -421,7 +421,7 @@ fn dummyWrite(context: void, data: []const u8) error{}!usize {...@@ -421,7 +421,7 @@ fn dummyWrite(context: void, data: []const u8) error{}!usize {
421 return data.len;421 return data.len;
422}422}
423423
424test "null_writer" {424test null_writer {
425 null_writer.writeAll("yay" ** 10) catch |err| switch (err) {};425 null_writer.writeAll("yay" ** 10) catch |err| switch (err) {};
426}426}
427427
lib/std/math.zig+90-70
...@@ -167,28 +167,51 @@ pub fn approxEqRel(comptime T: type, x: T, y: T, tolerance: T) bool {...@@ -167,28 +167,51 @@ pub fn approxEqRel(comptime T: type, x: T, y: T, tolerance: T) bool {
167 return @abs(x - y) <= @max(@abs(x), @abs(y)) * tolerance;167 return @abs(x - y) <= @max(@abs(x), @abs(y)) * tolerance;
168}168}
169169
170test "approxEqAbs and approxEqRel" {170test approxEqAbs {
171 inline for ([_]type{ f16, f32, f64, f128 }) |T| {171 inline for ([_]type{ f16, f32, f64, f128 }) |T| {
172 const eps_value = comptime floatEps(T);172 const eps_value = comptime floatEps(T);
173 const sqrt_eps_value = comptime sqrt(eps_value);
174 const nan_value = comptime nan(T);
175 const inf_value = comptime inf(T);
176 const min_value = comptime floatMin(T);173 const min_value = comptime floatMin(T);
177174
178 try testing.expect(approxEqAbs(T, 0.0, 0.0, eps_value));175 try testing.expect(approxEqAbs(T, 0.0, 0.0, eps_value));
179 try testing.expect(approxEqAbs(T, -0.0, -0.0, eps_value));176 try testing.expect(approxEqAbs(T, -0.0, -0.0, eps_value));
180 try testing.expect(approxEqAbs(T, 0.0, -0.0, eps_value));177 try testing.expect(approxEqAbs(T, 0.0, -0.0, eps_value));
181 try testing.expect(approxEqRel(T, 1.0, 1.0, sqrt_eps_value));
182 try testing.expect(!approxEqRel(T, 1.0, 0.0, sqrt_eps_value));
183 try testing.expect(!approxEqAbs(T, 1.0 + 2 * eps_value, 1.0, eps_value));178 try testing.expect(!approxEqAbs(T, 1.0 + 2 * eps_value, 1.0, eps_value));
184 try testing.expect(approxEqAbs(T, 1.0 + 1 * eps_value, 1.0, eps_value));179 try testing.expect(approxEqAbs(T, 1.0 + 1 * eps_value, 1.0, eps_value));
180 try testing.expect(approxEqAbs(T, min_value, 0.0, eps_value * 2));
181 try testing.expect(approxEqAbs(T, -min_value, 0.0, eps_value * 2));
182 }
183
184 comptime {
185 // `comptime_float` is guaranteed to have the same precision and operations of
186 // the largest other floating point type, which is f128 but it doesn't have a
187 // defined layout so we can't rely on `@bitCast` to construct the smallest
188 // possible epsilon value like we do in the tests above. In the same vein, we
189 // also can't represent a max/min, `NaN` or `Inf` values.
190 const eps_value = 1e-4;
191
192 try testing.expect(approxEqAbs(comptime_float, 0.0, 0.0, eps_value));
193 try testing.expect(approxEqAbs(comptime_float, -0.0, -0.0, eps_value));
194 try testing.expect(approxEqAbs(comptime_float, 0.0, -0.0, eps_value));
195 try testing.expect(!approxEqAbs(comptime_float, 1.0 + 2 * eps_value, 1.0, eps_value));
196 try testing.expect(approxEqAbs(comptime_float, 1.0 + 1 * eps_value, 1.0, eps_value));
197 }
198}
199
200test approxEqRel {
201 inline for ([_]type{ f16, f32, f64, f128 }) |T| {
202 const eps_value = comptime floatEps(T);
203 const sqrt_eps_value = comptime sqrt(eps_value);
204 const nan_value = comptime nan(T);
205 const inf_value = comptime inf(T);
206 const min_value = comptime floatMin(T);
207
208 try testing.expect(approxEqRel(T, 1.0, 1.0, sqrt_eps_value));
209 try testing.expect(!approxEqRel(T, 1.0, 0.0, sqrt_eps_value));
185 try testing.expect(!approxEqRel(T, 1.0, nan_value, sqrt_eps_value));210 try testing.expect(!approxEqRel(T, 1.0, nan_value, sqrt_eps_value));
186 try testing.expect(!approxEqRel(T, nan_value, nan_value, sqrt_eps_value));211 try testing.expect(!approxEqRel(T, nan_value, nan_value, sqrt_eps_value));
187 try testing.expect(approxEqRel(T, inf_value, inf_value, sqrt_eps_value));212 try testing.expect(approxEqRel(T, inf_value, inf_value, sqrt_eps_value));
188 try testing.expect(approxEqRel(T, min_value, min_value, sqrt_eps_value));213 try testing.expect(approxEqRel(T, min_value, min_value, sqrt_eps_value));
189 try testing.expect(approxEqRel(T, -min_value, -min_value, sqrt_eps_value));214 try testing.expect(approxEqRel(T, -min_value, -min_value, sqrt_eps_value));
190 try testing.expect(approxEqAbs(T, min_value, 0.0, eps_value * 2));
191 try testing.expect(approxEqAbs(T, -min_value, 0.0, eps_value * 2));
192 }215 }
193216
194 comptime {217 comptime {
...@@ -200,13 +223,8 @@ test "approxEqAbs and approxEqRel" {...@@ -200,13 +223,8 @@ test "approxEqAbs and approxEqRel" {
200 const eps_value = 1e-4;223 const eps_value = 1e-4;
201 const sqrt_eps_value = sqrt(eps_value);224 const sqrt_eps_value = sqrt(eps_value);
202225
203 try testing.expect(approxEqAbs(comptime_float, 0.0, 0.0, eps_value));
204 try testing.expect(approxEqAbs(comptime_float, -0.0, -0.0, eps_value));
205 try testing.expect(approxEqAbs(comptime_float, 0.0, -0.0, eps_value));
206 try testing.expect(approxEqRel(comptime_float, 1.0, 1.0, sqrt_eps_value));226 try testing.expect(approxEqRel(comptime_float, 1.0, 1.0, sqrt_eps_value));
207 try testing.expect(!approxEqRel(comptime_float, 1.0, 0.0, sqrt_eps_value));227 try testing.expect(!approxEqRel(comptime_float, 1.0, 0.0, sqrt_eps_value));
208 try testing.expect(!approxEqAbs(comptime_float, 1.0 + 2 * eps_value, 1.0, eps_value));
209 try testing.expect(approxEqAbs(comptime_float, 1.0 + 1 * eps_value, 1.0, eps_value));
210 }228 }
211}229}
212230
...@@ -310,7 +328,7 @@ pub fn radiansToDegrees(ang: anytype) if (@TypeOf(ang) == comptime_int) comptime...@@ -310,7 +328,7 @@ pub fn radiansToDegrees(ang: anytype) if (@TypeOf(ang) == comptime_int) comptime
310 @compileError("Input must be float or a comptime number, or a vector of floats.");328 @compileError("Input must be float or a comptime number, or a vector of floats.");
311}329}
312330
313test "radiansToDegrees" {331test radiansToDegrees {
314 const zero: f32 = 0;332 const zero: f32 = 0;
315 const half_pi: f32 = pi / 2.0;333 const half_pi: f32 = pi / 2.0;
316 const neg_quart_pi: f32 = -pi / 4.0;334 const neg_quart_pi: f32 = -pi / 4.0;
...@@ -345,7 +363,7 @@ pub fn degreesToRadians(ang: anytype) if (@TypeOf(ang) == comptime_int) comptime...@@ -345,7 +363,7 @@ pub fn degreesToRadians(ang: anytype) if (@TypeOf(ang) == comptime_int) comptime
345 @compileError("Input must be float or a comptime number, or a vector of floats.");363 @compileError("Input must be float or a comptime number, or a vector of floats.");
346}364}
347365
348test "degreesToRadians" {366test degreesToRadians {
349 const ninety: f32 = 90;367 const ninety: f32 = 90;
350 const neg_two_seventy: f32 = -270;368 const neg_two_seventy: f32 = -270;
351 const three_sixty: f32 = 360;369 const three_sixty: f32 = 360;
...@@ -506,7 +524,7 @@ pub fn wrap(x: anytype, r: anytype) @TypeOf(x) {...@@ -506,7 +524,7 @@ pub fn wrap(x: anytype, r: anytype) @TypeOf(x) {
506 },524 },
507 }525 }
508}526}
509test "wrap" {527test wrap {
510 // Within range528 // Within range
511 try testing.expect(wrap(@as(i32, -75), @as(i32, 180)) == -75);529 try testing.expect(wrap(@as(i32, -75), @as(i32, 180)) == -75);
512 try testing.expect(wrap(@as(i32, -75), @as(i32, -180)) == -75);530 try testing.expect(wrap(@as(i32, -75), @as(i32, -180)) == -75);
...@@ -543,8 +561,7 @@ test "wrap" {...@@ -543,8 +561,7 @@ test "wrap" {
543 var i: i32 = 1;561 var i: i32 = 1;
544 _ = &i;562 _ = &i;
545 try testing.expect(wrap(i, 10) == 1);563 try testing.expect(wrap(i, 10) == 1);
546}564
547test wrap {
548 const limit: i32 = 180;565 const limit: i32 = 180;
549 // Within range566 // Within range
550 try testing.expect(wrap(@as(i32, -75), limit) == -75);567 try testing.expect(wrap(@as(i32, -75), limit) == -75);
...@@ -569,7 +586,7 @@ pub fn clamp(val: anytype, lower: anytype, upper: anytype) @TypeOf(val, lower, u...@@ -569,7 +586,7 @@ pub fn clamp(val: anytype, lower: anytype, upper: anytype) @TypeOf(val, lower, u
569 assert(lower <= upper);586 assert(lower <= upper);
570 return @max(lower, @min(val, upper));587 return @max(lower, @min(val, upper));
571}588}
572test "clamp" {589test clamp {
573 // Within range590 // Within range
574 try testing.expect(std.math.clamp(@as(i32, -1), @as(i32, -4), @as(i32, 7)) == -1);591 try testing.expect(std.math.clamp(@as(i32, -1), @as(i32, -4), @as(i32, 7)) == -1);
575 // Below592 // Below
...@@ -650,7 +667,7 @@ pub fn shl(comptime T: type, a: T, shift_amt: anytype) T {...@@ -650,7 +667,7 @@ pub fn shl(comptime T: type, a: T, shift_amt: anytype) T {
650 return a << casted_shift_amt;667 return a << casted_shift_amt;
651}668}
652669
653test "shl" {670test shl {
654 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) {671 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) {
655 // https://github.com/ziglang/zig/issues/12012672 // https://github.com/ziglang/zig/issues/12012
656 return error.SkipZigTest;673 return error.SkipZigTest;
...@@ -695,7 +712,7 @@ pub fn shr(comptime T: type, a: T, shift_amt: anytype) T {...@@ -695,7 +712,7 @@ pub fn shr(comptime T: type, a: T, shift_amt: anytype) T {
695 return a >> casted_shift_amt;712 return a >> casted_shift_amt;
696}713}
697714
698test "shr" {715test shr {
699 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) {716 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) {
700 // https://github.com/ziglang/zig/issues/12012717 // https://github.com/ziglang/zig/issues/12012
701 return error.SkipZigTest;718 return error.SkipZigTest;
...@@ -741,7 +758,7 @@ pub fn rotr(comptime T: type, x: T, r: anytype) T {...@@ -741,7 +758,7 @@ pub fn rotr(comptime T: type, x: T, r: anytype) T {
741 }758 }
742}759}
743760
744test "rotr" {761test rotr {
745 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) {762 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) {
746 // https://github.com/ziglang/zig/issues/12012763 // https://github.com/ziglang/zig/issues/12012
747 return error.SkipZigTest;764 return error.SkipZigTest;
...@@ -787,7 +804,7 @@ pub fn rotl(comptime T: type, x: T, r: anytype) T {...@@ -787,7 +804,7 @@ pub fn rotl(comptime T: type, x: T, r: anytype) T {
787 }804 }
788}805}
789806
790test "rotl" {807test rotl {
791 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) {808 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) {
792 // https://github.com/ziglang/zig/issues/12012809 // https://github.com/ziglang/zig/issues/12012
793 return error.SkipZigTest;810 return error.SkipZigTest;
...@@ -850,7 +867,7 @@ pub fn IntFittingRange(comptime from: comptime_int, comptime to: comptime_int) t...@@ -850,7 +867,7 @@ pub fn IntFittingRange(comptime from: comptime_int, comptime to: comptime_int) t
850 return std.meta.Int(signedness, magnitude_bits);867 return std.meta.Int(signedness, magnitude_bits);
851}868}
852869
853test "IntFittingRange" {870test IntFittingRange {
854 try testing.expect(IntFittingRange(0, 0) == u0);871 try testing.expect(IntFittingRange(0, 0) == u0);
855 try testing.expect(IntFittingRange(0, 1) == u1);872 try testing.expect(IntFittingRange(0, 1) == u1);
856 try testing.expect(IntFittingRange(0, 2) == u2);873 try testing.expect(IntFittingRange(0, 2) == u2);
...@@ -918,7 +935,7 @@ pub fn divTrunc(comptime T: type, numerator: T, denominator: T) !T {...@@ -918,7 +935,7 @@ pub fn divTrunc(comptime T: type, numerator: T, denominator: T) !T {
918 return @divTrunc(numerator, denominator);935 return @divTrunc(numerator, denominator);
919}936}
920937
921test "divTrunc" {938test divTrunc {
922 try testDivTrunc();939 try testDivTrunc();
923 try comptime testDivTrunc();940 try comptime testDivTrunc();
924}941}
...@@ -942,7 +959,7 @@ pub fn divFloor(comptime T: type, numerator: T, denominator: T) !T {...@@ -942,7 +959,7 @@ pub fn divFloor(comptime T: type, numerator: T, denominator: T) !T {
942 return @divFloor(numerator, denominator);959 return @divFloor(numerator, denominator);
943}960}
944961
945test "divFloor" {962test divFloor {
946 try testDivFloor();963 try testDivFloor();
947 try comptime testDivFloor();964 try comptime testDivFloor();
948}965}
...@@ -979,7 +996,7 @@ pub fn divCeil(comptime T: type, numerator: T, denominator: T) !T {...@@ -979,7 +996,7 @@ pub fn divCeil(comptime T: type, numerator: T, denominator: T) !T {
979 }996 }
980}997}
981998
982test "divCeil" {999test divCeil {
983 try testDivCeil();1000 try testDivCeil();
984 try comptime testDivCeil();1001 try comptime testDivCeil();
985}1002}
...@@ -1023,7 +1040,7 @@ pub fn divExact(comptime T: type, numerator: T, denominator: T) !T {...@@ -1023,7 +1040,7 @@ pub fn divExact(comptime T: type, numerator: T, denominator: T) !T {
1023 return result;1040 return result;
1024}1041}
10251042
1026test "divExact" {1043test divExact {
1027 try testDivExact();1044 try testDivExact();
1028 try comptime testDivExact();1045 try comptime testDivExact();
1029}1046}
...@@ -1049,7 +1066,7 @@ pub fn mod(comptime T: type, numerator: T, denominator: T) !T {...@@ -1049,7 +1066,7 @@ pub fn mod(comptime T: type, numerator: T, denominator: T) !T {
1049 return @mod(numerator, denominator);1066 return @mod(numerator, denominator);
1050}1067}
10511068
1052test "mod" {1069test mod {
1053 try testMod();1070 try testMod();
1054 try comptime testMod();1071 try comptime testMod();
1055}1072}
...@@ -1075,7 +1092,7 @@ pub fn rem(comptime T: type, numerator: T, denominator: T) !T {...@@ -1075,7 +1092,7 @@ pub fn rem(comptime T: type, numerator: T, denominator: T) !T {
1075 return @rem(numerator, denominator);1092 return @rem(numerator, denominator);
1076}1093}
10771094
1078test "rem" {1095test rem {
1079 try testRem();1096 try testRem();
1080 try comptime testRem();1097 try comptime testRem();
1081}1098}
...@@ -1104,7 +1121,7 @@ pub fn negateCast(x: anytype) !std.meta.Int(.signed, @bitSizeOf(@TypeOf(x))) {...@@ -1104,7 +1121,7 @@ pub fn negateCast(x: anytype) !std.meta.Int(.signed, @bitSizeOf(@TypeOf(x))) {
1104 return -@as(int, @intCast(x));1121 return -@as(int, @intCast(x));
1105}1122}
11061123
1107test "negateCast" {1124test negateCast {
1108 try testing.expect((negateCast(@as(u32, 999)) catch unreachable) == -999);1125 try testing.expect((negateCast(@as(u32, 999)) catch unreachable) == -999);
1109 try testing.expect(@TypeOf(negateCast(@as(u32, 999)) catch unreachable) == i32);1126 try testing.expect(@TypeOf(negateCast(@as(u32, 999)) catch unreachable) == i32);
11101127
...@@ -1129,7 +1146,7 @@ pub fn cast(comptime T: type, x: anytype) ?T {...@@ -1129,7 +1146,7 @@ pub fn cast(comptime T: type, x: anytype) ?T {
1129 }1146 }
1130}1147}
11311148
1132test "cast" {1149test cast {
1133 try testing.expect(cast(u8, 300) == null);1150 try testing.expect(cast(u8, 300) == null);
1134 try testing.expect(cast(u8, @as(u32, 300)) == null);1151 try testing.expect(cast(u8, @as(u32, 300)) == null);
1135 try testing.expect(cast(i8, -200) == null);1152 try testing.expect(cast(i8, -200) == null);
...@@ -1188,7 +1205,7 @@ pub fn ByteAlignedInt(comptime T: type) type {...@@ -1188,7 +1205,7 @@ pub fn ByteAlignedInt(comptime T: type) type {
1188 return extended_type;1205 return extended_type;
1189}1206}
11901207
1191test "ByteAlignedInt" {1208test ByteAlignedInt {
1192 try testing.expect(ByteAlignedInt(u0) == u0);1209 try testing.expect(ByteAlignedInt(u0) == u0);
1193 try testing.expect(ByteAlignedInt(i0) == i0);1210 try testing.expect(ByteAlignedInt(i0) == i0);
1194 try testing.expect(ByteAlignedInt(u3) == u8);1211 try testing.expect(ByteAlignedInt(u3) == u8);
...@@ -1226,7 +1243,7 @@ pub fn floorPowerOfTwo(comptime T: type, value: T) T {...@@ -1226,7 +1243,7 @@ pub fn floorPowerOfTwo(comptime T: type, value: T) T {
1226 return @as(T, 1) << log2_int(uT, @as(uT, @intCast(value)));1243 return @as(T, 1) << log2_int(uT, @as(uT, @intCast(value)));
1227}1244}
12281245
1229test "floorPowerOfTwo" {1246test floorPowerOfTwo {
1230 try testFloorPowerOfTwo();1247 try testFloorPowerOfTwo();
1231 try comptime testFloorPowerOfTwo();1248 try comptime testFloorPowerOfTwo();
1232}1249}
...@@ -1288,7 +1305,7 @@ pub fn ceilPowerOfTwoAssert(comptime T: type, value: T) T {...@@ -1288,7 +1305,7 @@ pub fn ceilPowerOfTwoAssert(comptime T: type, value: T) T {
1288 return ceilPowerOfTwo(T, value) catch unreachable;1305 return ceilPowerOfTwo(T, value) catch unreachable;
1289}1306}
12901307
1291test "ceilPowerOfTwoPromote" {1308test ceilPowerOfTwoPromote {
1292 try testCeilPowerOfTwoPromote();1309 try testCeilPowerOfTwoPromote();
1293 try comptime testCeilPowerOfTwoPromote();1310 try comptime testCeilPowerOfTwoPromote();
1294}1311}
...@@ -1305,7 +1322,7 @@ fn testCeilPowerOfTwoPromote() !void {...@@ -1305,7 +1322,7 @@ fn testCeilPowerOfTwoPromote() !void {
1305 try testing.expectEqual(@as(u5, 16), ceilPowerOfTwoPromote(u4, 9));1322 try testing.expectEqual(@as(u5, 16), ceilPowerOfTwoPromote(u4, 9));
1306}1323}
13071324
1308test "ceilPowerOfTwo" {1325test ceilPowerOfTwo {
1309 try testCeilPowerOfTwo();1326 try testCeilPowerOfTwo();
1310 try comptime testCeilPowerOfTwo();1327 try comptime testCeilPowerOfTwo();
1311}1328}
...@@ -1398,7 +1415,7 @@ pub fn lossyCast(comptime T: type, value: anytype) T {...@@ -1398,7 +1415,7 @@ pub fn lossyCast(comptime T: type, value: anytype) T {
1398 }1415 }
1399}1416}
14001417
1401test "lossyCast" {1418test lossyCast {
1402 try testing.expect(lossyCast(i16, 70000.0) == @as(i16, 32767));1419 try testing.expect(lossyCast(i16, 70000.0) == @as(i16, 32767));
1403 try testing.expect(lossyCast(u32, @as(i16, -255)) == @as(u32, 0));1420 try testing.expect(lossyCast(u32, @as(i16, -255)) == @as(u32, 0));
1404 try testing.expect(lossyCast(i9, @as(u32, 200)) == @as(i9, 200));1421 try testing.expect(lossyCast(i9, @as(u32, 200)) == @as(i9, 200));
...@@ -1417,7 +1434,7 @@ pub fn lerp(a: anytype, b: anytype, t: anytype) @TypeOf(a, b, t) {...@@ -1417,7 +1434,7 @@ pub fn lerp(a: anytype, b: anytype, t: anytype) @TypeOf(a, b, t) {
1417 return @mulAdd(Type, b - a, t, a);1434 return @mulAdd(Type, b - a, t, a);
1418}1435}
14191436
1420test "lerp" {1437test lerp {
1421 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // https://github.com/ziglang/zig/issues/178841438 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // https://github.com/ziglang/zig/issues/17884
1422 if (builtin.zig_backend == .stage2_x86_64 and1439 if (builtin.zig_backend == .stage2_x86_64 and
1423 !comptime std.Target.x86.featureSetHas(builtin.cpu.features, .fma)) return error.SkipZigTest;1440 !comptime std.Target.x86.featureSetHas(builtin.cpu.features, .fma)) return error.SkipZigTest;
...@@ -1483,7 +1500,7 @@ pub fn minInt(comptime T: type) comptime_int {...@@ -1483,7 +1500,7 @@ pub fn minInt(comptime T: type) comptime_int {
1483 return -(1 << (bit_count - 1));1500 return -(1 << (bit_count - 1));
1484}1501}
14851502
1486test "minInt and maxInt" {1503test maxInt {
1487 try testing.expect(maxInt(u0) == 0);1504 try testing.expect(maxInt(u0) == 0);
1488 try testing.expect(maxInt(u1) == 1);1505 try testing.expect(maxInt(u1) == 1);
1489 try testing.expect(maxInt(u8) == 255);1506 try testing.expect(maxInt(u8) == 255);
...@@ -1500,7 +1517,9 @@ test "minInt and maxInt" {...@@ -1500,7 +1517,9 @@ test "minInt and maxInt" {
1500 try testing.expect(maxInt(i63) == 4611686018427387903);1517 try testing.expect(maxInt(i63) == 4611686018427387903);
1501 try testing.expect(maxInt(i64) == 9223372036854775807);1518 try testing.expect(maxInt(i64) == 9223372036854775807);
1502 try testing.expect(maxInt(i128) == 170141183460469231731687303715884105727);1519 try testing.expect(maxInt(i128) == 170141183460469231731687303715884105727);
1520}
15031521
1522test minInt {
1504 try testing.expect(minInt(u0) == 0);1523 try testing.expect(minInt(u0) == 0);
1505 try testing.expect(minInt(u1) == 0);1524 try testing.expect(minInt(u1) == 0);
1506 try testing.expect(minInt(u8) == 0);1525 try testing.expect(minInt(u8) == 0);
...@@ -1538,7 +1557,7 @@ pub fn mulWide(comptime T: type, a: T, b: T) std.meta.Int(...@@ -1538,7 +1557,7 @@ pub fn mulWide(comptime T: type, a: T, b: T) std.meta.Int(
1538 return @as(ResultInt, a) * @as(ResultInt, b);1557 return @as(ResultInt, a) * @as(ResultInt, b);
1539}1558}
15401559
1541test "mulWide" {1560test mulWide {
1542 try testing.expect(mulWide(u8, 5, 5) == 25);1561 try testing.expect(mulWide(u8, 5, 5) == 25);
1543 try testing.expect(mulWide(i8, 5, -5) == -25);1562 try testing.expect(mulWide(i8, 5, -5) == -25);
1544 try testing.expect(mulWide(u8, 100, 100) == 10000);1563 try testing.expect(mulWide(u8, 100, 100) == 10000);
...@@ -1563,6 +1582,12 @@ pub const Order = enum {...@@ -1563,6 +1582,12 @@ pub const Order = enum {
1563 };1582 };
1564 }1583 }
15651584
1585 test invert {
1586 try testing.expect(Order.invert(order(0, 0)) == .eq);
1587 try testing.expect(Order.invert(order(1, 0)) == .lt);
1588 try testing.expect(Order.invert(order(-1, 0)) == .gt);
1589 }
1590
1566 pub fn compare(self: Order, op: CompareOperator) bool {1591 pub fn compare(self: Order, op: CompareOperator) bool {
1567 return switch (self) {1592 return switch (self) {
1568 .lt => switch (op) {1593 .lt => switch (op) {
...@@ -1591,6 +1616,18 @@ pub const Order = enum {...@@ -1591,6 +1616,18 @@ pub const Order = enum {
1591 },1616 },
1592 };1617 };
1593 }1618 }
1619
1620 // https://github.com/ziglang/zig/issues/19295
1621 test "compare" {
1622 try testing.expect(order(-1, 0).compare(.lt));
1623 try testing.expect(order(-1, 0).compare(.lte));
1624 try testing.expect(order(0, 0).compare(.lte));
1625 try testing.expect(order(0, 0).compare(.eq));
1626 try testing.expect(order(0, 0).compare(.gte));
1627 try testing.expect(order(1, 0).compare(.gte));
1628 try testing.expect(order(1, 0).compare(.gt));
1629 try testing.expect(order(1, 0).compare(.neq));
1630 }
1594};1631};
15951632
1596/// Given two numbers, this function returns the order they are with respect to each other.1633/// Given two numbers, this function returns the order they are with respect to each other.
...@@ -1633,6 +1670,15 @@ pub const CompareOperator = enum {...@@ -1633,6 +1670,15 @@ pub const CompareOperator = enum {
1633 .neq => .neq,1670 .neq => .neq,
1634 };1671 };
1635 }1672 }
1673
1674 test reverse {
1675 inline for (@typeInfo(CompareOperator).Enum.fields) |op_field| {
1676 const op = @as(CompareOperator, @enumFromInt(op_field.value));
1677 try testing.expect(compare(2, op, 3) == compare(3, op.reverse(), 2));
1678 try testing.expect(compare(3, op, 3) == compare(3, op.reverse(), 3));
1679 try testing.expect(compare(4, op, 3) == compare(3, op.reverse(), 4));
1680 }
1681 }
1636};1682};
16371683
1638/// This function does the same thing as comparison operators, however the1684/// This function does the same thing as comparison operators, however the
...@@ -1649,7 +1695,7 @@ pub fn compare(a: anytype, op: CompareOperator, b: anytype) bool {...@@ -1649,7 +1695,7 @@ pub fn compare(a: anytype, op: CompareOperator, b: anytype) bool {
1649 };1695 };
1650}1696}
16511697
1652test "compare between signed and unsigned" {1698test compare {
1653 try testing.expect(compare(@as(i8, -1), .lt, @as(u8, 255)));1699 try testing.expect(compare(@as(i8, -1), .lt, @as(u8, 255)));
1654 try testing.expect(compare(@as(i8, 2), .gt, @as(u8, 1)));1700 try testing.expect(compare(@as(i8, 2), .gt, @as(u8, 1)));
1655 try testing.expect(!compare(@as(i8, -1), .gte, @as(u8, 255)));1701 try testing.expect(!compare(@as(i8, -1), .gte, @as(u8, 255)));
...@@ -1669,38 +1715,12 @@ test "compare between signed and unsigned" {...@@ -1669,38 +1715,12 @@ test "compare between signed and unsigned" {
1669 try testing.expect(compare(@as(u8, 1), .eq, @as(u8, 1)));1715 try testing.expect(compare(@as(u8, 1), .eq, @as(u8, 1)));
1670}1716}
16711717
1672test "order" {1718test order {
1673 try testing.expect(order(0, 0) == .eq);1719 try testing.expect(order(0, 0) == .eq);
1674 try testing.expect(order(1, 0) == .gt);1720 try testing.expect(order(1, 0) == .gt);
1675 try testing.expect(order(-1, 0) == .lt);1721 try testing.expect(order(-1, 0) == .lt);
1676}1722}
16771723
1678test "order.invert" {
1679 try testing.expect(Order.invert(order(0, 0)) == .eq);
1680 try testing.expect(Order.invert(order(1, 0)) == .lt);
1681 try testing.expect(Order.invert(order(-1, 0)) == .gt);
1682}
1683
1684test "order.compare" {
1685 try testing.expect(order(-1, 0).compare(.lt));
1686 try testing.expect(order(-1, 0).compare(.lte));
1687 try testing.expect(order(0, 0).compare(.lte));
1688 try testing.expect(order(0, 0).compare(.eq));
1689 try testing.expect(order(0, 0).compare(.gte));
1690 try testing.expect(order(1, 0).compare(.gte));
1691 try testing.expect(order(1, 0).compare(.gt));
1692 try testing.expect(order(1, 0).compare(.neq));
1693}
1694
1695test "compare.reverse" {
1696 inline for (@typeInfo(CompareOperator).Enum.fields) |op_field| {
1697 const op = @as(CompareOperator, @enumFromInt(op_field.value));
1698 try testing.expect(compare(2, op, 3) == compare(3, op.reverse(), 2));
1699 try testing.expect(compare(3, op, 3) == compare(3, op.reverse(), 3));
1700 try testing.expect(compare(4, op, 3) == compare(3, op.reverse(), 4));
1701 }
1702}
1703
1704/// Returns a mask of all ones if value is true,1724/// Returns a mask of all ones if value is true,
1705/// and a mask of all zeroes if value is false.1725/// and a mask of all zeroes if value is false.
1706/// Compiles to one instruction for register sized integers.1726/// Compiles to one instruction for register sized integers.
...@@ -1722,7 +1742,7 @@ pub inline fn boolMask(comptime MaskInt: type, value: bool) MaskInt {...@@ -1722,7 +1742,7 @@ pub inline fn boolMask(comptime MaskInt: type, value: bool) MaskInt {
1722 return -%@as(MaskInt, @intCast(@intFromBool(value)));1742 return -%@as(MaskInt, @intCast(@intFromBool(value)));
1723}1743}
17241744
1725test "boolMask" {1745test boolMask {
1726 const runTest = struct {1746 const runTest = struct {
1727 fn runTest() !void {1747 fn runTest() !void {
1728 try testing.expectEqual(@as(u1, 0), boolMask(u1, false));1748 try testing.expectEqual(@as(u1, 0), boolMask(u1, false));
...@@ -1870,7 +1890,7 @@ fn testSign() !void {...@@ -1870,7 +1890,7 @@ fn testSign() !void {
1870 try std.testing.expectEqual(0.0, sign(0.0));1890 try std.testing.expectEqual(0.0, sign(0.0));
1871}1891}
18721892
1873test "sign" {1893test sign {
1874 if (builtin.zig_backend == .stage2_llvm) {1894 if (builtin.zig_backend == .stage2_llvm) {
1875 // https://github.com/ziglang/zig/issues/120121895 // https://github.com/ziglang/zig/issues/12012
1876 return error.SkipZigTest;1896 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);
...@@ -3406,7 +3408,7 @@ pub fn indexOfMinMax(comptime T: type, slice: []const T) struct { usize, usize }...@@ -3406,7 +3408,7 @@ pub fn indexOfMinMax(comptime T: type, slice: []const T) struct { usize, usize }
3406 return .{ minIdx, maxIdx };3408 return .{ minIdx, maxIdx };
3407}3409}
34083410
3409test "indexOfMinMax" {3411test indexOfMinMax {
3410 try testing.expectEqual(.{ 0, 6 }, indexOfMinMax(u8, "abcdefg"));3412 try testing.expectEqual(.{ 0, 6 }, indexOfMinMax(u8, "abcdefg"));
3411 try testing.expectEqual(.{ 1, 0 }, indexOfMinMax(u8, "gabcdef"));3413 try testing.expectEqual(.{ 1, 0 }, indexOfMinMax(u8, "gabcdef"));
3412 try testing.expectEqual(.{ 0, 0 }, indexOfMinMax(u8, "a"));3414 try testing.expectEqual(.{ 0, 0 }, indexOfMinMax(u8, "a"));
...@@ -3427,7 +3429,7 @@ pub fn reverse(comptime T: type, items: []T) void {...@@ -3427,7 +3429,7 @@ pub fn reverse(comptime T: type, items: []T) void {
3427 }3429 }
3428}3430}
34293431
3430test "reverse" {3432test reverse {
3431 var arr = [_]i32{ 5, 3, 1, 2, 4 };3433 var arr = [_]i32{ 5, 3, 1, 2, 4 };
3432 reverse(i32, arr[0..]);3434 reverse(i32, arr[0..]);
34333435
...@@ -3488,7 +3490,7 @@ pub fn reverseIterator(slice: anytype) ReverseIterator(@TypeOf(slice)) {...@@ -3488,7 +3490,7 @@ pub fn reverseIterator(slice: anytype) ReverseIterator(@TypeOf(slice)) {
3488 return .{ .ptr = slice.ptr, .index = slice.len };3490 return .{ .ptr = slice.ptr, .index = slice.len };
3489}3491}
34903492
3491test "reverseIterator" {3493test reverseIterator {
3492 {3494 {
3493 var it = reverseIterator("abc");3495 var it = reverseIterator("abc");
3494 try testing.expectEqual(@as(?u8, 'c'), it.next());3496 try testing.expectEqual(@as(?u8, 'c'), it.next());
...@@ -3546,7 +3548,7 @@ pub fn rotate(comptime T: type, items: []T, amount: usize) void {...@@ -3546,7 +3548,7 @@ pub fn rotate(comptime T: type, items: []T, amount: usize) void {
3546 reverse(T, items);3548 reverse(T, items);
3547}3549}
35483550
3549test "rotate" {3551test rotate {
3550 var arr = [_]i32{ 5, 3, 1, 2, 4 };3552 var arr = [_]i32{ 5, 3, 1, 2, 4 };
3551 rotate(i32, arr[0..], 2);3553 rotate(i32, arr[0..], 2);
35523554
...@@ -3580,7 +3582,7 @@ pub fn replace(comptime T: type, input: []const T, needle: []const T, replacemen...@@ -3580,7 +3582,7 @@ pub fn replace(comptime T: type, input: []const T, needle: []const T, replacemen
3580 return replacements;3582 return replacements;
3581}3583}
35823584
3583test "replace" {3585test replace {
3584 var output: [29]u8 = undefined;3586 var output: [29]u8 = undefined;
3585 var replacements = replace(u8, "All your base are belong to us", "base", "Zig", output[0..]);3587 var replacements = replace(u8, "All your base are belong to us", "base", "Zig", output[0..]);
3586 var expected: []const u8 = "All your Zig are belong to us";3588 var expected: []const u8 = "All your Zig are belong to us";
...@@ -3643,7 +3645,7 @@ fn testCollapseRepeats(str: []const u8, elem: u8, expected: []const u8) !void {...@@ -3643,7 +3645,7 @@ fn testCollapseRepeats(str: []const u8, elem: u8, expected: []const u8) !void {
3643 defer std.testing.allocator.free(mutable);3645 defer std.testing.allocator.free(mutable);
3644 try testing.expect(std.mem.eql(u8, collapseRepeats(u8, mutable, elem), expected));3646 try testing.expect(std.mem.eql(u8, collapseRepeats(u8, mutable, elem), expected));
3645}3647}
3646test "collapseRepeats" {3648test collapseRepeats {
3647 try testCollapseRepeats("", '/', "");3649 try testCollapseRepeats("", '/', "");
3648 try testCollapseRepeats("a", '/', "a");3650 try testCollapseRepeats("a", '/', "a");
3649 try testCollapseRepeats("/", '/', "/");3651 try testCollapseRepeats("/", '/', "/");
...@@ -3677,7 +3679,7 @@ pub fn replacementSize(comptime T: type, input: []const T, needle: []const T, re...@@ -3677,7 +3679,7 @@ pub fn replacementSize(comptime T: type, input: []const T, needle: []const T, re
3677 return size;3679 return size;
3678}3680}
36793681
3680test "replacementSize" {3682test replacementSize {
3681 try testing.expect(replacementSize(u8, "All your base are belong to us", "base", "Zig") == 29);3683 try testing.expect(replacementSize(u8, "All your base are belong to us", "base", "Zig") == 29);
3682 try testing.expect(replacementSize(u8, "Favor reading code over writing code.", "code", "") == 29);3684 try testing.expect(replacementSize(u8, "Favor reading code over writing code.", "code", "") == 29);
3683 try testing.expect(replacementSize(u8, "Only one obvious way to do things.", "things.", "things in Zig.") == 41);3685 try testing.expect(replacementSize(u8, "Only one obvious way to do things.", "things.", "things in Zig.") == 41);
...@@ -3697,7 +3699,7 @@ pub fn replaceOwned(comptime T: type, allocator: Allocator, input: []const T, ne...@@ -3697,7 +3699,7 @@ pub fn replaceOwned(comptime T: type, allocator: Allocator, input: []const T, ne
3697 return output;3699 return output;
3698}3700}
36993701
3700test "replaceOwned" {3702test replaceOwned {
3701 const gpa = std.testing.allocator;3703 const gpa = std.testing.allocator;
37023704
3703 const base_replace = replaceOwned(u8, gpa, "All your base are belong to us", "base", "Zig") catch @panic("out of memory");3705 const base_replace = replaceOwned(u8, gpa, "All your base are belong to us", "base", "Zig") catch @panic("out of memory");
...@@ -3801,7 +3803,7 @@ pub fn alignPointer(ptr: anytype, align_to: usize) ?@TypeOf(ptr) {...@@ -3801,7 +3803,7 @@ pub fn alignPointer(ptr: anytype, align_to: usize) ?@TypeOf(ptr) {
3801 return @alignCast(ptr + adjust_off);3803 return @alignCast(ptr + adjust_off);
3802}3804}
38033805
3804test "alignPointer" {3806test alignPointer {
3805 const S = struct {3807 const S = struct {
3806 fn checkAlign(comptime T: type, base: usize, align_to: usize, expected: usize) !void {3808 fn checkAlign(comptime T: type, base: usize, align_to: usize, expected: usize) !void {
3807 const ptr: T = @ptrFromInt(base);3809 const ptr: T = @ptrFromInt(base);
...@@ -3850,7 +3852,7 @@ pub fn asBytes(ptr: anytype) AsBytesReturnType(@TypeOf(ptr)) {...@@ -3850,7 +3852,7 @@ pub fn asBytes(ptr: anytype) AsBytesReturnType(@TypeOf(ptr)) {
3850 return @ptrCast(@alignCast(ptr));3852 return @ptrCast(@alignCast(ptr));
3851}3853}
38523854
3853test "asBytes" {3855test asBytes {
3854 const deadbeef = @as(u32, 0xDEADBEEF);3856 const deadbeef = @as(u32, 0xDEADBEEF);
3855 const deadbeef_bytes = switch (native_endian) {3857 const deadbeef_bytes = switch (native_endian) {
3856 .big => "\xDE\xAD\xBE\xEF",3858 .big => "\xDE\xAD\xBE\xEF",
...@@ -3910,7 +3912,7 @@ pub fn toBytes(value: anytype) [@sizeOf(@TypeOf(value))]u8 {...@@ -3910,7 +3912,7 @@ pub fn toBytes(value: anytype) [@sizeOf(@TypeOf(value))]u8 {
3910 return asBytes(&value).*;3912 return asBytes(&value).*;
3911}3913}
39123914
3913test "toBytes" {3915test toBytes {
3914 var my_bytes = toBytes(@as(u32, 0x12345678));3916 var my_bytes = toBytes(@as(u32, 0x12345678));
3915 switch (native_endian) {3917 switch (native_endian) {
3916 .big => try testing.expect(eql(u8, &my_bytes, "\x12\x34\x56\x78")),3918 .big => try testing.expect(eql(u8, &my_bytes, "\x12\x34\x56\x78")),
...@@ -3934,7 +3936,7 @@ pub fn bytesAsValue(comptime T: type, bytes: anytype) BytesAsValueReturnType(T,...@@ -3934,7 +3936,7 @@ pub fn bytesAsValue(comptime T: type, bytes: anytype) BytesAsValueReturnType(T,
3934 return @ptrCast(bytes);3936 return @ptrCast(bytes);
3935}3937}
39363938
3937test "bytesAsValue" {3939test bytesAsValue {
3938 const deadbeef = @as(u32, 0xDEADBEEF);3940 const deadbeef = @as(u32, 0xDEADBEEF);
3939 const deadbeef_bytes = switch (native_endian) {3941 const deadbeef_bytes = switch (native_endian) {
3940 .big => "\xDE\xAD\xBE\xEF",3942 .big => "\xDE\xAD\xBE\xEF",
...@@ -3993,7 +3995,7 @@ test "bytesAsValue preserves pointer attributes" {...@@ -3993,7 +3995,7 @@ test "bytesAsValue preserves pointer attributes" {
3993pub fn bytesToValue(comptime T: type, bytes: anytype) T {3995pub fn bytesToValue(comptime T: type, bytes: anytype) T {
3994 return bytesAsValue(T, bytes).*;3996 return bytesAsValue(T, bytes).*;
3995}3997}
3996test "bytesToValue" {3998test bytesToValue {
3997 const deadbeef_bytes = switch (native_endian) {3999 const deadbeef_bytes = switch (native_endian) {
3998 .big => "\xDE\xAD\xBE\xEF",4000 .big => "\xDE\xAD\xBE\xEF",
3999 .little => "\xEF\xBE\xAD\xDE",4001 .little => "\xEF\xBE\xAD\xDE",
...@@ -4021,7 +4023,7 @@ pub fn bytesAsSlice(comptime T: type, bytes: anytype) BytesAsSliceReturnType(T,...@@ -4021,7 +4023,7 @@ pub fn bytesAsSlice(comptime T: type, bytes: anytype) BytesAsSliceReturnType(T,
4021 return @as(cast_target, @ptrCast(bytes))[0..@divExact(bytes.len, @sizeOf(T))];4023 return @as(cast_target, @ptrCast(bytes))[0..@divExact(bytes.len, @sizeOf(T))];
4022}4024}
40234025
4024test "bytesAsSlice" {4026test bytesAsSlice {
4025 {4027 {
4026 const bytes = [_]u8{ 0xDE, 0xAD, 0xBE, 0xEF };4028 const bytes = [_]u8{ 0xDE, 0xAD, 0xBE, 0xEF };
4027 const slice = bytesAsSlice(u16, bytes[0..]);4029 const slice = bytesAsSlice(u16, bytes[0..]);
...@@ -4110,7 +4112,7 @@ pub fn sliceAsBytes(slice: anytype) SliceAsBytesReturnType(@TypeOf(slice)) {...@@ -4110,7 +4112,7 @@ pub fn sliceAsBytes(slice: anytype) SliceAsBytesReturnType(@TypeOf(slice)) {
4110 return @as(cast_target, @ptrCast(slice))[0 .. slice.len * @sizeOf(std.meta.Elem(Slice))];4112 return @as(cast_target, @ptrCast(slice))[0 .. slice.len * @sizeOf(std.meta.Elem(Slice))];
4111}4113}
41124114
4113test "sliceAsBytes" {4115test sliceAsBytes {
4114 const bytes = [_]u16{ 0xDEAD, 0xBEEF };4116 const bytes = [_]u16{ 0xDEAD, 0xBEEF };
4115 const slice = sliceAsBytes(bytes[0..]);4117 const slice = sliceAsBytes(bytes[0..]);
4116 try testing.expect(slice.len == 4);4118 try testing.expect(slice.len == 4);
...@@ -4268,7 +4270,7 @@ fn doNotOptimizeAwayC(ptr: anytype) void {...@@ -4268,7 +4270,7 @@ fn doNotOptimizeAwayC(ptr: anytype) void {
4268 dest.* = 0;4270 dest.* = 0;
4269}4271}
42704272
4271test "doNotOptimizeAway" {4273test doNotOptimizeAway {
4272 comptime doNotOptimizeAway("test");4274 comptime doNotOptimizeAway("test");
42734275
4274 doNotOptimizeAway(null);4276 doNotOptimizeAway(null);
...@@ -4293,7 +4295,7 @@ test "doNotOptimizeAway" {...@@ -4293,7 +4295,7 @@ test "doNotOptimizeAway" {
4293 doNotOptimizeAway(@as(std.builtin.Endian, .little));4295 doNotOptimizeAway(@as(std.builtin.Endian, .little));
4294}4296}
42954297
4296test "alignForward" {4298test alignForward {
4297 try testing.expect(alignForward(usize, 1, 1) == 1);4299 try testing.expect(alignForward(usize, 1, 1) == 1);
4298 try testing.expect(alignForward(usize, 2, 1) == 2);4300 try testing.expect(alignForward(usize, 2, 1) == 2);
4299 try testing.expect(alignForward(usize, 1, 2) == 2);4301 try testing.expect(alignForward(usize, 1, 2) == 2);
...@@ -4362,7 +4364,7 @@ pub fn isAlignedGeneric(comptime T: type, addr: T, alignment: T) bool {...@@ -4362,7 +4364,7 @@ pub fn isAlignedGeneric(comptime T: type, addr: T, alignment: T) bool {
4362 return alignBackward(T, addr, alignment) == addr;4364 return alignBackward(T, addr, alignment) == addr;
4363}4365}
43644366
4365test "isAligned" {4367test isAligned {
4366 try testing.expect(isAligned(0, 4));4368 try testing.expect(isAligned(0, 4));
4367 try testing.expect(isAligned(1, 1));4369 try testing.expect(isAligned(1, 1));
4368 try testing.expect(isAligned(2, 1));4370 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+3-3
...@@ -1379,7 +1379,7 @@ pub fn GetFinalPathNameByHandle(...@@ -1379,7 +1379,7 @@ pub fn GetFinalPathNameByHandle(
1379 }1379 }
1380}1380}
13811381
1382test "GetFinalPathNameByHandle" {1382test GetFinalPathNameByHandle {
1383 if (builtin.os.tag != .windows)1383 if (builtin.os.tag != .windows)
1384 return;1384 return;
13851385
...@@ -2601,7 +2601,7 @@ pub fn ntToWin32Namespace(path: []const u16) !PathSpace {...@@ -2601,7 +2601,7 @@ pub fn ntToWin32Namespace(path: []const u16) !PathSpace {
2601 }2601 }
2602}2602}
26032603
2604test "ntToWin32Namespace" {2604test ntToWin32Namespace {
2605 const L = std.unicode.utf8ToUtf16LeStringLiteral;2605 const L = std.unicode.utf8ToUtf16LeStringLiteral;
26062606
2607 try testNtToWin32Namespace(L("UNC"), L("\\??\\UNC"));2607 try testNtToWin32Namespace(L("UNC"), L("\\??\\UNC"));
...@@ -3539,7 +3539,7 @@ pub const GUID = extern struct {...@@ -3539,7 +3539,7 @@ pub const GUID = extern struct {
3539 }3539 }
3540};3540};
35413541
3542test "GUID" {3542test GUID {
3543 try std.testing.expectEqual(3543 try std.testing.expectEqual(
3544 GUID{3544 GUID{
3545 .Data1 = 0x01234567,3545 .Data1 = 0x01234567,
lib/std/process.zig+3-3
...@@ -215,7 +215,7 @@ pub const EnvMap = struct {...@@ -215,7 +215,7 @@ pub const EnvMap = struct {
215 }215 }
216};216};
217217
218test "EnvMap" {218test EnvMap {
219 var env = EnvMap.init(testing.allocator);219 var env = EnvMap.init(testing.allocator);
220 defer env.deinit();220 defer env.deinit();
221221
...@@ -377,7 +377,7 @@ pub fn getEnvMap(allocator: Allocator) GetEnvMapError!EnvMap {...@@ -377,7 +377,7 @@ pub fn getEnvMap(allocator: Allocator) GetEnvMapError!EnvMap {
377 }377 }
378}378}
379379
380test "getEnvMap" {380test getEnvMap {
381 var env = try getEnvMap(testing.allocator);381 var env = try getEnvMap(testing.allocator);
382 defer env.deinit();382 defer env.deinit();
383}383}
...@@ -1181,7 +1181,7 @@ pub fn argsFree(allocator: Allocator, args_alloc: []const [:0]u8) void {...@@ -1181,7 +1181,7 @@ pub fn argsFree(allocator: Allocator, args_alloc: []const [:0]u8) void {
1181 return allocator.free(aligned_allocated_buf);1181 return allocator.free(aligned_allocated_buf);
1182}1182}
11831183
1184test "ArgIteratorWindows" {1184test ArgIteratorWindows {
1185 const t = testArgIteratorWindows;1185 const t = testArgIteratorWindows;
11861186
1187 try t(1187 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
...@@ -53,7 +53,7 @@ pub fn sleep(nanoseconds: u64) void {...@@ -53,7 +53,7 @@ pub fn sleep(nanoseconds: u64) void {
53 posix.nanosleep(s, ns);53 posix.nanosleep(s, ns);
54}54}
5555
56test "sleep" {56test sleep {
57 sleep(1);57 sleep(1);
58}58}
5959
...@@ -123,7 +123,7 @@ pub fn nanoTimestamp() i128 {...@@ -123,7 +123,7 @@ pub fn nanoTimestamp() i128 {
123 }123 }
124}124}
125125
126test "timestamp" {126test milliTimestamp {
127 const margin = ns_per_ms * 50;127 const margin = ns_per_ms * 50;
128128
129 const time_0 = milliTimestamp();129 const time_0 = milliTimestamp();
...@@ -327,7 +327,7 @@ pub const Timer = struct {...@@ -327,7 +327,7 @@ pub const Timer = struct {
327 }327 }
328};328};
329329
330test "Timer + Instant" {330test Timer {
331 const margin = ns_per_ms * 150;331 const margin = ns_per_ms * 150;
332332
333 var timer = try Timer.start();333 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;