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) {
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);
// Don't extend short runs with `binarySort()`
@ -36,7 +36,7 @@ public class FasterFinnSortMockitoTest {
// Capture calculated power values.
final ResultCaptor<Integer> resultCaptor = new ResultCaptor<>();
doAnswer(resultCaptor).when(spiedFfs).power(anyInt(), anyInt());
doAnswer(resultCaptor).when(spiedFfs).power(anyInt());
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.
*
* @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,
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;
int nRemaining = hi - lo;
if (nRemaining < 2)
return; // Arrays of size 0 and 1 are always sorted
// If array is small, do a "mini-TimSort" with no merges
if (!forcePowersortShortArray && nRemaining < MIN_MERGE) {
int initRunLen = countRunAndMakeAscending(a, lo, hi, c);
binarySort(a, lo, hi, lo + initRunLen, c);
return;
}
/*
/**
* March over the array once, left to right, finding natural runs,
* extending short natural runs to minRun elements, and merging runs
* to maintain stack invariant.
*/
if (ts == null) ts = new FasterFinnSort<>(a, c, work, workBase, workLen);
int minRun = ts.minRunLength(nRemaining);
System.out.println("minRun: " + minRun);
if (fs == null) fs = new FasterFinnSort<>(a, c, work, workBase, workLen, hi - lo);
int minRun = fs.minRunLength(nRemaining);
do {
// Identify next run
int runLen = countRunAndMakeAscending(a, lo, hi, c);
System.out.println("runLen: " + runLen);
// If run is short, extend to min(minRun, nRemaining)
if (runLen < minRun) {
System.out.println("Extend run of length " + runLen + " to " + Math.min(minRun, nRemaining));
int force = nRemaining <= minRun ? nRemaining : minRun;
binarySort(a, lo, lo + force, lo + runLen, c);
runLen = force;
}
// Push run onto pending-run stack, and maybe merge
ts.pushRun(lo, runLen, hi - lo);
ts.mergeCollapse();
fs.pushRun(lo, runLen);
fs.mergeCollapse();
// Advance to find next run
lo += runLen;
@ -60,7 +57,7 @@ public class FasterFinnSortWrapper {
// Merge all remaining runs to complete sort
assert lo == hi;
ts.mergeForceCollapse();
assert ts.stackSize == 1;
fs.mergeForceCollapse();
assert fs.stackSize == 1;
}
}