conversion_test.gno
3.99 Kb ยท 221 lines
1package int256
2
3import (
4 "testing"
5
6 "gno.land/p/demo/uint256"
7)
8
9func TestSetInt64(t *testing.T) {
10 tests := []struct {
11 v int64
12 expect int
13 }{
14 {0, 0},
15 {1, 1},
16 {-1, -1},
17 {9223372036854775807, 1}, // overflow (max int64)
18 {-9223372036854775808, -1}, // underflow (min int64)
19 }
20
21 for _, tt := range tests {
22 z := New().SetInt64(tt.v)
23 if z.Sign() != tt.expect {
24 t.Errorf("SetInt64(%d) = %d, want %d", tt.v, z.Sign(), tt.expect)
25 }
26 }
27}
28
29func TestUint64(t *testing.T) {
30 tests := []struct {
31 x string
32 want uint64
33 }{
34 {"0", 0},
35 {"1", 1},
36 {"9223372036854775807", 9223372036854775807},
37 {"9223372036854775808", 9223372036854775808},
38 {"18446744073709551615", 18446744073709551615},
39 }
40
41 for _, tt := range tests {
42 z := MustFromDecimal(tt.x)
43
44 got := z.Uint64()
45 if got != tt.want {
46 t.Errorf("Uint64(%s) = %d, want %d", tt.x, got, tt.want)
47 }
48 }
49}
50
51func TestUint64_Panic(t *testing.T) {
52 tests := []struct {
53 x string
54 }{
55 {"-1"},
56 {"18446744073709551616"},
57 {"18446744073709551617"},
58 }
59
60 for _, tt := range tests {
61 defer func() {
62 if r := recover(); r == nil {
63 t.Errorf("Uint64(%s) did not panic", tt.x)
64 }
65 }()
66
67 z := MustFromDecimal(tt.x)
68 z.Uint64()
69 }
70}
71
72func TestInt64(t *testing.T) {
73 tests := []struct {
74 x string
75 want int64
76 }{
77 {"0", 0},
78 {"1", 1},
79 {"9223372036854775807", 9223372036854775807},
80 {"-1", -1},
81 {"-9223372036854775808", -9223372036854775808},
82 }
83
84 for _, tt := range tests {
85 z := MustFromDecimal(tt.x)
86
87 got := z.Int64()
88 if got != tt.want {
89 t.Errorf("Uint64(%s) = %d, want %d", tt.x, got, tt.want)
90 }
91 }
92}
93
94func TestInt64_Panic(t *testing.T) {
95 tests := []struct {
96 x string
97 }{
98 {"18446744073709551616"},
99 {"18446744073709551617"},
100 }
101
102 for _, tt := range tests {
103 defer func() {
104 if r := recover(); r == nil {
105 t.Errorf("Int64(%s) did not panic", tt.x)
106 }
107 }()
108
109 z := MustFromDecimal(tt.x)
110 z.Int64()
111 }
112}
113
114func TestNeg(t *testing.T) {
115 tests := []struct {
116 x string
117 want string
118 }{
119 {"0", "0"},
120 {"1", "-1"},
121 {"-1", "1"},
122 {"9223372036854775807", "-9223372036854775807"},
123 {"-18446744073709551615", "18446744073709551615"},
124 }
125
126 for _, tt := range tests {
127 z := MustFromDecimal(tt.x)
128 z.Neg(z)
129
130 got := z.String()
131 if got != tt.want {
132 t.Errorf("Neg(%s) = %s, want %s", tt.x, got, tt.want)
133 }
134 }
135}
136
137func TestSet(t *testing.T) {
138 tests := []struct {
139 x string
140 want string
141 }{
142 {"0", "0"},
143 {"1", "1"},
144 {"-1", "-1"},
145 {"9223372036854775807", "9223372036854775807"},
146 {"-18446744073709551615", "-18446744073709551615"},
147 }
148
149 for _, tt := range tests {
150 z := MustFromDecimal(tt.x)
151 z.Set(z)
152
153 got := z.String()
154 if got != tt.want {
155 t.Errorf("Set(%s) = %s, want %s", tt.x, got, tt.want)
156 }
157 }
158}
159
160func TestSetUint256(t *testing.T) {
161 tests := []struct {
162 x string
163 want string
164 }{
165 {"0", "0"},
166 {"1", "1"},
167 {"9223372036854775807", "9223372036854775807"},
168 {"18446744073709551615", "18446744073709551615"},
169 }
170
171 for _, tt := range tests {
172 got := New()
173
174 z := uint256.MustFromDecimal(tt.x)
175 got.SetUint256(z)
176
177 if got.String() != tt.want {
178 t.Errorf("SetUint256(%s) = %s, want %s", tt.x, got.String(), tt.want)
179 }
180 }
181}
182
183func TestString(t *testing.T) {
184 tests := []struct {
185 input string
186 expected string
187 }{
188 {"0", "0"},
189 {"1", "1"},
190 {"-1", "-1"},
191 {"123456789", "123456789"},
192 {"-123456789", "-123456789"},
193 {"18446744073709551615", "18446744073709551615"}, // max uint64
194 {"-18446744073709551615", "-18446744073709551615"},
195 {TWO_POW_128_MINUS_1, TWO_POW_128_MINUS_1},
196 {MINUS_TWO_POW_128, MINUS_TWO_POW_128},
197 {MIN_INT256, MIN_INT256},
198 {MAX_INT256, MAX_INT256},
199 }
200
201 for _, tt := range tests {
202 x, err := FromDecimal(tt.input)
203 if err != nil {
204 t.Errorf("Failed to parse input (%s): %v", tt.input, err)
205 continue
206 }
207
208 output := x.String()
209
210 if output != tt.expected {
211 t.Errorf("String(%s) = %s, want %s", tt.input, output, tt.expected)
212 }
213 }
214}
215
216func TestNilToZero(t *testing.T) {
217 z := New().NilToZero()
218 if z.Sign() != 0 {
219 t.Errorf("NilToZero() = %d, want %d", z.Sign(), 0)
220 }
221}