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