github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/internal/base/internal_test.go (about) 1 // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors. 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 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package base 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/require" 21 ) 22 23 func (k InternalKey) encodedString() string { 24 buf := make([]byte, k.Size()) 25 k.Encode(buf) 26 return string(buf) 27 } 28 29 func TestInternalKey(t *testing.T) { 30 k := MakeInternalKey([]byte("foo"), 0x08070605040302, 1) 31 if got, want := k.encodedString(), "foo\x01\x02\x03\x04\x05\x06\x07\x08"; got != want { 32 t.Fatalf("k = %q want %q", got, want) 33 } 34 if !k.Valid() { 35 t.Fatalf("invalid key") 36 } 37 if got, want := string(k.UserKey), "foo"; got != want { 38 t.Errorf("ukey = %q want %q", got, want) 39 } 40 if got, want := k.Kind(), InternalKeyKind(1); got != want { 41 t.Errorf("kind = %d want %d", got, want) 42 } 43 if got, want := k.SeqNum(), uint64(0x08070605040302); got != want { 44 t.Errorf("seqNum = %d want %d", got, want) 45 } 46 } 47 48 func TestInvalidInternalKey(t *testing.T) { 49 testCases := []string{ 50 "", 51 "\x01\x02\x03\x04\x05\x06\x07", 52 "foo", 53 "foo\x08\x07\x06\x05\x04\x03\x02", 54 "foo\x13\x07\x06\x05\x04\x03\x02\x01", 55 } 56 for _, tc := range testCases { 57 k := DecodeInternalKey([]byte(tc)) 58 if k.Valid() { 59 t.Errorf("%q is a valid key, want invalid", tc) 60 } 61 62 if k.Kind() == InternalKeyKindInvalid && k.UserKey != nil { 63 t.Errorf("expected nil UserKey after decoding encodedKey=%q", tc) 64 } 65 } 66 } 67 68 func TestInternalKeyComparer(t *testing.T) { 69 keys := []string{ 70 "" + "\x01\xff\xff\xff\xff\xff\xff\xff", 71 "" + "\x00\xff\xff\xff\xff\xff\xff\xff", 72 "" + "\x01\x01\x00\x00\x00\x00\x00\x00", 73 "" + "\x00\x01\x00\x00\x00\x00\x00\x00", 74 "", 75 "" + "\x01\x00\x00\x00\x00\x00\x00\x00", 76 "" + "\x00\x00\x00\x00\x00\x00\x00\x00", 77 "\x00" + "\x00\x00\x00\x00\x00\x00\x00\x00", 78 "\x00blue" + "\x01\x11\x00\x00\x00\x00\x00\x00", 79 "bl\x00ue" + "\x01\x11\x00\x00\x00\x00\x00\x00", 80 "blue" + "\x01\x11\x00\x00\x00\x00\x00\x00", 81 "blue\x00" + "\x01\x11\x00\x00\x00\x00\x00\x00", 82 "green" + "\xff\x11\x00\x00\x00\x00\x00\x00", 83 "green" + "\x01\x11\x00\x00\x00\x00\x00\x00", 84 "green" + "\x01\x00\x00\x00\x00\x00\x00\x00", 85 "red" + "\x01\xff\xff\xff\xff\xff\xff\xff", 86 "red" + "\x01\x72\x73\x74\x75\x76\x77\x78", 87 "red" + "\x01\x00\x00\x00\x00\x00\x00\x11", 88 "red" + "\x01\x00\x00\x00\x00\x00\x11\x00", 89 "red" + "\x01\x00\x00\x00\x00\x11\x00\x00", 90 "red" + "\x01\x00\x00\x00\x11\x00\x00\x00", 91 "red" + "\x01\x00\x00\x11\x00\x00\x00\x00", 92 "red" + "\x01\x00\x11\x00\x00\x00\x00\x00", 93 "red" + "\x01\x11\x00\x00\x00\x00\x00\x00", 94 "red" + "\x00\x11\x00\x00\x00\x00\x00\x00", 95 "red" + "\x00\x00\x00\x00\x00\x00\x00\x00", 96 "\xfe" + "\x01\xff\xff\xff\xff\xff\xff\xff", 97 "\xfe" + "\x00\x00\x00\x00\x00\x00\x00\x00", 98 "\xff" + "\x01\xff\xff\xff\xff\xff\xff\xff", 99 "\xff" + "\x00\x00\x00\x00\x00\x00\x00\x00", 100 "\xff\x40" + "\x01\xff\xff\xff\xff\xff\xff\xff", 101 "\xff\x40" + "\x00\x00\x00\x00\x00\x00\x00\x00", 102 "\xff\xff" + "\x01\xff\xff\xff\xff\xff\xff\xff", 103 "\xff\xff" + "\x00\x00\x00\x00\x00\x00\x00\x00", 104 } 105 c := DefaultComparer.Compare 106 for i := range keys { 107 for j := range keys { 108 ik := DecodeInternalKey([]byte(keys[i])) 109 jk := DecodeInternalKey([]byte(keys[j])) 110 got := InternalCompare(c, ik, jk) 111 want := 0 112 if i < j { 113 want = -1 114 } else if i > j { 115 want = +1 116 } 117 if got != want { 118 t.Errorf("i=%d, j=%d, keys[i]=%q, keys[j]=%q: got %d, want %d", 119 i, j, keys[i], keys[j], got, want) 120 } 121 } 122 } 123 } 124 125 func TestMakeInternalValue(t *testing.T) { 126 value := []byte("test_value") 127 seqNum := uint64(1) 128 kind := InternalKeyKindSet 129 iValue := MakeInternalValue(value, seqNum, kind) 130 require.Equal(t, iValue.SeqNum(), seqNum) 131 require.Equal(t, iValue.Kind(), kind) 132 } 133 134 func TestEncodeInternalValue(t *testing.T) { 135 value := []byte("test_value") 136 seqNum := uint64(10) 137 kind := InternalKeyKindSet 138 encodeValue, closer := EncodeInternalValue(value, seqNum, kind) 139 defer func() { 140 if closer != nil { 141 closer() 142 } 143 }() 144 iv := DecodeInternalValue(encodeValue) 145 require.Equal(t, iv.SeqNum(), seqNum) 146 require.Equal(t, iv.Kind(), kind) 147 }