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
|
// -----------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// -----------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace System.UnitTesting
{
public static class TestUtilities
{
public static void CheckICollectionOfTConformance<T>(ICollection<T> list, T a, T b, T c, T d)
{
list.Clear();
EnumerableAssert.AreEqual(list);
Assert.IsFalse(list.IsReadOnly, "The list should not report being read-only for these tests to work");
Assert.IsFalse(list.Contains(a), "Contains should fail for anything when the collection is empty");
Assert.IsFalse(list.Remove(a), "Remove should fail on anything when the collection is empty");
list.Add(a);
EnumerableAssert.AreEqual(list, a);
list.Add(b);
EnumerableAssert.AreEqual(list, a, b);
list.Add(c);
EnumerableAssert.AreEqual(list, a, b, c);
list.Remove(b);
EnumerableAssert.AreEqual(list, a, c);
list.Remove(c);
EnumerableAssert.AreEqual(list, a);
list.Remove(a);
EnumerableAssert.AreEqual(list);
list.Add(a); list.Add(b); list.Add(c);
list.Clear();
EnumerableAssert.AreEqual(list);
list.Clear();
EnumerableAssert.AreEqual(list);
list.Add(d); list.Add(c); list.Add(b); list.Add(a);
T[] destination = new T[5];
list.CopyTo(destination, 0);
EnumerableAssert.AreEqual(destination, d, c, b, a, default(T));
}
public static void CheckIListOfTConformance<T>(IList<T> list, T a, T b, T c, T d)
{
CheckICollectionOfTConformance(list, a, b, c, d);
list.Clear();
list.Insert(0, d);
list.Insert(0, a);
list.Insert(1, c);
list.Insert(1, b);
CompareListContents(list, a, b, c, d);
list[1] = a;
CompareListContents(list, a, a, c, d);
list.RemoveAt(2);
CompareListContents(list, a, a, d);
Assert.AreEqual(2, list.IndexOf(d), "Expected indexof to return the correct location of {0}", d);
Assert.AreEqual(-1, list.IndexOf(b), "{0} should not be found in the collection", b);
Assert.AreEqual(-1, list.IndexOf(c), "{0} should not be found in the collection", c);
}
public static void CompareListContents<T>(IList<T> list, params object[] values)
{
EnumerableAssert.AreEqual(list, values);
for (var index = 0; index < values.Length; index++)
{
Assert.AreEqual(values[index], list[index],
"List should return true for Contains on every element, index {0}, length {1}", index, values[index]);
}
}
}
}
|