Debian Codegraph @

prometheus/internal/difflib_test.go

raw
1 // Copyright 2022 The Prometheus Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 package internal
14
15 import (
16 "bytes"
17 "fmt"
18 "math"
19 "reflect"
20 "strings"
21 "testing"
22 )
23
24 func assertAlmostEqual(t *testing.T, a, b float64, places int) {
25 if math.Abs(a-b) > math.Pow10(-places) {
26 t.Errorf("%.7f != %.7f", a, b)
27 }
28 }
29
30 func assertEqual(t *testing.T, a, b interface{}) {
31 if !reflect.DeepEqual(a, b) {
32 t.Errorf("%v != %v", a, b)
33 }
34 }
35
36 func splitChars(s string) []string {
37 chars := make([]string, 0, len(s))
38 // Assume ASCII inputs
39 for i := 0; i != len(s); i++ {
40 chars = append(chars, string(s[i]))
41 }
42 return chars
43 }
44
45 func TestSequenceMatcherRatio(t *testing.T) {
46 s := NewMatcher(splitChars("abcd"), splitChars("bcde"))
47 assertEqual(t, s.Ratio(), 0.75)
48 assertEqual(t, s.QuickRatio(), 0.75)
49 assertEqual(t, s.RealQuickRatio(), 1.0)
50 }
51
52 func TestGetOptCodes(t *testing.T) {
53 a := "qabxcd"
54 b := "abycdf"
55 s := NewMatcher(splitChars(a), splitChars(b))
56 w := &bytes.Buffer{}
57 for _, op := range s.GetOpCodes() {
58 fmt.Fprintf(w, "%s a[%d:%d], (%s) b[%d:%d] (%s)\n", string(op.Tag),
59 op.I1, op.I2, a[op.I1:op.I2], op.J1, op.J2, b[op.J1:op.J2])
60 }
61 result := w.String()
62 expected := `d a[0:1], (q) b[0:0] ()
63 e a[1:3], (ab) b[0:2] (ab)
64 r a[3:4], (x) b[2:3] (y)
65 e a[4:6], (cd) b[3:5] (cd)
66 i a[6:6], () b[5:6] (f)
67 `
68 if expected != result {
69 t.Errorf("unexpected op codes: \n%s", result)
70 }
71 }
72
73 func TestGroupedOpCodes(t *testing.T) {
74 a := []string{}
75 for i := 0; i != 39; i++ {
76 a = append(a, fmt.Sprintf("%02d", i))
77 }
78 b := []string{}
79 b = append(b, a[:8]...)
80 b = append(b, " i")
81 b = append(b, a[8:19]...)
82 b = append(b, " x")
83 b = append(b, a[20:22]...)
84 b = append(b, a[27:34]...)
85 b = append(b, " y")
86 b = append(b, a[35:]...)
87 s := NewMatcher(a, b)
88 w := &bytes.Buffer{}
89 for _, g := range s.GetGroupedOpCodes(-1) {
90 fmt.Fprintf(w, "group\n")
91 for _, op := range g {
92 fmt.Fprintf(w, " %s, %d, %d, %d, %d\n", string(op.Tag),
93 op.I1, op.I2, op.J1, op.J2)
94 }
95 }
96 result := w.String()
97 expected := `group
98 e, 5, 8, 5, 8
99 i, 8, 8, 8, 9
100 e, 8, 11, 9, 12
101 group
102 e, 16, 19, 17, 20
103 r, 19, 20, 20, 21
104 e, 20, 22, 21, 23
105 d, 22, 27, 23, 23
106 e, 27, 30, 23, 26
107 group
108 e, 31, 34, 27, 30
109 r, 34, 35, 30, 31
110 e, 35, 38, 31, 34
111 `
112 if expected != result {
113 t.Errorf("unexpected op codes: \n%s", result)
114 }
115 }
116
117 func ExampleGetUnifiedDiffString() {
118 a := `one
119 two
120 three
121 four
122 fmt.Printf("%s,%T",a,b)`
123 b := `zero
124 one
125 three
126 four`
127 diff := UnifiedDiff{
128 A: SplitLines(a),
129 B: SplitLines(b),
130 FromFile: "Original",
131 FromDate: "2005-01-26 23:30:50",
132 ToFile: "Current",
133 ToDate: "2010-04-02 10:20:52",
134 Context: 3,
135 }
136 result, _ := GetUnifiedDiffString(diff)
137 fmt.Println(strings.ReplaceAll(result, "\t", " "))
138 // Output:
139 // --- Original 2005-01-26 23:30:50
140 // +++ Current 2010-04-02 10:20:52
141 // @@ -1,5 +1,4 @@
142 // +zero
143 // one
144 // -two
145 // three
146 // four
147 // -fmt.Printf("%s,%T",a,b)
148 }
149
150 func rep(s string, count int) string {
151 return strings.Repeat(s, count)
152 }
153
154 func TestWithAsciiOneInsert(t *testing.T) {
155 sm := NewMatcher(splitChars(rep("b", 100)),
156 splitChars("a"+rep("b", 100)))
157 assertAlmostEqual(t, sm.Ratio(), 0.995, 3)
158 assertEqual(t, sm.GetOpCodes(),
159 []OpCode{{'i', 0, 0, 0, 1}, {'e', 0, 100, 1, 101}})
160 assertEqual(t, len(sm.bPopular), 0)
161
162 sm = NewMatcher(splitChars(rep("b", 100)),
163 splitChars(rep("b", 50)+"a"+rep("b", 50)))
164 assertAlmostEqual(t, sm.Ratio(), 0.995, 3)
165 assertEqual(t, sm.GetOpCodes(),
166 []OpCode{{'e', 0, 50, 0, 50}, {'i', 50, 50, 50, 51}, {'e', 50, 100, 51, 101}})
167 assertEqual(t, len(sm.bPopular), 0)
168 }
169
170 func TestWithAsciiOnDelete(t *testing.T) {
171 sm := NewMatcher(splitChars(rep("a", 40)+"c"+rep("b", 40)),
172 splitChars(rep("a", 40)+rep("b", 40)))
173 assertAlmostEqual(t, sm.Ratio(), 0.994, 3)
174 assertEqual(t, sm.GetOpCodes(),
175 []OpCode{{'e', 0, 40, 0, 40}, {'d', 40, 41, 40, 40}, {'e', 41, 81, 40, 80}})
176 }
177
178 func TestWithAsciiBJunk(t *testing.T) {
179 isJunk := func(s string) bool {
180 return s == " "
181 }
182 sm := NewMatcherWithJunk(splitChars(rep("a", 40)+rep("b", 40)),
183 splitChars(rep("a", 44)+rep("b", 40)), true, isJunk)
184 assertEqual(t, sm.bJunk, map[string]struct{}{})
185
186 sm = NewMatcherWithJunk(splitChars(rep("a", 40)+rep("b", 40)),
187 splitChars(rep("a", 44)+rep("b", 40)+rep(" ", 20)), false, isJunk)
188 assertEqual(t, sm.bJunk, map[string]struct{}{" ": {}})
189
190 isJunk = func(s string) bool {
191 return s == " " || s == "b"
192 }
193 sm = NewMatcherWithJunk(splitChars(rep("a", 40)+rep("b", 40)),
194 splitChars(rep("a", 44)+rep("b", 40)+rep(" ", 20)), false, isJunk)
195 assertEqual(t, sm.bJunk, map[string]struct{}{" ": {}, "b": {}})
196 }
197
198 func TestSFBugsRatioForNullSeqn(t *testing.T) {
199 sm := NewMatcher(nil, nil)
200 assertEqual(t, sm.Ratio(), 1.0)
201 assertEqual(t, sm.QuickRatio(), 1.0)
202 assertEqual(t, sm.RealQuickRatio(), 1.0)
203 }
204
205 func TestSFBugsComparingEmptyLists(t *testing.T) {
206 groups := NewMatcher(nil, nil).GetGroupedOpCodes(-1)
207 assertEqual(t, len(groups), 0)
208 diff := UnifiedDiff{
209 FromFile: "Original",
210 ToFile: "Current",
211 Context: 3,
212 }
213 result, err := GetUnifiedDiffString(diff)
214 assertEqual(t, err, nil)
215 assertEqual(t, result, "")
216 }
217
218 func TestOutputFormatRangeFormatUnified(t *testing.T) {
219 // Per the diff spec at http://www.unix.org/single_unix_specification/
220 //
221 // Each <range> field shall be of the form:
222 // %1d", <beginning line number> if the range contains exactly one line,
223 // and:
224 // "%1d,%1d", <beginning line number>, <number of lines> otherwise.
225 // If a range is empty, its beginning line number shall be the number of
226 // the line just before the range, or 0 if the empty range starts the file.
227 fm := formatRangeUnified
228 assertEqual(t, fm(3, 3), "3,0")
229 assertEqual(t, fm(3, 4), "4")
230 assertEqual(t, fm(3, 5), "4,2")
231 assertEqual(t, fm(3, 6), "4,3")
232 assertEqual(t, fm(0, 0), "0,0")
233 }
234
235 func TestSplitLines(t *testing.T) {
236 allTests := []struct {
237 input string
238 want []string
239 }{
240 {"foo", []string{"foo\n"}},
241 {"foo\nbar", []string{"foo\n", "bar\n"}},
242 {"foo\nbar\n", []string{"foo\n", "bar\n", "\n"}},
243 }
244 for _, test := range allTests {
245 assertEqual(t, SplitLines(test.input), test.want)
246 }
247 }
248
249 func benchmarkSplitLines(b *testing.B, count int) {
250 str := strings.Repeat("foo\n", count)
251
252 b.ResetTimer()
253
254 n := 0
255 for i := 0; i < b.N; i++ {
256 n += len(SplitLines(str))
257 }
258 }
259
260 func BenchmarkSplitLines100(b *testing.B) {
261 benchmarkSplitLines(b, 100)
262 }
263
264 func BenchmarkSplitLines10000(b *testing.B) {
265 benchmarkSplitLines(b, 10000)
266 }