Skip to content

Commit d44dcdb

Browse files
committed
raname package to diff_test
Doing this does not provide much of a benefit other than exlicitly testing API usage. Also, this changes makes it more obvious to call diff.Runes and diff.Ints in their respective tests (as opposed to Diff itself on &ints{} and &runes{}. I did have to pull the type ints struct from the diff.go file for the BenchmarkDiff call itself, though.
1 parent 55a6d30 commit d44dcdb

File tree

1 file changed

+36
-26
lines changed

1 file changed

+36
-26
lines changed

diff_test.go

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,64 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
package diff
5+
package diff_test
66

77
import (
8+
"github.com/mb0/diff"
89
"testing"
910
)
1011

1112
type testcase struct {
1213
name string
1314
a, b []int
14-
res []Change
15+
res []diff.Change
1516
}
1617

1718
var tests = []testcase{
1819
{"shift",
1920
[]int{1, 2, 3},
2021
[]int{0, 1, 2, 3},
21-
[]Change{{0, 0, 0, 1}},
22+
[]diff.Change{{0, 0, 0, 1}},
2223
},
2324
{"push",
2425
[]int{1, 2, 3},
2526
[]int{1, 2, 3, 4},
26-
[]Change{{3, 3, 0, 1}},
27+
[]diff.Change{{3, 3, 0, 1}},
2728
},
2829
{"unshift",
2930
[]int{0, 1, 2, 3},
3031
[]int{1, 2, 3},
31-
[]Change{{0, 0, 1, 0}},
32+
[]diff.Change{{0, 0, 1, 0}},
3233
},
3334
{"pop",
3435
[]int{1, 2, 3, 4},
3536
[]int{1, 2, 3},
36-
[]Change{{3, 3, 1, 0}},
37+
[]diff.Change{{3, 3, 1, 0}},
3738
},
3839
{"all changed",
3940
[]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
4041
[]int{10, 11, 12, 13, 14},
41-
[]Change{
42+
[]diff.Change{
4243
{0, 0, 10, 5},
4344
},
4445
},
4546
{"all same",
4647
[]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
4748
[]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
48-
[]Change{},
49+
[]diff.Change{},
4950
},
5051
{"wrap",
5152
[]int{1},
5253
[]int{0, 1, 2, 3},
53-
[]Change{
54+
[]diff.Change{
5455
{0, 0, 0, 1},
5556
{1, 2, 0, 2},
5657
},
5758
},
5859
{"snake",
5960
[]int{0, 1, 2, 3, 4, 5},
6061
[]int{1, 2, 3, 4, 5, 6},
61-
[]Change{
62+
[]diff.Change{
6263
{0, 0, 1, 0},
6364
{6, 5, 0, 1},
6465
},
@@ -69,7 +70,7 @@ var tests = []testcase{
6970
{"paper fig. 1",
7071
[]int{1, 2, 3, 1, 2, 2, 1},
7172
[]int{3, 2, 1, 2, 1, 3},
72-
[]Change{
73+
[]diff.Change{
7374
{0, 0, 1, 1},
7475
{2, 2, 1, 0},
7576
{5, 4, 1, 0},
@@ -80,7 +81,7 @@ var tests = []testcase{
8081

8182
func TestDiffAB(t *testing.T) {
8283
for _, test := range tests {
83-
res := Ints(test.a, test.b)
84+
res := diff.Ints(test.a, test.b)
8485
if len(res) != len(test.res) {
8586
t.Error(test.name, "expected length", len(test.res), "for", res)
8687
continue
@@ -95,21 +96,21 @@ func TestDiffAB(t *testing.T) {
9596

9697
func TestDiffBA(t *testing.T) {
9798
// interesting: fig.1 Diff(b, a) results in the same path as `diff -d a b`
98-
tests[len(tests)-1].res = []Change{
99+
tests[len(tests)-1].res = []diff.Change{
99100
{0, 0, 2, 0},
100101
{3, 1, 1, 0},
101102
{5, 2, 0, 1},
102103
{7, 5, 0, 1},
103104
}
104105
for _, test := range tests {
105-
res := Ints(test.b, test.a)
106+
res := diff.Ints(test.b, test.a)
106107
if len(res) != len(test.res) {
107108
t.Error(test.name, "expected length", len(test.res), "for", res)
108109
continue
109110
}
110111
for i, c := range test.res {
111112
// flip change data also
112-
rc := Change{c.B, c.A, c.Ins, c.Del}
113+
rc := diff.Change{c.B, c.A, c.Ins, c.Del}
113114
if rc != res[i] {
114115
t.Error(test.name, "expected ", rc, "got", res[i])
115116
}
@@ -118,13 +119,10 @@ func TestDiffBA(t *testing.T) {
118119
}
119120

120121
func TestDiffRunes(t *testing.T) {
121-
d := &runes{
122-
[]rune("brown fox jumps over the lazy dog"),
123-
[]rune("brwn faax junps ovver the lay dago"),
124-
}
125-
n, m := len(d.a), len(d.b)
126-
res := Diff(n, m, d)
127-
echange := []Change{
122+
a := []rune("brown fox jumps over the lazy dog")
123+
b := []rune("brwn faax junps ovver the lay dago")
124+
res := diff.Runes(a, b)
125+
echange := []diff.Change{
128126
{2, 2, 1, 0},
129127
{7, 6, 1, 2},
130128
{12, 12, 1, 1},
@@ -141,19 +139,31 @@ func TestDiffRunes(t *testing.T) {
141139
}
142140
}
143141

142+
type ints struct{ a, b []int }
143+
144+
func (d *ints) Equal(i, j int) bool { return d.a[i] == d.b[j] }
144145
func BenchmarkDiff(b *testing.B) {
145146
t := tests[len(tests)-1]
146147
d := &ints{t.a, t.b}
147148
n, m := len(d.a), len(d.b)
148149
for i := 0; i < b.N; i++ {
149-
Diff(n, m, d)
150+
diff.Diff(n, m, d)
151+
}
152+
}
153+
154+
func BenchmarkInts(b *testing.B) {
155+
t := tests[len(tests)-1]
156+
d1 := t.a
157+
d2 := t.b
158+
for i := 0; i < b.N; i++ {
159+
diff.Ints(d1, d2)
150160
}
151161
}
152162

153163
func BenchmarkDiffRunes(b *testing.B) {
154-
d := &runes{[]rune("1231221"), []rune("321213")}
155-
n, m := len(d.a), len(d.b)
164+
d1 := []rune("1231221")
165+
d2 := []rune("321213")
156166
for i := 0; i < b.N; i++ {
157-
Diff(n, m, d)
167+
diff.Runes(d1, d2)
158168
}
159169
}

0 commit comments

Comments
 (0)