-
Notifications
You must be signed in to change notification settings - Fork 0
/
encindexkeys_test.go
59 lines (56 loc) · 1.45 KB
/
encindexkeys_test.go
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
package edb
import (
"fmt"
"strconv"
"strings"
"testing"
)
func TestIndexDiffing(t *testing.T) {
tests := []struct {
old string
new string
removed string
}{
{"", "", ""},
{"", "1:a", ""},
{"1:a", "", "1:a"},
{"1:abc", "", "1:abc"},
{"1:a 1:b", "1:a", "1:b"},
{"1:a 1:b", "1:b", "1:a"},
{"1:a 1:b", "1:a 1:b", ""},
{"1:a 2:a 2:b", "", "1:a 2:a 2:b"},
{"1:a 2:a 2:b", "1:a", "2:a 2:b"},
{"1:a 2:a 2:b", "2:a", "1:a 2:b"},
{"1:a 2:a 2:b", "2:b", "1:a 2:a"},
{"1:a 2:a 2:b", "1:a 2:a", "2:b"},
{"1:a 2:a 2:b", "2:a 2:b", "1:a"},
{"1:a 2:a 2:b", "1:a 2:b", "2:a"},
{"1:a 2:a 2:b", "1:a 2:a 2:b", ""},
}
for _, tt := range tests {
oldKeys := parseIndexKeys(tt.old)
newKeys := parseIndexKeys(tt.new)
oldKeysData := appendIndexKeys(nil, oldKeys)
var removedKeys []string
findRemovedIndexKeys(oldKeysData, newKeys, func(ord uint64, key []byte) {
removedKeys = append(removedKeys, fmt.Sprintf("%d:%s", ord, key))
})
actual := strings.Join(removedKeys, " ")
if actual != tt.removed {
t.Errorf("** Removed(%s => %s) == %q, expected %q", tt.old, tt.new, actual, tt.removed)
}
}
}
func parseIndexKeys(s string) indexRows {
cc := strings.Fields(s)
rows := make(indexRows, len(cc))
for i, c := range cc {
ordStr, keyStr, ok := splitByte(c, ':')
if !ok {
panic("invalid entry: " + c)
}
ord := must(strconv.ParseUint(ordStr, 10, 64))
rows[i] = IndexRow{IndexOrd: ord, KeyRaw: []byte(keyStr)}
}
return rows
}