forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdec_coin_test.go
317 lines (286 loc) · 8.86 KB
/
dec_coin_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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package types
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestNewDecCoin(t *testing.T) {
require.NotPanics(t, func() {
NewInt64DecCoin(testDenom1, 5)
})
require.NotPanics(t, func() {
NewInt64DecCoin(testDenom1, 0)
})
require.Panics(t, func() {
NewInt64DecCoin(strings.ToUpper(testDenom1), 5)
})
require.Panics(t, func() {
NewInt64DecCoin(testDenom1, -5)
})
}
func TestNewDecCoinFromDec(t *testing.T) {
require.NotPanics(t, func() {
NewDecCoinFromDec(testDenom1, NewDec(5))
})
require.NotPanics(t, func() {
NewDecCoinFromDec(testDenom1, ZeroDec())
})
require.Panics(t, func() {
NewDecCoinFromDec(strings.ToUpper(testDenom1), NewDec(5))
})
require.Panics(t, func() {
NewDecCoinFromDec(testDenom1, NewDec(-5))
})
}
func TestNewDecCoinFromCoin(t *testing.T) {
require.NotPanics(t, func() {
NewDecCoinFromCoin(Coin{testDenom1, NewInt(5)})
})
require.NotPanics(t, func() {
NewDecCoinFromCoin(Coin{testDenom1, NewInt(0)})
})
require.Panics(t, func() {
NewDecCoinFromCoin(Coin{strings.ToUpper(testDenom1), NewInt(5)})
})
require.Panics(t, func() {
NewDecCoinFromCoin(Coin{testDenom1, NewInt(-5)})
})
}
func TestDecCoinIsPositive(t *testing.T) {
dc := NewInt64DecCoin(testDenom1, 5)
require.True(t, dc.IsPositive())
dc = NewInt64DecCoin(testDenom1, 0)
require.False(t, dc.IsPositive())
}
func TestAddDecCoin(t *testing.T) {
decCoinA1 := NewDecCoinFromDec(testDenom1, NewDecWithPrec(11, 1))
decCoinA2 := NewDecCoinFromDec(testDenom1, NewDecWithPrec(22, 1))
decCoinB1 := NewDecCoinFromDec(testDenom2, NewDecWithPrec(11, 1))
// regular add
res := decCoinA1.Add(decCoinA1)
require.Equal(t, decCoinA2, res, "sum of coins is incorrect")
// bad denom add
require.Panics(t, func() {
decCoinA1.Add(decCoinB1)
}, "expected panic on sum of different denoms")
}
func TestAddDecCoins(t *testing.T) {
one := NewDec(1)
zero := NewDec(0)
two := NewDec(2)
cases := []struct {
inputOne DecCoins
inputTwo DecCoins
expected DecCoins
}{
{DecCoins{{testDenom1, one}, {testDenom2, one}}, DecCoins{{testDenom1, one}, {testDenom2, one}}, DecCoins{{testDenom1, two}, {testDenom2, two}}},
{DecCoins{{testDenom1, zero}, {testDenom2, one}}, DecCoins{{testDenom1, zero}, {testDenom2, zero}}, DecCoins{{testDenom2, one}}},
{DecCoins{{testDenom1, zero}, {testDenom2, zero}}, DecCoins{{testDenom1, zero}, {testDenom2, zero}}, DecCoins(nil)},
}
for tcIndex, tc := range cases {
res := tc.inputOne.Add(tc.inputTwo)
require.Equal(t, tc.expected, res, "sum of coins is incorrect, tc #%d", tcIndex)
}
}
func TestSortDecCoins(t *testing.T) {
good := DecCoins{
NewInt64DecCoin("gas", 1),
NewInt64DecCoin("mineral", 1),
NewInt64DecCoin("tree", 1),
}
empty := DecCoins{
NewInt64DecCoin("gold", 0),
}
badSort1 := DecCoins{
NewInt64DecCoin("tree", 1),
NewInt64DecCoin("gas", 1),
NewInt64DecCoin("mineral", 1),
}
badSort2 := DecCoins{ // both are after the first one, but the second and third are in the wrong order
NewInt64DecCoin("gas", 1),
NewInt64DecCoin("tree", 1),
NewInt64DecCoin("mineral", 1),
}
badAmt := DecCoins{
NewInt64DecCoin("gas", 1),
NewInt64DecCoin("tree", 0),
NewInt64DecCoin("mineral", 1),
}
dup := DecCoins{
NewInt64DecCoin("gas", 1),
NewInt64DecCoin("gas", 1),
NewInt64DecCoin("mineral", 1),
}
cases := []struct {
coins DecCoins
before, after bool // valid before/after sort
}{
{good, true, true},
{empty, false, false},
{badSort1, false, true},
{badSort2, false, true},
{badAmt, false, false},
{dup, false, false},
}
for tcIndex, tc := range cases {
require.Equal(t, tc.before, tc.coins.IsValid(), "coin validity is incorrect before sorting, tc #%d", tcIndex)
tc.coins.Sort()
require.Equal(t, tc.after, tc.coins.IsValid(), "coin validity is incorrect after sorting, tc #%d", tcIndex)
}
}
func TestDecCoinsIsValid(t *testing.T) {
testCases := []struct {
input DecCoins
expected bool
}{
{DecCoins{}, true},
{DecCoins{DecCoin{testDenom1, NewDec(5)}}, true},
{DecCoins{DecCoin{testDenom1, NewDec(5)}, DecCoin{testDenom2, NewDec(100000)}}, true},
{DecCoins{DecCoin{testDenom1, NewDec(-5)}}, false},
{DecCoins{DecCoin{"AAA", NewDec(5)}}, false},
{DecCoins{DecCoin{testDenom1, NewDec(5)}, DecCoin{"B", NewDec(100000)}}, false},
{DecCoins{DecCoin{testDenom1, NewDec(5)}, DecCoin{testDenom2, NewDec(-100000)}}, false},
{DecCoins{DecCoin{testDenom1, NewDec(-5)}, DecCoin{testDenom2, NewDec(100000)}}, false},
{DecCoins{DecCoin{"AAA", NewDec(5)}, DecCoin{testDenom2, NewDec(100000)}}, false},
}
for i, tc := range testCases {
res := tc.input.IsValid()
require.Equal(t, tc.expected, res, "unexpected result for test case #%d, input: %v", i, tc.input)
}
}
func TestParseDecCoins(t *testing.T) {
testCases := []struct {
input string
expectedResult DecCoins
expectedErr bool
}{
{"", nil, false},
{"4stake", nil, true},
{"5.5atom,4stake", nil, true},
{"0.0stake", nil, true},
{"0.004STAKE", nil, true},
{
"0.004stake",
DecCoins{NewDecCoinFromDec("stake", NewDecWithPrec(4000000000000000, Precision))},
false,
},
{
"5.04atom,0.004stake",
DecCoins{
NewDecCoinFromDec("atom", NewDecWithPrec(5040000000000000000, Precision)),
NewDecCoinFromDec("stake", NewDecWithPrec(4000000000000000, Precision)),
},
false,
},
}
for i, tc := range testCases {
res, err := ParseDecCoins(tc.input)
if tc.expectedErr {
require.Error(t, err, "expected error for test case #%d, input: %v", i, tc.input)
} else {
require.NoError(t, err, "unexpected error for test case #%d, input: %v", i, tc.input)
require.Equal(t, tc.expectedResult, res, "unexpected result for test case #%d, input: %v", i, tc.input)
}
}
}
func TestDecCoinsString(t *testing.T) {
testCases := []struct {
input DecCoins
expected string
}{
{DecCoins{}, ""},
{
DecCoins{
NewDecCoinFromDec("atom", NewDecWithPrec(5040000000000000000, Precision)),
NewDecCoinFromDec("stake", NewDecWithPrec(4000000000000000, Precision)),
},
"5.040000000000000000atom,0.004000000000000000stake",
},
}
for i, tc := range testCases {
out := tc.input.String()
require.Equal(t, tc.expected, out, "unexpected result for test case #%d, input: %v", i, tc.input)
}
}
func TestDecCoinsIntersect(t *testing.T) {
testCases := []struct {
input1 string
input2 string
expectedResult string
}{
{"", "", ""},
{"1.0stake", "", ""},
{"1.0stake", "1.0stake", "1.0stake"},
{"", "1.0stake", ""},
{"1.0stake", "", ""},
{"2.0stake,1.0trope", "1.9stake", "1.9stake"},
{"2.0stake,1.0trope", "2.1stake", "2.0stake"},
{"2.0stake,1.0trope", "0.9trope", "0.9trope"},
{"2.0stake,1.0trope", "1.9stake,0.9trope", "1.9stake,0.9trope"},
{"2.0stake,1.0trope", "1.9stake,0.9trope,20.0other", "1.9stake,0.9trope"},
{"2.0stake,1.0trope", "1.0other", ""},
}
for i, tc := range testCases {
in1, err := ParseDecCoins(tc.input1)
require.NoError(t, err, "unexpected parse error in %v", i)
in2, err := ParseDecCoins(tc.input2)
require.NoError(t, err, "unexpected parse error in %v", i)
exr, err := ParseDecCoins(tc.expectedResult)
require.NoError(t, err, "unexpected parse error in %v", i)
require.True(t, in1.Intersect(in2).IsEqual(exr), "in1.cap(in2) != exr in %v", i)
}
}
func TestDecCoinsTruncateDecimal(t *testing.T) {
decCoinA := NewDecCoinFromDec("bar", MustNewDecFromStr("5.41"))
decCoinB := NewDecCoinFromDec("foo", MustNewDecFromStr("6.00"))
testCases := []struct {
input DecCoins
truncatedCoins Coins
changeCoins DecCoins
}{
{DecCoins{}, Coins(nil), DecCoins(nil)},
{
DecCoins{decCoinA, decCoinB},
Coins{NewInt64Coin(decCoinA.Denom, 5), NewInt64Coin(decCoinB.Denom, 6)},
DecCoins{NewDecCoinFromDec(decCoinA.Denom, MustNewDecFromStr("0.41"))},
},
{
DecCoins{decCoinB},
Coins{NewInt64Coin(decCoinB.Denom, 6)},
DecCoins(nil),
},
}
for i, tc := range testCases {
truncatedCoins, changeCoins := tc.input.TruncateDecimal()
require.Equal(
t, tc.truncatedCoins, truncatedCoins,
"unexpected truncated coins; tc #%d, input: %s", i, tc.input,
)
require.Equal(
t, tc.changeCoins, changeCoins,
"unexpected change coins; tc #%d, input: %s", i, tc.input,
)
}
}
func TestDecCoinsQuoDecTruncate(t *testing.T) {
x := MustNewDecFromStr("1.00")
y := MustNewDecFromStr("10000000000000000000.00")
testCases := []struct {
coins DecCoins
input Dec
result DecCoins
panics bool
}{
{DecCoins{}, ZeroDec(), DecCoins(nil), true},
{DecCoins{NewDecCoinFromDec("foo", x)}, y, DecCoins(nil), false},
{DecCoins{NewInt64DecCoin("foo", 5)}, NewDec(2), DecCoins{NewDecCoinFromDec("foo", MustNewDecFromStr("2.5"))}, false},
}
for i, tc := range testCases {
if tc.panics {
require.Panics(t, func() { tc.coins.QuoDecTruncate(tc.input) })
} else {
res := tc.coins.QuoDecTruncate(tc.input)
require.Equal(t, tc.result, res, "unexpected result; tc #%d, coins: %s, input: %s", i, tc.coins, tc.input)
}
}
}