**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / Test / System.Collections / CollectionBaseTest.cs
blob0cda043aac044b3c0a93f54c8d1309a52d28c0ab
1 //
2 // System.Collections.CollectionBase
3 // Test suite for System.Collections.CollectionBase
4 //
5 // Authors:
6 // Nick D. Drochak II
7 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 //
9 // (C) 2001 Nick D. Drochak II
10 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
14 using System;
15 using System.Collections;
16 using NUnit.Framework;
18 namespace MonoTests.System.Collections
21 [TestFixture]
22 public class CollectionBaseTest : Assertion
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()
53 IList listObj;
54 listObj = this;
55 listObj.Add(null);
58 // This constructor puts consecutive integers into the list
59 public ConcreteCollection(int i) {
60 IList listObj;
61 listObj = this;
63 int j;
64 for (j = 0; j< i; j++) {
65 listObj.Add(j);
69 void CheckIfThrow ()
71 if (mustThrowException > 0) {
72 mustThrowException--;
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)
81 IList listObj;
82 listObj = this;
83 return (int) listObj[index];
86 protected override void OnValidate (object value) {
87 this.onValidateFired = true;
88 CheckIfThrow ();
89 base.OnValidate (value);
92 // Mark the flag if this hook is fired
93 protected override void OnClear() {
94 this.onClearFired = true;
95 CheckIfThrow ();
98 // Mark the flag if this hook is fired
99 protected override void OnClearComplete()
101 this.onClearCompleteFired = true;
102 CheckIfThrow ();
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;
110 CheckIfThrow ();
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;
118 CheckIfThrow ();
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;
126 CheckIfThrow ();
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;
134 CheckIfThrow ();
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;
143 CheckIfThrow ();
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;
152 CheckIfThrow ();
155 public IList BaseList {
156 get { return base.List; }
158 } // public class ConcreteCollection
160 // Check the count property
161 [Test]
162 public void Count() {
163 ConcreteCollection myCollection;
164 myCollection = new ConcreteCollection(4);
165 Assert(4 == myCollection.Count);
168 // Make sure GetEnumerator returns an object
169 [Test]
170 public void GetEnumerator() {
171 ConcreteCollection myCollection;
172 myCollection = new ConcreteCollection(4);
173 Assert(null != myCollection.GetEnumerator());
176 // OnValid disallows nulls
177 [Test]
178 [ExpectedException(typeof(ArgumentNullException))]
179 public void OnValid() {
180 ConcreteCollection myCollection;
181 myCollection = new ConcreteCollection();
184 // Test various Insert paths
185 [Test]
186 public void Insert() {
187 ConcreteCollection myCollection;
188 int numberOfItems;
189 numberOfItems = 3;
190 // The constructor inserts
191 myCollection = new ConcreteCollection(numberOfItems);
192 Assert(myCollection.onInsertFired);
193 Assert(myCollection.onInsertCompleteFired);
195 // Using the IList interface, check inserts in the middle
196 IList listObj = myCollection;
197 listObj.Insert(1, 9);
198 Assert(myCollection.onInsertIndex == 1);
199 Assert(myCollection.onInsertCompleteIndex == 1);
200 Assert(myCollection.PeekAt(1) == 9);
203 // Test Clear and it's hooks
204 [Test]
205 public void Clear()
207 ConcreteCollection myCollection;
208 int numberOfItems;
209 numberOfItems = 1;
210 myCollection = new ConcreteCollection(numberOfItems);
211 myCollection.Clear();
212 Assert(myCollection.Count == 0);
213 Assert(myCollection.onClearFired);
214 Assert(myCollection.onClearCompleteFired);
217 // Test RemoveAt, other removes and the hooks
218 [Test]
219 public void Remove()
221 ConcreteCollection myCollection;
222 int numberOfItems;
223 numberOfItems = 3;
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(myCollection.PeekAt(1) == 2);
232 Assert(myCollection.onRemoveFired);
233 Assert(myCollection.onRemoveIndex == 1);
234 Assert(myCollection.onRemoveCompleteFired);
235 Assert(myCollection.onRemoveCompleteIndex == 1);
236 IList listObj = myCollection;
237 listObj.Remove(0);
238 // Confirm parameters are being passed to the hooks
239 Assert(myCollection.onRemoveIndex == 0);
240 Assert(myCollection.onRemoveCompleteIndex == 0);
243 // Test the random access feature
244 [Test]
245 public void Set()
247 ConcreteCollection myCollection;
248 int numberOfItems;
249 numberOfItems = 3;
250 myCollection = new ConcreteCollection(numberOfItems);
251 IList listObj = myCollection;
252 listObj[0] = 99;
253 Assert((int) listObj[0] == 99);
254 Assert(myCollection.onSetFired);
255 Assert(myCollection.onSetCompleteFired);
256 Assert(myCollection.onSetOldValue == 0);
257 Assert(myCollection.onSetCompleteOldValue == 0);
258 Assert(myCollection.onSetNewValue == 99);
259 Assert(myCollection.onSetCompleteNewValue == 99);
262 [Test]
263 public void InsertComplete_Add ()
265 ConcreteCollection coll = new ConcreteCollection (0);
266 coll.mustThrowException = 1;
268 try {
269 coll.BaseList.Add (0);
270 } catch {
272 AssertEquals (0, coll.Count);
275 [Test]
276 [ExpectedException (typeof (ArgumentOutOfRangeException))]
277 public void ValidateCalled ()
279 ConcreteCollection coll = new ConcreteCollection (0);
280 coll.mustThrowException = 1;
282 try {
283 coll.BaseList [5] = 8888;
284 } catch (ArgumentOutOfRangeException) {
285 throw;
286 } finally {
287 AssertEquals (false, coll.onValidateFired);
291 [Test]
292 public void SetCompleteCalled ()
294 ConcreteCollection coll = new ConcreteCollection (0);
296 coll.BaseList.Add (88);
297 coll.mustThrowException = 1;
298 try {
299 coll.BaseList [0] = 11;
300 } catch {
301 } finally {
302 AssertEquals (false, coll.onSetCompleteFired);
306 [Test]
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;
318 try {
319 coll.BaseList [0] = 11;
320 throwsException = false;
321 } catch {
322 } finally {
323 Assert (throwsException);
324 Assert (coll.onValidateFired);
325 Assert (coll.onSetFired);
326 Assert (coll.onSetCompleteFired);
327 AssertEquals (88, coll.BaseList [0]);
331 [Test]
332 [ExpectedException (typeof (ArgumentException))]
333 public void InvalidRemove ()
335 ConcreteCollection coll = new ConcreteCollection (0);
336 coll.BaseList.Remove (10);