zigc: test and fix strtol and similar

This commit is contained in:
rpkak
2026-04-21 17:24:00 +02:00
committed by Andrew Kelley
parent 9df02121d0
commit 03955476ad
4 changed files with 315 additions and 21 deletions
+31 -21
View File
@@ -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 // The prefix is allowed iff base == 0 or base == base of the prefix
const real_base: u6 = if (current[0] == '0') blk: { const real_base: u6, const digits = blk: {
current += 1; 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])) { _ = std.fmt.charToDigit(current[0], real_base) catch {
current += 1; // No digits to parse. Setting errno to .INVAL is optional in this case.
break :blk 16; 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) { 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); std.c._errno().* = @intFromEnum(std.c.E.RANGE);
return std.math.maxInt(T); 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; 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); std.c._errno().* = @intFromEnum(std.c.E.RANGE);
break :blk std.math.minInt(T); 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); std.c._errno().* = @intFromEnum(std.c.E.RANGE);
break :blk std.math.maxInt(T); break :blk std.math.maxInt(T);
}; };
@@ -222,7 +233,6 @@ fn parseDigitsWithSignGenericCharacter(
var value: T = 0; var value: T = 0;
while (true) { while (true) {
const c: u8 = std.math.cast(u8, current[0]) orelse break; 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); const digit: u6 = @intCast(std.fmt.charToDigit(c, base) catch break);
defer current += 1; defer current += 1;
+7
View File
@@ -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 atol(str: [*:0]const c_char) c_long;
pub extern "c" fn atoll(str: [*:0]const c_char) c_longlong; 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( pub extern "c" fn bsearch(
key: *const anyopaque, key: *const anyopaque,
base: *const anyopaque, base: *const anyopaque,
+24
View File
@@ -1,5 +1,6 @@
const builtin = @import("builtin"); const builtin = @import("builtin");
const std = @import("std"); const std = @import("std");
const c = std.c;
test { test {
_ = @import("c/inttypes.zig"); _ = @import("c/inttypes.zig");
@@ -12,3 +13,26 @@ test {
_ = @import("c/unistd.zig"); _ = @import("c/unistd.zig");
_ = @import("c/wchar.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);
}
+253
View File
@@ -6,6 +6,9 @@ const fmt = std.fmt;
const math = std.math; const math = std.math;
const testing = std.testing; const testing = std.testing;
const expectErrno = @import("../c.zig").expectErrno;
const expectErrnoAny = @import("../c.zig").expectErrnoAny;
test "abs" { test "abs" {
if (builtin.target.cpu.arch.isMIPS64()) return error.SkipZigTest; // TODO 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)})))); 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" { test "bsearch" {
const Comparison = struct { const Comparison = struct {
pub fn compare(a: *const anyopaque, b: *const anyopaque) callconv(.c) c_int { pub fn compare(a: *const anyopaque, b: *const anyopaque) callconv(.c) c_int {