| 1 | import py |
| 2 | from rpython.rlib.rdtoa import strtod, dtoa, dtoa_formatd |
| 3 | from rpython.rlib import rfloat |
| 4 | |
| 5 | def test_strtod(): |
| 6 | assert strtod("12345") == 12345.0 |
| 7 | assert strtod("1.1") == 1.1 |
| 8 | assert strtod("3.47") == 3.47 |
| 9 | assert strtod(".125") == .125 |
| 10 | py.test.raises(ValueError, strtod, "123A") |
| 11 | py.test.raises(ValueError, strtod, "") |
| 12 | py.test.raises(ValueError, strtod, " ") |
| 13 | py.test.raises(ValueError, strtod, "\0") |
| 14 | py.test.raises(ValueError, strtod, "3\09") |
| 15 | |
| 16 | def test_dtoa(): |
| 17 | assert dtoa(3.47) == "3.47" |
| 18 | assert dtoa(1.1) == "1.1" |
| 19 | assert dtoa(-1.1) == "-1.1" |
| 20 | assert dtoa(1.1, flags=rfloat.DTSF_SIGN) == "+1.1" |
| 21 | assert dtoa(12.3577) == "12.3577" |
| 22 | assert dtoa(10.0) == "10" |
| 23 | assert dtoa(1.0e100) == "1e+100" |
| 24 | |
| 25 | assert dtoa(rfloat.INFINITY) == 'inf' |
| 26 | assert dtoa(-rfloat.INFINITY) == '-inf' |
| 27 | assert dtoa(rfloat.NAN) == 'nan' |
| 28 | |
| 29 | def test_dtoa_precision(): |
| 30 | assert dtoa(1.1, code='f', precision=2) == "1.10" |
| 31 | assert dtoa(1e12, code='g', precision=12) == "1e+12" |
| 32 | |
| 33 | def test_flag_cut_exp_0(): |
| 34 | assert dtoa(1.1e9, code="g", precision=2, flags=rfloat.DTSF_CUT_EXP_0) == "1.1e+9" |
| 35 | assert dtoa(1.1e-9, code="g", precision=2, flags=rfloat.DTSF_CUT_EXP_0) == "1.1e-9" |
| 36 | |
| 37 | def test_g_trailing_zero_bug(): |
| 38 | assert dtoa_formatd(1505.0, "g", 3, 0) == "1.5e+03" |
| 39 | |
| 40 | def test_flag_neg_zero(): |
| 41 | assert dtoa(-0.0, code="g", precision=2, flags=rfloat.DTSF_NO_NEG_0) == "0" |