Debian Codegraph @

testutil/internal/difflib_test.go

raw symbols SCIP
1 // Copyright (c) The EfficientGo Authors.
2 // Licensed under the Apache License 2.0.
3
4 package internal
5
6 import (
7 "bytes"
8 "fmt"
9 "math"
10 "reflect"
11 "strings"
12 "testing"
13 )
14
15 func assertAlmostEqual(t *testing.T, a, b float64, places int) {
16 if math.Abs(a-b) > math.Pow10(-places) {
17 t.Errorf("%.7f != %.7f", a, b)
18 }
19 }
20
21 func assertEqual(t *testing.T, a, b interface{}) {
22 if !reflect.DeepEqual(a, b) {
23 t.Errorf("%v != %v", a, b)
24 }
25 }
26
27 func splitChars(s string) []string {
28 chars := make([]string, 0, len(s))
29 // Assume ASCII inputs
30 for i := 0; i != len(s); i++ {
31 chars = append(chars, string(s[i]))
32 }
33 return chars
34 }
35
36 func TestSequenceMatcherRatio(t *testing.T) {
37 s := NewMatcher(splitChars("abcd"), splitChars("bcde"))
38 assertEqual(t, s.Ratio(), 0.75)
39 assertEqual(t, s.QuickRatio(), 0.75)
40 assertEqual(t, s.RealQuickRatio(), 1.0)
41 }
42
43 func TestGetOptCodes(t *testing.T) {
44 a := "qabxcd"
45 b := "abycdf"
46 s := NewMatcher(splitChars(a), splitChars(b))
47 w := &bytes.Buffer{}
48 for _, op := range s.GetOpCodes() {
49 fmt.Fprintf(w, "%s a[%d:%d], (%s) b[%d:%d] (%s)\n", string(op.Tag),
50 op.I1, op.I2, a[op.I1:op.I2], op.J1, op.J2, b[op.J1:op.J2])
51 }
52 result := w.String()
53 expected := `d a[0:1], (q) b[0:0] ()
54 e a[1:3], (ab) b[0:2] (ab)
55 r a[3:4], (x) b[2:3] (y)
56 e a[4:6], (cd) b[3:5] (cd)
57 i a[6:6], () b[5:6] (f)
58 `
59 if expected != result {
60 t.Errorf("unexpected op codes: \n%s", result)
61 }
62 }
63
64 func TestGroupedOpCodes(t *testing.T) {
65 a := []string{}
66 for i := 0; i != 39; i++ {
67 a = append(a, fmt.Sprintf("%02d", i))
68 }
69 b := []string{}
70 b = append(b, a[:8]...)
71 b = append(b, " i")
72 b = append(b, a[8:19]...)
73 b = append(b, " x")
74 b = append(b, a[20:22]...)
75 b = append(b, a[27:34]...)
76 b = append(b, " y")
77 b = append(b, a[35:]...)
78 s := NewMatcher(a, b)
79 w := &bytes.Buffer{}
80 for _, g := range s.GetGroupedOpCodes(-1) {
81 fmt.Fprintf(w, "group\n")
82 for _, op := range g {
83 fmt.Fprintf(w, " %s, %d, %d, %d, %d\n", string(op.Tag),
84 op.I1, op.I2, op.J1, op.J2)
85 }
86 }
87 result := w.String()
88 expected := `group
89 e, 5, 8, 5, 8
90 i, 8, 8, 8, 9
91 e, 8, 11, 9, 12
92 group
93 e, 16, 19, 17, 20
94 r, 19, 20, 20, 21
95 e, 20, 22, 21, 23
96 d, 22, 27, 23, 23
97 e, 27, 30, 23, 26
98 group
99 e, 31, 34, 27, 30
100 r, 34, 35, 30, 31
101 e, 35, 38, 31, 34
102 `
103 if expected != result {
104 t.Errorf("unexpected op codes: \n%s", result)
105 }
106 }
107
108 func rep(s string, count int) string {
109 return strings.Repeat(s, count)
110 }
111
112 func TestWithAsciiOneInsert(t *testing.T) {
113 sm := NewMatcher(splitChars(rep("b", 100)),
114 splitChars("a"+rep("b", 100)))
115 assertAlmostEqual(t, sm.Ratio(), 0.995, 3)
116 assertEqual(t, sm.GetOpCodes(),
117 []OpCode{{'i', 0, 0, 0, 1}, {'e', 0, 100, 1, 101}})
118 assertEqual(t, len(sm.bPopular), 0)
119
120 sm = NewMatcher(splitChars(rep("b", 100)),
121 splitChars(rep("b", 50)+"a"+rep("b", 50)))
122 assertAlmostEqual(t, sm.Ratio(), 0.995, 3)
123 assertEqual(t, sm.GetOpCodes(),
124 []OpCode{{'e', 0, 50, 0, 50}, {'i', 50, 50, 50, 51}, {'e', 50, 100, 51, 101}})
125 assertEqual(t, len(sm.bPopular), 0)
126 }
127
128 func TestWithAsciiOnDelete(t *testing.T) {
129 sm := NewMatcher(splitChars(rep("a", 40)+"c"+rep("b", 40)),
130 splitChars(rep("a", 40)+rep("b", 40)))
131 assertAlmostEqual(t, sm.Ratio(), 0.994, 3)
132 assertEqual(t, sm.GetOpCodes(),
133 []OpCode{{'e', 0, 40, 0, 40}, {'d', 40, 41, 40, 40}, {'e', 41, 81, 40, 80}})
134 }
135
136 func TestWithAsciiBJunk(t *testing.T) {
137 isJunk := func(s string) bool {
138 return s == " "
139 }
140 sm := NewMatcherWithJunk(splitChars(rep("a", 40)+rep("b", 40)),
141 splitChars(rep("a", 44)+rep("b", 40)), true, isJunk)
142 assertEqual(t, sm.bJunk, map[string]struct{}{})
143
144 sm = NewMatcherWithJunk(splitChars(rep("a", 40)+rep("b", 40)),
145 splitChars(rep("a", 44)+rep("b", 40)+rep(" ", 20)), false, isJunk)
146 assertEqual(t, sm.bJunk, map[string]struct{}{" ": {}})
147
148 isJunk = func(s string) bool {
149 return s == " " || s == "b"
150 }
151 sm = NewMatcherWithJunk(splitChars(rep("a", 40)+rep("b", 40)),
152 splitChars(rep("a", 44)+rep("b", 40)+rep(" ", 20)), false, isJunk)
153 assertEqual(t, sm.bJunk, map[string]struct{}{" ": {}, "b": {}})
154 }
155
156 func TestSFBugsRatioForNullSeqn(t *testing.T) {
157 sm := NewMatcher(nil, nil)
158 assertEqual(t, sm.Ratio(), 1.0)
159 assertEqual(t, sm.QuickRatio(), 1.0)
160 assertEqual(t, sm.RealQuickRatio(), 1.0)
161 }
162
163 func TestSFBugsComparingEmptyLists(t *testing.T) {
164 groups := NewMatcher(nil, nil).GetGroupedOpCodes(-1)
165 assertEqual(t, len(groups), 0)
166 diff := UnifiedDiff{
167 FromFile: "Original",
168 ToFile: "Current",
169 Context: 3,
170 }
171 result, err := GetUnifiedDiffString(diff)
172 assertEqual(t, err, nil)
173 assertEqual(t, result, "")
174 }
175
176 func TestOutputFormatRangeFormatUnified(t *testing.T) {
177 // Per the diff spec at http://www.unix.org/single_unix_specification/
178 //
179 // Each <range> field shall be of the form:
180 // %1d", <beginning line number> if the range contains exactly one line,
181 // and:
182 // "%1d,%1d", <beginning line number>, <number of lines> otherwise.
183 // If a range is empty, its beginning line number shall be the number of
184 // the line just before the range, or 0 if the empty range starts the file.
185 fm := formatRangeUnified
186 assertEqual(t, fm(3, 3), "3,0")
187 assertEqual(t, fm(3, 4), "4")
188 assertEqual(t, fm(3, 5), "4,2")
189 assertEqual(t, fm(3, 6), "4,3")
190 assertEqual(t, fm(0, 0), "0,0")
191 }
192
193 func TestSplitLines(t *testing.T) {
194 allTests := []struct {
195 input string
196 want []string
197 }{
198 {"foo", []string{"foo\n"}},
199 {"foo\nbar", []string{"foo\n", "bar\n"}},
200 {"foo\nbar\n", []string{"foo\n", "bar\n", "\n"}},
201 }
202 for _, test := range allTests {
203 assertEqual(t, SplitLines(test.input), test.want)
204 }
205 }
206
207 func benchmarkSplitLines(b *testing.B, count int) {
208 str := strings.Repeat("foo\n", count)
209
210 b.ResetTimer()
211
212 n := 0
213 for i := 0; i < b.N; i++ {
214 n += len(SplitLines(str))
215 }
216 }
217
218 func BenchmarkSplitLines100(b *testing.B) {
219 benchmarkSplitLines(b, 100)
220 }
221
222 func BenchmarkSplitLines10000(b *testing.B) {
223 benchmarkSplitLines(b, 10000)
224 }