bigram_test.go raw

   1  package lattice
   2  
   3  import "testing"
   4  
   5  func TestBigramKeyDeterministic(t *testing.T) {
   6  	k1 := BigramKey(LangEN, []byte("going"), []byte("home"))
   7  	k2 := BigramKey(LangEN, []byte("going"), []byte("home"))
   8  	if k1 != k2 {
   9  		t.Errorf("BigramKey not deterministic: %x != %x", k1, k2)
  10  	}
  11  }
  12  
  13  func TestBigramKeyDifferentPairs(t *testing.T) {
  14  	k1 := BigramKey(LangEN, []byte("going"), []byte("home"))
  15  	k2 := BigramKey(LangEN, []byte("going"), []byte("house"))
  16  	if k1 == k2 {
  17  		t.Errorf("different pairs produced same key: %x", k1)
  18  	}
  19  }
  20  
  21  func TestBigramKeyDifferentLanguages(t *testing.T) {
  22  	k1 := BigramKey(LangEN, []byte("going"), []byte("home"))
  23  	k2 := BigramKey(LangBG, []byte("going"), []byte("home"))
  24  	if k1 == k2 {
  25  		t.Errorf("different languages produced same key: %x", k1)
  26  	}
  27  }
  28  
  29  func TestBigramKeyLanguageTag(t *testing.T) {
  30  	k := BigramKey(LangEN, []byte("going"), []byte("home"))
  31  	if KeyLang(k) != LangEN {
  32  		t.Errorf("BigramKey lang = %d, want %d", KeyLang(k), LangEN)
  33  	}
  34  
  35  	k2 := BigramKey(LangBG, []byte("отивам"), []byte("вкъщи"))
  36  	if KeyLang(k2) != LangBG {
  37  		t.Errorf("BigramKey lang = %d, want %d", KeyLang(k2), LangBG)
  38  	}
  39  }
  40  
  41  func TestBigramKeyOrderMatters(t *testing.T) {
  42  	k1 := BigramKey(LangEN, []byte("big"), []byte("house"))
  43  	k2 := BigramKey(LangEN, []byte("house"), []byte("big"))
  44  	if k1 == k2 {
  45  		t.Errorf("reversed pairs should have different keys: %x", k1)
  46  	}
  47  }
  48  
  49  func TestBigramKeyInsertAndLookup(t *testing.T) {
  50  	tree := NewTree(100)
  51  	k := BigramKey(LangEN, []byte("going"), []byte("home"))
  52  	insertWord(t, tree, Bcooccur, k)
  53  
  54  	_, _, found, err := tree.Walk(Bcooccur, k)
  55  	if err != nil || !found {
  56  		t.Errorf("bigram key not found after insert (%v)", err)
  57  	}
  58  }
  59