|
| 1 | +import { describe, it, expect } from 'vitest' |
| 2 | +import basicPrefixSum from './BasicPrefixSum.js' |
| 3 | + |
| 4 | +describe('BasicPrefixSum', () => { |
| 5 | + it('should compute prefix sum for normal array', () => { |
| 6 | + const input = [1, 2, 3, 4, 5] |
| 7 | + const expected = [1, 3, 6, 10, 15] |
| 8 | + expect(basicPrefixSum(input)).toEqual(expected) |
| 9 | + }) |
| 10 | + |
| 11 | + it('should handle empty array', () => { |
| 12 | + expect(basicPrefixSum([])).toEqual([]) |
| 13 | + }) |
| 14 | + |
| 15 | + it('should handle single element array', () => { |
| 16 | + expect(basicPrefixSum([5])).toEqual([5]) |
| 17 | + }) |
| 18 | + |
| 19 | + it('should handle negative numbers', () => { |
| 20 | + const input = [-1, 2, -3, 4] |
| 21 | + const expected = [-1, 1, -2, 2] |
| 22 | + expect(basicPrefixSum(input)).toEqual(expected) |
| 23 | + }) |
| 24 | + |
| 25 | + it('should throw TypeError for non-array input', () => { |
| 26 | + expect(() => basicPrefixSum('not an array')).toThrow(TypeError) |
| 27 | + expect(() => basicPrefixSum(123)).toThrow(TypeError) |
| 28 | + expect(() => basicPrefixSum(null)).toThrow(TypeError) |
| 29 | + }) |
| 30 | + |
| 31 | + it('should throw TypeError for non-numeric array elements', () => { |
| 32 | + expect(() => basicPrefixSum([1, 'string', 3])).toThrow(TypeError) |
| 33 | + expect(() => basicPrefixSum([1, null, 3])).toThrow(TypeError) |
| 34 | + expect(() => basicPrefixSum([1, undefined, 3])).toThrow(TypeError) |
| 35 | + }) |
| 36 | + |
| 37 | + it('should throw TypeError for infinite values', () => { |
| 38 | + expect(() => basicPrefixSum([1, Infinity, 3])).toThrow(TypeError) |
| 39 | + expect(() => basicPrefixSum([1, -Infinity, 3])).toThrow(TypeError) |
| 40 | + expect(() => basicPrefixSum([1, NaN, 3])).toThrow(TypeError) |
| 41 | + }) |
| 42 | +}) |
0 commit comments