From: Russ Cox Date: Sun, 2 Nov 2025 14:59:59 +0000 (-0500) Subject: internal/strconv: add tests and benchmarks for ftoaFixed X-Git-Tag: go1.26rc1~385 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=162ba6cc405851ee7f0d30b600de1a308321ddca;p=gostls13.git internal/strconv: add tests and benchmarks for ftoaFixed ftoaFixed is in the next CL; this proves the tests are correct against the current implementation, and it adds a benchmark for comparison with the new implementation. Change-Id: I7ac8a1f699b693ea6d11a7122b22fc70cc135af6 Reviewed-on: https://go-review.googlesource.com/c/go/+/717181 Auto-Submit: Russ Cox Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- diff --git a/src/internal/strconv/fp_test.go b/src/internal/strconv/fp_test.go index 042328c7d4..ba739941cc 100644 --- a/src/internal/strconv/fp_test.go +++ b/src/internal/strconv/fp_test.go @@ -99,12 +99,14 @@ func TestFp(t *testing.T) { s := bufio.NewScanner(strings.NewReader(testfp)) for lineno := 1; s.Scan(); lineno++ { line := s.Text() - if len(line) == 0 || line[0] == '#' { + line, _, _ = strings.Cut(line, "#") + line = strings.TrimSpace(line) + if line == "" { continue } a := strings.Split(line, " ") if len(a) != 4 { - t.Error("testdata/testfp.txt:", lineno, ": wrong field count") + t.Errorf("testdata/testfp.txt:%d: wrong field count", lineno) continue } var s string @@ -114,22 +116,21 @@ func TestFp(t *testing.T) { var ok bool v, ok = myatof64(a[2]) if !ok { - t.Error("testdata/testfp.txt:", lineno, ": cannot atof64 ", a[2]) + t.Errorf("testdata/testfp.txt:%d: cannot atof64 %s", lineno, a[2]) continue } s = fmt.Sprintf(a[1], v) case "float32": v1, ok := myatof32(a[2]) if !ok { - t.Error("testdata/testfp.txt:", lineno, ": cannot atof32 ", a[2]) + t.Errorf("testdata/testfp.txt:%d: cannot atof32 %s", lineno, a[2]) continue } s = fmt.Sprintf(a[1], v1) v = float64(v1) } if s != a[3] { - t.Error("testdata/testfp.txt:", lineno, ": ", a[0], " ", a[1], " ", a[2], " (", v, ") ", - "want ", a[3], " got ", s) + t.Errorf("testdata/testfp.txt:%d: %s %s %s %s: have %s want %s", lineno, a[0], a[1], a[2], a[3], s, a[3]) } } if s.Err() != nil { diff --git a/src/internal/strconv/ftoa_test.go b/src/internal/strconv/ftoa_test.go index 14d1200ff2..d510629537 100644 --- a/src/internal/strconv/ftoa_test.go +++ b/src/internal/strconv/ftoa_test.go @@ -5,9 +5,9 @@ package strconv_test import ( + . "internal/strconv" "math" "math/rand" - . "internal/strconv" "testing" ) @@ -294,8 +294,10 @@ var ftoaBenches = []struct { {"64Fixed1", 123456, 'e', 3, 64}, {"64Fixed2", 123.456, 'e', 3, 64}, + {"64Fixed2.5", 1.2345e+06, 'e', 3, 64}, {"64Fixed3", 1.23456e+78, 'e', 3, 64}, {"64Fixed4", 1.23456e-78, 'e', 3, 64}, + {"64Fixed5Hard", 4.096e+25, 'e', 5, 64}, // needs divisiblePow5(..., 20) {"64Fixed12", 1.23456e-78, 'e', 12, 64}, {"64Fixed16", 1.23456e-78, 'e', 16, 64}, // From testdata/testfp.txt @@ -303,6 +305,10 @@ var ftoaBenches = []struct { {"64Fixed17Hard", math.Ldexp(8887055249355788, 665), 'e', 17, 64}, {"64Fixed18Hard", math.Ldexp(6994187472632449, 690), 'e', 18, 64}, + {"64FixedF1", 123.456, 'f', 6, 64}, + {"64FixedF2", 0.0123, 'f', 6, 64}, + {"64FixedF3", 12.3456, 'f', 2, 64}, + // Trigger slow path (see issue #15672). // The shortest is: 8.034137530808823e+43 {"Slowpath64", 8.03413753080882349e+43, 'e', -1, 64},