119 lines
2.1 KiB
Go
119 lines
2.1 KiB
Go
package trie
|
|
|
|
import "testing"
|
|
|
|
func TestNewTrie(t *testing.T) {
|
|
tt := NewTrie()
|
|
|
|
if len(tt.children) != 0 {
|
|
t.Log("New trie should be empty")
|
|
t.Fail()
|
|
}
|
|
|
|
if tt.value != nil {
|
|
t.Log("New trie should not have a value")
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
func TestPutAndCount(t *testing.T) {
|
|
tt := NewTrie()
|
|
|
|
if tt.Count() != 0 {
|
|
t.Log("New trie should be empty")
|
|
t.Fail()
|
|
}
|
|
|
|
tt.Put("hello", 1)
|
|
if tt.Count() != 1 {
|
|
t.Log("One element")
|
|
t.Fail()
|
|
}
|
|
|
|
tt.Put("hello", 2)
|
|
if tt.Count() != 1 {
|
|
t.Log("Duplicate element")
|
|
t.Fail()
|
|
}
|
|
|
|
tt.Put("here", 3)
|
|
if tt.Count() != 2 {
|
|
t.Log("Two elements")
|
|
t.Fail()
|
|
}
|
|
|
|
tt.Put("there", 3)
|
|
if tt.Count() != 3 {
|
|
t.Log("Three elements")
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
func TestPutAndGet(t *testing.T) {
|
|
tt := NewTrie()
|
|
|
|
if _, ok := tt.Get("missing"); ok {
|
|
t.Log("New trie should be empty")
|
|
t.Fail()
|
|
}
|
|
|
|
if _, ok := tt.Get(""); ok {
|
|
t.Log("New trie should be empty")
|
|
t.Fail()
|
|
}
|
|
|
|
tt.Put("hello", 1)
|
|
if r, ok := tt.Get("hello"); !ok || r != 1 {
|
|
t.Log("hello")
|
|
t.Fail()
|
|
}
|
|
|
|
tt.Put("help", 2)
|
|
if r, ok := tt.Get("help"); !ok || r != 2 {
|
|
t.Log("help")
|
|
t.Fail()
|
|
}
|
|
|
|
tt.Put("hi", 3)
|
|
if r, ok := tt.Get("hi"); !ok || r != 3 {
|
|
t.Log("hi")
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
func TestPutAndGetLongestPrefix(t *testing.T) {
|
|
tt := NewTrie()
|
|
|
|
if _, value := tt.GetLongestPrefix(""); value != nil {
|
|
t.Log("New trie should be empty")
|
|
t.Fail()
|
|
}
|
|
|
|
tt.Put("integer", 1)
|
|
tt.Put("interval", 2)
|
|
tt.Put("in", 3)
|
|
tt.Put("string", 4)
|
|
tt.Put("struct", 5)
|
|
tt.Put("stop", 6)
|
|
|
|
if prefix, value := tt.GetLongestPrefix("interrupt"); prefix != "in" || value != 3 {
|
|
t.Log("Searching \"interrupt\"", prefix, value)
|
|
t.Fail()
|
|
}
|
|
|
|
if prefix, value := tt.GetLongestPrefix("str"); prefix != "" || value != nil {
|
|
t.Log("Searching \"str\"", prefix, value)
|
|
t.Fail()
|
|
}
|
|
|
|
if prefix, value := tt.GetLongestPrefix("struct"); prefix != "struct" || value != 5 {
|
|
t.Log("Searching \"struct\"", prefix, value)
|
|
t.Fail()
|
|
}
|
|
|
|
if prefix, value := tt.GetLongestPrefix("structure"); prefix != "struct" || value != 5 {
|
|
t.Log("Searching \"structure\"", prefix, value)
|
|
t.Fail()
|
|
}
|
|
}
|