github.com/serversong/goreporter@v0.0.0-20200325104552-3cfaf44fd178/linters/copycheck/suffixtree/dupl_test.go (about) 1 package suffixtree 2 3 import ( 4 "fmt" 5 "sort" 6 "testing" 7 ) 8 9 func (m Match) String() string { 10 str := "([" 11 for _, p := range m.Ps { 12 str += fmt.Sprintf("%d, ", p) 13 } 14 return str[:len(str)-2] + fmt.Sprintf("], %d)", m.Len) 15 } 16 17 func sliceCmp(sl1, sl2 []Pos) bool { 18 if len(sl1) != len(sl2) { 19 return false 20 } 21 sort.Sort(ByPos(sl1)) 22 sort.Sort(ByPos(sl2)) 23 for i := range sl1 { 24 if sl1[i] != sl2[i] { 25 return false 26 } 27 } 28 return true 29 } 30 31 type ByPos []Pos 32 33 func (p ByPos) Len() int { 34 return len(p) 35 } 36 37 func (p ByPos) Swap(i, j int) { 38 p[i], p[j] = p[j], p[i] 39 } 40 41 func (p ByPos) Less(i, j int) bool { 42 return p[i] < p[j] 43 } 44 45 func TestFindingDupl(t *testing.T) { 46 testCases := []struct { 47 s string 48 threshold int 49 matches []Match 50 }{ 51 {"abab$", 3, []Match{}}, 52 {"abab$", 2, []Match{{[]Pos{0, 2}, 2}}}, 53 {"abcbcabc$", 3, []Match{{[]Pos{0, 5}, 3}}}, 54 {"abcbcabc$", 2, []Match{{[]Pos{0, 5}, 3}, {[]Pos{1, 3, 6}, 2}}}, 55 {`All work and no play makes Jack a dull boy 56 All work and no play makes Jack a dull boy$`, 4, []Match{{[]Pos{0, 43}, 42}}}, 57 } 58 59 for _, tc := range testCases { 60 tree := New() 61 tree.Update(str2tok(tc.s)...) 62 ch := tree.FindDuplOver(tc.threshold) 63 for _, exp := range tc.matches { 64 act, ok := <-ch 65 if !ok { 66 t.Errorf("missing match %v for '%s'", exp, tc.s) 67 } else if exp.Len != act.Len || !sliceCmp(exp.Ps, act.Ps) { 68 t.Errorf("got %v, want %v", act, exp) 69 } 70 } 71 for act := range ch { 72 t.Errorf("beyond expected match %v for '%s'", act, tc.s) 73 } 74 } 75 }