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