distance_test.mx raw
1 package iskra
2
3 import (
4 "testing"
5
6 "git.smesh.lol/iskradb/lattice"
7 )
8
9 func TestTreeDistanceSameKey(t *testing.T) {
10 tr := NewInMemoryTree(100)
11 content := []byte("a")
12 key := SegmentKey(StageSRC, content)
13 tr.Insert(KindToBranch(KindFunc), key, "a", KindFunc, StageSRC)
14
15 d := tr.TreeDistance(lattice.Bverb, key, key)
16 if d != 0 {
17 t.Fatal("same key should have distance 0")
18 }
19 }
20
21 func TestTreeDistanceSameNode(t *testing.T) {
22 tr := NewInMemoryTree(100)
23 k1 := SegmentKey(StageSRC, []byte("aa"))
24 k2 := SegmentKey(StageSRC, []byte("bb"))
25 tr.Insert(KindToBranch(KindFunc), k1, "aa", KindFunc, StageSRC)
26 tr.Insert(KindToBranch(KindFunc), k2, "bb", KindFunc, StageSRC)
27
28 d := tr.TreeDistance(lattice.Bverb, k1, k2)
29 if d < 0 {
30 t.Fatal("distance should be >= 0 for found keys")
31 }
32 }
33
34 func TestTreeDistanceMultiLevel(t *testing.T) {
35 tr := NewInMemoryTree(100)
36 keys := []uint64{:0:20}
37 for i := 0; i < 20; i++ {
38 content := []byte("dist_" | string(rune('A'+i)))
39 key := SegmentKey(StageSRC, content)
40 tr.Insert(KindToBranch(KindFunc), key, "dist", KindFunc, StageSRC)
41 keys = append(keys, key)
42 }
43 d := tr.TreeDistance(lattice.Bverb, keys[0], keys[len(keys)-1])
44 if d < 0 {
45 t.Fatal("distance should be >= 0 for found keys")
46 }
47 }
48
49 func TestWalkPath(t *testing.T) {
50 tr := NewInMemoryTree(100)
51 content := []byte("wp")
52 key := SegmentKey(StageSRC, content)
53 tr.Insert(KindToBranch(KindFunc), key, "wp", KindFunc, StageSRC)
54
55 path := tr.WalkPath(lattice.Bverb, key)
56 if len(path) < 1 {
57 t.Fatal("path should have at least root")
58 }
59 }
60