github.com/benz9527/xboot@v0.0.0-20240504061247-c23f15593274/lib/kv/hasher_test.go (about)

     1  package kv
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  type hashObj struct {
    10  	val1 int
    11  	val2 string
    12  	val3 bool
    13  	val4 *float64
    14  	val5 map[string]struct{}
    15  	val6 []int
    16  }
    17  
    18  func TestHasher(t *testing.T) {
    19  	intKey := 100
    20  	intHasher := newHasher[int]()
    21  	require.Equal(t, intHasher.Hash(intKey), intHasher.Hash(intKey))
    22  
    23  	intPtrHasher := newHasher[*int]()
    24  	require.Equal(t, intPtrHasher.Hash(&intKey), intPtrHasher.Hash(&intKey))
    25  
    26  	strKey := "abc"
    27  	strHasher := newHasher[string]()
    28  	require.Equal(t, strHasher.Hash(strKey), strHasher.Hash(strKey))
    29  
    30  	strPtrHasher := newHasher[*string]()
    31  	require.Equal(t, strPtrHasher.Hash(&strKey), strPtrHasher.Hash(&strKey))
    32  
    33  	floatKey := 100.0
    34  	floatHasher := newHasher[float64]()
    35  	require.Equal(t, floatHasher.Hash(floatKey), floatHasher.Hash(floatKey))
    36  
    37  	floatPtrHasher := newHasher[*float64]()
    38  	require.Equal(t, floatPtrHasher.Hash(&floatKey), floatPtrHasher.Hash(&floatKey))
    39  
    40  	objKey := hashObj{val1: 100, val2: "abc", val3: true, val4: nil, val5: nil, val6: nil}
    41  	objPtrHasher := newHasher[*hashObj]()
    42  	require.Equal(t, objPtrHasher.Hash(&objKey), objPtrHasher.Hash(&objKey))
    43  
    44  	newSeedObjPtrHasher := newSeedHasher[*hashObj](objPtrHasher)
    45  	require.NotEqual(t, objPtrHasher.Hash(&objKey), newSeedObjPtrHasher.Hash(&objKey))
    46  }