|
| 1 | +import { powerSort } from '../PowerSort' |
| 2 | + |
| 3 | +describe('PowerSort', () => { |
| 4 | + it('should work for empty arrays', () => { |
| 5 | + expect(powerSort([])).toEqual([]) |
| 6 | + }) |
| 7 | + |
| 8 | + it('should work for single-element arrays', () => { |
| 9 | + expect(powerSort([5])).toEqual([5]) |
| 10 | + }) |
| 11 | + |
| 12 | + it('should sort arrays correctly', () => { |
| 13 | + expect(powerSort([5, 4])).toEqual([4, 5]) |
| 14 | + |
| 15 | + expect(powerSort([8, 4, 10, 15, 9])).toEqual([4, 8, 9, 10, 15]) |
| 16 | + |
| 17 | + expect(powerSort([1, 2, 3])).toEqual([1, 2, 3]) |
| 18 | + |
| 19 | + expect(powerSort([10, 5, 3, 8, 2, 6, 4, 7, 9, 1])).toEqual([ |
| 20 | + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 |
| 21 | + ]) |
| 22 | + }) |
| 23 | + |
| 24 | + it('should sort arrays containing duplicate values', () => { |
| 25 | + expect(powerSort([4, 2, 4, 1, 2, 3, 1])).toEqual([1, 1, 2, 2, 3, 4, 4]) |
| 26 | + }) |
| 27 | + |
| 28 | + it('should handle already sorted arrays', () => { |
| 29 | + expect(powerSort([1, 2, 3, 4, 5, 6])).toEqual([1, 2, 3, 4, 5, 6]) |
| 30 | + }) |
| 31 | + |
| 32 | + it('should handle reverse-sorted arrays', () => { |
| 33 | + expect(powerSort([9, 8, 7, 6, 5, 4, 3, 2, 1])).toEqual([ |
| 34 | + 1, 2, 3, 4, 5, 6, 7, 8, 9 |
| 35 | + ]) |
| 36 | + }) |
| 37 | + |
| 38 | + it('should sort arrays larger than MIN_RUN', () => { |
| 39 | + const array = Array.from({ length: 100 }, (_, index) => 100 - index) |
| 40 | + |
| 41 | + expect(powerSort(array)).toEqual( |
| 42 | + Array.from({ length: 100 }, (_, index) => index + 1) |
| 43 | + ) |
| 44 | + }) |
| 45 | + |
| 46 | + it('should return the same array after sorting', () => { |
| 47 | + const array = [5, 3, 1, 4, 2] |
| 48 | + |
| 49 | + const result = powerSort(array) |
| 50 | + |
| 51 | + expect(result).toBe(array) |
| 52 | + expect(array).toEqual([1, 2, 3, 4, 5]) |
| 53 | + }) |
| 54 | + |
| 55 | + it('should merge multiple natural runs correctly', () => { |
| 56 | + const array = [ |
| 57 | + ...Array.from({ length: 32 }, (_, i) => i + 96), |
| 58 | + ...Array.from({ length: 32 }, (_, i) => i + 64), |
| 59 | + ...Array.from({ length: 32 }, (_, i) => i + 32), |
| 60 | + ...Array.from({ length: 32 }, (_, i) => i) |
| 61 | + ] |
| 62 | + |
| 63 | + expect(powerSort(array)).toEqual(Array.from({ length: 128 }, (_, i) => i)) |
| 64 | + }) |
| 65 | + |
| 66 | + it('should merge natural runs of different lengths', () => { |
| 67 | + const array = [ |
| 68 | + ...Array.from({ length: 32 }, (_, i) => i + 200), |
| 69 | + ...Array.from({ length: 40 }, (_, i) => i + 150), |
| 70 | + ...Array.from({ length: 35 }, (_, i) => i + 100), |
| 71 | + ...Array.from({ length: 50 }, (_, i) => i) |
| 72 | + ] |
| 73 | + |
| 74 | + const expected = [ |
| 75 | + ...Array.from({ length: 50 }, (_, i) => i), |
| 76 | + ...Array.from({ length: 35 }, (_, i) => i + 100), |
| 77 | + ...Array.from({ length: 40 }, (_, i) => i + 150), |
| 78 | + ...Array.from({ length: 32 }, (_, i) => i + 200) |
| 79 | + ] |
| 80 | + |
| 81 | + expect(powerSort(array)).toEqual(expected) |
| 82 | + }) |
| 83 | +}) |
0 commit comments