authorgravatar for hi@mihaelm.commihael <hi@mihaelm.com> 2026-03-14 21:03:40+01:00
committergravatar for hi@mihaelm.commihael <hi@mihaelm.com> 2026-03-17 23:33:10+01:00
log1c280032aae151b184e9224e2e0d1414f7436a01
tree977ba537b32379399b66f206a70221af5ccf75fa
parenteab22ab2b88313f86158230ddcdcb22f28483d85
signaturebadge-check Signed by SSH key SHA256:aoFoShdYLdrqMichqKXSSieTKUfACUIDJHsKc4V2tQg

`libzigc`: Assign `std.testing` functions to consts


1 files changed, 30 insertions(+), 27 deletions(-)

lib/c/math.zig+30-27
......@@ -2,6 +2,9 @@ const builtin = @import("builtin");
22
33const std = @import("std");
44const math = std.math;
5const expect = std.testing.expect;
6const expectEqual = std.testing.expectEqual;
7const expectApproxEqRel = std.testing.expectApproxEqRel;
58
69const symbol = @import("../c.zig").symbol;
710
......@@ -194,35 +197,35 @@ test "modf" {
194197 const eps_val = 1e-6;
195198
196199 const normal_frac = modf(1234.5678, iptr);
197 try std.testing.expectApproxEqRel(0.5678, normal_frac, eps_val);
198 try std.testing.expectApproxEqRel(1234.0, iptr.*, eps_val);
200 try expectApproxEqRel(0.5678, normal_frac, eps_val);
201 try expectApproxEqRel(1234.0, iptr.*, eps_val);
199202
200203 // When `x` is a NaN, NaN is returned and `*iptr` is set to NaN
201204 const nan_frac = modf(math.nan(f64), iptr);
202 try std.testing.expect(math.isNan(nan_frac));
203 try std.testing.expect(math.isNan(iptr.*));
205 try expect(math.isNan(nan_frac));
206 try expect(math.isNan(iptr.*));
204207
205208 // When `x` is positive infinity, +0 is returned and `*iptr` is set to
206209 // positive infinity
207210 const pos_zero_frac = modf(math.inf(f64), iptr);
208 try std.testing.expect(math.isPositiveZero(pos_zero_frac));
209 try std.testing.expect(math.isPositiveInf(iptr.*));
211 try expect(math.isPositiveZero(pos_zero_frac));
212 try expect(math.isPositiveInf(iptr.*));
210213
211214 // When `x` is negative infinity, -0 is returned and `*iptr` is set to
212215 // negative infinity
213216 const neg_zero_frac = modf(-math.inf(f64), iptr);
214 try std.testing.expect(math.isNegativeZero(neg_zero_frac));
215 try std.testing.expect(math.isNegativeInf(iptr.*));
217 try expect(math.isNegativeZero(neg_zero_frac));
218 try expect(math.isNegativeInf(iptr.*));
216219
217220 // Return -0 when `x` is a negative integer
218221 const nz_frac = modf(-1000.0, iptr);
219 try std.testing.expect(math.isNegativeZero(nz_frac));
220 try std.testing.expectEqual(-1000.0, iptr.*);
222 try expect(math.isNegativeZero(nz_frac));
223 try expectEqual(-1000.0, iptr.*);
221224
222225 // Return +0 when `x` is a positive integer
223226 const pz_frac = modf(1000.0, iptr);
224 try std.testing.expect(math.isPositiveZero(pz_frac));
225 try std.testing.expectEqual(1000.0, iptr.*);
227 try expect(math.isPositiveZero(pz_frac));
228 try expectEqual(1000.0, iptr.*);
226229}
227230
228231fn nan(_: [*:0]const c_char) callconv(.c) f64 {
......@@ -272,35 +275,35 @@ fn rint(x: f64) callconv(.c) f64 {
272275
273276test "rint" {
274277 // Positive numbers round correctly
275 try std.testing.expectEqual(@as(f64, 42.0), rint(42.2));
276 try std.testing.expectEqual(@as(f64, 42.0), rint(41.8));
278 try expectEqual(@as(f64, 42.0), rint(42.2));
279 try expectEqual(@as(f64, 42.0), rint(41.8));
277280
278281 // Negative numbers round correctly
279 try std.testing.expectEqual(@as(f64, -6.0), rint(-5.9));
280 try std.testing.expectEqual(@as(f64, -6.0), rint(-6.1));
282 try expectEqual(@as(f64, -6.0), rint(-5.9));
283 try expectEqual(@as(f64, -6.0), rint(-6.1));
281284
282285 // No rounding needed test
283 try std.testing.expectEqual(@as(f64, 5.0), rint(5.0));
284 try std.testing.expectEqual(@as(f64, -10.0), rint(-10.0));
285 try std.testing.expectEqual(@as(f64, 0.0), rint(0.0));
286 try expectEqual(@as(f64, 5.0), rint(5.0));
287 try expectEqual(@as(f64, -10.0), rint(-10.0));
288 try expectEqual(@as(f64, 0.0), rint(0.0));
286289
287290 // Very large numbers return unchanged
288291 const large: f64 = 9007199254740992.0; // 2^53
289 try std.testing.expectEqual(large, rint(large));
290 try std.testing.expectEqual(-large, rint(-large));
292 try expectEqual(large, rint(large));
293 try expectEqual(-large, rint(-large));
291294
292295 // Small positive numbers round to zero
293296 const pos_result = rint(0.3);
294 try std.testing.expectEqual(@as(f64, 0.0), pos_result);
295 try std.testing.expect(@as(u64, @bitCast(pos_result)) == 0);
297 try expectEqual(@as(f64, 0.0), pos_result);
298 try expect(@as(u64, @bitCast(pos_result)) == 0);
296299
297300 // Small negative numbers round to negative zero
298301 const neg_result = rint(-0.3);
299 try std.testing.expectEqual(@as(f64, 0.0), neg_result);
302 try expectEqual(@as(f64, 0.0), neg_result);
300303 const bits: u64 = @bitCast(neg_result);
301 try std.testing.expect((bits >> 63) == 1);
304 try expect((bits >> 63) == 1);
302305
303306 // Exact half rounds to nearest even (banker's rounding)
304 try std.testing.expectEqual(@as(f64, 2.0), rint(2.5));
305 try std.testing.expectEqual(@as(f64, 4.0), rint(3.5));
307 try expectEqual(@as(f64, 2.0), rint(2.5));
308 try expectEqual(@as(f64, 4.0), rint(3.5));
306309}