-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbigint_test.go
127 lines (109 loc) · 2.87 KB
/
bigint_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
package bigint_test
import (
"database/sql/driver"
"math/big"
"testing"
"github.com/stretchr/testify/assert"
"github.com/pgcontrib/bigint"
)
func TestAll(t *testing.T) {
a := big.NewInt(100)
b := big.NewInt(200)
t.Run("multiply", func(t *testing.T) {
x := bigint.NewBigint(a)
y := bigint.NewBigint(b)
// 100 * 200 = 20000
assert.Equal(t, bigint.NewBigint(big.NewInt(20000)), x.Mul(y))
})
t.Run("add", func(t *testing.T) {
x := bigint.NewBigint(a)
y := bigint.NewBigint(b)
// 100 + 200 = 300
assert.Equal(t, bigint.NewBigint(big.NewInt(300)), x.Add(y))
})
t.Run("sub", func(t *testing.T) {
x := bigint.NewBigint(a)
y := bigint.NewBigint(b)
// 100 -200 = -100
assert.Equal(t, bigint.NewBigint(big.NewInt(-100)), x.Sub(y))
})
t.Run("div", func(t *testing.T) {
x := bigint.NewBigint(a)
y := bigint.NewBigint(b)
// 200 / 100 = 2
assert.Equal(t, bigint.NewBigint(big.NewInt(2)), y.Div(x))
})
t.Run("negation", func(t *testing.T) {
x := bigint.NewBigint(a)
assert.Equal(t, bigint.NewBigint(big.NewInt(-100)), x.Neg())
})
t.Run("int64", func(t *testing.T) {
x := bigint.NewBigint(a)
assert.Equal(t, int64(-100), x.Neg().ToInt64())
})
t.Run("uint64", func(t *testing.T) {
x := bigint.NewBigint(a)
assert.Equal(t, uint64(100), x.ToUInt64())
})
t.Run("toString", func(t *testing.T) {
x := bigint.NewBigint(a)
assert.Equal(t, "100", x.String())
})
t.Run("fromString", func(t *testing.T) {
x, err := bigint.FromString("100")
assert.Nil(t, err)
assert.Equal(t, "100", x.String())
})
t.Run("fromInt64", func(t *testing.T) {
x := bigint.FromInt64(100000000)
assert.Equal(t, int64(100000000), x.ToInt64())
})
t.Run("value", func(t *testing.T) {
x := bigint.FromInt64(10000000000)
v, err := x.Value()
assert.Nil(t, err)
assert.True(t, true, driver.IsValue(v))
})
t.Run("from-bigint", func(t *testing.T) {
x := bigint.NewBigint(a)
y := bigint.NewBigint(b)
d := x.FromBigInt(b)
// 100 * 200 = 20000
assert.Equal(t, y, d)
})
t.Run("scan", func(t *testing.T) {
x := bigint.NewBigint(a)
s, err := x.Value()
assert.NoError(t, err)
assert.NotEmpty(t, s)
})
t.Run("Abs", func(t *testing.T) {
x := bigint.NewBigint(a)
assert.Equal(t, x.Neg().Abs(), x)
})
t.Run("compare: ", func(t *testing.T) {
x := bigint.NewBigint(a) // 100
y := bigint.NewBigint(b) // 200
cmp := x.Cmp(y)
t.Run("eq ?", func(t *testing.T) {
assert.Equal(t, cmp.Eq(), false)
})
t.Run("lt ?", func(t *testing.T) {
assert.Equal(t, cmp.Lt(), true)
})
t.Run("gt ?", func(t *testing.T) {
assert.Equal(t, cmp.Gt(), false)
})
t.Run("leq ?", func(t *testing.T) {
assert.Equal(t, cmp.Leq(), true)
})
t.Run("geq ?", func(t *testing.T) {
assert.Equal(t, cmp.Geq(), false)
})
})
t.Run("empty string ", func(t *testing.T) {
x, err := bigint.FromString("")
assert.Nil(t, err)
assert.Equal(t, x.ToInt64(), int64(0))
})
}