Skip to content

Commit

Permalink
Add a failing line diff test case
Browse files Browse the repository at this point in the history
Current implementation produces wrong result because
it calls `DiffMain` on the following 2 arguments:

* `1,2,3,4,5,6,7,8,9,10`
* `1,2,3,4,5,6,7,8,9,11`

This numbers represent indices into the lines array.
The algorithm finds that equal part of those
strings is `1,2,3,4,5,6,7,8,9,1` and which is followed
by `Delete 0` and `Insert `1`.
  • Loading branch information
kdarkhan committed Jan 22, 2023
1 parent facec63 commit 74798f5
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions diffmatchpatch/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,38 @@ func TestMassiveRuneDiffConversion(t *testing.T) {
assert.NotEmpty(t, diffs)
}

func TestDiffPartialLineIndex(t *testing.T) {
dmp := New()
t1, t2, tt := dmp.DiffLinesToChars(
`line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10 text1`,
`line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10 text2`)
diffs := dmp.DiffMain(t1, t2, false)
diffs = dmp.DiffCharsToLines(diffs, tt)
assert.Equal(t, []Diff{
Diff{DiffEqual, "line 1\nline 2\nline 3\nline 4\nline 5\nline 6\nline 7\nline 8\nline 9\n"},
Diff{DiffDelete, "line 10 text1"},
Diff{DiffInsert, "line 10 text2"},
}, diffs)
}

func BenchmarkDiffMain(bench *testing.B) {
s1 := "`Twas brillig, and the slithy toves\nDid gyre and gimble in the wabe:\nAll mimsy were the borogoves,\nAnd the mome raths outgrabe.\n"
s2 := "I am the very model of a modern major general,\nI've information vegetable, animal, and mineral,\nI know the kings of England, and I quote the fights historical,\nFrom Marathon to Waterloo, in order categorical.\n"
Expand Down

0 comments on commit 74798f5

Please sign in to comment.