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:
-100
@@ -2,10 +2,6 @@ const builtin = @import("builtin");
|
||||
|
||||
const std = @import("std");
|
||||
const math = std.math;
|
||||
const expect = std.testing.expect;
|
||||
const expectEqual = std.testing.expectEqual;
|
||||
const expectApproxEqAbs = std.testing.expectApproxEqAbs;
|
||||
const expectApproxEqRel = std.testing.expectApproxEqRel;
|
||||
|
||||
const symbol = @import("../c.zig").symbol;
|
||||
|
||||
@@ -304,59 +300,6 @@ fn modfl(x: c_longdouble, iptr: *c_longdouble) callconv(.c) c_longdouble {
|
||||
};
|
||||
}
|
||||
|
||||
fn testModf(comptime T: type) !void {
|
||||
// Choose the appropriate `modf` impl to test based on type
|
||||
const f = switch (T) {
|
||||
f32 => modff,
|
||||
f64 => modf,
|
||||
c_longdouble => modfl,
|
||||
else => @compileError("modf not implemented for " ++ @typeName(T)),
|
||||
};
|
||||
|
||||
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 expectApproxEqAbs(expected, normal_frac, eps_val);
|
||||
try 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 expect(math.isNan(nan_frac));
|
||||
try 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 expect(math.isPositiveZero(pos_zero_frac));
|
||||
try 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 expect(math.isNegativeZero(neg_zero_frac));
|
||||
try expect(math.isNegativeInf(iptr.*));
|
||||
|
||||
// Return -0 when `x` is a negative integer
|
||||
const nz_frac = f(@as(T, -1000.0), iptr);
|
||||
try expect(math.isNegativeZero(nz_frac));
|
||||
try expectEqual(@as(T, -1000.0), iptr.*);
|
||||
|
||||
// Return +0 when `x` is a positive integer
|
||||
const pz_frac = f(@as(T, 1000.0), iptr);
|
||||
try expect(math.isPositiveZero(pz_frac));
|
||||
try expectEqual(@as(T, 1000.0), iptr.*);
|
||||
}
|
||||
|
||||
test "modf" {
|
||||
try testModf(f32);
|
||||
try testModf(f64);
|
||||
try testModf(c_longdouble);
|
||||
}
|
||||
|
||||
fn nan(_: [*:0]const c_char) callconv(.c) f64 {
|
||||
return math.nan(f64);
|
||||
}
|
||||
@@ -421,49 +364,6 @@ fn rintf(x: f32) callconv(.c) f32 {
|
||||
return y;
|
||||
}
|
||||
|
||||
fn testRint(comptime T: type) !void {
|
||||
const f = switch (T) {
|
||||
f32 => rintf,
|
||||
f64 => rint,
|
||||
else => @compileError("rint not implemented for" ++ @typeName(T)),
|
||||
};
|
||||
|
||||
// Positive numbers round correctly
|
||||
try expectEqual(@as(T, 42.0), f(42.2));
|
||||
try expectEqual(@as(T, 42.0), f(41.8));
|
||||
|
||||
// Negative numbers round correctly
|
||||
try expectEqual(@as(T, -6.0), f(-5.9));
|
||||
try expectEqual(@as(T, -6.0), f(-6.1));
|
||||
|
||||
// No rounding needed test
|
||||
try expectEqual(@as(T, 5.0), f(5.0));
|
||||
try expectEqual(@as(T, -10.0), f(-10.0));
|
||||
try expectEqual(@as(T, 0.0), f(0.0));
|
||||
|
||||
// Very large numbers return unchanged
|
||||
const large: T = 9007199254740992.0; // 2^53
|
||||
try expectEqual(large, f(large));
|
||||
try expectEqual(-large, f(-large));
|
||||
|
||||
// Small positive numbers round to zero
|
||||
const pos_result = f(0.3);
|
||||
try expect(math.isPositiveZero(pos_result));
|
||||
|
||||
// Small negative numbers round to negative zero
|
||||
const neg_result = f(-0.3);
|
||||
try expect(math.isNegativeZero(neg_result));
|
||||
|
||||
// Exact half rounds to nearest even (banker's rounding)
|
||||
try expectEqual(@as(T, 2.0), f(2.5));
|
||||
try expectEqual(@as(T, 4.0), f(3.5));
|
||||
}
|
||||
|
||||
test "rint" {
|
||||
try testRint(f32);
|
||||
try testRint(f64);
|
||||
}
|
||||
|
||||
fn tanh(x: f64) callconv(.c) f64 {
|
||||
return math.tanh(x);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user