github.com/klaytn/klaytn@v1.12.1/common/math/integer_test.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2017 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from common/math/integer_test.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package math
    22  
    23  import (
    24  	"testing"
    25  )
    26  
    27  type operation byte
    28  
    29  const (
    30  	sub operation = iota
    31  	add
    32  	mul
    33  )
    34  
    35  func TestOverflow(t *testing.T) {
    36  	for i, test := range []struct {
    37  		x        uint64
    38  		y        uint64
    39  		overflow bool
    40  		op       operation
    41  	}{
    42  		// add operations
    43  		{MaxUint64, 1, true, add},
    44  		{MaxUint64 - 1, 1, false, add},
    45  
    46  		// sub operations
    47  		{0, 1, true, sub},
    48  		{0, 0, false, sub},
    49  
    50  		// mul operations
    51  		{0, 0, false, mul},
    52  		{10, 10, false, mul},
    53  		{MaxUint64, 2, true, mul},
    54  		{MaxUint64, 1, false, mul},
    55  	} {
    56  		var overflows bool
    57  		switch test.op {
    58  		case sub:
    59  			_, overflows = SafeSub(test.x, test.y)
    60  		case add:
    61  			_, overflows = SafeAdd(test.x, test.y)
    62  		case mul:
    63  			_, overflows = SafeMul(test.x, test.y)
    64  		}
    65  
    66  		if test.overflow != overflows {
    67  			t.Errorf("%d failed. Expected test to be %v, got %v", i, test.overflow, overflows)
    68  		}
    69  	}
    70  }
    71  
    72  func TestHexOrDecimal64(t *testing.T) {
    73  	tests := []struct {
    74  		input string
    75  		num   uint64
    76  		ok    bool
    77  	}{
    78  		{"", 0, true},
    79  		{"0", 0, true},
    80  		{"0x0", 0, true},
    81  		{"12345678", 12345678, true},
    82  		{"0x12345678", 0x12345678, true},
    83  		{"0X12345678", 0x12345678, true},
    84  		// Tests for leading zero behaviour:
    85  		{"0123456789", 123456789, true}, // note: not octal
    86  		{"0x00", 0, true},
    87  		{"0x012345678abc", 0x12345678abc, true},
    88  		// Invalid syntax:
    89  		{"abcdef", 0, false},
    90  		{"0xgg", 0, false},
    91  		// Doesn't fit into 64 bits:
    92  		{"18446744073709551617", 0, false},
    93  	}
    94  	for _, test := range tests {
    95  		var num HexOrDecimal64
    96  		err := num.UnmarshalText([]byte(test.input))
    97  		if (err == nil) != test.ok {
    98  			t.Errorf("ParseUint64(%q) -> (err == nil) = %t, want %t", test.input, err == nil, test.ok)
    99  			continue
   100  		}
   101  		if err == nil && uint64(num) != test.num {
   102  			t.Errorf("ParseUint64(%q) -> %d, want %d", test.input, num, test.num)
   103  		}
   104  	}
   105  }
   106  
   107  func TestMustParseUint64(t *testing.T) {
   108  	if v := MustParseUint64("12345"); v != 12345 {
   109  		t.Errorf(`MustParseUint64("12345") = %d, want 12345`, v)
   110  	}
   111  }
   112  
   113  func TestMustParseUint64Panic(t *testing.T) {
   114  	defer func() {
   115  		if recover() == nil {
   116  			t.Error("MustParseBig should've panicked")
   117  		}
   118  	}()
   119  	MustParseUint64("ggg")
   120  }