Skip to content

Commit 162ba6c

Browse files
rscgopherbot
authored andcommitted
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>
1 parent 9795c7b commit 162ba6c

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/internal/strconv/fp_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,14 @@ func TestFp(t *testing.T) {
9999
s := bufio.NewScanner(strings.NewReader(testfp))
100100
for lineno := 1; s.Scan(); lineno++ {
101101
line := s.Text()
102-
if len(line) == 0 || line[0] == '#' {
102+
line, _, _ = strings.Cut(line, "#")
103+
line = strings.TrimSpace(line)
104+
if line == "" {
103105
continue
104106
}
105107
a := strings.Split(line, " ")
106108
if len(a) != 4 {
107-
t.Error("testdata/testfp.txt:", lineno, ": wrong field count")
109+
t.Errorf("testdata/testfp.txt:%d: wrong field count", lineno)
108110
continue
109111
}
110112
var s string
@@ -114,22 +116,21 @@ func TestFp(t *testing.T) {
114116
var ok bool
115117
v, ok = myatof64(a[2])
116118
if !ok {
117-
t.Error("testdata/testfp.txt:", lineno, ": cannot atof64 ", a[2])
119+
t.Errorf("testdata/testfp.txt:%d: cannot atof64 %s", lineno, a[2])
118120
continue
119121
}
120122
s = fmt.Sprintf(a[1], v)
121123
case "float32":
122124
v1, ok := myatof32(a[2])
123125
if !ok {
124-
t.Error("testdata/testfp.txt:", lineno, ": cannot atof32 ", a[2])
126+
t.Errorf("testdata/testfp.txt:%d: cannot atof32 %s", lineno, a[2])
125127
continue
126128
}
127129
s = fmt.Sprintf(a[1], v1)
128130
v = float64(v1)
129131
}
130132
if s != a[3] {
131-
t.Error("testdata/testfp.txt:", lineno, ": ", a[0], " ", a[1], " ", a[2], " (", v, ") ",
132-
"want ", a[3], " got ", s)
133+
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])
133134
}
134135
}
135136
if s.Err() != nil {

src/internal/strconv/ftoa_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
package strconv_test
66

77
import (
8+
. "internal/strconv"
89
"math"
910
"math/rand"
10-
. "internal/strconv"
1111
"testing"
1212
)
1313

@@ -294,15 +294,21 @@ var ftoaBenches = []struct {
294294

295295
{"64Fixed1", 123456, 'e', 3, 64},
296296
{"64Fixed2", 123.456, 'e', 3, 64},
297+
{"64Fixed2.5", 1.2345e+06, 'e', 3, 64},
297298
{"64Fixed3", 1.23456e+78, 'e', 3, 64},
298299
{"64Fixed4", 1.23456e-78, 'e', 3, 64},
300+
{"64Fixed5Hard", 4.096e+25, 'e', 5, 64}, // needs divisiblePow5(..., 20)
299301
{"64Fixed12", 1.23456e-78, 'e', 12, 64},
300302
{"64Fixed16", 1.23456e-78, 'e', 16, 64},
301303
// From testdata/testfp.txt
302304
{"64Fixed12Hard", math.Ldexp(6965949469487146, -249), 'e', 12, 64},
303305
{"64Fixed17Hard", math.Ldexp(8887055249355788, 665), 'e', 17, 64},
304306
{"64Fixed18Hard", math.Ldexp(6994187472632449, 690), 'e', 18, 64},
305307

308+
{"64FixedF1", 123.456, 'f', 6, 64},
309+
{"64FixedF2", 0.0123, 'f', 6, 64},
310+
{"64FixedF3", 12.3456, 'f', 2, 64},
311+
306312
// Trigger slow path (see issue #15672).
307313
// The shortest is: 8.034137530808823e+43
308314
{"Slowpath64", 8.03413753080882349e+43, 'e', -1, 64},

0 commit comments

Comments
 (0)