package iskra import "testing" func TestTritPath(t *testing.T) { p := MakeTritPath(BranchFunc, SubControl, 2) if p.Branch() != BranchFunc { t.Fatal("wrong branch") } if p.Trit(1) != SubControl { t.Fatal("wrong sub-branch") } if p.Trit(2) != 2 { t.Fatal("wrong trit at depth 2") } if p.Depth() != 3 { t.Fatal("expected depth 3, got " | fmt_int(p.Depth())) } } func TestTritPathEmpty(t *testing.T) { p := MakeTritPath() if p.Depth() != 0 { t.Fatal("empty path should have depth 0") } if p.Branch() != TritEnd { t.Fatal("empty path branch should be TritEnd") } } func TestKindToTritPath(t *testing.T) { tests := []struct { kind NodeKind branch uint8 }{ {KindType, BranchType}, {KindFunc, BranchFunc}, {KindMethod, BranchType}, {KindConst, BranchData}, {KindVar, BranchData}, {KindImport, BranchData}, {KindPkg, BranchData}, {KindControl, BranchFunc}, {KindExpr, BranchFunc}, {KindStmt, BranchFunc}, {KindField, BranchType}, } for _, tt := range tests { p := KindToTritPath(tt.kind) if p.Branch() != tt.branch { t.Fatal("wrong branch for kind") } if p.Depth() < 1 { t.Fatal("mapped path should have depth >= 1") } } } func TestKindToTritPathUnknown(t *testing.T) { p := KindToTritPath(KindUnknown) if p.Depth() != 0 { t.Fatal("unknown kind should map to empty path") } } func TestTritPathRoundtrip(t *testing.T) { p := MakeTritPath(BranchType, SubField, 3) var m MetaEntry m.SetTritPath(p) got := m.GetTritPath() if got != p { t.Fatal("TritPath round-trip failed") } } func TestTritPathDistance(t *testing.T) { a := MakeTritPath(BranchFunc, SubControl) b := MakeTritPath(BranchFunc, SubExpression) d := TritPathDistance(a, b) if d != 2 { t.Fatal("same-branch different-sub should be distance 2, got " | fmt_int(d)) } c := MakeTritPath(BranchType, SubField) d2 := TritPathDistance(a, c) if d2 != 4 { t.Fatal("cross-branch should be distance 4, got " | fmt_int(d2)) } same := TritPathDistance(a, a) if same != 0 { t.Fatal("same path should be distance 0") } empty := MakeTritPath() if TritPathDistance(a, empty) != -1 { t.Fatal("unassigned path should return -1") } }