github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/common/math/integer_test.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar 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-aigar 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-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package math
    19  
    20  import (
    21  	"testing"
    22  )
    23  
    24  type operation byte
    25  
    26  const (
    27  	sub operation = iota
    28  	add
    29  	mul
    30  )
    31  
    32  func TestOverflow(t *testing.T) {
    33  	for i, test := range []struct {
    34  		x        uint64
    35  		y        uint64
    36  		overflow bool
    37  		op       operation
    38  	}{
    39  		// add operations
    40  		{MaxUint64, 1, true, add},
    41  		{MaxUint64 - 1, 1, false, add},
    42  
    43  		// sub operations
    44  		{0, 1, true, sub},
    45  		{0, 0, false, sub},
    46  
    47  		// mul operations
    48  		{0, 0, false, mul},
    49  		{10, 10, false, mul},
    50  		{MaxUint64, 2, true, mul},
    51  		{MaxUint64, 1, false, mul},
    52  	} {
    53  		var overflows bool
    54  		switch test.op {
    55  		case sub:
    56  			_, overflows = SafeSub(test.x, test.y)
    57  		case add:
    58  			_, overflows = SafeAdd(test.x, test.y)
    59  		case mul:
    60  			_, overflows = SafeMul(test.x, test.y)
    61  		}
    62  
    63  		if test.overflow != overflows {
    64  			t.Errorf("%d failed. Expected test to be %v, got %v", i, test.overflow, overflows)
    65  		}
    66  	}
    67  }
    68  
    69  func TestHexOrDecimal64(t *testing.T) {
    70  	tests := []struct {
    71  		input string
    72  		num   uint64
    73  		ok    bool
    74  	}{
    75  		{"", 0, true},
    76  		{"0", 0, true},
    77  		{"0x0", 0, true},
    78  		{"12345678", 12345678, true},
    79  		{"0x12345678", 0x12345678, true},
    80  		{"0X12345678", 0x12345678, true},
    81  		// Tests for leading zero behaviour:
    82  		{"0123456789", 123456789, true}, // note: not octal
    83  		{"0x00", 0, true},
    84  		{"0x012345678abc", 0x12345678abc, true},
    85  		// Invalid syntax:
    86  		{"abcdef", 0, false},
    87  		{"0xgg", 0, false},
    88  		// Doesn't fit into 64 bits:
    89  		{"18446744073709551617", 0, false},
    90  	}
    91  	for _, test := range tests {
    92  		var num HexOrDecimal64
    93  		err := num.UnmarshalText([]byte(test.input))
    94  		if (err == nil) != test.ok {
    95  			t.Errorf("ParseUint64(%q) -> (err == nil) = %t, want %t", test.input, err == nil, test.ok)
    96  			continue
    97  		}
    98  		if err == nil && uint64(num) != test.num {
    99  			t.Errorf("ParseUint64(%q) -> %d, want %d", test.input, num, test.num)
   100  		}
   101  	}
   102  }
   103  
   104  func TestMustParseUint64(t *testing.T) {
   105  	if v := MustParseUint64("12345"); v != 12345 {
   106  		t.Errorf(`MustParseUint64("12345") = %d, want 12345`, v)
   107  	}
   108  }
   109  
   110  func TestMustParseUint64Panic(t *testing.T) {
   111  	defer func() {
   112  		if recover() == nil {
   113  			t.Error("MustParseBig should've panicked")
   114  		}
   115  	}()
   116  	MustParseUint64("ggg")
   117  }