rotation_test.go raw

   1  package lattice
   2  
   3  import "testing"
   4  
   5  func TestBranchRotation(t *testing.T) {
   6  	tests := []struct {
   7  		from, to uint8
   8  		want     Rotation
   9  	}{
  10  		// Clockwise (right): N→V, V→M, M→N
  11  		{BranchType, BranchFunc, RotRight},
  12  		{BranchFunc, BranchData, RotRight},
  13  		{BranchData, BranchType, RotRight},
  14  
  15  		// Counterclockwise (left): N→M, M→V, V→N
  16  		{BranchType, BranchData, RotLeft},
  17  		{BranchData, BranchFunc, RotLeft},
  18  		{BranchFunc, BranchType, RotLeft},
  19  
  20  		// Same branch (stay)
  21  		{BranchType, BranchType, RotStay},
  22  		{BranchFunc, BranchFunc, RotStay},
  23  		{BranchData, BranchData, RotStay},
  24  
  25  		// Unknown (unassigned branch)
  26  		{TritEnd, BranchType, RotUnknown},
  27  		{BranchType, TritEnd, RotUnknown},
  28  		{TritEnd, TritEnd, RotUnknown},
  29  	}
  30  	for _, tt := range tests {
  31  		got := BranchRotation(tt.from, tt.to)
  32  		if got != tt.want {
  33  			t.Errorf("BranchRotation(%d, %d) = %s, want %s",
  34  				tt.from, tt.to, got, tt.want)
  35  		}
  36  	}
  37  }
  38  
  39  func TestRotationMetaEntryRoundtrip(t *testing.T) {
  40  	// Test all 12 rotation values round-trip through MetaEntry.
  41  	all := []Rotation{
  42  		RotUnknown, RotRight, RotLeft, RotStay,
  43  		RotList, RotDefine, RotRelate, RotStop,
  44  		RotExclaim, RotQuestion, RotQuoteOpen, RotQuoteClose,
  45  	}
  46  	for _, r := range all {
  47  		var m MetaEntry
  48  		m.SetRotation(r)
  49  		got := m.Rotation()
  50  		if got != r {
  51  			t.Errorf("roundtrip %d: got %d", r, got)
  52  		}
  53  	}
  54  }
  55  
  56  func TestRotationDoesNotClobberTritPath(t *testing.T) {
  57  	var m MetaEntry
  58  	p := MakeTritPath(BranchData, 3, 2)
  59  	m.SetTritPath(p)
  60  	m.SetRotation(RotQuoteOpen) // highest punctuation value
  61  
  62  	if m.TritPath() != p {
  63  		t.Errorf("rotation clobbered TritPath: got %08x, want %08x",
  64  			uint32(m.TritPath()), uint32(p))
  65  	}
  66  	if m.Rotation() != RotQuoteOpen {
  67  		t.Errorf("rotation lost: got %s, want quote-open", m.Rotation())
  68  	}
  69  }
  70  
  71  func TestBigramTransitionRoundtrip(t *testing.T) {
  72  	var m MetaEntry
  73  	target := MakeTritPath(BranchFunc, 1) // verb, finite
  74  	m.SetBigramTransition(RotRight, target)
  75  
  76  	gotRot, gotPath := m.GetBigramTransition()
  77  	if gotRot != RotRight {
  78  		t.Errorf("rotation: got %s, want right", gotRot)
  79  	}
  80  	if gotPath != target {
  81  		t.Errorf("target path: got %08x, want %08x",
  82  			uint32(gotPath), uint32(target))
  83  	}
  84  }
  85  
  86  func TestBigramTransitionAllCombinations(t *testing.T) {
  87  	targets := []TritPath{
  88  		MakeTritPath(BranchType, 1),
  89  		MakeTritPath(BranchType, 3),
  90  		MakeTritPath(BranchFunc, 1),
  91  		MakeTritPath(BranchData, 3, 1),
  92  	}
  93  	rots := []Rotation{RotRight, RotLeft, RotStay, RotList, RotStop, RotQuoteOpen}
  94  
  95  	for _, rot := range rots {
  96  		for _, tp := range targets {
  97  			var m MetaEntry
  98  			m.SetBigramTransition(rot, tp)
  99  			gotRot, gotPath := m.GetBigramTransition()
 100  			if gotRot != rot || gotPath != tp {
 101  				t.Errorf("SetBigramTransition(%s, %08x): got (%s, %08x)",
 102  					rot, uint32(tp), gotRot, uint32(gotPath))
 103  			}
 104  		}
 105  	}
 106  }
 107  
 108  func TestRuneToRotation(t *testing.T) {
 109  	tests := []struct {
 110  		r    rune
 111  		want Rotation
 112  	}{
 113  		{',', RotList},
 114  		{':', RotDefine},
 115  		{';', RotRelate},
 116  		{'.', RotStop},
 117  		{'!', RotExclaim},
 118  		{'?', RotQuestion},
 119  		{'"', RotQuoteOpen},
 120  		{'\u201C', RotQuoteOpen},  // "
 121  		{'\u00AB', RotQuoteOpen},  // «
 122  		{'\u201D', RotQuoteClose}, // "
 123  		{'\u00BB', RotQuoteClose}, // »
 124  		{'a', RotUnknown},
 125  		{'-', RotUnknown},
 126  	}
 127  	for _, tt := range tests {
 128  		got := RuneToRotation(tt.r)
 129  		if got != tt.want {
 130  			t.Errorf("RuneToRotation(%q) = %d, want %d", tt.r, got, tt.want)
 131  		}
 132  	}
 133  }
 134  
 135  func TestIsPunctuation(t *testing.T) {
 136  	branch := []Rotation{RotUnknown, RotRight, RotLeft, RotStay}
 137  	for _, r := range branch {
 138  		if r.IsPunctuation() {
 139  			t.Errorf("%s should not be punctuation", r)
 140  		}
 141  	}
 142  	punct := []Rotation{RotList, RotDefine, RotRelate, RotStop,
 143  		RotExclaim, RotQuestion, RotQuoteOpen, RotQuoteClose}
 144  	for _, r := range punct {
 145  		if !r.IsPunctuation() {
 146  			t.Errorf("%s should be punctuation", r)
 147  		}
 148  	}
 149  }
 150  
 151  func TestIsBreak(t *testing.T) {
 152  	breaks := []Rotation{RotStop, RotExclaim, RotQuestion}
 153  	for _, r := range breaks {
 154  		if !r.IsBreak() {
 155  			t.Errorf("%s should be a break", r)
 156  		}
 157  	}
 158  	nonBreaks := []Rotation{RotRight, RotStay, RotList, RotDefine, RotRelate}
 159  	for _, r := range nonBreaks {
 160  		if r.IsBreak() {
 161  			t.Errorf("%s should not be a break", r)
 162  		}
 163  	}
 164  }
 165  
 166  func TestTritPathDistance(t *testing.T) {
 167  	tests := []struct {
 168  		name string
 169  		a, b TritPath
 170  		want int
 171  	}{
 172  		{"same leaf",
 173  			MakeTritPath(BranchType, 1),
 174  			MakeTritPath(BranchType, 1), 0},
 175  		{"noun common vs noun proper",
 176  			MakeTritPath(BranchType, 1),
 177  			MakeTritPath(BranchType, 2), 2},
 178  		{"noun common vs noun common numeral",
 179  			MakeTritPath(BranchType, 1),
 180  			MakeTritPath(BranchType, 1, 2), 1},
 181  		{"noun common vs verb finite",
 182  			MakeTritPath(BranchType, 1),
 183  			MakeTritPath(BranchFunc, 1), 4},
 184  		{"modifier connective prep vs noun common",
 185  			MakeTritPath(BranchData, 3, 1),
 186  			MakeTritPath(BranchType, 1), 5},
 187  		{"modifier connective conjunction vs particle",
 188  			MakeTritPath(BranchData, 3, 2),
 189  			MakeTritPath(BranchData, 3, 3), 2},
 190  		{"unassigned vs noun",
 191  			MakeTritPath(),
 192  			MakeTritPath(BranchType, 1), -1},
 193  	}
 194  	for _, tt := range tests {
 195  		got := TritPathDistance(tt.a, tt.b)
 196  		if got != tt.want {
 197  			t.Errorf("%s: TritPathDistance = %d, want %d",
 198  				tt.name, got, tt.want)
 199  		}
 200  	}
 201  }
 202  
 203  func TestTritPathDistanceSymmetric(t *testing.T) {
 204  	paths := []TritPath{
 205  		MakeTritPath(BranchType, 1),
 206  		MakeTritPath(BranchFunc, 1),
 207  		MakeTritPath(BranchData, 3, 2),
 208  		MakeTritPath(BranchType, 1, 2),
 209  	}
 210  	for _, a := range paths {
 211  		for _, b := range paths {
 212  			d1 := TritPathDistance(a, b)
 213  			d2 := TritPathDistance(b, a)
 214  			if d1 != d2 {
 215  				t.Errorf("asymmetric: d(%08x,%08x)=%d != d(%08x,%08x)=%d",
 216  					uint32(a), uint32(b), d1, uint32(b), uint32(a), d2)
 217  			}
 218  		}
 219  	}
 220  }
 221  
 222  func TestRotationString(t *testing.T) {
 223  	tests := []struct {
 224  		r    Rotation
 225  		want string
 226  	}{
 227  		{RotRight, "right"},
 228  		{RotLeft, "left"},
 229  		{RotStay, "stay"},
 230  		{RotList, ","},
 231  		{RotStop, "."},
 232  		{RotQuestion, "?"},
 233  	}
 234  	for _, tt := range tests {
 235  		if got := tt.r.String(); got != tt.want {
 236  			t.Errorf("Rotation(%d).String() = %q, want %q", tt.r, got, tt.want)
 237  		}
 238  	}
 239  }
 240