github.com/flower-corp/rosedb@v1.1.2-0.20230117132829-21dc4f7b319a/util/hash_test.go (about) 1 package util 2 3 import ( 4 "github.com/stretchr/testify/assert" 5 "strconv" 6 "sync" 7 "testing" 8 ) 9 10 func TestMemHash(t *testing.T) { 11 type args struct { 12 buf []byte 13 } 14 tests := []struct { 15 name string 16 args args 17 }{ 18 { 19 "1", args{buf: []byte("aa")}, 20 }, 21 { 22 "2", args{buf: []byte("11")}, 23 }, 24 { 25 "3", args{buf: []byte("0")}, 26 }, 27 } 28 29 for _, tt := range tests { 30 t.Run(tt.name, func(t *testing.T) { 31 got := MemHash(tt.args.buf) 32 assert.NotZero(t, got) 33 }) 34 } 35 } 36 37 func TestMemHash2(t *testing.T) { 38 m := make(map[uint64]struct{}) 39 for i := 0; i < 1000000; i++ { 40 v := MemHash([]byte("lotusdb")) 41 m[v] = struct{}{} 42 } 43 // all hash values should be the same in one process. 44 assert.Equal(t, 1, len(m)) 45 } 46 47 func TestMemHash3(t *testing.T) { 48 // different groutines 49 key := []byte("lotusdb") 50 wg := sync.WaitGroup{} 51 wg.Add(2) 52 53 var ( 54 hash1 uint64 55 hash2 uint64 56 ) 57 go func() { 58 hash1 = MemHash(key) 59 wg.Done() 60 }() 61 62 go func() { 63 hash2 = MemHash(key) 64 wg.Done() 65 }() 66 67 wg.Wait() 68 assert.Equal(t, hash1, hash2) 69 } 70 71 func TestMemHash_SpecialCase(t *testing.T) { 72 buf := make([]byte, 0) 73 u := rthash(buf, 0) 74 assert.Equal(t, u, uint64(0)) 75 } 76 77 func BenchmarkMemHash(b *testing.B) { 78 b.ResetTimer() 79 b.ReportAllocs() 80 for i := 0; i < b.N; i++ { 81 MemHash([]byte(strconv.Itoa(i * 1000))) 82 } 83 }