Added license and copyright
[mono.git] / mcs / class / Microsoft.VisualBasic / Test / Microsoft.VisualBasic / CollectionTest.cs
1 // Collection.cs - NUnit Test Cases for Microsoft.VisualBasic.Collection
2 //
3 // Authors:
4 //   Chris J. Breisch (cjbreisch@altavista.net)
5 //   Martin Willemoes Hansen (mwh@sysrq.dk)
6 //
7 // (C) Chris J. Breisch
8 // (C) Martin Willemoes Hansen
9 // 
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using NUnit.Framework;
35 using System;
36 using Microsoft.VisualBasic;
37 using System.Collections;
38
39 namespace MonoTests.Microsoft.VisualBasic
40 {
41         [TestFixture]
42         public class CollectionTest : Assertion
43         {
44                 
45                 [SetUp]
46                 public void GetReady() {}
47
48                 [TearDown]
49                 public void Clean() {}
50
51                 // Test Constructor
52                 [Test]
53                 public void New ()
54                 {
55                         Collection c;
56
57                         c = new Collection();
58
59                         AssertNotNull("#N01", c);
60                         AssertEquals("#N02", 0, c.Count);
61                 }
62
63                 // Test Add method with Key == null
64                 [Test]
65                 public void AddNoKey ()
66                 {
67                         Collection c;
68
69                         c = new Collection();
70
71                         c.Add(typeof(int), null, null, null);
72                         c.Add(typeof(double), null, null, null);
73                         c.Add(typeof(string), null, null, null);
74                         
75                         AssertEquals("#ANK01", 3, c.Count);
76
77                         // Collection class is 1-based
78                         AssertEquals("#ANK02", typeof(string), c[3]);
79
80                 }
81
82                 // Test Add method with Key specified
83                 [Test]
84                 public void AddKey ()
85                 {
86                         Collection c;
87
88                         c = new Collection();
89
90                         c.Add("Baseball", "Base", null, null);
91                         c.Add("Football", "Foot", null, null);
92                         c.Add("Basketball", "Basket", null, null);
93                         c.Add("Volleyball", "Volley", null, null);
94
95                         AssertEquals("#AK01", 4, c.Count);
96
97                         // Collection class is 1-based
98                         AssertEquals("#AK02", "Baseball", c[1]);
99                         AssertEquals("#AK03", "Volleyball", c["Volley"]);
100
101                 }
102
103                 // Test Add method with Before specified and Key == null
104                 [Test]
105                 public void AddBeforeNoKey ()
106                 {
107                         Collection c;
108
109                         c = new Collection();
110
111                         c.Add(typeof(int), null, null, null);
112                         c.Add(typeof(double), null, 1, null);
113                         c.Add(typeof(string), null, 2, null);
114                         c.Add(typeof(object), null, 2, null);
115
116                         AssertEquals("#ABNK01", 4, c.Count);
117
118                         // Collection class is 1-based
119                         AssertEquals("#ABNK02", typeof(int), c[4]);
120                         AssertEquals("#ABNK03", typeof(double), c[1]);
121                         AssertEquals("#ABNK04", typeof(object), c[2]);
122
123                 }
124
125                 // Test Add method with Before and Key
126                 [Test]
127                 public void AddBeforeKey ()
128                 {
129                         Collection c;
130
131                         c = new Collection();
132
133                         c.Add("Baseball", "Base", null, null);
134                         c.Add("Football", "Foot", 1, null);
135                         c.Add("Basketball", "Basket", 1, null);
136                         c.Add("Volleyball", "Volley", 3, null);
137
138                         AssertEquals("#ABK01", 4, c.Count);
139                         AssertEquals("#ABK02", "Basketball", c[1]);
140                         AssertEquals("#ABK03", "Baseball", c[4]);
141                         AssertEquals("#ABK04", "Volleyball", c["Volley"]);
142                         AssertEquals("#ABK05", "Football", c["Foot"]);
143
144                 }
145
146                 // Test Add method with After specified and Key == null
147                 [Test]
148                 public void AddAfterNoKey ()
149                 {
150                         Collection c;
151
152                         c = new Collection();
153
154                         c.Add(typeof(int), null, null, 0);
155                         c.Add(typeof(double), null, null, 1);
156                         c.Add(typeof(string), null, null, 1);
157                         c.Add(typeof(object), null, null, 3);
158
159                         AssertEquals("#AANK01", 4, c.Count);
160
161                         // Collection class is 1-based
162                         AssertEquals("#AANK02", typeof(object), c[4]);
163                         AssertEquals("#AANK03", typeof(int), c[1]);
164                         AssertEquals("#AANK04", typeof(string), c[2]);
165
166                 }
167
168                 // Test Add method with After and Key
169                 [Test]
170                 public void AddAfterKey ()
171                 {
172                         Collection c;
173
174                         c = new Collection();
175
176                         c.Add("Baseball", "Base", null, 0);
177                         c.Add("Football", "Foot", null, 1);
178                         c.Add("Basketball", "Basket", null, 1);
179                         c.Add("Volleyball", "Volley", null, 2);
180
181                         AssertEquals("#AAK01", 4, c.Count);
182
183                         // Collection class is 1-based
184                         AssertEquals("#AAK02", "Baseball", c[1]);
185                         AssertEquals("#AAK03", "Football", c[4]);
186                         AssertEquals("#AAK04", "Basketball", c["Basket"]);
187                         AssertEquals("#AAK05", "Volleyball", c["Volley"]);
188                 }
189
190                 // Test GetEnumerator method
191                 [Test]
192                 public void GetEnumerator ()
193                 {
194                         Collection c;
195                         IEnumerator e;
196                         object[] o = new object[4] {typeof(int), 
197                                 typeof(double), typeof(string), typeof(object)};
198                         int i = 0;
199
200                         c = new Collection();
201
202                         c.Add(typeof(int), null, null, null);
203                         c.Add(typeof(double), null, null, null);
204                         c.Add(typeof(string), null, null, null);
205                         c.Add(typeof(object), null, null, null);
206
207                         e = c.GetEnumerator();
208
209                         AssertNotNull("#GE01", e);
210
211                         while (e.MoveNext()) {
212                                 AssertEquals("#GE02." + i.ToString(), o[i], e.Current);
213                                 i++;
214                         }
215
216                         e.Reset();
217                         e.MoveNext();
218
219                         AssertEquals("#GE03", o[0], e.Current);
220
221                 }
222
223                 // Test GetEnumerator method again, this time using foreach
224                 [Test]
225                 public void Foreach ()
226                 {
227                         Collection c;
228                         object[] o = new object[4] {typeof(int), 
229                                 typeof(double), typeof(string), typeof(object)};
230                         int i = 0;
231                         
232                         c = new Collection();
233
234                         c.Add(typeof(int), null, null, null);
235                         c.Add(typeof(double), null, null, null);
236                         c.Add(typeof(string), null, null, null);
237                         c.Add(typeof(object), null, null, null);
238
239                         
240                         foreach (object item in c) {
241                                 AssertEquals("#fe01." + i.ToString(), o[i], item);
242                                 i++;
243                         }
244                         
245                 }
246
247                 // Test Remove method with Index
248                 [Test]
249                 public void RemoveNoKey ()
250                 {
251                         Collection c;
252
253                         c = new Collection();
254
255                         c.Add(typeof(int), null, null, null);
256                         c.Add(typeof(double), null, null, null);
257                         c.Add(typeof(string), null, null, null);
258                         c.Add(typeof(object), null, null, null);
259
260                         AssertEquals("#RNK01", 4, c.Count);
261
262                         c.Remove(3);
263
264                         AssertEquals("#RNK02", 3, c.Count);
265
266                         // Collection class is 1-based
267                         AssertEquals("#RNK03", typeof(object), c[3]);
268
269                         c.Remove(1);
270
271                         AssertEquals("#RNK04", 2, c.Count);
272                         AssertEquals("#RNK05", typeof(double), c[1]);
273                         AssertEquals("#RNK06", typeof(object), c[2]);
274
275                         c.Remove(2);
276
277                         AssertEquals("#RNK07", 1, c.Count);
278                         AssertEquals("#RNK08", typeof(double), c[1]);
279
280                         c.Remove(1);
281
282                         AssertEquals("#RNK09", 0, c.Count);
283                 
284                 }
285
286                 // Test Remove method with Key
287                 [Test]
288                 public void RemoveKey ()
289                 {
290                         Collection c;
291
292                         c = new Collection();
293
294                         c.Add("Baseball", "Base", null, null);
295                         c.Add("Football", "Foot", null, null);
296                         c.Add("Basketball", "Basket", null, null);
297                         c.Add("Volleyball", "Volley", null, null);
298
299                         AssertEquals("#RK01", 4, c.Count);
300
301                         c.Remove("Foot");
302
303                         AssertEquals("#RK02", 3, c.Count);
304                         AssertEquals("#RK03", "Basketball", c["Basket"]);
305
306                         // Collection class is 1-based
307                         AssertEquals("#RK04", "Volleyball", c[3]);
308
309                         c.Remove("Base");
310
311                         AssertEquals("#RK05", 2, c.Count);
312                         AssertEquals("#RK06", "Basketball", c[1]);
313                         AssertEquals("#RK07", "Volleyball", c["Volley"]);
314
315                         c.Remove(2);
316
317                         AssertEquals("#RK08", 1, c.Count);
318                         AssertEquals("#RK09", "Basketball", c[1]);
319                         AssertEquals("#RK10", "Basketball", c["Basket"]);
320
321                         c.Remove(1);
322
323                         AssertEquals("#RK11", 0, c.Count);
324                 }
325
326                 // Test all the Exceptions we're supposed to throw
327                 [Test]
328                 public void Exception ()
329                 {
330                         Collection c;
331                         bool caughtException = false;
332
333                         c = new Collection();
334
335                         try {
336                                 // nothing in Collection yet
337                                 object o = c[0];
338                         }
339                         catch (Exception e) {
340                                 AssertEquals("#E01", typeof(IndexOutOfRangeException), e.GetType());
341                                 caughtException = true;
342                         }
343
344                         AssertEquals("#E02", true, caughtException);
345                 
346                         c.Add("Baseball", "Base", null, null);
347                         c.Add("Football", "Foot", null, null);
348                         c.Add("Basketball", "Basket", null, null);
349                         c.Add("Volleyball", "Volley", null, null);
350
351                         caughtException = false;
352
353                         try {
354                                 // only 4 elements
355                                 object o = c[5];
356                         }
357                         catch (Exception e) {
358                                 AssertEquals("#E03", typeof(IndexOutOfRangeException), e.GetType());
359                                 caughtException = true;
360                         }
361
362                         AssertEquals("#E04", true, caughtException);
363             
364                         caughtException = false;
365                         
366                         try {
367                                 // Collection class is 1-based
368                                 object o = c[0];
369                         }
370                         catch (Exception e) {
371                                 AssertEquals("#E05", typeof(IndexOutOfRangeException), e.GetType());
372                                 caughtException = true;
373                         }
374
375                         AssertEquals("#E06", true, caughtException);
376             
377                         caughtException = false;
378                         
379                         try {
380                                 // no member with Key == "Kick"
381                                 object o = c["Kick"];
382                         }
383                         catch (Exception e) {
384                                 // FIXME
385                                 // VB Language Reference says IndexOutOfRangeException 
386                                 // here, but MS throws ArgumentException
387                                 // AssertEquals("#E07", typeof(IndexOutOfRangeException), e.GetType());
388                                 AssertEquals("#E07", typeof(ArgumentException), e.GetType());
389                                 caughtException = true;
390                         }
391
392                         AssertEquals("#E08", true, caughtException);
393          
394                         caughtException = false;
395                         
396                         try {
397                                 // Even though Indexer is an object, really it's a string
398                                 object o = c[typeof(int)];
399                         }
400                         catch (Exception e) {
401                                 AssertEquals("#E09", typeof(ArgumentException), e.GetType());
402                                 caughtException = true;
403                         }
404
405                         AssertEquals("#E10", true, caughtException);
406          
407                         caughtException = false;
408                         
409                         try {
410                                 // can't specify both Before and After
411                                 c.Add("Kickball", "Kick", "Volley", "Foot");
412                         }
413                         catch (Exception e) {
414                                 AssertEquals("#E11", typeof(ArgumentException), e.GetType());
415                                 caughtException = true;
416                         }
417
418                         AssertEquals("#E12", true, caughtException);
419          
420                         caughtException = false;
421                         
422                         try {
423                                 // Key "Foot" already exists
424                                 c.Add("Kickball", "Foot", null, null);
425                         }
426                         catch (Exception e) {
427                                 AssertEquals("#E13", typeof(ArgumentException), e.GetType());
428                                 caughtException = true;
429                         }
430
431                         AssertEquals("#E14", true, caughtException);
432
433                         caughtException = false;
434                         
435                         try {
436                                 // Even though Before is object, it's really a string
437                                 c.Add("Dodgeball", "Dodge", typeof(int), null);
438                         }
439                         catch (Exception e) {
440                                 AssertEquals("#E15", typeof(InvalidCastException), e.GetType());
441                                 caughtException = true;
442                         }
443
444                         AssertEquals("#E16", true, caughtException);
445         
446                         caughtException = false;
447                         
448                         try {
449                                 // Even though After is object, it's really a string
450                                 c.Add("Wallyball", "Wally", null, typeof(int));
451                         }
452                         catch (Exception e) {
453                                 AssertEquals("#E17", typeof(InvalidCastException), e.GetType());
454                                 caughtException = true;
455                         }
456
457                         AssertEquals("#E18", true, caughtException);
458         
459                         caughtException = false;
460                         
461                         try {
462                                 // have to pass a legitimate value to remove
463                                 c.Remove(null);
464                         }
465                         catch (Exception e) {
466                                 AssertEquals("#E19", typeof(ArgumentNullException), e.GetType());
467                                 caughtException = true;
468                         }
469
470                         AssertEquals("#E20", true, caughtException);
471         
472                         caughtException = false;
473                         
474                         try {
475                                 // no Key "Golf" exists
476                                 c.Remove("Golf");
477                         }
478                         catch (Exception e) {
479                                 AssertEquals("#E21", typeof(ArgumentException), e.GetType());
480                                 caughtException = true;
481                         }
482
483                         AssertEquals("#E22", true, caughtException);
484         
485                         caughtException = false;
486                         
487                         try {
488                                 // no Index 10 exists
489                                 c.Remove(10);
490                         }
491                         catch (Exception e) {
492                                 AssertEquals("#E23", typeof(IndexOutOfRangeException), e.GetType());
493                                 caughtException = true;
494                         }
495
496                         AssertEquals("#E24", true, caughtException);
497         
498                         caughtException = false;
499                         
500                         try {
501                                 IEnumerator e = c.GetEnumerator();
502                                 
503                                 // Must MoveNext before Current
504                                 object item = e.Current;
505                         }
506                         catch (Exception e) {
507                                 // FIXME
508                                 // On-line help says InvalidOperationException here, 
509                                 // but MS throws IndexOutOfRangeException
510                                 // AssertEquals("#E25", typeof(InvalidOperationException), e.GetType());
511                                 AssertEquals("#E25", typeof(IndexOutOfRangeException), e.GetType());
512                                 caughtException = true;
513                         }
514
515                         AssertEquals("#E26", true, caughtException);
516         
517                         caughtException = false;
518                         
519                         try {
520                                 IEnumerator e = c.GetEnumerator();
521                                 e.MoveNext();
522
523                                 c.Add("Paintball", "Paint", null, null);
524
525                                 // Can't MoveNext if Collection has been modified
526                                 e.MoveNext();
527                         }
528                         catch (Exception e) {
529                                 // FIXME
530                                 // On-line help says this should throw an error. MS doesn't.
531                                 AssertEquals("#E27", typeof(InvalidOperationException), e.GetType());
532                                 caughtException = true;
533                         }
534
535                         // FIXME
536                         // What to do about this?  MS doesn't throw an error
537                         // AssertEquals("#E28", true, caughtException);
538                         AssertEquals("#E28", false, caughtException);
539         
540                         caughtException = false;
541                         
542                         try {
543                                 IEnumerator e = c.GetEnumerator();
544                                 e.MoveNext();
545
546                                 c.Add("Racketball", "Racket", null, null);
547
548                                 // Can't Reset if Collection has been modified
549                                 e.Reset();
550                         }
551                         catch (Exception e) {
552                                 // FIXME
553                                 // On-line help says this should throw an error.  MS doesn't.
554                                 AssertEquals("#E29", typeof(InvalidOperationException), e.GetType());
555                                 caughtException = true;
556                         }
557
558                         // FIXME
559                         // What to do about this?  MS doesn't throw an error
560                         // AssertEquals("#E30", true, caughtException);
561                         AssertEquals("#E30", false, caughtException);
562
563                         caughtException = false;
564                 }
565         }
566 }