github.com/pingcap/badger@v1.5.1-0.20230103063557-828f39b09b6d/cache/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 "math" 21 "testing" 22 ) 23 24 func verifyHashProduct(t *testing.T, wantKey, wantConflict, key, conflict uint64) { 25 if wantKey != key || wantConflict != conflict { 26 t.Errorf("expected (%d, %d) but got (%d, %d)\n", 27 wantKey, wantConflict, key, conflict) 28 } 29 } 30 31 func TestKeyToHash(t *testing.T) { 32 var key uint64 33 var conflict uint64 34 35 key, conflict = KeyToHash(uint64(1)) 36 verifyHashProduct(t, 1, 0, key, conflict) 37 38 key, conflict = KeyToHash(1) 39 verifyHashProduct(t, 1, 0, key, conflict) 40 41 key, conflict = KeyToHash(int32(2)) 42 verifyHashProduct(t, 2, 0, key, conflict) 43 44 key, conflict = KeyToHash(int32(-2)) 45 verifyHashProduct(t, math.MaxUint64-1, 0, key, conflict) 46 47 key, conflict = KeyToHash(int64(-2)) 48 verifyHashProduct(t, math.MaxUint64-1, 0, key, conflict) 49 50 key, conflict = KeyToHash(uint32(3)) 51 verifyHashProduct(t, 3, 0, key, conflict) 52 53 key, conflict = KeyToHash(int64(3)) 54 verifyHashProduct(t, 3, 0, key, conflict) 55 }