You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by ni...@apache.org on 2021/11/18 18:14:23 UTC

[lucenenet] branch master updated: BUG: Lucene.Net.Tests.Util.TestPriorityQueue: Fixed issues with comparers after introducing J2N.Randomizer, which produces negative random numbers.

This is an automated email from the ASF dual-hosted git repository.

nightowl888 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucenenet.git


The following commit(s) were added to refs/heads/master by this push:
     new cf727f4  BUG: Lucene.Net.Tests.Util.TestPriorityQueue: Fixed issues with comparers after introducing J2N.Randomizer, which produces negative random numbers.
cf727f4 is described below

commit cf727f42141a1a9b73d5fa84ec35eab8a9c50361
Author: Shad Storhaug <sh...@shadstorhaug.com>
AuthorDate: Tue Nov 16 08:11:03 2021 +0700

    BUG: Lucene.Net.Tests.Util.TestPriorityQueue: Fixed issues with comparers after introducing J2N.Randomizer, which produces negative random numbers.
---
 src/Lucene.Net.Tests/Util/TestPriorityQueue.cs | 31 ++++++++++++++++++++------
 1 file changed, 24 insertions(+), 7 deletions(-)

diff --git a/src/Lucene.Net.Tests/Util/TestPriorityQueue.cs b/src/Lucene.Net.Tests/Util/TestPriorityQueue.cs
index d559002..f6b8d60 100644
--- a/src/Lucene.Net.Tests/Util/TestPriorityQueue.cs
+++ b/src/Lucene.Net.Tests/Util/TestPriorityQueue.cs
@@ -43,7 +43,7 @@ namespace Lucene.Net.Util
 
             protected internal override bool LessThan(int? a, int? b)
             {
-                return (a <= b);
+                return (a < b);
             }
         }
 
@@ -186,21 +186,38 @@ namespace Lucene.Net.Util
 
         private class Less : IComparer<int?>
         {
+            public static IComparer<int?> Default { get; } = new Less();
+
+            private Less() { }
+
             public int Compare(int? a, int? b)
             {
                 Assert.IsNotNull(a);
                 Assert.IsNotNull(b);
-                return (int) (a - b);
+                if (a < b)
+                    return -1;
+                if (a > b)
+                    return 1;
+                return 0;
             }
         }
 
         private class Greater : IComparer<int?>
         {
+            public static IComparer<int?> Default { get; } = new Greater();
+
+            private Greater() { }
+
             public int Compare(int? a, int? b)
             {
                 Assert.IsNotNull(a);
                 Assert.IsNotNull(b);
-                return (int) (a - b);
+                if (a > b)
+                    return -1;
+                if (a < b)
+                    return 1;
+                return 0;
+
             }
         } 
 
@@ -493,7 +510,7 @@ namespace Lucene.Net.Util
             for (int i = 1; i < size; i++)
             {
                 T next = pq.Pop();
-                Assert.IsTrue(pq.LessThan(last, next));
+                Assert.IsTrue(pq.LessThan(last, next) || last.Equals(next));
                 last = next;
             }
         }
@@ -542,7 +559,7 @@ namespace Lucene.Net.Util
 
             AddElements(pq, elements);
 
-            ArrayUtil.IntroSort(elements, new Less());
+            ArrayUtil.IntroSort(elements, Less.Default);
 
             PopAndTestElements(pq, elements);
         }
@@ -618,14 +635,14 @@ namespace Lucene.Net.Util
             Console.WriteLine("\nSorted list of elements...");
 
             pq = new IntegerQueue(maxSize);
-            ArrayUtil.IntroSort(elements, new Less());
+            ArrayUtil.IntroSort(elements, Less.Default);
             TimedAddAndPop<int?>(pq, elements);
             pq.Clear();
 
             Console.WriteLine("\nReverse sorted list of elements...");
 
             pq = new IntegerQueue(maxSize);
-            ArrayUtil.IntroSort(elements, new Greater());
+            ArrayUtil.IntroSort(elements, Greater.Default);
             TimedAddAndPop<int?>(pq, elements);
             pq.Clear();
         }