-
Notifications
You must be signed in to change notification settings - Fork 0
/
decimal_test.go
115 lines (101 loc) · 2.76 KB
/
decimal_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
package decimal
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestZero(t *testing.T) {
Zero().Add(NewFromInt(1))
require.NotEqual(t, "1", Zero().String())
}
func TestNewFromDecimal(t *testing.T) {
d0 := NewFromInt(0)
d1 := NewFromInt(2)
d2 := NewFromDecimal(d1)
require.True(t, d1.Equals(d2))
d1.native().SetUint64(0)
require.True(t, d1.Equals(d0))
require.False(t, d1.Equals(d2))
}
func TestNewFromString(t *testing.T) {
tests := []struct {
input string
output string
err string
}{
{
"1", "1", "",
},
{
"1.0", "1.0", "",
},
{
"-1", "-1", "",
},
{
"-1.0", "-1.0", "",
},
{
"1.", "1", "",
},
{
"-1.", "-1", "",
},
{
"", "", "Invalid decimal",
},
{
"1.2.3", "", "Invalid decimal",
},
{
"ABC", "", "Invalid decimal",
},
}
for _, test := range tests {
d, err := NewFromString(test.input)
if test.err != "" {
require.EqualError(t, err, test.err)
continue
} else if err != nil {
require.NoError(t, err)
}
require.Equal(t, test.output, d.String())
}
}
type testStruct struct {
str string
}
func (a testStruct) String() string {
return a.str
}
func TestNewFromInterface(t *testing.T) {
require.Equal(t, float32(1.0), MustNewFromInterface(float32(1.0)).MustFloat32())
require.Equal(t, float64(1.0), MustNewFromInterface(float64(1.0)).MustFloat64())
require.Equal(t, int(1), MustNewFromInterface(int(1)).MustInt())
require.Equal(t, int8(1), MustNewFromInterface(int8(1)).MustInt8())
require.Equal(t, int16(1), MustNewFromInterface(int16(1)).MustInt16())
require.Equal(t, int32(1), MustNewFromInterface(int32(1)).MustInt32())
require.Equal(t, int64(1), MustNewFromInterface(int64(1)).MustInt64())
require.Equal(t, uint(1), MustNewFromInterface(uint(1)).MustUint())
require.Equal(t, uint8(1), MustNewFromInterface(uint8(1)).MustUint8())
require.Equal(t, uint16(1), MustNewFromInterface(uint16(1)).MustUint16())
require.Equal(t, uint32(1), MustNewFromInterface(uint32(1)).MustUint32())
require.Equal(t, uint64(1), MustNewFromInterface(uint64(1)).MustUint64())
require.Equal(t, "1", MustNewFromInterface("1").String())
require.EqualValues(t, []byte("1"), MustNewFromInterface([]byte("1")).Bytes())
require.True(t, NewFromInt(1).Equals(MustNewFromInterface(NewFromInt(1))))
require.Equal(t, "123", MustNewFromInterface(&testStruct{"123"}).String())
_, err := NewFromInterface(&testStruct{"ABC"})
require.EqualError(t, err, "Unable to create decimal from value type *decimal.testStruct: Invalid decimal")
}
func TestNilDecimal(t *testing.T) {
var d Decimal
require.Equal(t, "0", d.String())
}
func TestIsNan(t *testing.T) {
inf, err := NewFromString("Inf")
if err != nil {
t.Fail()
}
require.True(t, inf.Mul(NewFromInt(0)).IsNaN())
require.False(t, NewFromInt(1).IsNaN())
}