-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimplementTrie_test.go
42 lines (39 loc) · 1.05 KB
/
implementTrie_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package demo
import (
"reflect"
"testing"
)
func TestTrie_Search(t *testing.T) {
root := NewTrie()
word := "ab"
if e := root.Search(word); e {
t.Errorf(" %s word should not exist \n", word)
}
if e := root.StartWith(word[:1]); e {
t.Errorf(" %s prfix should not exist \n", word[:1])
}
root.Insert(word)
if e := root.Search(word); !e {
t.Errorf(" %s word should exist \n", word)
}
if e := root.StartWith(word[:1]); !e {
t.Errorf(" %s prfix should exist \n", word[:1])
}
if e := root.StartWith(word); !e {
t.Errorf(" %s prefix should exist \n", word)
}
word = "abc"
if e := root.Search(word); e {
t.Errorf(" %s word should not exist \n", word)
}
root.Insert(word)
if e := root.Search(word); !e {
t.Errorf(" %s word should exist \n", word)
}
if ws := root.GetAllWords(); !reflect.DeepEqual(ws, []string{"ab", "abc"}) {
t.Errorf(" %s words not match %s \n", ws, []string{"ab", "abc"})
}
if ws := root.GetWordsStartWith(word[:2]); !reflect.DeepEqual(ws, []string{word}) {
t.Errorf(" %s words not match %s \n", ws, []string{word})
}
}