|
| 1 | +import { QuickSelect } from './QuickSelect' |
| 2 | + |
| 3 | +describe('QuickSelect tests', () => { |
| 4 | + it('should return the only element of a list of length 1', () => { |
| 5 | + // Test a mix of number types (i.e., positive/negative, numbers with decimals, fractions) |
| 6 | + expect(QuickSelect([100], 1)).toEqual(100) |
| 7 | + expect(QuickSelect([-23], 1)).toEqual(-23) |
| 8 | + expect(QuickSelect([2007.102], 1)).toEqual(2007.102) |
| 9 | + expect(QuickSelect([0.9], 1)).toEqual(0.9) |
| 10 | + expect(QuickSelect([-0.075], 1)).toEqual(-0.075) |
| 11 | + expect(QuickSelect([0], 1)).toEqual(0) |
| 12 | + expect(QuickSelect([1], 1)).toEqual(1) |
| 13 | + }) |
| 14 | + |
| 15 | + it('should throw an Error when k is greater than the length of the list', () => { |
| 16 | + expect(() => QuickSelect([100, 2], 5)).toThrow('Index Out of Bound') |
| 17 | + }) |
| 18 | + |
| 19 | + it('should throw an Error when k is less than 1', () => { |
| 20 | + expect(() => QuickSelect([100, 2], 0)).toThrow('Index Out of Bound') |
| 21 | + expect(() => QuickSelect([100, 2], -1)).toThrow('Index Out of Bound') |
| 22 | + }) |
| 23 | + |
| 24 | + describe('varieties of list composition', () => { |
| 25 | + it('should return the kth smallest element of a list that is in increasing order', () => { |
| 26 | + expect(QuickSelect([10, 22, 33, 44, 55], 1)).toEqual(10) |
| 27 | + expect(QuickSelect([10, 22, 33, 44, 55], 2)).toEqual(22) |
| 28 | + expect(QuickSelect([10, 22, 33, 44, 55], 3)).toEqual(33) |
| 29 | + expect(QuickSelect([10, 22, 33, 44, 55], 4)).toEqual(44) |
| 30 | + expect(QuickSelect([10, 22, 33, 44, 55], 5)).toEqual(55) |
| 31 | + }) |
| 32 | + |
| 33 | + it('should return the kth smallest element of an input list that is in decreasing order', () => { |
| 34 | + expect(QuickSelect([82, 33.12, 4.0, 1], 1)).toEqual(1) |
| 35 | + expect(QuickSelect([82, 33.12, 4.0, 1], 2)).toEqual(4.0) |
| 36 | + expect(QuickSelect([82, 33.12, 4.0, 1], 2)).toEqual(4) |
| 37 | + expect(QuickSelect([82, 33.12, 4.0, 1], 3)).toEqual(33.12) |
| 38 | + expect(QuickSelect([82, 33.12, 4.0, 1], 4)).toEqual(82) |
| 39 | + }) |
| 40 | + |
| 41 | + it('should return the kth smallest element of an input list that is no particular order', () => { |
| 42 | + expect(QuickSelect([123, 14231, -10, 0, 15], 3)).toEqual(15) |
| 43 | + expect(QuickSelect([0, 15, 123, 14231, -10], 3)).toEqual(15) |
| 44 | + expect(QuickSelect([-10, 15, 123, 14231, 0], 3)).toEqual(15) |
| 45 | + expect(QuickSelect([14231, 0, 15, 123, -10], 3)).toEqual(15) |
| 46 | + expect(QuickSelect([14231, 0, 15, -10, 123], 3)).toEqual(15) |
| 47 | + }) |
| 48 | + }) |
| 49 | +}) |
0 commit comments