Debian Codegraph @

internal/diff/lcs/old_test.go

raw symbols SCIP
1 // Copyright 2022 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package lcs
6
7 import (
8 "fmt"
9 "log"
10 "math/rand/v2"
11 "os"
12 "strings"
13 "testing"
14 )
15
16 func TestAlgosOld(t *testing.T) {
17 for i, algo := range []func(*editGraph) lcs{forward, backward, twosided} {
18 t.Run(strings.Fields("forward backward twosided")[i], func(t *testing.T) {
19 for _, tx := range Btests {
20 lim := len(tx.a) + len(tx.b)
21
22 diffs, lcs := compute(stringSeqs{tx.a, tx.b}, algo, lim)
23 check(t, tx.a, lcs, tx.lcs)
24 checkDiffs(t, tx.a, diffs, tx.b)
25
26 diffs, lcs = compute(stringSeqs{tx.b, tx.a}, algo, lim)
27 check(t, tx.b, lcs, tx.lcs)
28 checkDiffs(t, tx.b, diffs, tx.a)
29 }
30 })
31 }
32 }
33
34 func TestIntOld(t *testing.T) {
35 // need to avoid any characters in btests
36 lfill, rfill := "AAAAAAAAAAAA", "BBBBBBBBBBBB"
37 for _, tx := range Btests {
38 if len(tx.a) < 2 || len(tx.b) < 2 {
39 continue
40 }
41 left := tx.a + lfill
42 right := tx.b + rfill
43 lim := len(tx.a) + len(tx.b)
44 diffs, lcs := compute(stringSeqs{left, right}, twosided, lim)
45 check(t, left, lcs, tx.lcs)
46 checkDiffs(t, left, diffs, right)
47 diffs, lcs = compute(stringSeqs{right, left}, twosided, lim)
48 check(t, right, lcs, tx.lcs)
49 checkDiffs(t, right, diffs, left)
50
51 left = lfill + tx.a
52 right = rfill + tx.b
53 diffs, lcs = compute(stringSeqs{left, right}, twosided, lim)
54 check(t, left, lcs, tx.lcs)
55 checkDiffs(t, left, diffs, right)
56 diffs, lcs = compute(stringSeqs{right, left}, twosided, lim)
57 check(t, right, lcs, tx.lcs)
58 checkDiffs(t, right, diffs, left)
59 }
60 }
61
62 func TestSpecialOld(t *testing.T) { // exercises lcs.fix
63 a := "golang.org/x/tools/intern"
64 b := "github.com/google/safehtml/template\"\n\t\"golang.org/x/tools/intern"
65 diffs, lcs := compute(stringSeqs{a, b}, twosided, 4)
66 if !lcs.valid() {
67 t.Errorf("%d,%v", len(diffs), lcs)
68 }
69 }
70
71 func TestRegressionOld001(t *testing.T) {
72 a := "// Copyright 2019 The Go Authors. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license that can be found in the LICENSE file.\n\npackage diff_test\n\nimport (\n\t\"fmt\"\n\t\"math/rand\"\n\t\"strings\"\n\t\"testing\"\n\n\t\"golang.org/x/tools/gopls/internal/lsp/diff\"\n\t\"golang.org/x/tools/internal/diff/difftest\"\n\t\"golang.org/x/tools/gopls/internal/span\"\n)\n"
73
74 b := "// Copyright 2019 The Go Authors. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license that can be found in the LICENSE file.\n\npackage diff_test\n\nimport (\n\t\"fmt\"\n\t\"math/rand\"\n\t\"strings\"\n\t\"testing\"\n\n\t\"github.com/google/safehtml/template\"\n\t\"golang.org/x/tools/gopls/internal/lsp/diff\"\n\t\"golang.org/x/tools/internal/diff/difftest\"\n\t\"golang.org/x/tools/gopls/internal/span\"\n)\n"
75 for i := 1; i < len(b); i++ {
76 diffs, lcs := compute(stringSeqs{a, b}, twosided, i) // 14 from gopls
77 if !lcs.valid() {
78 t.Errorf("%d,%v", len(diffs), lcs)
79 }
80 checkDiffs(t, a, diffs, b)
81 }
82 }
83
84 func TestRegressionOld002(t *testing.T) {
85 a := "n\"\n)\n"
86 b := "n\"\n\t\"golang.org/x//nnal/stack\"\n)\n"
87 for i := 1; i <= len(b); i++ {
88 diffs, lcs := compute(stringSeqs{a, b}, twosided, i)
89 if !lcs.valid() {
90 t.Errorf("%d,%v", len(diffs), lcs)
91 }
92 checkDiffs(t, a, diffs, b)
93 }
94 }
95
96 func TestRegressionOld003(t *testing.T) {
97 a := "golang.org/x/hello v1.0.0\nrequire golang.org/x/unused v1"
98 b := "golang.org/x/hello v1"
99 for i := 1; i <= len(a); i++ {
100 diffs, lcs := compute(stringSeqs{a, b}, twosided, i)
101 if !lcs.valid() {
102 t.Errorf("%d,%v", len(diffs), lcs)
103 }
104 checkDiffs(t, a, diffs, b)
105 }
106 }
107
108 func TestRandOld(t *testing.T) {
109 rng := rng(t)
110
111 for i := range 1000 {
112 // TODO(adonovan): use ASCII and bytesSeqs here? The use of
113 // non-ASCII isn't relevant to the property exercised by the test.
114 a := []rune(randstr(rng, "abω", 16))
115 b := []rune(randstr(rng, "abωc", 16))
116 seq := runesSeqs{a, b}
117
118 const lim = 0 // make sure we get the lcs (24 was too small)
119 _, forw := compute(seq, forward, lim)
120 _, back := compute(seq, backward, lim)
121 _, two := compute(seq, twosided, lim)
122 if lcslen(two) != lcslen(forw) || lcslen(forw) != lcslen(back) {
123 t.Logf("\n%v\n%v\n%v", forw, back, two)
124 t.Fatalf("%d forw:%d back:%d two:%d", i, lcslen(forw), lcslen(back), lcslen(two))
125 }
126 if !two.valid() || !forw.valid() || !back.valid() {
127 t.Errorf("check failure")
128 }
129 }
130 }
131
132 // TestDiffAPI tests the public API functions (Diff{Bytes,Strings,Runes})
133 // to ensure at least minimal parity of the three representations.
134 func TestDiffAPI(t *testing.T) {
135 for _, test := range []struct {
136 a, b string
137 wantBytes, wantRunes string
138 }{
139 {"abcXdef", "abcxdef", "[{3 4 3 4}]", "[{3 4 3 4}]"}, // ASCII
140 {"abcωdef", "abcΩdef", "[{3 5 3 5}]", "[{3 4 3 4}]"}, // non-ASCII
141 } {
142
143 gotBytes := fmt.Sprint(DiffBytes([]byte(test.a), []byte(test.b)))
144 if gotBytes != test.wantBytes {
145 t.Errorf("DiffBytes(%q, %q) = %v, want %v",
146 test.a, test.b, gotBytes, test.wantBytes)
147 }
148 gotRunes := fmt.Sprint(DiffRunes([]rune(test.a), []rune(test.b)))
149 if gotRunes != test.wantRunes {
150 t.Errorf("DiffRunes(%q, %q) = %v, want %v",
151 test.a, test.b, gotRunes, test.wantRunes)
152 }
153 }
154 }
155
156 func BenchmarkTwoOld(b *testing.B) {
157 tests := genBench(rng(b), "abc", 96)
158 for b.Loop() {
159 for _, tt := range tests {
160 _, two := compute(stringSeqs{tt.before, tt.after}, twosided, 100)
161 if !two.valid() {
162 b.Error("check failed")
163 }
164 }
165 }
166 }
167
168 func BenchmarkForwOld(b *testing.B) {
169 tests := genBench(rng(b), "abc", 96)
170 for b.Loop() {
171 for _, tt := range tests {
172 _, two := compute(stringSeqs{tt.before, tt.after}, forward, 100)
173 if !two.valid() {
174 b.Error("check failed")
175 }
176 }
177 }
178 }
179
180 // rng returns a randomly initialized PRNG whose seeds are logged so
181 // that occasional test failures can be deterministically replayed.
182 func rng(tb testing.TB) *rand.Rand {
183 seed1, seed2 := rand.Uint64(), rand.Uint64()
184 tb.Logf("PRNG seeds: %d, %d", seed1, seed2)
185 return rand.New(rand.NewPCG(seed1, seed2))
186 }
187
188 func genBench(rng *rand.Rand, set string, n int) []struct{ before, after string } {
189 // before and after for benchmarks. 24 strings of length n with
190 // before and after differing at least once, and about 5%
191 var ans []struct{ before, after string }
192 for range 24 {
193 // maybe b should have an approximately known number of diffs
194 a := randstr(rng, set, n)
195 cnt := 0
196 bb := make([]rune, 0, n)
197 for _, r := range a {
198 if rand.Float64() < .05 {
199 cnt++
200 r = 'N'
201 }
202 bb = append(bb, r)
203 }
204 if cnt == 0 {
205 // avoid == shortcut
206 bb[n/2] = 'N'
207 }
208 ans = append(ans, struct{ before, after string }{a, string(bb)})
209 }
210 return ans
211 }
212
213 // This benchmark represents a common case for a diff command:
214 // large file with a single relatively small diff in the middle.
215 // (It's not clear whether this is representative of gopls workloads
216 // or whether it is important to gopls diff performance.)
217 //
218 // TODO(adonovan) opt: it could be much faster. For example,
219 // comparing a file against itself is about 10x faster than with the
220 // small deletion in the middle. Strangely, comparing a file against
221 // itself minus the last byte is faster still; I don't know why.
222 // There is much low-hanging fruit here for further improvement.
223 func BenchmarkLargeFileSmallDiff(b *testing.B) {
224 data, err := os.ReadFile("old.go") // large file
225 if err != nil {
226 log.Fatal(err)
227 }
228
229 n := len(data)
230
231 src := string(data)
232 dst := src[:n*49/100] + src[n*51/100:] // remove 2% from the middle
233 b.Run("string", func(b *testing.B) {
234 for b.Loop() {
235 compute(stringSeqs{src, dst}, twosided, len(src)+len(dst))
236 }
237 })
238
239 srcBytes := []byte(src)
240 dstBytes := []byte(dst)
241 b.Run("bytes", func(b *testing.B) {
242 for b.Loop() {
243 compute(bytesSeqs{srcBytes, dstBytes}, twosided, len(srcBytes)+len(dstBytes))
244 }
245 })
246
247 srcRunes := []rune(src)
248 dstRunes := []rune(dst)
249 b.Run("runes", func(b *testing.B) {
250 for b.Loop() {
251 compute(runesSeqs{srcRunes, dstRunes}, twosided, len(srcRunes)+len(dstRunes))
252 }
253 })
254 srcLines := strings.Split(src, "\n")
255 dstLines := strings.Split(dst, "\n")
256 b.Run("lines", func(b *testing.B) {
257 for b.Loop() {
258 compute(linesSeqs{srcLines, dstLines}, twosided, len(srcLines)+len(dstLines))
259 }
260 })
261 }