-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquickSortRecursive.test.ts
38 lines (34 loc) · 1.1 KB
/
quickSortRecursive.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { quickSortRecursive } from "./quickSortRecursive";
describe("quickSort (recursive)", () => {
test("sorts an array of numbers", () => {
expect(quickSortRecursive([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5])).toEqual([
1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9,
]);
});
test("sorts an array of strings", () => {
const stringComparator = (a: string, b: string) => a.localeCompare(b);
expect(
quickSortRecursive(
["apple", "banana", "cherry", "apple"],
stringComparator
)
).toEqual(["apple", "apple", "banana", "cherry"]);
});
test("sorts an array of objects", () => {
const arr = [{ value: 5 }, { value: 2 }, { value: 9 }, { value: 1 }];
const comparator = (a: { value: number }, b: { value: number }) =>
a.value - b.value;
expect(quickSortRecursive(arr, comparator)).toEqual([
{ value: 1 },
{ value: 2 },
{ value: 5 },
{ value: 9 },
]);
});
test("does not mutate the original array", () => {
const arr = [5, 3, 8, 4, 2];
const copy = [...arr];
quickSortRecursive(arr);
expect(arr).toEqual(copy);
});
});