github.com/avence12/go-ethereum@v1.5.10-0.20170320123548-1dfd65f6d047/common/math/integer_test.go (about)

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