mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
std: promote tests to doctests
Now these show up as "example usage" in generated documentation.
This commit is contained in:
parent
5ae838d105
commit
12191c8a22
26 changed files with 311 additions and 289 deletions
|
|
@ -147,7 +147,7 @@ pub fn isWhitespace(c: u8) bool {
|
|||
/// See also: `isWhitespace`
|
||||
pub const whitespace = [_]u8{ ' ', '\t', '\n', '\r', control_code.vt, control_code.ff };
|
||||
|
||||
test "whitespace" {
|
||||
test whitespace {
|
||||
for (whitespace) |char| try std.testing.expect(isWhitespace(char));
|
||||
|
||||
var i: u8 = 0;
|
||||
|
|
@ -278,7 +278,7 @@ pub fn lowerString(output: []u8, ascii_string: []const u8) []u8 {
|
|||
return output[0..ascii_string.len];
|
||||
}
|
||||
|
||||
test "lowerString" {
|
||||
test lowerString {
|
||||
var buf: [1024]u8 = undefined;
|
||||
const result = lowerString(&buf, "aBcDeFgHiJkLmNOPqrst0234+💩!");
|
||||
try std.testing.expectEqualStrings("abcdefghijklmnopqrst0234+💩!", result);
|
||||
|
|
@ -291,7 +291,7 @@ pub fn allocLowerString(allocator: std.mem.Allocator, ascii_string: []const u8)
|
|||
return lowerString(result, ascii_string);
|
||||
}
|
||||
|
||||
test "allocLowerString" {
|
||||
test allocLowerString {
|
||||
const result = try allocLowerString(std.testing.allocator, "aBcDeFgHiJkLmNOPqrst0234+💩!");
|
||||
defer std.testing.allocator.free(result);
|
||||
try std.testing.expectEqualStrings("abcdefghijklmnopqrst0234+💩!", result);
|
||||
|
|
@ -307,7 +307,7 @@ pub fn upperString(output: []u8, ascii_string: []const u8) []u8 {
|
|||
return output[0..ascii_string.len];
|
||||
}
|
||||
|
||||
test "upperString" {
|
||||
test upperString {
|
||||
var buf: [1024]u8 = undefined;
|
||||
const result = upperString(&buf, "aBcDeFgHiJkLmNOPqrst0234+💩!");
|
||||
try std.testing.expectEqualStrings("ABCDEFGHIJKLMNOPQRST0234+💩!", result);
|
||||
|
|
@ -320,7 +320,7 @@ pub fn allocUpperString(allocator: std.mem.Allocator, ascii_string: []const u8)
|
|||
return upperString(result, ascii_string);
|
||||
}
|
||||
|
||||
test "allocUpperString" {
|
||||
test allocUpperString {
|
||||
const result = try allocUpperString(std.testing.allocator, "aBcDeFgHiJkLmNOPqrst0234+💩!");
|
||||
defer std.testing.allocator.free(result);
|
||||
try std.testing.expectEqualStrings("ABCDEFGHIJKLMNOPQRST0234+💩!", result);
|
||||
|
|
@ -335,7 +335,7 @@ pub fn eqlIgnoreCase(a: []const u8, b: []const u8) bool {
|
|||
return true;
|
||||
}
|
||||
|
||||
test "eqlIgnoreCase" {
|
||||
test eqlIgnoreCase {
|
||||
try std.testing.expect(eqlIgnoreCase("HEl💩Lo!", "hel💩lo!"));
|
||||
try std.testing.expect(!eqlIgnoreCase("hElLo!", "hello! "));
|
||||
try std.testing.expect(!eqlIgnoreCase("hElLo!", "helro!"));
|
||||
|
|
@ -345,7 +345,7 @@ pub fn startsWithIgnoreCase(haystack: []const u8, needle: []const u8) bool {
|
|||
return if (needle.len > haystack.len) false else eqlIgnoreCase(haystack[0..needle.len], needle);
|
||||
}
|
||||
|
||||
test "startsWithIgnoreCase" {
|
||||
test startsWithIgnoreCase {
|
||||
try std.testing.expect(startsWithIgnoreCase("boB", "Bo"));
|
||||
try std.testing.expect(!startsWithIgnoreCase("Needle in hAyStAcK", "haystack"));
|
||||
}
|
||||
|
|
@ -354,7 +354,7 @@ pub fn endsWithIgnoreCase(haystack: []const u8, needle: []const u8) bool {
|
|||
return if (needle.len > haystack.len) false else eqlIgnoreCase(haystack[haystack.len - needle.len ..], needle);
|
||||
}
|
||||
|
||||
test "endsWithIgnoreCase" {
|
||||
test endsWithIgnoreCase {
|
||||
try std.testing.expect(endsWithIgnoreCase("Needle in HaYsTaCk", "haystack"));
|
||||
try std.testing.expect(!endsWithIgnoreCase("BoB", "Bo"));
|
||||
}
|
||||
|
|
@ -409,7 +409,7 @@ fn boyerMooreHorspoolPreprocessIgnoreCase(pattern: []const u8, table: *[256]usiz
|
|||
}
|
||||
}
|
||||
|
||||
test "indexOfIgnoreCase" {
|
||||
test indexOfIgnoreCase {
|
||||
try std.testing.expect(indexOfIgnoreCase("one Two Three Four", "foUr").? == 14);
|
||||
try std.testing.expect(indexOfIgnoreCase("one two three FouR", "gOur") == null);
|
||||
try std.testing.expect(indexOfIgnoreCase("foO", "Foo").? == 0);
|
||||
|
|
|
|||
|
|
@ -1648,7 +1648,7 @@ fn testStaticBitSet(comptime Set: type) !void {
|
|||
try testPureBitSet(Set);
|
||||
}
|
||||
|
||||
test "IntegerBitSet" {
|
||||
test IntegerBitSet {
|
||||
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
|
||||
|
||||
try testStaticBitSet(IntegerBitSet(0));
|
||||
|
|
@ -1661,7 +1661,7 @@ test "IntegerBitSet" {
|
|||
try testStaticBitSet(IntegerBitSet(127));
|
||||
}
|
||||
|
||||
test "ArrayBitSet" {
|
||||
test ArrayBitSet {
|
||||
inline for (.{ 0, 1, 2, 31, 32, 33, 63, 64, 65, 254, 500, 3000 }) |size| {
|
||||
try testStaticBitSet(ArrayBitSet(u8, size));
|
||||
try testStaticBitSet(ArrayBitSet(u16, size));
|
||||
|
|
@ -1671,7 +1671,7 @@ test "ArrayBitSet" {
|
|||
}
|
||||
}
|
||||
|
||||
test "DynamicBitSetUnmanaged" {
|
||||
test DynamicBitSetUnmanaged {
|
||||
const allocator = std.testing.allocator;
|
||||
var a = try DynamicBitSetUnmanaged.initEmpty(allocator, 300);
|
||||
try testing.expectEqual(@as(usize, 0), a.count());
|
||||
|
|
@ -1724,7 +1724,7 @@ test "DynamicBitSetUnmanaged" {
|
|||
}
|
||||
}
|
||||
|
||||
test "DynamicBitSet" {
|
||||
test DynamicBitSet {
|
||||
const allocator = std.testing.allocator;
|
||||
var a = try DynamicBitSet.initEmpty(allocator, 300);
|
||||
try testing.expectEqual(@as(usize, 0), a.count());
|
||||
|
|
@ -1765,7 +1765,7 @@ test "DynamicBitSet" {
|
|||
}
|
||||
}
|
||||
|
||||
test "StaticBitSet" {
|
||||
test StaticBitSet {
|
||||
try testing.expectEqual(IntegerBitSet(0), StaticBitSet(0));
|
||||
try testing.expectEqual(IntegerBitSet(5), StaticBitSet(5));
|
||||
try testing.expectEqual(IntegerBitSet(@bitSizeOf(usize)), StaticBitSet(@bitSizeOf(usize)));
|
||||
|
|
|
|||
|
|
@ -287,7 +287,7 @@ pub fn BoundedArrayAligned(
|
|||
};
|
||||
}
|
||||
|
||||
test "BoundedArray" {
|
||||
test BoundedArray {
|
||||
var a = try BoundedArray(u8, 64).init(32);
|
||||
|
||||
try testing.expectEqual(a.capacity(), 64);
|
||||
|
|
|
|||
|
|
@ -1231,7 +1231,7 @@ fn windowsCreateProcessSupportsExtension(ext: []const u16) ?CreateProcessSupport
|
|||
return null;
|
||||
}
|
||||
|
||||
test "windowsCreateProcessSupportsExtension" {
|
||||
test windowsCreateProcessSupportsExtension {
|
||||
try std.testing.expectEqual(CreateProcessSupportedExtension.exe, windowsCreateProcessSupportsExtension(&[_]u16{ '.', 'e', 'X', 'e' }).?);
|
||||
try std.testing.expect(windowsCreateProcessSupportsExtension(&[_]u16{ '.', 'e', 'X', 'e', 'c' }) == null);
|
||||
}
|
||||
|
|
@ -1322,7 +1322,7 @@ pub fn argvToCommandLineWindows(
|
|||
return try unicode.wtf8ToWtf16LeAllocZ(allocator, buf.items);
|
||||
}
|
||||
|
||||
test "argvToCommandLineWindows" {
|
||||
test argvToCommandLineWindows {
|
||||
const t = testArgvToCommandLineWindows;
|
||||
|
||||
try t(&.{
|
||||
|
|
@ -1556,7 +1556,7 @@ pub fn createNullDelimitedEnvMap(arena: mem.Allocator, env_map: *const EnvMap) !
|
|||
return envp_buf;
|
||||
}
|
||||
|
||||
test "createNullDelimitedEnvMap" {
|
||||
test createNullDelimitedEnvMap {
|
||||
const testing = std.testing;
|
||||
const allocator = testing.allocator;
|
||||
var envmap = EnvMap.init(allocator);
|
||||
|
|
|
|||
|
|
@ -138,7 +138,7 @@ pub inline fn secureZero(comptime T: type, s: []T) void {
|
|||
@memset(@as([]volatile T, s), 0);
|
||||
}
|
||||
|
||||
test "timingSafeEql" {
|
||||
test timingSafeEql {
|
||||
var a: [100]u8 = undefined;
|
||||
var b: [100]u8 = undefined;
|
||||
random.bytes(a[0..]);
|
||||
|
|
@ -162,7 +162,7 @@ test "timingSafeEql (vectors)" {
|
|||
try testing.expect(timingSafeEql(@Vector(100, u8), v1, v3));
|
||||
}
|
||||
|
||||
test "timingSafeCompare" {
|
||||
test timingSafeCompare {
|
||||
var a = [_]u8{10} ** 32;
|
||||
var b = [_]u8{10} ** 32;
|
||||
try testing.expectEqual(timingSafeCompare(u8, &a, &b, .big), .eq);
|
||||
|
|
@ -195,7 +195,7 @@ test "timingSafe{Add,Sub}" {
|
|||
}
|
||||
}
|
||||
|
||||
test "secureZero" {
|
||||
test secureZero {
|
||||
var a = [_]u8{0xfe} ** 8;
|
||||
var b = [_]u8{0xfe} ** 8;
|
||||
|
||||
|
|
|
|||
|
|
@ -905,7 +905,7 @@ fn machoSearchSymbols(symbols: []const MachoSymbol, address: usize) ?*const Mach
|
|||
return null;
|
||||
}
|
||||
|
||||
test "machoSearchSymbols" {
|
||||
test machoSearchSymbols {
|
||||
const symbols = [_]MachoSymbol{
|
||||
.{ .addr = 100, .strx = undefined, .size = undefined, .ofile = undefined },
|
||||
.{ .addr = 200, .strx = undefined, .size = undefined, .ofile = undefined },
|
||||
|
|
@ -1504,7 +1504,7 @@ fn printLineFromFileAnyOs(out_stream: anytype, line_info: LineInfo) !void {
|
|||
}
|
||||
}
|
||||
|
||||
test "printLineFromFileAnyOs" {
|
||||
test printLineFromFileAnyOs {
|
||||
var output = std.ArrayList(u8).init(std.testing.allocator);
|
||||
defer output.deinit();
|
||||
const output_stream = output.writer();
|
||||
|
|
|
|||
|
|
@ -507,7 +507,7 @@ test "LinearFifo(u8, .Dynamic)" {
|
|||
}
|
||||
}
|
||||
|
||||
test "LinearFifo" {
|
||||
test LinearFifo {
|
||||
inline for ([_]type{ u1, u8, u16, u64 }) |T| {
|
||||
inline for ([_]LinearFifoBufferType{ LinearFifoBufferType{ .Static = 32 }, .Slice, .Dynamic }) |bt| {
|
||||
const FifoType = LinearFifo(T, bt);
|
||||
|
|
|
|||
|
|
@ -1303,7 +1303,7 @@ pub fn fmtDuration(ns: u64) Formatter(formatDuration) {
|
|||
return .{ .data = data };
|
||||
}
|
||||
|
||||
test "fmtDuration" {
|
||||
test fmtDuration {
|
||||
var buf: [24]u8 = undefined;
|
||||
inline for (.{
|
||||
.{ .s = "0ns", .d = 0 },
|
||||
|
|
@ -1367,7 +1367,7 @@ pub fn fmtDurationSigned(ns: i64) Formatter(formatDurationSigned) {
|
|||
return .{ .data = ns };
|
||||
}
|
||||
|
||||
test "fmtDurationSigned" {
|
||||
test fmtDurationSigned {
|
||||
var buf: [24]u8 = undefined;
|
||||
inline for (.{
|
||||
.{ .s = "0ns", .d = 0 },
|
||||
|
|
@ -1497,7 +1497,7 @@ pub fn parseInt(comptime T: type, buf: []const u8, base: u8) ParseIntError!T {
|
|||
return parseWithSign(T, buf, base, .pos);
|
||||
}
|
||||
|
||||
test "parseInt" {
|
||||
test parseInt {
|
||||
try std.testing.expect((try parseInt(i32, "-10", 10)) == -10);
|
||||
try std.testing.expect((try parseInt(i32, "+10", 10)) == 10);
|
||||
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!
|
|||
return parseWithSign(T, buf, base, .pos);
|
||||
}
|
||||
|
||||
test "parseUnsigned" {
|
||||
test parseUnsigned {
|
||||
try std.testing.expect((try parseUnsigned(u16, "050124", 10)) == 50124);
|
||||
try std.testing.expect((try parseUnsigned(u16, "65535", 10)) == 65535);
|
||||
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 {
|
|||
return math.mul(usize, number, multiplier);
|
||||
}
|
||||
|
||||
test "parseIntSizeSuffix" {
|
||||
test parseIntSizeSuffix {
|
||||
try std.testing.expect(try parseIntSizeSuffix("2", 10) == 2);
|
||||
try std.testing.expect(try parseIntSizeSuffix("2B", 10) == 2);
|
||||
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
|
|||
return result[0 .. result.len - 1 :0];
|
||||
}
|
||||
|
||||
test "bufPrintInt" {
|
||||
test bufPrintIntToSlice {
|
||||
var buffer: [100]u8 = undefined;
|
||||
const buf = buffer[0..];
|
||||
|
||||
|
|
@ -1830,7 +1830,7 @@ pub inline fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [cou
|
|||
}
|
||||
}
|
||||
|
||||
test "comptimePrint" {
|
||||
test comptimePrint {
|
||||
@setEvalBranchQuota(2000);
|
||||
try std.testing.expectEqual(*const [3:0]u8, @TypeOf(comptimePrint("{}", .{100})));
|
||||
try std.testing.expectEqualSlices(u8, "100", comptimePrint("{}", .{100}));
|
||||
|
|
@ -2445,14 +2445,14 @@ pub fn hexToBytes(out: []u8, input: []const u8) ![]u8 {
|
|||
return out[0 .. in_i / 2];
|
||||
}
|
||||
|
||||
test "bytesToHex" {
|
||||
test bytesToHex {
|
||||
const input = "input slice";
|
||||
const encoded = bytesToHex(input, .lower);
|
||||
var decoded: [input.len]u8 = undefined;
|
||||
try std.testing.expectEqualSlices(u8, input, try hexToBytes(&decoded, &encoded));
|
||||
}
|
||||
|
||||
test "hexToBytes" {
|
||||
test hexToBytes {
|
||||
var buf: [32]u8 = undefined;
|
||||
try expectFmt("90" ** 32, "{s}", .{fmtSliceHexUpper(try hexToBytes(&buf, "90" ** 32))});
|
||||
try expectFmt("ABCD", "{s}", .{fmtSliceHexUpper(try hexToBytes(&buf, "ABCD"))});
|
||||
|
|
|
|||
|
|
@ -61,7 +61,7 @@ pub fn getAppDataDir(allocator: mem.Allocator, appname: []const u8) GetAppDataDi
|
|||
}
|
||||
}
|
||||
|
||||
test "getAppDataDir" {
|
||||
test getAppDataDir {
|
||||
if (native_os == .wasi) return error.SkipZigTest;
|
||||
|
||||
// We can't actually validate the result
|
||||
|
|
|
|||
|
|
@ -180,7 +180,7 @@ fn testJoinMaybeZPosix(paths: []const []const u8, expected: []const u8, zero: bo
|
|||
try testing.expectEqualSlices(u8, expected, if (zero) actual[0 .. actual.len - 1 :0] else actual);
|
||||
}
|
||||
|
||||
test "join" {
|
||||
test join {
|
||||
{
|
||||
const actual: []u8 = try join(testing.allocator, &[_][]const u8{});
|
||||
defer testing.allocator.free(actual);
|
||||
|
|
@ -303,7 +303,7 @@ pub fn isAbsolutePosixZ(path_c: [*:0]const u8) bool {
|
|||
return isAbsolutePosix(mem.sliceTo(path_c, 0));
|
||||
}
|
||||
|
||||
test "isAbsoluteWindows" {
|
||||
test isAbsoluteWindows {
|
||||
try testIsAbsoluteWindows("", false);
|
||||
try testIsAbsoluteWindows("/", true);
|
||||
try testIsAbsoluteWindows("//", true);
|
||||
|
|
@ -326,7 +326,7 @@ test "isAbsoluteWindows" {
|
|||
try testIsAbsoluteWindows("/usr/local", true);
|
||||
}
|
||||
|
||||
test "isAbsolutePosix" {
|
||||
test isAbsolutePosix {
|
||||
try testIsAbsolutePosix("", false);
|
||||
try testIsAbsolutePosix("/home/foo", true);
|
||||
try testIsAbsolutePosix("/home/foo/..", true);
|
||||
|
|
@ -400,7 +400,7 @@ pub fn windowsParsePath(path: []const u8) WindowsPath {
|
|||
return relative_path;
|
||||
}
|
||||
|
||||
test "windowsParsePath" {
|
||||
test windowsParsePath {
|
||||
{
|
||||
const parsed = windowsParsePath("//a/b");
|
||||
try testing.expect(parsed.is_abs);
|
||||
|
|
@ -884,7 +884,7 @@ pub fn dirnamePosix(path: []const u8) ?[]const u8 {
|
|||
return path[0..end_index];
|
||||
}
|
||||
|
||||
test "dirnamePosix" {
|
||||
test dirnamePosix {
|
||||
try testDirnamePosix("/a/b/c", "/a/b");
|
||||
try testDirnamePosix("/a/b/c///", "/a/b");
|
||||
try testDirnamePosix("/a", "/");
|
||||
|
|
@ -898,7 +898,7 @@ test "dirnamePosix" {
|
|||
try testDirnamePosix("a//", null);
|
||||
}
|
||||
|
||||
test "dirnameWindows" {
|
||||
test dirnameWindows {
|
||||
try testDirnameWindows("c:\\", null);
|
||||
try testDirnameWindows("c:\\foo", "c:\\");
|
||||
try testDirnameWindows("c:\\foo\\", "c:\\");
|
||||
|
|
@ -1011,7 +1011,7 @@ pub fn basenameWindows(path: []const u8) []const u8 {
|
|||
return path[start_index + 1 .. end_index];
|
||||
}
|
||||
|
||||
test "basename" {
|
||||
test basename {
|
||||
try testBasename("", "");
|
||||
try testBasename("/", "");
|
||||
try testBasename("/dir/basename.ext", "basename.ext");
|
||||
|
|
@ -1186,7 +1186,7 @@ pub fn relativePosix(allocator: Allocator, from: []const u8, to: []const u8) ![]
|
|||
return [_]u8{};
|
||||
}
|
||||
|
||||
test "relative" {
|
||||
test relative {
|
||||
try testRelativeWindows("c:/blah\\blah", "d:/games", "D:\\games");
|
||||
try testRelativeWindows("c:/aaaa/bbbb", "c:/aaaa", "..");
|
||||
try testRelativeWindows("c:/aaaa/bbbb", "c:/cccc", "..\\..\\cccc");
|
||||
|
|
@ -1271,7 +1271,7 @@ fn testExtension(path: []const u8, expected: []const u8) !void {
|
|||
try testing.expectEqualStrings(expected, extension(path));
|
||||
}
|
||||
|
||||
test "extension" {
|
||||
test extension {
|
||||
try testExtension("", "");
|
||||
try testExtension(".", "");
|
||||
try testExtension("a.", ".");
|
||||
|
|
@ -1328,7 +1328,7 @@ fn testStem(path: []const u8, expected: []const u8) !void {
|
|||
try testing.expectEqualStrings(expected, stem(path));
|
||||
}
|
||||
|
||||
test "stem" {
|
||||
test stem {
|
||||
try testStem("hello/world/lib.tar.gz", "lib.tar");
|
||||
try testStem("hello/world/lib.tar", "lib");
|
||||
try testStem("hello/world/lib", "lib");
|
||||
|
|
|
|||
|
|
@ -421,7 +421,7 @@ fn dummyWrite(context: void, data: []const u8) error{}!usize {
|
|||
return data.len;
|
||||
}
|
||||
|
||||
test "null_writer" {
|
||||
test null_writer {
|
||||
null_writer.writeAll("yay" ** 10) catch |err| switch (err) {};
|
||||
}
|
||||
|
||||
|
|
|
|||
162
lib/std/math.zig
162
lib/std/math.zig
|
|
@ -167,26 +167,16 @@ pub fn approxEqRel(comptime T: type, x: T, y: T, tolerance: T) bool {
|
|||
return @abs(x - y) <= @max(@abs(x), @abs(y)) * tolerance;
|
||||
}
|
||||
|
||||
test "approxEqAbs and approxEqRel" {
|
||||
test approxEqAbs {
|
||||
inline for ([_]type{ f16, f32, f64, f128 }) |T| {
|
||||
const eps_value = comptime floatEps(T);
|
||||
const sqrt_eps_value = comptime sqrt(eps_value);
|
||||
const nan_value = comptime nan(T);
|
||||
const inf_value = comptime inf(T);
|
||||
const min_value = comptime floatMin(T);
|
||||
|
||||
try testing.expect(approxEqAbs(T, 0.0, 0.0, eps_value));
|
||||
try testing.expect(approxEqAbs(T, -0.0, -0.0, eps_value));
|
||||
try testing.expect(approxEqAbs(T, 0.0, -0.0, eps_value));
|
||||
try testing.expect(approxEqRel(T, 1.0, 1.0, sqrt_eps_value));
|
||||
try testing.expect(!approxEqRel(T, 1.0, 0.0, sqrt_eps_value));
|
||||
try testing.expect(!approxEqAbs(T, 1.0 + 2 * eps_value, 1.0, eps_value));
|
||||
try testing.expect(approxEqAbs(T, 1.0 + 1 * eps_value, 1.0, eps_value));
|
||||
try testing.expect(!approxEqRel(T, 1.0, nan_value, sqrt_eps_value));
|
||||
try testing.expect(!approxEqRel(T, nan_value, nan_value, sqrt_eps_value));
|
||||
try testing.expect(approxEqRel(T, inf_value, inf_value, sqrt_eps_value));
|
||||
try testing.expect(approxEqRel(T, min_value, min_value, sqrt_eps_value));
|
||||
try testing.expect(approxEqRel(T, -min_value, -min_value, sqrt_eps_value));
|
||||
try testing.expect(approxEqAbs(T, min_value, 0.0, eps_value * 2));
|
||||
try testing.expect(approxEqAbs(T, -min_value, 0.0, eps_value * 2));
|
||||
}
|
||||
|
|
@ -198,18 +188,46 @@ test "approxEqAbs and approxEqRel" {
|
|||
// possible epsilon value like we do in the tests above. In the same vein, we
|
||||
// also can't represent a max/min, `NaN` or `Inf` values.
|
||||
const eps_value = 1e-4;
|
||||
const sqrt_eps_value = sqrt(eps_value);
|
||||
|
||||
try testing.expect(approxEqAbs(comptime_float, 0.0, 0.0, eps_value));
|
||||
try testing.expect(approxEqAbs(comptime_float, -0.0, -0.0, eps_value));
|
||||
try testing.expect(approxEqAbs(comptime_float, 0.0, -0.0, eps_value));
|
||||
try testing.expect(approxEqRel(comptime_float, 1.0, 1.0, sqrt_eps_value));
|
||||
try testing.expect(!approxEqRel(comptime_float, 1.0, 0.0, sqrt_eps_value));
|
||||
try testing.expect(!approxEqAbs(comptime_float, 1.0 + 2 * eps_value, 1.0, eps_value));
|
||||
try testing.expect(approxEqAbs(comptime_float, 1.0 + 1 * eps_value, 1.0, eps_value));
|
||||
}
|
||||
}
|
||||
|
||||
test approxEqRel {
|
||||
inline for ([_]type{ f16, f32, f64, f128 }) |T| {
|
||||
const eps_value = comptime floatEps(T);
|
||||
const sqrt_eps_value = comptime sqrt(eps_value);
|
||||
const nan_value = comptime nan(T);
|
||||
const inf_value = comptime inf(T);
|
||||
const min_value = comptime floatMin(T);
|
||||
|
||||
try testing.expect(approxEqRel(T, 1.0, 1.0, sqrt_eps_value));
|
||||
try testing.expect(!approxEqRel(T, 1.0, 0.0, sqrt_eps_value));
|
||||
try testing.expect(!approxEqRel(T, 1.0, nan_value, sqrt_eps_value));
|
||||
try testing.expect(!approxEqRel(T, nan_value, nan_value, sqrt_eps_value));
|
||||
try testing.expect(approxEqRel(T, inf_value, inf_value, sqrt_eps_value));
|
||||
try testing.expect(approxEqRel(T, min_value, min_value, sqrt_eps_value));
|
||||
try testing.expect(approxEqRel(T, -min_value, -min_value, sqrt_eps_value));
|
||||
}
|
||||
|
||||
comptime {
|
||||
// `comptime_float` is guaranteed to have the same precision and operations of
|
||||
// the largest other floating point type, which is f128 but it doesn't have a
|
||||
// defined layout so we can't rely on `@bitCast` to construct the smallest
|
||||
// possible epsilon value like we do in the tests above. In the same vein, we
|
||||
// also can't represent a max/min, `NaN` or `Inf` values.
|
||||
const eps_value = 1e-4;
|
||||
const sqrt_eps_value = sqrt(eps_value);
|
||||
|
||||
try testing.expect(approxEqRel(comptime_float, 1.0, 1.0, sqrt_eps_value));
|
||||
try testing.expect(!approxEqRel(comptime_float, 1.0, 0.0, sqrt_eps_value));
|
||||
}
|
||||
}
|
||||
|
||||
pub const doNotOptimizeAway = @compileError("Deprecated: use `std.mem.doNotOptimizeAway` instead");
|
||||
|
||||
pub fn raiseInvalid() void {
|
||||
|
|
@ -310,7 +328,7 @@ pub fn radiansToDegrees(ang: anytype) if (@TypeOf(ang) == comptime_int) comptime
|
|||
@compileError("Input must be float or a comptime number, or a vector of floats.");
|
||||
}
|
||||
|
||||
test "radiansToDegrees" {
|
||||
test radiansToDegrees {
|
||||
const zero: f32 = 0;
|
||||
const half_pi: f32 = pi / 2.0;
|
||||
const neg_quart_pi: f32 = -pi / 4.0;
|
||||
|
|
@ -345,7 +363,7 @@ pub fn degreesToRadians(ang: anytype) if (@TypeOf(ang) == comptime_int) comptime
|
|||
@compileError("Input must be float or a comptime number, or a vector of floats.");
|
||||
}
|
||||
|
||||
test "degreesToRadians" {
|
||||
test degreesToRadians {
|
||||
const ninety: f32 = 90;
|
||||
const neg_two_seventy: f32 = -270;
|
||||
const three_sixty: f32 = 360;
|
||||
|
|
@ -506,7 +524,7 @@ pub fn wrap(x: anytype, r: anytype) @TypeOf(x) {
|
|||
},
|
||||
}
|
||||
}
|
||||
test "wrap" {
|
||||
test wrap {
|
||||
// Within range
|
||||
try testing.expect(wrap(@as(i32, -75), @as(i32, 180)) == -75);
|
||||
try testing.expect(wrap(@as(i32, -75), @as(i32, -180)) == -75);
|
||||
|
|
@ -543,8 +561,7 @@ test "wrap" {
|
|||
var i: i32 = 1;
|
||||
_ = &i;
|
||||
try testing.expect(wrap(i, 10) == 1);
|
||||
}
|
||||
test wrap {
|
||||
|
||||
const limit: i32 = 180;
|
||||
// Within range
|
||||
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
|
|||
assert(lower <= upper);
|
||||
return @max(lower, @min(val, upper));
|
||||
}
|
||||
test "clamp" {
|
||||
test clamp {
|
||||
// Within range
|
||||
try testing.expect(std.math.clamp(@as(i32, -1), @as(i32, -4), @as(i32, 7)) == -1);
|
||||
// Below
|
||||
|
|
@ -650,7 +667,7 @@ pub fn shl(comptime T: type, a: T, shift_amt: anytype) T {
|
|||
return a << casted_shift_amt;
|
||||
}
|
||||
|
||||
test "shl" {
|
||||
test shl {
|
||||
if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) {
|
||||
// https://github.com/ziglang/zig/issues/12012
|
||||
return error.SkipZigTest;
|
||||
|
|
@ -695,7 +712,7 @@ pub fn shr(comptime T: type, a: T, shift_amt: anytype) T {
|
|||
return a >> casted_shift_amt;
|
||||
}
|
||||
|
||||
test "shr" {
|
||||
test shr {
|
||||
if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) {
|
||||
// https://github.com/ziglang/zig/issues/12012
|
||||
return error.SkipZigTest;
|
||||
|
|
@ -741,7 +758,7 @@ pub fn rotr(comptime T: type, x: T, r: anytype) T {
|
|||
}
|
||||
}
|
||||
|
||||
test "rotr" {
|
||||
test rotr {
|
||||
if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) {
|
||||
// https://github.com/ziglang/zig/issues/12012
|
||||
return error.SkipZigTest;
|
||||
|
|
@ -787,7 +804,7 @@ pub fn rotl(comptime T: type, x: T, r: anytype) T {
|
|||
}
|
||||
}
|
||||
|
||||
test "rotl" {
|
||||
test rotl {
|
||||
if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) {
|
||||
// https://github.com/ziglang/zig/issues/12012
|
||||
return error.SkipZigTest;
|
||||
|
|
@ -850,7 +867,7 @@ pub fn IntFittingRange(comptime from: comptime_int, comptime to: comptime_int) t
|
|||
return std.meta.Int(signedness, magnitude_bits);
|
||||
}
|
||||
|
||||
test "IntFittingRange" {
|
||||
test IntFittingRange {
|
||||
try testing.expect(IntFittingRange(0, 0) == u0);
|
||||
try testing.expect(IntFittingRange(0, 1) == u1);
|
||||
try testing.expect(IntFittingRange(0, 2) == u2);
|
||||
|
|
@ -918,7 +935,7 @@ pub fn divTrunc(comptime T: type, numerator: T, denominator: T) !T {
|
|||
return @divTrunc(numerator, denominator);
|
||||
}
|
||||
|
||||
test "divTrunc" {
|
||||
test divTrunc {
|
||||
try testDivTrunc();
|
||||
try comptime testDivTrunc();
|
||||
}
|
||||
|
|
@ -942,7 +959,7 @@ pub fn divFloor(comptime T: type, numerator: T, denominator: T) !T {
|
|||
return @divFloor(numerator, denominator);
|
||||
}
|
||||
|
||||
test "divFloor" {
|
||||
test divFloor {
|
||||
try testDivFloor();
|
||||
try comptime testDivFloor();
|
||||
}
|
||||
|
|
@ -979,7 +996,7 @@ pub fn divCeil(comptime T: type, numerator: T, denominator: T) !T {
|
|||
}
|
||||
}
|
||||
|
||||
test "divCeil" {
|
||||
test divCeil {
|
||||
try testDivCeil();
|
||||
try comptime testDivCeil();
|
||||
}
|
||||
|
|
@ -1023,7 +1040,7 @@ pub fn divExact(comptime T: type, numerator: T, denominator: T) !T {
|
|||
return result;
|
||||
}
|
||||
|
||||
test "divExact" {
|
||||
test divExact {
|
||||
try testDivExact();
|
||||
try comptime testDivExact();
|
||||
}
|
||||
|
|
@ -1049,7 +1066,7 @@ pub fn mod(comptime T: type, numerator: T, denominator: T) !T {
|
|||
return @mod(numerator, denominator);
|
||||
}
|
||||
|
||||
test "mod" {
|
||||
test mod {
|
||||
try testMod();
|
||||
try comptime testMod();
|
||||
}
|
||||
|
|
@ -1075,7 +1092,7 @@ pub fn rem(comptime T: type, numerator: T, denominator: T) !T {
|
|||
return @rem(numerator, denominator);
|
||||
}
|
||||
|
||||
test "rem" {
|
||||
test rem {
|
||||
try testRem();
|
||||
try comptime testRem();
|
||||
}
|
||||
|
|
@ -1104,7 +1121,7 @@ pub fn negateCast(x: anytype) !std.meta.Int(.signed, @bitSizeOf(@TypeOf(x))) {
|
|||
return -@as(int, @intCast(x));
|
||||
}
|
||||
|
||||
test "negateCast" {
|
||||
test negateCast {
|
||||
try testing.expect((negateCast(@as(u32, 999)) catch unreachable) == -999);
|
||||
try testing.expect(@TypeOf(negateCast(@as(u32, 999)) catch unreachable) == i32);
|
||||
|
||||
|
|
@ -1129,7 +1146,7 @@ pub fn cast(comptime T: type, x: anytype) ?T {
|
|||
}
|
||||
}
|
||||
|
||||
test "cast" {
|
||||
test cast {
|
||||
try testing.expect(cast(u8, 300) == null);
|
||||
try testing.expect(cast(u8, @as(u32, 300)) == null);
|
||||
try testing.expect(cast(i8, -200) == null);
|
||||
|
|
@ -1188,7 +1205,7 @@ pub fn ByteAlignedInt(comptime T: type) type {
|
|||
return extended_type;
|
||||
}
|
||||
|
||||
test "ByteAlignedInt" {
|
||||
test ByteAlignedInt {
|
||||
try testing.expect(ByteAlignedInt(u0) == u0);
|
||||
try testing.expect(ByteAlignedInt(i0) == i0);
|
||||
try testing.expect(ByteAlignedInt(u3) == u8);
|
||||
|
|
@ -1226,7 +1243,7 @@ pub fn floorPowerOfTwo(comptime T: type, value: T) T {
|
|||
return @as(T, 1) << log2_int(uT, @as(uT, @intCast(value)));
|
||||
}
|
||||
|
||||
test "floorPowerOfTwo" {
|
||||
test floorPowerOfTwo {
|
||||
try testFloorPowerOfTwo();
|
||||
try comptime testFloorPowerOfTwo();
|
||||
}
|
||||
|
|
@ -1288,7 +1305,7 @@ pub fn ceilPowerOfTwoAssert(comptime T: type, value: T) T {
|
|||
return ceilPowerOfTwo(T, value) catch unreachable;
|
||||
}
|
||||
|
||||
test "ceilPowerOfTwoPromote" {
|
||||
test ceilPowerOfTwoPromote {
|
||||
try testCeilPowerOfTwoPromote();
|
||||
try comptime testCeilPowerOfTwoPromote();
|
||||
}
|
||||
|
|
@ -1305,7 +1322,7 @@ fn testCeilPowerOfTwoPromote() !void {
|
|||
try testing.expectEqual(@as(u5, 16), ceilPowerOfTwoPromote(u4, 9));
|
||||
}
|
||||
|
||||
test "ceilPowerOfTwo" {
|
||||
test ceilPowerOfTwo {
|
||||
try testCeilPowerOfTwo();
|
||||
try comptime testCeilPowerOfTwo();
|
||||
}
|
||||
|
|
@ -1398,7 +1415,7 @@ pub fn lossyCast(comptime T: type, value: anytype) T {
|
|||
}
|
||||
}
|
||||
|
||||
test "lossyCast" {
|
||||
test lossyCast {
|
||||
try testing.expect(lossyCast(i16, 70000.0) == @as(i16, 32767));
|
||||
try testing.expect(lossyCast(u32, @as(i16, -255)) == @as(u32, 0));
|
||||
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) {
|
|||
return @mulAdd(Type, b - a, t, a);
|
||||
}
|
||||
|
||||
test "lerp" {
|
||||
test lerp {
|
||||
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // https://github.com/ziglang/zig/issues/17884
|
||||
if (builtin.zig_backend == .stage2_x86_64 and
|
||||
!comptime std.Target.x86.featureSetHas(builtin.cpu.features, .fma)) return error.SkipZigTest;
|
||||
|
|
@ -1483,7 +1500,7 @@ pub fn minInt(comptime T: type) comptime_int {
|
|||
return -(1 << (bit_count - 1));
|
||||
}
|
||||
|
||||
test "minInt and maxInt" {
|
||||
test maxInt {
|
||||
try testing.expect(maxInt(u0) == 0);
|
||||
try testing.expect(maxInt(u1) == 1);
|
||||
try testing.expect(maxInt(u8) == 255);
|
||||
|
|
@ -1500,7 +1517,9 @@ test "minInt and maxInt" {
|
|||
try testing.expect(maxInt(i63) == 4611686018427387903);
|
||||
try testing.expect(maxInt(i64) == 9223372036854775807);
|
||||
try testing.expect(maxInt(i128) == 170141183460469231731687303715884105727);
|
||||
}
|
||||
|
||||
test minInt {
|
||||
try testing.expect(minInt(u0) == 0);
|
||||
try testing.expect(minInt(u1) == 0);
|
||||
try testing.expect(minInt(u8) == 0);
|
||||
|
|
@ -1538,7 +1557,7 @@ pub fn mulWide(comptime T: type, a: T, b: T) std.meta.Int(
|
|||
return @as(ResultInt, a) * @as(ResultInt, b);
|
||||
}
|
||||
|
||||
test "mulWide" {
|
||||
test mulWide {
|
||||
try testing.expect(mulWide(u8, 5, 5) == 25);
|
||||
try testing.expect(mulWide(i8, 5, -5) == -25);
|
||||
try testing.expect(mulWide(u8, 100, 100) == 10000);
|
||||
|
|
@ -1563,6 +1582,12 @@ pub const Order = enum {
|
|||
};
|
||||
}
|
||||
|
||||
test invert {
|
||||
try testing.expect(Order.invert(order(0, 0)) == .eq);
|
||||
try testing.expect(Order.invert(order(1, 0)) == .lt);
|
||||
try testing.expect(Order.invert(order(-1, 0)) == .gt);
|
||||
}
|
||||
|
||||
pub fn compare(self: Order, op: CompareOperator) bool {
|
||||
return switch (self) {
|
||||
.lt => switch (op) {
|
||||
|
|
@ -1591,6 +1616,18 @@ pub const Order = enum {
|
|||
},
|
||||
};
|
||||
}
|
||||
|
||||
// https://github.com/ziglang/zig/issues/19295
|
||||
test "compare" {
|
||||
try testing.expect(order(-1, 0).compare(.lt));
|
||||
try testing.expect(order(-1, 0).compare(.lte));
|
||||
try testing.expect(order(0, 0).compare(.lte));
|
||||
try testing.expect(order(0, 0).compare(.eq));
|
||||
try testing.expect(order(0, 0).compare(.gte));
|
||||
try testing.expect(order(1, 0).compare(.gte));
|
||||
try testing.expect(order(1, 0).compare(.gt));
|
||||
try testing.expect(order(1, 0).compare(.neq));
|
||||
}
|
||||
};
|
||||
|
||||
/// Given two numbers, this function returns the order they are with respect to each other.
|
||||
|
|
@ -1633,6 +1670,15 @@ pub const CompareOperator = enum {
|
|||
.neq => .neq,
|
||||
};
|
||||
}
|
||||
|
||||
test reverse {
|
||||
inline for (@typeInfo(CompareOperator).Enum.fields) |op_field| {
|
||||
const op = @as(CompareOperator, @enumFromInt(op_field.value));
|
||||
try testing.expect(compare(2, op, 3) == compare(3, op.reverse(), 2));
|
||||
try testing.expect(compare(3, op, 3) == compare(3, op.reverse(), 3));
|
||||
try testing.expect(compare(4, op, 3) == compare(3, op.reverse(), 4));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/// 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 {
|
|||
};
|
||||
}
|
||||
|
||||
test "compare between signed and unsigned" {
|
||||
test compare {
|
||||
try testing.expect(compare(@as(i8, -1), .lt, @as(u8, 255)));
|
||||
try testing.expect(compare(@as(i8, 2), .gt, @as(u8, 1)));
|
||||
try testing.expect(!compare(@as(i8, -1), .gte, @as(u8, 255)));
|
||||
|
|
@ -1669,38 +1715,12 @@ test "compare between signed and unsigned" {
|
|||
try testing.expect(compare(@as(u8, 1), .eq, @as(u8, 1)));
|
||||
}
|
||||
|
||||
test "order" {
|
||||
test order {
|
||||
try testing.expect(order(0, 0) == .eq);
|
||||
try testing.expect(order(1, 0) == .gt);
|
||||
try testing.expect(order(-1, 0) == .lt);
|
||||
}
|
||||
|
||||
test "order.invert" {
|
||||
try testing.expect(Order.invert(order(0, 0)) == .eq);
|
||||
try testing.expect(Order.invert(order(1, 0)) == .lt);
|
||||
try testing.expect(Order.invert(order(-1, 0)) == .gt);
|
||||
}
|
||||
|
||||
test "order.compare" {
|
||||
try testing.expect(order(-1, 0).compare(.lt));
|
||||
try testing.expect(order(-1, 0).compare(.lte));
|
||||
try testing.expect(order(0, 0).compare(.lte));
|
||||
try testing.expect(order(0, 0).compare(.eq));
|
||||
try testing.expect(order(0, 0).compare(.gte));
|
||||
try testing.expect(order(1, 0).compare(.gte));
|
||||
try testing.expect(order(1, 0).compare(.gt));
|
||||
try testing.expect(order(1, 0).compare(.neq));
|
||||
}
|
||||
|
||||
test "compare.reverse" {
|
||||
inline for (@typeInfo(CompareOperator).Enum.fields) |op_field| {
|
||||
const op = @as(CompareOperator, @enumFromInt(op_field.value));
|
||||
try testing.expect(compare(2, op, 3) == compare(3, op.reverse(), 2));
|
||||
try testing.expect(compare(3, op, 3) == compare(3, op.reverse(), 3));
|
||||
try testing.expect(compare(4, op, 3) == compare(3, op.reverse(), 4));
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a mask of all ones if value is true,
|
||||
/// and a mask of all zeroes if value is false.
|
||||
/// Compiles to one instruction for register sized integers.
|
||||
|
|
@ -1722,7 +1742,7 @@ pub inline fn boolMask(comptime MaskInt: type, value: bool) MaskInt {
|
|||
return -%@as(MaskInt, @intCast(@intFromBool(value)));
|
||||
}
|
||||
|
||||
test "boolMask" {
|
||||
test boolMask {
|
||||
const runTest = struct {
|
||||
fn runTest() !void {
|
||||
try testing.expectEqual(@as(u1, 0), boolMask(u1, false));
|
||||
|
|
@ -1870,7 +1890,7 @@ fn testSign() !void {
|
|||
try std.testing.expectEqual(0.0, sign(0.0));
|
||||
}
|
||||
|
||||
test "sign" {
|
||||
test sign {
|
||||
if (builtin.zig_backend == .stage2_llvm) {
|
||||
// https://github.com/ziglang/zig/issues/12012
|
||||
return error.SkipZigTest;
|
||||
|
|
|
|||
130
lib/std/mem.zig
130
lib/std/mem.zig
|
|
@ -306,7 +306,7 @@ pub fn zeroes(comptime T: type) T {
|
|||
}
|
||||
}
|
||||
|
||||
test "zeroes" {
|
||||
test zeroes {
|
||||
const C_struct = extern struct {
|
||||
x: u32,
|
||||
y: u32 align(128),
|
||||
|
|
@ -475,7 +475,7 @@ pub fn zeroInit(comptime T: type, init: anytype) T {
|
|||
}
|
||||
}
|
||||
|
||||
test "zeroInit" {
|
||||
test zeroInit {
|
||||
const I = struct {
|
||||
d: f64,
|
||||
};
|
||||
|
|
@ -606,16 +606,19 @@ pub fn orderZ(comptime T: type, lhs: [*:0]const T, rhs: [*:0]const T) math.Order
|
|||
return math.order(lhs[i], rhs[i]);
|
||||
}
|
||||
|
||||
test "order and orderZ" {
|
||||
test order {
|
||||
try testing.expect(order(u8, "abcd", "bee") == .lt);
|
||||
try testing.expect(orderZ(u8, "abcd", "bee") == .lt);
|
||||
try testing.expect(order(u8, "abc", "abc") == .eq);
|
||||
try testing.expect(orderZ(u8, "abc", "abc") == .eq);
|
||||
try testing.expect(order(u8, "abc", "abc0") == .lt);
|
||||
try testing.expect(orderZ(u8, "abc", "abc0") == .lt);
|
||||
try testing.expect(order(u8, "", "") == .eq);
|
||||
try testing.expect(orderZ(u8, "", "") == .eq);
|
||||
try testing.expect(order(u8, "", "a") == .lt);
|
||||
}
|
||||
|
||||
test orderZ {
|
||||
try testing.expect(orderZ(u8, "abcd", "bee") == .lt);
|
||||
try testing.expect(orderZ(u8, "abc", "abc") == .eq);
|
||||
try testing.expect(orderZ(u8, "abc", "abc0") == .lt);
|
||||
try testing.expect(orderZ(u8, "", "") == .eq);
|
||||
try testing.expect(orderZ(u8, "", "a") == .lt);
|
||||
}
|
||||
|
||||
|
|
@ -624,7 +627,7 @@ pub fn lessThan(comptime T: type, lhs: []const T, rhs: []const T) bool {
|
|||
return order(T, lhs, rhs) == .lt;
|
||||
}
|
||||
|
||||
test "lessThan" {
|
||||
test lessThan {
|
||||
try testing.expect(lessThan(u8, "abcd", "bee"));
|
||||
try testing.expect(!lessThan(u8, "abc", "abc"));
|
||||
try testing.expect(lessThan(u8, "abc", "abc0"));
|
||||
|
|
@ -726,7 +729,7 @@ pub fn indexOfDiff(comptime T: type, a: []const T, b: []const T) ?usize {
|
|||
return if (a.len == b.len) null else shortest;
|
||||
}
|
||||
|
||||
test "indexOfDiff" {
|
||||
test indexOfDiff {
|
||||
try testing.expectEqual(indexOfDiff(u8, "one", "one"), null);
|
||||
try testing.expectEqual(indexOfDiff(u8, "one two", "one"), 3);
|
||||
try testing.expectEqual(indexOfDiff(u8, "one", "one two"), 3);
|
||||
|
|
@ -759,7 +762,7 @@ fn Span(comptime T: type) type {
|
|||
@compileError("invalid type given to std.mem.span: " ++ @typeName(T));
|
||||
}
|
||||
|
||||
test "Span" {
|
||||
test Span {
|
||||
try testing.expect(Span([*:1]u16) == [:1]u16);
|
||||
try testing.expect(Span(?[*:1]u16) == ?[:1]u16);
|
||||
try testing.expect(Span([*:1]const u8) == [:1]const u8);
|
||||
|
|
@ -793,7 +796,7 @@ pub fn span(ptr: anytype) Span(@TypeOf(ptr)) {
|
|||
}
|
||||
}
|
||||
|
||||
test "span" {
|
||||
test span {
|
||||
var array: [5]u16 = [_]u16{ 1, 2, 3, 4, 5 };
|
||||
const ptr = @as([*:3]u16, array[0..2 :3]);
|
||||
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(
|
|||
}
|
||||
}
|
||||
|
||||
test "sliceTo" {
|
||||
test sliceTo {
|
||||
try testing.expectEqualSlices(u8, "aoeu", sliceTo("aoeu", 0));
|
||||
|
||||
{
|
||||
|
|
@ -963,7 +966,7 @@ fn lenSliceTo(ptr: anytype, comptime end: std.meta.Elem(@TypeOf(ptr))) usize {
|
|||
@compileError("invalid type given to std.mem.sliceTo: " ++ @typeName(@TypeOf(ptr)));
|
||||
}
|
||||
|
||||
test "lenSliceTo" {
|
||||
test lenSliceTo {
|
||||
try testing.expect(lenSliceTo("aoeu", 0) == 4);
|
||||
|
||||
{
|
||||
|
|
@ -1018,7 +1021,7 @@ pub fn len(value: anytype) usize {
|
|||
}
|
||||
}
|
||||
|
||||
test "len" {
|
||||
test len {
|
||||
var array: [5]u16 = [_]u16{ 1, 2, 0, 4, 5 };
|
||||
const ptr = @as([*:4]u16, array[0..3 :4]);
|
||||
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
|
|||
return slice[begin..end];
|
||||
}
|
||||
|
||||
test "trim" {
|
||||
test trim {
|
||||
try testing.expectEqualSlices(u8, "foo\n ", trimLeft(u8, " foo\n ", " \n"));
|
||||
try testing.expectEqualSlices(u8, " foo", trimRight(u8, " foo\n ", " \n"));
|
||||
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,
|
|||
return null;
|
||||
}
|
||||
|
||||
test "indexOfScalarPos" {
|
||||
test indexOfScalarPos {
|
||||
const Types = [_]type{ u8, u16, u32, u64 };
|
||||
|
||||
inline for (Types) |T| {
|
||||
|
|
@ -1316,7 +1319,7 @@ pub fn indexOfNonePos(comptime T: type, slice: []const T, start_index: usize, va
|
|||
return null;
|
||||
}
|
||||
|
||||
test "indexOfNone" {
|
||||
test indexOfNone {
|
||||
try testing.expect(indexOfNone(u8, "abc123", "123").? == 0);
|
||||
try testing.expect(lastIndexOfNone(u8, "abc123", "123").? == 2);
|
||||
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
|
|||
return null;
|
||||
}
|
||||
|
||||
test "indexOf" {
|
||||
test indexOf {
|
||||
try testing.expect(indexOf(u8, "one two three four five six seven eight nine ten eleven", "three four").? == 8);
|
||||
try testing.expect(lastIndexOf(u8, "one two three four five six seven eight nine ten eleven", "three four").? == 8);
|
||||
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 {
|
|||
return found;
|
||||
}
|
||||
|
||||
test "count" {
|
||||
test count {
|
||||
try testing.expect(count(u8, "", "h") == 0);
|
||||
try testing.expect(count(u8, "h", "h") == 1);
|
||||
try testing.expect(count(u8, "hh", "h") == 2);
|
||||
|
|
@ -1565,7 +1568,7 @@ pub fn containsAtLeast(comptime T: type, haystack: []const T, expected_count: us
|
|||
return false;
|
||||
}
|
||||
|
||||
test "containsAtLeast" {
|
||||
test containsAtLeast {
|
||||
try testing.expect(containsAtLeast(u8, "aa", 0, "a"));
|
||||
try testing.expect(containsAtLeast(u8, "aa", 1, "a"));
|
||||
try testing.expect(containsAtLeast(u8, "aa", 2, "a"));
|
||||
|
|
@ -1698,6 +1701,9 @@ test readInt {
|
|||
|
||||
try testing.expect(readInt(i16, &[_]u8{ 0xff, 0xfd }, .big) == -3);
|
||||
try testing.expect(readInt(i16, &[_]u8{ 0xfc, 0xff }, .little) == -4);
|
||||
|
||||
try moreReadIntTests();
|
||||
try comptime moreReadIntTests();
|
||||
}
|
||||
|
||||
fn readPackedIntLittle(comptime T: type, bytes: []const u8, bit_offset: usize) T {
|
||||
|
|
@ -2027,7 +2033,7 @@ pub fn byteSwapAllFields(comptime S: type, ptr: *S) void {
|
|||
}
|
||||
}
|
||||
|
||||
test "byteSwapAllFields" {
|
||||
test byteSwapAllFields {
|
||||
const T = extern struct {
|
||||
f0: u8,
|
||||
f1: u16,
|
||||
|
|
@ -2136,7 +2142,7 @@ pub fn tokenizeScalar(comptime T: type, buffer: []const T, delimiter: T) TokenIt
|
|||
};
|
||||
}
|
||||
|
||||
test "tokenizeScalar" {
|
||||
test tokenizeScalar {
|
||||
var it = tokenizeScalar(u8, " abc def ghi ", ' ');
|
||||
try testing.expect(eql(u8, it.next().?, "abc"));
|
||||
try testing.expect(eql(u8, it.peek().?, "def"));
|
||||
|
|
@ -2177,7 +2183,7 @@ test "tokenizeScalar" {
|
|||
try testing.expect(it16.next() == null);
|
||||
}
|
||||
|
||||
test "tokenizeAny" {
|
||||
test tokenizeAny {
|
||||
var it = tokenizeAny(u8, "a|b,c/d e", " /,|");
|
||||
try testing.expect(eql(u8, it.next().?, "a"));
|
||||
try testing.expect(eql(u8, it.peek().?, "b"));
|
||||
|
|
@ -2205,7 +2211,7 @@ test "tokenizeAny" {
|
|||
try testing.expect(it16.next() == null);
|
||||
}
|
||||
|
||||
test "tokenizeSequence" {
|
||||
test tokenizeSequence {
|
||||
var it = tokenizeSequence(u8, "a<>b<><>c><>d><", "<>");
|
||||
try testing.expectEqualStrings("a", it.next().?);
|
||||
try testing.expectEqualStrings("b", it.peek().?);
|
||||
|
|
@ -2334,7 +2340,7 @@ pub fn splitScalar(comptime T: type, buffer: []const T, delimiter: T) SplitItera
|
|||
};
|
||||
}
|
||||
|
||||
test "splitScalar" {
|
||||
test splitScalar {
|
||||
var it = splitScalar(u8, "abc|def||ghi", '|');
|
||||
try testing.expectEqualSlices(u8, it.rest(), "abc|def||ghi");
|
||||
try testing.expectEqualSlices(u8, it.first(), "abc");
|
||||
|
|
@ -2377,7 +2383,7 @@ test "splitScalar" {
|
|||
try testing.expect(it16.next() == null);
|
||||
}
|
||||
|
||||
test "splitSequence" {
|
||||
test splitSequence {
|
||||
var it = splitSequence(u8, "a, b ,, c, d, e", ", ");
|
||||
try testing.expectEqualSlices(u8, it.first(), "a");
|
||||
try testing.expectEqualSlices(u8, it.rest(), "b ,, c, d, e");
|
||||
|
|
@ -2400,7 +2406,7 @@ test "splitSequence" {
|
|||
try testing.expect(it16.next() == null);
|
||||
}
|
||||
|
||||
test "splitAny" {
|
||||
test splitAny {
|
||||
var it = splitAny(u8, "a,b, c d e", ", ");
|
||||
try testing.expectEqualSlices(u8, it.first(), "a");
|
||||
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
|
|||
};
|
||||
}
|
||||
|
||||
test "splitBackwardsScalar" {
|
||||
test splitBackwardsScalar {
|
||||
var it = splitBackwardsScalar(u8, "abc|def||ghi", '|');
|
||||
try testing.expectEqualSlices(u8, it.rest(), "abc|def||ghi");
|
||||
try testing.expectEqualSlices(u8, it.first(), "ghi");
|
||||
|
|
@ -2575,7 +2581,7 @@ test "splitBackwardsScalar" {
|
|||
try testing.expect(it16.next() == null);
|
||||
}
|
||||
|
||||
test "splitBackwardsSequence" {
|
||||
test splitBackwardsSequence {
|
||||
var it = splitBackwardsSequence(u8, "a, b ,, c, d, e", ", ");
|
||||
try testing.expectEqualSlices(u8, it.rest(), "a, b ,, c, d, e");
|
||||
try testing.expectEqualSlices(u8, it.first(), "e");
|
||||
|
|
@ -2608,7 +2614,7 @@ test "splitBackwardsSequence" {
|
|||
try testing.expect(it16.next() == null);
|
||||
}
|
||||
|
||||
test "splitBackwardsAny" {
|
||||
test splitBackwardsAny {
|
||||
var it = splitBackwardsAny(u8, "a,b, c d e", ", ");
|
||||
try testing.expectEqualSlices(u8, it.rest(), "a,b, c d e");
|
||||
try testing.expectEqualSlices(u8, it.first(), "e");
|
||||
|
|
@ -2715,7 +2721,7 @@ pub fn window(comptime T: type, buffer: []const T, size: usize, advance: usize)
|
|||
};
|
||||
}
|
||||
|
||||
test "window" {
|
||||
test window {
|
||||
{
|
||||
// moving average size 3
|
||||
var it = window(u8, "abcdefg", 3, 1);
|
||||
|
|
@ -2841,7 +2847,7 @@ pub fn startsWith(comptime T: type, haystack: []const T, needle: []const T) bool
|
|||
return if (needle.len > haystack.len) false else eql(T, haystack[0..needle.len], needle);
|
||||
}
|
||||
|
||||
test "startsWith" {
|
||||
test startsWith {
|
||||
try testing.expect(startsWith(u8, "Bob", "Bo"));
|
||||
try testing.expect(!startsWith(u8, "Needle in haystack", "haystack"));
|
||||
}
|
||||
|
|
@ -2850,7 +2856,7 @@ pub fn endsWith(comptime T: type, haystack: []const T, needle: []const T) bool {
|
|||
return if (needle.len > haystack.len) false else eql(T, haystack[haystack.len - needle.len ..], needle);
|
||||
}
|
||||
|
||||
test "endsWith" {
|
||||
test endsWith {
|
||||
try testing.expect(endsWith(u8, "Needle in haystack", "haystack"));
|
||||
try testing.expect(!endsWith(u8, "Bob", "Bo"));
|
||||
}
|
||||
|
|
@ -3086,7 +3092,7 @@ fn joinMaybeZ(allocator: Allocator, separator: []const u8, slices: []const []con
|
|||
return buf;
|
||||
}
|
||||
|
||||
test "join" {
|
||||
test join {
|
||||
{
|
||||
const str = try join(testing.allocator, ",", &[_][]const u8{});
|
||||
defer testing.allocator.free(str);
|
||||
|
|
@ -3109,7 +3115,7 @@ test "join" {
|
|||
}
|
||||
}
|
||||
|
||||
test "joinZ" {
|
||||
test joinZ {
|
||||
{
|
||||
const str = try joinZ(testing.allocator, ",", &[_][]const u8{});
|
||||
defer testing.allocator.free(str);
|
||||
|
|
@ -3181,7 +3187,7 @@ pub fn concatMaybeSentinel(allocator: Allocator, comptime T: type, slices: []con
|
|||
return buf;
|
||||
}
|
||||
|
||||
test "concat" {
|
||||
test concat {
|
||||
{
|
||||
const str = try concat(testing.allocator, u8, &[_][]const u8{ "abc", "def", "ghi" });
|
||||
defer testing.allocator.free(str);
|
||||
|
|
@ -3219,17 +3225,13 @@ test "concat" {
|
|||
}
|
||||
}
|
||||
|
||||
test "testStringEquality" {
|
||||
test eql {
|
||||
try testing.expect(eql(u8, "abcd", "abcd"));
|
||||
try testing.expect(!eql(u8, "abcdef", "abZdef"));
|
||||
try testing.expect(!eql(u8, "abcdefg", "abcdef"));
|
||||
}
|
||||
|
||||
test "testReadInt" {
|
||||
try testReadIntImpl();
|
||||
try comptime testReadIntImpl();
|
||||
}
|
||||
fn testReadIntImpl() !void {
|
||||
fn moreReadIntTests() !void {
|
||||
{
|
||||
const bytes = [_]u8{
|
||||
0x12,
|
||||
|
|
@ -3287,7 +3289,7 @@ pub fn min(comptime T: type, slice: []const T) T {
|
|||
return best;
|
||||
}
|
||||
|
||||
test "min" {
|
||||
test min {
|
||||
try testing.expectEqual(min(u8, "abcdefg"), 'a');
|
||||
try testing.expectEqual(min(u8, "bcdefga"), 'a');
|
||||
try testing.expectEqual(min(u8, "a"), 'a');
|
||||
|
|
@ -3304,7 +3306,7 @@ pub fn max(comptime T: type, slice: []const T) T {
|
|||
return best;
|
||||
}
|
||||
|
||||
test "max" {
|
||||
test max {
|
||||
try testing.expectEqual(max(u8, "abcdefg"), 'g');
|
||||
try testing.expectEqual(max(u8, "gabcdef"), 'g');
|
||||
try testing.expectEqual(max(u8, "g"), 'g');
|
||||
|
|
@ -3357,7 +3359,7 @@ pub fn indexOfMin(comptime T: type, slice: []const T) usize {
|
|||
return index;
|
||||
}
|
||||
|
||||
test "indexOfMin" {
|
||||
test indexOfMin {
|
||||
try testing.expectEqual(indexOfMin(u8, "abcdefg"), 0);
|
||||
try testing.expectEqual(indexOfMin(u8, "bcdefga"), 6);
|
||||
try testing.expectEqual(indexOfMin(u8, "a"), 0);
|
||||
|
|
@ -3378,7 +3380,7 @@ pub fn indexOfMax(comptime T: type, slice: []const T) usize {
|
|||
return index;
|
||||
}
|
||||
|
||||
test "indexOfMax" {
|
||||
test indexOfMax {
|
||||
try testing.expectEqual(indexOfMax(u8, "abcdefg"), 6);
|
||||
try testing.expectEqual(indexOfMax(u8, "gabcdef"), 0);
|
||||
try testing.expectEqual(indexOfMax(u8, "a"), 0);
|
||||
|
|
@ -3406,7 +3408,7 @@ pub fn indexOfMinMax(comptime T: type, slice: []const T) struct { usize, usize }
|
|||
return .{ minIdx, maxIdx };
|
||||
}
|
||||
|
||||
test "indexOfMinMax" {
|
||||
test indexOfMinMax {
|
||||
try testing.expectEqual(.{ 0, 6 }, indexOfMinMax(u8, "abcdefg"));
|
||||
try testing.expectEqual(.{ 1, 0 }, indexOfMinMax(u8, "gabcdef"));
|
||||
try testing.expectEqual(.{ 0, 0 }, indexOfMinMax(u8, "a"));
|
||||
|
|
@ -3427,7 +3429,7 @@ pub fn reverse(comptime T: type, items: []T) void {
|
|||
}
|
||||
}
|
||||
|
||||
test "reverse" {
|
||||
test reverse {
|
||||
var arr = [_]i32{ 5, 3, 1, 2, 4 };
|
||||
reverse(i32, arr[0..]);
|
||||
|
||||
|
|
@ -3488,7 +3490,7 @@ pub fn reverseIterator(slice: anytype) ReverseIterator(@TypeOf(slice)) {
|
|||
return .{ .ptr = slice.ptr, .index = slice.len };
|
||||
}
|
||||
|
||||
test "reverseIterator" {
|
||||
test reverseIterator {
|
||||
{
|
||||
var it = reverseIterator("abc");
|
||||
try testing.expectEqual(@as(?u8, 'c'), it.next());
|
||||
|
|
@ -3546,7 +3548,7 @@ pub fn rotate(comptime T: type, items: []T, amount: usize) void {
|
|||
reverse(T, items);
|
||||
}
|
||||
|
||||
test "rotate" {
|
||||
test rotate {
|
||||
var arr = [_]i32{ 5, 3, 1, 2, 4 };
|
||||
rotate(i32, arr[0..], 2);
|
||||
|
||||
|
|
@ -3580,7 +3582,7 @@ pub fn replace(comptime T: type, input: []const T, needle: []const T, replacemen
|
|||
return replacements;
|
||||
}
|
||||
|
||||
test "replace" {
|
||||
test replace {
|
||||
var output: [29]u8 = undefined;
|
||||
var replacements = replace(u8, "All your base are belong to us", "base", "Zig", output[0..]);
|
||||
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 {
|
|||
defer std.testing.allocator.free(mutable);
|
||||
try testing.expect(std.mem.eql(u8, collapseRepeats(u8, mutable, elem), expected));
|
||||
}
|
||||
test "collapseRepeats" {
|
||||
test collapseRepeats {
|
||||
try testCollapseRepeats("", '/', "");
|
||||
try testCollapseRepeats("a", '/', "a");
|
||||
try testCollapseRepeats("/", '/', "/");
|
||||
|
|
@ -3677,7 +3679,7 @@ pub fn replacementSize(comptime T: type, input: []const T, needle: []const T, re
|
|||
return size;
|
||||
}
|
||||
|
||||
test "replacementSize" {
|
||||
test replacementSize {
|
||||
try testing.expect(replacementSize(u8, "All your base are belong to us", "base", "Zig") == 29);
|
||||
try testing.expect(replacementSize(u8, "Favor reading code over writing code.", "code", "") == 29);
|
||||
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
|
|||
return output;
|
||||
}
|
||||
|
||||
test "replaceOwned" {
|
||||
test replaceOwned {
|
||||
const gpa = std.testing.allocator;
|
||||
|
||||
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) {
|
|||
return @alignCast(ptr + adjust_off);
|
||||
}
|
||||
|
||||
test "alignPointer" {
|
||||
test alignPointer {
|
||||
const S = struct {
|
||||
fn checkAlign(comptime T: type, base: usize, align_to: usize, expected: usize) !void {
|
||||
const ptr: T = @ptrFromInt(base);
|
||||
|
|
@ -3850,7 +3852,7 @@ pub fn asBytes(ptr: anytype) AsBytesReturnType(@TypeOf(ptr)) {
|
|||
return @ptrCast(@alignCast(ptr));
|
||||
}
|
||||
|
||||
test "asBytes" {
|
||||
test asBytes {
|
||||
const deadbeef = @as(u32, 0xDEADBEEF);
|
||||
const deadbeef_bytes = switch (native_endian) {
|
||||
.big => "\xDE\xAD\xBE\xEF",
|
||||
|
|
@ -3910,7 +3912,7 @@ pub fn toBytes(value: anytype) [@sizeOf(@TypeOf(value))]u8 {
|
|||
return asBytes(&value).*;
|
||||
}
|
||||
|
||||
test "toBytes" {
|
||||
test toBytes {
|
||||
var my_bytes = toBytes(@as(u32, 0x12345678));
|
||||
switch (native_endian) {
|
||||
.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,
|
|||
return @ptrCast(bytes);
|
||||
}
|
||||
|
||||
test "bytesAsValue" {
|
||||
test bytesAsValue {
|
||||
const deadbeef = @as(u32, 0xDEADBEEF);
|
||||
const deadbeef_bytes = switch (native_endian) {
|
||||
.big => "\xDE\xAD\xBE\xEF",
|
||||
|
|
@ -3993,7 +3995,7 @@ test "bytesAsValue preserves pointer attributes" {
|
|||
pub fn bytesToValue(comptime T: type, bytes: anytype) T {
|
||||
return bytesAsValue(T, bytes).*;
|
||||
}
|
||||
test "bytesToValue" {
|
||||
test bytesToValue {
|
||||
const deadbeef_bytes = switch (native_endian) {
|
||||
.big => "\xDE\xAD\xBE\xEF",
|
||||
.little => "\xEF\xBE\xAD\xDE",
|
||||
|
|
@ -4021,7 +4023,7 @@ pub fn bytesAsSlice(comptime T: type, bytes: anytype) BytesAsSliceReturnType(T,
|
|||
return @as(cast_target, @ptrCast(bytes))[0..@divExact(bytes.len, @sizeOf(T))];
|
||||
}
|
||||
|
||||
test "bytesAsSlice" {
|
||||
test bytesAsSlice {
|
||||
{
|
||||
const bytes = [_]u8{ 0xDE, 0xAD, 0xBE, 0xEF };
|
||||
const slice = bytesAsSlice(u16, bytes[0..]);
|
||||
|
|
@ -4110,7 +4112,7 @@ pub fn sliceAsBytes(slice: anytype) SliceAsBytesReturnType(@TypeOf(slice)) {
|
|||
return @as(cast_target, @ptrCast(slice))[0 .. slice.len * @sizeOf(std.meta.Elem(Slice))];
|
||||
}
|
||||
|
||||
test "sliceAsBytes" {
|
||||
test sliceAsBytes {
|
||||
const bytes = [_]u16{ 0xDEAD, 0xBEEF };
|
||||
const slice = sliceAsBytes(bytes[0..]);
|
||||
try testing.expect(slice.len == 4);
|
||||
|
|
@ -4268,7 +4270,7 @@ fn doNotOptimizeAwayC(ptr: anytype) void {
|
|||
dest.* = 0;
|
||||
}
|
||||
|
||||
test "doNotOptimizeAway" {
|
||||
test doNotOptimizeAway {
|
||||
comptime doNotOptimizeAway("test");
|
||||
|
||||
doNotOptimizeAway(null);
|
||||
|
|
@ -4293,7 +4295,7 @@ test "doNotOptimizeAway" {
|
|||
doNotOptimizeAway(@as(std.builtin.Endian, .little));
|
||||
}
|
||||
|
||||
test "alignForward" {
|
||||
test alignForward {
|
||||
try testing.expect(alignForward(usize, 1, 1) == 1);
|
||||
try testing.expect(alignForward(usize, 2, 1) == 2);
|
||||
try testing.expect(alignForward(usize, 1, 2) == 2);
|
||||
|
|
@ -4362,7 +4364,7 @@ pub fn isAlignedGeneric(comptime T: type, addr: T, alignment: T) bool {
|
|||
return alignBackward(T, addr, alignment) == addr;
|
||||
}
|
||||
|
||||
test "isAligned" {
|
||||
test isAligned {
|
||||
try testing.expect(isAligned(0, 4));
|
||||
try testing.expect(isAligned(1, 1));
|
||||
try testing.expect(isAligned(2, 1));
|
||||
|
|
|
|||
|
|
@ -132,7 +132,7 @@ pub fn TrailerFlags(comptime Fields: type) type {
|
|||
};
|
||||
}
|
||||
|
||||
test "TrailerFlags" {
|
||||
test TrailerFlags {
|
||||
const Flags = TrailerFlags(struct {
|
||||
a: i32,
|
||||
b: bool,
|
||||
|
|
|
|||
|
|
@ -1379,7 +1379,7 @@ pub fn GetFinalPathNameByHandle(
|
|||
}
|
||||
}
|
||||
|
||||
test "GetFinalPathNameByHandle" {
|
||||
test GetFinalPathNameByHandle {
|
||||
if (builtin.os.tag != .windows)
|
||||
return;
|
||||
|
||||
|
|
@ -2601,7 +2601,7 @@ pub fn ntToWin32Namespace(path: []const u16) !PathSpace {
|
|||
}
|
||||
}
|
||||
|
||||
test "ntToWin32Namespace" {
|
||||
test ntToWin32Namespace {
|
||||
const L = std.unicode.utf8ToUtf16LeStringLiteral;
|
||||
|
||||
try testNtToWin32Namespace(L("UNC"), L("\\??\\UNC"));
|
||||
|
|
@ -3539,7 +3539,7 @@ pub const GUID = extern struct {
|
|||
}
|
||||
};
|
||||
|
||||
test "GUID" {
|
||||
test GUID {
|
||||
try std.testing.expectEqual(
|
||||
GUID{
|
||||
.Data1 = 0x01234567,
|
||||
|
|
|
|||
|
|
@ -215,7 +215,7 @@ pub const EnvMap = struct {
|
|||
}
|
||||
};
|
||||
|
||||
test "EnvMap" {
|
||||
test EnvMap {
|
||||
var env = EnvMap.init(testing.allocator);
|
||||
defer env.deinit();
|
||||
|
||||
|
|
@ -377,7 +377,7 @@ pub fn getEnvMap(allocator: Allocator) GetEnvMapError!EnvMap {
|
|||
}
|
||||
}
|
||||
|
||||
test "getEnvMap" {
|
||||
test getEnvMap {
|
||||
var env = try getEnvMap(testing.allocator);
|
||||
defer env.deinit();
|
||||
}
|
||||
|
|
@ -1181,7 +1181,7 @@ pub fn argsFree(allocator: Allocator, args_alloc: []const [:0]u8) void {
|
|||
return allocator.free(aligned_allocated_buf);
|
||||
}
|
||||
|
||||
test "ArgIteratorWindows" {
|
||||
test ArgIteratorWindows {
|
||||
const t = testArgIteratorWindows;
|
||||
|
||||
try t(
|
||||
|
|
|
|||
|
|
@ -430,7 +430,7 @@ pub fn binarySearch(
|
|||
return null;
|
||||
}
|
||||
|
||||
test "binarySearch" {
|
||||
test binarySearch {
|
||||
const S = struct {
|
||||
fn order_u32(context: void, lhs: u32, rhs: u32) math.Order {
|
||||
_ = context;
|
||||
|
|
@ -537,7 +537,7 @@ pub fn lowerBound(
|
|||
return left;
|
||||
}
|
||||
|
||||
test "lowerBound" {
|
||||
test lowerBound {
|
||||
const S = struct {
|
||||
fn lower_u32(context: void, lhs: u32, rhs: u32) bool {
|
||||
_ = context;
|
||||
|
|
@ -627,7 +627,7 @@ pub fn upperBound(
|
|||
return left;
|
||||
}
|
||||
|
||||
test "upperBound" {
|
||||
test upperBound {
|
||||
const S = struct {
|
||||
fn lower_u32(context: void, lhs: u32, rhs: u32) bool {
|
||||
_ = context;
|
||||
|
|
@ -712,7 +712,7 @@ pub fn equalRange(
|
|||
};
|
||||
}
|
||||
|
||||
test "equalRange" {
|
||||
test equalRange {
|
||||
const S = struct {
|
||||
fn lower_u32(context: void, lhs: u32, rhs: u32) bool {
|
||||
_ = context;
|
||||
|
|
@ -792,7 +792,7 @@ pub fn argMin(
|
|||
return smallest_index;
|
||||
}
|
||||
|
||||
test "argMin" {
|
||||
test argMin {
|
||||
try testing.expectEqual(@as(?usize, null), argMin(i32, &[_]i32{}, {}, asc_i32));
|
||||
try testing.expectEqual(@as(?usize, 0), argMin(i32, &[_]i32{1}, {}, asc_i32));
|
||||
try testing.expectEqual(@as(?usize, 0), argMin(i32, &[_]i32{ 1, 2, 3, 4, 5 }, {}, asc_i32));
|
||||
|
|
@ -812,7 +812,7 @@ pub fn min(
|
|||
return items[i];
|
||||
}
|
||||
|
||||
test "min" {
|
||||
test min {
|
||||
try testing.expectEqual(@as(?i32, null), min(i32, &[_]i32{}, {}, asc_i32));
|
||||
try testing.expectEqual(@as(?i32, 1), min(i32, &[_]i32{1}, {}, asc_i32));
|
||||
try testing.expectEqual(@as(?i32, 1), min(i32, &[_]i32{ 1, 2, 3, 4, 5 }, {}, asc_i32));
|
||||
|
|
@ -844,7 +844,7 @@ pub fn argMax(
|
|||
return biggest_index;
|
||||
}
|
||||
|
||||
test "argMax" {
|
||||
test argMax {
|
||||
try testing.expectEqual(@as(?usize, null), argMax(i32, &[_]i32{}, {}, asc_i32));
|
||||
try testing.expectEqual(@as(?usize, 0), argMax(i32, &[_]i32{1}, {}, asc_i32));
|
||||
try testing.expectEqual(@as(?usize, 4), argMax(i32, &[_]i32{ 1, 2, 3, 4, 5 }, {}, asc_i32));
|
||||
|
|
@ -864,7 +864,7 @@ pub fn max(
|
|||
return items[i];
|
||||
}
|
||||
|
||||
test "max" {
|
||||
test max {
|
||||
try testing.expectEqual(@as(?i32, null), max(i32, &[_]i32{}, {}, asc_i32));
|
||||
try testing.expectEqual(@as(?i32, 1), max(i32, &[_]i32{1}, {}, asc_i32));
|
||||
try testing.expectEqual(@as(?i32, 5), max(i32, &[_]i32{ 1, 2, 3, 4, 5 }, {}, asc_i32));
|
||||
|
|
@ -890,7 +890,7 @@ pub fn isSorted(
|
|||
return true;
|
||||
}
|
||||
|
||||
test "isSorted" {
|
||||
test isSorted {
|
||||
try testing.expect(isSorted(i32, &[_]i32{}, {}, asc_i32));
|
||||
try testing.expect(isSorted(i32, &[_]i32{10}, {}, asc_i32));
|
||||
try testing.expect(isSorted(i32, &[_]i32{ 1, 2, 3, 4, 5 }, {}, asc_i32));
|
||||
|
|
|
|||
|
|
@ -676,7 +676,7 @@ fn stripComponents(path: []const u8, count: u32) []const u8 {
|
|||
return path[i..];
|
||||
}
|
||||
|
||||
test "stripComponents" {
|
||||
test stripComponents {
|
||||
const expectEqualStrings = testing.expectEqualStrings;
|
||||
try expectEqualStrings("a/b/c", stripComponents("a/b/c", 0));
|
||||
try expectEqualStrings("b/c", stripComponents("a/b/c", 1));
|
||||
|
|
@ -685,7 +685,7 @@ test "stripComponents" {
|
|||
try expectEqualStrings("", stripComponents("a/b/c", 4));
|
||||
}
|
||||
|
||||
test "PaxIterator" {
|
||||
test PaxIterator {
|
||||
const Attr = struct {
|
||||
kind: PaxAttributeKind,
|
||||
value: []const u8 = undefined,
|
||||
|
|
|
|||
|
|
@ -242,7 +242,7 @@ fn expectApproxEqAbsInner(comptime T: type, expected: T, actual: T, tolerance: T
|
|||
}
|
||||
}
|
||||
|
||||
test "expectApproxEqAbs" {
|
||||
test expectApproxEqAbs {
|
||||
inline for ([_]type{ f16, f32, f64, f128 }) |T| {
|
||||
const pos_x: T = 12.0;
|
||||
const pos_y: T = 12.06;
|
||||
|
|
@ -278,7 +278,7 @@ fn expectApproxEqRelInner(comptime T: type, expected: T, actual: T, tolerance: T
|
|||
}
|
||||
}
|
||||
|
||||
test "expectApproxEqRel" {
|
||||
test expectApproxEqRel {
|
||||
inline for ([_]type{ f16, f32, f64, f128 }) |T| {
|
||||
const eps_value = comptime math.floatEps(T);
|
||||
const sqrt_eps_value = comptime @sqrt(eps_value);
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ pub fn sleep(nanoseconds: u64) void {
|
|||
posix.nanosleep(s, ns);
|
||||
}
|
||||
|
||||
test "sleep" {
|
||||
test sleep {
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
|
|
@ -123,7 +123,7 @@ pub fn nanoTimestamp() i128 {
|
|||
}
|
||||
}
|
||||
|
||||
test "timestamp" {
|
||||
test milliTimestamp {
|
||||
const margin = ns_per_ms * 50;
|
||||
|
||||
const time_0 = milliTimestamp();
|
||||
|
|
@ -327,7 +327,7 @@ pub const Timer = struct {
|
|||
}
|
||||
};
|
||||
|
||||
test "Timer + Instant" {
|
||||
test Timer {
|
||||
const margin = ns_per_ms * 150;
|
||||
|
||||
var timer = try Timer.start();
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ pub fn isLeapYear(year: Year) bool {
|
|||
return (0 == @mod(year, 400));
|
||||
}
|
||||
|
||||
test "isLeapYear" {
|
||||
test isLeapYear {
|
||||
try testing.expectEqual(false, isLeapYear(2095));
|
||||
try testing.expectEqual(true, isLeapYear(2096));
|
||||
try testing.expectEqual(false, isLeapYear(2100));
|
||||
|
|
|
|||
|
|
@ -907,7 +907,7 @@ pub fn fmtUtf8(utf8: []const u8) std.fmt.Formatter(formatUtf8) {
|
|||
return .{ .data = utf8 };
|
||||
}
|
||||
|
||||
test "fmtUtf8" {
|
||||
test fmtUtf8 {
|
||||
const expectFmt = testing.expectFmt;
|
||||
try expectFmt("", "{}", .{fmtUtf8("")});
|
||||
try expectFmt("foo", "{}", .{fmtUtf8("foo")});
|
||||
|
|
@ -1249,7 +1249,7 @@ pub fn utf8ToUtf16LeImpl(utf16le: []u16, utf8: []const u8, comptime surrogates:
|
|||
return dest_index;
|
||||
}
|
||||
|
||||
test "utf8ToUtf16Le" {
|
||||
test utf8ToUtf16Le {
|
||||
var utf16le: [128]u16 = undefined;
|
||||
{
|
||||
const length = try utf8ToUtf16Le(utf16le[0..], "𐐷");
|
||||
|
|
@ -1430,7 +1430,7 @@ pub fn fmtUtf16Le(utf16le: []const u16) std.fmt.Formatter(formatUtf16Le) {
|
|||
return .{ .data = utf16le };
|
||||
}
|
||||
|
||||
test "fmtUtf16Le" {
|
||||
test fmtUtf16Le {
|
||||
const expectFmt = testing.expectFmt;
|
||||
try expectFmt("", "{}", .{fmtUtf16Le(utf8ToUtf16LeStringLiteral(""))});
|
||||
try expectFmt("foo", "{}", .{fmtUtf16Le(utf8ToUtf16LeStringLiteral("foo"))});
|
||||
|
|
@ -1443,7 +1443,7 @@ test "fmtUtf16Le" {
|
|||
try expectFmt("", "{}", .{fmtUtf16Le(&[_]u16{mem.readInt(u16, "\x00\xe0", native_endian)})});
|
||||
}
|
||||
|
||||
test "utf8ToUtf16LeStringLiteral" {
|
||||
test utf8ToUtf16LeStringLiteral {
|
||||
{
|
||||
const bytes = [_:0]u16{
|
||||
mem.nativeToLittle(u16, 0x41),
|
||||
|
|
|
|||
|
|
@ -137,7 +137,7 @@ pub fn countLeaks() CountResult {
|
|||
return res;
|
||||
}
|
||||
|
||||
test "countLeaks" {
|
||||
test countLeaks {
|
||||
try testing.expectEqual(
|
||||
@as(CountResult, .{
|
||||
.leaked = 0,
|
||||
|
|
@ -167,7 +167,7 @@ pub fn countLeakBlocks() CountResult {
|
|||
return res;
|
||||
}
|
||||
|
||||
test "countLeakBlocks" {
|
||||
test countLeakBlocks {
|
||||
try testing.expectEqual(
|
||||
@as(CountResult, .{
|
||||
.leaked = 0,
|
||||
|
|
|
|||
|
|
@ -642,9 +642,8 @@ pub const Wip = struct {
|
|||
i += 1;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
test "addBundleAsRoots" {
|
||||
test addBundleAsRoots {
|
||||
var bundle = bundle: {
|
||||
var wip: ErrorBundle.Wip = undefined;
|
||||
try wip.init(std.testing.allocator);
|
||||
|
|
@ -734,4 +733,5 @@ test "addBundleAsRoots" {
|
|||
try copy.renderToWriter(.{ .ttyconf = ttyconf }, copy_buf.writer());
|
||||
|
||||
try std.testing.expectEqualStrings(bundle_buf.items, copy_buf.items);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ pub fn isPrimitive(name: []const u8) bool {
|
|||
return true;
|
||||
}
|
||||
|
||||
test "isPrimitive" {
|
||||
test isPrimitive {
|
||||
const expect = std.testing.expect;
|
||||
try expect(!isPrimitive(""));
|
||||
try expect(!isPrimitive("_"));
|
||||
|
|
|
|||
|
|
@ -148,7 +148,7 @@ pub fn parseEscapeSequence(slice: []const u8, offset: *usize) ParsedCharLiteral
|
|||
}
|
||||
}
|
||||
|
||||
test "parseCharLiteral" {
|
||||
test parseCharLiteral {
|
||||
try std.testing.expectEqual(
|
||||
ParsedCharLiteral{ .success = 'a' },
|
||||
parseCharLiteral("'a'"),
|
||||
|
|
@ -281,7 +281,7 @@ pub fn parseAlloc(allocator: std.mem.Allocator, bytes: []const u8) ParseError![]
|
|||
}
|
||||
}
|
||||
|
||||
test "parse" {
|
||||
test parseAlloc {
|
||||
const expect = std.testing.expect;
|
||||
const expectError = std.testing.expectError;
|
||||
const eql = std.mem.eql;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue