Skip to content

Commit

Permalink
Update AssertSorted method to better match Lucene, reduce allocations,
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirwin committed Jan 22, 2024
1 parent 8e73e32 commit 5d70518
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Lucene.Net.Tests/Util/BaseSortTestCase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using Lucene.Net.Support;
using NUnit.Framework;
using System;
Expand Down Expand Up @@ -52,8 +51,11 @@ protected BaseSortTestCase(bool stable)

public abstract Sorter NewSorter(Entry[] arr);

// LUCENENET specific
public class StableEntryComparer : IComparer<Entry>
{
public static readonly StableEntryComparer Default = new StableEntryComparer();

public int Compare(Entry a, Entry b)
{
if (a.Value < b.Value) return -1;
Expand All @@ -67,8 +69,8 @@ public int Compare(Entry a, Entry b)
public virtual void AssertSorted(Entry[] original, Entry[] sorted)
{
Assert.AreEqual(original.Length, sorted.Length);
// LUCENENET: this code differs significantly from the original Java test which used Arrays.sort().
Entry[] stableSorted = original.OrderBy(e => e, new StableEntryComparer()).ToArray();
Entry[] stableSorted = Arrays.CopyOf(original, original.Length);
Array.Sort(stableSorted, StableEntryComparer.Default); // LUCENENET: use StableEntryComparer
for (int i = 0; i < original.Length; ++i)
{
Assert.AreEqual(stableSorted[i].Value, sorted[i].Value);
Expand Down

0 comments on commit 5d70518

Please sign in to comment.