Merge pull request #901 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / System / Test / System.Collections.Generic / SortedDictionaryTest.cs
index 52df3f67ffb2b01ebe0faa38226102357766f123..66b70374b61494138ed5ab86347f9e586a2dc636 100644 (file)
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-#if NET_2_0
-
 using System;
+using System.IO;
 using System.Collections;
 using System.Collections.Generic;
-using System.Runtime.Serialization;
+using System.Runtime.Serialization.Formatters.Binary;
 
 using NUnit.Framework;
 
 namespace MonoTests.System.Collections.Generic
 {
        [TestFixture]
-        public class SortedDictionaryTest
+       public class SortedDictionaryTest
        {
                [Test]
                public void CtorNullComparer ()
@@ -134,11 +133,9 @@ namespace MonoTests.System.Collections.Generic
 
                [Test]
                [ExpectedException (typeof (ArgumentNullException))]
-               [Category ("NotWorking")] // see bug #78019
                public void AddNullKeyNullable ()
                {
-                       SortedDictionary<Nullable<int>,string> d =
-                               new SortedDictionary<Nullable<int>,string> ();
+                       SortedDictionary<int?,string> d = new SortedDictionary<int?,string> ();
                        d.Add (null, "TEST");
                }
 
@@ -250,6 +247,30 @@ namespace MonoTests.System.Collections.Generic
                        Assert.IsNull (s, "#6");
                }
 
+               [Test]
+               public void CopyTo ()
+               {
+                       SortedDictionary<int,string> d =
+                               new SortedDictionary<int,string> ();                    
+                       d.Add (1, "A");                 
+                       KeyValuePair <int, string> [] array =
+                               new KeyValuePair <int, string> [d.Count];
+                       d.CopyTo (array, 0);
+                       Assert.AreEqual (1, array.Length);
+                       Assert.AreEqual (1, array [0].Key);
+                       Assert.AreEqual ("A", array [0].Value);
+                       
+                       d = new SortedDictionary<int,string> ();                        
+                       array = new KeyValuePair <int, string> [d.Count];
+                       d.CopyTo (array, 0);
+                       Assert.AreEqual (0, array.Length);
+                       
+                       ICollection c = new SortedDictionary<int,string> ();
+                       array = new KeyValuePair <int, string> [c.Count];
+                       c.CopyTo (array, 0);
+                       Assert.AreEqual (0, array.Length);
+               }
+
                [Test]
                [ExpectedException (typeof (ArgumentNullException))]
                public void IDictionaryAddKeyNull ()
@@ -313,6 +334,24 @@ namespace MonoTests.System.Collections.Generic
                        col.Remove (1);
                }
 
+               [Test]          
+               public void KeysICollectionCopyTo ()
+               {
+                       SortedDictionary<int,string> d = new SortedDictionary<int, string> ();
+                       d.Add (1, "A");
+                       ICollection<int> col = d.Keys;
+                       int[] array = new int [col.Count];
+                       col.CopyTo (array, 0);
+                       Assert.AreEqual (1, array.Length);
+                       Assert.AreEqual (1, array [0]);
+                       
+                       // Bug #497720
+                       d = new SortedDictionary<int, string> ();                       
+                       col = d.Keys;
+                       array = new int [col.Count];
+                       col.CopyTo (array, 0);
+               }
+               
                [Test]
                [ExpectedException (typeof (NotSupportedException))]
                public void ValuesICollectionAdd ()
@@ -343,6 +382,23 @@ namespace MonoTests.System.Collections.Generic
                        col.Remove ("A");
                }
 
+               [Test]          
+               public void ValuesICollectionCopyTo ()
+               {
+                       SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
+                       d.Add (1, "A");
+                       ICollection<string> col = d.Values;
+                       string[] array = new string [col.Count];
+                       col.CopyTo (array, 0);
+                       Assert.AreEqual (1, array.Length);
+                       Assert.AreEqual ("A", array [0]);
+                       
+                       d = new SortedDictionary<int,string> ();                        
+                       col = d.Values;
+                       array = new string [col.Count];
+                       col.CopyTo (array, 0);
+               }
+
                [Test]
                public void KeysGetEnumerator1 ()
                {
@@ -406,8 +462,172 @@ namespace MonoTests.System.Collections.Generic
                        d.Add (4, "D");
                        e.MoveNext ();
                }
+
+
+               delegate void D ();
+               bool Throws (D d)
+               {
+                       try {
+                               d ();
+                               return false;
+                       } catch {
+                               return true;
+                       }
+               }
+
+               [Test]
+               // based on #491858, #517415
+               public void Enumerator_Current ()
+               {
+                       var e1 = new SortedDictionary<int,int>.Enumerator ();
+                       Assert.IsFalse (Throws (delegate { var x = e1.Current; GC.KeepAlive (x);}));
+
+                       var d = new SortedDictionary<int,int> ();
+                       var e2 = d.GetEnumerator ();
+                       Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x);}));
+                       e2.MoveNext ();
+                       Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x);}));
+                       e2.Dispose ();
+                       Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x);}));
+
+                       var e3 = ((IEnumerable<KeyValuePair<int,int>>) d).GetEnumerator ();
+                       Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x);}));
+                       e3.MoveNext ();
+                       Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x);}));
+                       e3.Dispose ();
+                       Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x);}));
+
+                       var e4 = ((IEnumerable) d).GetEnumerator ();
+                       Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x);}));
+                       e4.MoveNext ();
+                       Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x);}));
+                       ((IDisposable) e4).Dispose ();
+                       Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x);}));
+               }
+
+               [Test]
+               // based on #491858, #517415
+               public void KeyEnumerator_Current ()
+               {
+                       var e1 = new SortedDictionary<int,int>.KeyCollection.Enumerator ();
+                       Assert.IsFalse (Throws (delegate { var x = e1.Current; GC.KeepAlive (x); }));
+
+                       var d = new SortedDictionary<int,int> ().Keys;
+                       var e2 = d.GetEnumerator ();
+                       Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x); }));
+                       e2.MoveNext ();
+                       Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x); }));
+                       e2.Dispose ();
+                       Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x); }));
+
+                       var e3 = ((IEnumerable<int>) d).GetEnumerator ();
+                       Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x); }));
+                       e3.MoveNext ();
+                       Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x); }));
+                       e3.Dispose ();
+                       Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x); }));
+
+                       var e4 = ((IEnumerable) d).GetEnumerator ();
+                       Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x); }));
+                       e4.MoveNext ();
+                       Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x); }));
+                       ((IDisposable) e4).Dispose ();
+                       Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x); }));
+               }
+
+               [Test]
+               // based on #491858, #517415
+               public void ValueEnumerator_Current ()
+               {
+                       var e1 = new SortedDictionary<int,int>.ValueCollection.Enumerator ();
+                       Assert.IsFalse (Throws (delegate { var x = e1.Current; GC.KeepAlive (x); }));
+
+                       var d = new SortedDictionary<int,int> ().Values;
+                       var e2 = d.GetEnumerator ();
+                       Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x); }));
+                       e2.MoveNext ();
+                       Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x); }));
+                       e2.Dispose ();
+                       Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x); }));
+
+                       var e3 = ((IEnumerable<int>) d).GetEnumerator ();
+                       Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x); }));
+                       e3.MoveNext ();
+                       Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x); }));
+                       e3.Dispose ();
+                       Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x); }));
+
+                       var e4 = ((IEnumerable) d).GetEnumerator ();
+                       Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x); }));
+                       e4.MoveNext ();
+                       Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x); }));
+                       ((IDisposable) e4).Dispose ();
+                       Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x); }));
+               }
+
+               [Test]
+               public void ValueEnumeratorNotEmpty_Current ()
+               {
+                       var dict = new SortedDictionary<int, string> ();
+                       dict.Add (1, "1");
+
+                       IEnumerator e = new SortedDictionary<int, string>.ValueCollection (dict).GetEnumerator ();
+                       while (e.MoveNext()) {}
+
+                       Assert.IsTrue (Throws (delegate { var x = e.Current; }));
+               }
+
+               // Serialize a dictionary out and deserialize it back in again
+               SortedDictionary<int, string> Roundtrip(SortedDictionary<int, string> dic)
+               {
+                       BinaryFormatter bf = new BinaryFormatter ();
+                       MemoryStream stream = new MemoryStream ();
+                       bf.Serialize (stream, dic);
+                       stream.Position = 0;
+                       return (SortedDictionary<int, string>)bf.Deserialize (stream);
+               }
+           
+               [Test]
+               public void Serialize()
+               {
+                       SortedDictionary<int, string> test = new SortedDictionary<int, string>();
+                       test.Add(1, "a");
+                       test.Add(3, "c");
+                       test.Add(2, "b");
+
+                       SortedDictionary<int, string> result = Roundtrip(test);
+                       Assert.AreEqual(3, result.Count);
+
+                       Assert.AreEqual("a", result[1]);
+                       Assert.AreEqual("b", result[2]);
+                       Assert.AreEqual("c", result[3]);
+               }
+
+               [Test]
+               public void SerializeReverseComparer()
+               {
+                       SortedDictionary<int,string> test =
+                               new SortedDictionary<int,string> (
+                                       ReverseComparer<int>.Instance);
+
+                       test.Add (1, "A");
+                       test.Add (3, "B");
+                       test.Add (2, "C");
+
+                       SortedDictionary<int,string> result = Roundtrip (test);
+                   
+                       SortedDictionary<int,string>.Enumerator e = result.GetEnumerator ();
+                       Assert.IsTrue (e.MoveNext (), "#1");
+                       Assert.AreEqual ("B", e.Current.Value, "#2");
+                       Assert.IsTrue (e.MoveNext (), "#3");
+                       Assert.AreEqual ("C", e.Current.Value, "#4");
+                       Assert.IsTrue (e.MoveNext (), "#5");
+                       Assert.AreEqual ("A", e.Current.Value, "#6");
+                       Assert.IsFalse (e.MoveNext (), "#7");
+               }
        }
 
+       [Serializable]
        class ReverseComparer<T> : IComparer<T>
        {
                static ReverseComparer<T> instance = new ReverseComparer<T> ();
@@ -425,5 +645,3 @@ namespace MonoTests.System.Collections.Generic
                }
        }
 }
-
-#endif