WIP: use Mockito

This commit is contained in:
Daniel Langbein 2025-01-21 16:35:47 +00:00
parent 60ca1275b5
commit 1fb89e8985
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002
2 changed files with 12 additions and 15 deletions

View File

@ -28,7 +28,7 @@ public class FasterFinnSortMockitoTest {
} }
void printPowers(Object[] a) { void printPowers(Object[] a) {
FasterFinnSort<Object> ffs = new FasterFinnSort<>(a, NaturalOrder.INSTANCE, null, 0, 0); FasterFinnSort<Object> ffs = new FasterFinnSort<>(a, NaturalOrder.INSTANCE, null, 0, 0, a.length);
FasterFinnSort<Object> spiedFfs = spy(ffs); FasterFinnSort<Object> spiedFfs = spy(ffs);
// Don't extend short runs with `binarySort()` // Don't extend short runs with `binarySort()`
@ -36,7 +36,7 @@ public class FasterFinnSortMockitoTest {
// Capture calculated power values. // Capture calculated power values.
final ResultCaptor<Integer> resultCaptor = new ResultCaptor<>(); final ResultCaptor<Integer> resultCaptor = new ResultCaptor<>();
doAnswer(resultCaptor).when(spiedFfs).power(anyInt(), anyInt()); doAnswer(resultCaptor).when(spiedFfs).power(anyInt());
sort(a, spiedFfs); sort(a, spiedFfs);

View File

@ -10,48 +10,45 @@ public class FasterFinnSortWrapper {
/** /**
* Copy of `FasterFinnSort.java`. Param `ts` has been added to allow dependency injection for testing. * Copy of `FasterFinnSort.java`. Param `ts` has been added to allow dependency injection for testing.
* *
* @param ts: If non-null, the creation of `ts` with `new FasterFinnSort<>(a, c, work, workBase, workLen)` in this methods body is skipped. Instead, the given `ts` is used. * @param fs: If non-null, the creation of `ts` with `new FasterFinnSort<>(a, c, work, workBase, workLen)` in this methods body is skipped. Instead, the given `ts` is used.
*/ */
public static <T> void sort(T[] a, int lo, int hi, Comparator<? super T> c, public static <T> void sort(T[] a, int lo, int hi, Comparator<? super T> c,
T[] work, int workBase, int workLen, T[] work, int workBase, int workLen,
FasterFinnSort<T> ts, boolean forcePowersortShortArray) { FasterFinnSort<T> fs, boolean forcePowersortShortArray) {
assert c != null && a != null && lo >= 0 && lo <= hi && hi <= a.length; assert c != null && a != null && lo >= 0 && lo <= hi && hi <= a.length;
int nRemaining = hi - lo; int nRemaining = hi - lo;
if (nRemaining < 2) if (nRemaining < 2)
return; // Arrays of size 0 and 1 are always sorted return; // Arrays of size 0 and 1 are always sorted
// If array is small, do a "mini-TimSort" with no merges // If array is small, do a "mini-TimSort" with no merges
if (!forcePowersortShortArray && nRemaining < MIN_MERGE) { if (!forcePowersortShortArray && nRemaining < MIN_MERGE) {
int initRunLen = countRunAndMakeAscending(a, lo, hi, c); int initRunLen = countRunAndMakeAscending(a, lo, hi, c);
binarySort(a, lo, hi, lo + initRunLen, c); binarySort(a, lo, hi, lo + initRunLen, c);
return; return;
} }
/* /**
* March over the array once, left to right, finding natural runs, * March over the array once, left to right, finding natural runs,
* extending short natural runs to minRun elements, and merging runs * extending short natural runs to minRun elements, and merging runs
* to maintain stack invariant. * to maintain stack invariant.
*/ */
if (ts == null) ts = new FasterFinnSort<>(a, c, work, workBase, workLen); if (fs == null) fs = new FasterFinnSort<>(a, c, work, workBase, workLen, hi - lo);
int minRun = ts.minRunLength(nRemaining); int minRun = fs.minRunLength(nRemaining);
System.out.println("minRun: " + minRun);
do { do {
// Identify next run // Identify next run
int runLen = countRunAndMakeAscending(a, lo, hi, c); int runLen = countRunAndMakeAscending(a, lo, hi, c);
System.out.println("runLen: " + runLen);
// If run is short, extend to min(minRun, nRemaining) // If run is short, extend to min(minRun, nRemaining)
if (runLen < minRun) { if (runLen < minRun) {
System.out.println("Extend run of length " + runLen + " to " + Math.min(minRun, nRemaining));
int force = nRemaining <= minRun ? nRemaining : minRun; int force = nRemaining <= minRun ? nRemaining : minRun;
binarySort(a, lo, lo + force, lo + runLen, c); binarySort(a, lo, lo + force, lo + runLen, c);
runLen = force; runLen = force;
} }
// Push run onto pending-run stack, and maybe merge // Push run onto pending-run stack, and maybe merge
ts.pushRun(lo, runLen, hi - lo); fs.pushRun(lo, runLen);
ts.mergeCollapse(); fs.mergeCollapse();
// Advance to find next run // Advance to find next run
lo += runLen; lo += runLen;
@ -60,7 +57,7 @@ public class FasterFinnSortWrapper {
// Merge all remaining runs to complete sort // Merge all remaining runs to complete sort
assert lo == hi; assert lo == hi;
ts.mergeForceCollapse(); fs.mergeForceCollapse();
assert ts.stackSize == 1; assert fs.stackSize == 1;
} }
} }