// ftostest — exercises lib/strconv/ftos.ww (Hare ftos.ha / ftos_ryu.ha // Ryū port). Run with `out/bin/ww run lib/strconv/test/ftostest.ww`. // Same signalled-then-os.exit(signalled+10) pattern as stoftest/ // decimaltest: a non-zero exit names the failing @test (1..N → 11..N+10). // // Ports ref/hare/strconv/+test/ftos_test.ha's ffmt::G / prec=void / // fflags::NONE rows (the f64tos cases) verbatim — these are exactly // strconv.f64tos's output. encode_f_dec is covered by 13.37/1100/0.011/… // and encode_e_dec by 1e4/1.1e4/1e-3/1.1e-3/1e-4. nan/±infinity via // math.f64frombits of the IEEE bit patterns (ww math has no f64 NAN/INF // const — see math/floats.ww). // // f32tos is NOT exercised here: it is deferred to fold-5b (task #67), // gated on the #143 f32-arg-push cs≠ww cgen fix. // // Lives in lib/strconv/test/ so `import strconv` resolves to the // DIRECTORY (full package), not the strconv.ww FILE — same rationale as // stoftest/decimaltest. package strconv; import strconv; import os; import math; let signalled: i32 = 0; fn fail() void = { os.exit(signalled + 10); }; fn streq(a: str, b: str) bool = { if (a.len != b.len) { return false; }; let k: i32 = 0i32; for (k < a.len) { if (a[k] != b[k]) { return false; }; k += 1i32; }; return true; }; fn chk(n: f64, want: str) bool = { return streq(f64tos(n), want); }; // ftos_test.ha:57-64 (G/void/NONE-equivalent) — fixed-point renders. @test fn f64tos_fixed() void = { if (!chk(13.37, "13.37")) { fail(); }; if (!chk(12345.0, "12345")) { fail(); }; if (!chk(1100.0, "1100")) { fail(); }; if (!chk(100.0, "100")) { fail(); }; if (!chk(10.0, "10")) { fail(); }; if (!chk(1.0, "1")) { fail(); }; if (!chk(0.3, "0.3")) { fail(); }; if (!chk(0.1, "0.1")) { fail(); }; if (!chk(0.01, "0.01")) { fail(); }; if (!chk(0.011, "0.011")) { fail(); }; if (!chk(1.414, "1.414")) { fail(); }; if (!chk(-6345.972, "-6345.972")) { fail(); }; }; // ftos_test.ha:78-90 — scientific renders (the shortest-G E-dispatch). @test fn f64tos_scientific() void = { if (!chk(10000.0, "1e4")) { fail(); }; if (!chk(11000.0, "1.1e4")) { fail(); }; if (!chk(1000.0, "1e3")) { fail(); }; if (!chk(0.001, "1e-3")) { fail(); }; if (!chk(0.0011, "1.1e-3")) { fail(); }; if (!chk(0.0001, "1e-4")) { fail(); }; // dp=-2 < -1 → G dispatches to E (ftos_test.ha:62 is the ffmt::F row; // shortest-G of 0.0031415 is scientific). if (!chk(0.0031415, "3.1415e-3")) { fail(); }; }; // ftos_test.ha:12/16/27 — zero, ±infinity, nan. @test fn f64tos_special() void = { if (!chk(0.0, "0")) { fail(); }; // sign bit via (1u64 << 63), not the 0x8000000000000000 literal: // wwstage mis-emits the I64_MIN-magnitude literal (#144 family). let negzero: f64 = math.f64frombits(1u64 << 63u64); if (!chk(negzero, "-0")) { fail(); }; let inf: f64 = math.f64frombits(0x7FF0000000000000u64); if (!chk(inf, "infinity")) { fail(); }; let ninf: f64 = math.f64frombits(0xFFF0000000000000u64); if (!chk(ninf, "-infinity")) { fail(); }; let nan: f64 = math.f64frombits(0x7FF8000000000000u64); if (!chk(nan, "nan")) { fail(); }; }; export fn main() i32 = { signalled = 1; f64tos_fixed(); signalled = 2; f64tos_scientific(); signalled = 3; f64tos_special(); os.exit(0); return 0; };