|
| 1 | +import { QuickSelect } from "../quick_select"; |
| 2 | + |
| 3 | +describe('QuickSelect', () => { |
| 4 | + test('should return the kth smallest element in an array', () => { |
| 5 | + const array = [8, 3, 5, 1, 4, 2]; |
| 6 | + expect(QuickSelect(array, 0)).toBe(1); |
| 7 | + expect(QuickSelect(array, 1)).toBe(2); |
| 8 | + expect(QuickSelect(array, 2)).toBe(3); |
| 9 | + expect(QuickSelect(array, 3)).toBe(4); |
| 10 | + expect(QuickSelect(array, 4)).toBe(5); |
| 11 | + expect(QuickSelect(array, 5)).toBe(8); |
| 12 | + }); |
| 13 | + |
| 14 | + test('should work with arrays of size 1', () => { |
| 15 | + const array = [4]; |
| 16 | + expect(QuickSelect(array, 0)).toBe(4); |
| 17 | + }); |
| 18 | + |
| 19 | + test('should work with large arrays', () => { |
| 20 | + const array = Array.from({length: 1000}, (_, i) => i + 1); |
| 21 | + expect(QuickSelect(array, 499)).toBe(500); |
| 22 | + }); |
| 23 | + |
| 24 | + test('should throw error when k is out of bounds', () => { |
| 25 | + const array = [8, 3, 5, 1, 4, 2]; |
| 26 | + expect(() => QuickSelect(array, -1)).toThrow(); |
| 27 | + expect(() => QuickSelect(array, 6)).toThrow(); |
| 28 | + }); |
| 29 | +}); |
0 commit comments