Repository navigation
Expand file tree
/
Copy pathPowerSort.test.js
More file actions
94 lines (74 loc) · 2.68 KB
/
Copy pathPowerSort.test.js
File metadata and controls
94 lines (74 loc) · 2.68 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { powerSort } from '../PowerSort'
describe('PowerSort', () => {
it('should work for empty arrays', () => {
expect(powerSort([])).toEqual([])
})
it('should work for single-element arrays', () => {
expect(powerSort([5])).toEqual([5])
})
it('should sort arrays correctly', () => {
expect(powerSort([5, 4])).toEqual([4, 5])
expect(powerSort([8, 4, 10, 15, 9])).toEqual([4, 8, 9, 10, 15])
expect(powerSort([1, 2, 3])).toEqual([1, 2, 3])
expect(powerSort([10, 5, 3, 8, 2, 6, 4, 7, 9, 1])).toEqual([
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
])
})
it('should sort arrays containing duplicate values', () => {
expect(powerSort([4, 2, 4, 1, 2, 3, 1])).toEqual([1, 1, 2, 2, 3, 4, 4])
})
it('should handle already sorted arrays', () => {
expect(powerSort([1, 2, 3, 4, 5, 6])).toEqual([1, 2, 3, 4, 5, 6])
})
it('should handle reverse-sorted arrays', () => {
expect(powerSort([9, 8, 7, 6, 5, 4, 3, 2, 1])).toEqual([
1, 2, 3, 4, 5, 6, 7, 8, 9
])
})
it('should sort arrays larger than MIN_RUN', () => {
const array = Array.from({ length: 100 }, (_, index) => 100 - index)
expect(powerSort(array)).toEqual(
Array.from({ length: 100 }, (_, index) => index + 1)
)
})
it('should return the same array after sorting', () => {
const array = [5, 3, 1, 4, 2]
const result = powerSort(array)
expect(result).toBe(array)
expect(array).toEqual([1, 2, 3, 4, 5])
})
it('should merge multiple natural runs correctly', () => {
const array = [
...Array.from({ length: 32 }, (_, i) => i + 96),
...Array.from({ length: 32 }, (_, i) => i + 64),
...Array.from({ length: 32 }, (_, i) => i + 32),
...Array.from({ length: 32 }, (_, i) => i)
]
expect(powerSort(array)).toEqual(Array.from({ length: 128 }, (_, i) => i))
})
it('should merge natural runs of different lengths', () => {
const array = [
...Array.from({ length: 32 }, (_, i) => i + 200),
...Array.from({ length: 40 }, (_, i) => i + 150),
...Array.from({ length: 35 }, (_, i) => i + 100),
...Array.from({ length: 50 }, (_, i) => i)
]
const expected = [
...Array.from({ length: 50 }, (_, i) => i),
...Array.from({ length: 35 }, (_, i) => i + 100),
...Array.from({ length: 40 }, (_, i) => i + 150),
...Array.from({ length: 32 }, (_, i) => i + 200)
]
expect(powerSort(array)).toEqual(expected)
})
it('should handle a final single-element run', () => {
const array = [
...Array.from({ length: 32 }, (_, index) => 100 - index),
200
]
expect(powerSort(array)).toEqual([
...Array.from({ length: 32 }, (_, index) => index + 69),
200
])
})
})