Debian Codegraph @

strconv/float_test.go

raw
1 package strconv
2
3 import (
4 "fmt"
5 "math"
6 "math/rand"
7 "strconv"
8 "strings"
9 "testing"
10
11 "github.com/tdewolff/test"
12 )
13
14 func TestParseFloat(t *testing.T) {
15 floatTests := []struct {
16 f string
17 expected float64
18 }{
19 {"5", 5},
20 {"5.1", 5.1},
21 {"-5.1", -5.1},
22 {"5.1e-2", 5.1e-2},
23 {"5.1e+2", 5.1e+2},
24 {"0.0e1", 0.0e1},
25 {"18446744073709551620", 18446744073709551620.0},
26 {"1e23", 1e23},
27 // mantissa truncated in the integer part, with a dot
28 {"123456789123456789123.5", 1.234567891234568e+20},
29 {"123456789123456789123.", 1.234567891234568e+20},
30 {"-123456789123456789123.5", -1.234567891234568e+20},
31 {"123456789123456789123.5e3", 1.234567891234568e+23},
32 {"99999999999999999999.0", 1e20},
33 {"1234567891234567891234.25", 1.234567891234568e+21},
34 // uint64 accumulator at its limit
35 {"18446744073709551615", 18446744073709551615.0},
36 {"18446744073709551616", 1.8446744073709552e+19},
37 {"18446744073709551616.5", 1.8446744073709552e+19},
38 {"-18446744073709551616", -1.8446744073709552e+19},
39 {"184467440737095516165", 1.844674407370955e+20},
40 // 15 digits with 22 < exp, so the int*10^k path bails out
41 {"993349238352373e23", 9.93349238352373e+37},
42 // math.Pow10 is 0 below 1e-323 and infinite above 1e308
43 {"0.0000000000000000000001610832207361e330", 1.610832207361e+308},
44 {"5e-324", 5e-324},
45 {"9e-324", 1e-323},
46 {"0e309", 0},
47 // TODO: hard to test due to float imprecision
48 // {"1.7976931348623e+308", 1.7976931348623e+308)
49 // {"4.9406564584124e-308", 4.9406564584124e-308)
50 }
51 for _, tt := range floatTests {
52 t.Run(fmt.Sprint(tt.f), func(t *testing.T) {
53 f, n := ParseFloat([]byte(tt.f))
54 test.T(t, n, len(tt.f))
55 test.T(t, f, tt.expected)
56 })
57 }
58 }
59
60 // Literals that need scaling in more than one step, where a single math.Pow10
61 // leaves its domain. Compared against the stdlib within a few ULP.
62 func TestParseFloatScaling(t *testing.T) {
63 floatTests := []string{
64 "1" + strings.Repeat("0", 41) + "e-330",
65 "1" + strings.Repeat("0", 60) + "e-360",
66 "0." + strings.Repeat("0", 100) + "1e400",
67 "0." + strings.Repeat("0", 200) + "5e450",
68 "-1" + strings.Repeat("0", 41) + "e-330",
69 "-0e309",
70 }
71 for _, tt := range floatTests {
72 t.Run(fmt.Sprint(len(tt)), func(t *testing.T) {
73 expected, err := strconv.ParseFloat(tt, 64)
74 test.Error(t, err)
75 f, n := ParseFloat([]byte(tt))
76 test.T(t, n, len(tt))
77 if math.Abs(f-expected) > 1e-14*math.Abs(expected) {
78 test.Fail(t, fmt.Sprintf("%v != %v for %v", f, expected, tt))
79 }
80 })
81 }
82 }
83
84 func TestParseFloatError(t *testing.T) {
85 floatTests := []struct {
86 f string
87 n int
88 expected float64
89 }{
90 {"e1", 0, 0},
91 {".", 0, 0},
92 {"1e", 1, 1},
93 {"1e+", 1, 1},
94 {"1e+1", 4, 10},
95 }
96 for _, tt := range floatTests {
97 t.Run(fmt.Sprint(tt.f), func(t *testing.T) {
98 f, n := ParseFloat([]byte(tt.f))
99 test.T(t, n, tt.n)
100 test.T(t, f, tt.expected)
101 })
102 }
103 }
104
105 func TestAppendFloat(t *testing.T) {
106 floatTests := []struct {
107 f float64
108 prec int
109 expected string
110 }{
111 {0, 6, "0"},
112 {1, 6, "1"},
113 {9, 6, "9"},
114 {9.99999, 6, "9.99999"},
115 {123, 6, "123"},
116 {0.123456, 6, ".123456"},
117 {0.066, 6, ".066"},
118 {0.0066, 6, ".0066"},
119 {12e2, 6, "1200"},
120 {12e3, 6, "12e3"},
121 {0.1, 6, ".1"},
122 {0.001, 6, ".001"},
123 {0.0001, 6, "1e-4"},
124 {-1, 6, "-1"},
125 {-123, 6, "-123"},
126 {-123.456, 6, "-123.456"},
127 {-12e3, 6, "-12e3"},
128 {-0.1, 6, "-.1"},
129 {-0.0001, 6, "-1e-4"},
130 {0.000100009, 10, "100009e-9"},
131 {0.0001000009, 10, "1.000009e-4"},
132 {1e18, 0, "1e18"},
133 //{1e19, 0, "1e19"},
134 //{1e19, 18, "1e19"},
135 {1e1, 0, "10"},
136 {1e2, 1, "100"},
137 {1e3, 2, "1e3"},
138 {1e10, -1, "1e10"},
139 {1e15, -1, "1e15"},
140 {1e-5, 6, "1e-5"},
141 {math.NaN(), 0, ""},
142 {math.Inf(1), 0, ""},
143 {math.Inf(-1), 0, ""},
144 {0, 19, "0"},
145 {0.000923361977200859392, -1, "9.23361977200859392e-4"},
146 {1234, 2, "1.23e3"},
147 {12345, 2, "1.23e4"},
148 {12.345, 2, "12.3"},
149 {12.345, 3, "12.34"},
150 }
151 for _, tt := range floatTests {
152 t.Run(fmt.Sprint(tt.f), func(t *testing.T) {
153 f := AppendFloat([]byte{}, tt.f, tt.prec)
154 test.String(t, string(f), tt.expected)
155 })
156 }
157
158 b := make([]byte, 0, 22)
159 AppendFloat(b, 12.34, -1)
160 test.String(t, string(b[:5]), "12.34", "in buffer")
161 }
162
163 func FuzzParseFloat(f *testing.F) {
164 f.Add("5")
165 f.Add("99")
166 f.Add("18446744073709551615")
167 f.Add("5")
168 f.Add("5.1")
169 f.Add("-5.1")
170 f.Add("5.1e-2")
171 f.Add("5.1e+2")
172 f.Add("0.0e1")
173 f.Add("18446744073709551620")
174 f.Add("1e23")
175 f.Fuzz(func(t *testing.T, s string) {
176 ParseFloat([]byte(s))
177 })
178 }
179
180 func FuzzAppendFloat(f *testing.F) {
181 f.Add(0.0, 6)
182 f.Add(1.0, 6)
183 f.Add(9.0, 6)
184 f.Add(9.99999, 6)
185 f.Add(123.0, 6)
186 f.Add(0.123456, 6)
187 f.Add(0.066, 6)
188 f.Add(0.0066, 6)
189 f.Add(12e2, 6)
190 f.Add(12e3, 6)
191 f.Add(0.1, 6)
192 f.Add(0.001, 6)
193 f.Add(0.0001, 6)
194 f.Add(-1.0, 6)
195 f.Add(-123.0, 6)
196 f.Add(-123.456, 6)
197 f.Add(-12e3, 6)
198 f.Add(-0.1, 6)
199 f.Add(-0.0001, 6)
200 f.Add(0.000100009, 10)
201 f.Add(0.0001000009, 10)
202 f.Add(1e18, 0)
203 f.Add(1e1, 0)
204 f.Add(1e2, 1)
205 f.Add(1e3, 2)
206 f.Add(1e10, -1)
207 f.Add(1e15, -1)
208 f.Add(1e-5, 6)
209 f.Add(math.NaN(), 0)
210 f.Add(math.Inf(1), 0)
211 f.Add(math.Inf(-1), 0)
212 f.Add(0.0, 19)
213 f.Add(0.000923361977200859392, -1)
214 f.Add(1234.0, 2)
215 f.Add(12345.0, 2)
216 f.Add(12.345, 2)
217 f.Add(12.345, 3)
218 f.Fuzz(func(t *testing.T, f float64, prec int) {
219 AppendFloat([]byte{}, f, prec)
220 })
221 }
222
223 ////////////////////////////////////////////////////////////////
224
225 // ParseFloat keeps at most 19-20 significant digits, so it is not bit-exact
226 // against the stdlib, but it must stay within a few ULP of it.
227 func TestParseFloatRandom(t *testing.T) {
228 N := int(2e5)
229 if testing.Short() {
230 N = int(1e4)
231 }
232 r := rand.New(rand.NewSource(6))
233 bad := 0
234 for i := 0; i < N; i++ {
235 var sb strings.Builder
236 if r.Intn(4) == 0 {
237 sb.WriteByte('-')
238 }
239 // vary the integer length across the uint64 accumulator limit
240 sb.WriteByte(byte('1' + r.Intn(9)))
241 for j := 1 + r.Intn(30); 0 < j; j-- {
242 sb.WriteByte(byte('0' + r.Intn(10)))
243 }
244 if frac := r.Intn(14); 0 < frac || r.Intn(4) == 0 {
245 sb.WriteByte('.')
246 for ; 0 < frac; frac-- {
247 sb.WriteByte(byte('0' + r.Intn(10)))
248 }
249 }
250 if r.Intn(2) == 0 {
251 sb.WriteString("e")
252 sb.WriteString(strconv.Itoa(r.Intn(700) - 350))
253 }
254 s := sb.String()
255 expected, err := strconv.ParseFloat(s, 64)
256 if err != nil || math.IsInf(expected, 0) || math.Abs(expected) < 1e-280 {
257 continue // below 1e-280 scaling runs into subnormals
258 }
259 f, n := ParseFloat([]byte(s))
260 if n != len(s) || math.Abs(f-expected) > 1e-14*math.Abs(expected) {
261 if bad++; bad < 10 {
262 t.Errorf("ParseFloat(%v): %v (%v bytes) != %v", s, f, n, expected)
263 }
264 }
265 }
266 if 0 < bad {
267 t.Errorf("%v of %v literals differ from strconv.ParseFloat", bad, N)
268 }
269 }
270
271 func TestAppendFloatRandom(t *testing.T) {
272 N := int(1e6)
273 if testing.Short() {
274 N = 0
275 }
276 r := rand.New(rand.NewSource(99))
277 //prec := 10
278 for i := 0; i < N; i++ {
279 f := r.ExpFloat64()
280 //f = math.Floor(f*float64(prec)) / float64(prec)
281
282 b := AppendFloat([]byte{}, f, -1)
283 f2, _ := strconv.ParseFloat(string(b), 64)
284 if math.Abs(f-f2) > 1e-6 {
285 fmt.Println("Bad:", f, "!=", f2, "in", string(b))
286 }
287 }
288 }
289
290 func BenchmarkFloatToBytes1(b *testing.B) {
291 r := []byte{} //make([]byte, 10)
292 f := 123.456
293 for i := 0; i < b.N; i++ {
294 r = strconv.AppendFloat(r[:0], f, 'g', 6, 64)
295 }
296 }
297
298 func BenchmarkFloatToBytes2(b *testing.B) {
299 r := make([]byte, 10)
300 f := 123.456
301 for i := 0; i < b.N; i++ {
302 r = AppendFloat(r[:0], f, 6)
303 }
304 }
305
306 func BenchmarkModf1(b *testing.B) {
307 f := 123.456
308 x := 0.0
309 for i := 0; i < b.N; i++ {
310 a, b := math.Modf(f)
311 x += a + b
312 }
313 }
314
315 func BenchmarkModf2(b *testing.B) {
316 f := 123.456
317 x := 0.0
318 for i := 0; i < b.N; i++ {
319 a := float64(int64(f))
320 b := f - a
321 x += a + b
322 }
323 }
324
325 func BenchmarkPrintInt1(b *testing.B) {
326 X := int64(123456789)
327 n := LenInt(X)
328 r := make([]byte, n)
329 for i := 0; i < b.N; i++ {
330 x := X
331 j := n
332 for x > 0 {
333 j--
334 r[j] = '0' + byte(x%10)
335 x /= 10
336 }
337 }
338 }
339
340 func BenchmarkPrintInt2(b *testing.B) {
341 X := int64(123456789)
342 n := LenInt(X)
343 r := make([]byte, n)
344 for i := 0; i < b.N; i++ {
345 x := X
346 j := n
347 for x > 0 {
348 j--
349 newX := x / 10
350 r[j] = '0' + byte(x-10*newX)
351 x = newX
352 }
353 }
354 }
355
356 func BenchmarkPrintInt3(b *testing.B) {
357 X := int64(123456789)
358 n := LenInt(X)
359 r := make([]byte, n)
360 for i := 0; i < b.N; i++ {
361 x := X
362 j := 0
363 for j < n {
364 pow := int64pow10[n-j-1]
365 tmp := x / pow
366 r[j] = '0' + byte(tmp)
367 j++
368 x -= tmp * pow
369 }
370 }
371 }