github.com/theQRL/go-zond@v0.2.1/common/hexutil/hexutil_test.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package hexutil
    18  
    19  import (
    20  	"bytes"
    21  	"math/big"
    22  	"testing"
    23  )
    24  
    25  type marshalTest struct {
    26  	input interface{}
    27  	want  string
    28  }
    29  
    30  type unmarshalTest struct {
    31  	input        string
    32  	want         interface{}
    33  	wantErr      error // if set, decoding must fail on any platform
    34  	wantErr32bit error // if set, decoding must fail on 32bit platforms (used for Uint tests)
    35  }
    36  
    37  var (
    38  	encodeBytesTests = []marshalTest{
    39  		{[]byte{}, "0x"},
    40  		{[]byte{0}, "0x00"},
    41  		{[]byte{0, 0, 1, 2}, "0x00000102"},
    42  	}
    43  
    44  	encodeBigTests = []marshalTest{
    45  		{referenceBig("0"), "0x0"},
    46  		{referenceBig("1"), "0x1"},
    47  		{referenceBig("ff"), "0xff"},
    48  		{referenceBig("112233445566778899aabbccddeeff"), "0x112233445566778899aabbccddeeff"},
    49  		{referenceBig("80a7f2c1bcc396c00"), "0x80a7f2c1bcc396c00"},
    50  		{referenceBig("-80a7f2c1bcc396c00"), "-0x80a7f2c1bcc396c00"},
    51  	}
    52  
    53  	encodeZTests = []marshalTest{
    54  		{[]byte{}, "Z"},
    55  		{[]byte{0}, "Z00"},
    56  		{[]byte{0, 0, 1, 2}, "Z00000102"},
    57  	}
    58  
    59  	encodeUint64Tests = []marshalTest{
    60  		{uint64(0), "0x0"},
    61  		{uint64(1), "0x1"},
    62  		{uint64(0xff), "0xff"},
    63  		{uint64(0x1122334455667788), "0x1122334455667788"},
    64  	}
    65  
    66  	encodeUintTests = []marshalTest{
    67  		{uint(0), "0x0"},
    68  		{uint(1), "0x1"},
    69  		{uint(0xff), "0xff"},
    70  		{uint(0x11223344), "0x11223344"},
    71  	}
    72  
    73  	decodeBytesTests = []unmarshalTest{
    74  		// invalid
    75  		{input: ``, wantErr: ErrEmptyString},
    76  		{input: `0`, wantErr: ErrMissingPrefix},
    77  		{input: `0x0`, wantErr: ErrOddLength},
    78  		{input: `0x023`, wantErr: ErrOddLength},
    79  		{input: `0xxx`, wantErr: ErrSyntax},
    80  		{input: `0x01zz01`, wantErr: ErrSyntax},
    81  		// valid
    82  		{input: `0x`, want: []byte{}},
    83  		{input: `0X`, want: []byte{}},
    84  		{input: `0x02`, want: []byte{0x02}},
    85  		{input: `0X02`, want: []byte{0x02}},
    86  		{input: `0xffffffffff`, want: []byte{0xff, 0xff, 0xff, 0xff, 0xff}},
    87  		{
    88  			input: `0xffffffffffffffffffffffffffffffffffff`,
    89  			want:  []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
    90  		},
    91  	}
    92  
    93  	decodeBigTests = []unmarshalTest{
    94  		// invalid
    95  		{input: `0`, wantErr: ErrMissingPrefix},
    96  		{input: `0x`, wantErr: ErrEmptyNumber},
    97  		{input: `0x01`, wantErr: ErrLeadingZero},
    98  		{input: `0xx`, wantErr: ErrSyntax},
    99  		{input: `0x1zz01`, wantErr: ErrSyntax},
   100  		{
   101  			input:   `0x10000000000000000000000000000000000000000000000000000000000000000`,
   102  			wantErr: ErrBig256Range,
   103  		},
   104  		// valid
   105  		{input: `0x0`, want: big.NewInt(0)},
   106  		{input: `0x2`, want: big.NewInt(0x2)},
   107  		{input: `0x2F2`, want: big.NewInt(0x2f2)},
   108  		{input: `0X2F2`, want: big.NewInt(0x2f2)},
   109  		{input: `0x1122aaff`, want: big.NewInt(0x1122aaff)},
   110  		{input: `0xbBb`, want: big.NewInt(0xbbb)},
   111  		{input: `0xfffffffff`, want: big.NewInt(0xfffffffff)},
   112  		{
   113  			input: `0x112233445566778899aabbccddeeff`,
   114  			want:  referenceBig("112233445566778899aabbccddeeff"),
   115  		},
   116  		{
   117  			input: `0xffffffffffffffffffffffffffffffffffff`,
   118  			want:  referenceBig("ffffffffffffffffffffffffffffffffffff"),
   119  		},
   120  		{
   121  			input: `0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff`,
   122  			want:  referenceBig("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
   123  		},
   124  	}
   125  
   126  	decodeZTests = []unmarshalTest{ // invalid
   127  		{input: ``, wantErr: ErrEmptyString},
   128  		{input: `0`, wantErr: ErrMissingPrefixZ},
   129  		{input: `z`, wantErr: ErrMissingPrefixZ},
   130  		{input: `Z0`, wantErr: ErrOddLength},
   131  		{input: `Z023`, wantErr: ErrOddLength},
   132  		{input: `Zzz`, wantErr: ErrSyntax},
   133  		{input: `Z01zz01`, wantErr: ErrSyntax},
   134  		// valid
   135  		{input: `Z`, want: []byte{}},
   136  		{input: `Z02`, want: []byte{0x02}},
   137  		{input: `Zffffffffff`, want: []byte{0xff, 0xff, 0xff, 0xff, 0xff}},
   138  		{
   139  			input: `Zffffffffffffffffffffffffffffffffffff`,
   140  			want:  []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
   141  		},
   142  	}
   143  
   144  	decodeUint64Tests = []unmarshalTest{
   145  		// invalid
   146  		{input: `0`, wantErr: ErrMissingPrefix},
   147  		{input: `0x`, wantErr: ErrEmptyNumber},
   148  		{input: `0x01`, wantErr: ErrLeadingZero},
   149  		{input: `0xfffffffffffffffff`, wantErr: ErrUint64Range},
   150  		{input: `0xx`, wantErr: ErrSyntax},
   151  		{input: `0x1zz01`, wantErr: ErrSyntax},
   152  		// valid
   153  		{input: `0x0`, want: uint64(0)},
   154  		{input: `0x2`, want: uint64(0x2)},
   155  		{input: `0x2F2`, want: uint64(0x2f2)},
   156  		{input: `0X2F2`, want: uint64(0x2f2)},
   157  		{input: `0x1122aaff`, want: uint64(0x1122aaff)},
   158  		{input: `0xbbb`, want: uint64(0xbbb)},
   159  		{input: `0xffffffffffffffff`, want: uint64(0xffffffffffffffff)},
   160  	}
   161  )
   162  
   163  func TestEncode(t *testing.T) {
   164  	for _, test := range encodeBytesTests {
   165  		enc := Encode(test.input.([]byte))
   166  		if enc != test.want {
   167  			t.Errorf("input %x: wrong encoding %s", test.input, enc)
   168  		}
   169  	}
   170  }
   171  
   172  func TestDecode(t *testing.T) {
   173  	for _, test := range decodeBytesTests {
   174  		dec, err := Decode(test.input)
   175  		if !checkError(t, test.input, err, test.wantErr) {
   176  			continue
   177  		}
   178  		if !bytes.Equal(test.want.([]byte), dec) {
   179  			t.Errorf("input %s: value mismatch: got %x, want %x", test.input, dec, test.want)
   180  			continue
   181  		}
   182  	}
   183  }
   184  
   185  func TestEncodeBig(t *testing.T) {
   186  	for _, test := range encodeBigTests {
   187  		enc := EncodeBig(test.input.(*big.Int))
   188  		if enc != test.want {
   189  			t.Errorf("input %x: wrong encoding %s", test.input, enc)
   190  		}
   191  	}
   192  }
   193  
   194  func TestDecodeBig(t *testing.T) {
   195  	for _, test := range decodeBigTests {
   196  		dec, err := DecodeBig(test.input)
   197  		if !checkError(t, test.input, err, test.wantErr) {
   198  			continue
   199  		}
   200  		if dec.Cmp(test.want.(*big.Int)) != 0 {
   201  			t.Errorf("input %s: value mismatch: got %x, want %x", test.input, dec, test.want)
   202  			continue
   203  		}
   204  	}
   205  }
   206  
   207  func TestEncodeZ(t *testing.T) {
   208  	for _, test := range encodeZTests {
   209  		enc := EncodeZ(test.input.([]byte))
   210  		if enc != test.want {
   211  			t.Errorf("input %x: wrong encoding %s", test.input, enc)
   212  		}
   213  	}
   214  }
   215  
   216  func TestDecodeZ(t *testing.T) {
   217  	for _, test := range decodeZTests {
   218  		dec, err := DecodeZ(test.input)
   219  		if !checkError(t, test.input, err, test.wantErr) {
   220  			continue
   221  		}
   222  		if !bytes.Equal(test.want.([]byte), dec) {
   223  			t.Errorf("input %s: value mismatch: got %x, want %x", test.input, dec, test.want)
   224  			continue
   225  		}
   226  	}
   227  }
   228  
   229  func TestEncodeUint64(t *testing.T) {
   230  	for _, test := range encodeUint64Tests {
   231  		enc := EncodeUint64(test.input.(uint64))
   232  		if enc != test.want {
   233  			t.Errorf("input %x: wrong encoding %s", test.input, enc)
   234  		}
   235  	}
   236  }
   237  
   238  func TestDecodeUint64(t *testing.T) {
   239  	for _, test := range decodeUint64Tests {
   240  		dec, err := DecodeUint64(test.input)
   241  		if !checkError(t, test.input, err, test.wantErr) {
   242  			continue
   243  		}
   244  		if dec != test.want.(uint64) {
   245  			t.Errorf("input %s: value mismatch: got %x, want %x", test.input, dec, test.want)
   246  			continue
   247  		}
   248  	}
   249  }
   250  
   251  func BenchmarkEncodeBig(b *testing.B) {
   252  	for _, bench := range encodeBigTests {
   253  		b.Run(bench.want, func(b *testing.B) {
   254  			b.ReportAllocs()
   255  			bigint := bench.input.(*big.Int)
   256  			for i := 0; i < b.N; i++ {
   257  				EncodeBig(bigint)
   258  			}
   259  		})
   260  	}
   261  }