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 <rsc@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Russ Cox 2025-11-02 09:59:59 -05:00 committed by Gopher Robot
parent 9795c7ba22
commit 162ba6cc40
2 changed files with 14 additions and 7 deletions

View file

@ -99,12 +99,14 @@ func TestFp(t *testing.T) {
s := bufio.NewScanner(strings.NewReader(testfp)) s := bufio.NewScanner(strings.NewReader(testfp))
for lineno := 1; s.Scan(); lineno++ { for lineno := 1; s.Scan(); lineno++ {
line := s.Text() line := s.Text()
if len(line) == 0 || line[0] == '#' { line, _, _ = strings.Cut(line, "#")
line = strings.TrimSpace(line)
if line == "" {
continue continue
} }
a := strings.Split(line, " ") a := strings.Split(line, " ")
if len(a) != 4 { if len(a) != 4 {
t.Error("testdata/testfp.txt:", lineno, ": wrong field count") t.Errorf("testdata/testfp.txt:%d: wrong field count", lineno)
continue continue
} }
var s string var s string
@ -114,22 +116,21 @@ func TestFp(t *testing.T) {
var ok bool var ok bool
v, ok = myatof64(a[2]) v, ok = myatof64(a[2])
if !ok { 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 continue
} }
s = fmt.Sprintf(a[1], v) s = fmt.Sprintf(a[1], v)
case "float32": case "float32":
v1, ok := myatof32(a[2]) v1, ok := myatof32(a[2])
if !ok { 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 continue
} }
s = fmt.Sprintf(a[1], v1) s = fmt.Sprintf(a[1], v1)
v = float64(v1) v = float64(v1)
} }
if s != a[3] { if s != a[3] {
t.Error("testdata/testfp.txt:", lineno, ": ", a[0], " ", a[1], " ", a[2], " (", v, ") ", 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])
"want ", a[3], " got ", s)
} }
} }
if s.Err() != nil { if s.Err() != nil {

View file

@ -5,9 +5,9 @@
package strconv_test package strconv_test
import ( import (
. "internal/strconv"
"math" "math"
"math/rand" "math/rand"
. "internal/strconv"
"testing" "testing"
) )
@ -294,8 +294,10 @@ var ftoaBenches = []struct {
{"64Fixed1", 123456, 'e', 3, 64}, {"64Fixed1", 123456, 'e', 3, 64},
{"64Fixed2", 123.456, 'e', 3, 64}, {"64Fixed2", 123.456, 'e', 3, 64},
{"64Fixed2.5", 1.2345e+06, 'e', 3, 64},
{"64Fixed3", 1.23456e+78, 'e', 3, 64}, {"64Fixed3", 1.23456e+78, 'e', 3, 64},
{"64Fixed4", 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}, {"64Fixed12", 1.23456e-78, 'e', 12, 64},
{"64Fixed16", 1.23456e-78, 'e', 16, 64}, {"64Fixed16", 1.23456e-78, 'e', 16, 64},
// From testdata/testfp.txt // From testdata/testfp.txt
@ -303,6 +305,10 @@ var ftoaBenches = []struct {
{"64Fixed17Hard", math.Ldexp(8887055249355788, 665), 'e', 17, 64}, {"64Fixed17Hard", math.Ldexp(8887055249355788, 665), 'e', 17, 64},
{"64Fixed18Hard", math.Ldexp(6994187472632449, 690), 'e', 18, 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). // Trigger slow path (see issue #15672).
// The shortest is: 8.034137530808823e+43 // The shortest is: 8.034137530808823e+43
{"Slowpath64", 8.03413753080882349e+43, 'e', -1, 64}, {"Slowpath64", 8.03413753080882349e+43, 'e', -1, 64},