2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / Test / System.Collections / SortedListTest.cs
index a27fab9aa2e7144b2629c8692e818c5fd405c582..72c0ff74f88cd6ff8181ccb6681c43f52defb81a 100755 (executable)
@@ -1,6 +1,8 @@
 // SortedListTest.cs - NUnit Test Cases for the System.Collections.SortedList class\r
 //\r
-// Jaak Simm\r
+// Authors:\r
+//      Jaak Simm\r
+//      Duncan Mak (duncan@ximian.com)\r
 //\r
 // Thanks go to David Brandt (bucky@keystreams.com),\r
 // because this file is based on his ArrayListTest.cs\r
@@ -19,37 +21,20 @@ using NUnit.Framework;
 \r
 namespace MonoTests.System.Collections {\r
 \r
-\r
-/// <summary>SortedList test.</summary>\r
-public class SortedListTest : TestCase {\r
-       public SortedListTest() : base ("MonoTests.System.SortedListTest testsuite") {}\r
-       public SortedListTest(string name) : base(name) {}\r
-\r
+[TestFixture]\r
+public class SortedListTest : Assertion {\r
        protected SortedList sl1;\r
        protected SortedList sl2;\r
        protected SortedList emptysl;\r
        protected const int icap=16;\r
-               \r
-       protected override void SetUp() \r
-       {\r
-       }\r
 \r
-       protected override void TearDown() \r
-       {\r
-       }\r
-\r
-       public static ITest Suite {\r
-               get { \r
-                       return new TestSuite(typeof(SortedListTest)); \r
-               }\r
-       }\r
-       \r
        public void TestConstructor1() {\r
                SortedList temp1 = new SortedList();\r
                AssertNotNull("sl.constructor-1: returns null", temp1);\r
                AssertEquals("sl.constructor-1: incorrect initial capacity", icap, temp1.Capacity);\r
        }\r
-       \r
+\r
+        [Test]\r
        public void TestConstructor2() {\r
                Comparer c = Comparer.Default;\r
                SortedList temp1 = new SortedList(c);\r
@@ -57,6 +42,7 @@ public class SortedListTest : TestCase {
                AssertEquals("sl.constructor-2: incorrect initial capacity", icap, temp1.Capacity);\r
        }\r
 \r
+        [Test]\r
        public void TestConstructor3() {\r
                Hashtable d = new Hashtable();\r
                d.Add("one", "Mircosoft");\r
@@ -88,7 +74,8 @@ public class SortedListTest : TestCase {
                        Fail ("Unexpected Exception throw: e=" + e);\r
                }\r
        }\r
-       \r
+\r
+        [Test] \r
        public void TestConstructor4() {\r
                SortedList temp1 = new SortedList(17);\r
                AssertNotNull("sl.constructor-4: returns null", temp1);\r
@@ -104,6 +91,7 @@ public class SortedListTest : TestCase {
                }\r
        }\r
 \r
+        [Test] \r
        public void TestConstructor5() {\r
                Comparer c = Comparer.Default;\r
                SortedList temp1 = new SortedList(c,27);\r
@@ -115,8 +103,25 @@ public class SortedListTest : TestCase {
                } catch (ArgumentOutOfRangeException) {}\r
        }\r
 \r
-/*\r
-       FIXME: Once this is done in the class, re-enable the test\r
+       [Test]\r
+       public void Constructor_Capacity () \r
+       {\r
+               SortedList sl = new SortedList (0);\r
+               AssertEquals ("Capacity-Original", 0, sl.Capacity);\r
+               sl.Capacity = 0;\r
+               // doesn't reset to class default (16)\r
+               AssertEquals ("Capacity-Resetted", 0, sl.Capacity);\r
+\r
+               for (int i=1; i <= 16; i++) {\r
+                       sl = new SortedList (i);\r
+                       AssertEquals ("Capacity-Original" + i.ToString (), i, sl.Capacity);\r
+                       sl.Capacity = 0;\r
+                       // reset to class default (16)\r
+                       AssertEquals ("Capacity-Resetted" + i.ToString (), 16, sl.Capacity);\r
+               }\r
+       }\r
+\r
+        [Test] \r
        public void TestIsSynchronized() {\r
                SortedList sl1 = new SortedList();\r
                Assert("sl: should not be synchronized by default", \r
@@ -124,7 +129,8 @@ public class SortedListTest : TestCase {
                SortedList sl2 = SortedList.Synchronized(sl1);\r
                Assert("sl: synchronized wrapper not working", sl2.IsSynchronized);\r
        }\r
-*/\r
+\r
+        [Test] \r
        public void TestCapacity() {\r
                for (int i = 0; i < 100; i++) {\r
                        SortedList sl1 = new SortedList(i);\r
@@ -133,6 +139,54 @@ public class SortedListTest : TestCase {
                }\r
        }\r
 \r
+        [Test]\r
+        public void TestCapacity2 ()\r
+        {\r
+                SortedList list = new SortedList ();\r
+\r
+                list.Capacity = 5;\r
+                AssertEquals (5, list.Capacity);\r
+\r
+               SortedList sync = SortedList.Synchronized (list);\r
+                AssertEquals (5, sync.Capacity);\r
+\r
+               list.Capacity = 20;\r
+                AssertEquals (20, list.Capacity);\r
+                AssertEquals (20, sync.Capacity);\r
+        }\r
+\r
+        [Test]\r
+        public void TestCapacity3 ()\r
+        {\r
+               int new_capacity = 5;\r
+                SortedList list = new SortedList (1000);\r
+                list.Capacity = new_capacity;\r
+\r
+#if NET_1_1\r
+                AssertEquals (new_capacity, list.Capacity);\r
+#else\r
+                AssertEquals (16, list.Capacity);\r
+#endif\r
+        }\r
+\r
+       [Test]\r
+       public void Capacity_BackTo0 () \r
+       {\r
+               SortedList list = new SortedList (42);\r
+               AssertEquals ("42", 42, list.Capacity);\r
+               list.Capacity = 0;\r
+               AssertEquals ("0(16)", 16, list.Capacity);\r
+       }\r
+\r
+        [Test]\r
+        [ExpectedException (typeof (OutOfMemoryException))]\r
+        public void TestCapacity4 ()\r
+        {\r
+                SortedList list = new SortedList ();\r
+                list.Capacity = Int32.MaxValue;\r
+        }\r
+\r
+        [Test] \r
        public void TestCount() {\r
                {\r
                        SortedList sl1 = new SortedList();\r
@@ -146,17 +200,21 @@ public class SortedListTest : TestCase {
                }\r
        }\r
 \r
+        [Test] \r
        public void TestIsFixed() {\r
                SortedList sl1 = new SortedList();\r
                Assert("should not be fixed by default", !sl1.IsFixedSize);\r
        }\r
 \r
+\r
+        [Test] \r
        public void TestIsReadOnly() {\r
                SortedList sl1 = new SortedList();\r
                Assert("should not be ReadOnly by default", !sl1.IsReadOnly);\r
        }\r
 \r
 \r
+        [Test] \r
        public void TestItem() {\r
                SortedList sl1 = new SortedList();\r
                string key = null;\r
@@ -181,9 +239,10 @@ public class SortedListTest : TestCase {
                }\r
        }\r
 \r
-  public void TestSyncRoot()\r
-  {\r
-       SortedList sl1 = new SortedList();\r
+        [Test]\r
+        public void TestSyncRoot()\r
+        {\r
+                SortedList sl1 = new SortedList();\r
                AssertEquals("sl.SyncRoot: does not function",false, sl1.SyncRoot == null);\r
                /*\r
                lock( sl1.SyncRoot ) {\r
@@ -193,8 +252,9 @@ public class SortedListTest : TestCase {
                        }\r
                }\r
                */\r
-  }\r
+        }\r
 \r
+        [Test]\r
        public void TestValues()\r
        {\r
        SortedList sl1 = new SortedList();\r
@@ -207,6 +267,7 @@ public class SortedListTest : TestCase {
        \r
        \r
        // TODO: Add with IComparer\r
+        [Test]\r
        public void TestAdd() {\r
                // seems SortedList cannot be set fixedsize or readonly\r
                SortedList sl1 = new SortedList();\r
@@ -233,7 +294,8 @@ public class SortedListTest : TestCase {
                        } catch (ArgumentException) {}\r
                }\r
        }\r
-       \r
+\r
+        [Test]\r
        public void TestClear() {\r
                SortedList sl1 = new SortedList(10);\r
                sl1.Add("kala", 'c');\r
@@ -245,6 +307,33 @@ public class SortedListTest : TestCase {
                AssertEquals("sl.Clear: capacity is altered", 16, sl1.Capacity);\r
        }\r
 \r
+       [Test]\r
+       public void Clear_Capacity () \r
+       {\r
+               // strangely Clear change the default capacity (while Capacity doesn't)\r
+               for (int i=0; i <= 16; i++) {\r
+                       SortedList sl = new SortedList (i);\r
+                       AssertEquals ("Capacity-Original" + i.ToString (), i, sl.Capacity);\r
+                       sl.Clear ();\r
+                       // reset to class default (16)\r
+                       AssertEquals ("Capacity-Resetted" + i.ToString (), 16, sl.Capacity);\r
+               }\r
+       }\r
+\r
+       [Test]\r
+       public void Clear_Capacity_Reset () \r
+       {\r
+               SortedList sl = new SortedList (0);\r
+               AssertEquals ("0", 0, sl.Capacity);\r
+               sl.Clear ();\r
+               // reset to class default (16)\r
+               AssertEquals ("Clear", 16, sl.Capacity);\r
+               sl.Capacity = 0;\r
+               AssertEquals ("Capacity", 16, sl.Capacity);\r
+               // note: we didn't return to 0 - so Clear cahnge the default capacity\r
+       }\r
+\r
+        [Test]\r
        public void TestClone() {\r
                {\r
                        SortedList sl1 = new SortedList(10);\r
@@ -273,6 +362,7 @@ public class SortedListTest : TestCase {
                }\r
        }\r
 \r
+        [Test]\r
        public void TestContains() {\r
                SortedList sl1 = new SortedList(55);\r
                for (int i = 0; i <= 50; i++) {sl1.Add("kala "+i,i);}\r
@@ -286,6 +376,7 @@ public class SortedListTest : TestCase {
                Assert("sl.Contains: finds non-existing key", !sl1.Contains("ohoo"));\r
        }\r
 \r
+        [Test]\r
        public void TestContainsKey() {\r
                SortedList sl1 = new SortedList(55);\r
                for (int i = 0; i <= 50; i++) {sl1.Add("kala "+i,i);}\r
@@ -299,18 +390,21 @@ public class SortedListTest : TestCase {
                Assert("sl.ContainsKey: finds non-existing key", !sl1.ContainsKey("ohoo"));\r
        }\r
 \r
+        [Test]\r
        public void TestContainsValue() {\r
                SortedList sl1 = new SortedList(55);\r
-    sl1.Add(0, "zero");\r
-    sl1.Add(1, "one");\r
-    sl1.Add(2, "two");\r
-    sl1.Add(3, "three");\r
-    sl1.Add(4, "four");\r
+                sl1.Add(0, "zero");\r
+                sl1.Add(1, "one");\r
+                sl1.Add(2, "two");\r
+                sl1.Add(3, "three");\r
+                sl1.Add(4, "four");\r
+\r
                Assert("sl.ContainsValue: can't find existing value", sl1.ContainsValue("zero"));\r
                Assert("sl.ContainsValue: finds non-existing value", !sl1.ContainsValue("ohoo"));\r
                Assert("sl.ContainsValue: finds non-existing value", !sl1.ContainsValue(null));\r
        }\r
-       \r
+\r
+        [Test] \r
        public void TestCopyTo() {\r
                SortedList sl1 = new SortedList();\r
                for (int i = 0; i <= 10; i++) {sl1.Add("kala "+i,i);}\r
@@ -408,7 +502,8 @@ public class SortedListTest : TestCase {
                il.Add( "brown" );\r
                return il;\r
        }\r
-       \r
+\r
+        [Test] \r
        public void TestGetByIndex() {\r
                SortedList sl1 = DefaultSL();\r
                AssertEquals("cl.GetByIndex: failed(1)",sl1.GetByIndex(4),"over");\r
@@ -423,14 +518,21 @@ public class SortedListTest : TestCase {
                } catch (ArgumentOutOfRangeException) {}\r
        }\r
 \r
-       public void TestGetEnumerator() {\r
+        [Test]\r
+       public void GetEnumerator ()\r
+       {\r
                SortedList sl1 = DefaultSL();\r
                IDictionaryEnumerator e = sl1.GetEnumerator();\r
                AssertNotNull("sl.GetEnumerator: does not return enumerator", e);\r
                AssertEquals("sl.GetEnumerator: enumerator not working(1)",e.MoveNext(),true);\r
                AssertNotNull("sl.GetEnumerator: enumerator not working(2)",e.Current);\r
+\r
+               Assert ("ICloneable", (e is ICloneable));\r
+               Assert ("IDictionaryEnumerator", (e is ICloneable));\r
+               Assert ("IEnumerator", (e is ICloneable));\r
        }\r
 \r
+        [Test]\r
        public void TestGetKey() {\r
                SortedList sl1 = DefaultSL();\r
                AssertEquals("sl.GetKey: failed(1)",sl1.GetKey(4),1.5);\r
@@ -444,7 +546,8 @@ public class SortedListTest : TestCase {
                        Fail("sl.GetKey: does not throw ArgumentOutOfRangeException with too large index");\r
                } catch (ArgumentOutOfRangeException) {}\r
        }\r
-\r
+        \r
+        [Test]\r
        public void TestGetKeyList() {\r
                SortedList sl1 = DefaultSL();\r
                IList keys = sl1.GetKeyList();\r
@@ -457,6 +560,7 @@ public class SortedListTest : TestCase {
                AssertEquals("sl.GetKeyList: incorrect key(2)",keys[8],33.9);\r
        }\r
 \r
+        [Test]\r
        public void TestGetValueList() {\r
                SortedList sl1 = DefaultSL();\r
                IList originalvals = DefaultValues();\r
@@ -484,6 +588,7 @@ public class SortedListTest : TestCase {
        }\r
        */\r
 \r
+        [Test]\r
        public void TestIndexOfKey() {\r
                SortedList sl1 = new SortedList(24);\r
                string s=null;\r
@@ -510,6 +615,7 @@ public class SortedListTest : TestCase {
                }\r
        }\r
 \r
+        [Test]\r
        public void TestIndexOfValue() {\r
                SortedList sl1 = new SortedList(24);\r
                string s=null;\r
@@ -527,8 +633,78 @@ public class SortedListTest : TestCase {
                        AssertEquals("sl.IndexOfValue: incorrect index key", i, sl1.IndexOfValue(100+i*i));\r
                }\r
        }\r
-       \r
 \r
+        [Test]\r
+        public void TestIndexOfValue2 ()\r
+        {\r
+                SortedList list = new SortedList ();\r
+                list.Add ("key0", "la la");\r
+                list.Add ("key1", "value");\r
+                list.Add ("key2", "value");\r
+\r
+                int i = list.IndexOfValue ("value");\r
+\r
+                AssertEquals (1, i);\r
+        }\r
+\r
+        [Test]\r
+        public void TestIndexOfValue3 ()\r
+        {\r
+                SortedList list = new SortedList ();\r
+                int i = list.IndexOfValue ((string) null);\r
+\r
+                AssertEquals (1, -i);\r
+        }\r
+\r
+        [Test]\r
+        public void TestIndexer ()\r
+        {\r
+                SortedList list = new SortedList ();\r
+\r
+                list.Add (1, new Queue ());\r
+                list.Add (2, new Hashtable ());\r
+                list.Add (3, new Stack ());\r
+\r
+                AssertEquals (typeof (Queue), list [1].GetType ());\r
+                AssertEquals (typeof (Hashtable), list [2].GetType ());\r
+                AssertEquals (typeof (Stack), list [3].GetType ());                \r
+        }\r
+\r
+        [Test]\r
+        public void TestEnumerator ()\r
+        {\r
+                SortedList list = new SortedList ();\r
+\r
+                list.Add (1, new Queue ());\r
+                list.Add (2, new Hashtable ());\r
+                list.Add (3, new Stack ());\r
+\r
+                foreach (DictionaryEntry d in list) {\r
+\r
+                        int key = (int) d.Key;\r
+                        Type value = d.Value.GetType ();\r
+\r
+                        switch (key) {\r
+                        case 1:\r
+                                AssertEquals (typeof (Queue), value);\r
+                                break;\r
+\r
+                        case 2:\r
+                                AssertEquals (typeof (Hashtable), value);\r
+                                break;\r
+\r
+                        case 3:\r
+                                AssertEquals (typeof (Stack), value);\r
+                                break;\r
+\r
+                        default:\r
+                                Fail ("This is incorrect: " + value.FullName);\r
+                                break;\r
+                        }\r
+                }\r
+        }\r
+\r
+        [Test]\r
        public void TestRemove() {\r
                SortedList sl1 = new SortedList(24);\r
                string s=null;\r
@@ -555,6 +731,7 @@ public class SortedListTest : TestCase {
                        AssertEquals("sl.Remove: removing failed(2)",sl1["kala "+i],null);\r
        }\r
 \r
+        [Test]\r
        public void TestRemoveAt() {\r
                SortedList sl1 = new SortedList(24);\r
                int k;\r
@@ -585,6 +762,7 @@ public class SortedListTest : TestCase {
                        AssertEquals("sl.RemoveAt: removing failed(4)",sl1["kala "+string.Format("{0:D2}", i)],i);\r
        }\r
 \r
+        [Test]\r
        public void TestSetByIndex() {\r
                SortedList sl1 = new SortedList(24);\r
                for (int i = 49; i>=0; i--) sl1.Add(100+i,i);\r
@@ -608,6 +786,7 @@ public class SortedListTest : TestCase {
 \r
        }\r
 \r
+        [Test]\r
        public void TestTrimToSize() {\r
                SortedList sl1 = new SortedList(24);\r
                \r