Adding ArrayListTest to AllTests
[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 // Author:\r
6 //    Nick D. Drochak II\r
7 //\r
8 // (C) 2001 Nick D. Drochak II\r
9 //\r
10 \r
11 \r
12 using System;\r
13 using System.Collections;\r
14 using NUnit.Framework;\r
15 \r
16 namespace MonoTests.System.Collections\r
17 {\r
18 \r
19 public class CollectionBaseTest : TestCase      \r
20 {\r
21         public CollectionBaseTest () : base ("System.Collection.CollectionBase testsuite") {}\r
22         public CollectionBaseTest (String name) : base (name) {}\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 \r
48                 // This constructor is used to test OnValid()\r
49                 public ConcreteCollection()     \r
50                 {\r
51                         IList listObj;\r
52                         listObj = this;\r
53                         listObj.Add(null);\r
54                 }\r
55 \r
56                 // This constructor puts consecutive integers into the list\r
57                 public ConcreteCollection(int i) {\r
58                         IList listObj;\r
59                         listObj = this;\r
60 \r
61                         int j;\r
62                         for (j = 0; j< i; j++) {\r
63                                 listObj.Add(j);\r
64                         }\r
65                 }\r
66 \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
69                 {\r
70                         IList listObj;\r
71                         listObj = this;\r
72                         return (int) listObj[index];\r
73                 }\r
74 \r
75                 // Mark the flag if this hook is fired\r
76                 protected override void OnClear() {\r
77                         this.onClearFired = true;\r
78                 }\r
79 \r
80                 // Mark the flag if this hook is fired\r
81                 protected override void OnClearComplete() \r
82                 {\r
83                         this.onClearCompleteFired = true;\r
84                 }\r
85 \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
88                 {\r
89                         this.onInsertFired = true;\r
90                         this.onInsertIndex = index;\r
91                 }\r
92 \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
95                 {\r
96                         this.onInsertCompleteFired = true;\r
97                         this.onInsertCompleteIndex = index;\r
98                 }\r
99                 \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
102                 {\r
103                         this.onRemoveFired = true;\r
104                         this.onRemoveIndex = index;\r
105                 }\r
106                 \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
109                 {\r
110                         this.onRemoveCompleteFired = true;\r
111                         this.onRemoveCompleteIndex = index;\r
112                 }\r
113                 \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
116                 {\r
117                         this.onSetFired = true;\r
118                         this.onSetOldValue = (int) oldValue;\r
119                         this.onSetNewValue = (int) newValue;\r
120                 }\r
121                 \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
124                 {\r
125                         this.onSetCompleteFired = true;\r
126                         this.onSetCompleteOldValue = (int) oldValue;\r
127                         this.onSetCompleteNewValue = (int) newValue;\r
128                 }\r
129         }  // public class ConcreteCollection\r
130 \r
131         public static ITest Suite {\r
132                 get {\r
133                         return new TestSuite (typeof(CollectionBaseTest));\r
134                 }\r
135         }\r
136 \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
142         }\r
143 \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
149         }\r
150 \r
151         // OnValid disallows nulls\r
152         public void TestOnValid() {\r
153                 ConcreteCollection myCollection;\r
154                 try {\r
155                         myCollection = new ConcreteCollection();\r
156                 }\r
157                 catch (ArgumentNullException) {\r
158                 }\r
159         }\r
160 \r
161         // Test various Insert paths\r
162         public void TestInsert() {\r
163                 ConcreteCollection myCollection;\r
164                 int numberOfItems;\r
165                 numberOfItems = 3;\r
166                 // The constructor inserts\r
167                 myCollection = new ConcreteCollection(numberOfItems);\r
168                 Assert(myCollection.onInsertFired);\r
169                 Assert(myCollection.onInsertCompleteFired);\r
170 \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
177         }\r
178 \r
179         // Test Clear and it's hooks\r
180         public void TestClear() \r
181         {\r
182                 ConcreteCollection myCollection;\r
183                 int numberOfItems;\r
184                 numberOfItems = 1;\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
190         }\r
191 \r
192         // Test RemoveAt, other removes and the hooks\r
193         public void TestRemove() \r
194         {\r
195                 ConcreteCollection myCollection;\r
196                 int numberOfItems;\r
197                 numberOfItems = 3;\r
198                 // Set up a test collection\r
199                 myCollection = new ConcreteCollection(numberOfItems);\r
200 \r
201                 // The list is 0-based.  So if we remove the second one\r
202                 myCollection.RemoveAt(1);\r
203 \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
211                 listObj.Remove(0);\r
212                 // Confirm parameters are being passed to the hooks\r
213                 Assert(myCollection.onRemoveIndex == 0);\r
214                 Assert(myCollection.onRemoveCompleteIndex == 0);\r
215         }\r
216 \r
217         // Test the random access feature\r
218         public void TestSet() \r
219         {\r
220                 ConcreteCollection myCollection;\r
221                 int numberOfItems;\r
222                 numberOfItems = 3;\r
223                 myCollection = new ConcreteCollection(numberOfItems);\r
224                 IList listObj = myCollection;\r
225                 listObj[0] = 99;\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
233         }\r
234 }\r
235 \r
236 }\r