~gioverse/chat

ref: c86e075f0df33cc01ac99f167a09f981997727d0 chat/list/slice_test.go -rw-r--r-- 5.3 KiB
c86e075fChris Waldon debug: add function to easily log structures 10 months ago
                                                                                
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
package list

import "testing"

func TestSliceRemove(t *testing.T) {
	type testcase struct {
		name   string
		data   []Element
		index  int
		result []Element
	}
	for _, tc := range []testcase{
		{
			name:   "empty slice",
			data:   []Element{},
			index:  0,
			result: []Element{},
		},
		{
			name:   "nil slice",
			data:   nil,
			index:  0,
			result: nil,
		},
		{
			name: "index out of bounds",
			data: []Element{
				testElement{},
			},
			index: 5,
			result: []Element{
				testElement{},
			},
		},
		{
			name: "single element slice",
			data: []Element{
				testElement{},
			},
			index:  0,
			result: []Element{},
		},
		{
			name: "two element slice (remove first)",
			data: []Element{
				testElement{serial: "a"},
				testElement{serial: "b"},
			},
			index: 0,
			result: []Element{
				testElement{serial: "b"},
			},
		},
		{
			name: "two element slice (remove last)",
			data: []Element{
				testElement{serial: "a"},
				testElement{serial: "b"},
			},
			index: 1,
			result: []Element{
				testElement{serial: "a"},
			},
		},
		{
			name: "three element slice (remove first)",
			data: []Element{
				testElement{serial: "a"},
				testElement{serial: "b"},
				testElement{serial: "c"},
			},
			index: 0,
			result: []Element{
				testElement{serial: "c"},
				testElement{serial: "b"},
			},
		},
		{
			name: "three element slice (remove middle)",
			data: []Element{
				testElement{serial: "a"},
				testElement{serial: "b"},
				testElement{serial: "c"},
			},
			index: 1,
			result: []Element{
				testElement{serial: "a"},
				testElement{serial: "c"},
			},
		},
		{
			name: "three element slice (remove last)",
			data: []Element{
				testElement{serial: "a"},
				testElement{serial: "b"},
				testElement{serial: "c"},
			},
			index: 2,
			result: []Element{
				testElement{serial: "a"},
				testElement{serial: "b"},
			},
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			SliceRemove(&tc.data, tc.index)
			if !elementsEqual(tc.data, tc.result) {
				t.Errorf("expected %v, got %v", tc.result, tc.data)
			}
		})
	}
}

func TestSliceFilter(t *testing.T) {
	type testcase struct {
		name      string
		data      []Element
		predicate func(Element) bool
		result    []Element
	}
	for _, tc := range []testcase{
		{
			name:      "empty slice",
			data:      []Element{},
			predicate: func(_ Element) bool { return true },
			result:    []Element{},
		},
		{
			name:      "nil slice",
			data:      nil,
			predicate: func(_ Element) bool { return true },
			result:    nil,
		},
		{
			name: "nil predicate",
			data: []Element{
				testElement{},
			},
			predicate: nil,
			result: []Element{
				testElement{},
			},
		},
		{
			name: "single element slice remove none",
			data: []Element{
				testElement{},
			},
			predicate: func(_ Element) bool { return true },
			result: []Element{
				testElement{},
			},
		},
		{
			name: "single element slice remove all",
			data: []Element{
				testElement{},
			},
			predicate: func(_ Element) bool { return false },
			result:    []Element{},
		},
		{
			name: "two element slice (remove first)",
			data: []Element{
				testElement{serial: "a"},
				testElement{serial: "b"},
			},
			predicate: func(e Element) bool { return e.Serial() != "a" },
			result: []Element{
				testElement{serial: "b"},
			},
		},
		{
			name: "two element slice (remove last)",
			data: []Element{
				testElement{serial: "a"},
				testElement{serial: "b"},
			},
			predicate: func(e Element) bool { return e.Serial() != "b" },
			result: []Element{
				testElement{serial: "a"},
			},
		},
		{
			name: "three element slice (remove first)",
			data: []Element{
				testElement{serial: "a"},
				testElement{serial: "b"},
				testElement{serial: "c"},
			},
			predicate: func(e Element) bool { return e.Serial() != "a" },
			result: []Element{
				testElement{serial: "c"},
				testElement{serial: "b"},
			},
		},
		{
			name: "three element slice (remove middle)",
			data: []Element{
				testElement{serial: "a"},
				testElement{serial: "b"},
				testElement{serial: "c"},
			},
			predicate: func(e Element) bool { return e.Serial() != "b" },
			result: []Element{
				testElement{serial: "a"},
				testElement{serial: "c"},
			},
		},
		{
			name: "three element slice (remove last)",
			data: []Element{
				testElement{serial: "a"},
				testElement{serial: "b"},
				testElement{serial: "c"},
			},
			predicate: func(e Element) bool { return e.Serial() != "c" },
			result: []Element{
				testElement{serial: "a"},
				testElement{serial: "b"},
			},
		},
		{
			name: "three element slice (remove first two)",
			data: []Element{
				testElement{serial: "a"},
				testElement{serial: "b"},
				testElement{serial: "c"},
			},
			predicate: func(e Element) bool { return e.Serial() != "a" && e.Serial() != "b" },
			result: []Element{
				testElement{serial: "c"},
			},
		},
		{
			name: "three element slice (remove last two)",
			data: []Element{
				testElement{serial: "a"},
				testElement{serial: "b"},
				testElement{serial: "c"},
			},
			predicate: func(e Element) bool { return e.Serial() != "b" && e.Serial() != "c" },
			result: []Element{
				testElement{serial: "a"},
			},
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			SliceFilter(&tc.data, tc.predicate)
			if !elementsEqual(tc.data, tc.result) {
				t.Errorf("expected %v, got %v", tc.result, tc.data)
			}
		})
	}
}