|
1 | 1 | import { meanAbsoluteDeviation } from '../MeanAbsoluteDeviation.js' |
2 | 2 |
|
3 | | -describe('tests for mean absolute deviation', () => { |
4 | | - it('should be a function', () => { |
5 | | - expect(typeof meanAbsoluteDeviation).toEqual('function') |
| 3 | +describe('meanAbsoluteDeviation', () => { |
| 4 | + test('meanAbsoluteDeviation([2,2,3,4,14]) returns 3.6', () => { |
| 5 | + expect(meanAbsoluteDeviation([2, 2, 3, 4, 14])).toBeCloseTo(3.6) |
6 | 6 | }) |
7 | 7 |
|
8 | | - it('should throw an invalid input error', () => { |
9 | | - expect(() => meanAbsoluteDeviation('fgh')).toThrow() |
| 8 | + test('meanAbsoluteDeviation([1,1,1]) returns 0', () => { |
| 9 | + expect(meanAbsoluteDeviation([1, 1, 1])).toBe(0) |
10 | 10 | }) |
11 | 11 |
|
12 | | - it('should return the mean absolute deviation of an array of numbers', () => { |
13 | | - const meanAbDev = meanAbsoluteDeviation([2, 34, 5, 0, -2]) |
14 | | - expect(meanAbDev).toBe(10.479999999999999) |
| 12 | + test('meanAbsoluteDeviation single element returns 0', () => { |
| 13 | + expect(meanAbsoluteDeviation([5])).toBe(0) |
| 14 | + }) |
| 15 | + |
| 16 | + test('meanAbsoluteDeviation([1,2,3,4,5]) returns 1.2', () => { |
| 17 | + expect(meanAbsoluteDeviation([1, 2, 3, 4, 5])).toBeCloseTo(1.2) |
| 18 | + }) |
| 19 | + |
| 20 | + test('meanAbsoluteDeviation works with negative numbers', () => { |
| 21 | + expect(meanAbsoluteDeviation([-2, -1, 0, 1, 2])).toBeCloseTo(1.2) |
| 22 | + }) |
| 23 | + |
| 24 | + test('meanAbsoluteDeviation([10, 20]) returns 5', () => { |
| 25 | + expect(meanAbsoluteDeviation([10, 20])).toBeCloseTo(5) |
| 26 | + }) |
| 27 | + |
| 28 | + test('meanAbsoluteDeviation does not mutate original array', () => { |
| 29 | + const arr = [1, 2, 3, 4, 5] |
| 30 | + meanAbsoluteDeviation(arr) |
| 31 | + expect(arr).toEqual([1, 2, 3, 4, 5]) |
| 32 | + }) |
| 33 | + |
| 34 | + test('throws TypeError for empty array', () => { |
| 35 | + expect(() => meanAbsoluteDeviation([])).toThrow(TypeError) |
| 36 | + }) |
| 37 | + |
| 38 | + test('throws TypeError for non-array input', () => { |
| 39 | + expect(() => meanAbsoluteDeviation('123')).toThrow(TypeError) |
| 40 | + }) |
| 41 | + |
| 42 | + test('throws TypeError for array with non-numbers', () => { |
| 43 | + expect(() => meanAbsoluteDeviation([1, 'two', 3])).toThrow(TypeError) |
15 | 44 | }) |
16 | 45 | }) |
0 commit comments