2 // System.Collections.CollectionBase
\r
3 // Test suite for System.Collections.CollectionBase
\r
6 // Nick D. Drochak II
\r
7 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
\r
9 // (C) 2001 Nick D. Drochak II
\r
10 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
\r
15 using System.Collections;
\r
16 using NUnit.Framework;
\r
18 namespace MonoTests.System.Collections
\r
22 public class CollectionBaseTest : Assertion
\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
47 public int mustThrowException;
\r
48 public bool onValidateFired;
\r
50 // This constructor is used to test OnValid()
\r
51 public ConcreteCollection()
\r
58 // This constructor puts consecutive integers into the list
\r
59 public ConcreteCollection(int i) {
\r
64 for (j = 0; j< i; j++) {
\r
69 void CheckIfThrow ()
\r
71 if (mustThrowException > 0) {
\r
72 mustThrowException--;
\r
73 if (mustThrowException == 0)
\r
74 throw new Exception ();
\r
78 // A helper method to look at a value in the list at a specific index
\r
79 public int PeekAt(int index)
\r
83 return (int) listObj[index];
\r
86 protected override void OnValidate (object value) {
\r
87 this.onValidateFired = true;
\r
89 base.OnValidate (value);
\r
92 // Mark the flag if this hook is fired
\r
93 protected override void OnClear() {
\r
94 this.onClearFired = true;
\r
98 // Mark the flag if this hook is fired
\r
99 protected override void OnClearComplete()
\r
101 this.onClearCompleteFired = true;
\r
105 // Mark the flag, and save the paramter if this hook is fired
\r
106 protected override void OnInsert(int index, object value)
\r
108 this.onInsertFired = true;
\r
109 this.onInsertIndex = index;
\r
113 // Mark the flag, and save the paramter if this hook is fired
\r
114 protected override void OnInsertComplete(int index, object value)
\r
116 this.onInsertCompleteFired = true;
\r
117 this.onInsertCompleteIndex = index;
\r
121 // Mark the flag, and save the paramter if this hook is fired
\r
122 protected override void OnRemove(int index, object value)
\r
124 this.onRemoveFired = true;
\r
125 this.onRemoveIndex = index;
\r
129 // Mark the flag, and save the paramter if this hook is fired
\r
130 protected override void OnRemoveComplete(int index, object value)
\r
132 this.onRemoveCompleteFired = true;
\r
133 this.onRemoveCompleteIndex = index;
\r
137 // Mark the flag, and save the paramters if this hook is fired
\r
138 protected override void OnSet(int index, object oldValue, object newValue)
\r
140 this.onSetFired = true;
\r
141 this.onSetOldValue = (int) oldValue;
\r
142 this.onSetNewValue = (int) newValue;
\r
146 // Mark the flag, and save the paramters if this hook is fired
\r
147 protected override void OnSetComplete(int index, object oldValue, object newValue)
\r
149 this.onSetCompleteFired = true;
\r
150 this.onSetCompleteOldValue = (int) oldValue;
\r
151 this.onSetCompleteNewValue = (int) newValue;
\r
155 public IList BaseList {
\r
156 get { return base.List; }
\r
158 } // public class ConcreteCollection
\r
160 // Check the count property
\r
162 public void Count() {
\r
163 ConcreteCollection myCollection;
\r
164 myCollection = new ConcreteCollection(4);
\r
165 Assert(4 == myCollection.Count);
\r
168 // Make sure GetEnumerator returns an object
\r
170 public void GetEnumerator() {
\r
171 ConcreteCollection myCollection;
\r
172 myCollection = new ConcreteCollection(4);
\r
173 Assert(null != myCollection.GetEnumerator());
\r
176 // OnValid disallows nulls
\r
178 [ExpectedException(typeof(ArgumentNullException))]
\r
179 public void OnValid() {
\r
180 ConcreteCollection myCollection;
\r
181 myCollection = new ConcreteCollection();
\r
184 // Test various Insert paths
\r
186 public void Insert() {
\r
187 ConcreteCollection myCollection;
\r
190 // The constructor inserts
\r
191 myCollection = new ConcreteCollection(numberOfItems);
\r
192 Assert(myCollection.onInsertFired);
\r
193 Assert(myCollection.onInsertCompleteFired);
\r
195 // Using the IList interface, check inserts in the middle
\r
196 IList listObj = myCollection;
\r
197 listObj.Insert(1, 9);
\r
198 Assert(myCollection.onInsertIndex == 1);
\r
199 Assert(myCollection.onInsertCompleteIndex == 1);
\r
200 Assert(myCollection.PeekAt(1) == 9);
\r
203 // Test Clear and it's hooks
\r
205 public void Clear()
\r
207 ConcreteCollection myCollection;
\r
210 myCollection = new ConcreteCollection(numberOfItems);
\r
211 myCollection.Clear();
\r
212 Assert(myCollection.Count == 0);
\r
213 Assert(myCollection.onClearFired);
\r
214 Assert(myCollection.onClearCompleteFired);
\r
217 // Test RemoveAt, other removes and the hooks
\r
219 public void Remove()
\r
221 ConcreteCollection myCollection;
\r
224 // Set up a test collection
\r
225 myCollection = new ConcreteCollection(numberOfItems);
\r
227 // The list is 0-based. So if we remove the second one
\r
228 myCollection.RemoveAt(1);
\r
230 // We should see the original third one in it's place
\r
231 Assert(myCollection.PeekAt(1) == 2);
\r
232 Assert(myCollection.onRemoveFired);
\r
233 Assert(myCollection.onRemoveIndex == 1);
\r
234 Assert(myCollection.onRemoveCompleteFired);
\r
235 Assert(myCollection.onRemoveCompleteIndex == 1);
\r
236 IList listObj = myCollection;
\r
238 // Confirm parameters are being passed to the hooks
\r
239 Assert(myCollection.onRemoveIndex == 0);
\r
240 Assert(myCollection.onRemoveCompleteIndex == 0);
\r
243 // Test the random access feature
\r
247 ConcreteCollection myCollection;
\r
250 myCollection = new ConcreteCollection(numberOfItems);
\r
251 IList listObj = myCollection;
\r
253 Assert((int) listObj[0] == 99);
\r
254 Assert(myCollection.onSetFired);
\r
255 Assert(myCollection.onSetCompleteFired);
\r
256 Assert(myCollection.onSetOldValue == 0);
\r
257 Assert(myCollection.onSetCompleteOldValue == 0);
\r
258 Assert(myCollection.onSetNewValue == 99);
\r
259 Assert(myCollection.onSetCompleteNewValue == 99);
\r
263 public void InsertComplete_Add ()
\r
265 ConcreteCollection coll = new ConcreteCollection (0);
\r
266 coll.mustThrowException = 1;
\r
269 coll.BaseList.Add (0);
\r
272 AssertEquals (0, coll.Count);
\r
276 [ExpectedException (typeof (ArgumentOutOfRangeException))]
\r
277 public void ValidateCalled ()
\r
279 ConcreteCollection coll = new ConcreteCollection (0);
\r
280 coll.mustThrowException = 1;
\r
283 coll.BaseList [5] = 8888;
\r
284 } catch (ArgumentOutOfRangeException) {
\r
287 AssertEquals (false, coll.onValidateFired);
\r
292 public void SetCompleteCalled ()
\r
294 ConcreteCollection coll = new ConcreteCollection (0);
\r
296 coll.BaseList.Add (88);
\r
297 coll.mustThrowException = 1;
\r
299 coll.BaseList [0] = 11;
\r
302 AssertEquals (false, coll.onSetCompleteFired);
\r
307 public void SetCompleteUndo ()
\r
309 ConcreteCollection coll = new ConcreteCollection (0);
\r
311 bool throwsException = true;
\r
313 coll.BaseList.Add (88);
\r
314 coll.onValidateFired = false;
\r
315 coll.onInsertFired = false;
\r
316 coll.onSetCompleteFired = false;
\r
317 coll.mustThrowException = 3;
\r
319 coll.BaseList [0] = 11;
\r
320 throwsException = false;
\r
323 Assert (throwsException);
\r
324 Assert (coll.onValidateFired);
\r
325 Assert (coll.onSetFired);
\r
326 Assert (coll.onSetCompleteFired);
\r
327 AssertEquals (88, coll.BaseList [0]);
\r
332 [ExpectedException (typeof (ArgumentException))]
\r
333 public void InvalidRemove ()
\r
335 ConcreteCollection coll = new ConcreteCollection (0);
\r
336 coll.BaseList.Remove (10);
\r