|
| 1 | +import { percentile } from '../Percentile.js' |
| 2 | + |
| 3 | +describe('percentile', () => { |
| 4 | + test('percentile([1,2,3,4,5], 50) returns 3', () => { |
| 5 | + expect(percentile([1, 2, 3, 4, 5], 50)).toBe(3) |
| 6 | + }) |
| 7 | + |
| 8 | + test('percentile([1,2,3,4,5], 0) returns 1', () => { |
| 9 | + expect(percentile([1, 2, 3, 4, 5], 0)).toBe(1) |
| 10 | + }) |
| 11 | + |
| 12 | + test('percentile([1,2,3,4,5], 100) returns 5', () => { |
| 13 | + expect(percentile([1, 2, 3, 4, 5], 100)).toBe(5) |
| 14 | + }) |
| 15 | + |
| 16 | + test('percentile([1,2,3,4,5], 75) returns 4', () => { |
| 17 | + expect(percentile([1, 2, 3, 4, 5], 75)).toBe(4) |
| 18 | + }) |
| 19 | + |
| 20 | + test('percentile([1,2,3,4,5], 25) returns 2', () => { |
| 21 | + expect(percentile([1, 2, 3, 4, 5], 25)).toBe(2) |
| 22 | + }) |
| 23 | + |
| 24 | + test('percentile works with unsorted array', () => { |
| 25 | + expect(percentile([5, 3, 1, 4, 2], 50)).toBe(3) |
| 26 | + }) |
| 27 | + |
| 28 | + test('percentile single element returns that element', () => { |
| 29 | + expect(percentile([5], 50)).toBe(5) |
| 30 | + }) |
| 31 | + |
| 32 | + test('percentile does not mutate original array', () => { |
| 33 | + const arr = [5, 3, 1, 4, 2] |
| 34 | + percentile(arr, 50) |
| 35 | + expect(arr).toEqual([5, 3, 1, 4, 2]) |
| 36 | + }) |
| 37 | + |
| 38 | + test('throws TypeError for empty array', () => { |
| 39 | + expect(() => percentile([], 50)).toThrow(TypeError) |
| 40 | + }) |
| 41 | + |
| 42 | + test('throws TypeError for invalid percentile > 100', () => { |
| 43 | + expect(() => percentile([1, 2, 3], 101)).toThrow(TypeError) |
| 44 | + }) |
| 45 | + |
| 46 | + test('throws TypeError for invalid percentile < 0', () => { |
| 47 | + expect(() => percentile([1, 2, 3], -1)).toThrow(TypeError) |
| 48 | + }) |
| 49 | + |
| 50 | + test('throws TypeError for non-array input', () => { |
| 51 | + expect(() => percentile('123', 50)).toThrow(TypeError) |
| 52 | + }) |
| 53 | +}) |
0 commit comments