In class/corlib/System.Collections.Generic:
[mono.git] / mcs / class / corlib / Test / System.Collections.Generic / ListTest.cs
index 473d6a07691f21a8fe19009bae7192174ea4e33e..50b482ca28e3d14e0380daee238599041e4f283b 100644 (file)
@@ -4,7 +4,7 @@
 // Authors:
 //      David Waite (mass@akuma.org)
 //
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
 // Copyright (C) 2005 David Waite (mass@akuma.org)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
@@ -109,6 +109,13 @@ namespace MonoTests.System.Collections.Generic {
                        Assert.AreEqual (2, _list1 [2]);
                        Assert.AreEqual (3, _list1 [3]);
                        Assert.AreEqual (50, _list1 [4]);
+
+                       newRange = new List <int> ();
+                       List <int> li = new List <int> ();
+                       li.Add (1);
+                       newRange.InsertRange (0, li);
+                       newRange.InsertRange (newRange.Count, li);
+                       Assert.AreEqual (2, newRange.Count);
                }
 
                [Test, ExpectedException (typeof (ArgumentNullException))]
@@ -118,6 +125,12 @@ namespace MonoTests.System.Collections.Generic {
                        _list1.InsertRange (0, n);
                }
 
+               [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
+               public void InsertRangeNegativeIndexTest ()
+               {
+                       _list1.InsertRange (-1, _list1);
+               }
+
                [Test]
                public void IndexOfTest ()
                {
@@ -559,14 +572,145 @@ namespace MonoTests.System.Collections.Generic {
                        _list1.Capacity = _list1.Count - 1;
                }
 
-               [Test] // bug 77030
+               [Test]
                public void BinarySearch_EmptyList ()
                {
                        GenericComparer<int> comparer = new GenericComparer<int> ();
                        List<int> l = new List<int> ();
-                       l.BinarySearch (0, comparer);
+                       Assert.AreEqual (-1, l.BinarySearch (0, comparer), "BinarySearch");
+                       // bug 77030 - the comparer isn't called for an empty array/list
+                       Assert.IsFalse (comparer.Called, "Called");
+               }
+
+               [Test]
+               public void BinarySearch2_EmptyList ()
+               {
+                       GenericComparer<int> comparer = new GenericComparer<int> ();
+                       List<int> l = new List<int> ();
+                       Assert.AreEqual (-1, l.BinarySearch (0, 0, 0, comparer), "BinarySearch");
+                       // bug 77030 - the comparer isn't called for an empty array/list
                        Assert.IsFalse (comparer.Called, "Called");
                }
+
+               [Test]
+               public void AddRange_Bug77019 ()
+               {
+                       List<int> l = new List<int> ();
+                       Dictionary<string, int> d = new Dictionary<string, int> ();
+                       l.AddRange (d.Values);
+                       Assert.AreEqual (0, l.Count, "Count");
+               }
+
+               class SortTestComparer: IComparer<string> {
+
+                       public int Compare (string s1, string s2)
+                       {
+                               return String.Compare (s1, s2);
+                       }
+               }
+
+               [Test]
+               public void Sort_Bug76361 ()
+               {
+                       SortTestComparer comparer = new SortTestComparer ();
+                       List<string> l = new List<string> ();
+                       l.Add ("foo");
+                       l.Add ("bar");
+                       l.Sort (comparer);
+                       Assert.AreEqual ("bar", l[0], "0");
+                       Assert.AreEqual ("foo", l[1], "1");
+                       Assert.AreEqual (2, l.Count, "Count");
+               }
+
+               // for bug #77039 test case
+               class GenericIComparable: IComparable<GenericIComparable> {
+                       private int _NumberToSortOn;
+
+                       public int NumberToSortOn {
+                               get { return _NumberToSortOn; }
+                               set { _NumberToSortOn = value; }
+                       }
+
+                       public GenericIComparable (int val)
+                       {
+                               _NumberToSortOn = val;
+                       }
+
+                       public int CompareTo (GenericIComparable other)
+                       {
+                               return NumberToSortOn.CompareTo (other.NumberToSortOn);
+                       }
+               }
+
+               [Test]
+               public void Sort_GenericIComparable_Bug77039 ()
+               {
+                       List<GenericIComparable> l = new List<GenericIComparable> ();
+                       l.Add (new GenericIComparable (2));
+                       l.Add (new GenericIComparable (1));
+                       l.Add (new GenericIComparable (3));
+                       l.Sort ();
+                       Assert.AreEqual (1, l[0].NumberToSortOn, "0");
+                       Assert.AreEqual (2, l[1].NumberToSortOn, "1");
+                       Assert.AreEqual (3, l[2].NumberToSortOn, "2");
+               }
+
+               class NonGenericIComparable: IComparable {
+                       private int _NumberToSortOn;
+
+                       public int NumberToSortOn {
+                               get { return _NumberToSortOn; }
+                               set { _NumberToSortOn = value; }
+                       }
+
+                       public NonGenericIComparable (int val)
+                       {
+                               _NumberToSortOn = val;
+                       }
+
+                       public int CompareTo (object obj)
+                       {
+                               return NumberToSortOn.CompareTo ((obj as NonGenericIComparable).NumberToSortOn);
+                       }
+               }
+
+               [Test]
+               public void Sort_NonGenericIComparable ()
+               {
+                       List<NonGenericIComparable> l = new List<NonGenericIComparable> ();
+                       l.Add (new NonGenericIComparable (2));
+                       l.Add (new NonGenericIComparable (1));
+                       l.Add (new NonGenericIComparable (3));
+                       l.Sort ();
+                       Assert.AreEqual (1, l[0].NumberToSortOn, "0");
+                       Assert.AreEqual (2, l[1].NumberToSortOn, "1");
+                       Assert.AreEqual (3, l[2].NumberToSortOn, "2");
+               }
+
+               class NonComparable {
+               }
+
+               [Test]
+               public void Sort_GenericNonIComparable ()
+               {
+                       List<NonComparable> l = new List<NonComparable> ();
+                       l.Sort ();
+                       // no element -> no sort -> no exception
+                       l.Add (new NonComparable ());
+                       l.Sort ();
+                       // one element -> no sort -> no exception
+               }
+
+               [Test]
+               [ExpectedException (typeof (InvalidOperationException))]
+               public void Sort_GenericNonIComparable_2 ()
+               {
+                       List<NonComparable> l = new List<NonComparable> ();
+                       l.Add (new NonComparable ());
+                       l.Add (new NonComparable ());
+                       l.Sort ();
+                       // two element -> sort -> exception!
+               }
        }
 }
 #endif