Merge pull request #900 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / System / Test / System.Collections.Generic / LinkedListTest.cs
index b96e94ad0e12c4233125865848385b1de8451df9..65b54087b29cb9ea7ce0978e1a91bdd4b23795b5 100644 (file)
@@ -12,6 +12,24 @@ namespace MonoTests.System.Collections.Generic
        [TestFixture]
        public class LinkedListTest
        {
+               class EquatableValue : IEquatable<EquatableValue>
+               {
+                       public readonly string Value;
+
+                       public EquatableValue (string value)
+                       {
+                               this.Value = value;
+                       }
+
+                       public bool Equals (EquatableValue other)
+                       {
+                               if (other == null)
+                                       return false;
+
+                               return string.Equals (Value, other.Value, StringComparison.OrdinalIgnoreCase);
+                       }
+               }
+
                LinkedList <int> intlist;
                LinkedList <string> strings;
 
@@ -84,8 +102,13 @@ namespace MonoTests.System.Collections.Generic
                [Test]
                public void ClearTest ()
                {
+                       LinkedListNode <int> node = intlist.First;
                        intlist.Clear ();
+
                        Assert.AreEqual (0, intlist.Count);
+                       Assert.AreEqual (2, node.Value);
+                       Assert.IsNull (node.Next);
+                       Assert.IsNull (node.Previous);
                }
 
                [Test]
@@ -127,6 +150,10 @@ namespace MonoTests.System.Collections.Generic
                        intlist.CopyTo (output, 0);
                        for (int i = 0; i < 3; i++)
                                Assert.AreEqual (values [i], output [i]);
+                       
+                       LinkedList <int> l = new LinkedList <int> ();
+                       values = new int [l.Count];
+                       l.CopyTo (values, 0);
                }
 
                [Test]
@@ -177,7 +204,7 @@ namespace MonoTests.System.Collections.Generic
                {
                        intlist.Remove (null);
                }
-
+               
                [Test, ExpectedException (typeof (InvalidOperationException))]
                public void RemoveInvalidNodeTest ()
                {
@@ -243,6 +270,74 @@ namespace MonoTests.System.Collections.Generic
                        }
                        Assert.AreEqual(3, i);
                }
+
+               public void EnumeratorAfterEnd ()
+               {
+                       var linkedList = new LinkedList<string> ();
+                       linkedList.AddLast ("a");
+                       var e = linkedList.GetEnumerator ();
+                       Assert.IsTrue (e.MoveNext (), "#1");
+                       Assert.IsFalse (e.MoveNext (), "#2");
+                       Assert.IsFalse (e.MoveNext (), "#3");
+               }
+               
+               [Test] //bug 481621
+               public void PlayWithNullValues ()
+               {
+                       LinkedList <string> li = new LinkedList <string> ();
+                       li.AddLast ((string)null);
+                       li.AddLast ("abcd");
+                       li.AddLast ((string)null);
+                       li.AddLast ("efgh");
+                       Assert.AreEqual (4, li.Count);
+                       Assert.AreEqual ("efgh", li.Last.Value);
+                       Assert.IsNull (li.First.Value);
+
+                       Assert.IsTrue (li.Remove ((string)null));
+                       Assert.AreEqual (3, li.Count);
+                       Assert.AreEqual ("efgh", li.Last.Value);
+                       Assert.AreEqual ("abcd", li.First.Value);
+
+                       Assert.IsTrue (li.Remove ((string)null));
+                       Assert.AreEqual (2, li.Count);
+                       Assert.AreEqual ("efgh", li.Last.Value);
+                       Assert.AreEqual ("abcd", li.First.Value);
+
+                       Assert.IsFalse (li.Remove ((string)null));
+                       Assert.AreEqual (2, li.Count);
+                       Assert.AreEqual ("efgh", li.Last.Value);
+                       Assert.AreEqual ("abcd", li.First.Value);
+               }
+
+               [Test]
+               public void EqualityComparer ()
+               {
+                       var list = new LinkedList<EquatableValue> ();
+                       var mv  = new EquatableValue ("first");
+                       list.AddFirst (mv);
+
+                       var test = new EquatableValue ("FIRST");
+                       Assert.IsTrue (list.Contains (test), "#1");
+                       Assert.AreSame (mv, list.Find (test).Value, "#2");
+                       Assert.AreSame (mv, list.FindLast (test).Value, "#3");
+               }
+
+               [Test]
+               public void RemoveFromEmptyList ()
+               {
+                       var linkedList = new LinkedList<string> ();
+                       try {
+                               linkedList.RemoveFirst ();
+                               Assert.Fail ("#1");
+                       } catch (InvalidOperationException) {
+                       }
+
+                       try {
+                               linkedList.RemoveLast ();
+                               Assert.Fail ("#1");
+                       } catch (InvalidOperationException) {
+                       }
+               }
        }
 }