1 // SortedListTest.cs - NUnit Test Cases for the System.Collections.SortedList class
\r
5 // Duncan Mak (duncan@ximian.com)
\r
7 // Thanks go to David Brandt (bucky@keystreams.com),
\r
8 // because this file is based on his ArrayListTest.cs
\r
10 // (C) Ximian, Inc. http://www.ximian.com
\r
12 // main TODO: additional tests for functions affected by
\r
13 // fixedsize and read-only properties
\r
17 using System.Collections;
\r
19 using NUnit.Framework;
\r
22 namespace MonoTests.System.Collections {
\r
25 public class SortedListTest : Assertion {
\r
26 protected SortedList sl1;
\r
27 protected SortedList sl2;
\r
28 protected SortedList emptysl;
\r
29 protected const int icap=16;
\r
31 public void TestConstructor1() {
\r
32 SortedList temp1 = new SortedList();
\r
33 AssertNotNull("sl.constructor-1: returns null", temp1);
\r
34 AssertEquals("sl.constructor-1: incorrect initial capacity", icap, temp1.Capacity);
\r
38 public void TestConstructor2() {
\r
39 Comparer c = Comparer.Default;
\r
40 SortedList temp1 = new SortedList(c);
\r
41 AssertNotNull("sl.constructor-2: returns null", temp1);
\r
42 AssertEquals("sl.constructor-2: incorrect initial capacity", icap, temp1.Capacity);
\r
46 public void TestConstructor3() {
\r
47 Hashtable d = new Hashtable();
\r
48 d.Add("one", "Mircosoft");
\r
49 d.Add("two", "will");
\r
50 d.Add("three", "rule");
\r
51 d.Add("four", "the world");
\r
53 SortedList temp1 = new SortedList(d);
\r
54 AssertNotNull("sl.constructor-3: returns null", temp1);
\r
55 AssertEquals("sl.constructor-3: incorrect initial capacity", 4, temp1.Capacity);
\r
56 AssertEquals("sl.constructor-3: incorrect count", 4, temp1.Count);
\r
60 temp1 = new SortedList(d);
\r
61 Fail ("sl.constructor-3: does not throw ArgumentNullException");
\r
62 } catch (ArgumentNullException) {}
\r
64 d = new Hashtable();
\r
65 d.Add("one", "Mircosoft");
\r
66 d.Add("two", "will");
\r
67 d.Add("three", "rule");
\r
68 d.Add("four", "the world");
\r
70 temp1 = new SortedList(d);
\r
71 Fail ("sl.constructor-3: does not throw InvalidCastException");
\r
72 } catch (InvalidOperationException) {
\r
73 } catch (Exception e) {
\r
74 Fail ("Unexpected Exception throw: e=" + e);
\r
79 public void TestConstructor4() {
\r
80 SortedList temp1 = new SortedList(17);
\r
81 AssertNotNull("sl.constructor-4: returns null", temp1);
\r
82 AssertEquals("sl.constructor-4: incorrect initial capacity", temp1.Capacity, 17);
\r
84 temp1 = new SortedList(-6);
\r
85 Fail ("sl.constructor-4: does not throw ArgumentOutOfRangeException, with negative values");
\r
86 } catch (ArgumentOutOfRangeException) {}
\r
88 temp1 = new SortedList(0);
\r
89 } catch (ArgumentOutOfRangeException) {
\r
90 Fail ("sl.constructor-4: throws ArgumentOutOfRangeException with 0");
\r
95 public void TestConstructor5() {
\r
96 Comparer c = Comparer.Default;
\r
97 SortedList temp1 = new SortedList(c,27);
\r
98 AssertNotNull("sl.constructor-5: returns null", temp1);
\r
99 AssertEquals("sl.constructor-5: incorrect initial capacity", temp1.Capacity, 27);
\r
101 temp1 = new SortedList(-12);
\r
102 Fail ("sl.constructor-5: does not throw ArgumentOutOfRangeException, with negative values");
\r
103 } catch (ArgumentOutOfRangeException) {}
\r
107 public void Constructor_Capacity ()
\r
109 SortedList sl = new SortedList (0);
\r
110 AssertEquals ("Capacity-Original", 0, sl.Capacity);
\r
112 // doesn't reset to class default (16)
\r
113 AssertEquals ("Capacity-Resetted", 0, sl.Capacity);
\r
115 for (int i=1; i <= 16; i++) {
\r
116 sl = new SortedList (i);
\r
117 AssertEquals ("Capacity-Original" + i.ToString (), i, sl.Capacity);
\r
119 // reset to class default (16)
\r
120 AssertEquals ("Capacity-Resetted" + i.ToString (), 16, sl.Capacity);
\r
125 public void TestIsSynchronized() {
\r
126 SortedList sl1 = new SortedList();
\r
127 Assert("sl: should not be synchronized by default",
\r
128 !sl1.IsSynchronized);
\r
129 SortedList sl2 = SortedList.Synchronized(sl1);
\r
130 Assert("sl: synchronized wrapper not working", sl2.IsSynchronized);
\r
134 public void TestCapacity() {
\r
135 for (int i = 0; i < 100; i++) {
\r
136 SortedList sl1 = new SortedList(i);
\r
137 AssertEquals("Bad capacity of " + i,
\r
143 public void TestCapacity2 ()
\r
145 SortedList list = new SortedList ();
\r
148 AssertEquals (5, list.Capacity);
\r
150 SortedList sync = SortedList.Synchronized (list);
\r
151 AssertEquals (5, sync.Capacity);
\r
153 list.Capacity = 20;
\r
154 AssertEquals (20, list.Capacity);
\r
155 AssertEquals (20, sync.Capacity);
\r
159 public void TestCapacity3 ()
\r
161 int new_capacity = 5;
\r
162 SortedList list = new SortedList (1000);
\r
163 list.Capacity = new_capacity;
\r
166 AssertEquals (new_capacity, list.Capacity);
\r
168 AssertEquals (16, list.Capacity);
\r
173 public void Capacity_BackTo0 ()
\r
175 SortedList list = new SortedList (42);
\r
176 AssertEquals ("42", 42, list.Capacity);
\r
178 AssertEquals ("0(16)", 16, list.Capacity);
\r
182 [ExpectedException (typeof (OutOfMemoryException))]
\r
183 [Ignore ("This is not implemented in the runtime yet")]
\r
184 public void TestCapacity4 ()
\r
186 SortedList list = new SortedList ();
\r
187 list.Capacity = Int32.MaxValue;
\r
191 public void TestCount() {
\r
193 SortedList sl1 = new SortedList();
\r
194 AssertEquals("Bad initial count",
\r
196 for (int i = 1; i <= 100; i++) {
\r
197 sl1.Add(""+i,""+i);
\r
198 AssertEquals("Bad count " + i,
\r
205 public void TestIsFixed() {
\r
206 SortedList sl1 = new SortedList();
\r
207 Assert("should not be fixed by default", !sl1.IsFixedSize);
\r
212 public void TestIsReadOnly() {
\r
213 SortedList sl1 = new SortedList();
\r
214 Assert("should not be ReadOnly by default", !sl1.IsReadOnly);
\r
219 public void TestItem() {
\r
220 SortedList sl1 = new SortedList();
\r
224 object o = sl1[-1];
\r
225 } catch (ArgumentNullException) {
\r
226 Fail ("sl.Item: throws ArgumentNullException with negative values");
\r
229 object o = sl1[key];
\r
230 Fail ("sl.Item: does not throw ArgumentNullException with null key");
\r
231 } catch (ArgumentNullException) {}
\r
234 for (int i = 0; i <= 100; i++) {
\r
235 sl1.Add("kala "+i,i);
\r
237 for (int i = 0; i <= 100; i++) {
\r
238 AssertEquals("sl.Item: item not fetched for " + i,
\r
239 i, sl1["kala "+i]);
\r
244 public void TestSyncRoot()
\r
246 SortedList sl1 = new SortedList();
\r
247 AssertEquals("sl.SyncRoot: does not function",false, sl1.SyncRoot == null);
\r
249 lock( sl1.SyncRoot ) {
\r
250 foreach ( Object item in sl1 ) {
\r
252 Assert ("sl.SyncRoot: item not read-only",item.IsReadOnly);
\r
259 public void TestValues()
\r
261 SortedList sl1 = new SortedList();
\r
262 ICollection ic1 = sl1.Values;
\r
263 for (int i = 0; i <= 100; i++) {
\r
264 sl1.Add("kala "+i,i);
\r
265 AssertEquals("sl.Values: .Values has different count",ic1.Count,sl1.Count);
\r
270 // TODO: Add with IComparer
\r
272 public void TestAdd() {
\r
273 // seems SortedList cannot be set fixedsize or readonly
\r
274 SortedList sl1 = new SortedList();
\r
278 sl1.Add(key,"kala");
\r
279 Fail ("sl.Add: does not throw ArgumentNullException with null key");
\r
280 } catch (ArgumentNullException) {}
\r
284 for (int i = 1; i <= 100; i++) {
\r
285 sl1.Add("kala "+i,i);
\r
286 AssertEquals("sl.Add: incorrect count",i,sl1.Count);
\r
287 AssertEquals("sl.Add: incorrect value",i,sl1["kala "+i]);
\r
292 sl1.Add("kala",10);
\r
293 sl1.Add("kala",11);
\r
294 Fail ("sl.Add: does not throw ArgumentException when adding existing key");
\r
295 } catch (ArgumentException) {}
\r
300 public void TestClear() {
\r
301 SortedList sl1 = new SortedList(10);
\r
302 sl1.Add("kala", 'c');
\r
303 sl1.Add("kala2", 'd');
\r
304 AssertEquals("sl.Clear: capacity is incorrect", 10, sl1.Capacity);
\r
305 AssertEquals("sl.Clear: should have one element", 2, sl1.Count);
\r
307 AssertEquals("sl.Clear: is not cleared", 0, sl1.Count);
\r
308 AssertEquals("sl.Clear: capacity is altered", 16, sl1.Capacity);
\r
312 public void Clear_Capacity ()
\r
314 // strangely Clear change the default capacity (while Capacity doesn't)
\r
315 for (int i=0; i <= 16; i++) {
\r
316 SortedList sl = new SortedList (i);
\r
317 AssertEquals ("Capacity-Original" + i.ToString (), i, sl.Capacity);
\r
319 // reset to class default (16)
\r
320 AssertEquals ("Capacity-Resetted" + i.ToString (), 16, sl.Capacity);
\r
325 public void Clear_Capacity_Reset ()
\r
327 SortedList sl = new SortedList (0);
\r
328 AssertEquals ("0", 0, sl.Capacity);
\r
330 // reset to class default (16)
\r
331 AssertEquals ("Clear", 16, sl.Capacity);
\r
333 AssertEquals ("Capacity", 16, sl.Capacity);
\r
334 // note: we didn't return to 0 - so Clear cahnge the default capacity
\r
338 public void TestClone() {
\r
340 SortedList sl1 = new SortedList(10);
\r
341 for (int i = 0; i <= 50; i++) {sl1.Add("kala "+i,i);}
\r
342 SortedList sl2 = (SortedList)sl1.Clone();
\r
343 for (int i = 0; i <= 50; i++) {
\r
344 AssertEquals("sl.Clone: copying failed @"+i, sl1["kala "+i], sl2["kala "+i]);
\r
348 char[] d10 = {'a', 'b'};
\r
349 char[] d11 = {'a', 'c'};
\r
350 char[] d12 = {'b', 'c'};
\r
351 //char[][] d1 = {d10, d11, d12};
\r
352 SortedList sl1 = new SortedList();
\r
356 SortedList sl2 = (SortedList)sl1.Clone();
\r
357 AssertEquals("sl.Clone: Array not matching", sl1["d1"], sl2["d1"]);
\r
358 AssertEquals("sl.Clone: Array not matching", sl1["d2"], sl2["d2"]);
\r
359 AssertEquals("sl.Clone: Array not matching", sl1["d3"], sl2["d3"]);
\r
361 ((char[])sl1["d1"])[0] = 'z';
\r
362 AssertEquals("s1.Clone: shallow copy", sl1["d1"], sl2["d1"]);
\r
367 public void TestContains() {
\r
368 SortedList sl1 = new SortedList(55);
\r
369 for (int i = 0; i <= 50; i++) {sl1.Add("kala "+i,i);}
\r
372 if (sl1.Contains(null)){}
\r
373 Fail ("sl.Contains: does not throw ArgumentNullException with null key");
\r
374 } catch (ArgumentNullException) {}
\r
376 Assert("sl.Contains: can't find existing key", sl1.Contains("kala 17"));
\r
377 Assert("sl.Contains: finds non-existing key", !sl1.Contains("ohoo"));
\r
381 public void TestContainsKey() {
\r
382 SortedList sl1 = new SortedList(55);
\r
383 for (int i = 0; i <= 50; i++) {sl1.Add("kala "+i,i);}
\r
386 if (sl1.ContainsKey(null)){}
\r
387 Fail ("sl.ContainsKey: does not throw ArgumentNullException with null key");
\r
388 } catch (ArgumentNullException) {}
\r
390 Assert("sl.ContainsKey: can't find existing key", sl1.ContainsKey("kala 17"));
\r
391 Assert("sl.ContainsKey: finds non-existing key", !sl1.ContainsKey("ohoo"));
\r
395 public void TestContainsValue() {
\r
396 SortedList sl1 = new SortedList(55);
\r
397 sl1.Add(0, "zero");
\r
400 sl1.Add(3, "three");
\r
401 sl1.Add(4, "four");
\r
403 Assert("sl.ContainsValue: can't find existing value", sl1.ContainsValue("zero"));
\r
404 Assert("sl.ContainsValue: finds non-existing value", !sl1.ContainsValue("ohoo"));
\r
405 Assert("sl.ContainsValue: finds non-existing value", !sl1.ContainsValue(null));
\r
409 public void TestCopyTo() {
\r
410 SortedList sl1 = new SortedList();
\r
411 for (int i = 0; i <= 10; i++) {sl1.Add("kala "+i,i);}
\r
414 sl1.CopyTo(null, 2);
\r
415 Fail("sl.CopyTo: does not throw ArgumentNullException when target null");
\r
416 } catch (ArgumentNullException) {}
\r
420 Char[,] c2 = new Char[2,2];
\r
422 Fail("sl.CopyTo: does not throw ArgumentException when target is multiarray");
\r
423 } catch (ArgumentException) {}
\r
427 Char[] c1 = new Char[2];
\r
428 sl1.CopyTo(c1, -2);
\r
429 Fail("sl.CopyTo: does not throw ArgumentOutOfRangeException when index is negative");
\r
430 } catch (ArgumentOutOfRangeException) {}
\r
434 Char[] c1 = new Char[2];
\r
436 Fail("sl.CopyTo: does not throw ArgumentException when index is too large");
\r
437 } catch (ArgumentException) {}
\r
441 Char[] c1 = new Char[2];
\r
443 Fail("sl.CopyTo: does not throw ArgumentException when SortedList too big for the array");
\r
444 } catch (ArgumentException) {}
\r
448 Char[] c2 = new Char[15];
\r
450 Fail("sl.CopyTo: does not throw InvalidCastException when incompatible data types");
\r
451 } catch (InvalidCastException) {}
\r
454 // CopyTo function does not work well with SortedList
\r
455 // even example at MSDN gave InvalidCastException
\r
456 // thus, it is NOT tested here
\r
459 for (int i = 0; i <= 5; i++) {sl1.Add(i,""+i);}
\r
460 Char[] copy = new Char[15];
\r
461 Array.Clear(copy,0,copy.Length);
\r
462 copy.SetValue( "The", 0 );
\r
463 copy.SetValue( "quick", 1 );
\r
464 copy.SetValue( "brown", 2 );
\r
465 copy.SetValue( "fox", 3 );
\r
466 copy.SetValue( "jumped", 4 );
\r
467 copy.SetValue( "over", 5 );
\r
468 copy.SetValue( "the", 6 );
\r
469 copy.SetValue( "lazy", 7 );
\r
470 copy.SetValue( "dog", 8 );
\r
471 sl1.CopyTo(copy,1);
\r
472 AssertEquals("sl.CopyTo: incorrect copy(1).","The", copy.GetValue(0));
\r
473 AssertEquals("sl.CopyTo: incorrect copy(1).","quick", copy.GetValue(1));
\r
474 for (int i=2; i<8; i++) AssertEquals("sl.CopyTo: incorrect copy(2).",sl1["kala "+(i-2)], copy.GetValue(i));
\r
475 AssertEquals("sl.CopyTo: incorrect copy(3).","dog", copy.GetValue(8));
\r
479 public SortedList DefaultSL() {
\r
480 SortedList sl1 = new SortedList();
\r
481 sl1.Add( 1.0, "The" );
\r
482 sl1.Add( 1.1, "quick" );
\r
483 sl1.Add( 34.0, "brown" );
\r
484 sl1.Add( -100.75, "fox" );
\r
485 sl1.Add( 1.4, "jumped" );
\r
486 sl1.Add( 1.5, "over" );
\r
487 sl1.Add( 1.6, "the" );
\r
488 sl1.Add( 1.7, "lazy" );
\r
489 sl1.Add( 1.8, "dog" );
\r
493 public IList DefaultValues() {
\r
494 IList il = new ArrayList();
\r
498 il.Add( "jumped" );
\r
508 public void TestGetByIndex() {
\r
509 SortedList sl1 = DefaultSL();
\r
510 AssertEquals("cl.GetByIndex: failed(1)",sl1.GetByIndex(4),"over");
\r
511 AssertEquals("cl.GetByIndex: failed(2)",sl1.GetByIndex(8),"brown");
\r
513 sl1.GetByIndex(-1);
\r
514 Fail("sl.GetByIndex: does not throw ArgumentOutOfRangeException with negative index");
\r
515 } catch (ArgumentOutOfRangeException) {}
\r
517 sl1.GetByIndex(100);
\r
518 Fail("sl.GetByIndex: does not throw ArgumentOutOfRangeException with too large index");
\r
519 } catch (ArgumentOutOfRangeException) {}
\r
523 public void GetEnumerator ()
\r
525 SortedList sl1 = DefaultSL();
\r
526 IDictionaryEnumerator e = sl1.GetEnumerator();
\r
527 AssertNotNull("sl.GetEnumerator: does not return enumerator", e);
\r
528 AssertEquals("sl.GetEnumerator: enumerator not working(1)",e.MoveNext(),true);
\r
529 AssertNotNull("sl.GetEnumerator: enumerator not working(2)",e.Current);
\r
531 Assert ("ICloneable", (e is ICloneable));
\r
532 Assert ("IDictionaryEnumerator", (e is ICloneable));
\r
533 Assert ("IEnumerator", (e is ICloneable));
\r
537 public void TestGetKey() {
\r
538 SortedList sl1 = DefaultSL();
\r
539 AssertEquals("sl.GetKey: failed(1)",sl1.GetKey(4),1.5);
\r
540 AssertEquals("sl.GetKey: failed(2)",sl1.GetKey(8),34.0);
\r
543 Fail("sl.GetKey: does not throw ArgumentOutOfRangeException with negative index");
\r
544 } catch (ArgumentOutOfRangeException) {}
\r
547 Fail("sl.GetKey: does not throw ArgumentOutOfRangeException with too large index");
\r
548 } catch (ArgumentOutOfRangeException) {}
\r
552 public void TestGetKeyList() {
\r
553 SortedList sl1 = DefaultSL();
\r
554 IList keys = sl1.GetKeyList();
\r
555 AssertNotNull("sl.GetKeyList: does not return keylist", keys);
\r
556 Assert("sl.GetKeyList: keylist is not readonly", keys.IsReadOnly);
\r
557 AssertEquals("sl.GetKeyList: incorrect keylist size",keys.Count,9);
\r
558 AssertEquals("sl.GetKeyList: incorrect key(1)",keys[3],1.4);
\r
559 sl1.Add(33.9,"ehhe");
\r
560 AssertEquals("sl.GetKeyList: incorrect keylist size",keys.Count,10);
\r
561 AssertEquals("sl.GetKeyList: incorrect key(2)",keys[8],33.9);
\r
565 public void TestGetValueList() {
\r
566 SortedList sl1 = DefaultSL();
\r
567 IList originalvals = DefaultValues();
\r
568 IList vals = sl1.GetValueList();
\r
569 AssertNotNull("sl.GetValueList: does not return valuelist", vals);
\r
570 Assert("sl.GetValueList: valuelist is not readonly", vals.IsReadOnly);
\r
571 AssertEquals("sl.GetValueList: incorrect valuelist size",vals.Count,sl1.Count);
\r
572 for (int i=0; i<sl1.Count; i++) {
\r
573 AssertEquals("sl.GetValueList: incorrect key(1)",vals[i],originalvals[i]);
\r
576 sl1.Add(0.01,"ehhe");
\r
577 AssertEquals("sl.GetValueList: incorrect valuelist size",vals.Count,10);
\r
578 AssertEquals("sl.GetValueList: incorrect value(2)",vals[8],"dog");
\r
581 // TODO: IEnumerable.GetEnumerator [Explicit Interface Implementation]
\r
583 public void TestIEnumerable_GetEnumerator() {
\r
584 SortedList sl1 = DefaultSL();
\r
585 IEnumerator e = sl1.IEnumerable.GetEnumerator();
\r
586 AssertNotNull("sl.GetEnumerator: does not return enumerator", e);
\r
587 AssertEquals("sl.GetEnumerator: enumerator not working(1)",e.MoveNext(),true);
\r
588 AssertNotNull("sl.GetEnumerator: enumerator not working(2)",e.Current);
\r
593 public void TestIndexOfKey() {
\r
594 SortedList sl1 = new SortedList(24);
\r
597 for (int i = 0; i <= 50; i++) {
\r
598 s=string.Format("{0:D2}", i);
\r
599 sl1.Add("kala "+s,i);
\r
601 AssertEquals("sl.IndexOfKey: does not return -1 for non-existing key", -1, sl1.IndexOfKey("kala "));
\r
604 t=sl1.IndexOfKey(s);
\r
605 Fail("sl.IndexOfKey: ArgumentNullException not caught, when key is null");
\r
607 catch (ArgumentNullException) {}
\r
609 t=sl1.IndexOfKey(10);
\r
610 Fail("sl.IndexOfKey: InvalidOperationException not caught, when key invalid");
\r
612 catch (InvalidOperationException) {}
\r
613 for (int i=0; i<=50; i++) {
\r
614 s=string.Format("{0:D2}", i);
\r
615 AssertEquals("sl.IndexOfKey: incorrect index key", i, sl1.IndexOfKey("kala "+s));
\r
620 public void TestIndexOfValue() {
\r
621 SortedList sl1 = new SortedList(24);
\r
623 for (int i = 0; i < 50; i++) {
\r
624 s=string.Format("{0:D2}", i);
\r
625 sl1.Add("kala "+s,100+i*i);
\r
627 for (int i = 0; i < 50; i++) {
\r
628 s=string.Format("{0:D2}", i+50);
\r
629 sl1.Add("kala "+s,100+i*i);
\r
631 AssertEquals("sl.IndexOfValue: does not return -1 for non-existing value(1)", -1, sl1.IndexOfValue(102));
\r
632 AssertEquals("sl.IndexOfValue: does not return -1 for non-existing value(2)", -1, sl1.IndexOfValue(null));
\r
633 for (int i=0; i<50; i++) {
\r
634 AssertEquals("sl.IndexOfValue: incorrect index key", i, sl1.IndexOfValue(100+i*i));
\r
639 public void TestIndexOfValue2 ()
\r
641 SortedList list = new SortedList ();
\r
642 list.Add ("key0", "la la");
\r
643 list.Add ("key1", "value");
\r
644 list.Add ("key2", "value");
\r
646 int i = list.IndexOfValue ("value");
\r
648 AssertEquals (1, i);
\r
652 public void TestIndexOfValue3 ()
\r
654 SortedList list = new SortedList ();
\r
655 int i = list.IndexOfValue ((string) null);
\r
657 AssertEquals (1, -i);
\r
661 public void TestIndexer ()
\r
663 SortedList list = new SortedList ();
\r
665 list.Add (1, new Queue ());
\r
666 list.Add (2, new Hashtable ());
\r
667 list.Add (3, new Stack ());
\r
669 AssertEquals (typeof (Queue), list [1].GetType ());
\r
670 AssertEquals (typeof (Hashtable), list [2].GetType ());
\r
671 AssertEquals (typeof (Stack), list [3].GetType ());
\r
675 public void TestEnumerator ()
\r
677 SortedList list = new SortedList ();
\r
679 list.Add (1, new Queue ());
\r
680 list.Add (2, new Hashtable ());
\r
681 list.Add (3, new Stack ());
\r
683 foreach (DictionaryEntry d in list) {
\r
685 int key = (int) d.Key;
\r
686 Type value = d.Value.GetType ();
\r
690 AssertEquals (typeof (Queue), value);
\r
694 AssertEquals (typeof (Hashtable), value);
\r
698 AssertEquals (typeof (Stack), value);
\r
702 Fail ("This is incorrect: " + value.FullName);
\r
709 public void TestRemove() {
\r
710 SortedList sl1 = new SortedList(24);
\r
713 for (int i = 0; i < 50; i++) sl1.Add("kala "+i,i);
\r
717 Fail("sl.Remove: ArgumentNullException not caught, when key is null");
\r
718 } catch (ArgumentNullException) {}
\r
720 sl1.Remove("kala ");
\r
721 AssertEquals("sl.Remove: removes an item, when non-existing key given",sl1.Count,k);
\r
724 Fail("sl.Remove: IComparer exception is not thrown");
\r
725 } catch (Exception) {}
\r
727 for (int i=15; i<20; i++) sl1.Remove("kala "+i);
\r
728 for (int i=45; i<55; i++) sl1.Remove("kala "+i);
\r
730 AssertEquals("sl.Remove: removing failed",sl1.Count,40);
\r
731 for (int i=45; i<55; i++)
\r
732 AssertEquals("sl.Remove: removing failed(2)",sl1["kala "+i],null);
\r
736 public void TestRemoveAt() {
\r
737 SortedList sl1 = new SortedList(24);
\r
740 for (int i = 0; i < 50; i++) {
\r
741 s=string.Format("{0:D2}", i);
\r
742 sl1.Add("kala "+s,i);
\r
747 Fail("sl.RemoveAt: ArgumentOutOfRangeException not caught, when key is out of range");
\r
748 } catch (ArgumentOutOfRangeException) {}
\r
751 Fail("sl.RemoveAt: ArgumentOutOfRangeException not caught, when key is out of range");
\r
752 } catch (ArgumentOutOfRangeException) {}
\r
755 for (int i=0; i<20; i++) sl1.RemoveAt(9);
\r
757 AssertEquals("sl.RemoveAt: removing failed",sl1.Count,30);
\r
758 for (int i=0; i<9; i++)
\r
759 AssertEquals("sl.RemoveAt: removing failed(2)",sl1["kala "+string.Format("{0:D2}", i)],i);
\r
760 for (int i=9; i<29; i++)
\r
761 AssertEquals("sl.RemoveAt: removing failed(3)",sl1["kala "+string.Format("{0:D2}", i)],null);
\r
762 for (int i=29; i<50; i++)
\r
763 AssertEquals("sl.RemoveAt: removing failed(4)",sl1["kala "+string.Format("{0:D2}", i)],i);
\r
767 public void TestSetByIndex() {
\r
768 SortedList sl1 = new SortedList(24);
\r
769 for (int i = 49; i>=0; i--) sl1.Add(100+i,i);
\r
772 sl1.SetByIndex(-1,77);
\r
773 Fail("sl.SetByIndex: ArgumentOutOfRangeException not caught, when key is out of range");
\r
774 } catch (ArgumentOutOfRangeException) {}
\r
776 sl1.SetByIndex(100,88);
\r
777 Fail("sl.SetByIndex: ArgumentOutOfRangeException not caught, when key is out of range");
\r
778 } catch (ArgumentOutOfRangeException) {}
\r
780 for(int i=5; i<25; i++) sl1.SetByIndex(i,-1);
\r
781 for(int i=0; i<5; i++)
\r
782 AssertEquals("sl.SetByIndex: set failed(1)",sl1[100+i],i);
\r
783 for(int i=5; i<25; i++)
\r
784 AssertEquals("sl.SetByIndex: set failed(2)",sl1[100+i],-1);
\r
785 for(int i=25; i<50; i++)
\r
786 AssertEquals("sl.SetByIndex: set failed(3)",sl1[100+i],i);
\r
791 public void TestTrimToSize() {
\r
792 SortedList sl1 = new SortedList(24);
\r
795 AssertEquals("sl.TrimToSize: incorrect capacity after trimming empty list",icap,sl1.Capacity);
\r
797 for (int i = 72; i>=0; i--) sl1.Add(100+i,i);
\r
799 AssertEquals("sl.TrimToSize: incorrect capacity after trimming a list",73,sl1.Capacity);
\r