fastfloat_test.tmpl raw
1 // +build amd64
2
3
4 // Code generated by Makefile, DO NOT EDIT.
5
6 /*
7 * Copyright 2021 ByteDance Inc.
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22 package {{PACKAGE}}
23
24 import (
25 `encoding/json`
26 `math`
27 `math/rand`
28 `strconv`
29 `testing`
30
31 `github.com/stretchr/testify/assert`
32 )
33
34 func TestFastFloat_Encode(t *testing.T) {
35 var buf [64]byte
36 assert.Equal(t, "0" , string(buf[:f64toa(&buf[0], 0)]))
37 assert.Equal(t, "-0" , string(buf[:f64toa(&buf[0], math.Float64frombits(0x8000000000000000))]))
38 assert.Equal(t, "12340000000" , string(buf[:f64toa(&buf[0], 1234e7)]))
39 assert.Equal(t, "12.34" , string(buf[:f64toa(&buf[0], 1234e-2)]))
40 assert.Equal(t, "0.001234" , string(buf[:f64toa(&buf[0], 1234e-6)]))
41 assert.Equal(t, "1e+30" , string(buf[:f64toa(&buf[0], 1e30)]))
42 assert.Equal(t, "1.234e+33" , string(buf[:f64toa(&buf[0], 1234e30)]))
43 assert.Equal(t, "1.234e+308" , string(buf[:f64toa(&buf[0], 1234e305)]))
44 assert.Equal(t, "1.234e-317" , string(buf[:f64toa(&buf[0], 1234e-320)]))
45 assert.Equal(t, "1.7976931348623157e+308" , string(buf[:f64toa(&buf[0], 1.7976931348623157e308)]))
46 assert.Equal(t, "-12340000000" , string(buf[:f64toa(&buf[0], -1234e7)]))
47 assert.Equal(t, "-12.34" , string(buf[:f64toa(&buf[0], -1234e-2)]))
48 assert.Equal(t, "-0.001234" , string(buf[:f64toa(&buf[0], -1234e-6)]))
49 assert.Equal(t, "-1e+30" , string(buf[:f64toa(&buf[0], -1e30)]))
50 assert.Equal(t, "-1.234e+33" , string(buf[:f64toa(&buf[0], -1234e30)]))
51 assert.Equal(t, "-1.234e+308" , string(buf[:f64toa(&buf[0], -1234e305)]))
52 assert.Equal(t, "-1.234e-317" , string(buf[:f64toa(&buf[0], -1234e-320)]))
53 assert.Equal(t, "-2.2250738585072014e-308" , string(buf[:f64toa(&buf[0], -2.2250738585072014e-308)]))
54 }
55
56 func TestFastFloat_Random(t *testing.T) {
57 var buf [64]byte
58 N := 10000
59 for i := 0; i < N; i++ {
60 b64 := uint64(rand.Uint32())<<32 | uint64(rand.Uint32())
61 f64 := math.Float64frombits(b64)
62
63 jout, jerr := json.Marshal(f64)
64 n := f64toa(&buf[0], f64)
65 if jerr == nil {
66 assert.Equal(t, jout, buf[:n])
67 } else {
68 assert.True(t, n == 0)
69 }
70
71 f32 := math.Float32frombits(rand.Uint32())
72 jout, jerr = json.Marshal(f32)
73 n = f32toa(&buf[0], f32)
74 if jerr == nil {
75 assert.Equal(t, jout, buf[:n])
76 } else {
77 assert.True(t, n == 0)
78 }
79 }
80 }
81
82 func BenchmarkParseFloat64(b *testing.B) {
83 var f64toaBenches = []struct {
84 name string
85 float float64
86 }{
87 {"Zero", 0},
88 {"Decimal", 33909},
89 {"Float", 339.7784},
90 {"Exp", -5.09e75},
91 {"NegExp", -5.11e-95},
92 {"LongExp", 1.234567890123456e-78},
93 {"Big", 123456789123456789123456789},
94
95 }
96 for _, c := range f64toaBenches {
97 f64bench := []struct {
98 name string
99 test func(*testing.B)
100 }{{
101 name: "StdLib",
102 test: func(b *testing.B) { var buf [64]byte; for i := 0; i < b.N; i++ { strconv.AppendFloat(buf[:0], c.float, 'g', -1, 64) }},
103 }, {
104 name: "FastFloat",
105 test: func(b *testing.B) { var buf [64]byte; for i := 0; i < b.N; i++ { f64toa(&buf[0], c.float) }},
106 }}
107 for _, bm := range f64bench {
108 name := bm.name + "_" + c.name
109 b.Run(name, bm.test)
110 }
111 }
112 }
113
114 func BenchmarkParseFloat32(b *testing.B) {
115 var f32toaBenches = []struct {
116 name string
117 float float32
118 }{
119 {"Zero", 0},
120 {"Integer", 33909},
121 {"ExactFraction", 3.375},
122 {"Point", 339.7784},
123 {"Exp", -5.09e25},
124 {"NegExp", -5.11e-25},
125 {"Shortest", 1.234567e-8},
126 }
127 for _, c := range f32toaBenches {
128 bench := []struct {
129 name string
130 test func(*testing.B)
131 }{{
132 name: "StdLib32",
133 test: func(b *testing.B) { var buf [64]byte; for i := 0; i < b.N; i++ { strconv.AppendFloat(buf[:0], float64(c.float), 'g', -1, 32) }},
134 }, {
135 name: "FastFloat32",
136 test: func(b *testing.B) { var buf [64]byte; for i := 0; i < b.N; i++ { f32toa(&buf[0], c.float) }},
137 }}
138 for _, bm := range bench {
139 name := bm.name + "_" + c.name
140 b.Run(name, bm.test)
141 }
142 }
143 }
144