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");...@@ -2,6 +2,9 @@ const builtin = @import("builtin");
22
3const std = @import("std");3const std = @import("std");
4const math = std.math;4const math = std.math;
5const expect = std.testing.expect;
6const expectEqual = std.testing.expectEqual;
7const expectApproxEqRel = std.testing.expectApproxEqRel;
58
6const symbol = @import("../c.zig").symbol;9const symbol = @import("../c.zig").symbol;
710
...@@ -194,35 +197,35 @@ test "modf" {...@@ -194,35 +197,35 @@ test "modf" {
194 const eps_val = 1e-6;197 const eps_val = 1e-6;
195198
196 const normal_frac = modf(1234.5678, iptr);199 const normal_frac = modf(1234.5678, iptr);
197 try std.testing.expectApproxEqRel(0.5678, normal_frac, eps_val);200 try expectApproxEqRel(0.5678, normal_frac, eps_val);
198 try std.testing.expectApproxEqRel(1234.0, iptr.*, eps_val);201 try expectApproxEqRel(1234.0, iptr.*, eps_val);
199202
200 // When `x` is a NaN, NaN is returned and `*iptr` is set to NaN203 // When `x` is a NaN, NaN is returned and `*iptr` is set to NaN
201 const nan_frac = modf(math.nan(f64), iptr);204 const nan_frac = modf(math.nan(f64), iptr);
202 try std.testing.expect(math.isNan(nan_frac));205 try expect(math.isNan(nan_frac));
203 try std.testing.expect(math.isNan(iptr.*));206 try expect(math.isNan(iptr.*));
204207
205 // When `x` is positive infinity, +0 is returned and `*iptr` is set to208 // When `x` is positive infinity, +0 is returned and `*iptr` is set to
206 // positive infinity209 // positive infinity
207 const pos_zero_frac = modf(math.inf(f64), iptr);210 const pos_zero_frac = modf(math.inf(f64), iptr);
208 try std.testing.expect(math.isPositiveZero(pos_zero_frac));211 try expect(math.isPositiveZero(pos_zero_frac));
209 try std.testing.expect(math.isPositiveInf(iptr.*));212 try expect(math.isPositiveInf(iptr.*));
210213
211 // When `x` is negative infinity, -0 is returned and `*iptr` is set to214 // When `x` is negative infinity, -0 is returned and `*iptr` is set to
212 // negative infinity215 // negative infinity
213 const neg_zero_frac = modf(-math.inf(f64), iptr);216 const neg_zero_frac = modf(-math.inf(f64), iptr);
214 try std.testing.expect(math.isNegativeZero(neg_zero_frac));217 try expect(math.isNegativeZero(neg_zero_frac));
215 try std.testing.expect(math.isNegativeInf(iptr.*));218 try expect(math.isNegativeInf(iptr.*));
216219
217 // Return -0 when `x` is a negative integer220 // Return -0 when `x` is a negative integer
218 const nz_frac = modf(-1000.0, iptr);221 const nz_frac = modf(-1000.0, iptr);
219 try std.testing.expect(math.isNegativeZero(nz_frac));222 try expect(math.isNegativeZero(nz_frac));
220 try std.testing.expectEqual(-1000.0, iptr.*);223 try expectEqual(-1000.0, iptr.*);
221224
222 // Return +0 when `x` is a positive integer225 // Return +0 when `x` is a positive integer
223 const pz_frac = modf(1000.0, iptr);226 const pz_frac = modf(1000.0, iptr);
224 try std.testing.expect(math.isPositiveZero(pz_frac));227 try expect(math.isPositiveZero(pz_frac));
225 try std.testing.expectEqual(1000.0, iptr.*);228 try expectEqual(1000.0, iptr.*);
226}229}
227230
228fn nan(_: [*:0]const c_char) callconv(.c) f64 {231fn nan(_: [*:0]const c_char) callconv(.c) f64 {
...@@ -272,35 +275,35 @@ fn rint(x: f64) callconv(.c) f64 {...@@ -272,35 +275,35 @@ fn rint(x: f64) callconv(.c) f64 {
272275
273test "rint" {276test "rint" {
274 // Positive numbers round correctly277 // Positive numbers round correctly
275 try std.testing.expectEqual(@as(f64, 42.0), rint(42.2));278 try expectEqual(@as(f64, 42.0), rint(42.2));
276 try std.testing.expectEqual(@as(f64, 42.0), rint(41.8));279 try expectEqual(@as(f64, 42.0), rint(41.8));
277280
278 // Negative numbers round correctly281 // Negative numbers round correctly
279 try std.testing.expectEqual(@as(f64, -6.0), rint(-5.9));282 try expectEqual(@as(f64, -6.0), rint(-5.9));
280 try std.testing.expectEqual(@as(f64, -6.0), rint(-6.1));283 try expectEqual(@as(f64, -6.0), rint(-6.1));
281284
282 // No rounding needed test285 // No rounding needed test
283 try std.testing.expectEqual(@as(f64, 5.0), rint(5.0));286 try expectEqual(@as(f64, 5.0), rint(5.0));
284 try std.testing.expectEqual(@as(f64, -10.0), rint(-10.0));287 try expectEqual(@as(f64, -10.0), rint(-10.0));
285 try std.testing.expectEqual(@as(f64, 0.0), rint(0.0));288 try expectEqual(@as(f64, 0.0), rint(0.0));
286289
287 // Very large numbers return unchanged290 // Very large numbers return unchanged
288 const large: f64 = 9007199254740992.0; // 2^53291 const large: f64 = 9007199254740992.0; // 2^53
289 try std.testing.expectEqual(large, rint(large));292 try expectEqual(large, rint(large));
290 try std.testing.expectEqual(-large, rint(-large));293 try expectEqual(-large, rint(-large));
291294
292 // Small positive numbers round to zero295 // Small positive numbers round to zero
293 const pos_result = rint(0.3);296 const pos_result = rint(0.3);
294 try std.testing.expectEqual(@as(f64, 0.0), pos_result);297 try expectEqual(@as(f64, 0.0), pos_result);
295 try std.testing.expect(@as(u64, @bitCast(pos_result)) == 0);298 try expect(@as(u64, @bitCast(pos_result)) == 0);
296299
297 // Small negative numbers round to negative zero300 // Small negative numbers round to negative zero
298 const neg_result = rint(-0.3);301 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);
300 const bits: u64 = @bitCast(neg_result);303 const bits: u64 = @bitCast(neg_result);
301 try std.testing.expect((bits >> 63) == 1);304 try expect((bits >> 63) == 1);
302305
303 // Exact half rounds to nearest even (banker's rounding)306 // Exact half rounds to nearest even (banker's rounding)
304 try std.testing.expectEqual(@as(f64, 2.0), rint(2.5));307 try expectEqual(@as(f64, 2.0), rint(2.5));
305 try std.testing.expectEqual(@as(f64, 4.0), rint(3.5));308 try expectEqual(@as(f64, 4.0), rint(3.5));
306}309}