package lattice import "testing" func TestBranchRotation(t *testing.T) { tests := []struct { from, to uint8 want Rotation }{ // Clockwise (right): N→V, V→M, M→N {BranchType, BranchFunc, RotRight}, {BranchFunc, BranchData, RotRight}, {BranchData, BranchType, RotRight}, // Counterclockwise (left): N→M, M→V, V→N {BranchType, BranchData, RotLeft}, {BranchData, BranchFunc, RotLeft}, {BranchFunc, BranchType, RotLeft}, // Same branch (stay) {BranchType, BranchType, RotStay}, {BranchFunc, BranchFunc, RotStay}, {BranchData, BranchData, RotStay}, // Unknown (unassigned branch) {TritEnd, BranchType, RotUnknown}, {BranchType, TritEnd, RotUnknown}, {TritEnd, TritEnd, RotUnknown}, } for _, tt := range tests { got := BranchRotation(tt.from, tt.to) if got != tt.want { t.Errorf("BranchRotation(%d, %d) = %s, want %s", tt.from, tt.to, got, tt.want) } } } func TestRotationMetaEntryRoundtrip(t *testing.T) { // Test all 12 rotation values round-trip through MetaEntry. all := []Rotation{ RotUnknown, RotRight, RotLeft, RotStay, RotList, RotDefine, RotRelate, RotStop, RotExclaim, RotQuestion, RotQuoteOpen, RotQuoteClose, } for _, r := range all { var m MetaEntry m.SetRotation(r) got := m.Rotation() if got != r { t.Errorf("roundtrip %d: got %d", r, got) } } } func TestRotationDoesNotClobberTritPath(t *testing.T) { var m MetaEntry p := MakeTritPath(BranchData, 3, 2) m.SetTritPath(p) m.SetRotation(RotQuoteOpen) // highest punctuation value if m.TritPath() != p { t.Errorf("rotation clobbered TritPath: got %08x, want %08x", uint32(m.TritPath()), uint32(p)) } if m.Rotation() != RotQuoteOpen { t.Errorf("rotation lost: got %s, want quote-open", m.Rotation()) } } func TestBigramTransitionRoundtrip(t *testing.T) { var m MetaEntry target := MakeTritPath(BranchFunc, 1) // verb, finite m.SetBigramTransition(RotRight, target) gotRot, gotPath := m.GetBigramTransition() if gotRot != RotRight { t.Errorf("rotation: got %s, want right", gotRot) } if gotPath != target { t.Errorf("target path: got %08x, want %08x", uint32(gotPath), uint32(target)) } } func TestBigramTransitionAllCombinations(t *testing.T) { targets := []TritPath{ MakeTritPath(BranchType, 1), MakeTritPath(BranchType, 3), MakeTritPath(BranchFunc, 1), MakeTritPath(BranchData, 3, 1), } rots := []Rotation{RotRight, RotLeft, RotStay, RotList, RotStop, RotQuoteOpen} for _, rot := range rots { for _, tp := range targets { var m MetaEntry m.SetBigramTransition(rot, tp) gotRot, gotPath := m.GetBigramTransition() if gotRot != rot || gotPath != tp { t.Errorf("SetBigramTransition(%s, %08x): got (%s, %08x)", rot, uint32(tp), gotRot, uint32(gotPath)) } } } } func TestRuneToRotation(t *testing.T) { tests := []struct { r rune want Rotation }{ {',', RotList}, {':', RotDefine}, {';', RotRelate}, {'.', RotStop}, {'!', RotExclaim}, {'?', RotQuestion}, {'"', RotQuoteOpen}, {'\u201C', RotQuoteOpen}, // " {'\u00AB', RotQuoteOpen}, // « {'\u201D', RotQuoteClose}, // " {'\u00BB', RotQuoteClose}, // » {'a', RotUnknown}, {'-', RotUnknown}, } for _, tt := range tests { got := RuneToRotation(tt.r) if got != tt.want { t.Errorf("RuneToRotation(%q) = %d, want %d", tt.r, got, tt.want) } } } func TestIsPunctuation(t *testing.T) { branch := []Rotation{RotUnknown, RotRight, RotLeft, RotStay} for _, r := range branch { if r.IsPunctuation() { t.Errorf("%s should not be punctuation", r) } } punct := []Rotation{RotList, RotDefine, RotRelate, RotStop, RotExclaim, RotQuestion, RotQuoteOpen, RotQuoteClose} for _, r := range punct { if !r.IsPunctuation() { t.Errorf("%s should be punctuation", r) } } } func TestIsBreak(t *testing.T) { breaks := []Rotation{RotStop, RotExclaim, RotQuestion} for _, r := range breaks { if !r.IsBreak() { t.Errorf("%s should be a break", r) } } nonBreaks := []Rotation{RotRight, RotStay, RotList, RotDefine, RotRelate} for _, r := range nonBreaks { if r.IsBreak() { t.Errorf("%s should not be a break", r) } } } func TestTritPathDistance(t *testing.T) { tests := []struct { name string a, b TritPath want int }{ {"same leaf", MakeTritPath(BranchType, 1), MakeTritPath(BranchType, 1), 0}, {"noun common vs noun proper", MakeTritPath(BranchType, 1), MakeTritPath(BranchType, 2), 2}, {"noun common vs noun common numeral", MakeTritPath(BranchType, 1), MakeTritPath(BranchType, 1, 2), 1}, {"noun common vs verb finite", MakeTritPath(BranchType, 1), MakeTritPath(BranchFunc, 1), 4}, {"modifier connective prep vs noun common", MakeTritPath(BranchData, 3, 1), MakeTritPath(BranchType, 1), 5}, {"modifier connective conjunction vs particle", MakeTritPath(BranchData, 3, 2), MakeTritPath(BranchData, 3, 3), 2}, {"unassigned vs noun", MakeTritPath(), MakeTritPath(BranchType, 1), -1}, } for _, tt := range tests { got := TritPathDistance(tt.a, tt.b) if got != tt.want { t.Errorf("%s: TritPathDistance = %d, want %d", tt.name, got, tt.want) } } } func TestTritPathDistanceSymmetric(t *testing.T) { paths := []TritPath{ MakeTritPath(BranchType, 1), MakeTritPath(BranchFunc, 1), MakeTritPath(BranchData, 3, 2), MakeTritPath(BranchType, 1, 2), } for _, a := range paths { for _, b := range paths { d1 := TritPathDistance(a, b) d2 := TritPathDistance(b, a) if d1 != d2 { t.Errorf("asymmetric: d(%08x,%08x)=%d != d(%08x,%08x)=%d", uint32(a), uint32(b), d1, uint32(b), uint32(a), d2) } } } } func TestRotationString(t *testing.T) { tests := []struct { r Rotation want string }{ {RotRight, "right"}, {RotLeft, "left"}, {RotStay, "stay"}, {RotList, ","}, {RotStop, "."}, {RotQuestion, "?"}, } for _, tt := range tests { if got := tt.r.String(); got != tt.want { t.Errorf("Rotation(%d).String() = %q, want %q", tt.r, got, tt.want) } } }