-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathordered_set_test.go
More file actions
141 lines (114 loc) · 2.46 KB
/
Copy pathordered_set_test.go
File metadata and controls
141 lines (114 loc) · 2.46 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
129
130
131
132
133
134
135
136
137
138
139
140
141
package dchan
import (
"testing"
)
func TestHas(t *testing.T) {
s := newOrderedSet[string]()
if s.has("a") {
t.Error("empty set should not have 'a'")
}
s.put("a")
if !s.has("a") {
t.Error("set should have 'a' after put")
}
}
func TestGet(t *testing.T) {
s := newOrderedSet[string]()
_, ok := s.get(0)
if ok {
t.Error("get(0) on empty set should return false")
}
s.put("a")
s.put("b")
val, ok := s.get(0)
if !ok || val != "a" {
t.Errorf("get(0) = %v, %v, want 'a', true", val, ok)
}
val, ok = s.get(1)
if !ok || val != "b" {
t.Errorf("get(1) = %v, %v, want 'b', true", val, ok)
}
}
func TestPut(t *testing.T) {
s := newOrderedSet[string]()
if !s.put("a") {
t.Error("put('a') should return true")
}
if s.put("a") {
t.Error("put('a') again should return false")
}
if s.len() != 1 {
t.Errorf("expected 1 item, got %d", s.len())
}
}
func TestDelete(t *testing.T) {
s := newOrderedSet[string]()
if s.delete("a") {
t.Error("delete from empty set should return false")
}
s.put("a")
s.put("b")
s.put("c")
if !s.delete("b") {
t.Error("delete('b') should return true")
}
if s.has("b") {
t.Error("set should not have 'b' after delete")
}
if s.len() != 2 {
t.Errorf("expected 2 items, got %d", s.len())
}
// Verify order is preserved
if val, ok := s.get(0); !ok || val != "a" {
t.Errorf("expected 'a', got %v", val)
}
if val, ok := s.get(1); !ok || val != "c" {
t.Errorf("expected 'c', got %v", val)
}
}
func TestOrderPreservation(t *testing.T) {
s := newOrderedSet[string]()
s.put("a")
s.put("b")
s.put("c")
// Delete middle and verify order
s.delete("b")
if val, ok := s.get(0); !ok || val != "a" {
t.Errorf("expected 'a', got %v", val)
}
if val, ok := s.get(1); !ok || val != "c" {
t.Errorf("expected 'c', got %v", val)
}
// Re-add and verify it goes to end
s.put("b")
if val, ok := s.get(2); !ok || val != "b" {
t.Errorf("expected 'b', got %v", val)
}
}
func TestToSlice(t *testing.T) {
s := newOrderedSet[string]()
s.put("a")
s.put("b")
s.put("c")
slice := s.toSlice()
if len(slice) != 3 {
t.Errorf("expected 3 items, got %d", len(slice))
}
}
func TestDifference(t *testing.T) {
s1 := newOrderedSet[string]()
s2 := newOrderedSet[string]()
s1.put("a")
s1.put("b")
s1.put("c")
s2.put("b")
s2.put("c")
s2.put("d")
diff := s1.difference(s2)
if diff.len() != 1 {
t.Errorf("expected 1 item, got %d", diff.len())
}
if !diff.has("a") {
t.Error("set should have 'a' after difference")
}
}