// stoftest — exercises lib/strconv/stof.ww (Hare stof.ha port). // Run with `out/bin/ww run lib/strconv/test/stoftest.ww`. Same // signalled-then-os.exit(signalled+10) pattern as decimaltest: a // non-zero exit names the failing @test (1..N → exit 11..N+10). // // Ports ref/hare/strconv/stof.ha's @test vectors (stof64 / stof32 / // stofhex). Comparisons are BIT-level (math.f64bits / f32bits) so a // sign flip (-0.0 vs 0.0) or a 1-ulp miss fails the row rather than // silently passing on IEEE `==`. NaN via math.isnan; ±Inf via // f64frombits(INF_BITS) (ww math has no NAN/INF f32/f64 const — see // math/floats.ww). Hex extremes (Hare's math::F64_MAX_NORMAL etc., // absent in ww math) assert against the IEEE-754 bit patterns directly. // // Lives in lib/strconv/test/ so `import strconv` resolves to the // DIRECTORY (full package: strconv.ww + decimal.ww + stof_data.ww + // stof.ww), not the strconv.ww FILE — same rationale as decimaltest. package strconv_test; import strconv; import os; import math; let signalled: i32 = 0; fn fail() void = { os.exit(signalled + 10); }; // ---- unwrap helpers (bit-exact value checks) ------------------------- fn chk64(s: str, b: base, want: f64) bool = { match (stof64(s, b)) { case let v: f64 => { return math.f64bits(v) == math.f64bits(want); }; case let e: invalid => { return false; }; case let e: overflow => { return false; }; }; return false; }; fn chk32(s: str, b: base, want: f32) bool = { match (stof32(s, b)) { case let v: f32 => { return math.f32bits(v) == math.f32bits(want); }; case let e: invalid => { return false; }; case let e: overflow => { return false; }; }; return false; }; fn ovf64(s: str, b: base) bool = { match (stof64(s, b)) { case let v: f64 => { return false; }; case let e: invalid => { return false; }; case let e: overflow => { return true; }; }; return false; }; fn ovf32(s: str, b: base) bool = { match (stof32(s, b)) { case let v: f32 => { return false; }; case let e: invalid => { return false; }; case let e: overflow => { return true; }; }; return false; }; // inv64 — the invalid byte-index, or -1 if not invalid. fn inv64(s: str) i32 = { match (stof64(s, base.DEC)) { case let v: f64 => { return -1; }; case let e: invalid => { return (e: i32); }; case let e: overflow => { return -1; }; }; return -1; }; fn inv32(s: str) i32 = { match (stof32(s, base.DEC)) { case let v: f32 => { return -1; }; case let e: invalid => { return (e: i32); }; case let e: overflow => { return -1; }; }; return -1; }; fn nan64(s: str) bool = { match (stof64(s, base.DEC)) { case let v: f64 => { return math.isnan(v); }; case let e: invalid => { return false; }; case let e: overflow => { return false; }; }; return false; }; fn nan32(s: str) bool = { match (stof32(s, base.DEC)) { case let v: f32 => { return math.isnan((v: f64)); }; case let e: invalid => { return false; }; case let e: overflow => { return false; }; }; return false; }; // ---- stof64 ---------------------------------------------------------- // ref/hare/strconv/stof.ha:530. @test fn stof64_dec() void = { let inf: f64 = math.f64frombits(math.INF_BITS); let ninf: f64 = math.f64frombits(0xFFF0000000000000u64); let nzero: f64 = math.f64frombits(1u64 << 63u64); // -0.0 (1<<63 dodges the I64_MIN-literal emit bug #144) let subn: f64 = math.f64frombits(0x0000000000000001u64); // 5e-324 if (!chk64("0", base.DEC, 0.0)) { fail(); }; if (!chk64("200", base.DEC, 200.0)) { fail(); }; if (!chk64("12345", base.DEC, 12345.0)) { fail(); }; if (!chk64("+112233445566778899", base.DEC, 1.122334455667789e17)) { fail(); }; if (!chk64("3.14", base.DEC, 3.14)) { fail(); }; if (!chk64("2.99792458E+8", base.DEC, 299792458.0)) { fail(); }; if (!chk64("6.022e23", base.DEC, 6.022e23)) { fail(); }; if (!ovf64("1e310", base.DEC)) { fail(); }; if (!chk64("9007199254740991", base.DEC, 9007199254740991.0)) { fail(); }; if (!chk64("90071992547409915", base.DEC, 90071992547409920.0)) { fail(); }; if (!chk64("90071992547409925", base.DEC, 90071992547409920.0)) { fail(); }; if (!chk64("2.2250738585072014e-308", base.DEC, 2.2250738585072014e-308)) { fail(); }; if (!chk64("-1e-324", base.DEC, nzero)) { fail(); }; if (!chk64("5e-324", base.DEC, subn)) { fail(); }; // Decimal-engine edge probes migrated here when the white-box // decimaltest.ww retired (#16): the external-test idiom can't reach // strconv's unexported decimal fns, so these two black-box rows pin // the only edges not already guaranteed transitively. C1: all-9s // round-up carry + decimal_round dp>18 boundary. C2: nd>19 skips // eisel-lemire, forcing the pure-decimal slow path (extra trim). // Micro-gap (rule-7, honest): decimal_shift's k=0 early-return no-op // is a trivial guard, not reached transitively by any slow-path input // — consciously un-migrated, not silently dropped. if (!chk64("9999999999999999", base.DEC, 1.0e16)) { fail(); }; if (!chk64("100000000000000000001", base.DEC, 1.0e20)) { fail(); }; if (inv64("") != 0) { fail(); }; if (inv64("0ZO") != 1) { fail(); }; if (inv64("1.23ezz") != 5) { fail(); }; if (!chk64("Infinity", base.DEC, inf)) { fail(); }; if (!chk64("+Infinity", base.DEC, inf)) { fail(); }; if (!chk64("-Infinity", base.DEC, ninf)) { fail(); }; if (!chk64("infinity", base.DEC, inf)) { fail(); }; if (!chk64("inFinIty", base.DEC, inf)) { fail(); }; if (!chk64("-infinity", base.DEC, ninf)) { fail(); }; if (!chk64("-infiNity", base.DEC, ninf)) { fail(); }; if (!nan64("NaN")) { fail(); }; if (!nan64("nan")) { fail(); }; if (!nan64("naN")) { fail(); }; }; // ---- stof32 ---------------------------------------------------------- // ref/hare/strconv/stof.ha:560. @test fn stof32_dec() void = { let inf: f32 = math.f32frombits(0x7F800000u32); let ninf: f32 = math.f32frombits(0xFF800000u32); let nzero: f32 = math.f32frombits(0x80000000u32); // -0.0 let subn: f32 = math.f32frombits(0x00000001u32); // 1e-45 if (!chk32("0", base.DEC, 0.0f32)) { fail(); }; if (!chk32("1e10", base.DEC, 1.0e10f32)) { fail(); }; if (!chk32("299792458", base.DEC, 299792458.0f32)) { fail(); }; if (!chk32("6.022e23", base.DEC, 6.022e23f32)) { fail(); }; if (!ovf32("1e40", base.DEC)) { fail(); }; if (!chk32("16777215", base.DEC, 16777215.0f32)) { fail(); }; if (!chk32("167772155", base.DEC, 167772160.0f32)) { fail(); }; if (!chk32("167772145", base.DEC, 167772140.0f32)) { fail(); }; if (!chk32("6.62607015e-34", base.DEC, 6.62607015e-34f32)) { fail(); }; if (!chk32("1.1754944e-38", base.DEC, 1.1754944e-38f32)) { fail(); }; if (!chk32("-1e-50", base.DEC, nzero)) { fail(); }; if (!chk32("1e-45", base.DEC, subn)) { fail(); }; if (inv32("") != 0) { fail(); }; if (inv32("0ZO") != 1) { fail(); }; if (inv32("1.23e-zz") != 6) { fail(); }; if (!chk32("Infinity", base.DEC, inf)) { fail(); }; if (!chk32("+Infinity", base.DEC, inf)) { fail(); }; if (!chk32("-Infinity", base.DEC, ninf)) { fail(); }; if (!chk32("infinity", base.DEC, inf)) { fail(); }; if (!chk32("inFinIty", base.DEC, inf)) { fail(); }; if (!chk32("-infinity", base.DEC, ninf)) { fail(); }; if (!chk32("-infiniTy", base.DEC, ninf)) { fail(); }; if (!nan32("NaN")) { fail(); }; if (!nan32("nan")) { fail(); }; if (!nan32("naN")) { fail(); }; if (!chk32("9.19100241453305036800e+20", base.DEC, 9.19100241453305036800e+20f32)) { fail(); }; }; // ---- stofhex --------------------------------------------------------- // ref/hare/strconv/stof.ha:590. Hex-float surface-form literals (0x1.fp-2, // math::F64_MAX_NORMAL, …) are spelled as IEEE-754 bit patterns since ww // has no hex-float literal lexer / no F*_MAX_NORMAL math consts. @test fn stof64_hex() void = { if (!chk64("0p0", base.HEX, 0.0)) { fail(); }; if (!chk64("1p0", base.HEX, 1.0)) { fail(); }; if (!chk64("-1p0", base.HEX_LOWER, -1.0)) { fail(); }; // 0x1.fp-2 = 1.9375 * 2^-2 = 0.484375 (exact). if (!chk64("1.fp-2", base.HEX, 0.484375)) { fail(); }; // F64_MAX_NORMAL. if (!chk64("1.fffffffffffffp+1023", base.HEX, math.f64frombits(0x7FEFFFFFFFFFFFFFu64))) { fail(); }; // F64_MIN_NORMAL. if (!chk64("1.0000000000000p-1022", base.HEX, math.f64frombits(0x0010000000000000u64))) { fail(); }; // F64_MIN_SUBNORMAL. if (!chk64("0.0000000000001p-1022", base.HEX, math.f64frombits(0x0000000000000001u64))) { fail(); }; if (!ovf64("1p+1024", base.HEX)) { fail(); }; if (!chk64("0.00000000000001p-1022", base.HEX, 0.0)) { fail(); }; }; @test fn stof32_hex() void = { if (!chk32("0p0", base.HEX, 0.0f32)) { fail(); }; if (!chk32("1p0", base.HEX, 1.0f32)) { fail(); }; if (!chk32("-1p0", base.HEX, -1.0f32)) { fail(); }; if (!chk32("1.fp-2", base.HEX, 0.484375f32)) { fail(); }; // F32_MAX_NORMAL. if (!chk32("1.fffffd586b834p+127", base.HEX, math.f32frombits(0x7F7FFFFFu32))) { fail(); }; // F32_MIN_NORMAL. if (!chk32("1.0p-126", base.HEX, math.f32frombits(0x00800000u32))) { fail(); }; // F32_MIN_SUBNORMAL. if (!chk32("1.6p-150", base.HEX, math.f32frombits(0x00000001u32))) { fail(); }; if (!ovf32("1.0p+128", base.HEX)) { fail(); }; if (!chk32("1.0p-151", base.HEX, 0.0f32)) { fail(); }; }; export fn main() i32 = { signalled = 1; stof64_dec(); signalled = 2; stof32_dec(); signalled = 3; stof64_hex(); signalled = 4; stof32_hex(); return 0; };