github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/common/hexutil/hexutil_test.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //版权所有2016 Go Ethereum作者
    10  //此文件是Go以太坊库的一部分。
    11  //
    12  //Go-Ethereum库是免费软件:您可以重新分发它和/或修改
    13  //根据GNU发布的较低通用公共许可证的条款
    14  //自由软件基金会,或者许可证的第3版,或者
    15  //(由您选择)任何更高版本。
    16  //
    17  //Go以太坊图书馆的发行目的是希望它会有用,
    18  //但没有任何保证;甚至没有
    19  //适销性或特定用途的适用性。见
    20  //GNU较低的通用公共许可证,了解更多详细信息。
    21  //
    22  //你应该收到一份GNU较低级别的公共许可证副本
    23  //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。
    24  
    25  package hexutil
    26  
    27  import (
    28  	"bytes"
    29  	"math/big"
    30  	"testing"
    31  )
    32  
    33  type marshalTest struct {
    34  	input interface{}
    35  	want  string
    36  }
    37  
    38  type unmarshalTest struct {
    39  	input        string
    40  	want         interface{}
    41  wantErr      error //如果设置,则任何平台上的解码都必须失败。
    42  wantErr32bit error //
    43  }
    44  
    45  var (
    46  	encodeBytesTests = []marshalTest{
    47  		{[]byte{}, "0x"},
    48  		{[]byte{0}, "0x00"},
    49  		{[]byte{0, 0, 1, 2}, "0x00000102"},
    50  	}
    51  
    52  	encodeBigTests = []marshalTest{
    53  		{referenceBig("0"), "0x0"},
    54  		{referenceBig("1"), "0x1"},
    55  		{referenceBig("ff"), "0xff"},
    56  		{referenceBig("112233445566778899aabbccddeeff"), "0x112233445566778899aabbccddeeff"},
    57  		{referenceBig("80a7f2c1bcc396c00"), "0x80a7f2c1bcc396c00"},
    58  		{referenceBig("-80a7f2c1bcc396c00"), "-0x80a7f2c1bcc396c00"},
    59  	}
    60  
    61  	encodeUint64Tests = []marshalTest{
    62  		{uint64(0), "0x0"},
    63  		{uint64(1), "0x1"},
    64  		{uint64(0xff), "0xff"},
    65  		{uint64(0x1122334455667788), "0x1122334455667788"},
    66  	}
    67  
    68  	encodeUintTests = []marshalTest{
    69  		{uint(0), "0x0"},
    70  		{uint(1), "0x1"},
    71  		{uint(0xff), "0xff"},
    72  		{uint(0x11223344), "0x11223344"},
    73  	}
    74  
    75  	decodeBytesTests = []unmarshalTest{
    76  //无效
    77  		{input: ``, wantErr: ErrEmptyString},
    78  		{input: `0`, wantErr: ErrMissingPrefix},
    79  		{input: `0x0`, wantErr: ErrOddLength},
    80  		{input: `0x023`, wantErr: ErrOddLength},
    81  		{input: `0xxx`, wantErr: ErrSyntax},
    82  		{input: `0x01zz01`, wantErr: ErrSyntax},
    83  //有效的
    84  		{input: `0x`, want: []byte{}},
    85  		{input: `0X`, want: []byte{}},
    86  		{input: `0x02`, want: []byte{0x02}},
    87  		{input: `0X02`, want: []byte{0x02}},
    88  		{input: `0xffffffffff`, want: []byte{0xff, 0xff, 0xff, 0xff, 0xff}},
    89  		{
    90  			input: `0xffffffffffffffffffffffffffffffffffff`,
    91  			want:  []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
    92  		},
    93  	}
    94  
    95  	decodeBigTests = []unmarshalTest{
    96  //无效
    97  		{input: `0`, wantErr: ErrMissingPrefix},
    98  		{input: `0x`, wantErr: ErrEmptyNumber},
    99  		{input: `0x01`, wantErr: ErrLeadingZero},
   100  		{input: `0xx`, wantErr: ErrSyntax},
   101  		{input: `0x1zz01`, wantErr: ErrSyntax},
   102  		{
   103  			input:   `0x10000000000000000000000000000000000000000000000000000000000000000`,
   104  			wantErr: ErrBig256Range,
   105  		},
   106  //有效的
   107  		{input: `0x0`, want: big.NewInt(0)},
   108  		{input: `0x2`, want: big.NewInt(0x2)},
   109  		{input: `0x2F2`, want: big.NewInt(0x2f2)},
   110  		{input: `0X2F2`, want: big.NewInt(0x2f2)},
   111  		{input: `0x1122aaff`, want: big.NewInt(0x1122aaff)},
   112  		{input: `0xbBb`, want: big.NewInt(0xbbb)},
   113  		{input: `0xfffffffff`, want: big.NewInt(0xfffffffff)},
   114  		{
   115  			input: `0x112233445566778899aabbccddeeff`,
   116  			want:  referenceBig("112233445566778899aabbccddeeff"),
   117  		},
   118  		{
   119  			input: `0xffffffffffffffffffffffffffffffffffff`,
   120  			want:  referenceBig("ffffffffffffffffffffffffffffffffffff"),
   121  		},
   122  		{
   123  			input: `0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff`,
   124  			want:  referenceBig("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
   125  		},
   126  	}
   127  
   128  	decodeUint64Tests = []unmarshalTest{
   129  //无效
   130  		{input: `0`, wantErr: ErrMissingPrefix},
   131  		{input: `0x`, wantErr: ErrEmptyNumber},
   132  		{input: `0x01`, wantErr: ErrLeadingZero},
   133  		{input: `0xfffffffffffffffff`, wantErr: ErrUint64Range},
   134  		{input: `0xx`, wantErr: ErrSyntax},
   135  		{input: `0x1zz01`, wantErr: ErrSyntax},
   136  //有效的
   137  		{input: `0x0`, want: uint64(0)},
   138  		{input: `0x2`, want: uint64(0x2)},
   139  		{input: `0x2F2`, want: uint64(0x2f2)},
   140  		{input: `0X2F2`, want: uint64(0x2f2)},
   141  		{input: `0x1122aaff`, want: uint64(0x1122aaff)},
   142  		{input: `0xbbb`, want: uint64(0xbbb)},
   143  		{input: `0xffffffffffffffff`, want: uint64(0xffffffffffffffff)},
   144  	}
   145  )
   146  
   147  func TestEncode(t *testing.T) {
   148  	for _, test := range encodeBytesTests {
   149  		enc := Encode(test.input.([]byte))
   150  		if enc != test.want {
   151  			t.Errorf("input %x: wrong encoding %s", test.input, enc)
   152  		}
   153  	}
   154  }
   155  
   156  func TestDecode(t *testing.T) {
   157  	for _, test := range decodeBytesTests {
   158  		dec, err := Decode(test.input)
   159  		if !checkError(t, test.input, err, test.wantErr) {
   160  			continue
   161  		}
   162  		if !bytes.Equal(test.want.([]byte), dec) {
   163  			t.Errorf("input %s: value mismatch: got %x, want %x", test.input, dec, test.want)
   164  			continue
   165  		}
   166  	}
   167  }
   168  
   169  func TestEncodeBig(t *testing.T) {
   170  	for _, test := range encodeBigTests {
   171  		enc := EncodeBig(test.input.(*big.Int))
   172  		if enc != test.want {
   173  			t.Errorf("input %x: wrong encoding %s", test.input, enc)
   174  		}
   175  	}
   176  }
   177  
   178  func TestDecodeBig(t *testing.T) {
   179  	for _, test := range decodeBigTests {
   180  		dec, err := DecodeBig(test.input)
   181  		if !checkError(t, test.input, err, test.wantErr) {
   182  			continue
   183  		}
   184  		if dec.Cmp(test.want.(*big.Int)) != 0 {
   185  			t.Errorf("input %s: value mismatch: got %x, want %x", test.input, dec, test.want)
   186  			continue
   187  		}
   188  	}
   189  }
   190  
   191  func TestEncodeUint64(t *testing.T) {
   192  	for _, test := range encodeUint64Tests {
   193  		enc := EncodeUint64(test.input.(uint64))
   194  		if enc != test.want {
   195  			t.Errorf("input %x: wrong encoding %s", test.input, enc)
   196  		}
   197  	}
   198  }
   199  
   200  func TestDecodeUint64(t *testing.T) {
   201  	for _, test := range decodeUint64Tests {
   202  		dec, err := DecodeUint64(test.input)
   203  		if !checkError(t, test.input, err, test.wantErr) {
   204  			continue
   205  		}
   206  		if dec != test.want.(uint64) {
   207  			t.Errorf("input %s: value mismatch: got %x, want %x", test.input, dec, test.want)
   208  			continue
   209  		}
   210  	}
   211  }