2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / Test / System.Collections / CollectionBaseTest.cs
1 //\r
2 // System.Collections.CollectionBase\r
3 // Test suite for System.Collections.CollectionBase\r
4 //\r
5 // Authors:\r
6 //    Nick D. Drochak II\r
7 //    Gonzalo Paniagua Javier (gonzalo@ximian.com)\r
8 //\r
9 // (C) 2001 Nick D. Drochak II\r
10 // (c) 2003 Ximian, Inc. (http://www.ximian.com)\r
11 //\r
12 \r
13 \r
14 using System;\r
15 using System.Collections;\r
16 using NUnit.Framework;\r
17 \r
18 namespace MonoTests.System.Collections\r
19 {\r
20 \r
21 [TestFixture]\r
22 public class CollectionBaseTest : Assertion\r
23 {\r
24         // We need a concrete class to test the abstract base class\r
25         public class ConcreteCollection : CollectionBase \r
26         {\r
27                 // These fields are used as markers to test the On* hooks.\r
28                 public bool onClearFired;\r
29                 public bool onClearCompleteFired;\r
30 \r
31                 public bool onInsertFired;\r
32                 public int onInsertIndex;\r
33                 public bool onInsertCompleteFired;\r
34                 public int onInsertCompleteIndex;\r
35 \r
36                 public bool onRemoveFired;\r
37                 public int onRemoveIndex;\r
38                 public bool onRemoveCompleteFired;\r
39                 public int onRemoveCompleteIndex;\r
40 \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
49 \r
50                 // This constructor is used to test OnValid()\r
51                 public ConcreteCollection()     \r
52                 {\r
53                         IList listObj;\r
54                         listObj = this;\r
55                         listObj.Add(null);\r
56                 }\r
57 \r
58                 // This constructor puts consecutive integers into the list\r
59                 public ConcreteCollection(int i) {\r
60                         IList listObj;\r
61                         listObj = this;\r
62 \r
63                         int j;\r
64                         for (j = 0; j< i; j++) {\r
65                                 listObj.Add(j);\r
66                         }\r
67                 }\r
68 \r
69                 void CheckIfThrow ()\r
70                 {\r
71                         if (mustThrowException > 0) {\r
72                                 mustThrowException--;\r
73                                 if (mustThrowException == 0)\r
74                                         throw new Exception ();\r
75                         }\r
76                 }\r
77                 \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
80                 {\r
81                         IList listObj;\r
82                         listObj = this;\r
83                         return (int) listObj[index];\r
84                 }\r
85 \r
86                 protected override void OnValidate (object value) {\r
87                         this.onValidateFired = true;\r
88                         CheckIfThrow ();\r
89                         base.OnValidate (value);\r
90                 }\r
91 \r
92                 // Mark the flag if this hook is fired\r
93                 protected override void OnClear() {\r
94                         this.onClearFired = true;\r
95                         CheckIfThrow ();\r
96                 }\r
97 \r
98                 // Mark the flag if this hook is fired\r
99                 protected override void OnClearComplete() \r
100                 {\r
101                         this.onClearCompleteFired = true;\r
102                         CheckIfThrow ();\r
103                 }\r
104 \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
107                 {\r
108                         this.onInsertFired = true;\r
109                         this.onInsertIndex = index;\r
110                         CheckIfThrow ();\r
111                 }\r
112 \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
115                 {\r
116                         this.onInsertCompleteFired = true;\r
117                         this.onInsertCompleteIndex = index;\r
118                         CheckIfThrow ();\r
119                 }\r
120                 \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
123                 {\r
124                         this.onRemoveFired = true;\r
125                         this.onRemoveIndex = index;\r
126                         CheckIfThrow ();\r
127                 }\r
128                 \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
131                 {\r
132                         this.onRemoveCompleteFired = true;\r
133                         this.onRemoveCompleteIndex = index;\r
134                         CheckIfThrow ();\r
135                 }\r
136                 \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
139                 {\r
140                         this.onSetFired = true;\r
141                         this.onSetOldValue = (int) oldValue;\r
142                         this.onSetNewValue = (int) newValue;\r
143                         CheckIfThrow ();\r
144                 }\r
145                 \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
148                 {\r
149                         this.onSetCompleteFired = true;\r
150                         this.onSetCompleteOldValue = (int) oldValue;\r
151                         this.onSetCompleteNewValue = (int) newValue;\r
152                         CheckIfThrow ();\r
153                 }\r
154 \r
155                 public IList BaseList {\r
156                         get { return base.List; }\r
157                 }\r
158         }  // public class ConcreteCollection\r
159 \r
160         // Check the count property\r
161         [Test]\r
162         public void Count() {\r
163                 ConcreteCollection myCollection;\r
164                 myCollection = new ConcreteCollection(4);\r
165                 Assert(4 == myCollection.Count);\r
166         }\r
167 \r
168         // Make sure GetEnumerator returns an object\r
169         [Test]\r
170         public void GetEnumerator() {\r
171                 ConcreteCollection myCollection;\r
172                 myCollection = new ConcreteCollection(4);\r
173                 Assert(null != myCollection.GetEnumerator());\r
174         }\r
175 \r
176         // OnValid disallows nulls\r
177         [Test]\r
178         [ExpectedException(typeof(ArgumentNullException))]\r
179         public void OnValid() {\r
180                 ConcreteCollection myCollection;\r
181                 myCollection = new ConcreteCollection();\r
182         }\r
183 \r
184         // Test various Insert paths\r
185         [Test]\r
186         public void Insert() {\r
187                 ConcreteCollection myCollection;\r
188                 int numberOfItems;\r
189                 numberOfItems = 3;\r
190                 // The constructor inserts\r
191                 myCollection = new ConcreteCollection(numberOfItems);\r
192                 Assert(myCollection.onInsertFired);\r
193                 Assert(myCollection.onInsertCompleteFired);\r
194 \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
201         }\r
202 \r
203         // Test Clear and it's hooks\r
204         [Test]\r
205         public void Clear() \r
206         {\r
207                 ConcreteCollection myCollection;\r
208                 int numberOfItems;\r
209                 numberOfItems = 1;\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
215         }\r
216 \r
217         // Test RemoveAt, other removes and the hooks\r
218         [Test]\r
219         public void Remove() \r
220         {\r
221                 ConcreteCollection myCollection;\r
222                 int numberOfItems;\r
223                 numberOfItems = 3;\r
224                 // Set up a test collection\r
225                 myCollection = new ConcreteCollection(numberOfItems);\r
226 \r
227                 // The list is 0-based.  So if we remove the second one\r
228                 myCollection.RemoveAt(1);\r
229 \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
237                 listObj.Remove(0);\r
238                 // Confirm parameters are being passed to the hooks\r
239                 Assert(myCollection.onRemoveIndex == 0);\r
240                 Assert(myCollection.onRemoveCompleteIndex == 0);\r
241         }\r
242 \r
243         // Test the random access feature\r
244         [Test]\r
245         public void Set() \r
246         {\r
247                 ConcreteCollection myCollection;\r
248                 int numberOfItems;\r
249                 numberOfItems = 3;\r
250                 myCollection = new ConcreteCollection(numberOfItems);\r
251                 IList listObj = myCollection;\r
252                 listObj[0] = 99;\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
260         }\r
261 \r
262         [Test]\r
263         public void InsertComplete_Add ()\r
264         {\r
265                 ConcreteCollection coll = new ConcreteCollection (0);\r
266                 coll.mustThrowException = 1;\r
267 \r
268                 try {\r
269                         coll.BaseList.Add (0);\r
270                 } catch {\r
271                 }\r
272                 AssertEquals (0, coll.Count);\r
273         }\r
274 \r
275         [Test]\r
276         [ExpectedException (typeof (ArgumentOutOfRangeException))]\r
277         public void ValidateCalled ()\r
278         {\r
279                 ConcreteCollection coll = new ConcreteCollection (0);\r
280                 coll.mustThrowException = 1;\r
281 \r
282                 try {\r
283                         coll.BaseList [5] = 8888;\r
284                 } catch (ArgumentOutOfRangeException) {\r
285                         throw;\r
286                 } finally {\r
287                         AssertEquals (false, coll.onValidateFired);\r
288                 }\r
289         }\r
290 \r
291         [Test]\r
292         public void SetCompleteCalled ()\r
293         {\r
294                 ConcreteCollection coll = new ConcreteCollection (0);\r
295 \r
296                 coll.BaseList.Add (88);\r
297                 coll.mustThrowException = 1;\r
298                 try {\r
299                         coll.BaseList [0] = 11;\r
300                 } catch {\r
301                 } finally {\r
302                         AssertEquals (false, coll.onSetCompleteFired);\r
303                 }\r
304         }\r
305 \r
306         [Test]\r
307         public void SetCompleteUndo ()\r
308         {\r
309                 ConcreteCollection coll = new ConcreteCollection (0);\r
310 \r
311                 bool throwsException = true;\r
312 \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
318                 try {\r
319                         coll.BaseList [0] = 11;\r
320                         throwsException = false;\r
321                 } catch {\r
322                 } finally {\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
328                 }\r
329         }\r
330 \r
331         [Test]\r
332         [ExpectedException (typeof (ArgumentException))]\r
333         public void InvalidRemove ()\r
334         {\r
335                 ConcreteCollection coll = new ConcreteCollection (0);\r
336                 coll.BaseList.Remove (10);\r
337         }\r
338 }\r
339 \r
340 }\r