-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathFastList.Test.cs
More file actions
391 lines (322 loc) · 9.55 KB
/
Copy pathFastList.Test.cs
File metadata and controls
391 lines (322 loc) · 9.55 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) Martin Bustos @FronkonGames <fronkongames@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of
// the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using NUnit.Framework;
using FronkonGames.GameWork.Foundation;
/// <summary> FastList tests. </summary>
[TestFixture]
public class FastListTests
{
/// <summary> Add increases count. </summary>
[Test]
public void Add_IncreasesCount()
{
FastList<int> list = new();
list.Add(42);
Assert.AreEqual(1, list.Count);
}
/// <summary> Add multiple elements. </summary>
[Test]
public void Add_MultipleElements()
{
FastList<int> list = new();
list.Add(1);
list.Add(2);
list.Add(3);
Assert.AreEqual(3, list.Count);
Assert.AreEqual(1, list[0]);
Assert.AreEqual(2, list[1]);
Assert.AreEqual(3, list[2]);
}
/// <summary> Capacity grows automatically. </summary>
[Test]
public void Capacity_GrowsAutomatically()
{
FastList<int> list = new(2);
int initialCapacity = list.Capacity;
for (int i = 0; i < initialCapacity + 10; ++i)
list.Add(i);
Assert.Greater(list.Capacity, initialCapacity);
Assert.AreEqual(initialCapacity + 10, list.Count);
}
/// <summary> Initial capacity is correct. </summary>
[Test]
public void InitialCapacity_IsCorrect()
{
FastList<int> list = new(16);
Assert.AreEqual(16, list.Capacity);
Assert.AreEqual(0, list.Count);
}
/// <summary> Get returns correct value. </summary>
[Test]
public void Indexer_Get_ReturnsCorrectValue()
{
FastList<int> list = new();
list.Add(10);
list.Add(20);
list.Add(30);
Assert.AreEqual(10, list[0]);
Assert.AreEqual(20, list[1]);
Assert.AreEqual(30, list[2]);
}
/// <summary> Set updates value. </summary>
[Test]
public void Indexer_Set_UpdatesValue()
{
FastList<int> list = new();
list.Add(1);
list.Add(2);
list[0] = 99;
Assert.AreEqual(99, list[0]);
Assert.AreEqual(2, list[1]);
}
/// <summary> Index out of range throws. </summary>
[Test]
public void Indexer_OutOfRange_Throws()
{
FastList<int> list = new();
list.Add(1);
Assert.Throws<ArgumentOutOfRangeException>(() => _ = list[1]);
Assert.Throws<ArgumentOutOfRangeException>(() => _ = list[-1]);
}
/// <summary> RemoveAt removes element and returns true. </summary>
[Test]
public void RemoveAt_RemovesElement_ReturnsTrue()
{
FastList<int> list = new();
list.Add(1);
list.Add(2);
list.Add(3);
bool removed = list.RemoveAt(1);
Assert.True(removed);
Assert.AreEqual(2, list.Count);
}
/// <summary> RemoveAt swaps with last element. </summary>
[Test]
public void RemoveAt_SwapsWithLastElement()
{
FastList<int> list = new();
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);
list.RemoveAt(1);
Assert.AreEqual(3, list.Count);
Assert.AreEqual(1, list[0]);
Assert.AreEqual(4, list[1]);
Assert.AreEqual(3, list[2]);
}
/// <summary> RemoveAt invalid index returns false. </summary>
[Test]
public void RemoveAt_InvalidIndex_ReturnsFalse()
{
FastList<int> list = new();
list.Add(1);
Assert.False(list.RemoveAt(5));
Assert.False(list.RemoveAt(-1));
Assert.AreEqual(1, list.Count);
}
/// <summary> RemoveAt last element. </summary>
[Test]
public void RemoveAt_LastElement()
{
FastList<int> list = new();
list.Add(1);
list.Add(2);
list.Add(3);
bool removed = list.RemoveAt(2);
Assert.True(removed);
Assert.AreEqual(2, list.Count);
Assert.AreEqual(1, list[0]);
Assert.AreEqual(2, list[1]);
}
/// <summary> RemoveLast removes last element. </summary>
[Test]
public void RemoveLast_RemovesLastElement()
{
FastList<int> list = new();
list.Add(1);
list.Add(2);
list.Add(3);
bool removed = list.RemoveLast();
Assert.True(removed);
Assert.AreEqual(2, list.Count);
Assert.AreEqual(1, list[0]);
Assert.AreEqual(2, list[1]);
}
/// <summary> RemoveLast empty list returns false. </summary>
[Test]
public void RemoveLast_EmptyList_ReturnsFalse()
{
FastList<int> list = new();
Assert.False(list.RemoveLast());
Assert.AreEqual(0, list.Count);
}
/// <summary> Remove removes first occurrence. </summary>
[Test]
public void Remove_RemovesFirstOccurrence()
{
FastList<int> list = new();
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);
bool removed = list.Remove(2);
Assert.True(removed);
Assert.AreEqual(3, list.Count);
Assert.False(list.Contains(2));
Assert.True(list.Contains(4));
}
/// <summary> Remove item not found returns false. </summary>
[Test]
public void Remove_ItemNotFound_ReturnsFalse()
{
FastList<int> list = new();
list.Add(1);
list.Add(2);
bool removed = list.Remove(99);
Assert.False(removed);
Assert.AreEqual(2, list.Count);
}
/// <summary> Contains returns true when item exists. </summary>
[Test]
public void Contains_ItemExists_ReturnsTrue()
{
FastList<int> list = new();
list.Add(10);
list.Add(20);
list.Add(30);
Assert.True(list.Contains(20));
Assert.True(list.Contains(10));
Assert.True(list.Contains(30));
}
/// <summary> Contains returns false when item not found. </summary>
[Test]
public void Contains_ItemNotFound_ReturnsFalse()
{
FastList<int> list = new();
list.Add(1);
list.Add(2);
Assert.False(list.Contains(99));
}
/// <summary> IndexOf returns correct index. </summary>
[Test]
public void IndexOf_ReturnsCorrectIndex()
{
FastList<int> list = new();
list.Add(10);
list.Add(20);
list.Add(30);
Assert.AreEqual(0, list.IndexOf(10));
Assert.AreEqual(1, list.IndexOf(20));
Assert.AreEqual(2, list.IndexOf(30));
}
/// <summary> IndexOf returns -1 when not found. </summary>
[Test]
public void IndexOf_NotFound_ReturnsMinusOne()
{
FastList<int> list = new();
list.Add(1);
list.Add(2);
Assert.AreEqual(-1, list.IndexOf(99));
}
/// <summary> Clear resets count to 0. </summary>
[Test]
public void Clear_ResetsCountToZero()
{
FastList<int> list = new();
list.Add(1);
list.Add(2);
list.Add(3);
list.Clear();
Assert.AreEqual(0, list.Count);
}
/// <summary> Foreach iterates all elements. </summary>
[Test]
public void Foreach_IteratesAllElements()
{
FastList<int> list = new();
list.Add(10);
list.Add(20);
list.Add(30);
List<int> collected = new();
foreach (int item in list)
collected.Add(item);
Assert.AreEqual(3, collected.Count);
Assert.AreEqual(10, collected[0]);
Assert.AreEqual(20, collected[1]);
Assert.AreEqual(30, collected[2]);
}
/// <summary> Enumerator works correctly. </summary>
[Test]
public void Enumerator_WorksCorrectly()
{
FastList<int> list = new();
list.Add(5);
list.Add(10);
list.Add(15);
using FastList<int>.Enumerator enumerator = list.GetEnumerator();
Assert.True(enumerator.MoveNext());
Assert.AreEqual(5, enumerator.Current);
Assert.True(enumerator.MoveNext());
Assert.AreEqual(10, enumerator.Current);
Assert.True(enumerator.MoveNext());
Assert.AreEqual(15, enumerator.Current);
Assert.False(enumerator.MoveNext());
}
/// <summary> Dispose clears the list. </summary>
[Test]
public void Dispose_ClearsList()
{
FastList<int> list = new();
list.Add(1);
list.Add(2);
list.Add(3);
list.Dispose();
Assert.AreEqual(0, list.Count);
Assert.AreEqual(0, list.Capacity);
}
/// <summary> Empty list operations. </summary>
[Test]
public void EmptyList_Operations()
{
FastList<int> list = new();
Assert.AreEqual(0, list.Count);
Assert.False(list.Contains(1));
Assert.AreEqual(-1, list.IndexOf(1));
Assert.False(list.Remove(1));
Assert.False(list.RemoveAt(0));
Assert.False(list.RemoveLast());
Assert.Throws<ArgumentOutOfRangeException>(() => _ = list[0]);
}
/// <summary> Single element operations. </summary>
[Test]
public void SingleElement_Operations()
{
FastList<int> list = new();
list.Add(42);
Assert.AreEqual(1, list.Count);
Assert.AreEqual(42, list[0]);
Assert.True(list.Contains(42));
Assert.AreEqual(0, list.IndexOf(42));
list[0] = 99;
Assert.AreEqual(99, list[0]);
Assert.True(list.RemoveLast());
Assert.AreEqual(0, list.Count);
}
}