2002-06-20 Nick Drochak <ndrochak@gol.com>
[mono.git] / mcs / class / corlib / Test / System.Collections / SortedListTest.cs
1 // SortedListTest.cs - NUnit Test Cases for the System.Collections.SortedList class\r
2 //\r
3 // Jaak Simm\r
4 //\r
5 // Thanks go to David Brandt (bucky@keystreams.com),\r
6 // because this file is based on his ArrayListTest.cs\r
7 //\r
8 // (C) Ximian, Inc.  http://www.ximian.com\r
9 // \r
10 // main TODO: additional tests for functions affected by\r
11 //            fixedsize and read-only properties \r
12 \r
13 \r
14 using System;\r
15 using System.Collections;\r
16 \r
17 using NUnit.Framework;\r
18 \r
19 \r
20 namespace MonoTests.System.Collections {\r
21 \r
22 \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
27 \r
28         protected SortedList sl1;\r
29         protected SortedList sl2;\r
30         protected SortedList emptysl;\r
31         protected const int icap=16;\r
32                 \r
33         protected override void SetUp() \r
34         {\r
35         }\r
36 \r
37         protected override void TearDown() \r
38         {\r
39         }\r
40 \r
41         public static ITest Suite {\r
42                 get { \r
43                         return new TestSuite(typeof(SortedListTest)); \r
44                 }\r
45         }\r
46         \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
51         }\r
52         \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
58         }\r
59 \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
66                 \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
71 \r
72                 try {\r
73                         d=null;\r
74                         temp1 = new SortedList(d);\r
75                         Fail ("sl.constructor-3: does not throw ArgumentNullException");\r
76                 } catch (ArgumentNullException) {}\r
77                 try {\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
83                         d.Add(7987,"lkj");\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
89                 }\r
90         }\r
91         \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
96                 try {\r
97                         temp1 = new SortedList(-6);\r
98                         Fail ("sl.constructor-4: does not throw ArgumentOutOfRangeException, with negative values");\r
99                 } catch (ArgumentOutOfRangeException) {}\r
100                 try {\r
101                         temp1 = new SortedList(0);\r
102                 } catch (ArgumentOutOfRangeException) {\r
103                         Fail ("sl.constructor-4: throws ArgumentOutOfRangeException with 0");\r
104                 }\r
105         }\r
106 \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
112                 try {\r
113                         temp1 = new SortedList(-12);\r
114                         Fail ("sl.constructor-5: does not throw ArgumentOutOfRangeException, with negative values");\r
115                 } catch (ArgumentOutOfRangeException) {}\r
116         }\r
117 \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
124         }\r
125 \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
130                                      i, sl1.Capacity);\r
131                 }\r
132         }\r
133 \r
134         public void TestCount() {\r
135                 {\r
136                         SortedList sl1 = new SortedList();\r
137                         AssertEquals("Bad initial count",\r
138                                      0, sl1.Count);\r
139                         for (int i = 1; i <= 100; i++) {\r
140                                 sl1.Add(""+i,""+i);\r
141                                 AssertEquals("Bad count " + i,\r
142                                              i, sl1.Count);\r
143                         }\r
144                 }\r
145         }\r
146 \r
147         public void TestIsFixed() {\r
148                 SortedList sl1 = new SortedList();\r
149                 Assert("should not be fixed by default", !sl1.IsFixedSize);\r
150         }\r
151 \r
152         public void TestIsReadOnly() {\r
153                 SortedList sl1 = new SortedList();\r
154                 Assert("should not be ReadOnly by default", !sl1.IsReadOnly);\r
155         }\r
156 \r
157 \r
158         public void TestItem() {\r
159                 SortedList sl1 = new SortedList();\r
160                 string key = null;\r
161                 {\r
162                         try {\r
163                                 object o = sl1[-1];\r
164                         } catch (ArgumentNullException) {\r
165                                 Fail ("sl.Item: throws ArgumentNullException with negative values");\r
166                         }\r
167                         try {\r
168                                 object o = sl1[key];\r
169                                 Fail ("sl.Item: does not throw ArgumentNullException with null key");\r
170                         } catch (ArgumentNullException) {}\r
171                 }\r
172 \r
173                 for (int i = 0; i <= 100; i++) {\r
174                         sl1.Add("kala "+i,i);\r
175                 }\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
179                 }\r
180         }\r
181 \r
182   public void TestSyncRoot()\r
183   {\r
184         SortedList sl1 = new SortedList();\r
185                 AssertEquals("sl.SyncRoot: does not function",false, sl1.SyncRoot == null);\r
186                 /*\r
187                 lock( sl1.SyncRoot ) {\r
188                         foreach ( Object item in sl1 ) {\r
189                                 item="asdf";\r
190                                 Assert ("sl.SyncRoot: item not read-only",item.IsReadOnly);\r
191                         }\r
192                 }\r
193                 */\r
194   }\r
195 \r
196         public void TestValues()\r
197         {\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
203                 }\r
204         }\r
205         \r
206         \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
211                 string key = null;\r
212                 {\r
213                         try {\r
214                                 sl1.Add(key,"kala");\r
215                                 Fail ("sl.Add: does not throw ArgumentNullException with null key");\r
216                         } catch (ArgumentNullException) {}\r
217                 }\r
218 \r
219                 {\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
224                         }\r
225                 }\r
226                 {\r
227                         try {\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
232                 }\r
233         }\r
234         \r
235         \r
236         public void TestClear() {\r
237                 {\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
243                         sl1.Clear();\r
244                         AssertEquals("sl.Clear: is not cleared",0, sl1.Count);\r
245                         AssertEquals("sl.Clear: capacity is altered",16, sl1.Capacity);\r
246                 }\r
247         }\r
248 \r
249         public void TestClone() {\r
250                 {\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
256                         }\r
257                 }\r
258                 {\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
264                         sl1.Add("d1",d10);\r
265                         sl1.Add("d2",d11);\r
266                         sl1.Add("d3",d12);\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
271                         \r
272                         ((char[])sl1["d1"])[0] = 'z';\r
273                         AssertEquals("s1.Clone: shallow copy", sl1["d1"], sl2["d1"]);\r
274                 }\r
275         }\r
276 \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
280 \r
281                 try {\r
282                         if (sl1.Contains(null)){}\r
283                         Fail ("sl.Contains: does not throw ArgumentNullException with null key");\r
284                 } catch (ArgumentNullException) {}\r
285                 \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
288         }\r
289 \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
293 \r
294                 try {\r
295                         if (sl1.ContainsKey(null)){}\r
296                         Fail ("sl.ContainsKey: does not throw ArgumentNullException with null key");\r
297                 } catch (ArgumentNullException) {}\r
298                 \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
301         }\r
302 \r
303         public void TestContainsValue() {\r
304                 SortedList sl1 = new SortedList(55);\r
305     sl1.Add(0, "zero");\r
306     sl1.Add(1, "one");\r
307     sl1.Add(2, "two");\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
313         }\r
314         \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
318                 {\r
319                         try {\r
320                                 sl1.CopyTo(null, 2);\r
321                                 Fail("sl.CopyTo: does not throw ArgumentNullException when target null");\r
322                         } catch (ArgumentNullException) {}\r
323                 }\r
324                 {\r
325                         try {\r
326                                 Char[,] c2 = new Char[2,2];\r
327                                 sl1.CopyTo(c2, 2);\r
328                                 Fail("sl.CopyTo: does not throw ArgumentException when target is multiarray");\r
329                         } catch (ArgumentException) {}\r
330                 }\r
331                 {\r
332                         try {\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
337                 }\r
338                 {\r
339                         try {\r
340                                 Char[] c1 = new Char[2];\r
341                                 sl1.CopyTo(c1, 3);\r
342                                 Fail("sl.CopyTo: does not throw ArgumentException when index is too large");\r
343                         } catch (ArgumentException) {}\r
344                 }\r
345                 {\r
346                         try {\r
347                                 Char[] c1 = new Char[2];\r
348                                 sl1.CopyTo(c1, 1);\r
349                                 Fail("sl.CopyTo: does not throw ArgumentException when SortedList too big for the array");\r
350                         } catch (ArgumentException) {}\r
351                 }\r
352                 {\r
353                         try {\r
354                                 Char[] c2 = new Char[15];\r
355                                 sl1.CopyTo(c2, 0);\r
356                                 Fail("sl.CopyTo: does not throw InvalidCastException when incompatible data types");\r
357                         } catch (InvalidCastException) {}\r
358                 }\r
359 \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
363                 /*\r
364                                 sl1.Clear();\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
382                 */\r
383         }\r
384 \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
396                 return sl1;\r
397         }\r
398         \r
399         public IList DefaultValues() {\r
400                 IList il = new ArrayList();\r
401                 il.Add( "fox" );\r
402                 il.Add( "The" );\r
403                 il.Add( "quick" );\r
404                 il.Add( "jumped" );\r
405                 il.Add( "over" );\r
406                 il.Add( "the" );\r
407                 il.Add( "lazy" );\r
408                 il.Add( "dog" );\r
409                 il.Add( "brown" );\r
410                 return il;\r
411         }\r
412         \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
417                 try {\r
418                         sl1.GetByIndex(-1);\r
419                         Fail("sl.GetByIndex: does not throw ArgumentOutOfRangeException with negative index");\r
420                 } catch (ArgumentOutOfRangeException) {}\r
421                 try {\r
422                         sl1.GetByIndex(100);\r
423                         Fail("sl.GetByIndex: does not throw ArgumentOutOfRangeException with too large index");\r
424                 } catch (ArgumentOutOfRangeException) {}\r
425         }\r
426 \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
433         }\r
434 \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
439                 try {\r
440                         sl1.GetKey(-1);\r
441                         Fail("sl.GetKey: does not throw ArgumentOutOfRangeException with negative index");\r
442                 } catch (ArgumentOutOfRangeException) {}\r
443                 try {\r
444                         sl1.GetKey(100);\r
445                         Fail("sl.GetKey: does not throw ArgumentOutOfRangeException with too large index");\r
446                 } catch (ArgumentOutOfRangeException) {}\r
447         }\r
448 \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
459         }\r
460 \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
470                 }\r
471 \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
475         }\r
476 \r
477         // TODO: IEnumerable.GetEnumerator [Explicit Interface Implementation]\r
478         /*\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
485         }\r
486         */\r
487 \r
488         public void TestIndexOfKey() {\r
489                 SortedList sl1 = new SortedList(24);\r
490                 string s=null;\r
491                 int t;\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
495                 }\r
496                 AssertEquals("sl.IndexOfKey: does not return -1 for non-existing key",sl1.IndexOfKey("kala "),-1);\r
497                 s=null;\r
498                 try {\r
499                         t=sl1.IndexOfKey(s);\r
500                         Fail("sl.IndexOfKey: ArgumentNullException not caught, when key is null");\r
501                 }\r
502                 catch (ArgumentNullException) {}\r
503                 try {\r
504                         t=sl1.IndexOfKey(10);\r
505                         Fail("sl.IndexOfKey: InvalidOperationException not caught, when key invalid");\r
506                 }\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
511                 }\r
512         }\r
513 \r
514         public void TestIndexOfValue() {\r
515                 SortedList sl1 = new SortedList(24);\r
516                 string s=null;\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
520                 }\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
524                 }\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
529                 }\r
530         }\r
531         \r
532 \r
533         public void TestRemove() {\r
534                 SortedList sl1 = new SortedList(24);\r
535                 string s=null;\r
536                 int k;\r
537                 for (int i = 0; i < 50; i++) sl1.Add("kala "+i,i);\r
538                 \r
539                 try {\r
540                         sl1.Remove(s);\r
541                         Fail("sl.Remove: ArgumentNullException not caught, when key is null");\r
542                 } catch (ArgumentNullException) {}\r
543                 k=sl1.Count;\r
544                 sl1.Remove("kala ");\r
545                 AssertEquals("sl.Remove: removes an item, when non-existing key given",sl1.Count,k);\r
546                 try {\r
547                         sl1.Remove(15);\r
548                         Fail("sl.Remove: IComparer exception is not thrown");\r
549                 } catch (Exception) {}\r
550 \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
553                 \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
557         }\r
558 \r
559         public void TestRemoveAt() {\r
560                 SortedList sl1 = new SortedList(24);\r
561                 int k;\r
562                 string s=null;\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
566                 }\r
567                 \r
568                 try {\r
569                         sl1.RemoveAt(-1);\r
570                         Fail("sl.RemoveAt: ArgumentOutOfRangeException not caught, when key is out of range");\r
571                 } catch (ArgumentOutOfRangeException) {}\r
572                 try {\r
573                         sl1.RemoveAt(100);\r
574                         Fail("sl.RemoveAt: ArgumentOutOfRangeException not caught, when key is out of range");\r
575                 } catch (ArgumentOutOfRangeException) {}\r
576                 k=sl1.Count;\r
577 \r
578                 for (int i=0; i<20; i++) sl1.RemoveAt(9);\r
579                 \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
587         }\r
588 \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
592                 \r
593                 try {\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
597                 try {\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
601 \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
609 \r
610         }\r
611 \r
612         public void TestTrimToSize() {\r
613                 SortedList sl1 = new SortedList(24);\r
614                 \r
615                 sl1.TrimToSize();\r
616                 AssertEquals("sl.TrimToSize: incorrect capacity after trimming empty list",icap,sl1.Capacity);\r
617                 \r
618                 for (int i = 72; i>=0; i--) sl1.Add(100+i,i);\r
619                 sl1.TrimToSize();\r
620                 AssertEquals("sl.TrimToSize: incorrect capacity after trimming a list",73,sl1.Capacity);\r
621         }\r
622 }\r
623 \r
624 }\r