mirror of
https://codeberg.org/ziglang/zig.git
synced 2026-04-27 19:09:47 +03:00
libzigc: move all unit tests from lib/c/ to test/c/
Before: * test-zigc: run libzigc unit tests (part of test-modules) * test-libc: run libc-test cases Now: * test-libc: run libc API unit tests (part of test-modules) * test-libc-nsz: run libc-test cases libc API unit tests (previously referred to as libzigc unit tests) now run for all supported targets, even those we don't provide libzigc for. The idea is that this will help us catch bad assumptions in the unit tests, as well as bugs in other libcs. I considered this setup: * test-c: run libc API unit tests (part of test-modules) * test-libc-nsz: run libc-test cases * test-libc: both of the above However, I do not like it because it gives a false sense of security; the full module and C ABI test suites are still liable to catch libzigc bugs that test-c and test-libc-nsz might not. So contributors should just run the test steps outlined in https://codeberg.org/ziglang/zig/issues/30978. Co-authored-by: rpkak <rpkak@noreply.codeberg.org>
This commit is contained in:
+12
@@ -0,0 +1,12 @@
|
||||
const builtin = @import("builtin");
|
||||
const std = @import("std");
|
||||
|
||||
test {
|
||||
_ = @import("c/inttypes.zig");
|
||||
_ = @import("c/math.zig");
|
||||
_ = @import("c/search.zig");
|
||||
_ = @import("c/stdlib.zig");
|
||||
_ = @import("c/string.zig");
|
||||
_ = @import("c/strings.zig");
|
||||
_ = @import("c/unistd.zig");
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
const builtin = @import("builtin");
|
||||
const std = @import("std");
|
||||
|
||||
const c = std.c;
|
||||
const testing = std.testing;
|
||||
|
||||
test "imaxabs" {
|
||||
const val: c.intmax_t = -10;
|
||||
try testing.expectEqual(10, c.imaxabs(val));
|
||||
}
|
||||
|
||||
test "imaxdiv" {
|
||||
const expected: c.imaxdiv_t = .{ .quot = 9, .rem = 0 };
|
||||
try testing.expectEqual(expected, c.imaxdiv(9, 1));
|
||||
}
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
const builtin = @import("builtin");
|
||||
const std = @import("std");
|
||||
|
||||
const c = std.c;
|
||||
const math = std.math;
|
||||
const testing = std.testing;
|
||||
|
||||
fn testModf(comptime T: type) !void {
|
||||
const f = switch (T) {
|
||||
f32 => c.modff,
|
||||
f64 => c.modf,
|
||||
c_longdouble => c.modfl,
|
||||
else => unreachable,
|
||||
};
|
||||
|
||||
var int: T = undefined;
|
||||
const iptr = ∫
|
||||
const eps_val: comptime_float = @max(1e-6, math.floatEps(T));
|
||||
|
||||
const normal_frac = f(@as(T, 1234.567), iptr);
|
||||
// Account for precision error
|
||||
const expected = 1234.567 - @as(T, 1234);
|
||||
try testing.expectApproxEqAbs(expected, normal_frac, eps_val);
|
||||
try testing.expectApproxEqRel(@as(T, 1234.0), iptr.*, eps_val);
|
||||
|
||||
// When `x` is a NaN, NaN is returned and `*iptr` is set to NaN
|
||||
const nan_frac = f(math.nan(T), iptr);
|
||||
try testing.expect(math.isNan(nan_frac));
|
||||
try testing.expect(math.isNan(iptr.*));
|
||||
|
||||
// When `x` is positive infinity, +0 is returned and `*iptr` is set to
|
||||
// positive infinity
|
||||
const pos_zero_frac = f(math.inf(T), iptr);
|
||||
try testing.expect(math.isPositiveZero(pos_zero_frac));
|
||||
try testing.expect(math.isPositiveInf(iptr.*));
|
||||
|
||||
// When `x` is negative infinity, -0 is returned and `*iptr` is set to
|
||||
// negative infinity
|
||||
const neg_zero_frac = f(-math.inf(T), iptr);
|
||||
try testing.expect(math.isNegativeZero(neg_zero_frac));
|
||||
try testing.expect(math.isNegativeInf(iptr.*));
|
||||
|
||||
// Return -0 when `x` is a negative integer
|
||||
const nz_frac = f(@as(T, -1000.0), iptr);
|
||||
try testing.expect(math.isNegativeZero(nz_frac));
|
||||
try testing.expectEqual(@as(T, -1000.0), iptr.*);
|
||||
|
||||
// Return +0 when `x` is a positive integer
|
||||
const pz_frac = f(@as(T, 1000.0), iptr);
|
||||
try testing.expect(math.isPositiveZero(pz_frac));
|
||||
try testing.expectEqual(@as(T, 1000.0), iptr.*);
|
||||
}
|
||||
|
||||
test "modf" {
|
||||
try testModf(f32);
|
||||
try testModf(f64);
|
||||
try testModf(c_longdouble);
|
||||
}
|
||||
|
||||
fn testRint(comptime T: type) !void {
|
||||
const f = switch (T) {
|
||||
f32 => c.rintf,
|
||||
f64 => c.rint,
|
||||
else => @compileError("rint not implemented for" ++ @typeName(T)),
|
||||
};
|
||||
|
||||
// Positive numbers round correctly
|
||||
try testing.expectEqual(@as(T, 42.0), f(42.2));
|
||||
try testing.expectEqual(@as(T, 42.0), f(41.8));
|
||||
|
||||
// Negative numbers round correctly
|
||||
try testing.expectEqual(@as(T, -6.0), f(-5.9));
|
||||
try testing.expectEqual(@as(T, -6.0), f(-6.1));
|
||||
|
||||
// No rounding needed test
|
||||
try testing.expectEqual(@as(T, 5.0), f(5.0));
|
||||
try testing.expectEqual(@as(T, -10.0), f(-10.0));
|
||||
try testing.expectEqual(@as(T, 0.0), f(0.0));
|
||||
|
||||
// Very large numbers return unchanged
|
||||
const large: T = 9007199254740992.0; // 2^53
|
||||
try testing.expectEqual(large, f(large));
|
||||
try testing.expectEqual(-large, f(-large));
|
||||
|
||||
// Small positive numbers round to zero
|
||||
const pos_result = f(0.3);
|
||||
try testing.expect(math.isPositiveZero(pos_result));
|
||||
|
||||
// Small negative numbers round to negative zero
|
||||
const neg_result = f(-0.3);
|
||||
try testing.expect(math.isNegativeZero(neg_result));
|
||||
|
||||
// Exact half rounds to nearest even (banker's rounding)
|
||||
try testing.expectEqual(@as(T, 2.0), f(2.5));
|
||||
try testing.expectEqual(@as(T, 4.0), f(3.5));
|
||||
}
|
||||
|
||||
test "rint" {
|
||||
try testRint(f32);
|
||||
try testRint(f64);
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
const builtin = @import("builtin");
|
||||
const std = @import("std");
|
||||
|
||||
const c = std.c;
|
||||
const testing = std.testing;
|
||||
|
||||
/// Not defined in `std.c` because C headers don't either.
|
||||
const Node = extern struct {
|
||||
next: ?*Node,
|
||||
prev: ?*Node,
|
||||
};
|
||||
|
||||
test "insque and remque" {
|
||||
var first: Node = .{ .next = null, .prev = null };
|
||||
var second: Node = .{ .next = null, .prev = null };
|
||||
var third: Node = .{ .next = null, .prev = null };
|
||||
|
||||
c.insque(&first, null);
|
||||
try testing.expectEqual(@as(?*Node, null), first.next);
|
||||
try testing.expectEqual(@as(?*Node, null), first.prev);
|
||||
|
||||
c.insque(&second, &first);
|
||||
try testing.expectEqual(@as(?*Node, &second), first.next);
|
||||
try testing.expectEqual(@as(?*Node, &first), second.prev);
|
||||
|
||||
c.insque(&third, &first);
|
||||
try testing.expectEqual(@as(?*Node, &third), first.next);
|
||||
try testing.expectEqual(@as(?*Node, &second), third.next);
|
||||
try testing.expectEqual(@as(?*Node, &first), third.prev);
|
||||
try testing.expectEqual(@as(?*Node, &third), second.prev);
|
||||
|
||||
c.remque(&third);
|
||||
try testing.expectEqual(@as(?*Node, &second), first.next);
|
||||
try testing.expectEqual(@as(?*Node, &first), second.prev);
|
||||
|
||||
c.remque(&second);
|
||||
try testing.expectEqual(@as(?*Node, null), first.next);
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
const builtin = @import("builtin");
|
||||
const std = @import("std");
|
||||
|
||||
const c = std.c;
|
||||
const fmt = std.fmt;
|
||||
const math = std.math;
|
||||
const testing = std.testing;
|
||||
|
||||
test "abs" {
|
||||
const val: c_int = -10;
|
||||
try testing.expectEqual(10, c.abs(val));
|
||||
}
|
||||
|
||||
test "labs" {
|
||||
const val: c_long = -10;
|
||||
try testing.expectEqual(10, c.labs(val));
|
||||
}
|
||||
|
||||
test "llabs" {
|
||||
const val: c_longlong = -10;
|
||||
try testing.expectEqual(10, c.llabs(val));
|
||||
}
|
||||
|
||||
test "div" {
|
||||
const expected: c.div_t = .{ .quot = 5, .rem = 5 };
|
||||
try testing.expectEqual(expected, c.div(55, 10));
|
||||
}
|
||||
|
||||
test "ldiv" {
|
||||
const expected: c.ldiv_t = .{ .quot = -6, .rem = 2 };
|
||||
try testing.expectEqual(expected, c.ldiv(38, -6));
|
||||
}
|
||||
|
||||
test "lldiv" {
|
||||
const expected: c.lldiv_t = .{ .quot = 1, .rem = 2 };
|
||||
try testing.expectEqual(expected, c.lldiv(5, 3));
|
||||
}
|
||||
|
||||
test "atoi" {
|
||||
try testing.expectEqual(0, c.atoi(@ptrCast("stop42true")));
|
||||
try testing.expectEqual(42, c.atoi(@ptrCast("42true")));
|
||||
try testing.expectEqual(-1, c.atoi(@ptrCast("-01")));
|
||||
try testing.expectEqual(1, c.atoi(@ptrCast("+001")));
|
||||
try testing.expectEqual(100, c.atoi(@ptrCast(" 100")));
|
||||
try testing.expectEqual(500, c.atoi(@ptrCast("000000000000500")));
|
||||
try testing.expectEqual(1111, c.atoi(@ptrCast("0000000000001111_0000")));
|
||||
try testing.expectEqual(0, c.atoi(@ptrCast("0xAA")));
|
||||
try testing.expectEqual(700, c.atoi(@ptrCast("700B")));
|
||||
try testing.expectEqual(32453, c.atoi(@ptrCast("+32453more")));
|
||||
try testing.expectEqual(math.maxInt(c_int), c.atoi(@ptrCast(fmt.comptimePrint("{d}", .{math.maxInt(c_int)}))));
|
||||
try testing.expectEqual(math.minInt(c_int), c.atoi(@ptrCast(fmt.comptimePrint("{d}", .{math.minInt(c_int)}))));
|
||||
}
|
||||
|
||||
test "atol" {
|
||||
try testing.expectEqual(0, c.atol(@ptrCast("stop42true")));
|
||||
try testing.expectEqual(42, c.atol(@ptrCast("42true")));
|
||||
try testing.expectEqual(-1, c.atol(@ptrCast("-01")));
|
||||
try testing.expectEqual(1, c.atol(@ptrCast("+001")));
|
||||
try testing.expectEqual(100, c.atol(@ptrCast(" 100")));
|
||||
try testing.expectEqual(500, c.atol(@ptrCast("000000000000500")));
|
||||
try testing.expectEqual(1111, c.atol(@ptrCast("0000000000001111_0000")));
|
||||
try testing.expectEqual(0, c.atol(@ptrCast("0xAA")));
|
||||
try testing.expectEqual(700, c.atol(@ptrCast("700B")));
|
||||
try testing.expectEqual(32453, c.atol(@ptrCast("+32453more")));
|
||||
try testing.expectEqual(math.maxInt(c_long), c.atol(@ptrCast(fmt.comptimePrint("{d}", .{math.maxInt(c_long)}))));
|
||||
try testing.expectEqual(math.minInt(c_long), c.atol(@ptrCast(fmt.comptimePrint("{d}", .{math.minInt(c_long)}))));
|
||||
}
|
||||
|
||||
test "atoll" {
|
||||
try testing.expectEqual(0, c.atoll(@ptrCast("stop42true")));
|
||||
try testing.expectEqual(42, c.atoll(@ptrCast("42true")));
|
||||
try testing.expectEqual(-1, c.atoll(@ptrCast("-01")));
|
||||
try testing.expectEqual(1, c.atoll(@ptrCast("+001")));
|
||||
try testing.expectEqual(100, c.atoll(@ptrCast(" 100")));
|
||||
try testing.expectEqual(500, c.atoll(@ptrCast("000000000000500")));
|
||||
try testing.expectEqual(1111, c.atoll(@ptrCast("0000000000001111_0000")));
|
||||
try testing.expectEqual(0, c.atoll(@ptrCast("0xAA")));
|
||||
try testing.expectEqual(700, c.atoll(@ptrCast("700B")));
|
||||
try testing.expectEqual(32453, c.atoll(@ptrCast(" +32453more")));
|
||||
try testing.expectEqual(math.maxInt(c_longlong), c.atoll(@ptrCast(fmt.comptimePrint("{d}", .{math.maxInt(c_longlong)}))));
|
||||
try testing.expectEqual(math.minInt(c_longlong), c.atoll(@ptrCast(fmt.comptimePrint("{d}", .{math.minInt(c_longlong)}))));
|
||||
}
|
||||
|
||||
test "bsearch" {
|
||||
const Comparison = struct {
|
||||
pub fn compare(a: *const anyopaque, b: *const anyopaque) callconv(.c) c_int {
|
||||
const a_u16: *const u16 = @ptrCast(@alignCast(a));
|
||||
const b_u16: *const u16 = @ptrCast(@alignCast(b));
|
||||
|
||||
return switch (math.order(a_u16.*, b_u16.*)) {
|
||||
.gt => 1,
|
||||
.eq => 0,
|
||||
.lt => -1,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
const items: []const u16 = &.{ 0, 5, 7, 9, 10, 200, 512, 768 };
|
||||
|
||||
try testing.expectEqual(@as(?*anyopaque, null), c.bsearch(&@as(u16, 2000), items.ptr, items.len, @sizeOf(u16), Comparison.compare));
|
||||
|
||||
for (items) |*value| {
|
||||
try testing.expectEqual(@as(*const anyopaque, value), c.bsearch(value, items.ptr, items.len, @sizeOf(u16), Comparison.compare));
|
||||
}
|
||||
}
|
||||
|
||||
test {
|
||||
_ = @import("stdlib/drand48.zig");
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
const builtin = @import("builtin");
|
||||
const std = @import("std");
|
||||
|
||||
const c = std.c;
|
||||
const testing = std.testing;
|
||||
|
||||
test "erand48" {
|
||||
var xsubi: [3]c_ushort = .{ 37174, 64810, 11603 };
|
||||
|
||||
try testing.expectApproxEqAbs(0.8965, c.erand48(&xsubi), 0.0005);
|
||||
try testing.expectEqualSlices(c_ushort, &.{ 22537, 47966, 58735 }, &xsubi);
|
||||
|
||||
try testing.expectApproxEqAbs(0.3375, c.erand48(&xsubi), 0.0005);
|
||||
try testing.expectEqualSlices(c_ushort, &.{ 37344, 32911, 22119 }, &xsubi);
|
||||
|
||||
try testing.expectApproxEqAbs(0.6475, c.erand48(&xsubi), 0.0005);
|
||||
try testing.expectEqualSlices(c_ushort, &.{ 23659, 29872, 42445 }, &xsubi);
|
||||
|
||||
try testing.expectApproxEqAbs(0.5005, c.erand48(&xsubi), 0.0005);
|
||||
try testing.expectEqualSlices(c_ushort, &.{ 31642, 7875, 32802 }, &xsubi);
|
||||
|
||||
try testing.expectApproxEqAbs(0.5065, c.erand48(&xsubi), 0.0005);
|
||||
try testing.expectEqualSlices(c_ushort, &.{ 64669, 14399, 33170 }, &xsubi);
|
||||
}
|
||||
|
||||
test "jrand48" {
|
||||
var xsubi: [3]c_ushort = .{ 25175, 11052, 45015 };
|
||||
|
||||
try testing.expectEqual(1699503220, c.jrand48(&xsubi));
|
||||
try testing.expectEqualSlices(c_ushort, &.{ 2326, 23668, 25932 }, &xsubi);
|
||||
|
||||
try testing.expectEqual(-992276007, c.jrand48(&xsubi));
|
||||
try testing.expectEqualSlices(c_ushort, &.{ 41577, 4569, 50395 }, &xsubi);
|
||||
|
||||
try testing.expectEqual(-19535776, c.jrand48(&xsubi));
|
||||
try testing.expectEqualSlices(c_ushort, &.{ 31936, 59488, 65237 }, &xsubi);
|
||||
|
||||
try testing.expectEqual(79438377, c.jrand48(&xsubi));
|
||||
try testing.expectEqualSlices(c_ushort, &.{ 40395, 8745, 1212 }, &xsubi);
|
||||
|
||||
try testing.expectEqual(-1258917728, c.jrand48(&xsubi));
|
||||
try testing.expectEqualSlices(c_ushort, &.{ 37242, 28832, 46326 }, &xsubi);
|
||||
}
|
||||
|
||||
test "nrand48" {
|
||||
var xsubi: [3]c_ushort = .{ 546, 33817, 23389 };
|
||||
|
||||
try testing.expectEqual(914920692, c.nrand48(&xsubi));
|
||||
try testing.expectEqualSlices(c_ushort, &.{ 29829, 10728, 27921 }, &xsubi);
|
||||
|
||||
try testing.expectEqual(754104482, c.nrand48(&xsubi));
|
||||
try testing.expectEqualSlices(c_ushort, &.{ 6828, 28997, 23013 }, &xsubi);
|
||||
|
||||
try testing.expectEqual(609453945, c.nrand48(&xsubi));
|
||||
try testing.expectEqualSlices(c_ushort, &.{ 58183, 3826, 18599 }, &xsubi);
|
||||
|
||||
try testing.expectEqual(1878644360, c.nrand48(&xsubi));
|
||||
try testing.expectEqualSlices(c_ushort, &.{ 36678, 44304, 57331 }, &xsubi);
|
||||
|
||||
try testing.expectEqual(2114923686, c.nrand48(&xsubi));
|
||||
try testing.expectEqualSlices(c_ushort, &.{ 58585, 22861, 64542 }, &xsubi);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
const builtin = @import("builtin");
|
||||
const std = @import("std");
|
||||
|
||||
const c = std.c;
|
||||
const testing = std.testing;
|
||||
|
||||
test "strncmp" {
|
||||
try testing.expect(c.strncmp(@ptrCast("a"), @ptrCast("b"), 1) < 0);
|
||||
try testing.expect(c.strncmp(@ptrCast("a"), @ptrCast("c"), 1) < 0);
|
||||
try testing.expect(c.strncmp(@ptrCast("b"), @ptrCast("a"), 1) > 0);
|
||||
try testing.expect(c.strncmp(@ptrCast("\xff"), @ptrCast("\x02"), 1) > 0);
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
const builtin = @import("builtin");
|
||||
const std = @import("std");
|
||||
|
||||
const c = std.c;
|
||||
const mem = std.mem;
|
||||
const testing = std.testing;
|
||||
|
||||
test "bzero" {
|
||||
var array: [10]u8 = [_]u8{ '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
|
||||
var a = mem.zeroes([array.len]u8);
|
||||
a[9] = '0';
|
||||
c.bzero(&array[0], 9);
|
||||
try testing.expect(mem.eql(u8, &array, &a));
|
||||
}
|
||||
|
||||
fn testFfs(comptime T: type) !void {
|
||||
const ffs = switch (T) {
|
||||
c_int => c.ffs,
|
||||
c_long => c.ffsl,
|
||||
c_longlong => c.ffsll,
|
||||
else => unreachable,
|
||||
};
|
||||
|
||||
try testing.expectEqual(0, ffs(0));
|
||||
|
||||
for (0..@bitSizeOf(T)) |i| {
|
||||
const bit = @as(T, 1) << @intCast(i);
|
||||
|
||||
try testing.expectEqual(@as(T, @intCast(i + 1)), ffs(bit));
|
||||
}
|
||||
}
|
||||
|
||||
test "ffs" {
|
||||
try testFfs(c_int);
|
||||
try testFfs(c_long);
|
||||
try testFfs(c_longlong);
|
||||
}
|
||||
|
||||
test "strcasecmp" {
|
||||
try testing.expect(c.strcasecmp(@ptrCast("a"), @ptrCast("b")) < 0);
|
||||
try testing.expect(c.strcasecmp(@ptrCast("b"), @ptrCast("a")) > 0);
|
||||
try testing.expect(c.strcasecmp(@ptrCast("A"), @ptrCast("b")) < 0);
|
||||
try testing.expect(c.strcasecmp(@ptrCast("b"), @ptrCast("A")) > 0);
|
||||
try testing.expect(c.strcasecmp(@ptrCast("A"), @ptrCast("A")) == 0);
|
||||
try testing.expect(c.strcasecmp(@ptrCast("B"), @ptrCast("b")) == 0);
|
||||
try testing.expect(c.strcasecmp(@ptrCast("bb"), @ptrCast("AA")) > 0);
|
||||
}
|
||||
|
||||
test "strncasecmp" {
|
||||
try testing.expect(c.strncasecmp(@ptrCast("a"), @ptrCast("b"), 1) < 0);
|
||||
try testing.expect(c.strncasecmp(@ptrCast("b"), @ptrCast("a"), 1) > 0);
|
||||
try testing.expect(c.strncasecmp(@ptrCast("A"), @ptrCast("b"), 1) < 0);
|
||||
try testing.expect(c.strncasecmp(@ptrCast("b"), @ptrCast("A"), 1) > 0);
|
||||
try testing.expect(c.strncasecmp(@ptrCast("A"), @ptrCast("A"), 1) == 0);
|
||||
try testing.expect(c.strncasecmp(@ptrCast("B"), @ptrCast("b"), 1) == 0);
|
||||
try testing.expect(c.strncasecmp(@ptrCast("bb"), @ptrCast("AA"), 2) > 0);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
const builtin = @import("builtin");
|
||||
const std = @import("std");
|
||||
|
||||
const c = std.c;
|
||||
const testing = std.testing;
|
||||
|
||||
test "swab" {
|
||||
var a: [4]u8 = undefined;
|
||||
@memset(a[0..], '\x00');
|
||||
c.swab("abcd", &a, 4);
|
||||
try testing.expectEqualSlices(u8, "badc", &a);
|
||||
|
||||
// Partial copy
|
||||
@memset(a[0..], '\x00');
|
||||
c.swab("abcd", &a, 2);
|
||||
try testing.expectEqualSlices(u8, "ba\x00\x00", &a);
|
||||
|
||||
// n < 1
|
||||
@memset(a[0..], '\x00');
|
||||
c.swab("abcd", &a, 0);
|
||||
try testing.expectEqualSlices(u8, "\x00" ** 4, &a);
|
||||
c.swab("abcd", &a, -1);
|
||||
try testing.expectEqualSlices(u8, "\x00" ** 4, &a);
|
||||
|
||||
// Odd n
|
||||
@memset(a[0..], '\x00');
|
||||
c.swab("abcd", &a, 1);
|
||||
try testing.expectEqualSlices(u8, "\x00" ** 4, &a);
|
||||
c.swab("abcd", &a, 3);
|
||||
try testing.expectEqualSlices(u8, "ba\x00\x00", &a);
|
||||
}
|
||||
+22
-41
@@ -18,7 +18,7 @@ pub const DebuggerContext = @import("src/Debugger.zig");
|
||||
pub const LlvmIrContext = @import("src/LlvmIr.zig");
|
||||
pub const LibcContext = @import("src/Libc.zig");
|
||||
|
||||
const TestTarget = struct {
|
||||
const ModuleTestTarget = struct {
|
||||
linkage: ?std.builtin.LinkMode = null,
|
||||
target: std.Target.Query = .{},
|
||||
optimize_mode: std.builtin.OptimizeMode = .Debug,
|
||||
@@ -36,33 +36,14 @@ const TestTarget = struct {
|
||||
// invocation. This could be because of a slow backend, requiring a newer LLVM version, being
|
||||
// too niche, etc.
|
||||
extra_target: bool = false,
|
||||
|
||||
pub fn supportsModule(
|
||||
self: *const TestTarget,
|
||||
target: *const std.Build.ResolvedTarget,
|
||||
name: []const u8,
|
||||
) bool {
|
||||
if (mem.eql(u8, name, "zigc")) {
|
||||
if (target.result.isMuslLibC()) return self.linkage == .static or (self.linkage == null and !target.query.isNative());
|
||||
if (target.result.isMinGW()) return true;
|
||||
if (target.result.isWasiLibC()) return true;
|
||||
return false;
|
||||
}
|
||||
if (mem.eql(u8, name, "std")) {
|
||||
if (target.result.cpu.arch.isSpirV()) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
const test_targets = blk: {
|
||||
const module_test_targets = blk: {
|
||||
// getBaselineCpuFeatures calls populateDependencies which has a O(N ^ 2) algorithm
|
||||
// (where N is roughly 160, which technically makes it O(1), but it adds up to a
|
||||
// lot of branches)
|
||||
@setEvalBranchQuota(80_000);
|
||||
break :blk [_]TestTarget{
|
||||
break :blk [_]ModuleTestTarget{
|
||||
// Native Targets
|
||||
|
||||
.{}, // 0 index must be all defaults
|
||||
@@ -2475,24 +2456,21 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {
|
||||
const step = b.step(b.fmt("test-{s}", .{options.name}), options.desc);
|
||||
|
||||
if (options.test_only) |test_only| {
|
||||
const test_target: TestTarget = switch (test_only) {
|
||||
.default => test_targets[0],
|
||||
const test_target: ModuleTestTarget = switch (test_only) {
|
||||
.default => module_test_targets[0],
|
||||
.fuzz => |optimize| .{
|
||||
.optimize_mode = optimize,
|
||||
.use_llvm = true,
|
||||
},
|
||||
};
|
||||
const resolved_target = b.resolveTargetQuery(test_target.target);
|
||||
|
||||
if (test_target.supportsModule(&resolved_target, options.name)) {
|
||||
const triple_txt = resolved_target.query.zigTriple(b.allocator) catch @panic("OOM");
|
||||
addOneModuleTest(b, step, test_target, &resolved_target, triple_txt, options);
|
||||
}
|
||||
const triple_txt = resolved_target.query.zigTriple(b.allocator) catch @panic("OOM");
|
||||
addOneModuleTest(b, step, test_target, &resolved_target, triple_txt, options);
|
||||
|
||||
return step;
|
||||
}
|
||||
|
||||
for_targets: for (test_targets) |test_target| {
|
||||
for_targets: for (module_test_targets) |test_target| {
|
||||
if (test_target.skip_modules.len > 0) {
|
||||
for (test_target.skip_modules) |skip_mod| {
|
||||
if (std.mem.eql(u8, options.name, skip_mod)) continue :for_targets;
|
||||
@@ -2501,8 +2479,6 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {
|
||||
|
||||
const resolved_target = b.resolveTargetQuery(test_target.target);
|
||||
|
||||
if (!test_target.supportsModule(&resolved_target, options.name)) continue;
|
||||
|
||||
if (!options.test_extra_targets and test_target.extra_target) continue;
|
||||
|
||||
if (options.skip_non_native and !test_target.target.isNative())
|
||||
@@ -2510,6 +2486,13 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {
|
||||
|
||||
const target = &resolved_target.result;
|
||||
|
||||
if (std.mem.eql(u8, options.name, "libc")) {
|
||||
// The libc API tests obviously need to link libc. So for test
|
||||
// target entries where we wouldn't link libc by default, skip the
|
||||
// libc API tests.
|
||||
if (test_target.link_libc == null and !std.os.targetRequiresLibC(target)) continue;
|
||||
}
|
||||
|
||||
if (options.skip_spirv and target.cpu.arch.isSpirV()) continue;
|
||||
if (options.skip_wasm and target.cpu.arch.isWasm()) continue;
|
||||
|
||||
@@ -2544,8 +2527,6 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {
|
||||
if (!would_use_llvm and target.cpu.arch == .aarch64) {
|
||||
// TODO get std tests passing for the aarch64 self-hosted backend.
|
||||
if (mem.eql(u8, options.name, "std")) continue;
|
||||
// TODO get zigc tests passing for the aarch64 self-hosted backend.
|
||||
if (mem.eql(u8, options.name, "zigc")) continue;
|
||||
}
|
||||
|
||||
const want_this_mode = for (options.optimize_modes) |m| {
|
||||
@@ -2561,7 +2542,7 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {
|
||||
fn addOneModuleTest(
|
||||
b: *std.Build,
|
||||
step: *Step,
|
||||
test_target: TestTarget,
|
||||
test_target: ModuleTestTarget,
|
||||
resolved_target: *const std.Build.ResolvedTarget,
|
||||
triple_txt: []const u8,
|
||||
options: ModuleTestOptions,
|
||||
@@ -2598,11 +2579,11 @@ fn addOneModuleTest(
|
||||
});
|
||||
these_tests.linkage = test_target.linkage;
|
||||
// https://codeberg.org/ziglang/zig/issues/31701
|
||||
if (!(mem.eql(u8, options.name, "compiler-rt") or mem.eql(u8, options.name, "zigc"))) {
|
||||
if (!(mem.eql(u8, options.name, "compiler-rt") or mem.eql(u8, options.name, "libc"))) {
|
||||
if (options.no_builtin) these_tests.root_module.no_builtin = true;
|
||||
}
|
||||
// https://codeberg.org/ziglang/zig/issues/31702
|
||||
if (mem.eql(u8, options.name, "compiler-rt") or mem.eql(u8, options.name, "zigc")) {
|
||||
if (mem.eql(u8, options.name, "compiler-rt") or mem.eql(u8, options.name, "libc")) {
|
||||
these_tests.root_module.stack_protector = false;
|
||||
}
|
||||
if (options.build_options) |build_options| {
|
||||
@@ -2992,7 +2973,7 @@ pub fn addLlvmIrTests(b: *std.Build, options: LlvmIrContext.Options) ?*Step {
|
||||
return step;
|
||||
}
|
||||
|
||||
const libc_targets: []const std.Target.Query = &.{
|
||||
const libc_test_nsz_targets: []const std.Target.Query = &.{
|
||||
.{
|
||||
.cpu_arch = .arm,
|
||||
.os_tag = .linux,
|
||||
@@ -3155,8 +3136,8 @@ const libc_targets: []const std.Target.Query = &.{
|
||||
},
|
||||
};
|
||||
|
||||
pub fn addLibcTests(b: *std.Build, options: LibcContext.Options) ?*Step {
|
||||
const step = b.step("test-libc", "Run libc-test test cases");
|
||||
pub fn addLibcTestNszTests(b: *std.Build, options: LibcContext.Options) ?*Step {
|
||||
const step = b.step("test-libc-nsz", "Run external libc-test test cases");
|
||||
const opt_libc_test_path = b.option(std.Build.LazyPath, "libc-test-path", "path to libc-test source directory");
|
||||
if (opt_libc_test_path) |libc_test_path| {
|
||||
var context: LibcContext = .{
|
||||
@@ -3168,7 +3149,7 @@ pub fn addLibcTests(b: *std.Build, options: LibcContext.Options) ?*Step {
|
||||
|
||||
libc.addCases(&context);
|
||||
|
||||
for (libc_targets) |target_query| {
|
||||
for (libc_test_nsz_targets) |target_query| {
|
||||
const target = b.resolveTargetQuery(target_query);
|
||||
context.addTarget(target);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user