-
Notifications
You must be signed in to change notification settings - Fork 0
/
dtoa_test.go
208 lines (201 loc) · 5.68 KB
/
dtoa_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package dtoa
import (
"math"
"strconv"
"testing"
)
func TestDtoa(t *testing.T) {
buf := []byte{}
table := []struct {
i int
f float64
s string
}{
{324, 0.0, "0.0"},
//{324, -0.0, "-0.0"},
{324, math.Float64frombits(1 << 63), "-0.0"},
{324, 1.0, "1.0"},
{324, -1.0, "-1.0"},
{324, 1.2345, "1.2345"},
{324, 1.2345678, "1.2345678"},
{324, 0.123456789012, "0.123456789012"},
{324, 1234567.8, "1234567.8"},
{324, -79.39773355813419, "-79.39773355813419"},
{324, 0.000001, "0.000001"},
{324, 0.0000001, "1e-7"},
{324, 1e30, "1e30"},
{324, 1.234567890123456e30, "1.234567890123456e30"},
{324, 5e-324, "5e-324"}, // Min subnormal positive double
{324, 2.225073858507201e-308, "2.225073858507201e-308"}, // Max subnormal positive double
{324, 2.2250738585072014e-308, "2.2250738585072014e-308"}, // Min normal positive double
{324, 1.7976931348623157e308, "1.7976931348623157e308"}, // Max double
{3, 0.0, "0.0"},
{1, 0.0, "0.0"},
//{3, -0.0, "-0.0"},
{3, math.Float64frombits(1 << 63), "-0.0"},
{3, 1.0, "1.0"},
{3, -1.0, "-1.0"},
{3, 1.2345, "1.234"},
{2, 1.2345, "1.23"},
{1, 1.2345, "1.2"},
{3, 1.2345678, "1.234"},
{3, 1.0001, "1.0"},
{2, 1.0001, "1.0"},
{1, 1.0001, "1.0"},
{3, 0.123456789012, "0.123"},
{2, 0.123456789012, "0.12"},
{1, 0.123456789012, "0.1"},
{4, 0.0001, "0.0001"},
{3, 0.0001, "0.0"},
{2, 0.0001, "0.0"},
{1, 0.0001, "0.0"},
{3, 1234567.8, "1234567.8"},
{3, 1e30, "1e30"},
{3, 5e-324, "0.0"}, // Min subnormal positive double
{3, 2.225073858507201e-308, "0.0"}, // Max subnormal positive double
{3, 2.2250738585072014e-308, "0.0"}, // Min normal positive double
{3, 1.7976931348623157e308, "1.7976931348623157e308"}, // Max double
{5, -0.14000000000000001, "-0.14"},
{4, -0.14000000000000001, "-0.14"},
{3, -0.14000000000000001, "-0.14"},
{3, -0.10000000000000001, "-0.1"},
{2, -0.10000000000000001, "-0.1"},
{1, -0.10000000000000001, "-0.1"},
}
for _, it := range table {
buf = buf[:0]
buf = Dtoa(buf, it.f, it.i)
if string(buf) != it.s {
t.Errorf("%f, %d, %s, Dtoa = %s\n", it.f, it.i, it.s, string(buf))
}
}
buf = make([]byte, 100)
for _, it := range table {
start := len(buf)
buf = Dtoa(buf, it.f, it.i)
if string(buf[start:]) != it.s {
t.Errorf("%f, %d, %s, Dtoa = %s\n", it.f, it.i, it.s, string(buf[start:]))
}
}
}
func TestDtoaSimple(t *testing.T) {
buf := []byte{}
table := []struct {
i int
f float64
s string
}{
{324, 0.0, "0"},
{324, math.Float64frombits(1 << 63), "-0"},
{324, 1.0, "1"},
{324, -1.0, "-1"},
{324, 1.2345, "1.2345"},
{324, 1.2345678, "1.2345678"},
{324, 0.123456789012, "0.123456789012"},
{324, 1234567.8, "1234567.8"},
{324, -79.39773355813419, "-79.39773355813419"},
{324, 0.000001, "0.000001"},
{324, 0.0000001, "1e-7"},
{324, 1e30, "1e30"},
{324, 1.234567890123456e30, "1.234567890123456e30"},
{324, 5e-324, "5e-324"}, // Min subnormal positive double
{324, 2.225073858507201e-308, "2.225073858507201e-308"}, // Max subnormal positive double
{324, 2.2250738585072014e-308, "2.2250738585072014e-308"}, // Min normal positive double
{324, 1.7976931348623157e308, "1.7976931348623157e308"}, // Max double
{3, 0.0, "0"},
{1, 0.0, "0"},
//{3, -0.0, "-0.0"},
{3, math.Float64frombits(1 << 63), "-0"},
{3, 1.0, "1"},
{3, -1.0, "-1"},
{3, 1.2345, "1.234"},
{2, 1.2345, "1.23"},
{1, 1.2345, "1.2"},
{3, 1.2345678, "1.234"},
{3, 1.0001, "1"},
{2, 1.0001, "1"},
{1, 1.0001, "1"},
{3, 0.123456789012, "0.123"},
{2, 0.123456789012, "0.12"},
{1, 0.123456789012, "0.1"},
{4, 0.0001, "0.0001"},
{3, 0.0001, "0"},
{2, 0.0001, "0"},
{1, 0.0001, "0"},
{3, 1234567.8, "1234567.8"},
{3, 1e30, "1e30"},
{3, 5e-324, "0"}, // Min subnormal positive double
{3, 2.225073858507201e-308, "0"}, // Max subnormal positive double
{3, 2.2250738585072014e-308, "0"}, // Min normal positive double
{3, 1.7976931348623157e308, "1.7976931348623157e308"}, // Max double
{5, -0.14000000000000001, "-0.14"},
{4, -0.14000000000000001, "-0.14"},
{3, -0.14000000000000001, "-0.14"},
{3, -0.10000000000000001, "-0.1"},
{2, -0.10000000000000001, "-0.1"},
{1, -0.10000000000000001, "-0.1"},
}
for _, it := range table {
buf = buf[:0]
buf = DtoaSimple(buf, it.f, it.i)
if string(buf) != it.s {
t.Errorf("%f, %d, %s, DtoaSimple = %s\n", it.f, it.i, it.s, string(buf))
}
}
buf = make([]byte, 100)
for _, it := range table {
start := len(buf)
buf = DtoaSimple(buf, it.f, it.i)
if string(buf[start:]) != it.s {
t.Errorf("%f, %d, %s, DtoaSimple = %s\n", it.f, it.i, it.s, string(buf[start:]))
}
}
}
func BenchmarkStrconvAppendFloat1(b *testing.B) {
buf := []byte{}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
strconv.AppendFloat(buf, 1.1, 'f', -1, 64)
}
}
func BenchmarkStrconvAppendFloat2(b *testing.B) {
buf := []byte{}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
strconv.AppendFloat(buf, 3.1415926535, 'f', -1, 64)
}
}
func BenchmarkStrconvAppendFloat3(b *testing.B) {
buf := []byte{}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
strconv.AppendFloat(buf, 2.225073858507201e-308, 'f', 1, 64)
}
}
func BenchmarkDtoaDtoa1(b *testing.B) {
buf := []byte{}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
Dtoa(buf, 1.1, -1)
}
}
func BenchmarkDtoaDtoa2(b *testing.B) {
buf := []byte{}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
Dtoa(buf, 3.1415926535, -1)
}
}
func BenchmarkDtoaDtoa3(b *testing.B) {
buf := []byte{}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
Dtoa(buf, 2.225073858507201e-308, 1)
}
}