-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathset_test.go
148 lines (139 loc) · 3.82 KB
/
set_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
package genericset
import (
"testing"
)
func TestIsElement(t *testing.T) {
cases := []struct {
desc string
items []int
elem int
expected bool
}{
{"element should be in the set", []int{1, 2, 3}, 1, true},
{"element should be in the set", []int{1, 2, 3, 4, 5}, 5, true},
{"element in not in the set", []int{1, 2, 3, 4, 5}, 10, false},
{"element in not in the set", []int{}, 10, false},
}
for _, tc := range cases {
s := New[int]()
s.Add(tc.items...)
actual := s.IsElement(tc.elem)
if actual != tc.expected {
t.Fatalf("%s: expected: %v got: %v for set: %v and element %d",
tc.desc, tc.expected, actual, s, tc.elem)
}
}
}
func TestIsEmpty(t *testing.T) {
s := New[string]()
if !s.IsEmpty() {
t.Error("set should be empty")
}
s.Add("elem")
if s.IsEmpty() {
t.Error("set is not empty")
}
}
func TestIsSubset(t *testing.T) {
cases := []struct {
desc string
items1 []int
items2 []int
expected bool
}{
{"equal sets", []int{1, 2, 3}, []int{1, 2, 3}, true},
{"s1 is a subset of s2", []int{1, 2, 3}, []int{1, 2, 3, 4, 5}, true},
{"sets has no common elements", []int{1, 2}, []int{3, 4}, false},
{"s1 cannot be a subset of an empty set", []int{1, 2, 3}, []int{}, false},
{"both sets are empty", []int{}, []int{}, true},
{"empty set are subset of any set", []int{}, []int{1}, true},
}
for _, tc := range cases {
s1 := New[int]()
s1.Add(tc.items1...)
s2 := New[int]()
s2.Add(tc.items2...)
actual := s1.IsSubset(&s2)
if actual != tc.expected {
t.Fatalf("%s: expected: %v got: %v for set: %v and set %v",
tc.desc, tc.expected, actual, s1, s2)
}
}
}
func TestIntersection(t *testing.T) {
cases := []struct {
desc string
items1 []int
items2 []int
expected []int
}{
{"equal sets", []int{1, 2, 3}, []int{1, 2, 3}, []int{1, 2, 3}},
{"sets has no common elements", []int{1, 2}, []int{3, 4}, []int{}},
{"one set is empty", []int{1, 2, 3}, []int{}, []int{}},
{"both sets are empty", []int{}, []int{}, []int{}},
}
for _, tc := range cases {
s1 := New[int]()
s1.Add(tc.items1...)
s2 := New[int]()
s2.Add(tc.items2...)
expected := New[int]()
expected.Add(tc.expected...)
actual := s1.Intersection(&s2)
if actual.Size() != expected.Size() || !expected.IsSubset(actual) {
t.Fatalf("%s: expected: %v got: %v for set: %v and set %v",
tc.desc, tc.expected, actual, s1, s2)
}
}
}
func TestIsDisjoint(t *testing.T) {
cases := []struct {
desc string
items1 []int
items2 []int
expected bool
}{
{"equal sets", []int{1, 2, 3}, []int{1, 2, 3}, false},
{"s1 is a subset of s2", []int{1, 2, 3}, []int{1, 2, 3, 4, 5}, false},
{"sets has no common elements", []int{1, 2}, []int{3, 4}, true},
{"both sets are empty", []int{}, []int{}, true},
{"one set is empty", []int{}, []int{1}, true},
}
for _, tc := range cases {
s1 := New[int]()
s1.Add(tc.items1...)
s2 := New[int]()
s2.Add(tc.items2...)
actual := s1.IsDisjoint(&s2)
if actual != tc.expected {
t.Fatalf("%s: expected: %v got: %v for set: %v and set %v",
tc.desc, tc.expected, actual, s1, s2)
}
}
}
func TestUnion(t *testing.T) {
cases := []struct {
desc string
items1 []int
items2 []int
expected []int
}{
{"equal sets", []int{1, 2, 3}, []int{1, 2, 3}, []int{1, 2, 3}},
{"sets has no common elements", []int{1, 2}, []int{3, 4}, []int{1, 2, 3, 4}},
{"one set is empty", []int{1, 2, 3}, []int{}, []int{1, 2, 3}},
{"both sets are empty", []int{}, []int{}, []int{}},
}
for _, tc := range cases {
s1 := New[int]()
s1.Add(tc.items1...)
s2 := New[int]()
s2.Add(tc.items2...)
expected := New[int]()
expected.Add(tc.expected...)
actual := s1.Union(&s2)
if actual.Size() != expected.Size() || !expected.IsSubset(actual) {
t.Fatalf("%s: expected: %v got: %v for set: %v and set %v",
tc.desc, tc.expected, actual, s1, s2)
}
}
}