-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdecimal64scan_test.go
148 lines (123 loc) · 2.99 KB
/
decimal64scan_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
package decimal
import (
"bytes"
"fmt"
"strconv"
"strings"
"testing"
)
func TestParse64(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip("skipping TestParse64 in short mode.")
}
parseEquals64 := parseEquals64(t)
for i := int64(-1000); i <= 1000; i++ {
for _, suffix := range []string{"", ".", ".0", "e0"} {
s := strconv.Itoa(int(i))
di := New64FromInt64(i)
parseEquals64(di, s+suffix)
}
}
}
func TestParse64Inf(t *testing.T) {
t.Parallel()
parseEquals64 := parseEquals64(t)
parseEquals64(Infinity64, "Inf")
parseEquals64(Infinity64, "inf")
parseEquals64(Infinity64, "∞")
parseEquals64(NegInfinity64, "-Inf")
parseEquals64(NegInfinity64, "-inf")
parseEquals64(NegInfinity64, "-∞")
parseEquals64(QNaN64, "nan")
parseEquals64(QNaN64, "NaN")
}
// TODO: Find out what the correct behavior is with bad inputs
// TODO: Does nan get returned if there are leading/trailing whitespaces?
func TestParse64BadInputs(t *testing.T) {
t.Parallel()
test := func(input string) {
t.Helper()
d, _ := Parse64(input)
equal(t, SNaN64.IsNaN(), d.IsNaN())
}
test("")
test(" ")
test("x")
test("++0")
test("--0")
test("+-0")
test("-+0")
test("0..")
test("0..2")
test("0e")
test("0ee")
test("0ee2")
test("0ex")
}
func TestParse64BigExp(t *testing.T) {
t.Parallel()
parseEquals64 := parseEquals64(t)
parseEquals64(Zero64, "0e-9999")
parseEquals64(NegZero64, "-0e-9999")
parseEquals64(Zero64, "1e-9999")
parseEquals64(NegZero64, "-1e-9999")
parseEquals64(Zero64, "0e9999")
parseEquals64(NegZero64, "-0e9999")
parseEquals64(Infinity64, "1e9999")
parseEquals64(NegInfinity64, "-1e9999")
}
func TestParse64LongMantissa(t *testing.T) {
t.Parallel()
parseEquals64 := parseEquals64(t)
parseEquals64(One64, "1000000000000000000000e-21")
parseEquals64(New64FromInt64(123), "1230000000000000000000e-19")
}
func TestDecimal64ScanFlakyScanState(t *testing.T) {
t.Parallel()
failAt := func(text string, failAt int) {
state := flakyScanState{
actual: &scanner{reader: strings.NewReader(text)},
failAt: failAt,
}
var d Decimal64
notnil(t, d.Scan(&state, 'e'))
}
failAt("x", 0)
for i := 0; i < 7; i++ {
failAt("-1.0e-3", i)
}
}
func BenchmarkIOParse64(b *testing.B) {
var d Decimal64
for n := 0; n < b.N; n++ {
buf := bytes.NewBufferString("123456789")
fmt.Fscanf(buf, "%g", &d) //nolint:errcheck
}
}
func BenchmarkIODecimal64Scan(b *testing.B) {
reader := strings.NewReader("")
for n := 0; n < b.N; n++ {
reader.Reset("123456789")
var d Decimal64
if err := d.Scan(&scanner{reader: reader}, 'g'); err != nil {
panic("Benchmarking Scan failed")
}
}
}
func parseEquals64(t *testing.T) func(expected Decimal64, input string) {
return func(expected Decimal64, input string) {
nopanic(t, func() {
n := MustParse64(input)
equalD64(t, expected, n)
})
n, err := Parse64(input)
isnil(t, err)
equalD64(t, expected, n)
n = SNaN64
count, err := fmt.Sscanf(input, "%g", &n)
isnil(t, err)
equal(t, 1, count)
equalD64(t, expected, n)
}
}