Repository navigation
Expand file tree
/
Copy pathsortedset_test.go
More file actions
128 lines (118 loc) · 2.4 KB
/
Copy pathsortedset_test.go
File metadata and controls
128 lines (118 loc) · 2.4 KB
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
package handy
import (
"testing"
)
func TestSortedIntList(t *testing.T) {
cases := []struct {
in []int
want string
}{
{[]int{1}, "{1}"},
{[]int{1, 2, 1}, "{1 1 2}"},
{[]int{1, 2, 3}, "{1 2 3}"},
{[]int{3, 2, 1}, "{1 2 3}"},
}
for _, c := range cases {
l := NewSortedList[int]()
for _, x := range c.in {
l.Insert(x)
}
got := l.String()
if got != c.want {
t.Errorf("Want: %s, got: %s", c.want, got)
}
}
}
func TestSortedIntListRemoval(t *testing.T) {
cases := []struct {
in []int
out []int
want string
}{
{[]int{1}, []int{1}, "{}"},
{[]int{1, 2, 1}, []int{1, 1}, "{2}"},
{[]int{1, 2, 3}, []int{1, 2, 3, 4}, "{}"},
{[]int{3, 2, 1}, []int{4, 5, 6}, "{1 2 3}"},
}
for _, c := range cases {
l := NewSortedList[int]()
for _, x := range c.in {
l.Insert(x)
}
for _, x := range c.out {
l.Remove(x)
}
got := l.String()
if got != c.want {
t.Errorf("Want: %s, got: %s", c.want, got)
}
}
}
func TestSortedStringList(t *testing.T) {
cases := []struct {
in []string
want string
}{
{[]string{"ab"}, `{ab}`},
{[]string{"ab", "cd"}, `{ab cd}`},
{[]string{"ab", "aa"}, `{aa ab}`},
}
for _, c := range cases {
l := NewSortedList[string]()
for _, x := range c.in {
l.Insert(x)
}
got := l.String()
if got != c.want {
t.Errorf("Want: %s, got: %s", c.want, got)
}
}
}
func TestSortedSet(t *testing.T) {
cases := []struct {
set_a []int
set_b []int
union string
intersect string
}{
{[]int{1}, []int{}, "{1}", "{}"},
{[]int{1}, []int{2}, "{1 2}", "{}"},
{[]int{1, 1, 2}, []int{1, 2, 3}, "{1 1 2 3}", "{1 2}"},
}
for _, c := range cases {
sa := NewSortedSet[int](10)
for _, x := range c.set_a {
sa.Insert(x)
}
sb := NewSortedSet[int](10)
for _, x := range c.set_b {
sb.Insert(x)
}
u := sa.Union(sb)
i := sa.Intersect(sb)
if u.String() != c.union {
t.Errorf("Want: %s, got: %s", c.union, u)
}
if i.String() != c.intersect {
t.Errorf("Want: %s, got: %s", c.intersect, i)
}
}
}
func TestSortedMultiSet(t *testing.T) {
sa := NewSortedSet[int](10)
sb := NewSortedSet[int](10)
for _, x := range []int{1, 2, 3, 3} {
sa.Insert(x)
}
for _, x := range []int{2, 3, 3, 4} {
sb.Insert(x)
}
u := sa.Union(sb)
i := sa.Intersect(sb)
if u.String() != "{1 2 3 3 4}" {
t.Errorf("Want: {1 2 3 3 4}, got: %s", u)
}
if i.String() != "{2 3 3}" {
t.Errorf("Want: {2 3 3}, got: %s", i)
}
}