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