package iskra import "testing" func TestNewTree(t *testing.T) { tr := NewInMemoryTree(100) if tr.EntryCount() != 0 { t.Fatal("new tree should have 0 entries") } } func TestInsertAndWalk(t *testing.T) { tr := NewInMemoryTree(100) content := []byte("hello") key := SegmentKey(StageSRC, content) ri := tr.Insert(KindToBranch(KindFunc), key, "hello", KindFunc, StageSRC) if ri == NullLatticeRec { t.Fatal("Insert returned NullRec") } _, _, found := tr.WalkAll(key) if !found { t.Fatal("inserted key not found") } if tr.EntryCount() != 1 { t.Fatal("expected 1 entry, got " | fmt_int(tr.EntryCount())) } } func TestWalkMiss(t *testing.T) { tr := NewInMemoryTree(100) key := SegmentKey(StageSRC, []byte("hello")) tr.Insert(KindToBranch(KindFunc), key, "hello", KindFunc, StageSRC) missKey := SegmentKey(StageSRC, []byte("goodbye")) _, _, found := tr.WalkAll(missKey) if found { t.Fatal("non-existent key should not be found") } } func TestInsertMany(t *testing.T) { tr := NewInMemoryTree(200) n := 100 keys := []uint64{:0:n} for i := 0; i < n; i++ { content := []byte("seg_" | string(rune('A'+i%26)) | string(rune('0'+i/26))) key := SegmentKey(StageSRC, content) tr.Insert(KindToBranch(KindFunc), key, "seg", KindFunc, StageSRC) keys = append(keys, key) } if tr.EntryCount() != n { t.Fatal("expected " | fmt_int(n) | " entries, got " | fmt_int(tr.EntryCount())) } for _, key := range keys { _, _, found := tr.WalkAll(key) if !found { t.Fatal("key not found after bulk insert") } } } func TestLookupMeta(t *testing.T) { tr := NewInMemoryTree(100) content := []byte("fn body") key := SegmentKey(StageSRC, content) tr.Insert(KindToBranch(KindFunc), key, "MyFunc", KindFunc, StageSRC) found := tr.LookupMeta(key) if found == nil { t.Fatal("LookupMeta returned nil") } if found.StageTag != StageSRC { t.Fatal("wrong stage tag") } if found.Kind != KindFunc { t.Fatal("wrong kind") } } func TestLookupMetaMiss(t *testing.T) { tr := NewInMemoryTree(100) key := SegmentKey(StageSRC, []byte("missing")) found := tr.LookupMeta(key) if found != nil { t.Fatal("LookupMeta should return nil for missing key") } } func TestFormAt(t *testing.T) { tr := NewInMemoryTree(100) content := []byte("fn body") key := SegmentKey(StageSRC, content) ri := tr.Insert(KindToBranch(KindFunc), key, "DecodeRune", KindFunc, StageSRC) form := tr.FormAt(ri) if form != "DecodeRune" { t.Fatal("wrong form: " | form) } } func fmt_int(n int) string { if n == 0 { return "0" } neg := n < 0 if neg { n = -n } buf := []byte{:0:20} for n > 0 { buf = append(buf, byte('0'+n%10)) n /= 10 } if neg { buf = append(buf, '-') } for i, j := 0, len(buf)-1; i < j; i, j = i+1, j-1 { buf[i], buf[j] = buf[j], buf[i] } return string(buf) }