func TestMarks(tt *testing.T) {
	t := trie.NewTrie()
	trie.AddWord(t, "hello")
	trie.AddWord(t, "goodbye")
	trie.AddWord(t, "goodday")

	m := trie.DescendPath(t, "hello")
	m.Mark(10)
	if m.GetMark() != 10 {
		tt.Fail()
	}
}
func TestReverseLookup(tt *testing.T) {
	t := trie.NewTrie()
	trie.AddWord(t, "hello")
	trie.AddWord(t, "goodbye")
	trie.AddWord(t, "goodday")

	m := trie.DescendPath(t, "hello")
	rev := trie.ReverseLookup(t, m)
	if rev != "hello" {
		tt.Fail()
	}
}
func TestStartsWordDescend(tt *testing.T) {
	t := trie.NewTrie()
	trie.AddWord(t, "hello")
	trie.AddWord(t, "goodbye")
	trie.AddWord(t, "goodday")

	if !t.StartsWord('h' - 'a') {
		tt.Fail()
	}
	if !t.StartsWord('g' - 'a') {
		tt.Fail()
	}
	if t.StartsWord('x' - 'a') {
		tt.Fail()
	}

	th := t.Descend('h' - 'a')
	if th == nil {
		tt.Fail()
	}
	if th.IsWord() {
		tt.Fail()
	}
	to := th.Descend('e' - 'a').Descend('l' - 'a').Descend('l' - 'a').Descend('o' - 'a')
	if to == nil {
		tt.Fail()
	}
	if !to.IsWord() {
		tt.Fail()
	}

	t_hell := trie.DescendPath(t, "hell")
	if t_hell == nil {
		tt.Fail()
	}
	if t_hell.IsWord() {
		tt.Fail()
	}

	// this test fails, oddly, even though fmt.Println(t_hell.Descend('h' - 'a')) prints '<nil>'
	if t_hell.Descend('h'-'a') != nil {
		tt.Fail()
	}

	t_hello := t_hell.Descend('o' - 'a')
	if !t_hello.IsWord() {
		tt.Fail()
	}
}
func noTestAddWordIsWord(tt *testing.T) {
	t := trie.NewTrie()
	trie.AddWord(t, "hello")
	trie.AddWord(t, "goodbye")
	trie.AddWord(t, "goodday")

	if !trie.IsWord(t, "hello") {
		tt.Fail()
	}
	if !trie.IsWord(t, "goodbye") {
		tt.Fail()
	}
	if !trie.IsWord(t, "goodday") {
		tt.Fail()
	}
	if trie.IsWord(t, "h") {
		tt.Fail()
	}
	if trie.IsWord(t, "he") {
		tt.Fail()
	}
	if trie.IsWord(t, "hel") {
		tt.Fail()
	}
	if trie.IsWord(t, "hell") {
		tt.Fail()
	}
	if trie.IsWord(t, "goodby") {
		tt.Fail()
	}
	if trie.IsWord(t, "goodda") {
		tt.Fail()
	}
	if trie.IsWord(t, "z") {
		tt.Fail()
	}
}
Exemple #5
0
func boggleTrieFromFilename(filename string) *trie.Trie {
	f, err := os.Open(filename)
	if err != nil {
		fmt.Println(err)
		return nil
	}
	defer f.Close()

	r, err := bufio.NewReaderSize(f, 4*1024)
	if err != nil {
		fmt.Println(err)
		return nil
	}

	t := trie.NewTrie()
	for {
		line, isPrefix, err := r.ReadLine()
		if err == os.EOF {
			break
		}
		if isPrefix {
			fmt.Println("need more buffering!")
			return nil
		}
		if err != nil {
			fmt.Println("error! ", err)
			return nil
		}

		word := string(line)
		ok, boggleword := BogglifyWord(word)
		if ok {
			trie.AddWord(t, boggleword)
		}
	}

	return t
}