github.com/petermattis/pebble@v0.0.0-20190905164901-ab51a2166067/internal/base/internal_test.go (about) 1 // Copyright 2012 The LevelDB-Go and Pebble Authors. All rights reserved. Use 2 // of this source code is governed by a BSD-style license that can be found in 3 // the LICENSE file. 4 5 package base 6 7 import ( 8 "testing" 9 ) 10 11 func (k InternalKey) encodedString() string { 12 buf := make([]byte, k.Size()) 13 k.Encode(buf) 14 return string(buf) 15 } 16 17 func TestInternalKey(t *testing.T) { 18 k := MakeInternalKey([]byte("foo"), 0x08070605040302, 1) 19 if got, want := k.encodedString(), "foo\x01\x02\x03\x04\x05\x06\x07\x08"; got != want { 20 t.Fatalf("k = %q want %q", got, want) 21 } 22 if !k.Valid() { 23 t.Fatalf("invalid key") 24 } 25 if got, want := string(k.UserKey), "foo"; got != want { 26 t.Errorf("ukey = %q want %q", got, want) 27 } 28 if got, want := k.Kind(), InternalKeyKind(1); got != want { 29 t.Errorf("kind = %d want %d", got, want) 30 } 31 if got, want := k.SeqNum(), uint64(0x08070605040302); got != want { 32 t.Errorf("seqNum = %d want %d", got, want) 33 } 34 } 35 36 func TestInvalidInternalKey(t *testing.T) { 37 testCases := []string{ 38 "", 39 "\x01\x02\x03\x04\x05\x06\x07", 40 "foo", 41 "foo\x08\x07\x06\x05\x04\x03\x02", 42 "foo\x12\x07\x06\x05\x04\x03\x02\x01", 43 } 44 for _, tc := range testCases { 45 k := DecodeInternalKey([]byte(tc)) 46 if k.Valid() { 47 t.Errorf("%q is a valid key, want invalid", tc) 48 } 49 } 50 } 51 52 func TestInternalKeyComparer(t *testing.T) { 53 // keys are some internal keys, in sorted order. 54 keys := []string{ 55 // The empty key is not a valid internal key, but it still must 56 // sort lower than any other key. It is used as a zero value when 57 // checking that a sequence of internal keys are in sorted order. 58 "", 59 // The next two keys are also invalid internal keys. They are 'less 60 // than' any valid internal key, and 'greater than' the empty key. 61 "A", 62 "B", 63 // The remaining test keys are all valid. 64 "" + "\x01\xff\xff\xff\xff\xff\xff\xff", 65 "" + "\x00\xff\xff\xff\xff\xff\xff\xff", 66 "" + "\x01\x01\x00\x00\x00\x00\x00\x00", 67 "" + "\x00\x01\x00\x00\x00\x00\x00\x00", 68 "" + "\x01\x00\x00\x00\x00\x00\x00\x00", 69 "" + "\x00\x00\x00\x00\x00\x00\x00\x00", 70 "\x00" + "\x00\x00\x00\x00\x00\x00\x00\x00", 71 "\x00blue" + "\x01\x11\x00\x00\x00\x00\x00\x00", 72 "bl\x00ue" + "\x01\x11\x00\x00\x00\x00\x00\x00", 73 "blue" + "\x01\x11\x00\x00\x00\x00\x00\x00", 74 "blue\x00" + "\x01\x11\x00\x00\x00\x00\x00\x00", 75 "green" + "\x01\x11\x00\x00\x00\x00\x00\x00", 76 "red" + "\x01\xff\xff\xff\xff\xff\xff\xff", 77 "red" + "\x01\x72\x73\x74\x75\x76\x77\x78", 78 "red" + "\x01\x00\x00\x00\x00\x00\x00\x11", 79 "red" + "\x01\x00\x00\x00\x00\x00\x11\x00", 80 "red" + "\x01\x00\x00\x00\x00\x11\x00\x00", 81 "red" + "\x01\x00\x00\x00\x11\x00\x00\x00", 82 "red" + "\x01\x00\x00\x11\x00\x00\x00\x00", 83 "red" + "\x01\x00\x11\x00\x00\x00\x00\x00", 84 "red" + "\x01\x11\x00\x00\x00\x00\x00\x00", 85 "red" + "\x00\x11\x00\x00\x00\x00\x00\x00", 86 "red" + "\x00\x00\x00\x00\x00\x00\x00\x00", 87 "\xfe" + "\x01\xff\xff\xff\xff\xff\xff\xff", 88 "\xfe" + "\x00\x00\x00\x00\x00\x00\x00\x00", 89 "\xff" + "\x01\xff\xff\xff\xff\xff\xff\xff", 90 "\xff" + "\x00\x00\x00\x00\x00\x00\x00\x00", 91 "\xff\x40" + "\x01\xff\xff\xff\xff\xff\xff\xff", 92 "\xff\x40" + "\x00\x00\x00\x00\x00\x00\x00\x00", 93 "\xff\xff" + "\x01\xff\xff\xff\xff\xff\xff\xff", 94 "\xff\xff" + "\x00\x00\x00\x00\x00\x00\x00\x00", 95 } 96 c := DefaultComparer.Compare 97 for i := range keys { 98 for j := range keys { 99 ik := DecodeInternalKey([]byte(keys[i])) 100 jk := DecodeInternalKey([]byte(keys[j])) 101 got := InternalCompare(c, ik, jk) 102 want := 0 103 if i < j { 104 want = -1 105 } else if i > j { 106 want = +1 107 } 108 if got != want { 109 t.Errorf("i=%d, j=%d, keys[i]=%q, keys[j]=%q: got %d, want %d", 110 i, j, keys[i], keys[j], got, want) 111 } 112 } 113 } 114 } 115 116 func TestInternalKeySeparator(t *testing.T) { 117 testCases := []struct { 118 a string 119 b string 120 expected string 121 }{ 122 {"foo.SET.100", "foo.SET.99", "foo.SET.100"}, 123 {"foo.SET.100", "foo.SET.100", "foo.SET.100"}, 124 {"foo.SET.100", "foo.DEL.100", "foo.SET.100"}, 125 {"foo.SET.100", "foo.SET.101", "foo.SET.100"}, 126 {"foo.SET.100", "bar.SET.99", "foo.SET.100"}, 127 {"foo.SET.100", "hello.SET.200", "g.MAX.72057594037927935"}, 128 {"ABC1AAAAA.SET.100", "ABC2ABB.SET.200", "ABC2.MAX.72057594037927935"}, 129 {"AAA1AAA.SET.100", "AAA2AA.SET.200", "AAA2.MAX.72057594037927935"}, 130 {"AAA1AAA.SET.100", "AAA4.SET.200", "AAA2.MAX.72057594037927935"}, 131 {"AAA1AAA.SET.100", "AAA2.SET.200", "AAA1B.MAX.72057594037927935"}, 132 {"AAA1AAA.SET.100", "AAA2A.SET.200", "AAA2.MAX.72057594037927935"}, 133 {"AAA1.SET.100", "AAA2.SET.200", "AAA1.SET.100"}, 134 {"foo.SET.100", "foobar.SET.200", "foo.SET.100"}, 135 {"foobar.SET.100", "foo.SET.200", "foobar.SET.100"}, 136 } 137 d := DefaultComparer 138 for _, c := range testCases { 139 t.Run("", func(t *testing.T) { 140 a := ParseInternalKey(c.a) 141 b := ParseInternalKey(c.b) 142 expected := ParseInternalKey(c.expected) 143 result := a.Separator(d.Compare, d.Separator, nil, b) 144 if cmp := InternalCompare(d.Compare, expected, result); cmp != 0 { 145 t.Fatalf("expected %s, but found %s", expected, result) 146 } 147 }) 148 } 149 }