/
DynamicListUnitTest.cs
449 lines (385 loc) · 16 KB
/
DynamicListUnitTest.cs
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DynamicListUnitTest.cs" company="Katya">
// Katya.com. All rights reserved.
// </copyright>
// // <summary>
// The dynamic list unit test.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace CustomLinkedListUnitTest
{
using System;
using CustomLinkedList;
using CustomLinkedListUnitTests;
using Microsoft.VisualStudio.TestTools.UnitTesting;
/// <summary>
/// The dynamic list unit test.
/// </summary>
[TestClass]
public class DynamicListUnitTest : TestBase
{
/// <summary>
/// Initializes a new instance of the <see cref="DynamicListUnitTest"/> class.
/// </summary>
public DynamicListUnitTest()
{
this.TestContext = this.TestContext;
}
/// <summary>
/// Gets or sets the dynamic list.
/// </summary>
public static DynamicList<string> DynamicList { get; set; }
/// <summary>
/// Gets or sets the test context.
/// </summary>
public TestContext TestContext { get; set; }
/// <summary>
/// Initializes the dynamic list tests.
/// </summary>
/// <param name="testContext">
/// The test context.
/// </param>
[ClassInitialize]
public static void DynamicListTestsInitialize(TestContext testContext)
{
DynamicList = new DynamicList<string>();
}
/// <summary>
/// Resets the dynamic list object.
/// </summary>
[TestInitialize]
public void ResetDynamicListObject()
{
DynamicList = new DynamicList<string>();
}
/// <summary>
/// The count test tests count on new list creation - should pass test.
/// </summary>
[TestMethod]
public void Count_TestCountOnNewListCreation_ShouldPassTest()
{
int initialCount = DynamicList.Count;
Assert.AreEqual(0, initialCount, "A newly created list should have a count of 0.");
}
/// <summary>
/// The count test tests count after adding one element - should pass test.
/// </summary>
[TestMethod]
public void Count_TestCountAfterAddingOneElement_ShouldPassTest()
{
DynamicList.Add("one");
this.AddCleanupAction(() => DynamicList.Remove("one"));
int count = DynamicList.Count;
Assert.AreEqual(1, count, "After adding one element the count of the list should be 1.");
}
/// <summary>
/// The indexer test with empty dynamic list should throw argument out of range exception.
/// </summary>
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Indexer_TestWithEmptyDynamicList_ShouldThrowArgumentOutOfRangeException()
{
string value = DynamicList[0];
}
/// <summary>
/// The indexer test with negative index should throw argument out of range exception.
/// </summary>
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Indexer_TestWithNegativeIndex_ShouldThrowArgumentOutOfRangeException()
{
string value = DynamicList[-1];
}
/// <summary>
/// The indexer test with index beyond list count should throw argument out of range exception.
/// </summary>
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Indexer_TestWithIndexBeyondListCount_ShouldThrowArgumentOutOfRangeException()
{
DynamicList.Add("one");
this.AddCleanupAction(() => DynamicList.Remove("one"));
string actual = DynamicList[1];
}
/// <summary>
/// The indexer test with valid index should pass test.
/// </summary>
[TestMethod]
public void Indexer_TestWithValidIndex_ShouldPassTest()
{
DynamicList.Add("one");
this.AddCleanupAction(() => DynamicList.Remove("one"));
string value = DynamicList[0];
}
/// <summary>
/// The add test tests added element value should pass test.
/// </summary>
[TestMethod]
public void Add_TestAddedElementValue_ShouldPassTest()
{
DynamicList.Add("one");
this.AddCleanupAction(() => DynamicList.Remove("one"));
string value = DynamicList[0];
Assert.AreEqual("one", value, "The value at index 0 should be equal to the value entered.");
}
/// <summary>
/// The elements test tests positions of elements added in sequence - should pass test.
/// </summary>
[TestMethod]
public void Elements_TestPositionsOfElementsAddedInSequence_ShouldPassTest()
{
DynamicList.Add("first");
DynamicList.Add("second");
this.AddCleanupAction(() => DynamicList.Remove("first"));
this.AddCleanupAction(() => DynamicList.Remove("second"));
string firstElement = DynamicList[0];
string secondElement = DynamicList[1];
Assert.AreEqual(
"first",
firstElement,
"The element at index 0 should be the first element added to the list.");
Assert.AreEqual(
"second",
secondElement,
"The element at index 1 should be the second element added to the list.");
}
/// <summary>
/// The set test at negative index should throw argument out of range exception.
/// </summary>
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Set_TestAtNegativeIndex_ShouldThrowArgumentOutOfRangeException()
{
DynamicList[-1] = "one";
}
/// <summary>
/// The set test 1: at index beyond list count should throw argument out of range exception.
/// </summary>
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Set_TestAtIndexBeyondListCount1_ShouldThrowArgumentOutOfRangeException()
{
DynamicList.Add("one");
this.AddCleanupAction(() => DynamicList.Remove("one"));
DynamicList[1] = "two";
}
/// <summary>
/// The set test 2: at index beyond list count should throw argument out of range exception.
/// </summary>
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Set_TestAtIndexBeyondListCount2_ShouldThrowArgumentOutOfRangeException()
{
DynamicList[0] = "one";
}
/// <summary>
/// The set test at valid index should pass test.
/// </summary>
[TestMethod]
public void Set_TestAtValidIndex_ShouldPassTest()
{
DynamicList.Add("initial");
DynamicList[0] = "changed";
this.AddCleanupAction(() => DynamicList.Remove("changed"));
string newValue = DynamicList[0];
Assert.AreEqual("changed", newValue, "The value at index 0 should be changed through the indexer.");
}
/// <summary>
/// The remove test at negative index should throw argument out of range exception.
/// </summary>
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Remove_TestAtNegativeIndex_ShouldThrowArgumentOutOfRangeException()
{
DynamicList.RemoveAt(-1);
}
/// <summary>
/// The remove test at index beyond list count should throw argument out of range exception.
/// </summary>
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Remove_TestAtIndexBeyondListCount_ShouldThrowArgumentOutOfRangeException()
{
DynamicList.Add("first");
DynamicList.Add("second");
this.AddCleanupAction(() => DynamicList.Remove("first"));
this.AddCleanupAction(() => DynamicList.Remove("second"));
DynamicList.RemoveAt(2);
}
/// <summary>
/// The count test - list count after remove at should pass test.
/// </summary>
[TestMethod]
public void Count_TestListCountAfterRemoveAt_ShouldPassTest()
{
DynamicList.Add("one");
DynamicList.RemoveAt(0);
int count = DynamicList.Count;
Assert.AreEqual(0, count);
}
/// <summary>
/// The remove at test - remaining elements positions after remove at should pass test.
/// </summary>
[TestMethod]
public void RemoveAt_TestRemainingElementsPositionsAfterRemoveAt_ShouldPassTest()
{
DynamicList.Add("one");
DynamicList.Add("two");
DynamicList.Add("three");
this.AddCleanupAction(() => DynamicList.Remove("one"));
this.AddCleanupAction(() => DynamicList.Remove("three"));
DynamicList.RemoveAt(1);
string first = DynamicList[0];
string second = DynamicList[1];
Assert.AreEqual("one", first, "The string entered first should be at position 0.");
Assert.AreEqual("three", second, "The entered third should be at position 1 after the removal at 1.");
}
/// <summary>
/// The count test after removing non existing element from list should pass test.
/// </summary>
[TestMethod]
public void Count_TestAfterRemovingNonExistingElementFromList_ShouldPassTest()
{
DynamicList.Add("one");
DynamicList.Add("two");
this.AddCleanupAction(() => DynamicList.Remove("one"));
this.AddCleanupAction(() => DynamicList.Remove("two"));
DynamicList.Remove("three");
int count = DynamicList.Count;
Assert.AreEqual(
2,
count,
"The count of the list should remain unchanged after trying to remove a non-existing element.");
}
/// <summary>
/// The remove test return value after removing non existing element from list should pass test.
/// </summary>
[TestMethod]
public void Remove_TestReturnValueAfterRemovingNonExistingElementFromList_ShouldPassTest()
{
DynamicList.Add("one");
DynamicList.Add("two");
this.AddCleanupAction(() => DynamicList.Remove("one"));
this.AddCleanupAction(() => DynamicList.Remove("two"));
int index = DynamicList.Remove("three");
Assert.AreEqual(-1, index, "The returned index of a non-existing element should be -1.");
}
/// <summary>
/// The count test after removing element from list should pass test.
/// </summary>
[TestMethod]
public void Count_TestAfterRemovingElementFromList_ShouldPassTest()
{
DynamicList.Add("first");
DynamicList.Add("second");
DynamicList.Add("third");
this.AddCleanupAction(() => DynamicList.Remove("first"));
this.AddCleanupAction(() => DynamicList.Remove("third"));
DynamicList.Remove("second");
int count = DynamicList.Count;
Assert.AreEqual(2, count, "The count of elements of the list should be 2 after removing an element.");
}
/// <summary>
/// The remove test return value after removing element from list should pass test.
/// </summary>
[TestMethod]
public void Remove_TestReturnValueAfterRemovingElementFromList_ShouldPassTest()
{
DynamicList.Add("first");
DynamicList.Add("second");
DynamicList.Add("third");
this.AddCleanupAction(() => DynamicList.Remove("first"));
this.AddCleanupAction(() => DynamicList.Remove("third"));
int index = DynamicList.Remove("second");
Assert.AreEqual(1, index, "The index of the removed element should be 1.");
}
/// <summary>
/// The remove test remaining elements positions after remove should pass test.
/// </summary>
[TestMethod]
public void Remove_TestRemainingElementsPositionsAfterRemove_ShouldPassTest()
{
DynamicList.Add("first");
DynamicList.Add("second");
DynamicList.Add("third");
this.AddCleanupAction(() => DynamicList.Remove("first"));
this.AddCleanupAction(() => DynamicList.Remove("third"));
DynamicList.Remove("second");
string first = DynamicList[0];
string second = DynamicList[1];
Assert.AreEqual("first", first, "The first element of the list should be the first one entered.");
Assert.AreEqual(
"third",
second,
"The second element in the list should be the third one entered after removing the second element.");
}
/// <summary>
/// The index of test on non existing element should pass test.
/// </summary>
[TestMethod]
public void IndexOf_TestOnNonExistingElement_ShouldPassTest()
{
DynamicList.Add("first");
DynamicList.Add("second");
this.AddCleanupAction(() => DynamicList.Remove("first"));
this.AddCleanupAction(() => DynamicList.Remove("second"));
int index = DynamicList.IndexOf("third");
Assert.AreEqual(-1, index, "The return value when searching for a non-existing element should be -1.");
}
/// <summary>
/// The index of test on existing element should pass test.
/// </summary>
[TestMethod]
public void IndexOf_TestOnExistingElement_ShouldPassTest()
{
DynamicList.Add("first");
DynamicList.Add("second");
this.AddCleanupAction(() => DynamicList.Remove("first"));
this.AddCleanupAction(() => DynamicList.Remove("second"));
int index = DynamicList.IndexOf("second");
Assert.AreEqual(1, index, "The index of the searched element should be 1.");
}
/// <summary>
/// The contains test on non existing element should pass test.
/// </summary>
[TestMethod]
public void Contains_TestOnNonExisingElement_ShouldPassTest()
{
DynamicList.Add("first");
DynamicList.Add("second");
this.AddCleanupAction(() => DynamicList.Remove("first"));
this.AddCleanupAction(() => DynamicList.Remove("second"));
bool isFound = DynamicList.Contains("third");
Assert.IsFalse(isFound, "The element is not in the list and Contains should return false.");
}
/// <summary>
/// The contains test on existing element should pass test.
/// </summary>
[TestMethod]
public void Contains_TestOnExisingElement_ShouldPassTest()
{
DynamicList.Add("first");
DynamicList.Add("second");
this.AddCleanupAction(() => DynamicList.Remove("first"));
this.AddCleanupAction(() => DynamicList.Remove("second"));
bool isFound = DynamicList.Contains("second");
Assert.IsTrue(isFound, "The element is in the list and Contains should return true.");
}
/// <summary>
/// The test cleanup removes any values added to the DynamicList for testing purposes.
/// </summary>
[TestCleanup]
public void Cleanup()
{
this.TestCleanup();
}
/// <summary>
/// The class cleanup - returns the DynamicList object to not-initialized state.
/// </summary>
[ClassCleanup]
public static void ClassCleanup()
{
DynamicList.Clear();
}
}
}