New test.
[mono.git] / mcs / class / corlib / Test / System.Collections.Generic / DictionaryTest.cs
index c727cee9ad5e5a5082956cd4897c2bb8e65d4c77..3192530e0eed1f448e9be86436167c89ea1a70a9 100644 (file)
@@ -341,10 +341,8 @@ namespace MonoTests.System.Collections.Generic {
                        IEnumerator itr = ((IEnumerable)_dictionary).GetEnumerator ();
                        while (itr.MoveNext ()) {
                                object o = itr.Current;
-                               Assert.AreEqual (typeof (DictionaryEntry), o.GetType (), "Current should return a type of DictionaryEntry");
-                               DictionaryEntry entry = (DictionaryEntry)itr.Current;
-                               if (entry.Key.ToString () == "key4")
-                                       entry.Value = "value33";
+                               Assert.AreEqual (typeof (KeyValuePair<string,object>), o.GetType (), "Current should return a type of KeyValuePair");
+                               KeyValuePair<string,object> entry = (KeyValuePair<string,object>) itr.Current;
                        }
                        Assert.AreEqual ("value4", _dictionary ["key4"].ToString (), "");
                }
@@ -360,7 +358,7 @@ namespace MonoTests.System.Collections.Generic {
                        IEnumerator <KeyValuePair <string, object>> itr = ((IEnumerable <KeyValuePair <string, object>>)_dictionary).GetEnumerator ();
                        while (itr.MoveNext ()) {
                                object o = itr.Current;
-                               Assert.AreEqual (typeof (KeyValuePair <string, object>), o.GetType (), "Current should return a type of DictionaryEntry");
+                               Assert.AreEqual (typeof (KeyValuePair <string, object>), o.GetType (), "Current should return a type of KeyValuePair<object,string>");
                                KeyValuePair <string, object> entry = (KeyValuePair <string, object>)itr.Current;
                        }
                        Assert.AreEqual ("value4", _dictionary ["key4"].ToString (), "");
@@ -378,9 +376,7 @@ namespace MonoTests.System.Collections.Generic {
                        while (itr.MoveNext ()) {
                                object o = itr.Current;
                                Assert.AreEqual (typeof (DictionaryEntry), o.GetType (), "Current should return a type of DictionaryEntry");
-                               DictionaryEntry entry = (DictionaryEntry)itr.Current;
-                               if (entry.Key.ToString () == "key4")
-                                       entry.Value = "value33";
+                               DictionaryEntry entry = (DictionaryEntry) itr.Current;
                        }
                        Assert.AreEqual ("value4", _dictionary ["key4"].ToString (), "");
        
@@ -396,23 +392,17 @@ namespace MonoTests.System.Collections.Generic {
        
                        int i = 0;
                        foreach (KeyValuePair <string, object> entry in _dictionary)
-                       {
                                i++;
-                       }
                        Assert.AreEqual(4, i, "fail1: foreach entry failed!");
        
                        i = 0;
-                       foreach (DictionaryEntry entry in ((IEnumerable)_dictionary))
-                       {
+                       foreach (KeyValuePair <string, object> entry in ((IEnumerable)_dictionary))
                                i++;
-                       }
                        Assert.AreEqual(4, i, "fail2: foreach entry failed!");
        
                        i = 0;
                        foreach (DictionaryEntry entry in ((IDictionary)_dictionary))
-                       {
                                i++;
-                       }
                        Assert.AreEqual (4, i, "fail3: foreach entry failed!");
                }
        
@@ -500,13 +490,14 @@ namespace MonoTests.System.Collections.Generic {
                [Test]
                public void PlainEnumeratorReturnTest ()
                {
-                       // Test that we return a DictionaryEntry for non-generic dictionary iteration
+                       // Test that we return a KeyValuePair even for non-generic dictionary iteration
                        _dictionary["foo"] = "bar";
                        IEnumerator<KeyValuePair<string, object>> enumerator = _dictionary.GetEnumerator();
-                       Assert.IsTrue(enumerator.MoveNext());
-                       Assert.IsTrue(((IEnumerator)enumerator).Current is DictionaryEntry);
-                       Assert.IsTrue(((IDictionaryEnumerator)enumerator).Current is DictionaryEntry);
-                       Assert.IsFalse(((object) enumerator.Current) is DictionaryEntry);
+                       Assert.IsTrue(enumerator.MoveNext(), "#1");
+                       Assert.AreEqual (typeof (KeyValuePair<string,object>), ((IEnumerator)enumerator).Current.GetType (), "#2");
+                       Assert.AreEqual (typeof (DictionaryEntry), ((IDictionaryEnumerator)enumerator).Entry.GetType (), "#3");
+                       Assert.AreEqual (typeof (KeyValuePair<string,object>), ((IDictionaryEnumerator)enumerator).Current.GetType (), "#4");
+                       Assert.AreEqual (typeof (KeyValuePair<string,object>), ((object) enumerator.Current).GetType (), "#5");
                }
 
                [Test, ExpectedException (typeof (InvalidOperationException))]
@@ -578,6 +569,118 @@ namespace MonoTests.System.Collections.Generic {
                                Assert.AreEqual(i, d3[i]);
                        }
                }
+
+               [Test]
+               public void ZeroCapacity ()
+               {
+                       Dictionary<int, int> x = new Dictionary <int, int> (0);
+                       x.Add (1, 2);
+                       
+                       x = new Dictionary <int, int> (0);
+                       x.Clear ();
+
+                       x = new Dictionary <int, int> (0);
+                       int aa = x.Count;
+                       
+                       x = new Dictionary <int, int> (0);
+                       try {
+                               int j = x [1];
+                       } catch (KeyNotFoundException){
+                       }
+
+                       bool b;
+                       b = x.ContainsKey (10);
+                       b = x.ContainsValue (10);
+
+                       x = new Dictionary <int, int> (0);
+                       x.Remove (10);
+                       
+                       x = new Dictionary <int, int> (0);
+                       int intv;
+                       x.TryGetValue (1, out intv);
+
+                       object oa = x.Keys;
+                       object ob = x.Values;
+                       foreach (KeyValuePair<int,int> a in x){
+                       }
+               }
+
+               [Test]
+               public void Empty_KeysValues_CopyTo ()
+               {
+                       Dictionary<int, int> d = new Dictionary<int, int> ();
+                       int[] array = new int[1];
+                       d.Keys.CopyTo (array, array.Length);
+                       d.Values.CopyTo (array, array.Length);
+               }
+
+               [Test]
+               public void Empty_CopyTo ()
+               {
+                       Dictionary<int, int> d = new Dictionary<int, int> ();
+                       ICollection c = (ICollection) d;
+                       DictionaryEntry [] array = new DictionaryEntry [1];
+                       c.CopyTo (array, array.Length);
+
+                       ICollection<KeyValuePair<int,int>> c2 = d;
+                       KeyValuePair<int,int> [] array2 = new KeyValuePair<int,int> [1];
+                       c2.CopyTo (array2, array2.Length);
+               }
+
+               [Test]
+               public void IDictionary_Contains ()
+               {
+                       IDictionary d = new Dictionary<int, int> ();
+                       d.Add (1, 2);
+                       Assert.IsTrue (d.Contains (1));
+                       Assert.IsFalse (d.Contains (2));
+                       Assert.IsFalse (d.Contains ("x"));
+               }
+
+               [Test, ExpectedException (typeof (ArgumentNullException))]
+               public void IDictionary_Contains2 ()
+               {
+                       IDictionary d = new Dictionary<int, int> ();
+                       d.Contains (null);
+               }
+
+               [Test, ExpectedException (typeof (ArgumentNullException))]
+               public void IDictionary_Add1 ()
+               {
+                       IDictionary d = new Dictionary<int, int> ();
+                       d.Add (null, 1);
+               }
+
+               [Test, ExpectedException (typeof (ArgumentException))]
+               public void IDictionary_Add2 ()
+               {
+                       IDictionary d = new Dictionary<int, int> ();
+                       d.Add ("bar", 1);
+               }
+
+               [Test, ExpectedException (typeof (ArgumentException))]
+               public void IDictionary_Add3 ()
+               {
+                       IDictionary d = new Dictionary<int, int> ();
+                       d.Add (1, "bar");
+               }
+
+               [Test]
+               public void IDictionary_Remove1 ()
+               {
+                       IDictionary d = new Dictionary<int, int> ();
+                       d.Add (1, 2);
+                       d.Remove (1);
+                       d.Remove (5);
+                       d.Remove ("foo");
+               }
+
+               [Test, ExpectedException (typeof (ArgumentNullException))]
+               public void IDictionary_Remove2 ()
+               {
+                       IDictionary d = new Dictionary<int, int> ();
+                       d.Remove (null);
+               }
        }
 }