-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathintersection_test.go
124 lines (118 loc) · 2.31 KB
/
intersection_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
package listintersection
import (
"reflect"
"testing"
)
func TestListFromString(t *testing.T) {
type args struct {
list string
}
tests := []struct {
name string
args args
want *List
}{
{
name: "1->2->3",
args: args{list: "1->2->3"},
want: &List{head: &Node{
val: "1",
next: &Node{
val: "2",
next: &Node{
val: "3",
},
},
},
},
},
{
name: "1->2->",
args: args{list: "1->2->"},
want: &List{head: &Node{
val: "1",
next: &Node{
val: "2",
next: &Node{
val: "",
},
},
},
},
},
{
name: "",
args: args{list: ""},
want: &List{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ListFromString(tt.args.list); got.String() != tt.want.String() {
t.Errorf("ListFromString() = %v, want %v", got.String(), tt.want.String())
}
})
}
}
func TestList_CheckIntersection(t *testing.T) {
type args struct {
ll *List
}
tests := []struct {
name string
list *List
args args
want *Node
}{
{
name: "first smaller: 3->7->8->10 and 1->2->3->99->1->8->10",
list: ListFromString("3->7->8->10"),
args: args{ll: ListFromString("1->2->3->99->1->8->10")},
want: ListFromString("8->10").head,
},
{
name: "3->7->8->10 and 99->1->8->10",
list: ListFromString("3->7->8->10"),
args: args{ll: ListFromString("99->1->8->10")},
want: ListFromString("8->10").head,
},
{
name: "No intersection",
list: ListFromString("3->7->8->10"),
args: args{ll: ListFromString("3->4->5")},
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.list.CheckIntersection(tt.args.ll); !reflect.DeepEqual(got, tt.want) {
t.Errorf("List.CheckIntersection() = %v, want %v", got, tt.want)
}
})
}
}
func TestList_Add(t *testing.T) {
type args struct {
node *Node
}
tests := []struct {
name string
list *List
args args
want *List
}{
{
name: "1->2->3 + 4->5->6",
list: ListFromString("1->2->3"),
args: args{node: ListFromString("4->5->6").head},
want: ListFromString("1->2->3->4->5->6"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.list.Add(tt.args.node); tt.want.String() != got.String() {
t.Errorf("List.Add() = %v, want %v", got, tt.want)
}
})
}
}