From 5d705184c755bed2a7b7459251d1caae8495083e Mon Sep 17 00:00:00 2001 From: Paul Irwin Date: Mon, 22 Jan 2024 08:02:04 -0700 Subject: [PATCH] Update AssertSorted method to better match Lucene, reduce allocations, #259 --- src/Lucene.Net.Tests/Util/BaseSortTestCase.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Lucene.Net.Tests/Util/BaseSortTestCase.cs b/src/Lucene.Net.Tests/Util/BaseSortTestCase.cs index 6f5e043b7c..6ed41f97bb 100644 --- a/src/Lucene.Net.Tests/Util/BaseSortTestCase.cs +++ b/src/Lucene.Net.Tests/Util/BaseSortTestCase.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Linq; using Lucene.Net.Support; using NUnit.Framework; using System; @@ -52,8 +51,11 @@ protected BaseSortTestCase(bool stable) public abstract Sorter NewSorter(Entry[] arr); + // LUCENENET specific public class StableEntryComparer : IComparer { + public static readonly StableEntryComparer Default = new StableEntryComparer(); + public int Compare(Entry a, Entry b) { if (a.Value < b.Value) return -1; @@ -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);