github.com/etecs-ru/ristretto@v0.9.1/z/z_test.go (about) 1 /* 2 * Copyright 2019 Dgraph Labs, Inc. and Contributors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package z 18 19 import ( 20 "fmt" 21 "math" 22 "testing" 23 24 "github.com/stretchr/testify/require" 25 ) 26 27 func verifyHashProduct(t *testing.T, wantKey, wantConflict, key, conflict uint64) { 28 require.Equal(t, wantKey, key) 29 require.Equal(t, wantConflict, conflict) 30 } 31 32 func TestKeyToHash(t *testing.T) { 33 var key uint64 34 var conflict uint64 35 36 key, conflict = KeyToHash(uint64(1)) 37 verifyHashProduct(t, 1, 0, key, conflict) 38 39 key, conflict = KeyToHash(1) 40 verifyHashProduct(t, 1, 0, key, conflict) 41 42 key, conflict = KeyToHash(int32(2)) 43 verifyHashProduct(t, 2, 0, key, conflict) 44 45 key, conflict = KeyToHash(int32(-2)) 46 verifyHashProduct(t, math.MaxUint64-1, 0, key, conflict) 47 48 key, conflict = KeyToHash(int64(-2)) 49 verifyHashProduct(t, math.MaxUint64-1, 0, key, conflict) 50 51 key, conflict = KeyToHash(uint32(3)) 52 verifyHashProduct(t, 3, 0, key, conflict) 53 54 key, conflict = KeyToHash(int64(3)) 55 verifyHashProduct(t, 3, 0, key, conflict) 56 } 57 58 func TestMulipleSignals(t *testing.T) { 59 closer := NewCloser(0) 60 require.NotPanics(t, func() { closer.Signal() }) 61 // Should not panic. 62 require.NotPanics(t, func() { closer.Signal() }) 63 require.NotPanics(t, func() { closer.SignalAndWait() }) 64 65 // Attempt 2. 66 closer = NewCloser(1) 67 require.NotPanics(t, func() { closer.Done() }) 68 69 require.NotPanics(t, func() { closer.SignalAndWait() }) 70 // Should not panic. 71 require.NotPanics(t, func() { closer.SignalAndWait() }) 72 require.NotPanics(t, func() { closer.Signal() }) 73 } 74 75 func TestCloser(t *testing.T) { 76 closer := NewCloser(1) 77 go func() { 78 defer closer.Done() 79 <-closer.Ctx().Done() 80 }() 81 closer.SignalAndWait() 82 } 83 84 func TestZeroOut(t *testing.T) { 85 dst := make([]byte, 4*1024) 86 fill := func() { 87 for i := 0; i < len(dst); i++ { 88 dst[i] = 0xFF 89 } 90 } 91 check := func(buf []byte, b byte) { 92 for i := 0; i < len(buf); i++ { 93 require.Equalf(t, b, buf[i], "idx: %d", i) 94 } 95 } 96 fill() 97 98 ZeroOut(dst, 0, 1) 99 check(dst[:1], 0x00) 100 check(dst[1:], 0xFF) 101 102 ZeroOut(dst, 0, 1024) 103 check(dst[:1024], 0x00) 104 check(dst[1024:], 0xFF) 105 106 ZeroOut(dst, 0, len(dst)) 107 check(dst, 0x00) 108 } 109 110 func BenchmarkKeyToHashBytes(b *testing.B) { 111 bench := []interface{}{ 112 []byte("foo"), 113 []byte("barbaz"), 114 []byte("quxquuxquuz"), 115 []byte("corgegraultgarplywaldo"), 116 } 117 benchmarkKeyToHash(b, bench) 118 } 119 120 func BenchmarkKeyToHashString(b *testing.B) { 121 bench := []interface{}{ 122 "foo", 123 "barbaz", 124 "quxquuxquuz", 125 "corgegraultgarplywaldo", 126 } 127 benchmarkKeyToHash(b, bench) 128 } 129 130 var key, conflict uint64 131 132 func benchmarkKeyToHash(b *testing.B, bench []interface{}) { 133 for _, bb := range bench { 134 b.Run(fmt.Sprintf("%s", bb), func(b *testing.B) { 135 switch bb := bb.(type) { 136 case []byte: 137 b.SetBytes(int64(len(bb))) 138 case string: 139 b.SetBytes(int64(len(bb))) 140 } 141 b.ReportAllocs() 142 b.ResetTimer() 143 144 for i := 0; i < b.N; i++ { 145 key, conflict = KeyToHash(bb) 146 } 147 }) 148 } 149 }