-
Notifications
You must be signed in to change notification settings - Fork 32
/
sets_test.go
126 lines (117 loc) · 2.39 KB
/
sets_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
package immutable
import (
"testing"
)
func TestSetsPut(t *testing.T) {
s := NewSet[string](nil)
s2 := s.Add("1").Add("1")
s2.Add("2")
if s.Len() != 0 {
t.Fatalf("Unexpected mutation of set")
}
if s.Has("1") {
t.Fatalf("Unexpected set element")
}
if s2.Len() != 1 {
t.Fatalf("Unexpected non-mutation of set")
}
if !s2.Has("1") {
t.Fatalf("Set element missing")
}
itr := s2.Iterator()
counter := 0
for !itr.Done() {
i, v := itr.Next()
t.Log(i, v)
counter++
}
if counter != 1 {
t.Fatalf("iterator wrong length")
}
}
func TestSetsDelete(t *testing.T) {
s := NewSet[string](nil)
s2 := s.Add("1")
s3 := s.Delete("1")
if s2.Len() != 1 {
t.Fatalf("Unexpected non-mutation of set")
}
if !s2.Has("1") {
t.Fatalf("Set element missing")
}
if s3.Len() != 0 {
t.Fatalf("Unexpected set length after delete")
}
if s3.Has("1") {
t.Fatalf("Unexpected set element after delete")
}
}
func TestSortedSetsPut(t *testing.T) {
s := NewSortedSet[string](nil)
s2 := s.Add("1").Add("1").Add("0")
if s.Len() != 0 {
t.Fatalf("Unexpected mutation of set")
}
if s.Has("1") {
t.Fatalf("Unexpected set element")
}
if s2.Len() != 2 {
t.Fatalf("Unexpected non-mutation of set")
}
if !s2.Has("1") {
t.Fatalf("Set element missing")
}
itr := s2.Iterator()
counter := 0
for !itr.Done() {
i, v := itr.Next()
t.Log(i, v)
if counter == 0 && i != "0" {
t.Fatalf("sort did not work for first el")
}
if counter == 1 && i != "1" {
t.Fatalf("sort did not work for second el")
}
counter++
}
if counter != 2 {
t.Fatalf("iterator wrong length")
}
}
func TestSortedSetsDelete(t *testing.T) {
s := NewSortedSet[string](nil)
s2 := s.Add("1")
s3 := s.Delete("1")
if s2.Len() != 1 {
t.Fatalf("Unexpected non-mutation of set")
}
if !s2.Has("1") {
t.Fatalf("Set element missing")
}
if s3.Len() != 0 {
t.Fatalf("Unexpected set length after delete")
}
if s3.Has("1") {
t.Fatalf("Unexpected set element after delete")
}
}
func TestSortedSetBuilder(t *testing.T) {
b := NewSortedSetBuilder[string](nil)
b.Set("test3")
b.Set("test1")
b.Set("test2")
s := b.SortedSet()
items := s.Items()
if len(items) != 3 {
t.Fatalf("Set has wrong number of items")
}
if items[0] != "test1" {
t.Fatalf("First item incorrectly sorted")
}
if items[1] != "test2" {
t.Fatalf("Second item incorrectly sorted")
}
if items[2] != "test3" {
t.Fatalf("Third item incorrectly sorted")
}
}