branch_test.mx raw

   1  package iskra
   2  
   3  import "testing"
   4  
   5  func TestTritPath(t *testing.T) {
   6  	p := MakeTritPath(BranchFunc, SubControl, 2)
   7  	if p.Branch() != BranchFunc {
   8  		t.Fatal("wrong branch")
   9  	}
  10  	if p.Trit(1) != SubControl {
  11  		t.Fatal("wrong sub-branch")
  12  	}
  13  	if p.Trit(2) != 2 {
  14  		t.Fatal("wrong trit at depth 2")
  15  	}
  16  	if p.Depth() != 3 {
  17  		t.Fatal("expected depth 3, got " | fmt_int(p.Depth()))
  18  	}
  19  }
  20  
  21  func TestTritPathEmpty(t *testing.T) {
  22  	p := MakeTritPath()
  23  	if p.Depth() != 0 {
  24  		t.Fatal("empty path should have depth 0")
  25  	}
  26  	if p.Branch() != TritEnd {
  27  		t.Fatal("empty path branch should be TritEnd")
  28  	}
  29  }
  30  
  31  func TestKindToTritPath(t *testing.T) {
  32  	tests := []struct {
  33  		kind   NodeKind
  34  		branch uint8
  35  	}{
  36  		{KindType, BranchType},
  37  		{KindFunc, BranchFunc},
  38  		{KindMethod, BranchType},
  39  		{KindConst, BranchData},
  40  		{KindVar, BranchData},
  41  		{KindImport, BranchData},
  42  		{KindPkg, BranchData},
  43  		{KindControl, BranchFunc},
  44  		{KindExpr, BranchFunc},
  45  		{KindStmt, BranchFunc},
  46  		{KindField, BranchType},
  47  	}
  48  	for _, tt := range tests {
  49  		p := KindToTritPath(tt.kind)
  50  		if p.Branch() != tt.branch {
  51  			t.Fatal("wrong branch for kind")
  52  		}
  53  		if p.Depth() < 1 {
  54  			t.Fatal("mapped path should have depth >= 1")
  55  		}
  56  	}
  57  }
  58  
  59  func TestKindToTritPathUnknown(t *testing.T) {
  60  	p := KindToTritPath(KindUnknown)
  61  	if p.Depth() != 0 {
  62  		t.Fatal("unknown kind should map to empty path")
  63  	}
  64  }
  65  
  66  func TestTritPathRoundtrip(t *testing.T) {
  67  	p := MakeTritPath(BranchType, SubField, 3)
  68  	var m MetaEntry
  69  	m.SetTritPath(p)
  70  	got := m.GetTritPath()
  71  	if got != p {
  72  		t.Fatal("TritPath round-trip failed")
  73  	}
  74  }
  75  
  76  func TestTritPathDistance(t *testing.T) {
  77  	a := MakeTritPath(BranchFunc, SubControl)
  78  	b := MakeTritPath(BranchFunc, SubExpression)
  79  	d := TritPathDistance(a, b)
  80  	if d != 2 {
  81  		t.Fatal("same-branch different-sub should be distance 2, got " | fmt_int(d))
  82  	}
  83  
  84  	c := MakeTritPath(BranchType, SubField)
  85  	d2 := TritPathDistance(a, c)
  86  	if d2 != 4 {
  87  		t.Fatal("cross-branch should be distance 4, got " | fmt_int(d2))
  88  	}
  89  
  90  	same := TritPathDistance(a, a)
  91  	if same != 0 {
  92  		t.Fatal("same path should be distance 0")
  93  	}
  94  
  95  	empty := MakeTritPath()
  96  	if TritPathDistance(a, empty) != -1 {
  97  		t.Fatal("unassigned path should return -1")
  98  	}
  99  }
 100