This repository has been archived by the owner on Aug 7, 2023. It is now read-only.
forked from btcsuite/btcutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amount_test.go
352 lines (333 loc) · 6.86 KB
/
amount_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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Copyright (c) 2013, 2014 The btcsuite developers
// Copyright (c) 2015 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package dcrutil_test
import (
"math"
"reflect"
"sort"
"testing"
. "github.com/decred/dcrutil"
)
func TestAmountCreation(t *testing.T) {
tests := []struct {
name string
amount float64
valid bool
expected Amount
}{
// Positive tests.
{
name: "zero",
amount: 0,
valid: true,
expected: 0,
},
{
name: "max producable",
amount: 21e6,
valid: true,
expected: MaxAmount,
},
{
name: "min producable",
amount: -21e6,
valid: true,
expected: -MaxAmount,
},
{
name: "exceeds max producable",
amount: 21e6 + 1e-8,
valid: true,
expected: MaxAmount + 1,
},
{
name: "exceeds min producable",
amount: -21e6 - 1e-8,
valid: true,
expected: -MaxAmount - 1,
},
{
name: "one hundred",
amount: 100,
valid: true,
expected: 100 * AtomsPerCoin,
},
{
name: "fraction",
amount: 0.01234567,
valid: true,
expected: 1234567,
},
{
name: "rounding up",
amount: 54.999999999999943157,
valid: true,
expected: 55 * AtomsPerCoin,
},
{
name: "rounding down",
amount: 55.000000000000056843,
valid: true,
expected: 55 * AtomsPerCoin,
},
// Negative tests.
{
name: "not-a-number",
amount: math.NaN(),
valid: false,
},
{
name: "-infinity",
amount: math.Inf(-1),
valid: false,
},
{
name: "+infinity",
amount: math.Inf(1),
valid: false,
},
}
for _, test := range tests {
a, err := NewAmount(test.amount)
switch {
case test.valid && err != nil:
t.Errorf("%v: Positive test Amount creation failed with: %v", test.name, err)
continue
case !test.valid && err == nil:
t.Errorf("%v: Negative test Amount creation succeeded (value %v) when should fail", test.name, a)
continue
}
if a != test.expected {
t.Errorf("%v: Created amount %v does not match expected %v", test.name, a, test.expected)
continue
}
}
}
func TestAmountUnitConversions(t *testing.T) {
tests := []struct {
name string
amount Amount
unit AmountUnit
converted float64
s string
}{
{
name: "MDCR",
amount: MaxAmount,
unit: AmountMegaCoin,
converted: 21,
s: "21 MDCR",
},
{
name: "kDCR",
amount: 44433322211100,
unit: AmountKiloCoin,
converted: 444.33322211100,
s: "444.333222111 kDCR",
},
{
name: "Coin",
amount: 44433322211100,
unit: AmountCoin,
converted: 444333.22211100,
s: "444333.222111 DCR",
},
{
name: "mDCR",
amount: 44433322211100,
unit: AmountMilliCoin,
converted: 444333222.11100,
s: "444333222.111 mDCR",
},
{
name: "μDCR",
amount: 44433322211100,
unit: AmountMicroCoin,
converted: 444333222111.00,
s: "444333222111 μDCR",
},
{
name: "atom",
amount: 44433322211100,
unit: AmountAtom,
converted: 44433322211100,
s: "44433322211100 Atom",
},
{
name: "non-standard unit",
amount: 44433322211100,
unit: AmountUnit(-1),
converted: 4443332.2211100,
s: "4443332.22111 1e-1 DCR",
},
}
for _, test := range tests {
f := test.amount.ToUnit(test.unit)
if f != test.converted {
t.Errorf("%v: converted value %v does not match expected %v", test.name, f, test.converted)
continue
}
s := test.amount.Format(test.unit)
if s != test.s {
t.Errorf("%v: format '%v' does not match expected '%v'", test.name, s, test.s)
continue
}
// Verify that Amount.ToCoin works as advertised.
f1 := test.amount.ToUnit(AmountCoin)
f2 := test.amount.ToCoin()
if f1 != f2 {
t.Errorf("%v: ToCoin does not match ToUnit(AmountCoin): %v != %v", test.name, f1, f2)
}
// Verify that Amount.String works as advertised.
s1 := test.amount.Format(AmountCoin)
s2 := test.amount.String()
if s1 != s2 {
t.Errorf("%v: String does not match Format(AmountCoin): %v != %v", test.name, s1, s2)
}
}
}
func TestAmountMulF64(t *testing.T) {
tests := []struct {
name string
amt Amount
mul float64
res Amount
}{
{
name: "Multiply 0.1 DCR by 2",
amt: 100e5, // 0.1 DCR
mul: 2,
res: 200e5, // 0.2 DCR
},
{
name: "Multiply 0.2 DCR by 0.02",
amt: 200e5, // 0.2 DCR
mul: 1.02,
res: 204e5, // 0.204 DCR
},
{
name: "Multiply 0.1 DCR by -2",
amt: 100e5, // 0.1 DCR
mul: -2,
res: -200e5, // -0.2 DCR
},
{
name: "Multiply 0.2 DCR by -0.02",
amt: 200e5, // 0.2 DCR
mul: -1.02,
res: -204e5, // -0.204 DCR
},
{
name: "Multiply -0.1 DCR by 2",
amt: -100e5, // -0.1 DCR
mul: 2,
res: -200e5, // -0.2 DCR
},
{
name: "Multiply -0.2 DCR by 0.02",
amt: -200e5, // -0.2 DCR
mul: 1.02,
res: -204e5, // -0.204 DCR
},
{
name: "Multiply -0.1 DCR by -2",
amt: -100e5, // -0.1 DCR
mul: -2,
res: 200e5, // 0.2 DCR
},
{
name: "Multiply -0.2 DCR by -0.02",
amt: -200e5, // -0.2 DCR
mul: -1.02,
res: 204e5, // 0.204 DCR
},
{
name: "Round down",
amt: 49, // 49 Atoms
mul: 0.01,
res: 0,
},
{
name: "Round up",
amt: 50, // 50 Atoms
mul: 0.01,
res: 1, // 1 Atom
},
{
name: "Multiply by 0.",
amt: 1e8, // 1 DCR
mul: 0,
res: 0, // 0 DCR
},
{
name: "Multiply 1 by 0.5.",
amt: 1, // 1 Atom
mul: 0.5,
res: 1, // 1 Atom
},
{
name: "Multiply 100 by 66%.",
amt: 100, // 100 Atoms
mul: 0.66,
res: 66, // 66 Atoms
},
{
name: "Multiply 100 by 66.6%.",
amt: 100, // 100 Atoms
mul: 0.666,
res: 67, // 67 Atoms
},
{
name: "Multiply 100 by 2/3.",
amt: 100, // 100 Atoms
mul: 2.0 / 3,
res: 67, // 67 Atoms
},
}
for _, test := range tests {
a := test.amt.MulF64(test.mul)
if a != test.res {
t.Errorf("%v: expected %v got %v", test.name, test.res, a)
}
}
}
func TestAmountSorter(t *testing.T) {
tests := []struct {
name string
as []Amount
want []Amount
}{
{
name: "Sort zero length slice of Amounts",
as: []Amount{},
want: []Amount{},
},
{
name: "Sort 1-element slice of Amounts",
as: []Amount{7},
want: []Amount{7},
},
{
name: "Sort 2-element slice of Amounts",
as: []Amount{7, 5},
want: []Amount{5, 7},
},
{
name: "Sort 6-element slice of Amounts",
as: []Amount{0, 9e8, 4e6, 4e6, 3, 9e12},
want: []Amount{0, 3, 4e6, 4e6, 9e8, 9e12},
},
}
for i, test := range tests {
result := make([]Amount, len(test.as))
copy(result, test.as)
sort.Sort(AmountSorter(result))
if !reflect.DeepEqual(result, test.want) {
t.Errorf("AmountSorter #%d got %v want %v", i, result,
test.want)
continue
}
}
}