Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 101 additions & 49 deletions src/__tests__/quicksort/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,69 +5,121 @@ import { ascendingInvalidFirst } from "../../comparators/ascendingInvalidFirst";
import { descendingInvalidLast } from "../../comparators/descendingInvalidLast";

describe("quicksort", () => {
describe("short sort array of numbers in order of", () => {
it("ascending", () => {
const array = [5, 7.3, 7, 3.1, -20, 0, 40];
quicksort(array, 0, array.length - 1);
expect(array).toEqual([-20, 0, 3.1, 5, 7, 7.3, 40]);
});
describe("should sort array of numbers in order of", () => {
describe("ascending", () => {
it("by default", () => {
const array = [5, 7.3, 7, 3.1, -20, 0, 40];
quicksort(array, 0, array.length - 1);
expect(array).toEqual([-20, 0, 3.1, 5, 7, 7.3, 40]);
});

it("ascending with non-numeric values at the end", () => {
const array = [5, "MOO", 7.3, null, 7, 3.1, -20, 0, undefined, 40];
quicksort(array, 0, array.length - 1);
it("with floating point value", () => {
const array = [
0.1,
0.1 + 0.11,
0.3 - 0.1,
0.2,
0.3 + 0.1,
0.4 - 0.1,
0.3,
0.1 + 0.2,
];
quicksort(array, 0, array.length - 1);
expect(array).toEqual([
0.1, // 0.1
0.19999999999999998, // 0.3 - 0.1
0.2, // 0.2
0.21000000000000002, // 0.1 + 0.11
0.3, // 0.3
0.30000000000000004, // 0.1 + 0.2
0.30000000000000004, // 0.4 - 0.1
0.4, // 0.3 + 0.1
]);
});

const sortedValidNumericValues = array.slice(0, 7);
const lastInvalidValued = array.slice(7);
it("with non-numeric values at the end", () => {
const array = [5, "MOO", 7.3, null, 7, 3.1, -20, 0, undefined, 40];
quicksort(array, 0, array.length - 1);

expect(sortedValidNumericValues).toEqual([-20, 0, 3.1, 5, 7, 7.3, 40]);
expect(lastInvalidValued).toContain("MOO");
expect(lastInvalidValued).toContain(null);
expect(lastInvalidValued).toContain(undefined);
});
const sortedValidNumericValues = array.slice(0, 7);
const lastInvalidValued = array.slice(7);

it("ascending with non-numeric values at the start", () => {
const array = [5, "MOO", 7.3, null, 7, 3.1, -20, 0, undefined, 40];
quicksort(array, 0, array.length - 1, ascendingInvalidFirst);
expect(sortedValidNumericValues).toEqual([-20, 0, 3.1, 5, 7, 7.3, 40]);
expect(lastInvalidValued).toContain("MOO");
expect(lastInvalidValued).toContain(null);
expect(lastInvalidValued).toContain(undefined);
});

const firstInvalidValued = array.slice(0, 3);
const sortedValidNumericValues = array.slice(3);
it("with non-numeric values at the start", () => {
const array = [5, "MOO", 7.3, null, 7, 3.1, -20, 0, undefined, 40];
quicksort(array, 0, array.length - 1, ascendingInvalidFirst);

expect(firstInvalidValued).toContain("MOO");
expect(firstInvalidValued).toContain(null);
expect(firstInvalidValued).toContain(undefined);
expect(sortedValidNumericValues).toEqual([-20, 0, 3.1, 5, 7, 7.3, 40]);
});
const firstInvalidValued = array.slice(0, 3);
const sortedValidNumericValues = array.slice(3);

it("descending", () => {
const array = [5, 7.3, 7, 3.1, -20, 0, 40];
quicksort(array, 0, array.length - 1, descending);
expect(array).toEqual([40, 7.3, 7, 5, 3.1, 0, -20]);
expect(firstInvalidValued).toContain("MOO");
expect(firstInvalidValued).toContain(null);
expect(firstInvalidValued).toContain(undefined);
expect(sortedValidNumericValues).toEqual([-20, 0, 3.1, 5, 7, 7.3, 40]);
});
});

it("descending with non-numeric values at the start", () => {
const array = [5, "MOO", 7.3, null, 7, 3.1, -20, 0, undefined, 40];
quicksort(array, 0, array.length - 1, descending);
describe("descending", () => {
it("by default", () => {
const array = [5, 7.3, 7, 3.1, -20, 0, 40];
quicksort(array, 0, array.length - 1, descending);
expect(array).toEqual([40, 7.3, 7, 5, 3.1, 0, -20]);
});

const firstInvalidValued = array.slice(0, 3);
const sortedValidNumericValues = array.slice(3);
it("with floating point value", () => {
const array = [
0.1,
0.1 + 0.11,
0.3 - 0.1,
0.2,
0.3 + 0.1,
0.4 - 0.1,
0.3,
0.1 + 0.2,
];
quicksort(array, 0, array.length - 1, descending);
expect(array).toEqual([
0.4, // 0.3 + 0.1
0.30000000000000004, // 0.4 - 0.1
0.30000000000000004, // 0.1 + 0.2
0.3, // 0.3
0.21000000000000002, // 0.1 + 0.11
0.2, // 0.2
0.19999999999999998, // 0.3 - 0.1
0.1, // 0.1
]);
});

expect(firstInvalidValued).toContain("MOO");
expect(firstInvalidValued).toContain(null);
expect(firstInvalidValued).toContain(undefined);
expect(sortedValidNumericValues).toEqual([40, 7.3, 7, 5, 3.1, 0, -20]);
});
it("with non-numeric values at the start", () => {
const array = [5, "MOO", 7.3, null, 7, 3.1, -20, 0, undefined, 40];
quicksort(array, 0, array.length - 1, descending);

const firstInvalidValued = array.slice(0, 3);
const sortedValidNumericValues = array.slice(3);

expect(firstInvalidValued).toContain("MOO");
expect(firstInvalidValued).toContain(null);
expect(firstInvalidValued).toContain(undefined);
expect(sortedValidNumericValues).toEqual([40, 7.3, 7, 5, 3.1, 0, -20]);
});

it("descending with non-numeric values at the end", () => {
const array = [5, "MOO", 7.3, null, 7, 3.1, -20, 0, undefined, 40];
quicksort(array, 0, array.length - 1, descendingInvalidLast);
it("with non-numeric values at the end", () => {
const array = [5, "MOO", 7.3, null, 7, 3.1, -20, 0, undefined, 40];
quicksort(array, 0, array.length - 1, descendingInvalidLast);

const sortedValidNumericValues = array.slice(0, 7);
const lastInvalidValued = array.slice(7);
const sortedValidNumericValues = array.slice(0, 7);
const lastInvalidValued = array.slice(7);

expect(sortedValidNumericValues).toEqual([40, 7.3, 7, 5, 3.1, 0, -20]);
expect(lastInvalidValued).toContain("MOO");
expect(lastInvalidValued).toContain(null);
expect(lastInvalidValued).toContain(undefined);
expect(sortedValidNumericValues).toEqual([40, 7.3, 7, 5, 3.1, 0, -20]);
expect(lastInvalidValued).toContain("MOO");
expect(lastInvalidValued).toContain(null);
expect(lastInvalidValued).toContain(undefined);
});
});
});
});
Loading