-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
printer_test.go
124 lines (111 loc) · 2.29 KB
/
printer_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
package fpdecimal_test
import (
"fmt"
"math"
"math/rand"
"strconv"
"testing"
"github.com/nikolaydubina/fpdecimal"
)
func FuzzFixedPointDecimalToString_Fractions(f *testing.F) {
tests := []float64{
0,
0.100,
0.101,
0.010,
0.001,
0.0001,
0.123,
0.103,
0.100001,
12.001,
12.010,
12.345,
1,
2,
10,
12345678,
}
for _, tc := range tests {
f.Add(tc)
f.Add(-tc)
}
f.Fuzz(func(t *testing.T, r float64) {
if r > math.MaxInt64/1000 || r < math.MinInt64/1000 {
t.Skip()
}
// gaps start around these floats
if r > 100_000_000 || r < -100_000_000 {
t.Skip()
}
s := fmt.Sprintf("%.3f", r)
rs, _ := strconv.ParseFloat(s, 64)
v, err := fpdecimal.ParseFixedPointDecimal([]byte(s), 3)
if err != nil {
t.Error(err.Error())
}
if s == "-0.000" || s == "0.000" || rs == 0 || rs == -0 || (rs > -0.001 && rs < 0.001) {
if q := fpdecimal.FixedPointDecimalToString(v, 3); q != "0" {
t.Error(r, s, q)
}
return
}
if s, fs := strconv.FormatFloat(rs, 'f', -1, 64), fpdecimal.FixedPointDecimalToString(v, 3); s != fs {
t.Error(s, fs, r, v)
}
})
}
func FuzzFixedPointDecimalToString_NoFractions(f *testing.F) {
tests := []int64{
0,
1,
2,
10,
12345678,
}
for _, tc := range tests {
f.Add(tc)
f.Add(-tc)
}
f.Fuzz(func(t *testing.T, r int64) {
if a, b := fpdecimal.FixedPointDecimalToString(r, 0), strconv.FormatInt(r, 10); a != b {
t.Error(a, b)
}
})
}
func BenchmarkFixedPointDecimalToString(b *testing.B) {
var s string
for _, tc := range testsFloats {
tests := make([]int64, 0, len(tc.vals))
for range tc.vals {
tests = append(tests, int64(rand.Int()))
}
b.ResetTimer()
b.Run(tc.name, func(b *testing.B) {
for n := 0; n < b.N; n++ {
s = fpdecimal.FixedPointDecimalToString(tests[n%len(tests)], 3)
if s == "" {
b.Error("empty str")
}
}
})
}
}
func BenchmarkAppendFixedPointDecimal(b *testing.B) {
d := make([]byte, 0, 21)
for _, tc := range testsFloats {
tests := make([]int64, 0, len(tc.vals))
for range tc.vals {
tests = append(tests, int64(rand.Int()))
}
b.ResetTimer()
b.Run(tc.name, func(b *testing.B) {
for n := 0; n < b.N; n++ {
d = fpdecimal.AppendFixedPointDecimal(d, tests[n%len(tests)], 3)
if len(d) == 0 {
b.Error("empty str")
}
}
})
}
}