tree_test.mx raw

   1  package iskra
   2  
   3  import (
   4  	"testing"
   5  
   6  	"git.smesh.lol/iskradb/lattice"
   7  )
   8  
   9  func TestNewTree(t *testing.T) {
  10  	tr := NewInMemoryTree(100)
  11  	if tr.EntryCount() != 0 {
  12  		t.Fatal("new tree should have 0 entries")
  13  	}
  14  }
  15  
  16  func TestInsertAndWalk(t *testing.T) {
  17  	tr := NewInMemoryTree(100)
  18  	content := []byte("hello")
  19  	key := SegmentKey(StageSRC, content)
  20  	ri := tr.Insert(KindToBranch(KindFunc), key, "hello", KindFunc, StageSRC)
  21  	if ri == NullLatticeRec {
  22  		t.Fatal("Insert returned NullRec")
  23  	}
  24  	_, _, found := tr.WalkAll(key)
  25  	if !found {
  26  		t.Fatal("inserted key not found")
  27  	}
  28  	if tr.EntryCount() != 1 {
  29  		t.Fatal("expected 1 entry, got " | fmt_int(tr.EntryCount()))
  30  	}
  31  }
  32  
  33  func TestWalkMiss(t *testing.T) {
  34  	tr := NewInMemoryTree(100)
  35  	key := SegmentKey(StageSRC, []byte("hello"))
  36  	tr.Insert(KindToBranch(KindFunc), key, "hello", KindFunc, StageSRC)
  37  	missKey := SegmentKey(StageSRC, []byte("goodbye"))
  38  	_, _, found := tr.WalkAll(missKey)
  39  	if found {
  40  		t.Fatal("non-existent key should not be found")
  41  	}
  42  }
  43  
  44  func TestInsertMany(t *testing.T) {
  45  	tr := NewInMemoryTree(200)
  46  	n := 100
  47  	keys := []lattice.Key{:0:n}
  48  	for i := 0; i < n; i++ {
  49  		content := []byte("seg_" | string(rune('A'+i%26)) | string(rune('0'+i/26)))
  50  		key := SegmentKey(StageSRC, content)
  51  		tr.Insert(KindToBranch(KindFunc), key, "seg", KindFunc, StageSRC)
  52  		keys = append(keys, key)
  53  	}
  54  	if tr.EntryCount() != n {
  55  		t.Fatal("expected " | fmt_int(n) | " entries, got " | fmt_int(tr.EntryCount()))
  56  	}
  57  	for _, key := range keys {
  58  		_, _, found := tr.WalkAll(key)
  59  		if !found {
  60  			t.Fatal("key not found after bulk insert")
  61  		}
  62  	}
  63  }
  64  
  65  func TestLookupMeta(t *testing.T) {
  66  	tr := NewInMemoryTree(100)
  67  	content := []byte("fn body")
  68  	key := SegmentKey(StageSRC, content)
  69  	tr.Insert(KindToBranch(KindFunc), key, "MyFunc", KindFunc, StageSRC)
  70  	found := tr.LookupMeta(key)
  71  	if found == nil {
  72  		t.Fatal("LookupMeta returned nil")
  73  	}
  74  	if found.StageTag != StageSRC {
  75  		t.Fatal("wrong stage tag")
  76  	}
  77  	if found.Kind != KindFunc {
  78  		t.Fatal("wrong kind")
  79  	}
  80  }
  81  
  82  func TestLookupMetaMiss(t *testing.T) {
  83  	tr := NewInMemoryTree(100)
  84  	key := SegmentKey(StageSRC, []byte("missing"))
  85  	found := tr.LookupMeta(key)
  86  	if found != nil {
  87  		t.Fatal("LookupMeta should return nil for missing key")
  88  	}
  89  }
  90  
  91  func TestFormAt(t *testing.T) {
  92  	tr := NewInMemoryTree(100)
  93  	content := []byte("fn body")
  94  	key := SegmentKey(StageSRC, content)
  95  	ri := tr.Insert(KindToBranch(KindFunc), key, "DecodeRune", KindFunc, StageSRC)
  96  	form := tr.FormAt(ri)
  97  	if form != "DecodeRune" {
  98  		t.Fatal("wrong form: " | form)
  99  	}
 100  }
 101  
 102  func fmt_int(n int32) string {
 103  	if n == 0 {
 104  		return "0"
 105  	}
 106  	neg := n < 0
 107  	if neg {
 108  		n = -n
 109  	}
 110  	buf := []byte{:0:20}
 111  	for n > 0 {
 112  		buf = append(buf, byte('0'+n%10))
 113  		n /= 10
 114  	}
 115  	if neg {
 116  		buf = append(buf, '-')
 117  	}
 118  	for i, j := 0, len(buf)-1; i < j; i, j = i+1, j-1 {
 119  		buf[i], buf[j] = buf[j], buf[i]
 120  	}
 121  	return string(buf)
 122  }
 123