-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
patience_test.go
166 lines (163 loc) · 3.33 KB
/
patience_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Package patience implements the Patience Diff algorithm.
package patience
import (
"reflect"
"testing"
)
func Test_uniqueElements(t *testing.T) {
type args struct {
a []string
}
tests := []struct {
name string
args args
wantOut []string
wantIndices []int
}{
{
name: "Test every element is unique",
args: args{
a: []string{"a", "b", "c"},
},
wantOut: []string{"a", "b", "c"},
wantIndices: []int{0, 1, 2},
},
{
name: "Test duplicate elements",
args: args{
a: []string{"a", "b", "a", "c"},
},
wantOut: []string{"b", "c"},
wantIndices: []int{1, 3},
},
{
name: "Test no unique elements",
args: args{
a: []string{"a", "b", "a", "c", "c", "b"},
},
wantOut: []string{},
wantIndices: []int{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotOut, gotIndices := uniqueElements(tt.args.a)
if !reflect.DeepEqual(gotOut, tt.wantOut) {
t.Errorf("uniqueElements() = %v, want %v", gotOut, tt.wantOut)
}
if !reflect.DeepEqual(gotIndices, tt.wantIndices) {
t.Errorf("uniqueElements() = %v, want %v", gotIndices, tt.wantIndices)
}
})
}
}
func TestDiff(t *testing.T) {
type args struct {
a []string
b []string
}
tests := []struct {
name string
args args
want []DiffLine
}{
{
name: "Test empty slices",
args: args{
a: []string{},
b: []string{},
},
want: nil,
},
{
name: "Test empty slice a",
args: args{
a: []string{},
b: []string{"a"},
},
want: []DiffLine{
{Text: "a", Type: Insert},
},
},
{
name: "Test empty slice b",
args: args{
a: []string{"a"},
b: []string{},
},
want: []DiffLine{
{Text: "a", Type: Delete},
},
},
{
name: "Test no diff",
args: args{
a: []string{"a"},
b: []string{"a"},
},
want: []DiffLine{
{Text: "a", Type: Equal},
},
},
{
name: "Test equal elements at the head of slices a and b",
args: args{
a: []string{"a", "b"},
b: []string{"a", "c"},
},
want: []DiffLine{
{Text: "a", Type: Equal},
{Text: "b", Type: Delete},
{Text: "c", Type: Insert},
},
},
{
name: "Test equal elements at the tail of slices a and b",
args: args{
a: []string{"a", "c"},
b: []string{"b", "c"},
},
want: []DiffLine{
{Text: "a", Type: Delete},
{Text: "b", Type: Insert},
{Text: "c", Type: Equal},
},
},
{
name: "Test equal elements at the head and tail of slices a and b",
args: args{
a: []string{"a", "b", "c"},
b: []string{"a", "d", "c"},
},
want: []DiffLine{
{Text: "a", Type: Equal},
{Text: "b", Type: Delete},
{Text: "d", Type: Insert},
{Text: "c", Type: Equal},
},
},
{
name: "Test diffing the gaps between the LCS elements of slices a and b",
args: args{
a: []string{"a", "w", "b", "x", "c"},
b: []string{"a", "y", "b", "z", "c"},
},
want: []DiffLine{
{Text: "a", Type: Equal},
{Text: "w", Type: Delete},
{Text: "y", Type: Insert},
{Text: "b", Type: Equal},
{Text: "x", Type: Delete},
{Text: "z", Type: Insert},
{Text: "c", Type: Equal},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Diff(tt.args.a, tt.args.b); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Diff() = %v, want %v", got, tt.want)
}
})
}
}