1 // Collection.cs - NUnit Test Cases for Microsoft.VisualBasic.Collection
4 // Chris J. Breisch (cjbreisch@altavista.net)
5 // Martin Willemoes Hansen (mwh@sysrq.dk)
7 // (C) Chris J. Breisch
8 // (C) Martin Willemoes Hansen
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
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.
34 using NUnit.Framework;
36 using Microsoft.VisualBasic;
37 using System.Collections;
39 namespace MonoTests.Microsoft.VisualBasic
42 public class CollectionTest : Assertion
46 public void GetReady() {}
49 public void Clean() {}
59 AssertNotNull("#N01", c);
60 AssertEquals("#N02", 0, c.Count);
63 // Test Add method with Key == null
65 public void AddNoKey ()
71 c.Add(typeof(int), null, null, null);
72 c.Add(typeof(double), null, null, null);
73 c.Add(typeof(string), null, null, null);
75 AssertEquals("#ANK01", 3, c.Count);
77 // Collection class is 1-based
78 AssertEquals("#ANK02", typeof(string), c[3]);
82 // Test Add method with Key specified
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);
95 AssertEquals("#AK01", 4, c.Count);
97 // Collection class is 1-based
98 AssertEquals("#AK02", "Baseball", c[1]);
99 AssertEquals("#AK03", "Volleyball", c["Volley"]);
103 // Test Add method with Before specified and Key == null
105 public void AddBeforeNoKey ()
109 c = new Collection();
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);
116 AssertEquals("#ABNK01", 4, c.Count);
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]);
125 // Test Add method with Before and Key
127 public void AddBeforeKey ()
131 c = new Collection();
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);
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"]);
146 // Test Add method with After specified and Key == null
148 public void AddAfterNoKey ()
152 c = new Collection();
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);
159 AssertEquals("#AANK01", 4, c.Count);
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]);
168 // Test Add method with After and Key
170 public void AddAfterKey ()
174 c = new Collection();
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);
181 AssertEquals("#AAK01", 4, c.Count);
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"]);
190 // Test GetEnumerator method
192 public void GetEnumerator ()
196 object[] o = new object[4] {typeof(int),
197 typeof(double), typeof(string), typeof(object)};
200 c = new Collection();
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);
207 e = c.GetEnumerator();
209 AssertNotNull("#GE01", e);
211 while (e.MoveNext()) {
212 AssertEquals("#GE02." + i.ToString(), o[i], e.Current);
219 AssertEquals("#GE03", o[0], e.Current);
223 // Test GetEnumerator method again, this time using foreach
225 public void Foreach ()
228 object[] o = new object[4] {typeof(int),
229 typeof(double), typeof(string), typeof(object)};
232 c = new Collection();
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);
240 foreach (object item in c) {
241 AssertEquals("#fe01." + i.ToString(), o[i], item);
247 // Test Remove method with Index
249 public void RemoveNoKey ()
253 c = new Collection();
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);
260 AssertEquals("#RNK01", 4, c.Count);
264 AssertEquals("#RNK02", 3, c.Count);
266 // Collection class is 1-based
267 AssertEquals("#RNK03", typeof(object), c[3]);
271 AssertEquals("#RNK04", 2, c.Count);
272 AssertEquals("#RNK05", typeof(double), c[1]);
273 AssertEquals("#RNK06", typeof(object), c[2]);
277 AssertEquals("#RNK07", 1, c.Count);
278 AssertEquals("#RNK08", typeof(double), c[1]);
282 AssertEquals("#RNK09", 0, c.Count);
286 // Test Remove method with Key
288 public void RemoveKey ()
292 c = new Collection();
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);
299 AssertEquals("#RK01", 4, c.Count);
303 AssertEquals("#RK02", 3, c.Count);
304 AssertEquals("#RK03", "Basketball", c["Basket"]);
306 // Collection class is 1-based
307 AssertEquals("#RK04", "Volleyball", c[3]);
311 AssertEquals("#RK05", 2, c.Count);
312 AssertEquals("#RK06", "Basketball", c[1]);
313 AssertEquals("#RK07", "Volleyball", c["Volley"]);
317 AssertEquals("#RK08", 1, c.Count);
318 AssertEquals("#RK09", "Basketball", c[1]);
319 AssertEquals("#RK10", "Basketball", c["Basket"]);
323 AssertEquals("#RK11", 0, c.Count);
326 // Test all the Exceptions we're supposed to throw
328 public void Exception ()
331 bool caughtException = false;
333 c = new Collection();
336 // nothing in Collection yet
339 catch (Exception e) {
340 AssertEquals("#E01", typeof(IndexOutOfRangeException), e.GetType());
341 caughtException = true;
344 AssertEquals("#E02", true, caughtException);
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);
351 caughtException = false;
357 catch (Exception e) {
358 AssertEquals("#E03", typeof(IndexOutOfRangeException), e.GetType());
359 caughtException = true;
362 AssertEquals("#E04", true, caughtException);
364 caughtException = false;
367 // Collection class is 1-based
370 catch (Exception e) {
371 AssertEquals("#E05", typeof(IndexOutOfRangeException), e.GetType());
372 caughtException = true;
375 AssertEquals("#E06", true, caughtException);
377 caughtException = false;
380 // no member with Key == "Kick"
381 object o = c["Kick"];
383 catch (Exception e) {
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;
392 AssertEquals("#E08", true, caughtException);
394 caughtException = false;
397 // Even though Indexer is an object, really it's a string
398 object o = c[typeof(int)];
400 catch (Exception e) {
401 AssertEquals("#E09", typeof(ArgumentException), e.GetType());
402 caughtException = true;
405 AssertEquals("#E10", true, caughtException);
407 caughtException = false;
410 // can't specify both Before and After
411 c.Add("Kickball", "Kick", "Volley", "Foot");
413 catch (Exception e) {
414 AssertEquals("#E11", typeof(ArgumentException), e.GetType());
415 caughtException = true;
418 AssertEquals("#E12", true, caughtException);
420 caughtException = false;
423 // Key "Foot" already exists
424 c.Add("Kickball", "Foot", null, null);
426 catch (Exception e) {
427 AssertEquals("#E13", typeof(ArgumentException), e.GetType());
428 caughtException = true;
431 AssertEquals("#E14", true, caughtException);
433 caughtException = false;
436 // Even though Before is object, it's really a string
437 c.Add("Dodgeball", "Dodge", typeof(int), null);
439 catch (Exception e) {
440 AssertEquals("#E15", typeof(InvalidCastException), e.GetType());
441 caughtException = true;
444 AssertEquals("#E16", true, caughtException);
446 caughtException = false;
449 // Even though After is object, it's really a string
450 c.Add("Wallyball", "Wally", null, typeof(int));
452 catch (Exception e) {
453 AssertEquals("#E17", typeof(InvalidCastException), e.GetType());
454 caughtException = true;
457 AssertEquals("#E18", true, caughtException);
459 caughtException = false;
462 // have to pass a legitimate value to remove
465 catch (Exception e) {
466 AssertEquals("#E19", typeof(ArgumentNullException), e.GetType());
467 caughtException = true;
470 AssertEquals("#E20", true, caughtException);
472 caughtException = false;
475 // no Key "Golf" exists
478 catch (Exception e) {
479 AssertEquals("#E21", typeof(ArgumentException), e.GetType());
480 caughtException = true;
483 AssertEquals("#E22", true, caughtException);
485 caughtException = false;
488 // no Index 10 exists
491 catch (Exception e) {
492 AssertEquals("#E23", typeof(IndexOutOfRangeException), e.GetType());
493 caughtException = true;
496 AssertEquals("#E24", true, caughtException);
498 caughtException = false;
501 IEnumerator e = c.GetEnumerator();
503 // Must MoveNext before Current
504 object item = e.Current;
506 catch (Exception e) {
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;
515 AssertEquals("#E26", true, caughtException);
517 caughtException = false;
520 IEnumerator e = c.GetEnumerator();
523 c.Add("Paintball", "Paint", null, null);
525 // Can't MoveNext if Collection has been modified
528 catch (Exception e) {
530 // On-line help says this should throw an error. MS doesn't.
531 AssertEquals("#E27", typeof(InvalidOperationException), e.GetType());
532 caughtException = true;
536 // What to do about this? MS doesn't throw an error
537 // AssertEquals("#E28", true, caughtException);
538 AssertEquals("#E28", false, caughtException);
540 caughtException = false;
543 IEnumerator e = c.GetEnumerator();
546 c.Add("Racketball", "Racket", null, null);
548 // Can't Reset if Collection has been modified
551 catch (Exception e) {
553 // On-line help says this should throw an error. MS doesn't.
554 AssertEquals("#E29", typeof(InvalidOperationException), e.GetType());
555 caughtException = true;
559 // What to do about this? MS doesn't throw an error
560 // AssertEquals("#E30", true, caughtException);
561 AssertEquals("#E30", false, caughtException);
563 caughtException = false;