package search import ( "git.fuyu.moe/Fuyu/assert" "strings" "testing" ) func TestAdd(t *testing.T) { var DB Search as := assert.New(t) for i := 0; i < 50; i++ { DB.Add(i, [][]string{[]string{"test"}, []string{"another", "test"}, []string{"third", "one"}}) } as.Eq("test", DB.Words[0][0][0]) as.Contains("another", DB.Words[0][1]) as.Eq(50, len(DB.Words)) } func TestMatch(t *testing.T) { var DB Search as := assert.New(t) DB.Add(2, [][]string{[]string{"test"}, []string{"another", "test"}, []string{"third", "one"}}) DB.Add(1, [][]string{[]string{"test"}, []string{"another", "test"}, []string{"third", "test"}}) res := DB.Match("third") as.Eq(2, res[0]) as.Eq(2, len(res)) } func TestCleanSearch(t *testing.T) { var DB Search as := assert.New(t) as.SameElements(DB.CleanSearch("Title of a anime in here"), []string{`title`, `of`, `a`, `anime`, `in`, `here`}) } func TestIgnore(t *testing.T) { var DB Search as := assert.New(t) expectedIgnored := []string{`a`, `of`, `in`, `the`, `wa`, `ga`, `no`, `ni`, `wo`, `he`, `o`, `ka`} DB.Ignore(expectedIgnored) as.SameElements(expectedIgnored, DB.IgnoreWords) as.SameElements(DB.CleanSearch("Title of a anime in here"), []string{`title`, `anime`, `here`}) } func TestReplace(t *testing.T) { var DB Search as := assert.New(t) expectedReplace := strings.Split(`〜☆♪・〈〉「」!『』²Ⅱ+[](),.!?\/{}+-_=~"'@#$%^&*|;:<>`, ``) DB.Replace(expectedReplace) as.SameElements(expectedReplace, DB.RemoveCharacters) as.SameElements(DB.CleanSearch("Ti☆tle〜 of☆a anime² in♪ 「here」"), []string{`ti`, `tle`, `of`, `a`, `anime`, `in`, `here`}) } func BenchmarkMatch(b *testing.B) { var DB Search for i := 0; i < 10000; i++ { DB.Add(i, [][]string{[]string{string(i)}, []string{"another", "test" + string(i)}, []string{"third", "test"}}) } for n := 0; n < b.N; n++ { DB.Match(`5`) } }