Beispiel #1
0
func TestTrie(t *testing.T) {
	words := []string{"abfg", "acfg", "adfg"}
	missings := []string{"", "foo", "été", "adfgg", "adf"}

	trie, err := gtrie.Create(words)
	if err != nil {
		log.Fatal(err)
	} else if trie == nil {
		log.Fatal("returned trie was nil")
	}

	// Ensure that stored words are accepted.
	for _, word := range words {
		if !trie.Accepts(word) {
			t.Errorf("expected %s to be accepted", word)
		}
	}

	// Ensure that missings words aren't accepted.
	for _, word := range missings {
		if trie.Accepts(word) {
			t.Errorf("expected %s to be rejected", word)
		}
	}

	// Ensure that the graph is minimal by counting the number of nodes.
	size := gtrie.Size(trie)
	if size != 5 {
		t.Errorf("expected size of 5 but got %s", size)
	}
}
Beispiel #2
0
func TestCreateUnsortedWords(t *testing.T) {
	_, err := gtrie.Create([]string{"ab", "ef", "cd"})

	if err == nil {
		t.Errorf("expected error when creating trie from unsorted words")
	}
}
Beispiel #3
0
func BenchmarkAccepts(b *testing.B) {
	b.StopTimer()

	words := []string{"abaissassions", "abaisserions", "abandonnassions"}
	trie, err := gtrie.Create(readWords("words.txt"))
	if err != nil {
		log.Fatal(err)
	}

	b.StartTimer()

	for i := 0; i < b.N; i++ {
		for _, word := range words {
			if !trie.Accepts(word) {
				log.Fatal(word)
			}
		}
	}
}