github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/common/hexutil/hexutil_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:34</date>
    10  //</624450073580802048>
    11  
    12  
    13  package hexutil
    14  
    15  import (
    16  	"bytes"
    17  	"math/big"
    18  	"testing"
    19  )
    20  
    21  type marshalTest struct {
    22  	input interface{}
    23  	want  string
    24  }
    25  
    26  type unmarshalTest struct {
    27  	input        string
    28  	want         interface{}
    29  wantErr      error //如果设置,则任何平台上的解码都必须失败。
    30  wantErr32bit error //如果设置,则解码必须在32位平台上失败(用于uint测试)
    31  }
    32  
    33  var (
    34  	encodeBytesTests = []marshalTest{
    35  		{[]byte{}, "0x"},
    36  		{[]byte{0}, "0x00"},
    37  		{[]byte{0, 0, 1, 2}, "0x00000102"},
    38  	}
    39  
    40  	encodeBigTests = []marshalTest{
    41  		{referenceBig("0"), "0x0"},
    42  		{referenceBig("1"), "0x1"},
    43  		{referenceBig("ff"), "0xff"},
    44  		{referenceBig("112233445566778899aabbccddeeff"), "0x112233445566778899aabbccddeeff"},
    45  		{referenceBig("80a7f2c1bcc396c00"), "0x80a7f2c1bcc396c00"},
    46  		{referenceBig("-80a7f2c1bcc396c00"), "-0x80a7f2c1bcc396c00"},
    47  	}
    48  
    49  	encodeUint64Tests = []marshalTest{
    50  		{uint64(0), "0x0"},
    51  		{uint64(1), "0x1"},
    52  		{uint64(0xff), "0xff"},
    53  		{uint64(0x1122334455667788), "0x1122334455667788"},
    54  	}
    55  
    56  	encodeUintTests = []marshalTest{
    57  		{uint(0), "0x0"},
    58  		{uint(1), "0x1"},
    59  		{uint(0xff), "0xff"},
    60  		{uint(0x11223344), "0x11223344"},
    61  	}
    62  
    63  	decodeBytesTests = []unmarshalTest{
    64  //无效
    65  		{input: ``, wantErr: ErrEmptyString},
    66  		{input: `0`, wantErr: ErrMissingPrefix},
    67  		{input: `0x0`, wantErr: ErrOddLength},
    68  		{input: `0x023`, wantErr: ErrOddLength},
    69  		{input: `0xxx`, wantErr: ErrSyntax},
    70  		{input: `0x01zz01`, wantErr: ErrSyntax},
    71  //有效的
    72  		{input: `0x`, want: []byte{}},
    73  		{input: `0X`, want: []byte{}},
    74  		{input: `0x02`, want: []byte{0x02}},
    75  		{input: `0X02`, want: []byte{0x02}},
    76  		{input: `0xffffffffff`, want: []byte{0xff, 0xff, 0xff, 0xff, 0xff}},
    77  		{
    78  			input: `0xffffffffffffffffffffffffffffffffffff`,
    79  			want:  []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
    80  		},
    81  	}
    82  
    83  	decodeBigTests = []unmarshalTest{
    84  //无效
    85  		{input: `0`, wantErr: ErrMissingPrefix},
    86  		{input: `0x`, wantErr: ErrEmptyNumber},
    87  		{input: `0x01`, wantErr: ErrLeadingZero},
    88  		{input: `0xx`, wantErr: ErrSyntax},
    89  		{input: `0x1zz01`, wantErr: ErrSyntax},
    90  		{
    91  			input:   `0x10000000000000000000000000000000000000000000000000000000000000000`,
    92  			wantErr: ErrBig256Range,
    93  		},
    94  //有效的
    95  		{input: `0x0`, want: big.NewInt(0)},
    96  		{input: `0x2`, want: big.NewInt(0x2)},
    97  		{input: `0x2F2`, want: big.NewInt(0x2f2)},
    98  		{input: `0X2F2`, want: big.NewInt(0x2f2)},
    99  		{input: `0x1122aaff`, want: big.NewInt(0x1122aaff)},
   100  		{input: `0xbBb`, want: big.NewInt(0xbbb)},
   101  		{input: `0xfffffffff`, want: big.NewInt(0xfffffffff)},
   102  		{
   103  			input: `0x112233445566778899aabbccddeeff`,
   104  			want:  referenceBig("112233445566778899aabbccddeeff"),
   105  		},
   106  		{
   107  			input: `0xffffffffffffffffffffffffffffffffffff`,
   108  			want:  referenceBig("ffffffffffffffffffffffffffffffffffff"),
   109  		},
   110  		{
   111  			input: `0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff`,
   112  			want:  referenceBig("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
   113  		},
   114  	}
   115  
   116  	decodeUint64Tests = []unmarshalTest{
   117  //无效
   118  		{input: `0`, wantErr: ErrMissingPrefix},
   119  		{input: `0x`, wantErr: ErrEmptyNumber},
   120  		{input: `0x01`, wantErr: ErrLeadingZero},
   121  		{input: `0xfffffffffffffffff`, wantErr: ErrUint64Range},
   122  		{input: `0xx`, wantErr: ErrSyntax},
   123  		{input: `0x1zz01`, wantErr: ErrSyntax},
   124  //有效的
   125  		{input: `0x0`, want: uint64(0)},
   126  		{input: `0x2`, want: uint64(0x2)},
   127  		{input: `0x2F2`, want: uint64(0x2f2)},
   128  		{input: `0X2F2`, want: uint64(0x2f2)},
   129  		{input: `0x1122aaff`, want: uint64(0x1122aaff)},
   130  		{input: `0xbbb`, want: uint64(0xbbb)},
   131  		{input: `0xffffffffffffffff`, want: uint64(0xffffffffffffffff)},
   132  	}
   133  )
   134  
   135  func TestEncode(t *testing.T) {
   136  	for _, test := range encodeBytesTests {
   137  		enc := Encode(test.input.([]byte))
   138  		if enc != test.want {
   139  			t.Errorf("input %x: wrong encoding %s", test.input, enc)
   140  		}
   141  	}
   142  }
   143  
   144  func TestDecode(t *testing.T) {
   145  	for _, test := range decodeBytesTests {
   146  		dec, err := Decode(test.input)
   147  		if !checkError(t, test.input, err, test.wantErr) {
   148  			continue
   149  		}
   150  		if !bytes.Equal(test.want.([]byte), dec) {
   151  			t.Errorf("input %s: value mismatch: got %x, want %x", test.input, dec, test.want)
   152  			continue
   153  		}
   154  	}
   155  }
   156  
   157  func TestEncodeBig(t *testing.T) {
   158  	for _, test := range encodeBigTests {
   159  		enc := EncodeBig(test.input.(*big.Int))
   160  		if enc != test.want {
   161  			t.Errorf("input %x: wrong encoding %s", test.input, enc)
   162  		}
   163  	}
   164  }
   165  
   166  func TestDecodeBig(t *testing.T) {
   167  	for _, test := range decodeBigTests {
   168  		dec, err := DecodeBig(test.input)
   169  		if !checkError(t, test.input, err, test.wantErr) {
   170  			continue
   171  		}
   172  		if dec.Cmp(test.want.(*big.Int)) != 0 {
   173  			t.Errorf("input %s: value mismatch: got %x, want %x", test.input, dec, test.want)
   174  			continue
   175  		}
   176  	}
   177  }
   178  
   179  func TestEncodeUint64(t *testing.T) {
   180  	for _, test := range encodeUint64Tests {
   181  		enc := EncodeUint64(test.input.(uint64))
   182  		if enc != test.want {
   183  			t.Errorf("input %x: wrong encoding %s", test.input, enc)
   184  		}
   185  	}
   186  }
   187  
   188  func TestDecodeUint64(t *testing.T) {
   189  	for _, test := range decodeUint64Tests {
   190  		dec, err := DecodeUint64(test.input)
   191  		if !checkError(t, test.input, err, test.wantErr) {
   192  			continue
   193  		}
   194  		if dec != test.want.(uint64) {
   195  			t.Errorf("input %s: value mismatch: got %x, want %x", test.input, dec, test.want)
   196  			continue
   197  		}
   198  	}
   199  }
   200