2005-12-19 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / Test / System.Collections.Generic / ListTest.cs
1 //
2 // MonoTests.System.Collections.Generic.Test.DictionaryTest
3 //
4 // Authors:
5 //      David Waite (mass@akuma.org)
6 //
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
8 // Copyright (C) 2005 David Waite (mass@akuma.org)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 #if NET_2_0
31
32 using System;
33 using System.Collections;
34 using System.Collections.Generic;
35 using System.Collections.ObjectModel;
36 using System.Text;
37 using NUnit.Framework;
38
39 namespace MonoTests.System.Collections.Generic {
40
41         class GenericComparer<T> : IComparer<T> {
42
43                 private bool called = false;
44
45                 public bool Called {
46                         get {
47                                 bool result = called;
48                                 called = false;
49                                 return called;
50                         }
51                 }
52
53                 public int Compare (T x, T y)
54                 {
55                         called = true;
56                         return 0;
57                 }
58         }
59
60         [TestFixture]
61         public class ListTest
62         {
63
64                 int [] _list1_contents;
65                 List <int> _list1;
66
67                 [SetUp]
68                 public void SetUp ()
69                 {
70                         // FIXME arrays currently do not support generic collection
71                         // interfaces
72                         _list1_contents = new int [] { 55, 50, 22, 80, 56, 52, 40, 63 };
73                         // _list1 = new List <int> (_list1_contents);
74                         
75                         _list1 = new List <int> (8);
76                         foreach (int i in _list1_contents)
77                                 _list1.Add (i);
78                 }
79
80                 [Test]  // This was for bug #74980
81                 public void InsertTest ()
82                 {
83                         List <string> test = new List <string> ();
84                         test.Insert (0, "a");
85                         test.Insert (0, "b");
86                         test.Insert (1, "c");
87
88                         Assert.AreEqual (3, test.Count);
89                         Assert.AreEqual ("b", test [0]);
90                         Assert.AreEqual ("c", test [1]);
91                         Assert.AreEqual ("a", test [2]);
92                 }
93
94                 [Test]
95                 public void InsertRangeTest ()
96                 {
97                         int count = _list1.Count;
98                         // FIXME arrays currently do not support generic collection 
99                         // interfaces
100                         int [] items = {1, 2, 3};
101                         // List <int> newRange = new List <int> (items);
102                         List <int> newRange = new List <int> (3);
103                         foreach (int i in items)
104                                    newRange.Add (i);
105                         _list1.InsertRange (1, newRange);
106                         Assert.AreEqual (count + 3, _list1.Count);
107                         Assert.AreEqual (55, _list1 [0]);
108                         Assert.AreEqual (1, _list1 [1]);
109                         Assert.AreEqual (2, _list1 [2]);
110                         Assert.AreEqual (3, _list1 [3]);
111                         Assert.AreEqual (50, _list1 [4]);
112                 }
113
114                 [Test, ExpectedException (typeof (ArgumentNullException))]
115                 public void InsertRangeNullTest ()
116                 {
117                         IEnumerable <int> n = null;
118                         _list1.InsertRange (0, n);
119                 }
120
121                 [Test]
122                 public void IndexOfTest ()
123                 {
124                         List <int> l = new List <int> ();
125
126                         l.Add (100);
127                         l.Add (200);
128
129                         Assert.AreEqual (1, l.IndexOf (200), "Could not find value");
130                 }
131
132                 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
133                 public void IndexOfOutOfRangeTest ()
134                 {
135                         List <int> l = new List <int> (4);
136                         l.IndexOf (0, 0, 4);
137                 }
138
139                 [Test]
140                 public void GetRangeTest ()
141                 {
142                         List <int> r = _list1.GetRange (2, 4);
143                         Assert.AreEqual (4, r.Count);
144                         Assert.AreEqual (22, r [0]);
145                         Assert.AreEqual (80, r [1]);
146                         Assert.AreEqual (56, r [2]);
147                         Assert.AreEqual (52, r [3]);
148                 }
149
150                 [Test]
151                 public void EnumeratorTest ()
152                 {
153                         List <int>.Enumerator e = _list1.GetEnumerator ();
154                         for (int i = 0; i < _list1_contents.Length; i++)
155                         {
156                                 Assert.IsTrue (e.MoveNext ());
157                                 Assert.AreEqual (_list1_contents [i], e.Current);
158                         }
159                         Assert.IsFalse (e.MoveNext ());
160                 }
161
162                 [Test]
163                 public void ConstructWithSizeTest ()
164                 {
165                         List <object> l_1 = new List <object> (1);
166                         List <object> l_2 = new List <object> (50);
167                         List <object> l_3 = new List <object> (0);
168
169                         Assert.AreEqual (1, l_1.Capacity);
170                         Assert.AreEqual (50, l_2.Capacity);
171                         Assert.AreEqual (0, l_3.Capacity);
172                 }
173
174                 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
175                 public void ConstructWithInvalidSizeTest ()
176                 {
177                         List <int> l = new List <int> (-1);
178                 }
179
180                 [Test]
181                 public void ConstructWithCollectionTest ()
182                 {
183                         List <int> l1 = new List <int> (_list1);
184                         Assert.AreEqual (_list1.Count, l1.Count);
185                         Assert.AreEqual (l1.Count, l1.Capacity);
186                         for (int i = 0; i < l1.Count; i++)
187                                 Assert.AreEqual (_list1 [i], l1 [i]);
188                 }
189
190                 [Test, ExpectedException (typeof (ArgumentNullException))]
191                 public void ConstructWithInvalidCollectionTest ()
192                 {
193                         List <int> n = null;
194                         List <int> l1 = new List <int> (n);
195                 }
196
197                 [Test]
198                 public void AddTest ()
199                 {
200                         int count = _list1.Count;
201                         _list1.Add (-1);
202                         Assert.AreEqual (count + 1, _list1.Count);
203                         Assert.AreEqual (-1, _list1 [_list1.Count - 1]);
204                 }
205
206                 [Test]
207                 public void AddRangeTest ()
208                 {
209                         int count = _list1.Count;
210                         // FIXME arrays currently do not support generic collection
211                         // interfaces
212                         int [] range = { -1, -2, -3 };
213                         List <int> tmp = new List <int> (3);
214                         foreach (int i in range)
215                                 tmp.Add (i);
216                         // _list1.AddRange (range);
217                         _list1.AddRange (tmp);
218                         
219                         Assert.AreEqual (count + 3, _list1.Count);
220                         Assert.AreEqual (-1, _list1 [_list1.Count - 3]);
221                         Assert.AreEqual (-2, _list1 [_list1.Count - 2]);
222                         Assert.AreEqual (-3, _list1 [_list1.Count - 1]);
223                 }
224
225                 [Test, ExpectedException (typeof (ArgumentNullException))]
226                 public void AddNullRangeTest ()
227                 {
228                         int [] n = null;
229                         _list1.AddRange (n);
230                 }
231
232                 [Test]
233                 public void BinarySearchTest ()
234                 {
235                         List <int> l = new List <int> (_list1);
236                         l.Sort ();
237                         Assert.AreEqual (0, l.BinarySearch (22));
238                         Assert.AreEqual (-2, l.BinarySearch (23));
239                         Assert.AreEqual (- (l.Count + 1), l.BinarySearch (int.MaxValue));
240                 }
241
242                 [Test]
243                 public void SortTest ()
244                 {
245                         List <int> l = new List <int> (_list1);
246                         l.Sort ();
247                         Assert.AreEqual (_list1.Count, l.Count);
248                         Assert.AreEqual (22, l [0]);
249                         int minimum = 22;
250                         foreach (int i in l)
251                         {
252                                 Assert.IsTrue (minimum <= i);
253                                 minimum = i;
254                         }
255                 }
256
257                 [Test]
258                 public void ClearTest ()
259                 {
260                         int capacity = _list1.Capacity;
261                         _list1.Clear ();
262                         Assert.AreEqual (0, _list1.Count);
263                         Assert.AreEqual (capacity, _list1.Capacity);
264                 }
265
266                 [Test]
267                 public void ContainsTest ()
268                 {
269                         Assert.IsTrue (_list1.Contains (22));
270                         Assert.IsFalse (_list1.Contains (23));
271                 }
272
273                 private string StringConvert (int i)
274                 {
275                         return i.ToString ();
276                 }
277                 
278                 [Test]
279                 public void ConvertAllTest ()
280                 {
281                         List <string> s = _list1.ConvertAll ( (Converter <int, string>)StringConvert);
282                         Assert.AreEqual (_list1.Count, s.Count);
283                         Assert.AreEqual ("55", s [0]);
284                 }
285
286                 [Test]
287                 public void CopyToTest ()
288                 {
289                         int [] a = new int [2];
290                         _list1.CopyTo (1, a, 0, 2);
291                         Assert.AreEqual (50, a [0]);
292                         Assert.AreEqual (22, a [1]);
293
294                         int [] b = new int [_list1.Count + 1];
295                         b [_list1.Count] = 555;
296                         _list1.CopyTo (b);
297                         Assert.AreEqual (55, b [0]);
298                         Assert.AreEqual (555, b [_list1.Count]);
299
300                         b [0] = 888;
301                         _list1.CopyTo (b, 1);
302                         Assert.AreEqual (888, b [0]);
303                         Assert.AreEqual (55, b [1]);
304                 }
305
306                 [Test, ExpectedException (typeof (ArgumentNullException))]
307                 public void CopyToNullTest ()
308                 {
309                         int [] a = null;
310                         _list1.CopyTo (0, a, 0, 0);
311                 }
312
313                 static bool FindMultipleOfThree (int i)
314                 {
315                         return (i % 3) == 0;
316                 }
317
318                 static bool FindMultipleOfFour (int i)
319                 {
320                         return (i % 4) == 0;
321                 }
322
323                 static bool FindMultipleOfTwelve (int i)
324                 {
325                         return (i % 12) == 0;
326                 }
327
328                 [Test]
329                 public void FindTest ()
330                 {
331                         int i = _list1.Find (FindMultipleOfThree);
332                         Assert.AreEqual (63, i);
333
334                         i = _list1.Find (FindMultipleOfTwelve);
335                         Assert.AreEqual (default (int), i);
336                 }
337
338                 [Test, ExpectedException (typeof (ArgumentNullException))]
339                 public void FindNullTest ()
340                 {
341                         int i = _list1.Find (null);
342                 }
343
344                 [Test]
345                 public void FindAllTest ()
346                 {
347                         List <int> findings = _list1.FindAll (FindMultipleOfFour);
348                         Assert.AreEqual (4, findings.Count);
349                         Assert.AreEqual (80, findings [0]);
350                         Assert.AreEqual (56, findings [1]);
351                         Assert.AreEqual (52, findings [2]);
352                         Assert.AreEqual (40, findings [3]);
353
354                         findings = _list1.FindAll (FindMultipleOfTwelve);
355                         Assert.IsNotNull (findings);
356                         Assert.AreEqual (0, findings.Count);
357                 }
358
359                 [Test, ExpectedException (typeof (ArgumentNullException))]
360                 public void FindAllNullTest ()
361                 {
362                         List <int> findings = _list1.FindAll (null);
363                 }
364
365                 [Test]
366                 public void FindIndexTest ()
367                 {
368                         int i = _list1.FindIndex (FindMultipleOfThree);
369                         Assert.AreEqual (7, i);
370
371                         i = _list1.FindIndex (FindMultipleOfTwelve);
372                         Assert.AreEqual (-1, i);
373                 }
374
375                 [Test, ExpectedException (typeof (ArgumentNullException))]
376                 public void FindIndexNullTest ()
377                 {
378                         int i = _list1.FindIndex (null);
379                 }
380
381                 [Test]
382                 public void FindLastTest ()
383                 {
384                         int i = _list1.FindLast (FindMultipleOfFour);
385                         Assert.AreEqual (40, i);
386
387                         i = _list1.FindLast (FindMultipleOfTwelve);
388                         Assert.AreEqual (default (int), i);
389                 }
390
391                 [Test, ExpectedException (typeof (ArgumentNullException))]
392                 public void FindLastNullTest ()
393                 {
394                         int i = _list1.FindLast (null);
395                 }
396
397                 // FIXME currently generates Invalid IL Code error
398                 /*
399                 [Test]
400                 public void ForEachTest ()
401                 {
402                         int i = 0;
403                         _list1.ForEach (delegate (int j) { i += j; });
404
405                         Assert.AreEqual (418, i);
406                 }
407                 */
408                 [Test]
409                 public void FindLastIndexTest ()
410                 {
411                         int i = _list1.FindLastIndex (FindMultipleOfFour);
412                         Assert.AreEqual (6, i);
413
414                         i = _list1.FindLastIndex (5, FindMultipleOfFour);
415                         Assert.AreEqual (5, i);
416
417                         i = _list1.FindIndex (FindMultipleOfTwelve);
418                         Assert.AreEqual (-1, i);
419                 }
420
421                 [Test, ExpectedException (typeof (ArgumentNullException))]
422                 public void FindLastIndexNullTest ()
423                 {
424                         int i = _list1.FindLastIndex (null);
425                 }
426
427                 [Test]
428                 public void RemoveTest ()
429                 {
430                         int count = _list1.Count;
431                         bool result = _list1.Remove (22);
432                         Assert.IsTrue (result);
433                         Assert.AreEqual (count - 1, _list1.Count);
434
435                         Assert.AreEqual (-1, _list1.IndexOf (22));
436
437                         result = _list1.Remove (0);
438                         Assert.IsFalse (result);
439                 }
440
441                 [Test]
442                 public void RemoveAllTest ()
443                 {
444                         int count = _list1.Count;
445                         int removedCount = _list1.RemoveAll (FindMultipleOfFour);
446                         Assert.AreEqual (4, removedCount);
447                         Assert.AreEqual (count - 4, _list1.Count);
448
449                         removedCount = _list1.RemoveAll (FindMultipleOfTwelve);
450                         Assert.AreEqual (0, removedCount);
451                         Assert.AreEqual (count - 4, _list1.Count);
452                 }
453
454                 [Test]
455                 public void RemoveAtTest ()
456                 {
457                         int count = _list1.Count;
458                         _list1.RemoveAt (0);
459                         Assert.AreEqual (count - 1, _list1.Count);
460                         Assert.AreEqual (50, _list1 [0]);
461                 }
462
463                 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
464                 public void RemoveOutOfRangeTest ()
465                 {
466                         _list1.RemoveAt (_list1.Count);
467                 }
468
469                 [Test]
470                 public void RemoveRangeTest ()
471                 {
472                         int count = _list1.Count;
473                         _list1.RemoveRange (1, 2);
474                         Assert.AreEqual (count - 2, _list1.Count);
475                         Assert.AreEqual (55, _list1 [0]);
476                         Assert.AreEqual (80, _list1 [1]);
477
478                         _list1.RemoveRange (0, 0);
479                         Assert.AreEqual (count - 2, _list1.Count);
480                 }
481
482                 [Test]
483                 public void RemoveRangeFromEmptyListTest ()
484                 {
485                         List<int> l = new List<int> ();
486                         l.RemoveRange (0, 0);
487                 }
488
489                 [Test, ExpectedException (typeof (ArgumentException))]
490                 public void RemoveRangeOutOfRangeTest ()
491                 {
492                         _list1.RemoveRange (1, _list1.Count);
493                 }
494
495                 [Test]
496                 public void ReverseTest ()
497                 {
498                         int count = _list1.Count;
499                         _list1.Reverse ();
500                         Assert.AreEqual (count, _list1.Count);
501
502                         Assert.AreEqual (63, _list1 [0]);
503                         Assert.AreEqual (55, _list1 [count - 1]);
504
505                         _list1.Reverse (0, 2);
506
507                         Assert.AreEqual (40, _list1 [0]);
508                         Assert.AreEqual (63, _list1 [1]);
509                 }
510
511                 [Test, ExpectedException (typeof (ArgumentException))]
512                 public void ReverseOutOfRangeTest ()
513                 {
514                         _list1.Reverse (1, _list1.Count);
515                 }
516
517                 [Test]
518                 public void ToArrayTest ()
519                 {
520                         int [] copiedContents = _list1.ToArray ();
521                         Assert.IsFalse (ReferenceEquals (copiedContents, _list1_contents));
522
523                         Assert.AreEqual (_list1.Count, copiedContents.Length);
524                         Assert.AreEqual (_list1 [0], copiedContents [0]);
525                 }
526
527                 [Test]
528                 public void TrimExcessTest ()
529                 {
530                         List <string> l = new List <string> ();
531                         l.Add ("foo");
532
533                         Assert.IsTrue (l.Count < l.Capacity);
534                         l.TrimExcess ();
535                         Assert.AreEqual (l.Count, l.Capacity);
536                 }
537
538                 bool IsPositive (int i)
539                 {
540                         return i >= 0;
541                 }
542
543                 [Test]
544                 public void TrueForAllTest ()
545                 {
546                         Assert.IsFalse (_list1.TrueForAll (FindMultipleOfFour));
547                         Assert.IsTrue (_list1.TrueForAll (IsPositive));
548                 }
549
550                 [Test, ExpectedException (typeof (ArgumentNullException))]
551                 public void TrueForAllNullTest ()
552                 {
553                         _list1.TrueForAll (null);
554                 }
555
556                 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
557                 public void CapacityOutOfRangeTest ()
558                 {
559                         _list1.Capacity = _list1.Count - 1;
560                 }
561
562                 [Test] // bug 77030
563                 public void BinarySearch_EmptyList ()
564                 {
565                         GenericComparer<int> comparer = new GenericComparer<int> ();
566                         List<int> l = new List<int> ();
567                         l.BinarySearch (0, comparer);
568                         Assert.IsFalse (comparer.Called, "Called");
569                 }
570         }
571 }
572 #endif
573