]> Cypherpunks repositories - gostls13.git/commitdiff
internal/strconv: add tests and benchmarks for ftoaFixed
authorRuss Cox <rsc@golang.org>
Sun, 2 Nov 2025 14:59:59 +0000 (09:59 -0500)
committerGopher Robot <gobot@golang.org>
Tue, 4 Nov 2025 04:09:15 +0000 (20:09 -0800)
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 <rsc@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/internal/strconv/fp_test.go
src/internal/strconv/ftoa_test.go

index 042328c7d4e4c800fa4ee2d16e5013d4e6d8a544..ba739941cc8a920f64d822e6d65e2adfcf8f3cea 100644 (file)
@@ -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 {
index 14d1200ff26b487be8a5753f38de04ec3c686c5b..d510629537548bbae7ff529d9e7fcdc2d8dcf09c 100644 (file)
@@ -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},