[corlib] Fixes array enumerator to match .net behaviour
authorMarek Safar <marek.safar@gmail.com>
Thu, 5 Oct 2017 11:54:35 +0000 (13:54 +0200)
committerMarek Safar <marek.safar@gmail.com>
Thu, 5 Oct 2017 14:51:56 +0000 (16:51 +0200)
mcs/class/corlib/System/Array.cs
mcs/class/corlib/Test/System/ArrayTest.cs

index 00e0430d3de27dc3b5019a4a2cb84604d1934cc4..d699446f2b5ce3aad52382b8900a6bde0a3161bd 100644 (file)
@@ -207,7 +207,7 @@ namespace System
                        // we just decr the size, so, 0 - 1 == FINISHED
                        const int FINISHED = -1;
                        
-                       Array array;
+                       readonly Array array;
                        int idx;
 
                        internal InternalEnumerator (Array array)
@@ -218,7 +218,6 @@ namespace System
 
                        public void Dispose ()
                        {
-                               idx = NOT_STARTED;
                        }
 
                        public bool MoveNext ()
index 5279edc1a2b0dbb336d6c648be93ee00c1a2e492..0a04041aea34c055fdd0dd984c56be97ee6eedb8 100644 (file)
@@ -3385,6 +3385,30 @@ public class ArrayTest
                }
        }
 
+       [Test]
+       public void IEnumerator_Dispose ()
+       {
+               IEnumerable<int> e = new int[] { 1 };
+               var en = e.GetEnumerator ();
+               Assert.IsTrue (en.MoveNext (), "#1");
+               Assert.IsFalse (en.MoveNext (), "#2");
+               en.Dispose ();
+               Assert.IsFalse (en.MoveNext (), "#3");
+       }
+
+       [Test]
+       public void IEnumerator_ZeroSize ()
+       {
+               IEnumerable<int> e = Array.Empty<int> ();
+               var en = e.GetEnumerator ();
+               Assert.IsFalse (en.MoveNext (), "#1");
+
+               e = Array.Empty<int> ();
+               en = e.GetEnumerator ();
+               Assert.IsFalse (en.MoveNext (), "#2");
+       }
+
+       [Test]
        public void ICollection_IsReadOnly() {
                ICollection<string> arr = new string [10];