github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/internal/rthash/hash_test.go (about) 1 package rthash 2 3 import ( 4 "math/rand" 5 "reflect" 6 "testing" 7 ) 8 9 func TestHashFunc(t *testing.T) { 10 testCases := []struct { 11 value any 12 hashFunc any 13 }{ 14 {"abc", NewHashFunc[string]()}, 15 {int8(19), NewHashFunc[int8]()}, 16 {uint8(19), NewHashFunc[uint8]()}, 17 {int16(12345), NewHashFunc[int16]()}, 18 {uint16(12345), NewHashFunc[uint16]()}, 19 {int32(8484848), NewHashFunc[int32]()}, 20 {uint32(8484848), NewHashFunc[uint32]()}, 21 {int64(1234567890), NewHashFunc[int64]()}, 22 {uint64(1234567890), NewHashFunc[uint64]()}, 23 {int(1234567890), NewHashFunc[int]()}, 24 {uint64(1234567890), NewHashFunc[uint64]()}, 25 {uintptr(1234567890), NewHashFunc[uintptr]()}, 26 {float32(1.1314), NewHashFunc[float32]()}, 27 {float64(1.1314), NewHashFunc[float64]()}, 28 {complex(float32(1.1314), float32(1.1314)), NewHashFunc[complex64]()}, 29 {complex(float64(1.1314), float64(1.1314)), NewHashFunc[complex128]()}, 30 {hashable{1234, "1234"}, NewHashFunc[hashable]()}, 31 } 32 33 for _, tc := range testCases { 34 hash := reflect.ValueOf(tc.hashFunc). 35 Call([]reflect.Value{reflect.ValueOf(tc.value)})[0].Interface().(uintptr) 36 t.Logf("%T: %v, hash: %d", tc.value, tc.value, hash) 37 } 38 } 39 40 func TestBytesHash(t *testing.T) { 41 testCases := []string{ 42 "123", 43 "abc", 44 } 45 f := NewBytesHash() 46 for _, x := range testCases { 47 hash := f([]byte(x)) 48 t.Logf("bytes: %s, hash: %d", x, hash) 49 } 50 } 51 52 type hashable struct { 53 A int 54 B string 55 } 56 57 func BenchmarkHashFunc_Int64(b *testing.B) { 58 f := NewHashFunc[int64]() 59 x := rand.Int63() 60 for i := 0; i < b.N; i++ { 61 _ = f(x) 62 } 63 } 64 65 func BenchmarkHashFunc_String(b *testing.B) { 66 f := NewHashFunc[string]() 67 x := "this is a short sample text" 68 for i := 0; i < b.N; i++ { 69 _ = f(x) 70 } 71 } 72 73 func BenchmarkHashFunc_Bytes(b *testing.B) { 74 f := NewBytesHash() 75 x := []byte("this is a short sample text") 76 for i := 0; i < b.N; i++ { 77 _ = f(x) 78 } 79 }