forked from woodsbury/decimal128
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan_test.go
More file actions
126 lines (106 loc) · 3.83 KB
/
scan_test.go
File metadata and controls
126 lines (106 loc) · 3.83 KB
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
package decimal128
import (
"errors"
"fmt"
"strconv"
"testing"
)
var textValues = map[string]Decimal{
"0": zero(false),
"+0": zero(false),
"-0": zero(true),
"0.00000000000000000000000000000000000005e-99999999": zero(false),
"0.00000000000000000000000000000000000005e-60150": zero(false),
"0.00000000000000000000000000000000000005e-6150": zero(false),
"1": compose(false, uint128{1, 0}, exponentBias),
"-1e1": compose(true, uint128{10, 0}, exponentBias),
"-1_23e1_0": compose(true, uint128{123, 0}, exponentBias+10),
"00123.45600e10": compose(false, uint128{123456, 0}, exponentBias+7),
"123.456e-10": compose(false, uint128{123456, 0}, exponentBias-13),
"0.00000000000000000000000000000000000005e-30": compose(false, uint128{5, 0}, exponentBias-68),
"0.00000000000000000000000000000000000005": compose(false, uint128{5, 0}, exponentBias-38),
"0.00000000000000000000000000000000000005e30": compose(false, uint128{5, 0}, exponentBias-8),
"500000000000000000000000000000000000000e-30": compose(false, uint128{5, 0}, exponentBias+8),
"500000000000000000000000000000000000000": compose(false, uint128{5, 0}, exponentBias+38),
"500000000000000000000000000000000000000e30": compose(false, uint128{5, 0}, exponentBias+68),
"inf": inf(false),
"+Inf": inf(false),
"-Infinity": inf(true),
"nan": NaN(),
"NaN": NaN(),
"0e99999999": zero(false),
"-0e99999999": zero(true),
}
func TestDecimalScan(t *testing.T) {
t.Parallel()
for val, num := range textValues {
if val == "-Infinity" {
// Scan only accepts "Inf" for infinity
continue
}
var res Decimal
_, err := fmt.Sscan(val, &res)
if !resultEqual(res, num) || err != nil {
t.Errorf("fmt.Sscan(%q) = (%v, %v), want (%v, <nil>)", val, res, err, num)
}
_, err = fmt.Sscanf(val, "%g", &res)
if !resultEqual(res, num) || err != nil {
t.Errorf("fmt.Sscanf(%q, \"%%g\") = (%v, %v), want (%v, <nil>)", val, res, err, num)
}
val += "\n"
_, err = fmt.Sscanf(val, "%g\n", &res)
if !resultEqual(res, num) || err != nil {
t.Errorf("fmt.Sscanf(%q, \"%%g\\n\") = (%v, %v), want (%v, <nil>)", val, res, err, num)
}
_, err = fmt.Sscanln(val, &res)
if !resultEqual(res, num) || err != nil {
t.Errorf("fmt.Sscanln(%q) = (%v, %v), want (%v, <nil>)", val, res, err, num)
}
}
var res1, res2 Decimal
n, err := fmt.Sscanf("1 2", "%g %g", &res1, &res2)
if n != 2 || !res1.Equal(FromUint64(1)) || !res2.Equal(FromUint64(2)) || err != nil {
t.Errorf("fmt.Sscanf(\"1 2\") = (%v, %v, %v), want (1, 2, <nil>)", res1, res2, err)
}
n, err = fmt.Sscanln("1 2\n", &res1, &res2)
if n != 2 || !res1.Equal(FromUint64(1)) || !res2.Equal(FromUint64(2)) || err != nil {
t.Errorf("fmt.Sscanln(\"1 2\\n\") = (%v, %v, %v), want (1, 2, <nil>)", res1, res2, err)
}
}
func TestDecimalUnmarshalText(t *testing.T) {
t.Parallel()
for val, num := range textValues {
var res Decimal
err := res.UnmarshalText([]byte(val))
if !resultEqual(res, num) || err != nil {
t.Errorf("Decimal.UnmarshalText(%s) = (%v, %v), want (%v, <nil>)", val, res, err, num)
}
}
}
func TestParse(t *testing.T) {
t.Parallel()
for val, num := range textValues {
res, err := Parse(val)
if !resultEqual(res, num) || err != nil {
t.Errorf("Parse(%s) = (%v, %v), want (%v, <nil>)", val, res, err, num)
}
}
res, err := Parse("1e999999999")
if !res.IsInf(1) || !errors.Is(err, strconv.ErrRange) {
t.Errorf("Parse(1e999999999) = (%v, %v), want (Inf, value out of range)", res, err)
}
res, err = Parse("-1e999999999")
if !res.IsInf(-1) || !errors.Is(err, strconv.ErrRange) {
t.Errorf("Parse(-1e999999999) = (%v, %v), want (-Inf, value out of range)", res, err)
}
}
func FuzzParse(f *testing.F) {
f.Add("123_456.789e10")
f.Add("+Inf")
f.Add("-Inf")
f.Add("NaN")
f.Fuzz(func(t *testing.T, s string) {
t.Parallel()
Parse(s)
})
}