2 // System.Collections.CollectionBase
\r
3 // Test suite for System.Collections.CollectionBase
\r
6 // Nick D. Drochak II
\r
8 // (C) 2001 Nick D. Drochak II
\r
13 using System.Collections;
\r
14 using NUnit.Framework;
\r
16 namespace MonoTests.System.Collections
\r
19 public class CollectionBaseTest : TestCase
\r
21 public CollectionBaseTest () : base ("System.Collection.CollectionBase testsuite") {}
\r
22 public CollectionBaseTest (String name) : base (name) {}
\r
24 // We need a concrete class to test the abstract base class
\r
25 public class ConcreteCollection : CollectionBase
\r
27 // These fields are used as markers to test the On* hooks.
\r
28 public bool onClearFired;
\r
29 public bool onClearCompleteFired;
\r
31 public bool onInsertFired;
\r
32 public int onInsertIndex;
\r
33 public bool onInsertCompleteFired;
\r
34 public int onInsertCompleteIndex;
\r
36 public bool onRemoveFired;
\r
37 public int onRemoveIndex;
\r
38 public bool onRemoveCompleteFired;
\r
39 public int onRemoveCompleteIndex;
\r
41 public bool onSetFired;
\r
42 public int onSetOldValue;
\r
43 public int onSetNewValue;
\r
44 public bool onSetCompleteFired;
\r
45 public int onSetCompleteOldValue;
\r
46 public int onSetCompleteNewValue;
\r
48 // This constructor is used to test OnValid()
\r
49 public ConcreteCollection()
\r
56 // This constructor puts consecutive integers into the list
\r
57 public ConcreteCollection(int i) {
\r
62 for (j = 0; j< i; j++) {
\r
67 // A helper method to look at a value in the list at a specific index
\r
68 public int PeekAt(int index)
\r
72 return (int) listObj[index];
\r
75 // Mark the flag if this hook is fired
\r
76 protected override void OnClear() {
\r
77 this.onClearFired = true;
\r
80 // Mark the flag if this hook is fired
\r
81 protected override void OnClearComplete()
\r
83 this.onClearCompleteFired = true;
\r
86 // Mark the flag, and save the paramter if this hook is fired
\r
87 protected override void OnInsert(int index, object value)
\r
89 this.onInsertFired = true;
\r
90 this.onInsertIndex = index;
\r
93 // Mark the flag, and save the paramter if this hook is fired
\r
94 protected override void OnInsertComplete(int index, object value)
\r
96 this.onInsertCompleteFired = true;
\r
97 this.onInsertCompleteIndex = index;
\r
100 // Mark the flag, and save the paramter if this hook is fired
\r
101 protected override void OnRemove(int index, object value)
\r
103 this.onRemoveFired = true;
\r
104 this.onRemoveIndex = index;
\r
107 // Mark the flag, and save the paramter if this hook is fired
\r
108 protected override void OnRemoveComplete(int index, object value)
\r
110 this.onRemoveCompleteFired = true;
\r
111 this.onRemoveCompleteIndex = index;
\r
114 // Mark the flag, and save the paramters if this hook is fired
\r
115 protected override void OnSet(int index, object oldValue, object newValue)
\r
117 this.onSetFired = true;
\r
118 this.onSetOldValue = (int) oldValue;
\r
119 this.onSetNewValue = (int) newValue;
\r
122 // Mark the flag, and save the paramters if this hook is fired
\r
123 protected override void OnSetComplete(int index, object oldValue, object newValue)
\r
125 this.onSetCompleteFired = true;
\r
126 this.onSetCompleteOldValue = (int) oldValue;
\r
127 this.onSetCompleteNewValue = (int) newValue;
\r
129 } // public class ConcreteCollection
\r
131 public static ITest Suite {
\r
133 return new TestSuite (typeof(CollectionBaseTest));
\r
137 // Check the count property
\r
138 public void TestCount() {
\r
139 ConcreteCollection myCollection;
\r
140 myCollection = new ConcreteCollection(4);
\r
141 Assert(4 == myCollection.Count);
\r
144 // Make sure GetEnumerator returns an object
\r
145 public void TestGetEnumerator() {
\r
146 ConcreteCollection myCollection;
\r
147 myCollection = new ConcreteCollection(4);
\r
148 Assert(null != myCollection.GetEnumerator());
\r
151 // OnValid disallows nulls
\r
152 public void TestOnValid() {
\r
153 ConcreteCollection myCollection;
\r
155 myCollection = new ConcreteCollection();
\r
157 catch (ArgumentNullException) {
\r
161 // Test various Insert paths
\r
162 public void TestInsert() {
\r
163 ConcreteCollection myCollection;
\r
166 // The constructor inserts
\r
167 myCollection = new ConcreteCollection(numberOfItems);
\r
168 Assert(myCollection.onInsertFired);
\r
169 Assert(myCollection.onInsertCompleteFired);
\r
171 // Using the IList interface, check inserts in the middle
\r
172 IList listObj = myCollection;
\r
173 listObj.Insert(1, 9);
\r
174 Assert(myCollection.onInsertIndex == 1);
\r
175 Assert(myCollection.onInsertCompleteIndex == 1);
\r
176 Assert(myCollection.PeekAt(1) == 9);
\r
179 // Test Clear and it's hooks
\r
180 public void TestClear()
\r
182 ConcreteCollection myCollection;
\r
185 myCollection = new ConcreteCollection(numberOfItems);
\r
186 myCollection.Clear();
\r
187 Assert(myCollection.Count == 0);
\r
188 Assert(myCollection.onClearFired);
\r
189 Assert(myCollection.onClearCompleteFired);
\r
192 // Test RemoveAt, other removes and the hooks
\r
193 public void TestRemove()
\r
195 ConcreteCollection myCollection;
\r
198 // Set up a test collection
\r
199 myCollection = new ConcreteCollection(numberOfItems);
\r
201 // The list is 0-based. So if we remove the second one
\r
202 myCollection.RemoveAt(1);
\r
204 // We should see the original third one in it's place
\r
205 Assert(myCollection.PeekAt(1) == 2);
\r
206 Assert(myCollection.onRemoveFired);
\r
207 Assert(myCollection.onRemoveIndex == 1);
\r
208 Assert(myCollection.onRemoveCompleteFired);
\r
209 Assert(myCollection.onRemoveCompleteIndex == 1);
\r
210 IList listObj = myCollection;
\r
212 // Confirm parameters are being passed to the hooks
\r
213 Assert(myCollection.onRemoveIndex == 0);
\r
214 Assert(myCollection.onRemoveCompleteIndex == 0);
\r
217 // Test the random access feature
\r
218 public void TestSet()
\r
220 ConcreteCollection myCollection;
\r
223 myCollection = new ConcreteCollection(numberOfItems);
\r
224 IList listObj = myCollection;
\r
226 Assert((int) listObj[0] == 99);
\r
227 Assert(myCollection.onSetFired);
\r
228 Assert(myCollection.onSetCompleteFired);
\r
229 Assert(myCollection.onSetOldValue == 0);
\r
230 Assert(myCollection.onSetCompleteOldValue == 0);
\r
231 Assert(myCollection.onSetNewValue == 99);
\r
232 Assert(myCollection.onSetCompleteNewValue == 99);
\r