github.com/turingchain2020/turingchain@v1.1.21/common/common_test.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package common
     6  
     7  import (
     8  	"math"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestRandString(t *testing.T) {
    15  	str := GetRandString(10)
    16  	t.Log(str)
    17  	assert.Len(t, str, 10)
    18  }
    19  
    20  func TestInt64Add(t *testing.T) {
    21  	i := math.MaxInt64
    22  	assert.Equal(t, i+1, -9223372036854775808)
    23  	assert.Equal(t, i+2, -9223372036854775807)
    24  }
    25  
    26  func TestPointer(t *testing.T) {
    27  	id := StorePointer(0)
    28  	assert.Equal(t, int64(1), id)
    29  	data, err := GetPointer(id)
    30  	assert.Nil(t, err)
    31  	assert.Equal(t, 0, data)
    32  
    33  	RemovePointer(id)
    34  	_, err = GetPointer(id)
    35  	assert.Equal(t, ErrPointerNotFound, err)
    36  }
    37  
    38  func TestRandStringLen(t *testing.T) {
    39  	str := GetRandBytes(10, 20)
    40  	t.Log(string(str))
    41  	if len(str) < 10 || len(str) > 20 {
    42  		t.Error("rand str len")
    43  	}
    44  }
    45  
    46  func TestGetRandPrintString(t *testing.T) {
    47  	str := GetRandPrintString(10, 20)
    48  	t.Log(str)
    49  	if len(str) < 10 || len(str) > 20 {
    50  		t.Error("rand str len")
    51  	}
    52  }
    53  
    54  func TestMinMax(t *testing.T) {
    55  	assert.Equal(t, MinInt32(1, 2), int32(1))
    56  	assert.Equal(t, MaxInt32(1, 2), int32(2))
    57  }
    58  
    59  func TestHex(t *testing.T) {
    60  	var data [Sha256Len]byte
    61  	assert.Equal(t, "0000000000000000000000000000000000000000000000000000000000000000", HashHex(data[:]))
    62  	assert.Equal(t, 64, len(HashHex(data[:])))
    63  	assert.Equal(t, "0x0000000000000000000000000000000000000000000000000000000000000000", ToHex(data[:]))
    64  	bdata, err := FromHex("0x0000000000000000000000000000000000000000000000000000000000000000")
    65  	assert.Nil(t, err)
    66  	assert.Equal(t, bdata, data[:])
    67  
    68  	bdata, err = FromHex("0X0000000000000000000000000000000000000000000000000000000000000000")
    69  	assert.Nil(t, err)
    70  	assert.Equal(t, bdata, data[:])
    71  
    72  	bdata, err = FromHex("0000000000000000000000000000000000000000000000000000000000000000")
    73  	assert.Nil(t, err)
    74  	assert.Equal(t, bdata, data[:])
    75  
    76  	data2 := CopyBytes(data[:])
    77  	data[0] = 1
    78  	assert.Equal(t, data2[0], uint8(0))
    79  
    80  	assert.Equal(t, false, IsHex("0x"))
    81  	assert.Equal(t, false, IsHex("0x0"))
    82  	assert.Equal(t, true, IsHex("0x00"))
    83  }
    84  
    85  func TestHash(t *testing.T) {
    86  	var data [Sha256Len]byte
    87  	assert.Equal(t, "0x66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925", ToHex(Sha256(data[:])))
    88  	assert.Equal(t, "0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563", ToHex(Sha3(data[:])))
    89  	assert.Equal(t, "0x2b32db6c2c0a6235fb1397e8225ea85e0f0e6e8c7b126d0016ccbde0e667151e", ToHex(Sha2Sum(data[:])))
    90  	assert.Equal(t, "0xb8bcb07f6344b42ab04250c86a6e8b75d3fdbbc6", ToHex(Rimp160(data[:])))
    91  }