-
Notifications
You must be signed in to change notification settings - Fork 0
/
currency_test.go
99 lines (78 loc) · 1.74 KB
/
currency_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
package currency_test
import (
"encoding/json"
"runtime"
"testing"
"currency"
)
func assert(cond bool, t *testing.T) {
if cond {
return
}
_, file, line, ok := runtime.Caller(1)
if !ok {
file = "???"
line = 0
}
t.Fatalf("%s: %d: Assertion failed\n", file, line)
}
type testcase struct {
in string
out string
oprec int
}
var tests = [...]testcase{
{"123456.987654321", "123456.987654321000", 12},
{"123456.987654321", "123456.98765", 5},
{"123456.987654321", "123456.987654", 6},
{"123.00456000000", "123.00456", 5},
{"123.00456000000", "123.004560", 6},
{"000.00456000000", "0.004", 3},
{"000.00456000000", "0.00456", 5},
{"123.00000", "123.0", 1},
{"123.00000", "123.00", 2},
{"000.0000", "0.0", 1},
}
func Test_fmt(t *testing.T) {
for _, tc := range tests {
c, err := currency.NewFromString(tc.in)
assert(err == nil, t)
s := c.StringFixed(tc.oprec)
t.Logf("in=|%s|, exp out=|%s| => |%s|\n", tc.in, tc.out, s)
assert(s == tc.out, t)
}
}
func Test_json(t *testing.T) {
ii, err := currency.NewFromString("123.0005430123")
assert(err == nil, t)
m, err := json.Marshal(ii)
assert(err == nil, t)
t.Logf("json=%s\n", string(m))
var xx currency.Currency
err = json.Unmarshal(m, &xx)
assert(err == nil, t)
assert(ii.Eq(&xx), t)
}
func Benchmark_NewFromString(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = currency.NewFromString("0023.0045600000")
}
}
func Benchmark_String0(b *testing.B) {
ii, err := currency.NewFromString("0023.0045600000")
if err != nil {
panic(err)
}
for i := 0; i < b.N; i++ {
_ = ii.String()
}
}
func Benchmark_String1(b *testing.B) {
ii, err := currency.NewFromString("0023.0000004567")
if err != nil {
panic(err)
}
for i := 0; i < b.N; i++ {
_ = ii.String()
}
}