2010-07-21 Miguel de Icaza <miguel@novell.com>
authorMiguel de Icaza <miguel@gnome.org>
Wed, 21 Jul 2010 21:47:44 +0000 (21:47 -0000)
committerMiguel de Icaza <miguel@gnome.org>
Wed, 21 Jul 2010 21:47:44 +0000 (21:47 -0000)
* Array.cs: Fixes a couple of other cases where we were missing
the handling for null values, fixes a broken case of null
handling.

svn path=/trunk/mcs/; revision=160699

mcs/class/corlib/System/Array.cs
mcs/class/corlib/System/ChangeLog
mcs/class/corlib/Test/System/ArrayTest.cs

index 80da1e70eeedcf0b153b0e28beaea39dae62ed1f..b1e005caabb24a9c123a3a75fb18b8e337ee50bb 100644 (file)
@@ -1427,6 +1427,8 @@ namespace System
                                                // This has the effect of moving the null values to the front if comparer is null
                                                while (high > low0 && keys.GetValueImpl (high) != null)
                                                        --high;
+                                               while (low < high0 && keys.GetValueImpl (low) == null)
+                                                       ++low;
                                        } else {
                                                while (low < high0 && cmpPivot.CompareTo (keys.GetValueImpl (low)) > 0)
                                                        ++low;
@@ -1679,12 +1681,19 @@ namespace System
                        var keyPivot = array [mid];
 
                        while (true) {
-                               // Move the walls in
-                               while (low < high0 && keyPivot.CompareTo (array [low]) > 0)
-                                       ++low;
-                               while (high > low0 && keyPivot.CompareTo (array [high]) < 0)
-                                       --high;
-
+                               if (keyPivot == null){
+                                       while (low < high0 && array [low] == null)
+                                               ++low;
+                                       while (high > low0 && array [high] != null)
+                                               --high;
+                               } else {
+                                       // Move the walls in
+                                       while (low < high0 && keyPivot.CompareTo (array [low]) > 0)
+                                               ++low;
+                                       while (high > low0 && keyPivot.CompareTo (array [high]) < 0)
+                                               --high;
+                               }
+                               
                                if (low <= high) {
                                        swap (array, items, low, high);
                                        ++low;
@@ -1731,7 +1740,7 @@ namespace System
                                        } else {
                                                while (low < high0 && keys [low] == null)
                                                        ++low;
-                                               while (high > low0 && keys [high] == null)
+                                               while (high > low0 && keys [high] != null)
                                                        --high;
                                        }
                                }
@@ -1760,11 +1769,18 @@ namespace System
                        T keyPivot = array [mid];
 
                        while (true) {
-                               // Move the walls in
-                               while (low < high0 && comparison (array [low], keyPivot) < 0)
-                                       ++low;
-                               while (high > low0 && comparison (keyPivot, array [high]) < 0)
-                                       --high;
+                               if (keyPivot == null){
+                                       while (low < high0 && array [low] == null)
+                                               ++low;
+                                       while (high > low0 && array [high] != null)
+                                               --high;
+                               } else {
+                                       // Move the walls in
+                                       while (low < high0 && comparison (array [low], keyPivot) < 0)
+                                               ++low;
+                                       while (high > low0 && comparison (keyPivot, array [high]) < 0)
+                                               --high;
+                               }
 
                                if (low <= high) {
                                        swap<T> (array, low, high);
index fc265770261de7ada41c8f86a481ea667cedaa9f..a25123384fb6914245f8a0e24426160151ceeb74 100644 (file)
@@ -1,3 +1,9 @@
+2010-07-21  Miguel de Icaza  <miguel@novell.com>
+
+       * Array.cs: Fixes a couple of other cases where we were missing
+       the handling for null values, fixes a broken case of null
+       handling. 
+
 2010-07-21  Jb Evain  <jbevain@novell.com>
 
        * AppDomain.cs: add IsFullyTrusted stub.
index 6c82348ecafeabb9d3c4af2354ec326e410b109c..0ffcb6ce9da5d6e21bb5ed7be69a87c602c6c726 100644 (file)
@@ -3162,6 +3162,30 @@ public class ArrayTest
                Assert.AreEqual (null, array.GetValue (0));
        }
 
+       [Test]
+       public void SortNullsWithGenericVersion ()
+       {
+            string[] s1 = new string[6]{
+               "J",
+                "M",
+                 null,
+                "P",
+                "T",
+                "A"};
+
+            string[] s2 = new string[]{null,
+                "A",
+                "J",
+                "M",
+                "P",
+                "T"};
+
+           Array.Sort<string> (s1);
+            for (int i = 0; i < 6; i++) {
+                   Assert.AreEqual (s1[i], s2[i], "At:" + i);
+            }
+       }
+       
        //
        // This is a test case for the case that was broken by the code contributed
        // for bug  #351638.
@@ -3187,7 +3211,7 @@ public class ArrayTest
                        return ((string)x).CompareTo((string)y);
                }
        }
-       
+
 #if NET_4_0
        [Test]
        [ExpectedException (typeof (ArgumentException))]