| 1 | const builtin = @import("builtin"); |
| 2 | const std = @import("std"); |
| 3 | |
| 4 | const c = std.c; |
| 5 | const fmt = std.fmt; |
| 6 | const math = std.math; |
| 7 | const testing = std.testing; |
| 8 | |
| 9 | const expectErrno = @import("../c.zig").expectErrno; |
| 10 | const expectErrnoAny = @import("../c.zig").expectErrnoAny; |
| 11 | |
| 12 | test "abs" { |
| 13 | if (builtin.target.cpu.arch.isMIPS64()) return error.SkipZigTest; // TODO |
| 14 | |
| 15 | const val: c_int = -10; |
| 16 | try testing.expectEqual(10, c.abs(val)); |
| 17 | } |
| 18 | |
| 19 | test "labs" { |
| 20 | if (builtin.target.cpu.arch.isMIPS64() and @sizeOf(usize) == 4) return error.SkipZigTest; // TODO |
| 21 | |
| 22 | const val: c_long = -10; |
| 23 | try testing.expectEqual(10, c.labs(val)); |
| 24 | } |
| 25 | |
| 26 | test "llabs" { |
| 27 | const val: c_longlong = -10; |
| 28 | try testing.expectEqual(10, c.llabs(val)); |
| 29 | } |
| 30 | |
| 31 | test "div" { |
| 32 | if (builtin.target.cpu.arch.isLoongArch()) return error.SkipZigTest; // TODO |
| 33 | if (builtin.target.cpu.arch.isMIPS64()) return error.SkipZigTest; // TODO |
| 34 | if (builtin.target.cpu.arch.isPowerPC()) return error.SkipZigTest; // TODO |
| 35 | if (builtin.target.cpu.arch == .s390x) return error.SkipZigTest; // TODO |
| 36 | if (builtin.target.cpu.arch == .x86 and builtin.target.os.tag == .windows) return error.SkipZigTest; // TODO |
| 37 | |
| 38 | const expected: c.div_t = .{ .quot = 5, .rem = 5 }; |
| 39 | try testing.expectEqual(expected, c.div(55, 10)); |
| 40 | } |
| 41 | |
| 42 | test "ldiv" { |
| 43 | if (builtin.target.cpu.arch.isMIPS64() and @sizeOf(usize) == 4) return error.SkipZigTest; // TODO |
| 44 | if (builtin.target.cpu.arch.isPowerPC32()) return error.SkipZigTest; // TODO |
| 45 | if (builtin.target.cpu.arch == .s390x) return error.SkipZigTest; // TODO |
| 46 | if (builtin.target.cpu.arch == .x86 and builtin.target.os.tag == .windows) return error.SkipZigTest; // TODO |
| 47 | |
| 48 | const expected: c.ldiv_t = .{ .quot = -6, .rem = 2 }; |
| 49 | try testing.expectEqual(expected, c.ldiv(38, -6)); |
| 50 | } |
| 51 | |
| 52 | test "lldiv" { |
| 53 | if (builtin.target.cpu.arch.isPowerPC32()) return error.SkipZigTest; // TODO |
| 54 | if (builtin.target.cpu.arch == .s390x) return error.SkipZigTest; // TODO |
| 55 | |
| 56 | const expected: c.lldiv_t = .{ .quot = 1, .rem = 2 }; |
| 57 | try testing.expectEqual(expected, c.lldiv(5, 3)); |
| 58 | } |
| 59 | |
| 60 | test "atoi" { |
| 61 | try testing.expectEqual(0, c.atoi(@ptrCast("stop42true"))); |
| 62 | try testing.expectEqual(42, c.atoi(@ptrCast("42true"))); |
| 63 | try testing.expectEqual(-1, c.atoi(@ptrCast("-01"))); |
| 64 | try testing.expectEqual(1, c.atoi(@ptrCast("+001"))); |
| 65 | try testing.expectEqual(100, c.atoi(@ptrCast(" 100"))); |
| 66 | try testing.expectEqual(500, c.atoi(@ptrCast("000000000000500"))); |
| 67 | try testing.expectEqual(1111, c.atoi(@ptrCast("0000000000001111_0000"))); |
| 68 | try testing.expectEqual(0, c.atoi(@ptrCast("0xAA"))); |
| 69 | try testing.expectEqual(700, c.atoi(@ptrCast("700B"))); |
| 70 | try testing.expectEqual(32453, c.atoi(@ptrCast("+32453more"))); |
| 71 | try testing.expectEqual(math.maxInt(c_int), c.atoi(@ptrCast(fmt.comptimePrint("{d}", .{math.maxInt(c_int)})))); |
| 72 | try testing.expectEqual(math.minInt(c_int), c.atoi(@ptrCast(fmt.comptimePrint("{d}", .{math.minInt(c_int)})))); |
| 73 | } |
| 74 | |
| 75 | test "atol" { |
| 76 | try testing.expectEqual(0, c.atol(@ptrCast("stop42true"))); |
| 77 | try testing.expectEqual(42, c.atol(@ptrCast("42true"))); |
| 78 | try testing.expectEqual(-1, c.atol(@ptrCast("-01"))); |
| 79 | try testing.expectEqual(1, c.atol(@ptrCast("+001"))); |
| 80 | try testing.expectEqual(100, c.atol(@ptrCast(" 100"))); |
| 81 | try testing.expectEqual(500, c.atol(@ptrCast("000000000000500"))); |
| 82 | try testing.expectEqual(1111, c.atol(@ptrCast("0000000000001111_0000"))); |
| 83 | try testing.expectEqual(0, c.atol(@ptrCast("0xAA"))); |
| 84 | try testing.expectEqual(700, c.atol(@ptrCast("700B"))); |
| 85 | try testing.expectEqual(32453, c.atol(@ptrCast("+32453more"))); |
| 86 | try testing.expectEqual(math.maxInt(c_long), c.atol(@ptrCast(fmt.comptimePrint("{d}", .{math.maxInt(c_long)})))); |
| 87 | try testing.expectEqual(math.minInt(c_long), c.atol(@ptrCast(fmt.comptimePrint("{d}", .{math.minInt(c_long)})))); |
| 88 | } |
| 89 | |
| 90 | test "atoll" { |
| 91 | try testing.expectEqual(0, c.atoll(@ptrCast("stop42true"))); |
| 92 | try testing.expectEqual(42, c.atoll(@ptrCast("42true"))); |
| 93 | try testing.expectEqual(-1, c.atoll(@ptrCast("-01"))); |
| 94 | try testing.expectEqual(1, c.atoll(@ptrCast("+001"))); |
| 95 | try testing.expectEqual(100, c.atoll(@ptrCast(" 100"))); |
| 96 | try testing.expectEqual(500, c.atoll(@ptrCast("000000000000500"))); |
| 97 | try testing.expectEqual(1111, c.atoll(@ptrCast("0000000000001111_0000"))); |
| 98 | try testing.expectEqual(0, c.atoll(@ptrCast("0xAA"))); |
| 99 | try testing.expectEqual(700, c.atoll(@ptrCast("700B"))); |
| 100 | try testing.expectEqual(32453, c.atoll(@ptrCast(" +32453more"))); |
| 101 | try testing.expectEqual(math.maxInt(c_longlong), c.atoll(@ptrCast(fmt.comptimePrint("{d}", .{math.maxInt(c_longlong)})))); |
| 102 | try testing.expectEqual(math.minInt(c_longlong), c.atoll(@ptrCast(fmt.comptimePrint("{d}", .{math.minInt(c_longlong)})))); |
| 103 | } |
| 104 | |
| 105 | fn testStrToLLikeFunction( |
| 106 | func: anytype, |
| 107 | str: [*:0]const c_char, |
| 108 | base: c_int, |
| 109 | expected: comptime_int, |
| 110 | expected_len: ?usize, |
| 111 | expected_errno: c.E, |
| 112 | ) !void { |
| 113 | var end_ptr: [*:0]c_char = undefined; |
| 114 | try testing.expectEqual(expected, func(str, if (expected_len == null) null else &end_ptr, base)); |
| 115 | if (expected_len) |len| try testing.expectEqual(len, end_ptr - str); |
| 116 | try expectErrno(expected_errno); |
| 117 | } |
| 118 | |
| 119 | fn testStrToLLikeFunctionAnyErrno( |
| 120 | func: anytype, |
| 121 | str: [*:0]const c_char, |
| 122 | base: c_int, |
| 123 | expected: comptime_int, |
| 124 | expected_len: ?usize, |
| 125 | expected_errnos: []const c.E, |
| 126 | ) !void { |
| 127 | var end_ptr: [*:0]c_char = undefined; |
| 128 | try testing.expectEqual(expected, func(str, if (expected_len == null) null else &end_ptr, base)); |
| 129 | if (expected_len) |len| try testing.expectEqual(len, end_ptr - str); |
| 130 | try expectErrnoAny(expected_errnos); |
| 131 | } |
| 132 | |
| 133 | test "strtol" { |
| 134 | if (builtin.target.cpu.arch.isX86() and builtin.target.os.tag == .windows and builtin.target.abi == .msvc) return error.SkipZigTest; // TODO |
| 135 | |
| 136 | c._errno().* = @backingInt(c.E.SUCCESS); |
| 137 | try testStrToLLikeFunctionAnyErrno(c.strtol, @ptrCast("stop42true"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 138 | try testStrToLLikeFunction(c.strtol, @ptrCast("42true"), 0, 42, 2, .SUCCESS); |
| 139 | try testStrToLLikeFunction(c.strtol, @ptrCast("-01"), 0, -1, 3, .SUCCESS); |
| 140 | try testStrToLLikeFunction(c.strtol, @ptrCast("+001"), 0, 1, 4, .SUCCESS); |
| 141 | try testStrToLLikeFunction(c.strtol, @ptrCast(" 100"), 0, 100, 15, .SUCCESS); |
| 142 | try testStrToLLikeFunction(c.strtol, @ptrCast("000000000000500"), 0, 0o500, 15, .SUCCESS); |
| 143 | try testStrToLLikeFunction(c.strtol, @ptrCast("000000000000500"), 10, 500, 15, .SUCCESS); |
| 144 | try testStrToLLikeFunction(c.strtol, @ptrCast(" 0500"), 0, 0o500, 15, .SUCCESS); |
| 145 | try testStrToLLikeFunction(c.strtol, @ptrCast("0000000000001111_0000"), 10, 1111, 16, .SUCCESS); |
| 146 | try testStrToLLikeFunction(c.strtol, @ptrCast(" 1111_0000"), 0, 1111, 16, .SUCCESS); |
| 147 | try testStrToLLikeFunction(c.strtol, @ptrCast("0xAA"), 0, 0xAA, 4, .SUCCESS); |
| 148 | try testStrToLLikeFunction(c.strtol, @ptrCast("0xAA"), 10, 0, 1, .SUCCESS); |
| 149 | try testStrToLLikeFunction(c.strtol, @ptrCast("0xAA"), 16, 0xAA, 4, .SUCCESS); |
| 150 | try testStrToLLikeFunction(c.strtol, @ptrCast("0xAA"), 36, 43138, 4, .SUCCESS); |
| 151 | try testStrToLLikeFunction(c.strtol, @ptrCast("700B"), 0, 700, 3, .SUCCESS); |
| 152 | try testStrToLLikeFunction(c.strtol, @ptrCast("32453more"), 0, 32453, 5, .SUCCESS); |
| 153 | try testStrToLLikeFunction(c.strtol, @ptrCast(fmt.comptimePrint("{d}", .{math.maxInt(c_long)})), 0, math.maxInt(c_long), null, .SUCCESS); |
| 154 | try testStrToLLikeFunction(c.strtol, @ptrCast(fmt.comptimePrint("{d}", .{math.minInt(c_long)})), 0, math.minInt(c_long), null, .SUCCESS); |
| 155 | try testStrToLLikeFunction(c.strtol, @ptrCast(fmt.comptimePrint("{d}", .{math.maxInt(c_long) + 1})), 0, math.maxInt(c_long), null, .RANGE); |
| 156 | try testStrToLLikeFunction(c.strtol, @ptrCast(fmt.comptimePrint("{d}", .{math.minInt(c_long) - 1})), 0, math.minInt(c_long), null, .RANGE); |
| 157 | try testStrToLLikeFunctionAnyErrno(c.strtol, @ptrCast(""), 0, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 158 | try testStrToLLikeFunctionAnyErrno(c.strtol, @ptrCast(""), 12, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 159 | try testStrToLLikeFunctionAnyErrno(c.strtol, @ptrCast("-"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 160 | try testStrToLLikeFunctionAnyErrno(c.strtol, @ptrCast(" -"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 161 | try testStrToLLikeFunctionAnyErrno(c.strtol, @ptrCast(" "), 0, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 162 | try testStrToLLikeFunction(c.strtol, @ptrCast("0"), 0, 0, 1, .SUCCESS); |
| 163 | try testStrToLLikeFunction(c.strtol, @ptrCast("0"), 8, 0, 1, .SUCCESS); |
| 164 | try testStrToLLikeFunction(c.strtol, @ptrCast("09"), 0, 0, 1, .SUCCESS); |
| 165 | try testStrToLLikeFunction(c.strtol, @ptrCast("09"), 10, 9, 2, .SUCCESS); |
| 166 | if (builtin.os.tag != .windows) |
| 167 | try testStrToLLikeFunction(c.strtol, @ptrCast("0x"), 0, 0, 1, .SUCCESS); |
| 168 | try testStrToLLikeFunction(c.strtol, @ptrCast("1"), 37, 0, null, .INVAL); |
| 169 | try testStrToLLikeFunction(c.strtol, @ptrCast("1"), 1, 0, null, .INVAL); |
| 170 | try testStrToLLikeFunction(c.strtol, @ptrCast("1"), -1, 0, null, .INVAL); |
| 171 | } |
| 172 | |
| 173 | test "strtoll" { |
| 174 | if (builtin.target.cpu.arch.isX86() and builtin.target.os.tag == .windows and builtin.target.abi == .msvc) return error.SkipZigTest; // TODO |
| 175 | |
| 176 | c._errno().* = @backingInt(c.E.SUCCESS); |
| 177 | try testStrToLLikeFunctionAnyErrno(c.strtoll, @ptrCast("stop42true"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 178 | try testStrToLLikeFunction(c.strtoll, @ptrCast("42true"), 0, 42, 2, .SUCCESS); |
| 179 | try testStrToLLikeFunction(c.strtoll, @ptrCast("-01"), 0, -1, 3, .SUCCESS); |
| 180 | try testStrToLLikeFunction(c.strtoll, @ptrCast("+001"), 0, 1, 4, .SUCCESS); |
| 181 | try testStrToLLikeFunction(c.strtoll, @ptrCast(" 100"), 0, 100, 15, .SUCCESS); |
| 182 | try testStrToLLikeFunction(c.strtoll, @ptrCast("000000000000500"), 0, 0o500, 15, .SUCCESS); |
| 183 | try testStrToLLikeFunction(c.strtoll, @ptrCast("000000000000500"), 10, 500, 15, .SUCCESS); |
| 184 | try testStrToLLikeFunction(c.strtoll, @ptrCast(" 0500"), 0, 0o500, 15, .SUCCESS); |
| 185 | try testStrToLLikeFunction(c.strtoll, @ptrCast("0000000000001111_0000"), 10, 1111, 16, .SUCCESS); |
| 186 | try testStrToLLikeFunction(c.strtoll, @ptrCast(" 1111_0000"), 0, 1111, 16, .SUCCESS); |
| 187 | try testStrToLLikeFunction(c.strtoll, @ptrCast("0xAA"), 0, 0xAA, 4, .SUCCESS); |
| 188 | try testStrToLLikeFunction(c.strtoll, @ptrCast("0xAA"), 10, 0, 1, .SUCCESS); |
| 189 | try testStrToLLikeFunction(c.strtoll, @ptrCast("0xAA"), 16, 0xAA, 4, .SUCCESS); |
| 190 | try testStrToLLikeFunction(c.strtoll, @ptrCast("0xAA"), 36, 43138, 4, .SUCCESS); |
| 191 | try testStrToLLikeFunction(c.strtoll, @ptrCast("700B"), 0, 700, 3, .SUCCESS); |
| 192 | try testStrToLLikeFunction(c.strtoll, @ptrCast("32453more"), 0, 32453, 5, .SUCCESS); |
| 193 | try testStrToLLikeFunction(c.strtoll, @ptrCast(fmt.comptimePrint("{d}", .{math.maxInt(c_longlong)})), 0, math.maxInt(c_longlong), null, .SUCCESS); |
| 194 | try testStrToLLikeFunction(c.strtoll, @ptrCast(fmt.comptimePrint("{d}", .{math.minInt(c_longlong)})), 0, math.minInt(c_longlong), null, .SUCCESS); |
| 195 | try testStrToLLikeFunction(c.strtoll, @ptrCast(fmt.comptimePrint("{d}", .{math.maxInt(c_longlong) + 1})), 0, math.maxInt(c_longlong), null, .RANGE); |
| 196 | try testStrToLLikeFunction(c.strtoll, @ptrCast(fmt.comptimePrint("{d}", .{math.minInt(c_longlong) - 1})), 0, math.minInt(c_longlong), null, .RANGE); |
| 197 | try testStrToLLikeFunctionAnyErrno(c.strtoll, @ptrCast(""), 0, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 198 | try testStrToLLikeFunctionAnyErrno(c.strtoll, @ptrCast(""), 12, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 199 | try testStrToLLikeFunctionAnyErrno(c.strtoll, @ptrCast("-"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 200 | try testStrToLLikeFunctionAnyErrno(c.strtoll, @ptrCast(" -"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 201 | try testStrToLLikeFunctionAnyErrno(c.strtoll, @ptrCast(" "), 0, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 202 | try testStrToLLikeFunction(c.strtoll, @ptrCast("0"), 0, 0, 1, .SUCCESS); |
| 203 | try testStrToLLikeFunction(c.strtoll, @ptrCast("0"), 8, 0, 1, .SUCCESS); |
| 204 | try testStrToLLikeFunction(c.strtoll, @ptrCast("09"), 0, 0, 1, .SUCCESS); |
| 205 | try testStrToLLikeFunction(c.strtoll, @ptrCast("09"), 10, 9, 2, .SUCCESS); |
| 206 | if (builtin.os.tag != .windows) |
| 207 | try testStrToLLikeFunction(c.strtoll, @ptrCast("0x"), 0, 0, 1, .SUCCESS); |
| 208 | try testStrToLLikeFunction(c.strtoll, @ptrCast("1"), 37, 0, null, .INVAL); |
| 209 | try testStrToLLikeFunction(c.strtoll, @ptrCast("1"), 1, 0, null, .INVAL); |
| 210 | try testStrToLLikeFunction(c.strtoll, @ptrCast("1"), -1, 0, null, .INVAL); |
| 211 | } |
| 212 | |
| 213 | test "strtoul" { |
| 214 | if (builtin.target.cpu.arch.isX86() and builtin.target.os.tag == .windows and builtin.target.abi == .msvc) return error.SkipZigTest; // TODO |
| 215 | |
| 216 | c._errno().* = @backingInt(c.E.SUCCESS); |
| 217 | try testStrToLLikeFunctionAnyErrno(c.strtoul, @ptrCast("stop42true"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 218 | try testStrToLLikeFunction(c.strtoul, @ptrCast("42true"), 0, 42, 2, .SUCCESS); |
| 219 | try testStrToLLikeFunction(c.strtoul, @ptrCast("-01"), 0, math.maxInt(c_ulong), 3, .SUCCESS); |
| 220 | try testStrToLLikeFunction(c.strtoul, @ptrCast("+001"), 0, 1, 4, .SUCCESS); |
| 221 | try testStrToLLikeFunction(c.strtoul, @ptrCast(" 100"), 0, 100, 15, .SUCCESS); |
| 222 | try testStrToLLikeFunction(c.strtoul, @ptrCast("000000000000500"), 0, 0o500, 15, .SUCCESS); |
| 223 | try testStrToLLikeFunction(c.strtoul, @ptrCast("000000000000500"), 10, 500, 15, .SUCCESS); |
| 224 | try testStrToLLikeFunction(c.strtoul, @ptrCast(" 0500"), 0, 0o500, 15, .SUCCESS); |
| 225 | try testStrToLLikeFunction(c.strtoul, @ptrCast("0000000000001111_0000"), 10, 1111, 16, .SUCCESS); |
| 226 | try testStrToLLikeFunction(c.strtoul, @ptrCast(" 1111_0000"), 0, 1111, 16, .SUCCESS); |
| 227 | try testStrToLLikeFunction(c.strtoul, @ptrCast("0xAA"), 0, 0xAA, 4, .SUCCESS); |
| 228 | try testStrToLLikeFunction(c.strtoul, @ptrCast("0xAA"), 10, 0, 1, .SUCCESS); |
| 229 | try testStrToLLikeFunction(c.strtoul, @ptrCast("0xAA"), 16, 0xAA, 4, .SUCCESS); |
| 230 | try testStrToLLikeFunction(c.strtoul, @ptrCast("0xAA"), 36, 43138, 4, .SUCCESS); |
| 231 | try testStrToLLikeFunction(c.strtoul, @ptrCast("700B"), 0, 700, 3, .SUCCESS); |
| 232 | try testStrToLLikeFunction(c.strtoul, @ptrCast("32453more"), 0, 32453, 5, .SUCCESS); |
| 233 | try testStrToLLikeFunction(c.strtoul, @ptrCast(fmt.comptimePrint("{d}", .{math.maxInt(c_ulong)})), 0, math.maxInt(c_ulong), null, .SUCCESS); |
| 234 | try testStrToLLikeFunction(c.strtoul, @ptrCast(fmt.comptimePrint("{d}", .{math.maxInt(c_ulong) + 1})), 0, math.maxInt(c_ulong), null, .RANGE); |
| 235 | try testStrToLLikeFunctionAnyErrno(c.strtoul, @ptrCast(""), 0, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 236 | try testStrToLLikeFunctionAnyErrno(c.strtoul, @ptrCast(""), 12, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 237 | try testStrToLLikeFunctionAnyErrno(c.strtoul, @ptrCast("-"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 238 | try testStrToLLikeFunctionAnyErrno(c.strtoul, @ptrCast(" -"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 239 | try testStrToLLikeFunctionAnyErrno(c.strtoul, @ptrCast(" "), 0, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 240 | try testStrToLLikeFunction(c.strtoul, @ptrCast("0"), 0, 0, 1, .SUCCESS); |
| 241 | try testStrToLLikeFunction(c.strtoul, @ptrCast("0"), 8, 0, 1, .SUCCESS); |
| 242 | try testStrToLLikeFunction(c.strtoul, @ptrCast("09"), 0, 0, 1, .SUCCESS); |
| 243 | try testStrToLLikeFunction(c.strtoul, @ptrCast("09"), 10, 9, 2, .SUCCESS); |
| 244 | if (builtin.os.tag != .windows) |
| 245 | try testStrToLLikeFunction(c.strtoul, @ptrCast("0x"), 0, 0, 1, .SUCCESS); |
| 246 | try testStrToLLikeFunction(c.strtoul, @ptrCast("1"), 37, 0, null, .INVAL); |
| 247 | try testStrToLLikeFunction(c.strtoul, @ptrCast("1"), 1, 0, null, .INVAL); |
| 248 | try testStrToLLikeFunction(c.strtoul, @ptrCast("1"), -1, 0, null, .INVAL); |
| 249 | } |
| 250 | |
| 251 | test "strtoull" { |
| 252 | if (builtin.target.cpu.arch.isX86() and builtin.target.os.tag == .windows and builtin.target.abi == .msvc) return error.SkipZigTest; // TODO |
| 253 | |
| 254 | c._errno().* = @backingInt(c.E.SUCCESS); |
| 255 | try testStrToLLikeFunctionAnyErrno(c.strtoull, @ptrCast("stop42true"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 256 | try testStrToLLikeFunction(c.strtoull, @ptrCast("42true"), 0, 42, 2, .SUCCESS); |
| 257 | try testStrToLLikeFunction(c.strtoull, @ptrCast("-01"), 0, math.maxInt(c_ulonglong), 3, .SUCCESS); |
| 258 | try testStrToLLikeFunction(c.strtoull, @ptrCast("+001"), 0, 1, 4, .SUCCESS); |
| 259 | try testStrToLLikeFunction(c.strtoull, @ptrCast(" 100"), 0, 100, 15, .SUCCESS); |
| 260 | try testStrToLLikeFunction(c.strtoull, @ptrCast("000000000000500"), 0, 0o500, 15, .SUCCESS); |
| 261 | try testStrToLLikeFunction(c.strtoull, @ptrCast("000000000000500"), 10, 500, 15, .SUCCESS); |
| 262 | try testStrToLLikeFunction(c.strtoull, @ptrCast(" 0500"), 0, 0o500, 15, .SUCCESS); |
| 263 | try testStrToLLikeFunction(c.strtoull, @ptrCast("0000000000001111_0000"), 10, 1111, 16, .SUCCESS); |
| 264 | try testStrToLLikeFunction(c.strtoull, @ptrCast(" 1111_0000"), 0, 1111, 16, .SUCCESS); |
| 265 | try testStrToLLikeFunction(c.strtoull, @ptrCast("0xAA"), 0, 0xAA, 4, .SUCCESS); |
| 266 | try testStrToLLikeFunction(c.strtoull, @ptrCast("0xAA"), 10, 0, 1, .SUCCESS); |
| 267 | try testStrToLLikeFunction(c.strtoull, @ptrCast("0xAA"), 16, 0xAA, 4, .SUCCESS); |
| 268 | try testStrToLLikeFunction(c.strtoull, @ptrCast("0xAA"), 36, 43138, 4, .SUCCESS); |
| 269 | try testStrToLLikeFunction(c.strtoull, @ptrCast("700B"), 0, 700, 3, .SUCCESS); |
| 270 | try testStrToLLikeFunction(c.strtoull, @ptrCast("32453more"), 0, 32453, 5, .SUCCESS); |
| 271 | try testStrToLLikeFunction(c.strtoull, @ptrCast(fmt.comptimePrint("{d}", .{math.maxInt(c_ulonglong)})), 0, math.maxInt(c_ulonglong), null, .SUCCESS); |
| 272 | try testStrToLLikeFunction(c.strtoull, @ptrCast(fmt.comptimePrint("{d}", .{math.maxInt(c_ulonglong) + 1})), 0, math.maxInt(c_ulonglong), null, .RANGE); |
| 273 | try testStrToLLikeFunctionAnyErrno(c.strtoull, @ptrCast(""), 0, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 274 | try testStrToLLikeFunctionAnyErrno(c.strtoull, @ptrCast(""), 12, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 275 | try testStrToLLikeFunctionAnyErrno(c.strtoull, @ptrCast("-"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 276 | try testStrToLLikeFunctionAnyErrno(c.strtoull, @ptrCast(" -"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 277 | try testStrToLLikeFunctionAnyErrno(c.strtoull, @ptrCast(" "), 0, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 278 | try testStrToLLikeFunction(c.strtoull, @ptrCast("0"), 0, 0, 1, .SUCCESS); |
| 279 | try testStrToLLikeFunction(c.strtoull, @ptrCast("0"), 8, 0, 1, .SUCCESS); |
| 280 | try testStrToLLikeFunction(c.strtoull, @ptrCast("09"), 0, 0, 1, .SUCCESS); |
| 281 | try testStrToLLikeFunction(c.strtoull, @ptrCast("09"), 10, 9, 2, .SUCCESS); |
| 282 | if (builtin.os.tag != .windows) |
| 283 | try testStrToLLikeFunction(c.strtoull, @ptrCast("0x"), 0, 0, 1, .SUCCESS); |
| 284 | try testStrToLLikeFunction(c.strtoull, @ptrCast("1"), 37, 0, null, .INVAL); |
| 285 | try testStrToLLikeFunction(c.strtoull, @ptrCast("1"), 1, 0, null, .INVAL); |
| 286 | try testStrToLLikeFunction(c.strtoull, @ptrCast("1"), -1, 0, null, .INVAL); |
| 287 | } |
| 288 | |
| 289 | test "strtoimax" { |
| 290 | if (builtin.target.cpu.arch.isX86() and builtin.target.os.tag == .windows and builtin.target.abi == .msvc) return error.SkipZigTest; // TODO |
| 291 | |
| 292 | c._errno().* = @backingInt(c.E.SUCCESS); |
| 293 | try testStrToLLikeFunctionAnyErrno(c.strtoimax, @ptrCast("stop42true"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 294 | try testStrToLLikeFunction(c.strtoimax, @ptrCast("42true"), 0, 42, 2, .SUCCESS); |
| 295 | try testStrToLLikeFunction(c.strtoimax, @ptrCast("-01"), 0, -1, 3, .SUCCESS); |
| 296 | try testStrToLLikeFunction(c.strtoimax, @ptrCast("+001"), 0, 1, 4, .SUCCESS); |
| 297 | try testStrToLLikeFunction(c.strtoimax, @ptrCast(" 100"), 0, 100, 15, .SUCCESS); |
| 298 | try testStrToLLikeFunction(c.strtoimax, @ptrCast("000000000000500"), 0, 0o500, 15, .SUCCESS); |
| 299 | try testStrToLLikeFunction(c.strtoimax, @ptrCast("000000000000500"), 10, 500, 15, .SUCCESS); |
| 300 | try testStrToLLikeFunction(c.strtoimax, @ptrCast(" 0500"), 0, 0o500, 15, .SUCCESS); |
| 301 | try testStrToLLikeFunction(c.strtoimax, @ptrCast("0000000000001111_0000"), 10, 1111, 16, .SUCCESS); |
| 302 | try testStrToLLikeFunction(c.strtoimax, @ptrCast(" 1111_0000"), 0, 1111, 16, .SUCCESS); |
| 303 | try testStrToLLikeFunction(c.strtoimax, @ptrCast("0xAA"), 0, 0xAA, 4, .SUCCESS); |
| 304 | try testStrToLLikeFunction(c.strtoimax, @ptrCast("0xAA"), 10, 0, 1, .SUCCESS); |
| 305 | try testStrToLLikeFunction(c.strtoimax, @ptrCast("0xAA"), 16, 0xAA, 4, .SUCCESS); |
| 306 | try testStrToLLikeFunction(c.strtoimax, @ptrCast("0xAA"), 36, 43138, 4, .SUCCESS); |
| 307 | try testStrToLLikeFunction(c.strtoimax, @ptrCast("700B"), 0, 700, 3, .SUCCESS); |
| 308 | try testStrToLLikeFunction(c.strtoimax, @ptrCast("32453more"), 0, 32453, 5, .SUCCESS); |
| 309 | try testStrToLLikeFunction(c.strtoimax, @ptrCast(fmt.comptimePrint("{d}", .{math.maxInt(c.intmax_t)})), 0, math.maxInt(c.intmax_t), null, .SUCCESS); |
| 310 | try testStrToLLikeFunction(c.strtoimax, @ptrCast(fmt.comptimePrint("{d}", .{math.minInt(c.intmax_t)})), 0, math.minInt(c.intmax_t), null, .SUCCESS); |
| 311 | try testStrToLLikeFunction(c.strtoimax, @ptrCast(fmt.comptimePrint("{d}", .{math.maxInt(c.intmax_t) + 1})), 0, math.maxInt(c.intmax_t), null, .RANGE); |
| 312 | try testStrToLLikeFunction(c.strtoimax, @ptrCast(fmt.comptimePrint("{d}", .{math.minInt(c.intmax_t) - 1})), 0, math.minInt(c.intmax_t), null, .RANGE); |
| 313 | try testStrToLLikeFunctionAnyErrno(c.strtoimax, @ptrCast(""), 0, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 314 | try testStrToLLikeFunctionAnyErrno(c.strtoimax, @ptrCast(""), 12, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 315 | try testStrToLLikeFunctionAnyErrno(c.strtoimax, @ptrCast("-"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 316 | try testStrToLLikeFunctionAnyErrno(c.strtoimax, @ptrCast(" -"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 317 | try testStrToLLikeFunctionAnyErrno(c.strtoimax, @ptrCast(" "), 0, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 318 | try testStrToLLikeFunction(c.strtoimax, @ptrCast("0"), 0, 0, 1, .SUCCESS); |
| 319 | try testStrToLLikeFunction(c.strtoimax, @ptrCast("0"), 8, 0, 1, .SUCCESS); |
| 320 | try testStrToLLikeFunction(c.strtoimax, @ptrCast("09"), 0, 0, 1, .SUCCESS); |
| 321 | try testStrToLLikeFunction(c.strtoimax, @ptrCast("09"), 10, 9, 2, .SUCCESS); |
| 322 | if (builtin.os.tag != .windows) |
| 323 | try testStrToLLikeFunction(c.strtoimax, @ptrCast("0x"), 0, 0, 1, .SUCCESS); |
| 324 | try testStrToLLikeFunction(c.strtoimax, @ptrCast("1"), 37, 0, null, .INVAL); |
| 325 | try testStrToLLikeFunction(c.strtoimax, @ptrCast("1"), 1, 0, null, .INVAL); |
| 326 | try testStrToLLikeFunction(c.strtoimax, @ptrCast("1"), -1, 0, null, .INVAL); |
| 327 | } |
| 328 | |
| 329 | test "strtoumax" { |
| 330 | if (builtin.target.cpu.arch.isX86() and builtin.target.os.tag == .windows and builtin.target.abi == .msvc) return error.SkipZigTest; // TODO |
| 331 | |
| 332 | c._errno().* = @backingInt(c.E.SUCCESS); |
| 333 | try testStrToLLikeFunctionAnyErrno(c.strtoumax, @ptrCast("stop42true"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 334 | try testStrToLLikeFunction(c.strtoumax, @ptrCast("42true"), 0, 42, 2, .SUCCESS); |
| 335 | try testStrToLLikeFunction(c.strtoumax, @ptrCast("-01"), 0, math.maxInt(c.uintmax_t), 3, .SUCCESS); |
| 336 | try testStrToLLikeFunction(c.strtoumax, @ptrCast("+001"), 0, 1, 4, .SUCCESS); |
| 337 | try testStrToLLikeFunction(c.strtoumax, @ptrCast(" 100"), 0, 100, 15, .SUCCESS); |
| 338 | try testStrToLLikeFunction(c.strtoumax, @ptrCast("000000000000500"), 0, 0o500, 15, .SUCCESS); |
| 339 | try testStrToLLikeFunction(c.strtoumax, @ptrCast("000000000000500"), 10, 500, 15, .SUCCESS); |
| 340 | try testStrToLLikeFunction(c.strtoumax, @ptrCast(" 0500"), 0, 0o500, 15, .SUCCESS); |
| 341 | try testStrToLLikeFunction(c.strtoumax, @ptrCast("0000000000001111_0000"), 10, 1111, 16, .SUCCESS); |
| 342 | try testStrToLLikeFunction(c.strtoumax, @ptrCast(" 1111_0000"), 0, 1111, 16, .SUCCESS); |
| 343 | try testStrToLLikeFunction(c.strtoumax, @ptrCast("0xAA"), 0, 0xAA, 4, .SUCCESS); |
| 344 | try testStrToLLikeFunction(c.strtoumax, @ptrCast("0xAA"), 10, 0, 1, .SUCCESS); |
| 345 | try testStrToLLikeFunction(c.strtoumax, @ptrCast("0xAA"), 16, 0xAA, 4, .SUCCESS); |
| 346 | try testStrToLLikeFunction(c.strtoumax, @ptrCast("0xAA"), 36, 43138, 4, .SUCCESS); |
| 347 | try testStrToLLikeFunction(c.strtoumax, @ptrCast("700B"), 0, 700, 3, .SUCCESS); |
| 348 | try testStrToLLikeFunction(c.strtoumax, @ptrCast("32453more"), 0, 32453, 5, .SUCCESS); |
| 349 | try testStrToLLikeFunction(c.strtoumax, @ptrCast(fmt.comptimePrint("{d}", .{math.maxInt(c_ulonglong)})), 0, math.maxInt(c_ulonglong), null, .SUCCESS); |
| 350 | try testStrToLLikeFunction(c.strtoumax, @ptrCast(fmt.comptimePrint("{d}", .{math.maxInt(c_ulonglong) + 1})), 0, math.maxInt(c_ulonglong), null, .RANGE); |
| 351 | try testStrToLLikeFunctionAnyErrno(c.strtoumax, @ptrCast(""), 0, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 352 | try testStrToLLikeFunctionAnyErrno(c.strtoumax, @ptrCast(""), 12, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 353 | try testStrToLLikeFunctionAnyErrno(c.strtoumax, @ptrCast("-"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 354 | try testStrToLLikeFunctionAnyErrno(c.strtoumax, @ptrCast(" -"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 355 | try testStrToLLikeFunctionAnyErrno(c.strtoumax, @ptrCast(" "), 0, 0, 0, &.{ .SUCCESS, .INVAL }); |
| 356 | try testStrToLLikeFunction(c.strtoumax, @ptrCast("0"), 0, 0, 1, .SUCCESS); |
| 357 | try testStrToLLikeFunction(c.strtoumax, @ptrCast("0"), 8, 0, 1, .SUCCESS); |
| 358 | try testStrToLLikeFunction(c.strtoumax, @ptrCast("09"), 0, 0, 1, .SUCCESS); |
| 359 | try testStrToLLikeFunction(c.strtoumax, @ptrCast("09"), 10, 9, 2, .SUCCESS); |
| 360 | if (builtin.os.tag != .windows) |
| 361 | try testStrToLLikeFunction(c.strtoumax, @ptrCast("0x"), 0, 0, 1, .SUCCESS); |
| 362 | try testStrToLLikeFunction(c.strtoumax, @ptrCast("1"), 37, 0, null, .INVAL); |
| 363 | try testStrToLLikeFunction(c.strtoumax, @ptrCast("1"), 1, 0, null, .INVAL); |
| 364 | try testStrToLLikeFunction(c.strtoumax, @ptrCast("1"), -1, 0, null, .INVAL); |
| 365 | } |
| 366 | |
| 367 | test "bsearch" { |
| 368 | const Comparison = struct { |
| 369 | pub fn compare(a: *const anyopaque, b: *const anyopaque) callconv(.c) c_int { |
| 370 | const a_u16: *const u16 = @ptrCast(@alignCast(a)); |
| 371 | const b_u16: *const u16 = @ptrCast(@alignCast(b)); |
| 372 | |
| 373 | return switch (math.order(a_u16.*, b_u16.*)) { |
| 374 | .gt => 1, |
| 375 | .eq => 0, |
| 376 | .lt => -1, |
| 377 | }; |
| 378 | } |
| 379 | }; |
| 380 | |
| 381 | const items: []const u16 = &.{ 0, 5, 7, 9, 10, 200, 512, 768 }; |
| 382 | |
| 383 | try testing.expectEqual(@as(?*anyopaque, null), c.bsearch(&@as(u16, 2000), items.ptr, items.len, @sizeOf(u16), Comparison.compare)); |
| 384 | |
| 385 | for (items) |*value| { |
| 386 | try testing.expectEqual(@as(*const anyopaque, value), c.bsearch(value, items.ptr, items.len, @sizeOf(u16), Comparison.compare)); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | test { |
| 391 | _ = @import("stdlib/drand48.zig"); |
| 392 | } |