Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / github.com / sergi / go-diff@v1.1.0 / diffmatchpatch / match_test.go
1 // Copyright (c) 2012-2016 The go-diff authors. All rights reserved.
2 // https://github.com/sergi/go-diff
3 // See the included LICENSE file for license details.
4 //
5 // go-diff is a Go implementation of Google's Diff, Match, and Patch library
6 // Original library is Copyright (c) 2006 Google Inc.
7 // http://code.google.com/p/google-diff-match-patch/
8
9 package diffmatchpatch
10
11 import (
12         "fmt"
13         "testing"
14
15         "github.com/stretchr/testify/assert"
16 )
17
18 func TestMatchAlphabet(t *testing.T) {
19         type TestCase struct {
20                 Pattern string
21
22                 Expected map[byte]int
23         }
24
25         dmp := New()
26
27         for i, tc := range []TestCase{
28                 {
29                         Pattern: "abc",
30
31                         Expected: map[byte]int{
32                                 'a': 4,
33                                 'b': 2,
34                                 'c': 1,
35                         },
36                 },
37                 {
38                         Pattern: "abcaba",
39
40                         Expected: map[byte]int{
41                                 'a': 37,
42                                 'b': 18,
43                                 'c': 8,
44                         },
45                 },
46         } {
47                 actual := dmp.MatchAlphabet(tc.Pattern)
48                 assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc))
49         }
50 }
51
52 func TestMatchBitap(t *testing.T) {
53         type TestCase struct {
54                 Name string
55
56                 Text     string
57                 Pattern  string
58                 Location int
59
60                 Expected int
61         }
62
63         dmp := New()
64         dmp.MatchDistance = 100
65         dmp.MatchThreshold = 0.5
66
67         for i, tc := range []TestCase{
68                 {"Exact match #1", "abcdefghijk", "fgh", 5, 5},
69                 {"Exact match #2", "abcdefghijk", "fgh", 0, 5},
70                 {"Fuzzy match #1", "abcdefghijk", "efxhi", 0, 4},
71                 {"Fuzzy match #2", "abcdefghijk", "cdefxyhijk", 5, 2},
72                 {"Fuzzy match #3", "abcdefghijk", "bxy", 1, -1},
73                 {"Overflow", "123456789xx0", "3456789x0", 2, 2},
74                 {"Before start match", "abcdef", "xxabc", 4, 0},
75                 {"Beyond end match", "abcdef", "defyy", 4, 3},
76                 {"Oversized pattern", "abcdef", "xabcdefy", 0, 0},
77         } {
78                 actual := dmp.MatchBitap(tc.Text, tc.Pattern, tc.Location)
79                 assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
80         }
81
82         dmp.MatchThreshold = 0.4
83
84         for i, tc := range []TestCase{
85                 {"Threshold #1", "abcdefghijk", "efxyhi", 1, 4},
86         } {
87                 actual := dmp.MatchBitap(tc.Text, tc.Pattern, tc.Location)
88                 assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
89         }
90
91         dmp.MatchThreshold = 0.3
92
93         for i, tc := range []TestCase{
94                 {"Threshold #2", "abcdefghijk", "efxyhi", 1, -1},
95         } {
96                 actual := dmp.MatchBitap(tc.Text, tc.Pattern, tc.Location)
97                 assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
98         }
99
100         dmp.MatchThreshold = 0.0
101
102         for i, tc := range []TestCase{
103                 {"Threshold #3", "abcdefghijk", "bcdef", 1, 1},
104         } {
105                 actual := dmp.MatchBitap(tc.Text, tc.Pattern, tc.Location)
106                 assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
107         }
108
109         dmp.MatchThreshold = 0.5
110
111         for i, tc := range []TestCase{
112                 {"Multiple select #1", "abcdexyzabcde", "abccde", 3, 0},
113                 {"Multiple select #2", "abcdexyzabcde", "abccde", 5, 8},
114         } {
115                 actual := dmp.MatchBitap(tc.Text, tc.Pattern, tc.Location)
116                 assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
117         }
118
119         // Strict location.
120         dmp.MatchDistance = 10
121
122         for i, tc := range []TestCase{
123                 {"Distance test #1", "abcdefghijklmnopqrstuvwxyz", "abcdefg", 24, -1},
124                 {"Distance test #2", "abcdefghijklmnopqrstuvwxyz", "abcdxxefg", 1, 0},
125         } {
126                 actual := dmp.MatchBitap(tc.Text, tc.Pattern, tc.Location)
127                 assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
128         }
129
130         // Loose location.
131         dmp.MatchDistance = 1000
132
133         for i, tc := range []TestCase{
134                 {"Distance test #3", "abcdefghijklmnopqrstuvwxyz", "abcdefg", 24, 0},
135         } {
136                 actual := dmp.MatchBitap(tc.Text, tc.Pattern, tc.Location)
137                 assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
138         }
139 }
140
141 func TestMatchMain(t *testing.T) {
142         type TestCase struct {
143                 Name string
144
145                 Text1    string
146                 Text2    string
147                 Location int
148
149                 Expected int
150         }
151
152         dmp := New()
153
154         for i, tc := range []TestCase{
155                 {"Equality", "abcdef", "abcdef", 1000, 0},
156                 {"Null text", "", "abcdef", 1, -1},
157                 {"Null pattern", "abcdef", "", 3, 3},
158                 {"Exact match", "abcdef", "de", 3, 3},
159                 {"Beyond end match", "abcdef", "defy", 4, 3},
160                 {"Oversized pattern", "abcdef", "abcdefy", 0, 0},
161         } {
162                 actual := dmp.MatchMain(tc.Text1, tc.Text2, tc.Location)
163                 assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
164         }
165
166         dmp.MatchThreshold = 0.7
167
168         for i, tc := range []TestCase{
169                 {"Complex match", "I am the very model of a modern major general.", " that berry ", 5, 4},
170         } {
171                 actual := dmp.MatchMain(tc.Text1, tc.Text2, tc.Location)
172                 assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc))
173         }
174 }