github.com/lazin/go-ngram@v0.0.0-20160527144230-80eaf16ac4eb/ngram_test.go (about) 1 package ngram 2 3 import ( 4 "fmt" 5 "testing" 6 ) 7 8 func TestIndexBasics(t *testing.T) { 9 var ng NGramIndex 10 index := &ng 11 id, error := index.Add("hello") 12 if error != nil { 13 t.Error(error) 14 } 15 strval, error := index.GetString(id) 16 if error != nil { 17 t.Error(error) 18 } 19 if strval != "hello" { 20 t.Error("Can't read string from index") 21 } 22 } 23 24 func TestSearching(t *testing.T) { 25 var ng NGramIndex 26 index := &ng 27 _, error := index.Add("hello") 28 if error != nil { 29 t.Error(error) 30 } 31 _, error = index.Add("world") 32 if error != nil { 33 t.Error(error) 34 } 35 results, error := index.Search("hello", 0.0) 36 if error != nil { 37 t.Error(error) 38 } 39 if len(results) != 1 { 40 t.Error("len(results) != 1") 41 } 42 if results[0].Similarity != 1.0 && results[0].TokenID != 0 { 43 t.Error("Bad result") 44 } 45 results, error = index.Search("12345") 46 if len(results) != 0 { 47 t.Error("Invalid value found") 48 } 49 result, error := index.BestMatch("hel") 50 if error != nil { 51 t.Error(error) 52 } 53 if result.TokenID != 0 { 54 t.Error("BestMatch doesn't work as expected") 55 } 56 } 57 58 func TestIndexInitialization(t *testing.T) { 59 index, error := NewNGramIndex() 60 if error != nil { 61 t.Error(error) 62 } 63 if index.n != defaultN { 64 t.Error("n is not set to default value") 65 } 66 if index.pad != defaultPad { 67 t.Error("pad is not set to default value") 68 } 69 index, error = NewNGramIndex(SetN(4)) 70 if error != nil { 71 t.Error(error) 72 } 73 if index.n != 4 { 74 t.Error("n is not set to 4") 75 } 76 index, error = NewNGramIndex(SetPad('@')) 77 if error != nil { 78 t.Error(error) 79 } 80 if index.pad != "@" { 81 t.Error("pad is not set to @") 82 } 83 // check off limits 84 index, error = NewNGramIndex(SetN(1)) 85 if error == nil { 86 t.Error("Error not set (1)") 87 } 88 index, error = NewNGramIndex(SetN(maxN + 1)) 89 if error == nil { 90 t.Error("Error not set (2)") 91 } 92 } 93 94 func BenchmarkAdd(b *testing.B) { 95 b.StopTimer() 96 // init 97 index, _ := NewNGramIndex() 98 var arr []string 99 for i := 0; i < 10000; i++ { 100 str := fmt.Sprintf("%x", i) 101 arr = append(arr, str) 102 } 103 b.StartTimer() 104 for _, hexstr := range arr { 105 index.Add(hexstr) 106 } 107 } 108 109 func BenchmarkSearch(b *testing.B) { 110 b.StopTimer() 111 // init 112 index, _ := NewNGramIndex() 113 var arr []string 114 for i := 0; i < 10000; i++ { 115 str := fmt.Sprintf("%000x", i) 116 arr = append(arr, str) 117 } 118 for _, hexstr := range arr { 119 index.Add(hexstr) 120 } 121 b.StartTimer() 122 for i := 0; i < 10000; i += 4 { 123 index.Search(arr[i], 0.5) 124 } 125 }