branch_test.go raw

   1  package lattice
   2  
   3  import "testing"
   4  
   5  func TestMakeTritPath(t *testing.T) {
   6  	// Empty path: all zeros = unassigned.
   7  	p := MakeTritPath()
   8  	if p.Depth() != 0 {
   9  		t.Errorf("empty path depth: got %d, want 0", p.Depth())
  10  	}
  11  	if p.Branch() != TritEnd {
  12  		t.Errorf("empty path branch: got %d, want %d", p.Branch(), TritEnd)
  13  	}
  14  	if uint32(p) != 0 {
  15  		t.Errorf("empty path value: got 0x%08x, want 0x00000000", uint32(p))
  16  	}
  17  
  18  	// Noun, common: [1, 1, 0, 0, ...]
  19  	p = MakeTritPath(BranchType, SubField)
  20  	if p.Depth() != 2 {
  21  		t.Errorf("noun common depth: got %d, want 2", p.Depth())
  22  	}
  23  	if p.Branch() != BranchType {
  24  		t.Errorf("noun common branch: got %d, want %d", p.Branch(), BranchType)
  25  	}
  26  	if p.Trit(1) != 1 {
  27  		t.Errorf("noun common trit[1]: got %d, want 1", p.Trit(1))
  28  	}
  29  
  30  	// Verb, finite: [2, 1, 0, ...]
  31  	p = MakeTritPath(BranchFunc, SubField)
  32  	if p.Branch() != BranchFunc {
  33  		t.Errorf("verb branch: got %d, want %d", p.Branch(), BranchFunc)
  34  	}
  35  	if p.Depth() != 2 {
  36  		t.Errorf("verb depth: got %d, want 2", p.Depth())
  37  	}
  38  
  39  	// Modifier, connective, conjunction: [3, 3, 2, 0, ...]
  40  	p = MakeTritPath(BranchData, 3, 2)
  41  	if p.Depth() != 3 {
  42  		t.Errorf("conjunction depth: got %d, want 3", p.Depth())
  43  	}
  44  	if p.Trit(0) != BranchData {
  45  		t.Errorf("conjunction trit[0]: got %d, want %d", p.Trit(0), BranchData)
  46  	}
  47  	if p.Trit(1) != 3 {
  48  		t.Errorf("conjunction trit[1]: got %d, want 3", p.Trit(1))
  49  	}
  50  	if p.Trit(2) != 2 {
  51  		t.Errorf("conjunction trit[2]: got %d, want 2", p.Trit(2))
  52  	}
  53  }
  54  
  55  func TestPOSToTritPath(t *testing.T) {
  56  	tests := []struct {
  57  		pos    POS
  58  		branch uint8
  59  		depth  int
  60  	}{
  61  		{POSNoun, BranchType, 2},
  62  		{POSProperNoun, BranchType, 2},
  63  		{POSPronoun, BranchType, 2},
  64  		{POSVerb, BranchFunc, 2},
  65  		{POSAuxiliary, BranchFunc, 2},
  66  		{POSAdjective, BranchData, 2},
  67  		{POSAdverb, BranchData, 2},
  68  		{POSPreposition, BranchData, 3},
  69  		{POSConjunction, BranchData, 3},
  70  		{POSUnknown, TritEnd, 0},
  71  	}
  72  	for _, tt := range tests {
  73  		p := POSToTritPath(tt.pos)
  74  		if p.Branch() != tt.branch {
  75  			t.Errorf("POS %d: branch got %d, want %d", tt.pos, p.Branch(), tt.branch)
  76  		}
  77  		if p.Depth() != tt.depth {
  78  			t.Errorf("POS %d: depth got %d, want %d", tt.pos, p.Depth(), tt.depth)
  79  		}
  80  	}
  81  }
  82  
  83  func TestMetaEntryTritPathRoundtrip(t *testing.T) {
  84  	var m MetaEntry
  85  	p := MakeTritPath(BranchData, 3, 2)
  86  	m.SetTritPath(p)
  87  	got := m.TritPath()
  88  	if got != p {
  89  		t.Errorf("roundtrip: got %08x, want %08x", uint32(got), uint32(p))
  90  	}
  91  }
  92  
  93  func TestZeroExtraMeansUnassigned(t *testing.T) {
  94  	var m MetaEntry // zero-initialized
  95  	p := m.TritPath()
  96  	if p.Depth() != 0 {
  97  		t.Errorf("zero Extra depth: got %d, want 0", p.Depth())
  98  	}
  99  	if p.Branch() != TritEnd {
 100  		t.Errorf("zero Extra branch: got %d, want %d (TritEnd)", p.Branch(), TritEnd)
 101  	}
 102  }
 103