write a shedload more tests

pull/21833/head
Michael Telatynski 2021-06-11 16:28:07 +01:00
parent a4fa2779d4
commit 3d4411390f
2 changed files with 181 additions and 56 deletions

View File

@ -39,21 +39,32 @@ export const stringToBase = (str: string, alphabet = ALPHABET): number => {
const pad = (str: string, length: number, alphabet = ALPHABET): string => str.padEnd(length, alphabet[0]); const pad = (str: string, length: number, alphabet = ALPHABET): string => str.padEnd(length, alphabet[0]);
export const averageBetweenStrings = (a: string, b: string, alphabet = ALPHABET): string => { export const midPointsBetweenStrings = (
const n = Math.max(a.length, b.length); a: string,
const aBase = stringToBase(pad(a, n, alphabet), alphabet); b: string,
const bBase = stringToBase(pad(b, n, alphabet), alphabet); count: number,
return baseToString((aBase + bBase) / 2, alphabet); maxLen: number,
}; alphabet = ALPHABET,
): string[] => {
export const midPointsBetweenStrings = (a: string, b: string, count: number, alphabet = ALPHABET): string[] => { const n = Math.min(maxLen, Math.max(a.length, b.length));
const n = Math.max(a.length, b.length); const aPadded = pad(a, n, alphabet);
const aBase = stringToBase(pad(a, n, alphabet), alphabet); const bPadded = pad(b, n, alphabet);
const bBase = stringToBase(pad(b, n, alphabet), alphabet); const aBase = stringToBase(aPadded, alphabet);
const step = (bBase - aBase) / (count + 1); const bBase = stringToBase(bPadded, alphabet);
if (step < 1) { if (bBase - aBase - 1 < count) {
if (n < maxLen) {
// this recurses once at most due to the new limit of n+1
return midPointsBetweenStrings(
pad(aPadded, n + 1, alphabet),
pad(bPadded, n + 1, alphabet),
count,
n + 1,
alphabet,
);
}
return []; return [];
} }
const step = (bBase - aBase) / (count + 1);
return Array(count).fill(undefined).map((_, i) => baseToString(aBase + step + (i * step), alphabet)); return Array(count).fill(undefined).map((_, i) => baseToString(aBase + step + (i * step), alphabet));
}; };
@ -66,6 +77,7 @@ export const reorderLexicographically = (
orders: Array<string | undefined>, orders: Array<string | undefined>,
fromIndex: number, fromIndex: number,
toIndex: number, toIndex: number,
maxLen = 50,
): IEntry[] => { ): IEntry[] => {
if ( if (
fromIndex < 0 || toIndex < 0 || fromIndex < 0 || toIndex < 0 ||
@ -89,7 +101,7 @@ export const reorderLexicographically = (
const nextBase = newOrder[toIndex + 1]?.order !== undefined const nextBase = newOrder[toIndex + 1]?.order !== undefined
? stringToBase(newOrder[toIndex + 1].order) ? stringToBase(newOrder[toIndex + 1].order)
: Number.MAX_VALUE; : Number.MAX_VALUE;
for (let i = toIndex - 1, j = 0; i >= 0; i--, j++) { for (let i = toIndex - 1, j = 1; i >= 0; i--, j++) {
if (newOrder[i]?.order !== undefined && nextBase - stringToBase(newOrder[i].order) > j) break; if (newOrder[i]?.order !== undefined && nextBase - stringToBase(newOrder[i].order) > j) break;
leftBoundIdx = i; leftBoundIdx = i;
} }
@ -102,7 +114,7 @@ export const reorderLexicographically = (
const prevBase = newOrder[toIndex - 1]?.order !== undefined const prevBase = newOrder[toIndex - 1]?.order !== undefined
? stringToBase(newOrder[toIndex - 1]?.order) ? stringToBase(newOrder[toIndex - 1]?.order)
: Number.MIN_VALUE; : Number.MIN_VALUE;
for (let i = toIndex + 1, j = 0; i < newOrder.length; i++, j++) { for (let i = toIndex + 1, j = 1; i < newOrder.length; i++, j++) {
if (newOrder[i]?.order === undefined || stringToBase(newOrder[i].order) - prevBase > j) break; // TODO verify if (newOrder[i]?.order === undefined || stringToBase(newOrder[i].order) - prevBase > j) break; // TODO verify
rightBoundIdx = i; rightBoundIdx = i;
} }
@ -122,10 +134,10 @@ export const reorderLexicographically = (
const nextOrder = newOrder[rightBoundIdx + 1]?.order const nextOrder = newOrder[rightBoundIdx + 1]?.order
?? String.fromCharCode(ALPHABET_END).repeat(prevOrder.length + 1); // TODO ?? String.fromCharCode(ALPHABET_END).repeat(prevOrder.length + 1); // TODO
const changes = midPointsBetweenStrings(prevOrder, nextOrder, 1 + rightBoundIdx - leftBoundIdx); const changes = midPointsBetweenStrings(prevOrder, nextOrder, 1 + rightBoundIdx - leftBoundIdx, maxLen);
// TODO If we exceed maxLen then reorder EVERYTHING // TODO If we exceed maxLen then reorder EVERYTHING
console.log("@@ test", { prevOrder, nextOrder, changes, leftBoundIdx, rightBoundIdx, orders, fromIndex, toIndex, newOrder, orderToLeftUndefined }); console.log("@@ test", { prevOrder, nextOrder, changes, leftBoundIdx, rightBoundIdx, orders, fromIndex, toIndex, newOrder, orderToLeftUndefined, leftDiff, rightDiff });
return changes.map((order, i) => { return changes.map((order, i) => {
const index = newOrder[leftBoundIdx + i].index; const index = newOrder[leftBoundIdx + i].index;

View File

@ -18,7 +18,6 @@ import { sortBy } from "lodash";
import { import {
ALPHABET, ALPHABET,
averageBetweenStrings,
baseToString, baseToString,
midPointsBetweenStrings, midPointsBetweenStrings,
reorderLexicographically, reorderLexicographically,
@ -29,10 +28,10 @@ const moveLexicographicallyTest = (
orders: Array<string | undefined>, orders: Array<string | undefined>,
fromIndex: number, fromIndex: number,
toIndex: number, toIndex: number,
expectedIndices: number[], expectedChanges: number,
maxLength?: number,
): void => { ): void => {
const ops = reorderLexicographically(orders, fromIndex, toIndex); const ops = reorderLexicographically(orders, fromIndex, toIndex, maxLength);
expect(ops.map(o => o.index).sort()).toStrictEqual(expectedIndices.sort());
const zipped: Array<[number, string | undefined]> = orders.map((o, i) => [i, o]); const zipped: Array<[number, string | undefined]> = orders.map((o, i) => [i, o]);
ops.forEach(({ index, order }) => { ops.forEach(({ index, order }) => {
@ -42,6 +41,7 @@ const moveLexicographicallyTest = (
const newOrders = sortBy(zipped, i => i[1]); const newOrders = sortBy(zipped, i => i[1]);
console.log("@@ moveLexicographicallyTest", {orders, zipped, newOrders, fromIndex, toIndex, ops}); console.log("@@ moveLexicographicallyTest", {orders, zipped, newOrders, fromIndex, toIndex, ops});
expect(newOrders[toIndex][0]).toBe(fromIndex); expect(newOrders[toIndex][0]).toBe(fromIndex);
expect(ops).toHaveLength(expectedChanges);
}; };
describe("stringOrderField", () => { describe("stringOrderField", () => {
@ -70,44 +70,20 @@ describe("stringOrderField", () => {
expect(baseToString(1234)).toBe(",~"); expect(baseToString(1234)).toBe(",~");
}); });
it("averageBetweenStrings", () => {
[
{ a: "a", b: "z", output: `m` },
{ a: "ba", b: "z", output: `n@` },
{ a: "z", b: "ba", output: `n@` },
{ a: "# ", b: "$8888", output: `#[[[[` },
{ a: "cat", b: "doggo", output: `d9>Cw` },
{ a: "cat", b: "doggo", output: "cumqh", alphabet: "abcdefghijklmnopqrstuvwxyz" },
{ a: "aa", b: "zz", output: "mz", alphabet: "abcdefghijklmnopqrstuvwxyz" },
{ a: "a", b: "z", output: "m", alphabet: "abcdefghijklmnopqrstuvwxyz" },
{ a: "AA", b: "zz", output: "^." },
{ a: "A", b: "z", output: "]" },
{
a: "A".repeat(50),
b: "Z".repeat(50),
output: "M}M}M}N ba`54Qpt\\\\Z+kNA#O(9}z>@2jJm]%Y^$m<8lRzz/2[Y",
},
].forEach((c) => {
// assert that the output string falls lexicographically between `a` and `b`
expect([c.a, c.b, c.output].sort()[1]).toBe(c.output);
expect(averageBetweenStrings(c.a, c.b, c.alphabet)).toBe(c.output);
});
expect(averageBetweenStrings("Q#!x+k", "V6yr>L")).toBe("S\\Mu5,");
});
it("midPointsBetweenStrings", () => { it("midPointsBetweenStrings", () => {
expect(midPointsBetweenStrings("a", "e", 3)).toStrictEqual(["b", "c", "d"]); const midpoints = ["a", ...midPointsBetweenStrings("a", "e", 3, 1), "e"].sort();
expect(midPointsBetweenStrings("a", "e", 0)).toStrictEqual([]); expect(midpoints[0]).toBe("a");
expect(midPointsBetweenStrings("a", "e", 4)).toStrictEqual([]); expect(midpoints[4]).toBe("e");
expect(midPointsBetweenStrings("a", "e", 0, 1)).toStrictEqual([]);
expect(midPointsBetweenStrings("a", "e", 4, 1)).toStrictEqual([]);
}); });
it("moveLexicographically left", () => { it("moveLexicographically left", () => {
moveLexicographicallyTest(["a", "c", "e", "g", "i"], 2, 1, [2]); moveLexicographicallyTest(["a", "c", "e", "g", "i"], 2, 1, 1);
}); });
it("moveLexicographically right", () => { it("moveLexicographically right", () => {
moveLexicographicallyTest(["a", "c", "e", "g", "i"], 1, 2, [1]); moveLexicographicallyTest(["a", "c", "e", "g", "i"], 1, 2, 1);
}); });
it("moveLexicographically all undefined", () => { it("moveLexicographically all undefined", () => {
@ -115,7 +91,7 @@ describe("stringOrderField", () => {
[undefined, undefined, undefined, undefined, undefined, undefined], [undefined, undefined, undefined, undefined, undefined, undefined],
4, 4,
1, 1,
[0, 4], 2,
); );
}); });
@ -124,7 +100,7 @@ describe("stringOrderField", () => {
[undefined, undefined, undefined, undefined, undefined, undefined], [undefined, undefined, undefined, undefined, undefined, undefined],
1, 1,
4, 4,
[0, 1, 2, 3, 4], 5,
); );
}); });
@ -133,7 +109,7 @@ describe("stringOrderField", () => {
["a", "c", "e", undefined, undefined, undefined], ["a", "c", "e", undefined, undefined, undefined],
5, 5,
2, 2,
[5], 1,
); );
}); });
@ -142,7 +118,144 @@ describe("stringOrderField", () => {
["a", "a", "e", undefined, undefined, undefined], ["a", "a", "e", undefined, undefined, undefined],
5, 5,
1, 1,
[1, 5], 2,
);
});
it("test A moving to the start when all is undefined", () => {
moveLexicographicallyTest(
[undefined, undefined, undefined, undefined],
2,
0,
1,
);
});
it("test B moving to the end when all is undefined", () => {
moveLexicographicallyTest(
[undefined, undefined, undefined, undefined],
1,
3,
4,
);
});
it("test C moving left when all is undefined", () => {
moveLexicographicallyTest(
[undefined, undefined, undefined, undefined, undefined, undefined],
4,
1,
2,
);
});
it("test D moving right when all is undefined", () => {
moveLexicographicallyTest(
[undefined, undefined, undefined, undefined],
1,
2,
3,
);
});
it("test E moving more right when all is undefined", () => {
moveLexicographicallyTest(
[undefined, undefined, undefined, undefined, undefined, /**/ undefined, undefined],
1,
4,
5,
);
});
it("test F moving left when right is undefined", () => {
moveLexicographicallyTest(
["20", undefined, undefined, undefined, undefined, undefined],
4,
2,
2,
);
});
it("test G moving right when right is undefined", () => {
moveLexicographicallyTest(
["50", undefined, undefined, undefined, undefined, /**/ undefined, undefined],
1,
4,
4,
);
});
it("test H moving left when right is defined", () => {
moveLexicographicallyTest(
["10", "20", "30", "40", undefined, undefined],
3,
1,
1,
);
});
it("test I moving right when right is defined", () => {
moveLexicographicallyTest(
["10", "20", "30", "40", "50", undefined],
1,
3,
1,
);
});
it("test J moving left when all is defined", () => {
moveLexicographicallyTest(
["11", "13", "15", "17", "19"],
2,
1,
1,
);
});
it("test K moving right when all is defined", () => {
moveLexicographicallyTest(
["11", "13", "15", "17", "19"],
1,
2,
1,
);
});
it("test L moving left into no left space", () => {
moveLexicographicallyTest(
["11", "12", "13", "14", "19"],
3,
1,
2,
2,
);
});
it("test M moving right into no right space", () => {
moveLexicographicallyTest(
["15", "16", "17", "18", "19"],
1,
3,
2,
2,
);
});
it("test N moving right into no left space", () => {
moveLexicographicallyTest(
["11", "12", "13", "14", "15", "16", undefined],
1,
3,
3,
);
});
it("test O moving left into no right space", () => {
moveLexicographicallyTest(
["15", "16", "17", "18", "19"],
4,
3,
2,
); );
}); });
}); });