-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_test.go
More file actions
317 lines (237 loc) · 6.75 KB
/
array_test.go
File metadata and controls
317 lines (237 loc) · 6.75 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package core_test
import (
. "dappco.re/go"
)
// --- Array[T] ---
func TestArray_New_Good(t *T) {
a := NewArray("a", "b", "c")
AssertEqual(t, 3, a.Len())
}
func TestArray_Add_Good(t *T) {
a := NewArray[string]()
a.Add("x", "y")
AssertEqual(t, 2, a.Len())
AssertTrue(t, a.Contains("x"))
AssertTrue(t, a.Contains("y"))
}
func TestArray_AddUnique_Good(t *T) {
a := NewArray("a", "b")
a.AddUnique("b", "c")
AssertEqual(t, 3, a.Len())
}
func TestArray_Contains_Good(t *T) {
a := NewArray(1, 2, 3)
AssertTrue(t, a.Contains(2))
AssertFalse(t, a.Contains(99))
}
func TestArray_Filter_Good(t *T) {
a := NewArray(1, 2, 3, 4, 5)
r := a.Filter(func(n int) bool { return n%2 == 0 })
AssertTrue(t, r.OK)
evens := r.Value.(*Array[int])
AssertEqual(t, 2, evens.Len())
AssertTrue(t, evens.Contains(2))
AssertTrue(t, evens.Contains(4))
}
func TestArray_Each_Good(t *T) {
a := NewArray("a", "b", "c")
var collected []string
a.Each(func(s string) { collected = append(collected, s) })
AssertEqual(t, []string{"a", "b", "c"}, collected)
}
func TestArray_Remove_Good(t *T) {
a := NewArray("a", "b", "c")
a.Remove("b")
AssertEqual(t, 2, a.Len())
AssertFalse(t, a.Contains("b"))
}
func TestArray_Remove_Bad(t *T) {
a := NewArray("a", "b")
a.Remove("missing")
AssertEqual(t, 2, a.Len())
}
func TestArray_Deduplicate_Good(t *T) {
a := NewArray("a", "b", "a", "c", "b")
a.Deduplicate()
AssertEqual(t, 3, a.Len())
}
func TestArray_Clear_Good(t *T) {
a := NewArray(1, 2, 3)
a.Clear()
AssertEqual(t, 0, a.Len())
}
func TestArray_AsSlice_Good(t *T) {
a := NewArray("x", "y")
s := a.AsSlice()
AssertEqual(t, []string{"x", "y"}, s)
}
func TestArray_Empty_Good(t *T) {
a := NewArray[int]()
AssertEqual(t, 0, a.Len())
AssertFalse(t, a.Contains(0))
AssertEqual(t, []int(nil), a.AsSlice())
}
func TestArray_NewArray_Good(t *T) {
agents := NewArray("codex", "hades", "homelab")
AssertEqual(t, 3, agents.Len())
AssertTrue(t, agents.Contains("hades"))
}
func TestArray_NewArray_Bad(t *T) {
agents := NewArray[string]()
AssertEqual(t, 0, agents.Len())
AssertNil(t, agents.AsSlice())
}
func TestArray_NewArray_Ugly(t *T) {
agents := NewArray("codex", "codex")
AssertEqual(t, 2, agents.Len())
agents.Deduplicate()
AssertEqual(t, []string{"codex"}, agents.AsSlice())
}
func TestArray_Array_Add_Good(t *T) {
agents := NewArray("codex")
agents.Add("hades", "homelab")
AssertEqual(t, []string{"codex", "hades", "homelab"}, agents.AsSlice())
}
func TestArray_Array_Add_Bad(t *T) {
agents := NewArray("codex")
agents.Add()
AssertEqual(t, []string{"codex"}, agents.AsSlice())
}
func TestArray_Array_Add_Ugly(t *T) {
agents := NewArray[string]()
agents.Add("")
AssertEqual(t, []string{""}, agents.AsSlice())
}
func TestArray_Array_AddUnique_Good(t *T) {
agents := NewArray("codex", "hades")
agents.AddUnique("hades", "homelab")
AssertEqual(t, []string{"codex", "hades", "homelab"}, agents.AsSlice())
}
func TestArray_Array_AddUnique_Bad(t *T) {
agents := NewArray("codex")
agents.AddUnique("codex", "codex")
AssertEqual(t, []string{"codex"}, agents.AsSlice())
}
func TestArray_Array_AddUnique_Ugly(t *T) {
agents := NewArray("")
agents.AddUnique("", "codex")
AssertEqual(t, []string{"", "codex"}, agents.AsSlice())
}
func TestArray_Array_AsSlice_Good(t *T) {
agents := NewArray("codex", "hades")
AssertEqual(t, []string{"codex", "hades"}, agents.AsSlice())
}
func TestArray_Array_AsSlice_Bad(t *T) {
agents := NewArray[string]()
AssertNil(t, agents.AsSlice())
}
func TestArray_Array_AsSlice_Ugly(t *T) {
agents := NewArray("codex")
copy := agents.AsSlice()
copy[0] = "hades"
AssertEqual(t, []string{"codex"}, agents.AsSlice())
AssertEqual(t, []string{"hades"}, copy)
}
func TestArray_Array_Clear_Good(t *T) {
agents := NewArray("codex", "hades")
agents.Clear()
AssertEqual(t, 0, agents.Len())
AssertNil(t, agents.AsSlice())
}
func TestArray_Array_Clear_Bad(t *T) {
agents := NewArray[string]()
agents.Clear()
AssertEqual(t, 0, agents.Len())
}
func TestArray_Array_Clear_Ugly(t *T) {
agents := NewArray("codex")
agents.Clear()
agents.Add("hades")
AssertEqual(t, []string{"hades"}, agents.AsSlice())
}
func TestArray_Array_Contains_Good(t *T) {
agents := NewArray("codex", "hades")
AssertTrue(t, agents.Contains("codex"))
}
func TestArray_Array_Contains_Bad(t *T) {
agents := NewArray("codex", "hades")
AssertFalse(t, agents.Contains("homelab"))
}
func TestArray_Array_Contains_Ugly(t *T) {
var agents Array[string]
AssertFalse(t, agents.Contains(""))
}
func TestArray_Array_Deduplicate_Good(t *T) {
agents := NewArray("codex", "hades", "codex")
agents.Deduplicate()
AssertEqual(t, []string{"codex", "hades"}, agents.AsSlice())
}
func TestArray_Array_Deduplicate_Bad(t *T) {
agents := NewArray("codex", "hades")
agents.Deduplicate()
AssertEqual(t, []string{"codex", "hades"}, agents.AsSlice())
}
func TestArray_Array_Deduplicate_Ugly(t *T) {
agents := NewArray[string]()
agents.Deduplicate()
AssertEmpty(t, agents.AsSlice())
}
func TestArray_Array_Each_Good(t *T) {
agents := NewArray("codex", "hades")
seen := []string{}
agents.Each(func(name string) { seen = append(seen, name) })
AssertEqual(t, []string{"codex", "hades"}, seen)
}
func TestArray_Array_Each_Bad(t *T) {
agents := NewArray("codex")
AssertPanics(t, func() { agents.Each(nil) })
}
func TestArray_Array_Each_Ugly(t *T) {
agents := NewArray[string]()
AssertNotPanics(t, func() { agents.Each(nil) })
}
func TestArray_Array_Filter_Good(t *T) {
agents := NewArray("codex", "hades", "homelab")
r := agents.Filter(func(name string) bool { return HasPrefix(name, "h") })
AssertTrue(t, r.OK)
AssertEqual(t, []string{"hades", "homelab"}, r.Value.(*Array[string]).AsSlice())
}
func TestArray_Array_Filter_Bad(t *T) {
agents := NewArray("codex")
AssertPanics(t, func() { agents.Filter(nil) })
}
func TestArray_Array_Filter_Ugly(t *T) {
agents := NewArray[string]()
r := agents.Filter(nil)
AssertTrue(t, r.OK)
AssertNil(t, r.Value.(*Array[string]).AsSlice())
}
func TestArray_Array_Len_Good(t *T) {
agents := NewArray("codex", "hades")
AssertEqual(t, 2, agents.Len())
}
func TestArray_Array_Len_Bad(t *T) {
agents := NewArray[string]()
AssertEqual(t, 0, agents.Len())
}
func TestArray_Array_Len_Ugly(t *T) {
agents := NewArray("codex")
agents.Add("hades")
agents.Remove("codex")
AssertEqual(t, 1, agents.Len())
}
func TestArray_Array_Remove_Good(t *T) {
agents := NewArray("codex", "hades")
agents.Remove("codex")
AssertEqual(t, []string{"hades"}, agents.AsSlice())
}
func TestArray_Array_Remove_Bad(t *T) {
agents := NewArray("codex")
agents.Remove("missing")
AssertEqual(t, []string{"codex"}, agents.AsSlice())
}
func TestArray_Array_Remove_Ugly(t *T) {
agents := NewArray("codex", "codex", "hades")
agents.Remove("codex")
AssertEqual(t, []string{"codex", "hades"}, agents.AsSlice())
}