# The laws of bend-float64's format mini-language (spec.bend), at literal inputs; expected results are Python's # format(x, spec). test/spec_check.py compares a million random specs with Python. # Gate: ./bend-check SPEC_PROOF.bend --verdict import Base import ./float64.bend as F import ./spec.bend as S # Guarantees: zero padding groups its zeros (010,.1f: 0,001,234.5); fill and centring (*^15,.2f: the extra fill on # the right); '<' with '0' pads with zeros on the right (<+08.2f: -1.50000); '+' signs a NaN (+nan); zero padding # of inf (0000000inf); z drops a negative zero after rounding (-0.04 at z.1f: 0.0); '#' keeps the point (1.); # no type with a precision switches to an exponent one place sooner (12 at .2: 1.2e+01); no type, no precision is # repr (1.0); '%' (0.5 at .1%: 50.0%), and after -inf (-inf%). # Why it matters: each is a rule of Python's mini-language that a reimplementation easily gets wrong; output that # differs from Python's breaks tables and logs compared across languages. law spec_layout: {[S.format(F.of_bits(1083394560, 0), "010,.1f"), S.format(F.of_bits(1083394560, 0), "*^15,.2f"), S.format(F.of_bits(3220701184, 0), "<+08.2f"), S.format(F.of_bits(2146959360, 0), "+"), S.format(F.of_bits(2146435072, 0), "010f"), S.format(F.of_bits(3215227617, 1202590843), "z.1f"), S.format(F.of_bits(1072693248, 0), "#.0f"), S.format(F.of_bits(1076363264, 0), ".2"), S.format(F.of_bits(1072693248, 0), ""), S.format(F.of_bits(1071644672, 0), ".1%"), S.format(F.of_bits(4293918720, 0), "%")] == [Done{"0,001,234.5"}, Done{"***1,234.50****"}, Done{"-1.50000"}, Done{"+nan"}, Done{"0000000inf"}, Done{"0.0"}, Done{"1."}, Done{"1.2e+01"}, Done{"1.0"}, Done{"50.0%"}, Done{"-inf%"}] : List<&2, Result<&2, &2, String, String>>} # Guarantees: specs Python refuses are refused with Python's message: a point without digits, both ',' and '_', an # unknown type, a grouping with a non-float type (checked first), an unprintable type code (written \x20), two # characters left over; and type 'n' is refused (it depends on the locale). # Why it matters: a caller passing a user's spec must get the same error Python would give, never a guess. law spec_errors: {[S.format(F.of_bits(1073217536, 0), ".f"), S.format(F.of_bits(1073217536, 0), ",_"), S.format(F.of_bits(1073217536, 0), "d"), S.format(F.of_bits(1073217536, 0), "_7"), S.format(F.of_bits(1073217536, 0), "2 "), S.format(F.of_bits(1073217536, 0), "10.2,f"), S.format(F.of_bits(1073217536, 0), "n")] == [Fail{"Format specifier missing precision"}, Fail{"Cannot specify both ',' and '_'."}, Fail{"Unknown format code 'd' for object of type 'float'"}, Fail{"Cannot specify '_' with '7'."}, Fail{"Unknown format code '\\x20' for object of type 'float'"}, Fail{"Invalid format specifier '10.2,f' for object of type 'float'"}, Fail{"format type 'n' is not supported (it depends on the locale)"}] : List<&2, Result<&2, &2, String, String>>}