-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcli_amount_parse_test.go
More file actions
60 lines (54 loc) · 1.72 KB
/
cli_amount_parse_test.go
File metadata and controls
60 lines (54 loc) · 1.72 KB
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
package main
import (
"fmt"
"math"
"strings"
"testing"
)
func TestParseAmount_BoundariesOverflowAndPrecision(t *testing.T) {
const atomicPerBNT = uint64(100_000_000)
maxWhole := uint64(math.MaxUint64) / atomicPerBNT
remainder := uint64(math.MaxUint64) % atomicPerBNT
type tc struct {
in string
want uint64
wantErr string
}
tests := []tc{
{in: "0", want: 0},
{in: "1", want: 1 * atomicPerBNT},
{in: "1 BNT", want: 1 * atomicPerBNT},
{in: "1.1", want: 1*atomicPerBNT + 10_000_000},
{in: "1.00000000", want: 1 * atomicPerBNT},
// Truncation (not rounding) beyond 8 decimals:
{in: "1.000000009", want: 1 * atomicPerBNT},
{in: "0.000000009", want: 0},
// Max whole that doesn't overflow multiplication:
{in: fmt.Sprintf("%d", maxWhole), want: maxWhole * atomicPerBNT},
// Exact max uint64: maxWhole + remainder fractional.
{in: fmt.Sprintf("%d.%08d", maxWhole, remainder), want: math.MaxUint64},
// First overflowing whole (whole*atomicPerBNT):
{in: fmt.Sprintf("%d", maxWhole+1), wantErr: "amount too large"},
// Overflow on result+frac (frac > remainder):
{in: fmt.Sprintf("%d.%08d", maxWhole, remainder+1), wantErr: "amount too large"},
{in: "1.2.3", wantErr: "invalid amount format"},
}
for _, tt := range tests {
got, err := parseAmount(tt.in)
if tt.wantErr != "" {
if err == nil {
t.Fatalf("parseAmount(%q): expected error %q, got nil", tt.in, tt.wantErr)
}
if !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("parseAmount(%q): unexpected error: %v", tt.in, err)
}
continue
}
if err != nil {
t.Fatalf("parseAmount(%q): unexpected error: %v", tt.in, err)
}
if got != tt.want {
t.Fatalf("parseAmount(%q): got %d, want %d", tt.in, got, tt.want)
}
}
}