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