github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/soliton/mvmap/mvmap_test.go (about) 1 // Copyright 2020 WHTCORPS INC, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package mvmap 15 16 import ( 17 "bytes" 18 "encoding/binary" 19 "fmt" 20 "hash/fnv" 21 "testing" 22 23 . "github.com/whtcorpsinc/check" 24 ) 25 26 func TestT(t *testing.T) { 27 TestingT(t) 28 } 29 30 func TestMVMap(t *testing.T) { 31 m := NewMVMap() 32 var vals [][]byte 33 m.Put([]byte("abc"), []byte("abc1")) 34 m.Put([]byte("abc"), []byte("abc2")) 35 m.Put([]byte("def"), []byte("def1")) 36 m.Put([]byte("def"), []byte("def2")) 37 vals = m.Get([]byte("abc"), vals[:0]) 38 if fmt.Sprintf("%s", vals) != "[abc1 abc2]" { 39 t.FailNow() 40 } 41 vals = m.Get([]byte("def"), vals[:0]) 42 if fmt.Sprintf("%s", vals) != "[def1 def2]" { 43 t.FailNow() 44 } 45 46 if m.Len() != 4 { 47 t.FailNow() 48 } 49 50 results := []string{"abc abc1", "abc abc2", "def def1", "def def2"} 51 it := m.NewIterator() 52 for i := 0; i < 4; i++ { 53 key, val := it.Next() 54 if fmt.Sprintf("%s %s", key, val) != results[i] { 55 t.FailNow() 56 } 57 } 58 key, val := it.Next() 59 if key != nil || val != nil { 60 t.FailNow() 61 } 62 } 63 64 func BenchmarkMVMapPut(b *testing.B) { 65 m := NewMVMap() 66 buffer := make([]byte, 8) 67 for i := 0; i < b.N; i++ { 68 binary.BigEndian.PutUint64(buffer, uint64(i)) 69 m.Put(buffer, buffer) 70 } 71 } 72 73 func BenchmarkMVMapGet(b *testing.B) { 74 m := NewMVMap() 75 buffer := make([]byte, 8) 76 for i := 0; i < b.N; i++ { 77 binary.BigEndian.PutUint64(buffer, uint64(i)) 78 m.Put(buffer, buffer) 79 } 80 val := make([][]byte, 0, 8) 81 b.ResetTimer() 82 for i := 0; i < b.N; i++ { 83 binary.BigEndian.PutUint64(buffer, uint64(i)) 84 val = m.Get(buffer, val[:0]) 85 if len(val) != 1 || !bytes.Equal(val[0], buffer) { 86 b.FailNow() 87 } 88 } 89 } 90 91 func TestFNVHash(t *testing.T) { 92 b := []byte{0xcb, 0xf2, 0x9c, 0xe4, 0x84, 0x22, 0x23, 0x25} 93 sum1 := fnvHash64(b) 94 hash := fnv.New64() 95 hash.Reset() 96 hash.Write(b) 97 sum2 := hash.Sum64() 98 if sum1 != sum2 { 99 t.FailNow() 100 } 101 }