@@ -21,6 +21,7 @@ import (
21
21
"encoding/hex"
22
22
"encoding/json"
23
23
"errors"
24
+ "github.com/holiman/uint256"
24
25
"math/big"
25
26
"testing"
26
27
)
@@ -176,6 +177,64 @@ func TestUnmarshalBig(t *testing.T) {
176
177
}
177
178
}
178
179
180
+ var unmarshalU256Tests = []unmarshalTest {
181
+ // invalid encoding
182
+ {input : "" , wantErr : errJSONEOF },
183
+ {input : "null" , wantErr : errNonString (u256T )},
184
+ {input : "10" , wantErr : errNonString (u256T )},
185
+ {input : `"0"` , wantErr : wrapTypeError (ErrMissingPrefix , u256T )},
186
+ {input : `"0x"` , wantErr : wrapTypeError (ErrEmptyNumber , u256T )},
187
+ {input : `"0x01"` , wantErr : wrapTypeError (ErrLeadingZero , u256T )},
188
+ {input : `"0xx"` , wantErr : wrapTypeError (ErrSyntax , u256T )},
189
+ {input : `"0x1zz01"` , wantErr : wrapTypeError (ErrSyntax , u256T )},
190
+ {
191
+ input : `"0x10000000000000000000000000000000000000000000000000000000000000000"` ,
192
+ wantErr : wrapTypeError (ErrBig256Range , u256T ),
193
+ },
194
+
195
+ // valid encoding
196
+ {input : `""` , want : big .NewInt (0 )},
197
+ {input : `"0x0"` , want : big .NewInt (0 )},
198
+ {input : `"0x2"` , want : big .NewInt (0x2 )},
199
+ {input : `"0x2F2"` , want : big .NewInt (0x2f2 )},
200
+ {input : `"0X2F2"` , want : big .NewInt (0x2f2 )},
201
+ {input : `"0x1122aaff"` , want : big .NewInt (0x1122aaff )},
202
+ {input : `"0xbBb"` , want : big .NewInt (0xbbb )},
203
+ {input : `"0xfffffffff"` , want : big .NewInt (0xfffffffff )},
204
+ {
205
+ input : `"0x112233445566778899aabbccddeeff"` ,
206
+ want : referenceBig ("112233445566778899aabbccddeeff" ),
207
+ },
208
+ {
209
+ input : `"0xffffffffffffffffffffffffffffffffffff"` ,
210
+ want : referenceBig ("ffffffffffffffffffffffffffffffffffff" ),
211
+ },
212
+ {
213
+ input : `"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"` ,
214
+ want : referenceBig ("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" ),
215
+ },
216
+ }
217
+
218
+ func TestUnmarshalU256 (t * testing.T ) {
219
+ for _ , test := range unmarshalU256Tests {
220
+ var v U256
221
+ err := json .Unmarshal ([]byte (test .input ), & v )
222
+ if ! checkError (t , test .input , err , test .wantErr ) {
223
+ continue
224
+ }
225
+ if test .want == nil {
226
+ continue
227
+ }
228
+ want := new (uint256.Int )
229
+ want .SetFromBig (test .want .(* big.Int ))
230
+ have := (* uint256 .Int )(& v )
231
+ if want .Cmp (have ) != 0 {
232
+ t .Errorf ("input %s: value mismatch: have %x, want %x" , test .input , have , want )
233
+ continue
234
+ }
235
+ }
236
+ }
237
+
179
238
func BenchmarkUnmarshalBig (b * testing.B ) {
180
239
input := []byte (`"0x123456789abcdef123456789abcdef"` )
181
240
for i := 0 ; i < b .N ; i ++ {
0 commit comments