1 // SortedListTest.cs - NUnit Test Cases for the System.Collections.SortedList class
\r
5 // Thanks go to David Brandt (bucky@keystreams.com),
\r
6 // because this file is based on his ArrayListTest.cs
\r
8 // (C) Ximian, Inc. http://www.ximian.com
\r
10 // main TODO: additional tests for functions affected by
\r
11 // fixedsize and read-only properties
\r
15 using System.Collections;
\r
17 using NUnit.Framework;
\r
20 namespace MonoTests.System.Collections {
\r
23 /// <summary>SortedList test.</summary>
\r
24 public class SortedListTest : TestCase {
\r
25 public SortedListTest() : base ("MonoTests.System.SortedListTest testsuite") {}
\r
26 public SortedListTest(string name) : base(name) {}
\r
28 protected SortedList sl1;
\r
29 protected SortedList sl2;
\r
30 protected SortedList emptysl;
\r
31 protected const int icap=16;
\r
33 protected override void SetUp()
\r
37 protected override void TearDown()
\r
41 public static ITest Suite {
\r
43 return new TestSuite(typeof(SortedListTest));
\r
47 public void TestConstructor1() {
\r
48 SortedList temp1 = new SortedList();
\r
49 AssertNotNull("sl.constructor-1: returns null", temp1);
\r
50 AssertEquals("sl.constructor-1: incorrect initial capacity", temp1.Capacity, icap);
\r
53 public void TestConstructor2() {
\r
54 Comparer c = Comparer.Default;
\r
55 SortedList temp1 = new SortedList(c);
\r
56 AssertNotNull("sl.constructor-2: returns null", temp1);
\r
57 AssertEquals("sl.constructor-2: incorrect initial capacity", temp1.Capacity, icap);
\r
60 public void TestConstructor3() {
\r
61 Hashtable d = new Hashtable();
\r
62 d.Add("one", "Mircosoft");
\r
63 d.Add("two", "will");
\r
64 d.Add("three", "rule");
\r
65 d.Add("four", "the world");
\r
67 SortedList temp1 = new SortedList(d);
\r
68 AssertNotNull("sl.constructor-3: returns null", temp1);
\r
69 AssertEquals("sl.constructor-3: incorrect initial capacity", temp1.Capacity, 4);
\r
70 AssertEquals("sl.constructor-3: incorrect count", temp1.Count, 4);
\r
74 temp1 = new SortedList(d);
\r
75 Fail ("sl.constructor-3: does not throw ArgumentNullException");
\r
76 } catch (ArgumentNullException) {}
\r
78 d = new Hashtable();
\r
79 d.Add("one", "Mircosoft");
\r
80 d.Add("two", "will");
\r
81 d.Add("three", "rule");
\r
82 d.Add("four", "the world");
\r
84 temp1 = new SortedList(d);
\r
85 Fail ("sl.constructor-3: does not throw InvalidCastException");
\r
86 } catch (InvalidOperationException) {
\r
87 } catch (Exception e) {
\r
88 Fail ("Unexpected Exception throw: e=" + e);
\r
92 public void TestConstructor4() {
\r
93 SortedList temp1 = new SortedList(17);
\r
94 AssertNotNull("sl.constructor-4: returns null", temp1);
\r
95 AssertEquals("sl.constructor-4: incorrect initial capacity", temp1.Capacity, 17);
\r
97 temp1 = new SortedList(-6);
\r
98 Fail ("sl.constructor-4: does not throw ArgumentOutOfRangeException, with negative values");
\r
99 } catch (ArgumentOutOfRangeException) {}
\r
101 temp1 = new SortedList(0);
\r
102 } catch (ArgumentOutOfRangeException) {
\r
103 Fail ("sl.constructor-4: throws ArgumentOutOfRangeException with 0");
\r
107 public void TestConstructor5() {
\r
108 Comparer c = Comparer.Default;
\r
109 SortedList temp1 = new SortedList(c,27);
\r
110 AssertNotNull("sl.constructor-5: returns null", temp1);
\r
111 AssertEquals("sl.constructor-5: incorrect initial capacity", temp1.Capacity, 27);
\r
113 temp1 = new SortedList(-12);
\r
114 Fail ("sl.constructor-5: does not throw ArgumentOutOfRangeException, with negative values");
\r
115 } catch (ArgumentOutOfRangeException) {}
\r
118 public void TestIsSynchronized() {
\r
119 SortedList sl1 = new SortedList();
\r
120 Assert("sl: should not be synchronized by default",
\r
121 !sl1.IsSynchronized);
\r
122 SortedList sl2 = SortedList.Synchronized(sl1);
\r
123 Assert("sl: synchronized wrapper not working", sl2.IsSynchronized);
\r
126 public void TestCapacity() {
\r
127 for (int i = 0; i < 100; i++) {
\r
128 SortedList sl1 = new SortedList(i);
\r
129 AssertEquals("Bad capacity of " + i,
\r
134 public void TestCount() {
\r
136 SortedList sl1 = new SortedList();
\r
137 AssertEquals("Bad initial count",
\r
139 for (int i = 1; i <= 100; i++) {
\r
140 sl1.Add(""+i,""+i);
\r
141 AssertEquals("Bad count " + i,
\r
147 public void TestIsFixed() {
\r
148 SortedList sl1 = new SortedList();
\r
149 Assert("should not be fixed by default", !sl1.IsFixedSize);
\r
152 public void TestIsReadOnly() {
\r
153 SortedList sl1 = new SortedList();
\r
154 Assert("should not be ReadOnly by default", !sl1.IsReadOnly);
\r
158 public void TestItem() {
\r
159 SortedList sl1 = new SortedList();
\r
163 object o = sl1[-1];
\r
164 } catch (ArgumentNullException) {
\r
165 Fail ("sl.Item: throws ArgumentNullException with negative values");
\r
168 object o = sl1[key];
\r
169 Fail ("sl.Item: does not throw ArgumentNullException with null key");
\r
170 } catch (ArgumentNullException) {}
\r
173 for (int i = 0; i <= 100; i++) {
\r
174 sl1.Add("kala "+i,i);
\r
176 for (int i = 0; i <= 100; i++) {
\r
177 AssertEquals("sl.Item: item not fetched for " + i,
\r
178 i, sl1["kala "+i]);
\r
182 public void TestSyncRoot()
\r
184 SortedList sl1 = new SortedList();
\r
185 AssertEquals("sl.SyncRoot: does not function",false, sl1.SyncRoot == null);
\r
187 lock( sl1.SyncRoot ) {
\r
188 foreach ( Object item in sl1 ) {
\r
190 Assert ("sl.SyncRoot: item not read-only",item.IsReadOnly);
\r
196 public void TestValues()
\r
198 SortedList sl1 = new SortedList();
\r
199 ICollection ic1 = sl1.Values;
\r
200 for (int i = 0; i <= 100; i++) {
\r
201 sl1.Add("kala "+i,i);
\r
202 AssertEquals("sl.Values: .Values has different count",ic1.Count,sl1.Count);
\r
207 // TODO: Add with IComparer
\r
208 public void TestAdd() {
\r
209 // seems SortedList cannot be set fixedsize or readonly
\r
210 SortedList sl1 = new SortedList();
\r
214 sl1.Add(key,"kala");
\r
215 Fail ("sl.Add: does not throw ArgumentNullException with null key");
\r
216 } catch (ArgumentNullException) {}
\r
220 for (int i = 1; i <= 100; i++) {
\r
221 sl1.Add("kala "+i,i);
\r
222 AssertEquals("sl.Add: incorrect count",i,sl1.Count);
\r
223 AssertEquals("sl.Add: incorrect value",i,sl1["kala "+i]);
\r
228 sl1.Add("kala",10);
\r
229 sl1.Add("kala",11);
\r
230 Fail ("sl.Add: does not throw ArgumentException when adding existing key");
\r
231 } catch (ArgumentException) {}
\r
236 public void TestClear() {
\r
238 SortedList sl1 = new SortedList(10);
\r
239 sl1.Add("kala",'c');
\r
240 sl1.Add("kala2",'d');
\r
241 AssertEquals("sl.Clear: capacity is incorrect",10, sl1.Capacity);
\r
242 AssertEquals("sl.Clear: should have one element", 2, sl1.Count);
\r
244 AssertEquals("sl.Clear: is not cleared",0, sl1.Count);
\r
245 AssertEquals("sl.Clear: capacity is altered",16, sl1.Capacity);
\r
249 public void TestClone() {
\r
251 SortedList sl1 = new SortedList(10);
\r
252 for (int i = 0; i <= 50; i++) {sl1.Add("kala "+i,i);}
\r
253 SortedList sl2 = (SortedList)sl1.Clone();
\r
254 for (int i = 0; i <= 50; i++) {
\r
255 AssertEquals("sl.Clone: copying failed @"+i, sl1["kala "+i], sl2["kala "+i]);
\r
259 char[] d10 = {'a', 'b'};
\r
260 char[] d11 = {'a', 'c'};
\r
261 char[] d12 = {'b', 'c'};
\r
262 //char[][] d1 = {d10, d11, d12};
\r
263 SortedList sl1 = new SortedList();
\r
267 SortedList sl2 = (SortedList)sl1.Clone();
\r
268 AssertEquals("sl.Clone: Array not matching", sl1["d1"], sl2["d1"]);
\r
269 AssertEquals("sl.Clone: Array not matching", sl1["d2"], sl2["d2"]);
\r
270 AssertEquals("sl.Clone: Array not matching", sl1["d3"], sl2["d3"]);
\r
272 ((char[])sl1["d1"])[0] = 'z';
\r
273 AssertEquals("s1.Clone: shallow copy", sl1["d1"], sl2["d1"]);
\r
277 public void TestContains() {
\r
278 SortedList sl1 = new SortedList(55);
\r
279 for (int i = 0; i <= 50; i++) {sl1.Add("kala "+i,i);}
\r
282 if (sl1.Contains(null)){}
\r
283 Fail ("sl.Contains: does not throw ArgumentNullException with null key");
\r
284 } catch (ArgumentNullException) {}
\r
286 Assert("sl.Contains: can't find existing key", sl1.Contains("kala 17"));
\r
287 Assert("sl.Contains: finds non-existing key", !sl1.Contains("ohoo"));
\r
290 public void TestContainsKey() {
\r
291 SortedList sl1 = new SortedList(55);
\r
292 for (int i = 0; i <= 50; i++) {sl1.Add("kala "+i,i);}
\r
295 if (sl1.ContainsKey(null)){}
\r
296 Fail ("sl.ContainsKey: does not throw ArgumentNullException with null key");
\r
297 } catch (ArgumentNullException) {}
\r
299 Assert("sl.ContainsKey: can't find existing key", sl1.ContainsKey("kala 17"));
\r
300 Assert("sl.ContainsKey: finds non-existing key", !sl1.ContainsKey("ohoo"));
\r
303 public void TestContainsValue() {
\r
304 SortedList sl1 = new SortedList(55);
\r
305 sl1.Add(0, "zero");
\r
308 sl1.Add(3, "three");
\r
309 sl1.Add(4, "four");
\r
310 Assert("sl.ContainsValue: can't find existing value", sl1.ContainsValue("zero"));
\r
311 Assert("sl.ContainsValue: finds non-existing value", !sl1.ContainsValue("ohoo"));
\r
312 Assert("sl.ContainsValue: finds non-existing value", !sl1.ContainsValue(null));
\r
315 public void TestCopyTo() {
\r
316 SortedList sl1 = new SortedList();
\r
317 for (int i = 0; i <= 10; i++) {sl1.Add("kala "+i,i);}
\r
320 sl1.CopyTo(null, 2);
\r
321 Fail("sl.CopyTo: does not throw ArgumentNullException when target null");
\r
322 } catch (ArgumentNullException) {}
\r
326 Char[,] c2 = new Char[2,2];
\r
328 Fail("sl.CopyTo: does not throw ArgumentException when target is multiarray");
\r
329 } catch (ArgumentException) {}
\r
333 Char[] c1 = new Char[2];
\r
334 sl1.CopyTo(c1, -2);
\r
335 Fail("sl.CopyTo: does not throw ArgumentOutOfRangeException when index is negative");
\r
336 } catch (ArgumentOutOfRangeException) {}
\r
340 Char[] c1 = new Char[2];
\r
342 Fail("sl.CopyTo: does not throw ArgumentException when index is too large");
\r
343 } catch (ArgumentException) {}
\r
347 Char[] c1 = new Char[2];
\r
349 Fail("sl.CopyTo: does not throw ArgumentException when SortedList too big for the array");
\r
350 } catch (ArgumentException) {}
\r
354 Char[] c2 = new Char[15];
\r
356 Fail("sl.CopyTo: does not throw InvalidCastException when incompatible data types");
\r
357 } catch (InvalidCastException) {}
\r
360 // CopyTo function does not work well with SortedList
\r
361 // even example at MSDN gave InvalidCastException
\r
362 // thus, it is NOT testet here
\r
365 for (int i = 0; i <= 5; i++) {sl1.Add(i,""+i);}
\r
366 Char[] copy = new Char[15];
\r
367 Array.Clear(copy,0,copy.Length);
\r
368 copy.SetValue( "The", 0 );
\r
369 copy.SetValue( "quick", 1 );
\r
370 copy.SetValue( "brown", 2 );
\r
371 copy.SetValue( "fox", 3 );
\r
372 copy.SetValue( "jumped", 4 );
\r
373 copy.SetValue( "over", 5 );
\r
374 copy.SetValue( "the", 6 );
\r
375 copy.SetValue( "lazy", 7 );
\r
376 copy.SetValue( "dog", 8 );
\r
377 sl1.CopyTo(copy,1);
\r
378 AssertEquals("sl.CopyTo: incorrect copy(1).","The", copy.GetValue(0));
\r
379 AssertEquals("sl.CopyTo: incorrect copy(1).","quick", copy.GetValue(1));
\r
380 for (int i=2; i<8; i++) AssertEquals("sl.CopyTo: incorrect copy(2).",sl1["kala "+(i-2)], copy.GetValue(i));
\r
381 AssertEquals("sl.CopyTo: incorrect copy(3).","dog", copy.GetValue(8));
\r
385 public SortedList DefaultSL() {
\r
386 SortedList sl1 = new SortedList();
\r
387 sl1.Add( 1.0, "The" );
\r
388 sl1.Add( 1.1, "quick" );
\r
389 sl1.Add( 34.0, "brown" );
\r
390 sl1.Add( -100.75, "fox" );
\r
391 sl1.Add( 1.4, "jumped" );
\r
392 sl1.Add( 1.5, "over" );
\r
393 sl1.Add( 1.6, "the" );
\r
394 sl1.Add( 1.7, "lazy" );
\r
395 sl1.Add( 1.8, "dog" );
\r
399 public IList DefaultValues() {
\r
400 IList il = new ArrayList();
\r
404 il.Add( "jumped" );
\r
413 public void TestGetByIndex() {
\r
414 SortedList sl1 = DefaultSL();
\r
415 AssertEquals("cl.GetByIndex: failed(1)",sl1.GetByIndex(4),"over");
\r
416 AssertEquals("cl.GetByIndex: failed(2)",sl1.GetByIndex(8),"brown");
\r
418 sl1.GetByIndex(-1);
\r
419 Fail("sl.GetByIndex: does not throw ArgumentOutOfRangeException with negative index");
\r
420 } catch (ArgumentOutOfRangeException) {}
\r
422 sl1.GetByIndex(100);
\r
423 Fail("sl.GetByIndex: does not throw ArgumentOutOfRangeException with too large index");
\r
424 } catch (ArgumentOutOfRangeException) {}
\r
427 public void TestGetEnumerator() {
\r
428 SortedList sl1 = DefaultSL();
\r
429 IDictionaryEnumerator e = sl1.GetEnumerator();
\r
430 AssertNotNull("sl.GetEnumerator: does not return enumerator", e);
\r
431 AssertEquals("sl.GetEnumerator: enumerator not working(1)",e.MoveNext(),true);
\r
432 AssertNotNull("sl.GetEnumerator: enumerator not working(2)",e.Current);
\r
435 public void TestGetKey() {
\r
436 SortedList sl1 = DefaultSL();
\r
437 AssertEquals("sl.GetKey: failed(1)",sl1.GetKey(4),1.5);
\r
438 AssertEquals("sl.GetKey: failed(2)",sl1.GetKey(8),34.0);
\r
441 Fail("sl.GetKey: does not throw ArgumentOutOfRangeException with negative index");
\r
442 } catch (ArgumentOutOfRangeException) {}
\r
445 Fail("sl.GetKey: does not throw ArgumentOutOfRangeException with too large index");
\r
446 } catch (ArgumentOutOfRangeException) {}
\r
449 public void TestGetKeyList() {
\r
450 SortedList sl1 = DefaultSL();
\r
451 IList keys = sl1.GetKeyList();
\r
452 AssertNotNull("sl.GetKeyList: does not return keylist", keys);
\r
453 Assert("sl.GetKeyList: keylist is not readonly", keys.IsReadOnly);
\r
454 AssertEquals("sl.GetKeyList: incorrect keylist size",keys.Count,9);
\r
455 AssertEquals("sl.GetKeyList: incorrect key(1)",keys[3],1.4);
\r
456 sl1.Add(33.9,"ehhe");
\r
457 AssertEquals("sl.GetKeyList: incorrect keylist size",keys.Count,10);
\r
458 AssertEquals("sl.GetKeyList: incorrect key(2)",keys[8],33.9);
\r
461 public void TestGetValueList() {
\r
462 SortedList sl1 = DefaultSL();
\r
463 IList originalvals = DefaultValues();
\r
464 IList vals = sl1.GetValueList();
\r
465 AssertNotNull("sl.GetValueList: does not return valuelist", vals);
\r
466 Assert("sl.GetValueList: valuelist is not readonly", vals.IsReadOnly);
\r
467 AssertEquals("sl.GetValueList: incorrect valuelist size",vals.Count,sl1.Count);
\r
468 for (int i=0; i<sl1.Count; i++) {
\r
469 AssertEquals("sl.GetValueList: incorrect key(1)",vals[i],originalvals[i]);
\r
472 sl1.Add(0.01,"ehhe");
\r
473 AssertEquals("sl.GetValueList: incorrect valuelist size",vals.Count,10);
\r
474 AssertEquals("sl.GetValueList: incorrect value(2)",vals[8],"dog");
\r
477 // TODO: IEnumerable.GetEnumerator [Explicit Interface Implementation]
\r
479 public void TestIEnumerable_GetEnumerator() {
\r
480 SortedList sl1 = DefaultSL();
\r
481 IEnumerator e = sl1.IEnumerable.GetEnumerator();
\r
482 AssertNotNull("sl.GetEnumerator: does not return enumerator", e);
\r
483 AssertEquals("sl.GetEnumerator: enumerator not working(1)",e.MoveNext(),true);
\r
484 AssertNotNull("sl.GetEnumerator: enumerator not working(2)",e.Current);
\r
488 public void TestIndexOfKey() {
\r
489 SortedList sl1 = new SortedList(24);
\r
492 for (int i = 0; i <= 50; i++) {
\r
493 s=string.Format("{0:D2}", i);
\r
494 sl1.Add("kala "+s,i);
\r
496 AssertEquals("sl.IndexOfKey: does not return -1 for non-existing key",sl1.IndexOfKey("kala "),-1);
\r
499 t=sl1.IndexOfKey(s);
\r
500 Fail("sl.IndexOfKey: ArgumentNullException not caught, when key is null");
\r
502 catch (ArgumentNullException) {}
\r
504 t=sl1.IndexOfKey(10);
\r
505 Fail("sl.IndexOfKey: InvalidOperationException not caught, when key invalid");
\r
507 catch (InvalidOperationException) {}
\r
508 for (int i=0; i<=50; i++) {
\r
509 s=string.Format("{0:D2}", i);
\r
510 AssertEquals("sl.IndexOfKey: incorrect index key",sl1.IndexOfKey("kala "+s),i);
\r
514 public void TestIndexOfValue() {
\r
515 SortedList sl1 = new SortedList(24);
\r
517 for (int i = 0; i < 50; i++) {
\r
518 s=string.Format("{0:D2}", i);
\r
519 sl1.Add("kala "+s,100+i*i);
\r
521 for (int i = 0; i < 50; i++) {
\r
522 s=string.Format("{0:D2}", i+50);
\r
523 sl1.Add("kala "+s,100+i*i);
\r
525 AssertEquals("sl.IndexOfValue: does not return -1 for non-existing value(1)",sl1.IndexOfValue(102),-1);
\r
526 AssertEquals("sl.IndexOfValue: does not return -1 for non-existing value(2)",sl1.IndexOfValue(null),-1);
\r
527 for (int i=0; i<50; i++) {
\r
528 AssertEquals("sl.IndexOfValue: incorrect index key",sl1.IndexOfValue(100+i*i),i);
\r
533 public void TestRemove() {
\r
534 SortedList sl1 = new SortedList(24);
\r
537 for (int i = 0; i < 50; i++) sl1.Add("kala "+i,i);
\r
541 Fail("sl.Remove: ArgumentNullException not caught, when key is null");
\r
542 } catch (ArgumentNullException) {}
\r
544 sl1.Remove("kala ");
\r
545 AssertEquals("sl.Remove: removes an item, when non-existing key given",sl1.Count,k);
\r
548 Fail("sl.Remove: IComparer exception is not thrown");
\r
549 } catch (Exception) {}
\r
551 for (int i=15; i<20; i++) sl1.Remove("kala "+i);
\r
552 for (int i=45; i<55; i++) sl1.Remove("kala "+i);
\r
554 AssertEquals("sl.Remove: removing failed",sl1.Count,40);
\r
555 for (int i=45; i<55; i++)
\r
556 AssertEquals("sl.Remove: removing failed(2)",sl1["kala "+i],null);
\r
559 public void TestRemoveAt() {
\r
560 SortedList sl1 = new SortedList(24);
\r
563 for (int i = 0; i < 50; i++) {
\r
564 s=string.Format("{0:D2}", i);
\r
565 sl1.Add("kala "+s,i);
\r
570 Fail("sl.RemoveAt: ArgumentOutOfRangeException not caught, when key is out of range");
\r
571 } catch (ArgumentOutOfRangeException) {}
\r
574 Fail("sl.RemoveAt: ArgumentOutOfRangeException not caught, when key is out of range");
\r
575 } catch (ArgumentOutOfRangeException) {}
\r
578 for (int i=0; i<20; i++) sl1.RemoveAt(9);
\r
580 AssertEquals("sl.RemoveAt: removing failed",sl1.Count,30);
\r
581 for (int i=0; i<9; i++)
\r
582 AssertEquals("sl.RemoveAt: removing failed(2)",sl1["kala "+string.Format("{0:D2}", i)],i);
\r
583 for (int i=9; i<29; i++)
\r
584 AssertEquals("sl.RemoveAt: removing failed(3)",sl1["kala "+string.Format("{0:D2}", i)],null);
\r
585 for (int i=29; i<50; i++)
\r
586 AssertEquals("sl.RemoveAt: removing failed(4)",sl1["kala "+string.Format("{0:D2}", i)],i);
\r
589 public void TestSetByIndex() {
\r
590 SortedList sl1 = new SortedList(24);
\r
591 for (int i = 49; i>=0; i--) sl1.Add(100+i,i);
\r
594 sl1.SetByIndex(-1,77);
\r
595 Fail("sl.SetByIndex: ArgumentOutOfRangeException not caught, when key is out of range");
\r
596 } catch (ArgumentOutOfRangeException) {}
\r
598 sl1.SetByIndex(100,88);
\r
599 Fail("sl.SetByIndex: ArgumentOutOfRangeException not caught, when key is out of range");
\r
600 } catch (ArgumentOutOfRangeException) {}
\r
602 for(int i=5; i<25; i++) sl1.SetByIndex(i,-1);
\r
603 for(int i=0; i<5; i++)
\r
604 AssertEquals("sl.SetByIndex: set failed(1)",sl1[100+i],i);
\r
605 for(int i=5; i<25; i++)
\r
606 AssertEquals("sl.SetByIndex: set failed(2)",sl1[100+i],-1);
\r
607 for(int i=25; i<50; i++)
\r
608 AssertEquals("sl.SetByIndex: set failed(3)",sl1[100+i],i);
\r
612 public void TestTrimToSize() {
\r
613 SortedList sl1 = new SortedList(24);
\r
616 AssertEquals("sl.TrimToSize: incorrect capacity after trimming empty list",icap,sl1.Capacity);
\r
618 for (int i = 72; i>=0; i--) sl1.Add(100+i,i);
\r
620 AssertEquals("sl.TrimToSize: incorrect capacity after trimming a list",73,sl1.Capacity);
\r