github.com/igggame/nebulas-go@v2.1.0+incompatible/util/uint128_test.go (about)

     1  package util
     2  
     3  import (
     4  	"fmt"
     5  	"math/big"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  const (
    13  	maxUint8  = ^uint8(0)
    14  	maxUint64 = ^uint64(0)
    15  )
    16  
    17  func TestMax(t *testing.T) {
    18  	bigMaxUint64 := &big.Int{}
    19  	bigMaxUint64.SetString(strings.Repeat("f", 16), 16)
    20  	fmt.Println(strings.Repeat("f", 16), bigMaxUint64.String())
    21  
    22  	bigMaxUint128 := &big.Int{}
    23  	bigMaxUint128.SetString(strings.Repeat("f", 32), 16)
    24  	fmt.Println(strings.Repeat("f", 32), bigMaxUint128.String())
    25  
    26  	bigMaxUint256 := &big.Int{}
    27  	bigMaxUint256.SetString(strings.Repeat("f", 64), 16)
    28  	fmt.Println(strings.Repeat("f", 64), bigMaxUint256.String())
    29  
    30  	bigMaxUint512 := &big.Int{}
    31  	bigMaxUint512.SetString(strings.Repeat("f", 128), 16)
    32  	fmt.Println(strings.Repeat("f", 128), bigMaxUint512.String())
    33  }
    34  
    35  func TestUint128(t *testing.T) {
    36  	bigInt0 := big.NewInt(0)
    37  
    38  	bigInt1 := big.NewInt(1)
    39  	bigIntNeg1 := big.NewInt(-1)
    40  
    41  	bigMaxUint8 := &big.Int{}
    42  	bigMaxUint8.SetUint64(uint64(maxUint8))
    43  
    44  	bigMaxUint64 := &big.Int{}
    45  	bigMaxUint64.SetUint64(maxUint64)
    46  
    47  	bigMaxUint64Add1 := &big.Int{}
    48  	bigMaxUint64Add1.Add(bigMaxUint64, big.NewInt(1))
    49  
    50  	bigUint128 := &big.Int{}
    51  	bigUint128.Mul(bigMaxUint64, big.NewInt(67280421310721))
    52  
    53  	bigMaxUint128 := &big.Int{}
    54  	bigMaxUint128.SetString(strings.Repeat("f", 32), 16)
    55  
    56  	bigMaxUint128Add1 := &big.Int{}
    57  	bigMaxUint128Add1.Add(bigMaxUint128, big.NewInt(1))
    58  
    59  	tests := []struct {
    60  		input       *big.Int // input
    61  		expected    [16]byte // expected Big-Endian result
    62  		expectedErr error
    63  	}{
    64  		{bigInt0, [16]byte{
    65  			0, 0, 0, 0,
    66  			0, 0, 0, 0,
    67  			0, 0, 0, 0,
    68  			0, 0, 0, 0}, nil},
    69  		{bigInt1, [16]byte{
    70  			0, 0, 0, 0,
    71  			0, 0, 0, 0,
    72  			0, 0, 0, 0,
    73  			0, 0, 0, 1}, nil},
    74  		{bigIntNeg1, [16]byte{}, ErrUint128Underflow},
    75  		{bigMaxUint8, [16]byte{
    76  			0, 0, 0, 0,
    77  			0, 0, 0, 0,
    78  			0, 0, 0, 0,
    79  			0, 0, 0, 255}, nil},
    80  		{bigMaxUint64, [16]byte{
    81  			0, 0, 0, 0,
    82  			0, 0, 0, 0,
    83  			255, 255, 255, 255,
    84  			255, 255, 255, 255}, nil},
    85  		{bigMaxUint64Add1, [16]byte{
    86  			0, 0, 0, 0,
    87  			0, 0, 0, 1,
    88  			0, 0, 0, 0,
    89  			0, 0, 0, 0}, nil},
    90  		{bigUint128, [16]byte{
    91  			0, 0, 61, 48,
    92  			241, 156, 209, 0,
    93  			255, 255, 194, 207,
    94  			14, 99, 46, 255}, nil},
    95  		{bigMaxUint128, [16]byte{
    96  			255, 255, 255, 255,
    97  			255, 255, 255, 255,
    98  			255, 255, 255, 255,
    99  			255, 255, 255, 255}, nil},
   100  		{bigMaxUint128Add1, [16]byte{}, ErrUint128Overflow},
   101  	}
   102  	for _, tt := range tests {
   103  		u1, err := NewUint128FromBigInt(tt.input)
   104  		if err != nil {
   105  			assert.Equal(t, tt.expectedErr, err)
   106  			continue
   107  		}
   108  		fsb, err := u1.ToFixedSizeBytes()
   109  		fmt.Println("uint128.Int =", u1.value, "bitlen =", u1.value.BitLen(), "[]bytes =", u1.Bytes(), "[16]bytes =", fsb, "err =", err)
   110  
   111  		if tt.expectedErr != nil {
   112  			assert.Equal(t, tt.expectedErr, err)
   113  			continue
   114  		}
   115  
   116  		assert.Nil(t, u1.Validate(), "Validate doesn't pass.")
   117  		assert.Equal(t, tt.expected, fsb, "ToFixedSizeBytes result doesn't match.")
   118  
   119  		u2 := NewUint128FromFixedSizeBytes(fsb)
   120  		assert.Equal(t, u1.Bytes(), u2.Bytes(), "FromFixedSizeBytes result doesn't match.")
   121  	}
   122  }
   123  
   124  func TestUint128Operation(t *testing.T) {
   125  	a, _ := NewUint128FromInt(10)
   126  	b, _ := NewUint128FromInt(9)
   127  	tmp := NewUint128FromUint(uint64(1 << 63))
   128  	assert.Equal(t, tmp.value.Bytes(), []byte{0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0})
   129  
   130  	sumExpect, _ := NewUint128FromInt(19)
   131  	sumResult, _ := a.Add(b)
   132  	assert.Equal(t, sumExpect.Bytes(), sumResult.Bytes())
   133  
   134  	diffExpect, _ := NewUint128FromInt(1)
   135  	diffResult, _ := a.Sub(b)
   136  	assert.Equal(t, diffExpect.Bytes(), diffResult.Bytes())
   137  
   138  	productExpect, _ := NewUint128FromInt(90)
   139  	productResult, _ := a.Mul(b)
   140  	assert.Equal(t, productExpect.Bytes(), productResult.Bytes())
   141  
   142  	quotientExpect, _ := NewUint128FromInt(1)
   143  	quotientResult, _ := a.Div(b)
   144  	assert.Equal(t, quotientExpect.Bytes(), quotientResult.Bytes())
   145  
   146  	powerExpect, _ := NewUint128FromInt(1000000000)
   147  	powerResult, _ := a.Exp(b)
   148  	assert.Equal(t, powerExpect.Bytes(), powerResult.Bytes())
   149  
   150  	c := a.DeepCopy()
   151  	c.value.SetUint64(2)
   152  	assert.NotEqual(t, a.Bytes(), c.Bytes())
   153  
   154  	assert.Equal(t, a.Cmp(b), 1)
   155  	assert.Equal(t, b.Cmp(a), -1)
   156  	assert.Equal(t, a.Cmp(a), 0)
   157  }