1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
package iradix
import (
"fmt"
"sort"
"testing"
"testing/quick"
"golang.org/x/exp/slices"
)
func TestReverseIterator_SeekReverseLowerBoundFuzz(t *testing.T) {
r := New[any]()
var set []string
// This specifies a property where each call adds a new random key to the radix
// tree.
//
// It also maintains a plain sorted list of the same set of keys and asserts
// that iterating from some random key to the beginning using ReverseLowerBound
// produces the same list as filtering all sorted keys that are bigger.
radixAddAndScan := func(newKey, searchKey readableString) []string {
r, _, _ = r.Insert([]byte(newKey), nil)
// Now iterate the tree from searchKey to the beginning
it := r.Root().ReverseIterator()
var result []string
it.SeekReverseLowerBound([]byte(searchKey))
for {
key, _, ok := it.Previous()
if !ok {
break
}
result = append(result, string(key))
}
return result
}
sliceAddSortAndFilter := func(newKey, searchKey readableString) []string {
// Append the key to the set and re-sort
set = append(set, string(newKey))
sort.Strings(set)
t.Logf("Current Set: %#v", set)
t.Logf("Search Key: %#v %v", searchKey, "" >= string(searchKey))
var result []string
for i := len(set) - 1; i >= 0; i-- {
k := set[i]
// Check this is not a duplicate of the previous value we just included.
// Note we don't just store the last string to compare because empty
// string is a valid value in the set and makes comparing on the first
// iteration awkward.
if i < len(set)-1 && set[i+1] == k {
continue
}
if k <= string(searchKey) {
result = append(result, k)
}
}
return result
}
if err := quick.CheckEqual(radixAddAndScan, sliceAddSortAndFilter, nil); err != nil {
t.Error(err)
}
}
func TestReverseIterator_SeekLowerBound(t *testing.T) {
// these should be defined in order
var fixedLenKeys = []string{
"20020",
"00020",
"00010",
"00004",
"00001",
"00000",
}
// these should be defined in order
var mixedLenKeys = []string{
"zip",
"zap",
"found",
"foo",
"f",
"barbazboo",
"abc",
"a1",
}
type exp struct {
keys []string
search string
want []string
}
cases := []exp{
{
fixedLenKeys,
"20020",
fixedLenKeys,
},
{
fixedLenKeys,
"20000",
[]string{
"00020",
"00010",
"00004",
"00001",
"00000",
},
},
{
fixedLenKeys,
"00010",
[]string{
"00010",
"00004",
"00001",
"00000",
},
},
{
fixedLenKeys,
"00000",
[]string{
"00000",
},
},
{
fixedLenKeys,
"0",
[]string{},
},
{
mixedLenKeys,
"{", // after all lower case letters
mixedLenKeys,
},
{
mixedLenKeys,
"zip",
mixedLenKeys,
},
{
mixedLenKeys,
"b",
[]string{
"abc",
"a1",
},
},
{
mixedLenKeys,
"barbazboo0",
[]string{
"barbazboo",
"abc",
"a1",
},
},
{
mixedLenKeys,
"a",
[]string{},
},
{
mixedLenKeys,
"a1",
[]string{
"a1",
},
},
// We SHOULD support keys that are prefixes of each other despite some
// confusion in the original implementation.
{
[]string{"f", "fo", "foo", "food", "bug"},
"foo",
[]string{"foo", "fo", "f", "bug"},
},
{
[]string{"f", "fo", "foo", "food", "bug"},
"foozzzzzzzzzz", // larger than any key but with shared prefix
[]string{"food", "foo", "fo", "f", "bug"},
},
// We also support the empty key (which is a prefix of every other key) as a
// valid key to insert and search for.
{
[]string{"f", "fo", "foo", "food", "bug", ""},
"foo",
[]string{"foo", "fo", "f", "bug", ""},
},
{
[]string{"f", "bug", ""},
"",
[]string{""},
},
{
[]string{"f", "bug", "xylophone"},
"",
[]string{},
},
// This case could panic before. it involves a node with a shared prefix and
// children where the reverse lower bound is greater than all the children
{
[]string{"foo00", "foo11"},
"foo",
[]string{},
},
// When fixing the panic above the above test could pass but we need to
// verify the logic is still correct in the case there was a lower bound in
// another node.
{
[]string{"bar", "foo00", "foo11"},
"foo",
[]string{"bar"},
},
// Found by fuzz test that hit code that wasn't covered by any other example
// here.
{
[]string{"bdgedcdc", "agcbcaba"},
"beefdafg",
[]string{"bdgedcdc", "agcbcaba"},
},
{
[]string{"", "acc", "accea", "accgbbb", "b", "bdebfc", "bdfdcbb", "becccc", "bgefcfc", "c", "cab", "cbd", "cgeaff", "cggfbcb", "cggge", "dcgbd", "ddd", "decfd", "dgb", "e", "edaffec", "ee", "eedc", "efafdbd", "eg", "egf", "egfcd", "f", "fggfdad", "g", "gageecc", "ggd"},
"adgba",
[]string{"accgbbb", "accea", "acc", ""},
},
}
for idx, test := range cases {
t.Run(fmt.Sprintf("case%03d", idx), func(t *testing.T) {
r := New[any]()
// Insert keys
for _, k := range test.keys {
var ok bool
r, _, ok = r.Insert([]byte(k), nil)
if ok {
t.Fatalf("duplicate key %s in keys", k)
}
}
if r.Len() != len(test.keys) {
t.Fatal("failed adding keys")
}
// Get and seek iterator
root := r.Root()
iter := root.ReverseIterator()
iter.SeekReverseLowerBound([]byte(test.search))
// Consume all the keys
var out []string
for {
key, _, ok := iter.Previous()
if !ok {
break
}
out = append(out, string(key))
}
if !slices.Equal(test.want, out) {
t.Fatalf("mis-match: key=%s\n got=%v\n want=%v", test.search,
out, test.want)
}
})
}
}
func TestReverseIterator_SeekPrefix(t *testing.T) {
r := New[any]()
keys := []string{"001", "002", "005", "010", "100"}
for _, k := range keys {
r, _, _ = r.Insert([]byte(k), nil)
}
cases := []struct {
name string
prefix string
expectResult bool
}{
{
name: "existing prefix",
prefix: "005",
expectResult: true,
},
{
name: "non-existing prefix",
prefix: "2",
expectResult: false,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
it := r.Root().ReverseIterator()
it.SeekPrefix([]byte(c.prefix))
if c.expectResult && it.i.node == nil {
t.Errorf("expexted prefix %s to exist", c.prefix)
return
}
if !c.expectResult && it.i.node != nil {
t.Errorf("unexpected node for prefix '%s'", c.prefix)
return
}
})
}
}
func TestReverseIterator_SeekPrefixWatch(t *testing.T) {
key := []byte("key")
// Create tree
r := New[any]()
r, _, _ = r.Insert(key, nil)
// Find mutate channel
it := r.Root().ReverseIterator()
ch := it.SeekPrefixWatch(key)
// Change prefix
tx := r.Txn()
tx.TrackMutate(true)
tx.Insert(key, "value")
tx.Commit()
// Check if channel closed
select {
case <-ch:
default:
t.Errorf("channel not closed")
}
}
func TestReverseIterator_Previous(t *testing.T) {
r := New[any]()
keys := []string{"001", "002", "005", "010", "100"}
for _, k := range keys {
r, _, _ = r.Insert([]byte(k), nil)
}
it := r.Root().ReverseIterator()
for i := len(keys) - 1; i >= 0; i-- {
got, _, _ := it.Previous()
want := keys[i]
if string(got) != want {
t.Errorf("got: %v, want: %v", got, want)
}
}
}
|