-
Notifications
You must be signed in to change notification settings - Fork 1
/
gwei_test.go
139 lines (130 loc) · 3.43 KB
/
gwei_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
package unit
import (
"math/big"
"reflect"
"testing"
)
func TestNewGWei(t *testing.T) {
type args struct {
value *big.Float
}
ether10, _ := new(big.Int).SetString("10000000000000000000", 10)
ether100, _ := new(big.Int).SetString("100000000000000000000", 10)
ether1000, _ := new(big.Int).SetString("1000000000000000000000", 10)
ether10000000, _ := new(big.Int).SetString("10000000000000000000000000", 10)
tests := []struct {
name string
args args
want *Unit
}{
{
name: "10000 wei",
args: args{value: big.NewFloat(0.00001)},
want: &Unit{Value: big.NewInt(10000)},
},
{
name: "100000 GWei",
args: args{value: big.NewFloat(100000)},
want: &Unit{Value: big.NewInt(100000000000000)},
},
{
name: "0.1 ether",
args: args{value: big.NewFloat(100000000)},
want: &Unit{Value: big.NewInt(100000000000000000)},
},
{
name: "1 ether",
args: args{value: big.NewFloat(1000000000)},
want: &Unit{Value: big.NewInt(1000000000000000000)},
},
{
name: "10 ether",
args: args{value: big.NewFloat(10000000000)},
want: &Unit{Value: ether10},
},
{
name: "100 ether",
args: args{value: big.NewFloat(100000000000)},
want: &Unit{Value: ether100},
},
{
name: "1000 ether",
args: args{value: big.NewFloat(1000000000000)},
want: &Unit{Value: ether1000},
},
{
name: "10000000 ether",
args: args{value: big.NewFloat(10000000000000000)},
want: &Unit{Value: ether10000000},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewGWei(tt.args.value); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewGWei() = %v, want %v", got, tt.want)
}
})
}
}
func TestUnit_GWei(t *testing.T) {
type fields struct {
Value *big.Int
}
ether10, _ := new(big.Int).SetString("10000000000000000000", 10)
ether100, _ := new(big.Int).SetString("100000000000000000000", 10)
ether1000, _ := new(big.Int).SetString("1000000000000000000000", 10)
ether10000000, _ := new(big.Int).SetString("10000000000000000000000000", 10)
decEther, _ := new(big.Int).SetString("91231929312891218293912932993", 10)
veryBigEther, _ := new(big.Int).SetString("99999999999999999899879999999999999999999999999999999999999999999999999999999000000000000000000", 10)
tests := []struct {
name string
fields fields
want *big.Float
}{
{
name: "1 ether",
fields: fields{Value: big.NewInt(1000000000000000000)},
want: big.NewFloat(1000000000),
},
{
name: "10 ether",
fields: fields{Value: ether10},
want: big.NewFloat(10000000000),
},
{
name: "100 ether",
fields: fields{Value: ether100},
want: big.NewFloat(100000000000),
},
{
name: "1000 ether",
fields: fields{Value: ether1000},
want: big.NewFloat(1000000000000),
},
{
name: "10000000 ether",
fields: fields{Value: ether10000000},
want: big.NewFloat(10000000000000000),
},
{
name: "91231929312.891218293912932993 ether",
fields: fields{Value: decEther},
want: big.NewFloat(91231929312891218293.912932993),
},
{
name: "a lot ether",
fields: fields{Value: veryBigEther},
want: big.NewFloat(99999999999999999899879999999999999999999999999999999999999999999999999999999000000000),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u := Unit{
Value: tt.fields.Value,
}
if got := u.GWei(); got.String() != tt.want.String() {
t.Errorf("GWei() = %s, want %s", got.String(), tt.want.String())
}
})
}
}