func TestTreeEachBetween(t *testing.T) { tree := NewTree() for i := 11; i < 20; i++ { tree.Put([]byte(fmt.Sprint(i)), []byte(fmt.Sprint(i)), 1) } var foundKeys, cmpKeys [][]byte var foundValues, cmpValues [][]byte for i := 10; i < 21; i++ { for j := i; j < 21; j++ { foundKeys, cmpKeys, foundValues, cmpValues = nil, nil, nil, nil tree.EachBetween([]byte(fmt.Sprint(i)), []byte(fmt.Sprint(j)), true, true, func(key []byte, byteValue []byte, timestamp int64) bool { foundKeys = append(foundKeys, key) foundValues = append(foundValues, byteValue) return true }) cmpKeys, cmpValues = createKVArraysUp(common.Max(11, i), common.Min(j+1, 20)) if !reflect.DeepEqual(cmpKeys, foundKeys) || !reflect.DeepEqual(cmpValues, foundValues) { t.Errorf("%v.EachBetween(%v, %v, true, true) => %v should be %v", tree, i, j, foundValues, cmpValues) } foundKeys, cmpKeys, foundValues, cmpValues = nil, nil, nil, nil tree.EachBetween([]byte(fmt.Sprint(i)), []byte(fmt.Sprint(j)), false, true, func(key []byte, byteValue []byte, timestamp int64) bool { foundKeys = append(foundKeys, key) foundValues = append(foundValues, byteValue) return true }) cmpKeys, cmpValues = createKVArraysUp(common.Max(11, i+1), common.Min(j+1, 20)) if !reflect.DeepEqual(cmpKeys, foundKeys) || !reflect.DeepEqual(cmpValues, foundValues) { t.Errorf("%v.EachBetween(%v, %v, false, true) => %v should be %v", tree, i, j, foundValues, cmpValues) } foundKeys, cmpKeys, foundValues, cmpValues = nil, nil, nil, nil tree.EachBetween([]byte(fmt.Sprint(i)), []byte(fmt.Sprint(j)), true, false, func(key []byte, byteValue []byte, timestamp int64) bool { foundKeys = append(foundKeys, key) foundValues = append(foundValues, byteValue) return true }) cmpKeys, cmpValues = createKVArraysUp(common.Max(11, i), common.Min(j, 20)) if !reflect.DeepEqual(cmpKeys, foundKeys) || !reflect.DeepEqual(cmpValues, foundValues) { t.Errorf("%v.EachBetween(%v, %v, true, false) => %v should be %v", tree, i, j, foundValues, cmpValues) } foundKeys, cmpKeys, foundValues, cmpValues = nil, nil, nil, nil tree.EachBetween([]byte(fmt.Sprint(i)), []byte(fmt.Sprint(j)), false, false, func(key []byte, byteValue []byte, timestamp int64) bool { foundKeys = append(foundKeys, key) foundValues = append(foundValues, byteValue) return true }) cmpKeys, cmpValues = createKVArraysUp(common.Max(11, i+1), common.Min(j, 20)) if !reflect.DeepEqual(cmpKeys, foundKeys) || !reflect.DeepEqual(cmpValues, foundValues) { t.Errorf("%v.EachBetween(%v, %v, false, false) => %v should be %v", tree, i, j, foundValues, cmpValues) } } } }
func testCounts(t *testing.T, dhashes []*Node, c testClient) { var key []byte var value []byte subTree := []byte("jaguar") c.SubAddConfiguration(subTree, "mirrored", "yes") for i := byte(1); i < 9; i++ { key = []byte{i} value = []byte{19 - i} c.SSubPut(subTree, key, value) } assertMirrored(t, dhashes, c, subTree) for i := byte(0); i < 10; i++ { for j := byte(0); j < 10; j++ { wanted := common.Max(0, common.Min(int(j+1), 9)-common.Max(int(i), 1)) found := c.Count([]byte("jaguar"), []byte{i}, []byte{j}, true, true) if found != wanted { t.Errorf("wrong count for %v-%v true true, wanted %v but found %v", i, j, wanted, found) } wanted = common.Max(0, common.Min(int(j), 9)-common.Max(int(i), 1)) found = c.Count([]byte("jaguar"), []byte{i}, []byte{j}, true, false) if found != wanted { t.Errorf("wrong count for %v-%v true false, wanted %v but found %v", i, j, wanted, found) } wanted = common.Max(0, common.Min(int(j+1), 9)-common.Max(int(i+1), 1)) found = c.Count([]byte("jaguar"), []byte{i}, []byte{j}, false, true) if found != wanted { t.Errorf("wrong count for %v-%v true false, wanted %v but found %v", i, j, wanted, found) } wanted = common.Max(0, common.Min(int(j), 9)-common.Max(int(i+1), 1)) found = c.Count([]byte("jaguar"), []byte{i}, []byte{j}, false, false) if found != wanted { t.Errorf("wrong count for %v-%v false false, wanted %v but found %v", i, j, wanted, found) } wanted = common.Max(0, common.Min(int(j+10), 19)-common.Max(int(i+11), 11)) found = c.MirrorCount([]byte("jaguar"), []byte{i + 10}, []byte{j + 10}, false, false) if found != wanted { t.Errorf("wrong count for mirror %v-%v false false, wanted %v but found %v", i+10, j+10, wanted, found) } wanted = common.Max(0, common.Min(int(j+10), 19)-common.Max(int(i+10), 11)) found = c.MirrorCount([]byte("jaguar"), []byte{i + 10}, []byte{j + 10}, true, false) if found != wanted { t.Errorf("wrong count for mirror %v-%v true false, wanted %v but found %v", i+10, j+10, wanted, found) } wanted = common.Max(0, common.Min(int(j+11), 19)-common.Max(int(i+11), 11)) found = c.MirrorCount([]byte("jaguar"), []byte{i + 10}, []byte{j + 10}, false, true) if found != wanted { t.Errorf("wrong count for mirror %v-%v false true, wanted %v but found %v", i+10, j+10, wanted, found) } wanted = common.Max(0, common.Min(int(j+11), 19)-common.Max(int(i+10), 11)) found = c.MirrorCount([]byte("jaguar"), []byte{i + 10}, []byte{j + 10}, true, true) if found != wanted { t.Errorf("wrong count for mirror %v-%v true true, wanted %v but found %v", i+10, j+10, wanted, found) } } } }
func TestTreeSizeBetween(t *testing.T) { tree := NewTree() for i := 11; i < 20; i++ { tree.Put([]byte(fmt.Sprint(i)), []byte(fmt.Sprint(i)), 1) } for i := 10; i < 21; i++ { for j := i; j < 21; j++ { expected := common.Max(0, common.Min(j+1, 20)-common.Max(11, i)) val := tree.SizeBetween([]byte(fmt.Sprint(i)), []byte(fmt.Sprint(j)), true, true) if val != expected { t.Errorf("%v.SizeBetween(%v, %v, true, true) should be %v but was %v", tree.Describe(), common.HexEncode([]byte(fmt.Sprint(i))), common.HexEncode([]byte(fmt.Sprint(j))), expected, val) } expected = common.Max(0, common.Min(j+1, 20)-common.Max(11, i+1)) val = tree.SizeBetween([]byte(fmt.Sprint(i)), []byte(fmt.Sprint(j)), false, true) if val != expected { t.Errorf("%v.SizeBetween(%v, %v, false, true) should be %v but was %v", tree.Describe(), common.HexEncode([]byte(fmt.Sprint(i))), common.HexEncode([]byte(fmt.Sprint(j))), expected, val) } expected = common.Max(0, common.Min(j, 20)-common.Max(11, i)) val = tree.SizeBetween([]byte(fmt.Sprint(i)), []byte(fmt.Sprint(j)), true, false) if val != expected { t.Errorf("%v.SizeBetween(%v, %v, true, false) should be %v but was %v", tree.Describe(), common.HexEncode([]byte(fmt.Sprint(i))), common.HexEncode([]byte(fmt.Sprint(j))), expected, val) } expected = common.Max(0, common.Min(j, 20)-common.Max(11, i+1)) val = tree.SizeBetween([]byte(fmt.Sprint(i)), []byte(fmt.Sprint(j)), false, false) if val != expected { t.Errorf("%v.SizeBetween(%v, %v, false, false) should be %v but was %v", tree.Describe(), common.HexEncode([]byte(fmt.Sprint(i))), common.HexEncode([]byte(fmt.Sprint(j))), expected, val) } } } for i := 0; i < 10; i++ { tree.SubPut([]byte{50}, []byte{byte(i)}, []byte{byte(i)}, 1) } ary := []byte{50} if s := tree.SizeBetween(ary, ary, true, true); s != 10 { t.Errorf("wrong size calculated for %v\nbetween %v and %v\nwanted %v but got %v", tree.Describe(), common.HexEncode(ary), common.HexEncode(ary), 10, s) } }
func TestTreeReverseEachBetweenIndex(t *testing.T) { tree := NewTree() for i := 11; i < 20; i++ { tree.Put([]byte(fmt.Sprint(i)), []byte(fmt.Sprint(i)), 1) } var foundKeys, cmpKeys [][]byte var foundValues, cmpValues [][]byte for i := -1; i < 10; i++ { for j := i; j < 10; j++ { foundKeys, cmpKeys, foundValues, cmpValues = nil, nil, nil, nil tree.ReverseEachBetweenIndex(&i, &j, func(key []byte, byteValue []byte, timestamp int64, index int) bool { foundKeys = append(foundKeys, key) foundValues = append(foundValues, byteValue) return true }) cmpKeys, cmpValues = createKVArraysDown(common.Max(11, 19-j), common.Min(20, 20-i)) if !reflect.DeepEqual(cmpKeys, foundKeys) || !reflect.DeepEqual(cmpValues, foundValues) { t.Errorf("%v.EachBetweenIndex(%v, %v) => %v should be %v", tree, i, j, foundValues, cmpValues) } } } }