|
| 1 | +import { standardDeviation } from '../StandardDeviation.js' |
| 2 | + |
| 3 | +describe('standardDeviation', () => { |
| 4 | + test('standardDeviation([2,4,4,4,5,5,7,9]) returns 2', () => { |
| 5 | + expect(standardDeviation([2, 4, 4, 4, 5, 5, 7, 9])).toBe(2) |
| 6 | + }) |
| 7 | + |
| 8 | + test('standardDeviation([1, 2, 3]) returns ~0.816', () => { |
| 9 | + expect(standardDeviation([1, 2, 3])).toBeCloseTo(0.816) |
| 10 | + }) |
| 11 | + |
| 12 | + test('standardDeviation of identical elements returns 0', () => { |
| 13 | + expect(standardDeviation([5, 5, 5, 5])).toBe(0) |
| 14 | + }) |
| 15 | + |
| 16 | + test('standardDeviation single element returns 0', () => { |
| 17 | + expect(standardDeviation([42])).toBe(0) |
| 18 | + }) |
| 19 | + |
| 20 | + test('standardDeviation works with negative numbers', () => { |
| 21 | + expect(standardDeviation([-2, -1, 0, 1, 2])).toBeCloseTo(1.414) |
| 22 | + }) |
| 23 | + |
| 24 | + test('standardDeviation works with decimals', () => { |
| 25 | + expect(standardDeviation([1.5, 2.5, 3.5])).toBeCloseTo(0.816) |
| 26 | + }) |
| 27 | + |
| 28 | + test('throws TypeError for empty array', () => { |
| 29 | + expect(() => standardDeviation([])).toThrow(TypeError) |
| 30 | + }) |
| 31 | + |
| 32 | + test('throws TypeError for non-array input', () => { |
| 33 | + expect(() => standardDeviation('123')).toThrow(TypeError) |
| 34 | + }) |
| 35 | + |
| 36 | + test('throws TypeError for array with non-numbers', () => { |
| 37 | + expect(() => standardDeviation([1, 'two', 3])).toThrow(TypeError) |
| 38 | + }) |
| 39 | +}) |
0 commit comments