github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/common/math/integer_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  //版权所有2017 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 math
    26  
    27  import (
    28  	"testing"
    29  )
    30  
    31  type operation byte
    32  
    33  const (
    34  	sub operation = iota
    35  	add
    36  	mul
    37  )
    38  
    39  func TestOverflow(t *testing.T) {
    40  	for i, test := range []struct {
    41  		x        uint64
    42  		y        uint64
    43  		overflow bool
    44  		op       operation
    45  	}{
    46  //添加操作
    47  		{MaxUint64, 1, true, add},
    48  		{MaxUint64 - 1, 1, false, add},
    49  
    50  //子操作
    51  		{0, 1, true, sub},
    52  		{0, 0, false, sub},
    53  
    54  //多重运算
    55  		{0, 0, false, mul},
    56  		{10, 10, false, mul},
    57  		{MaxUint64, 2, true, mul},
    58  		{MaxUint64, 1, false, mul},
    59  	} {
    60  		var overflows bool
    61  		switch test.op {
    62  		case sub:
    63  			_, overflows = SafeSub(test.x, test.y)
    64  		case add:
    65  			_, overflows = SafeAdd(test.x, test.y)
    66  		case mul:
    67  			_, overflows = SafeMul(test.x, test.y)
    68  		}
    69  
    70  		if test.overflow != overflows {
    71  			t.Errorf("%d failed. Expected test to be %v, got %v", i, test.overflow, overflows)
    72  		}
    73  	}
    74  }
    75  
    76  func TestHexOrDecimal64(t *testing.T) {
    77  	tests := []struct {
    78  		input string
    79  		num   uint64
    80  		ok    bool
    81  	}{
    82  		{"", 0, true},
    83  		{"0", 0, true},
    84  		{"0x0", 0, true},
    85  		{"12345678", 12345678, true},
    86  		{"0x12345678", 0x12345678, true},
    87  		{"0X12345678", 0x12345678, true},
    88  //超前零行为测试:
    89  {"0123456789", 123456789, true}, //注:不是八进制
    90  		{"0x00", 0, true},
    91  		{"0x012345678abc", 0x12345678abc, true},
    92  //无效语法:
    93  		{"abcdef", 0, false},
    94  		{"0xgg", 0, false},
    95  //不适合64位:
    96  		{"18446744073709551617", 0, false},
    97  	}
    98  	for _, test := range tests {
    99  		var num HexOrDecimal64
   100  		err := num.UnmarshalText([]byte(test.input))
   101  		if (err == nil) != test.ok {
   102  			t.Errorf("ParseUint64(%q) -> (err == nil) = %t, want %t", test.input, err == nil, test.ok)
   103  			continue
   104  		}
   105  		if err == nil && uint64(num) != test.num {
   106  			t.Errorf("ParseUint64(%q) -> %d, want %d", test.input, num, test.num)
   107  		}
   108  	}
   109  }
   110  
   111  func TestMustParseUint64(t *testing.T) {
   112  	if v := MustParseUint64("12345"); v != 12345 {
   113  		t.Errorf(`MustParseUint64("12345") = %d, want 12345`, v)
   114  	}
   115  }
   116  
   117  func TestMustParseUint64Panic(t *testing.T) {
   118  	defer func() {
   119  		if recover() == nil {
   120  			t.Error("MustParseBig should've panicked")
   121  		}
   122  	}()
   123  	MustParseUint64("ggg")
   124  }