You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by sy...@apache.org on 2014/09/16 00:53:03 UTC

[12/13] git commit: Correctly translated sorting tests

Correctly translated sorting tests

The logic of the sorting tests wasn't ported fully or correctly. Now
these tests match the ones in the Java version.


Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/caa463fc
Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/caa463fc
Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/caa463fc

Branch: refs/heads/master
Commit: caa463fcddf9dbbfb321331acf84fd5473d272a9
Parents: e5d7b15
Author: Prad Nelluru <pr...@microsoft.com>
Authored: Mon Sep 15 14:38:15 2014 -0700
Committer: Prad Nelluru <pr...@microsoft.com>
Committed: Mon Sep 15 14:38:15 2014 -0700

----------------------------------------------------------------------
 src/Lucene.Net.Core/Util/VirtualMethod.cs       |   4 +-
 .../core/Util/BaseSortTestCase.cs               | 154 +++++++++++++++++--
 .../core/Util/TestInPlaceMergeSorter.cs         |   5 -
 .../core/Util/TestIntroSorter.cs                |   5 -
 src/Lucene.Net.Tests/core/Util/TestTimSorter.cs |   5 -
 5 files changed, 139 insertions(+), 34 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/caa463fc/src/Lucene.Net.Core/Util/VirtualMethod.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Util/VirtualMethod.cs b/src/Lucene.Net.Core/Util/VirtualMethod.cs
index a5ec7a2..7c29e31 100644
--- a/src/Lucene.Net.Core/Util/VirtualMethod.cs
+++ b/src/Lucene.Net.Core/Util/VirtualMethod.cs
@@ -38,7 +38,7 @@ namespace Lucene.Net.Util
     ///   new VirtualMethod&lt;BaseClass&gt;(BaseClass.class, "oldName", parameters...);
     /// </pre>
     /// <p>this enforces the singleton status of these objects, as the maintenance of the cache would be too costly else.
-    /// If you try to create a second instance of for the same method/{@code baseClass} combination, an exception is thrown.
+    /// If you try to create a second instance of for the same method/{@code baseClass} combination, an exception is thrown.</p>
     /// <p>To detect if e.g. the old method was overridden by a more far subclass on the inheritance path to the current
     /// instance's class, use a <strong>non-static</strong> field:</p>
     /// <pre class="prettyprint">
@@ -49,7 +49,7 @@ namespace Lucene.Net.Util
     ///  final boolean isDeprecatedMethodOverridden =
     ///   VirtualMethod.compareImplementationDistance(this.getClass(), oldMethod, newMethod) > 0
     /// </pre>
-    /// <p><seealso cref="#getImplementationDistance"/> returns the distance of the subclass that overrides this method.
+    /// <p><seealso cref="getImplementationDistance"/> returns the distance of the subclass that overrides this method.
     /// The one with the larger distance should be used preferable.
     /// this way also more complicated method rename scenarios can be handled
     /// (think of 2.9 {@code TokenStream} deprecations).</p>

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/caa463fc/src/Lucene.Net.Tests/core/Util/BaseSortTestCase.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests/core/Util/BaseSortTestCase.cs b/src/Lucene.Net.Tests/core/Util/BaseSortTestCase.cs
index 99fc16a..e2d4b05 100644
--- a/src/Lucene.Net.Tests/core/Util/BaseSortTestCase.cs
+++ b/src/Lucene.Net.Tests/core/Util/BaseSortTestCase.cs
@@ -1,3 +1,6 @@
+using System.Collections.Generic;
+using System.Linq;
+using Lucene.Net.JavaCompatibility;
 using Lucene.Net.Support;
 using NUnit.Framework;
 using System;
@@ -41,51 +44,168 @@ namespace Lucene.Net.Util
         }
 
         private readonly bool Stable;
+        private readonly Random random;
 
         protected BaseSortTestCase(bool stable)
         {
             this.Stable = stable;
+            this.random = Random();
         }
 
         public abstract Sorter NewSorter(Entry[] arr);
 
+        public class StableEntryComparer : IComparer<Entry>
+        {
+            public int Compare(Entry a, Entry b)
+            {
+                if (a.Value < b.Value) return -1;
+                else if (a.Value > b.Value) return 1;
+
+                //required for stable sorting
+                return a.Ord < b.Ord ? -1 : a.Ord == b.Ord ? 0 : 1;
+            }
+        }
+
         public virtual void AssertSorted(Entry[] original, Entry[] sorted)
         {
             Assert.AreEqual(original.Length, sorted.Length);
-            Entry[] actuallySorted = Arrays.CopyOf(original, original.Length);
-            Array.Sort(actuallySorted);
+            Entry[] stableSorted = original.OrderBy(e => e, new StableEntryComparer()).ToArray();
             for (int i = 0; i < original.Length; ++i)
             {
-                Assert.AreEqual(actuallySorted[i].Value, sorted[i].Value);
+                Assert.AreEqual(stableSorted[i].Value, sorted[i].Value);
                 if (Stable)
                 {
-                    Assert.AreEqual(actuallySorted[i].Ord, sorted[i].Ord);
+                    Assert.AreEqual(stableSorted[i].Ord, sorted[i].Ord);
                 }
             }
         }
 
-        [Test]
-        public virtual void Test(Entry[] arr)
+        public virtual void SortTest(Entry[] arr)
         {
-            int o = Random().Next(1000);
-            var toSort = new Entry[o + arr.Length + Random().Next(3)];
+            int o = random.Next(1000);
+            var toSort = new Entry[o + arr.Length + random.Next(3)];
             Array.Copy(arr, 0, toSort, o, arr.Length);
             Sorter sorter = NewSorter(toSort);
             sorter.Sort(o, o + arr.Length);
             AssertSorted(arr, Arrays.CopyOfRange(toSort, o, o + arr.Length));
         }
 
-        internal enum Strategy
+        private delegate void Strategy(Entry[] arr, int i);
+
+        private void RandomStrategy(Entry[] arr, int i)
+        {
+            arr[i] = new Entry(random.Next(), i);
+        }
+
+        private void RandomLowCardinalityStrategy(Entry[] arr, int i)
+        {
+            arr[i] = new Entry(random.nextInt(6), i);
+        }
+
+        private void AscendingStrategy(Entry[] arr, int i)
+        {
+            arr[i] = i == 0
+            ? new Entry(random.nextInt(6), 0)
+            : new Entry(arr[i - 1].Value + random.nextInt(6), i);
+        }
+
+        private void DescendingStrategy(Entry[] arr, int i)
+        {
+            arr[i] = i == 0
+            ? new Entry(random.nextInt(6), 0)
+            : new Entry(arr[i - 1].Value - random.nextInt(6), i);
+        }
+        
+        private void StrictlyDescendingStrategy(Entry[] arr, int i)
+        {
+            arr[i] = i == 0
+            ? new Entry(random.nextInt(6), 0)
+            : new Entry(arr[i - 1].Value - TestUtil.NextInt(random, 1, 5), i);
+            
+        }
+
+        private void AscendingSequencesStrategy(Entry[] arr, int i)
+        {
+            arr[i] = i == 0
+            ? new Entry(random.nextInt(6), 0)
+            : new Entry(Rarely(random) ? random.nextInt(1000) : arr[i - 1].Value + random.nextInt(6), i);
+            
+        }
+        
+        private void MostlyAscendingStrategy(Entry[] arr, int i)
+        {
+            arr[i] = i == 0
+            ? new Entry(random.nextInt(6), 0)
+            : new Entry(arr[i - 1].Value + TestUtil.NextInt(random, -8, 10), i);
+            
+        }
+
+        private void DoTest(Strategy strategy, int length)
+        {
+            Entry[] arr = new Entry[length];
+            for (int i = 0; i < arr.Length; ++i) {
+                strategy(arr, i);
+            }
+            SortTest(arr);
+        }
+
+        private void DoTest(Strategy strategy)
+        {
+            DoTest(strategy, Random().Next(20000));
+        }
+
+        [Test]
+        public void TestEmpty()
         {
-            RANDOM,
-            RANDOM_LOW_CARDINALITY,
-            ASCENDING,
-            DESCENDING,
-            STRICTLY_DESCENDING,
-            ASCENDING_SEQUENCES,
-            MOSTLY_ASCENDING
+            SortTest(new Entry[0]);
         }
 
-        public abstract void Set(Entry[] arr, int i);
+        [Test]
+        public void TestOne()
+        {
+            DoTest(RandomStrategy, 1);
+        }
+
+        [Test]
+        public void TestTwo()
+        {
+            DoTest(RandomStrategy, 2);
+        }
+
+        [Test]
+        public void TestRandom()
+        {
+            DoTest(RandomStrategy);
+        }
+
+        [Test]
+        public void TestRandomLowCardinality()
+        {
+            DoTest(RandomLowCardinalityStrategy, 2);
+        }
+
+        [Test]
+        public void TestAscending()
+        {
+            DoTest(AscendingStrategy, 2);
+        }
+
+        [Test]
+        public void TestAscendingSequences()
+        {
+            DoTest(AscendingSequencesStrategy, 2);
+        }
+
+        [Test]
+        public void TestDescending()
+        {
+            DoTest(DescendingStrategy, 2);
+        }
+
+        [Test]
+        public void TestStrictlyDescendingStrategy()
+        {
+            DoTest(StrictlyDescendingStrategy, 2);
+        }
     }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/caa463fc/src/Lucene.Net.Tests/core/Util/TestInPlaceMergeSorter.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests/core/Util/TestInPlaceMergeSorter.cs b/src/Lucene.Net.Tests/core/Util/TestInPlaceMergeSorter.cs
index 2726465..5c35706 100644
--- a/src/Lucene.Net.Tests/core/Util/TestInPlaceMergeSorter.cs
+++ b/src/Lucene.Net.Tests/core/Util/TestInPlaceMergeSorter.cs
@@ -32,10 +32,5 @@ namespace Lucene.Net.Util
         {
             return new ArrayInPlaceMergeSorter<Entry>(arr, ArrayUtil.naturalComparator<Entry>());
         }
-
-        public override void Set(Entry[] arr, int i)
-        {
-            throw new System.NotImplementedException();
-        }
     }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/caa463fc/src/Lucene.Net.Tests/core/Util/TestIntroSorter.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests/core/Util/TestIntroSorter.cs b/src/Lucene.Net.Tests/core/Util/TestIntroSorter.cs
index b66a66b..c71893a 100644
--- a/src/Lucene.Net.Tests/core/Util/TestIntroSorter.cs
+++ b/src/Lucene.Net.Tests/core/Util/TestIntroSorter.cs
@@ -28,10 +28,5 @@ namespace Lucene.Net.Util
         {
             return new ArrayIntroSorter<Entry>(arr, ArrayUtil.naturalComparator<Entry>());
         }
-
-        public override void Set(Entry[] arr, int i)
-        {
-            throw new System.NotImplementedException();
-        }
     }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/caa463fc/src/Lucene.Net.Tests/core/Util/TestTimSorter.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests/core/Util/TestTimSorter.cs b/src/Lucene.Net.Tests/core/Util/TestTimSorter.cs
index e798746..c12b850 100644
--- a/src/Lucene.Net.Tests/core/Util/TestTimSorter.cs
+++ b/src/Lucene.Net.Tests/core/Util/TestTimSorter.cs
@@ -28,10 +28,5 @@ namespace Lucene.Net.Util
         {
             return new ArrayTimSorter<Entry>(arr, ArrayUtil.naturalComparator<Entry>(), TestUtil.NextInt(Random(), 0, arr.Length));
         }
-
-        public override void Set(Entry[] arr, int i)
-        {
-            throw new System.NotImplementedException();
-        }
     }
 }
\ No newline at end of file