From 03955476ad9d838850392a0d5a7baa6beea3452b Mon Sep 17 00:00:00 2001 From: rpkak Date: Tue, 21 Apr 2026 17:24:00 +0200 Subject: [PATCH] zigc: test and fix strtol and similar --- lib/c/stdlib.zig | 52 ++++++---- lib/std/c.zig | 7 ++ test/c.zig | 24 +++++ test/c/stdlib.zig | 253 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 315 insertions(+), 21 deletions(-) diff --git a/lib/c/stdlib.zig b/lib/c/stdlib.zig index eed91193c3..dabcff40b4 100644 --- a/lib/c/stdlib.zig +++ b/lib/c/stdlib.zig @@ -158,29 +158,40 @@ fn stringToInteger(comptime T: type, noalias buf: [*:0]const u8, noalias maybe_e }; // The prefix is allowed iff base == 0 or base == base of the prefix - const real_base: u6 = if (current[0] == '0') blk: { - current += 1; + const real_base: u6, const digits = blk: { + if (current[0] == '0') { + if ((base == 0 or base == 16) and std.ascii.toLower(current[1]) == 'x' and std.ascii.isHex(current[2])) { + break :blk .{ 16, current[2..] }; + } else if (base == 0) { + break :blk .{ 8, current }; + } else { + break :blk .{ + switch (base) { + 0 => 10, + else => @intCast(base), + }, + current, + }; + } + } else { + const real_base: u6 = switch (base) { + 0 => 10, + else => @intCast(base), + }; - if ((base == 0 or base == 16) and std.ascii.toLower(current[0]) == 'x' and std.ascii.isHex(current[1])) { - current += 1; - break :blk 16; + _ = std.fmt.charToDigit(current[0], real_base) catch { + // No digits to parse. Setting errno to .INVAL is optional in this case. + if (maybe_end) |end| { + end.* = buf; + } + return 0; + }; + break :blk .{ real_base, current }; } - - if ((base == 0 or base == 8) and std.ascii.isDigit(current[0])) { - break :blk 8; - } - - break :blk switch (base) { - 0 => 10, - else => @intCast(base), - }; - } else switch (base) { - 0 => 10, - else => @intCast(base), }; if (@typeInfo(T).int.signedness == .unsigned) { - const result = parseDigitsWithSignGenericCharacter(T, u8, current, maybe_end, real_base, .pos) catch { + const result = parseDigitsWithSignGenericCharacter(T, u8, digits, maybe_end, real_base, .pos) catch { std.c._errno().* = @intFromEnum(std.c.E.RANGE); return std.math.maxInt(T); }; @@ -188,12 +199,12 @@ fn stringToInteger(comptime T: type, noalias buf: [*:0]const u8, noalias maybe_e return if (negative) -%result else result; } - if (negative) return parseDigitsWithSignGenericCharacter(T, u8, current, maybe_end, real_base, .neg) catch blk: { + if (negative) return parseDigitsWithSignGenericCharacter(T, u8, digits, maybe_end, real_base, .neg) catch blk: { std.c._errno().* = @intFromEnum(std.c.E.RANGE); break :blk std.math.minInt(T); }; - return parseDigitsWithSignGenericCharacter(T, u8, current, maybe_end, real_base, .pos) catch blk: { + return parseDigitsWithSignGenericCharacter(T, u8, digits, maybe_end, real_base, .pos) catch blk: { std.c._errno().* = @intFromEnum(std.c.E.RANGE); break :blk std.math.maxInt(T); }; @@ -222,7 +233,6 @@ fn parseDigitsWithSignGenericCharacter( var value: T = 0; while (true) { const c: u8 = std.math.cast(u8, current[0]) orelse break; - if (!std.ascii.isAlphanumeric(c)) break; const digit: u6 = @intCast(std.fmt.charToDigit(c, base) catch break); defer current += 1; diff --git a/lib/std/c.zig b/lib/std/c.zig index 349b38f612..387f4a8791 100644 --- a/lib/std/c.zig +++ b/lib/std/c.zig @@ -11166,6 +11166,13 @@ pub extern "c" fn atoi(str: [*:0]const c_char) c_int; pub extern "c" fn atol(str: [*:0]const c_char) c_long; pub extern "c" fn atoll(str: [*:0]const c_char) c_longlong; +pub extern "c" fn strtol(noalias str: [*:0]const c_char, noalias str_end: ?*[*:0]c_char, base: c_int) callconv(.c) c_long; +pub extern "c" fn strtoll(noalias str: [*:0]const c_char, noalias str_end: ?*[*:0]c_char, base: c_int) callconv(.c) c_longlong; +pub extern "c" fn strtoul(noalias str: [*:0]const c_char, noalias str_end: ?*[*:0]c_char, base: c_int) callconv(.c) c_ulong; +pub extern "c" fn strtoull(noalias str: [*:0]const c_char, noalias str_end: ?*[*:0]c_char, base: c_int) callconv(.c) c_ulonglong; +pub extern "c" fn strtoimax(noalias str: [*:0]const c_char, noalias str_end: ?*[*:0]c_char, base: c_int) callconv(.c) intmax_t; +pub extern "c" fn strtoumax(noalias str: [*:0]const c_char, noalias str_end: ?*[*:0]c_char, base: c_int) callconv(.c) uintmax_t; + pub extern "c" fn bsearch( key: *const anyopaque, base: *const anyopaque, diff --git a/test/c.zig b/test/c.zig index bf2c8a5fb2..f3cecb6c34 100644 --- a/test/c.zig +++ b/test/c.zig @@ -1,5 +1,6 @@ const builtin = @import("builtin"); const std = @import("std"); +const c = std.c; test { _ = @import("c/inttypes.zig"); @@ -12,3 +13,26 @@ test { _ = @import("c/unistd.zig"); _ = @import("c/wchar.zig"); } + +pub fn expectErrno(expected_errno: c.E) !void { + try std.testing.expectEqual(expected_errno, @as(c.E, @enumFromInt(c._errno().*))); + c._errno().* = @intFromEnum(c.E.SUCCESS); +} + +pub fn expectErrnoAny(expected_errnos: []const c.E) !void { + const errno = c._errno().*; + for (expected_errnos) |expected_errno| { + if (errno == @intFromEnum(expected_errno)) break; + } else { + var buffer: [64]u8 = undefined; + const stderr = std.debug.lockStderr(&buffer); + defer std.debug.unlockStderr(); + try stderr.file_writer.interface.print("expected one of {t}", .{expected_errnos[0]}); + for (expected_errnos[1..]) |expected_errno| { + try stderr.file_writer.interface.print(", {t}", .{expected_errno}); + } + try stderr.file_writer.interface.print(", found {t}\n", .{@as(c.E, @enumFromInt(errno))}); + return error.TestExpectedEqual; + } + c._errno().* = @intFromEnum(c.E.SUCCESS); +} diff --git a/test/c/stdlib.zig b/test/c/stdlib.zig index 11ac5de2bc..589a8ef59a 100644 --- a/test/c/stdlib.zig +++ b/test/c/stdlib.zig @@ -6,6 +6,9 @@ const fmt = std.fmt; const math = std.math; const testing = std.testing; +const expectErrno = @import("../c.zig").expectErrno; +const expectErrnoAny = @import("../c.zig").expectErrnoAny; + test "abs" { if (builtin.target.cpu.arch.isMIPS64()) return error.SkipZigTest; // TODO @@ -97,6 +100,256 @@ test "atoll" { try testing.expectEqual(math.minInt(c_longlong), c.atoll(@ptrCast(fmt.comptimePrint("{d}", .{math.minInt(c_longlong)})))); } +fn testStrToLLikeFunction( + func: anytype, + str: [*:0]const c_char, + base: c_int, + expected: comptime_int, + expected_len: ?usize, + expected_errno: c.E, +) !void { + var end_ptr: [*:0]c_char = undefined; + try testing.expectEqual(expected, func(str, if (expected_len == null) null else &end_ptr, base)); + if (expected_len) |len| try testing.expectEqual(len, end_ptr - str); + try expectErrno(expected_errno); +} + +fn testStrToLLikeFunctionAnyErrno( + func: anytype, + str: [*:0]const c_char, + base: c_int, + expected: comptime_int, + expected_len: ?usize, + expected_errnos: []const c.E, +) !void { + var end_ptr: [*:0]c_char = undefined; + try testing.expectEqual(expected, func(str, if (expected_len == null) null else &end_ptr, base)); + if (expected_len) |len| try testing.expectEqual(len, end_ptr - str); + try expectErrnoAny(expected_errnos); +} + +test "strtol" { + c._errno().* = @intFromEnum(c.E.SUCCESS); + try testStrToLLikeFunctionAnyErrno(c.strtol, @ptrCast("stop42true"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunction(c.strtol, @ptrCast("42true"), 0, 42, 2, .SUCCESS); + try testStrToLLikeFunction(c.strtol, @ptrCast("-01"), 0, -1, 3, .SUCCESS); + try testStrToLLikeFunction(c.strtol, @ptrCast("+001"), 0, 1, 4, .SUCCESS); + try testStrToLLikeFunction(c.strtol, @ptrCast(" 100"), 0, 100, 15, .SUCCESS); + try testStrToLLikeFunction(c.strtol, @ptrCast("000000000000500"), 0, 0o500, 15, .SUCCESS); + try testStrToLLikeFunction(c.strtol, @ptrCast("000000000000500"), 10, 500, 15, .SUCCESS); + try testStrToLLikeFunction(c.strtol, @ptrCast(" 0500"), 0, 0o500, 15, .SUCCESS); + try testStrToLLikeFunction(c.strtol, @ptrCast("0000000000001111_0000"), 10, 1111, 16, .SUCCESS); + try testStrToLLikeFunction(c.strtol, @ptrCast(" 1111_0000"), 0, 1111, 16, .SUCCESS); + try testStrToLLikeFunction(c.strtol, @ptrCast("0xAA"), 0, 0xAA, 4, .SUCCESS); + try testStrToLLikeFunction(c.strtol, @ptrCast("0xAA"), 10, 0, 1, .SUCCESS); + try testStrToLLikeFunction(c.strtol, @ptrCast("0xAA"), 16, 0xAA, 4, .SUCCESS); + try testStrToLLikeFunction(c.strtol, @ptrCast("0xAA"), 36, 43138, 4, .SUCCESS); + try testStrToLLikeFunction(c.strtol, @ptrCast("700B"), 0, 700, 3, .SUCCESS); + try testStrToLLikeFunction(c.strtol, @ptrCast("32453more"), 0, 32453, 5, .SUCCESS); + try testStrToLLikeFunction(c.strtol, @ptrCast(fmt.comptimePrint("{d}", .{math.maxInt(c_long)})), 0, math.maxInt(c_long), null, .SUCCESS); + try testStrToLLikeFunction(c.strtol, @ptrCast(fmt.comptimePrint("{d}", .{math.minInt(c_long)})), 0, math.minInt(c_long), null, .SUCCESS); + try testStrToLLikeFunction(c.strtol, @ptrCast(fmt.comptimePrint("{d}", .{math.maxInt(c_long) + 1})), 0, math.maxInt(c_long), null, .RANGE); + try testStrToLLikeFunction(c.strtol, @ptrCast(fmt.comptimePrint("{d}", .{math.minInt(c_long) - 1})), 0, math.minInt(c_long), null, .RANGE); + try testStrToLLikeFunctionAnyErrno(c.strtol, @ptrCast(""), 0, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunctionAnyErrno(c.strtol, @ptrCast(""), 12, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunctionAnyErrno(c.strtol, @ptrCast("-"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunctionAnyErrno(c.strtol, @ptrCast(" -"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunctionAnyErrno(c.strtol, @ptrCast(" "), 0, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunction(c.strtol, @ptrCast("0"), 0, 0, 1, .SUCCESS); + try testStrToLLikeFunction(c.strtol, @ptrCast("0"), 8, 0, 1, .SUCCESS); + try testStrToLLikeFunction(c.strtol, @ptrCast("09"), 0, 0, 1, .SUCCESS); + try testStrToLLikeFunction(c.strtol, @ptrCast("09"), 10, 9, 2, .SUCCESS); + if (builtin.os.tag != .windows) + try testStrToLLikeFunction(c.strtol, @ptrCast("0x"), 0, 0, 1, .SUCCESS); + try testStrToLLikeFunction(c.strtol, @ptrCast("1"), 37, 0, null, .INVAL); + try testStrToLLikeFunction(c.strtol, @ptrCast("1"), 1, 0, null, .INVAL); + try testStrToLLikeFunction(c.strtol, @ptrCast("1"), -1, 0, null, .INVAL); +} + +test "strtoll" { + c._errno().* = @intFromEnum(c.E.SUCCESS); + try testStrToLLikeFunctionAnyErrno(c.strtoll, @ptrCast("stop42true"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunction(c.strtoll, @ptrCast("42true"), 0, 42, 2, .SUCCESS); + try testStrToLLikeFunction(c.strtoll, @ptrCast("-01"), 0, -1, 3, .SUCCESS); + try testStrToLLikeFunction(c.strtoll, @ptrCast("+001"), 0, 1, 4, .SUCCESS); + try testStrToLLikeFunction(c.strtoll, @ptrCast(" 100"), 0, 100, 15, .SUCCESS); + try testStrToLLikeFunction(c.strtoll, @ptrCast("000000000000500"), 0, 0o500, 15, .SUCCESS); + try testStrToLLikeFunction(c.strtoll, @ptrCast("000000000000500"), 10, 500, 15, .SUCCESS); + try testStrToLLikeFunction(c.strtoll, @ptrCast(" 0500"), 0, 0o500, 15, .SUCCESS); + try testStrToLLikeFunction(c.strtoll, @ptrCast("0000000000001111_0000"), 10, 1111, 16, .SUCCESS); + try testStrToLLikeFunction(c.strtoll, @ptrCast(" 1111_0000"), 0, 1111, 16, .SUCCESS); + try testStrToLLikeFunction(c.strtoll, @ptrCast("0xAA"), 0, 0xAA, 4, .SUCCESS); + try testStrToLLikeFunction(c.strtoll, @ptrCast("0xAA"), 10, 0, 1, .SUCCESS); + try testStrToLLikeFunction(c.strtoll, @ptrCast("0xAA"), 16, 0xAA, 4, .SUCCESS); + try testStrToLLikeFunction(c.strtoll, @ptrCast("0xAA"), 36, 43138, 4, .SUCCESS); + try testStrToLLikeFunction(c.strtoll, @ptrCast("700B"), 0, 700, 3, .SUCCESS); + try testStrToLLikeFunction(c.strtoll, @ptrCast("32453more"), 0, 32453, 5, .SUCCESS); + try testStrToLLikeFunction(c.strtoll, @ptrCast(fmt.comptimePrint("{d}", .{math.maxInt(c_longlong)})), 0, math.maxInt(c_longlong), null, .SUCCESS); + try testStrToLLikeFunction(c.strtoll, @ptrCast(fmt.comptimePrint("{d}", .{math.minInt(c_longlong)})), 0, math.minInt(c_longlong), null, .SUCCESS); + try testStrToLLikeFunction(c.strtoll, @ptrCast(fmt.comptimePrint("{d}", .{math.maxInt(c_longlong) + 1})), 0, math.maxInt(c_longlong), null, .RANGE); + try testStrToLLikeFunction(c.strtoll, @ptrCast(fmt.comptimePrint("{d}", .{math.minInt(c_longlong) - 1})), 0, math.minInt(c_longlong), null, .RANGE); + try testStrToLLikeFunctionAnyErrno(c.strtoll, @ptrCast(""), 0, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunctionAnyErrno(c.strtoll, @ptrCast(""), 12, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunctionAnyErrno(c.strtoll, @ptrCast("-"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunctionAnyErrno(c.strtoll, @ptrCast(" -"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunctionAnyErrno(c.strtoll, @ptrCast(" "), 0, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunction(c.strtoll, @ptrCast("0"), 0, 0, 1, .SUCCESS); + try testStrToLLikeFunction(c.strtoll, @ptrCast("0"), 8, 0, 1, .SUCCESS); + try testStrToLLikeFunction(c.strtoll, @ptrCast("09"), 0, 0, 1, .SUCCESS); + try testStrToLLikeFunction(c.strtoll, @ptrCast("09"), 10, 9, 2, .SUCCESS); + if (builtin.os.tag != .windows) + try testStrToLLikeFunction(c.strtoll, @ptrCast("0x"), 0, 0, 1, .SUCCESS); + try testStrToLLikeFunction(c.strtoll, @ptrCast("1"), 37, 0, null, .INVAL); + try testStrToLLikeFunction(c.strtoll, @ptrCast("1"), 1, 0, null, .INVAL); + try testStrToLLikeFunction(c.strtoll, @ptrCast("1"), -1, 0, null, .INVAL); +} + +test "strtoul" { + c._errno().* = @intFromEnum(c.E.SUCCESS); + try testStrToLLikeFunctionAnyErrno(c.strtoul, @ptrCast("stop42true"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunction(c.strtoul, @ptrCast("42true"), 0, 42, 2, .SUCCESS); + try testStrToLLikeFunction(c.strtoul, @ptrCast("-01"), 0, math.maxInt(c_ulong), 3, .SUCCESS); + try testStrToLLikeFunction(c.strtoul, @ptrCast("+001"), 0, 1, 4, .SUCCESS); + try testStrToLLikeFunction(c.strtoul, @ptrCast(" 100"), 0, 100, 15, .SUCCESS); + try testStrToLLikeFunction(c.strtoul, @ptrCast("000000000000500"), 0, 0o500, 15, .SUCCESS); + try testStrToLLikeFunction(c.strtoul, @ptrCast("000000000000500"), 10, 500, 15, .SUCCESS); + try testStrToLLikeFunction(c.strtoul, @ptrCast(" 0500"), 0, 0o500, 15, .SUCCESS); + try testStrToLLikeFunction(c.strtoul, @ptrCast("0000000000001111_0000"), 10, 1111, 16, .SUCCESS); + try testStrToLLikeFunction(c.strtoul, @ptrCast(" 1111_0000"), 0, 1111, 16, .SUCCESS); + try testStrToLLikeFunction(c.strtoul, @ptrCast("0xAA"), 0, 0xAA, 4, .SUCCESS); + try testStrToLLikeFunction(c.strtoul, @ptrCast("0xAA"), 10, 0, 1, .SUCCESS); + try testStrToLLikeFunction(c.strtoul, @ptrCast("0xAA"), 16, 0xAA, 4, .SUCCESS); + try testStrToLLikeFunction(c.strtoul, @ptrCast("0xAA"), 36, 43138, 4, .SUCCESS); + try testStrToLLikeFunction(c.strtoul, @ptrCast("700B"), 0, 700, 3, .SUCCESS); + try testStrToLLikeFunction(c.strtoul, @ptrCast("32453more"), 0, 32453, 5, .SUCCESS); + try testStrToLLikeFunction(c.strtoul, @ptrCast(fmt.comptimePrint("{d}", .{math.maxInt(c_ulong)})), 0, math.maxInt(c_ulong), null, .SUCCESS); + try testStrToLLikeFunction(c.strtoul, @ptrCast(fmt.comptimePrint("{d}", .{math.maxInt(c_ulong) + 1})), 0, math.maxInt(c_ulong), null, .RANGE); + try testStrToLLikeFunctionAnyErrno(c.strtoul, @ptrCast(""), 0, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunctionAnyErrno(c.strtoul, @ptrCast(""), 12, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunctionAnyErrno(c.strtoul, @ptrCast("-"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunctionAnyErrno(c.strtoul, @ptrCast(" -"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunctionAnyErrno(c.strtoul, @ptrCast(" "), 0, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunction(c.strtoul, @ptrCast("0"), 0, 0, 1, .SUCCESS); + try testStrToLLikeFunction(c.strtoul, @ptrCast("0"), 8, 0, 1, .SUCCESS); + try testStrToLLikeFunction(c.strtoul, @ptrCast("09"), 0, 0, 1, .SUCCESS); + try testStrToLLikeFunction(c.strtoul, @ptrCast("09"), 10, 9, 2, .SUCCESS); + if (builtin.os.tag != .windows) + try testStrToLLikeFunction(c.strtoul, @ptrCast("0x"), 0, 0, 1, .SUCCESS); + try testStrToLLikeFunction(c.strtoul, @ptrCast("1"), 37, 0, null, .INVAL); + try testStrToLLikeFunction(c.strtoul, @ptrCast("1"), 1, 0, null, .INVAL); + try testStrToLLikeFunction(c.strtoul, @ptrCast("1"), -1, 0, null, .INVAL); +} + +test "strtoull" { + c._errno().* = @intFromEnum(c.E.SUCCESS); + try testStrToLLikeFunctionAnyErrno(c.strtoull, @ptrCast("stop42true"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunction(c.strtoull, @ptrCast("42true"), 0, 42, 2, .SUCCESS); + try testStrToLLikeFunction(c.strtoull, @ptrCast("-01"), 0, math.maxInt(c_ulonglong), 3, .SUCCESS); + try testStrToLLikeFunction(c.strtoull, @ptrCast("+001"), 0, 1, 4, .SUCCESS); + try testStrToLLikeFunction(c.strtoull, @ptrCast(" 100"), 0, 100, 15, .SUCCESS); + try testStrToLLikeFunction(c.strtoull, @ptrCast("000000000000500"), 0, 0o500, 15, .SUCCESS); + try testStrToLLikeFunction(c.strtoull, @ptrCast("000000000000500"), 10, 500, 15, .SUCCESS); + try testStrToLLikeFunction(c.strtoull, @ptrCast(" 0500"), 0, 0o500, 15, .SUCCESS); + try testStrToLLikeFunction(c.strtoull, @ptrCast("0000000000001111_0000"), 10, 1111, 16, .SUCCESS); + try testStrToLLikeFunction(c.strtoull, @ptrCast(" 1111_0000"), 0, 1111, 16, .SUCCESS); + try testStrToLLikeFunction(c.strtoull, @ptrCast("0xAA"), 0, 0xAA, 4, .SUCCESS); + try testStrToLLikeFunction(c.strtoull, @ptrCast("0xAA"), 10, 0, 1, .SUCCESS); + try testStrToLLikeFunction(c.strtoull, @ptrCast("0xAA"), 16, 0xAA, 4, .SUCCESS); + try testStrToLLikeFunction(c.strtoull, @ptrCast("0xAA"), 36, 43138, 4, .SUCCESS); + try testStrToLLikeFunction(c.strtoull, @ptrCast("700B"), 0, 700, 3, .SUCCESS); + try testStrToLLikeFunction(c.strtoull, @ptrCast("32453more"), 0, 32453, 5, .SUCCESS); + try testStrToLLikeFunction(c.strtoull, @ptrCast(fmt.comptimePrint("{d}", .{math.maxInt(c_ulonglong)})), 0, math.maxInt(c_ulonglong), null, .SUCCESS); + try testStrToLLikeFunction(c.strtoull, @ptrCast(fmt.comptimePrint("{d}", .{math.maxInt(c_ulonglong) + 1})), 0, math.maxInt(c_ulonglong), null, .RANGE); + try testStrToLLikeFunctionAnyErrno(c.strtoull, @ptrCast(""), 0, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunctionAnyErrno(c.strtoull, @ptrCast(""), 12, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunctionAnyErrno(c.strtoull, @ptrCast("-"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunctionAnyErrno(c.strtoull, @ptrCast(" -"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunctionAnyErrno(c.strtoull, @ptrCast(" "), 0, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunction(c.strtoull, @ptrCast("0"), 0, 0, 1, .SUCCESS); + try testStrToLLikeFunction(c.strtoull, @ptrCast("0"), 8, 0, 1, .SUCCESS); + try testStrToLLikeFunction(c.strtoull, @ptrCast("09"), 0, 0, 1, .SUCCESS); + try testStrToLLikeFunction(c.strtoull, @ptrCast("09"), 10, 9, 2, .SUCCESS); + if (builtin.os.tag != .windows) + try testStrToLLikeFunction(c.strtoull, @ptrCast("0x"), 0, 0, 1, .SUCCESS); + try testStrToLLikeFunction(c.strtoull, @ptrCast("1"), 37, 0, null, .INVAL); + try testStrToLLikeFunction(c.strtoull, @ptrCast("1"), 1, 0, null, .INVAL); + try testStrToLLikeFunction(c.strtoull, @ptrCast("1"), -1, 0, null, .INVAL); +} + +test "strtoimax" { + c._errno().* = @intFromEnum(c.E.SUCCESS); + try testStrToLLikeFunctionAnyErrno(c.strtoimax, @ptrCast("stop42true"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunction(c.strtoimax, @ptrCast("42true"), 0, 42, 2, .SUCCESS); + try testStrToLLikeFunction(c.strtoimax, @ptrCast("-01"), 0, -1, 3, .SUCCESS); + try testStrToLLikeFunction(c.strtoimax, @ptrCast("+001"), 0, 1, 4, .SUCCESS); + try testStrToLLikeFunction(c.strtoimax, @ptrCast(" 100"), 0, 100, 15, .SUCCESS); + try testStrToLLikeFunction(c.strtoimax, @ptrCast("000000000000500"), 0, 0o500, 15, .SUCCESS); + try testStrToLLikeFunction(c.strtoimax, @ptrCast("000000000000500"), 10, 500, 15, .SUCCESS); + try testStrToLLikeFunction(c.strtoimax, @ptrCast(" 0500"), 0, 0o500, 15, .SUCCESS); + try testStrToLLikeFunction(c.strtoimax, @ptrCast("0000000000001111_0000"), 10, 1111, 16, .SUCCESS); + try testStrToLLikeFunction(c.strtoimax, @ptrCast(" 1111_0000"), 0, 1111, 16, .SUCCESS); + try testStrToLLikeFunction(c.strtoimax, @ptrCast("0xAA"), 0, 0xAA, 4, .SUCCESS); + try testStrToLLikeFunction(c.strtoimax, @ptrCast("0xAA"), 10, 0, 1, .SUCCESS); + try testStrToLLikeFunction(c.strtoimax, @ptrCast("0xAA"), 16, 0xAA, 4, .SUCCESS); + try testStrToLLikeFunction(c.strtoimax, @ptrCast("0xAA"), 36, 43138, 4, .SUCCESS); + try testStrToLLikeFunction(c.strtoimax, @ptrCast("700B"), 0, 700, 3, .SUCCESS); + try testStrToLLikeFunction(c.strtoimax, @ptrCast("32453more"), 0, 32453, 5, .SUCCESS); + try testStrToLLikeFunction(c.strtoimax, @ptrCast(fmt.comptimePrint("{d}", .{math.maxInt(c.intmax_t)})), 0, math.maxInt(c.intmax_t), null, .SUCCESS); + try testStrToLLikeFunction(c.strtoimax, @ptrCast(fmt.comptimePrint("{d}", .{math.minInt(c.intmax_t)})), 0, math.minInt(c.intmax_t), null, .SUCCESS); + try testStrToLLikeFunction(c.strtoimax, @ptrCast(fmt.comptimePrint("{d}", .{math.maxInt(c.intmax_t) + 1})), 0, math.maxInt(c.intmax_t), null, .RANGE); + try testStrToLLikeFunction(c.strtoimax, @ptrCast(fmt.comptimePrint("{d}", .{math.minInt(c.intmax_t) - 1})), 0, math.minInt(c.intmax_t), null, .RANGE); + try testStrToLLikeFunctionAnyErrno(c.strtoimax, @ptrCast(""), 0, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunctionAnyErrno(c.strtoimax, @ptrCast(""), 12, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunctionAnyErrno(c.strtoimax, @ptrCast("-"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunctionAnyErrno(c.strtoimax, @ptrCast(" -"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunctionAnyErrno(c.strtoimax, @ptrCast(" "), 0, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunction(c.strtoimax, @ptrCast("0"), 0, 0, 1, .SUCCESS); + try testStrToLLikeFunction(c.strtoimax, @ptrCast("0"), 8, 0, 1, .SUCCESS); + try testStrToLLikeFunction(c.strtoimax, @ptrCast("09"), 0, 0, 1, .SUCCESS); + try testStrToLLikeFunction(c.strtoimax, @ptrCast("09"), 10, 9, 2, .SUCCESS); + if (builtin.os.tag != .windows) + try testStrToLLikeFunction(c.strtoimax, @ptrCast("0x"), 0, 0, 1, .SUCCESS); + try testStrToLLikeFunction(c.strtoimax, @ptrCast("1"), 37, 0, null, .INVAL); + try testStrToLLikeFunction(c.strtoimax, @ptrCast("1"), 1, 0, null, .INVAL); + try testStrToLLikeFunction(c.strtoimax, @ptrCast("1"), -1, 0, null, .INVAL); +} + +test "strtoumax" { + c._errno().* = @intFromEnum(c.E.SUCCESS); + try testStrToLLikeFunctionAnyErrno(c.strtoumax, @ptrCast("stop42true"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunction(c.strtoumax, @ptrCast("42true"), 0, 42, 2, .SUCCESS); + try testStrToLLikeFunction(c.strtoumax, @ptrCast("-01"), 0, math.maxInt(c.uintmax_t), 3, .SUCCESS); + try testStrToLLikeFunction(c.strtoumax, @ptrCast("+001"), 0, 1, 4, .SUCCESS); + try testStrToLLikeFunction(c.strtoumax, @ptrCast(" 100"), 0, 100, 15, .SUCCESS); + try testStrToLLikeFunction(c.strtoumax, @ptrCast("000000000000500"), 0, 0o500, 15, .SUCCESS); + try testStrToLLikeFunction(c.strtoumax, @ptrCast("000000000000500"), 10, 500, 15, .SUCCESS); + try testStrToLLikeFunction(c.strtoumax, @ptrCast(" 0500"), 0, 0o500, 15, .SUCCESS); + try testStrToLLikeFunction(c.strtoumax, @ptrCast("0000000000001111_0000"), 10, 1111, 16, .SUCCESS); + try testStrToLLikeFunction(c.strtoumax, @ptrCast(" 1111_0000"), 0, 1111, 16, .SUCCESS); + try testStrToLLikeFunction(c.strtoumax, @ptrCast("0xAA"), 0, 0xAA, 4, .SUCCESS); + try testStrToLLikeFunction(c.strtoumax, @ptrCast("0xAA"), 10, 0, 1, .SUCCESS); + try testStrToLLikeFunction(c.strtoumax, @ptrCast("0xAA"), 16, 0xAA, 4, .SUCCESS); + try testStrToLLikeFunction(c.strtoumax, @ptrCast("0xAA"), 36, 43138, 4, .SUCCESS); + try testStrToLLikeFunction(c.strtoumax, @ptrCast("700B"), 0, 700, 3, .SUCCESS); + try testStrToLLikeFunction(c.strtoumax, @ptrCast("32453more"), 0, 32453, 5, .SUCCESS); + try testStrToLLikeFunction(c.strtoumax, @ptrCast(fmt.comptimePrint("{d}", .{math.maxInt(c_ulonglong)})), 0, math.maxInt(c_ulonglong), null, .SUCCESS); + try testStrToLLikeFunction(c.strtoumax, @ptrCast(fmt.comptimePrint("{d}", .{math.maxInt(c_ulonglong) + 1})), 0, math.maxInt(c_ulonglong), null, .RANGE); + try testStrToLLikeFunctionAnyErrno(c.strtoumax, @ptrCast(""), 0, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunctionAnyErrno(c.strtoumax, @ptrCast(""), 12, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunctionAnyErrno(c.strtoumax, @ptrCast("-"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunctionAnyErrno(c.strtoumax, @ptrCast(" -"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunctionAnyErrno(c.strtoumax, @ptrCast(" "), 0, 0, 0, &.{ .SUCCESS, .INVAL }); + try testStrToLLikeFunction(c.strtoumax, @ptrCast("0"), 0, 0, 1, .SUCCESS); + try testStrToLLikeFunction(c.strtoumax, @ptrCast("0"), 8, 0, 1, .SUCCESS); + try testStrToLLikeFunction(c.strtoumax, @ptrCast("09"), 0, 0, 1, .SUCCESS); + try testStrToLLikeFunction(c.strtoumax, @ptrCast("09"), 10, 9, 2, .SUCCESS); + if (builtin.os.tag != .windows) + try testStrToLLikeFunction(c.strtoumax, @ptrCast("0x"), 0, 0, 1, .SUCCESS); + try testStrToLLikeFunction(c.strtoumax, @ptrCast("1"), 37, 0, null, .INVAL); + try testStrToLLikeFunction(c.strtoumax, @ptrCast("1"), 1, 0, null, .INVAL); + try testStrToLLikeFunction(c.strtoumax, @ptrCast("1"), -1, 0, null, .INVAL); +} + test "bsearch" { const Comparison = struct { pub fn compare(a: *const anyopaque, b: *const anyopaque) callconv(.c) c_int {