|
| 1 | +import { zeckendorfRepresentation } from '../ZeckendorfRepresentation.js' |
| 2 | + |
| 3 | +describe('zeckendorfRepresentation', () => { |
| 4 | + test('zeckendorfRepresentation(1) returns [1]', () => { |
| 5 | + expect(zeckendorfRepresentation(1)).toEqual([1]) |
| 6 | + }) |
| 7 | + |
| 8 | + test('zeckendorfRepresentation(2) returns [2]', () => { |
| 9 | + expect(zeckendorfRepresentation(2)).toEqual([2]) |
| 10 | + }) |
| 11 | + |
| 12 | + test('zeckendorfRepresentation(3) returns [3]', () => { |
| 13 | + expect(zeckendorfRepresentation(3)).toEqual([3]) |
| 14 | + }) |
| 15 | + |
| 16 | + test('zeckendorfRepresentation(11) returns [8, 3]', () => { |
| 17 | + expect(zeckendorfRepresentation(11)).toEqual([8, 3]) |
| 18 | + }) |
| 19 | + |
| 20 | + test('zeckendorfRepresentation(100) returns [89, 8, 3]', () => { |
| 21 | + expect(zeckendorfRepresentation(100)).toEqual([89, 8, 3]) |
| 22 | + }) |
| 23 | + |
| 24 | + test('result sums to original number', () => { |
| 25 | + const result = zeckendorfRepresentation(50) |
| 26 | + const sum = result.reduce((acc, n) => acc + n, 0) |
| 27 | + expect(sum).toBe(50) |
| 28 | + }) |
| 29 | + |
| 30 | + test('no two consecutive Fibonacci numbers in result', () => { |
| 31 | + const fibs = [1, 2, 3, 5, 8, 13, 21, 34, 55, 89] |
| 32 | + const result = zeckendorfRepresentation(100) |
| 33 | + for (let i = 0; i < result.length - 1; i++) { |
| 34 | + const idx1 = fibs.indexOf(result[i]) |
| 35 | + const idx2 = fibs.indexOf(result[i + 1]) |
| 36 | + expect(Math.abs(idx1 - idx2)).toBeGreaterThan(1) |
| 37 | + } |
| 38 | + }) |
| 39 | + |
| 40 | + test('throws TypeError for zero', () => { |
| 41 | + expect(() => zeckendorfRepresentation(0)).toThrow(TypeError) |
| 42 | + }) |
| 43 | + |
| 44 | + test('throws TypeError for negative input', () => { |
| 45 | + expect(() => zeckendorfRepresentation(-11)).toThrow(TypeError) |
| 46 | + }) |
| 47 | + |
| 48 | + test('throws TypeError for float input', () => { |
| 49 | + expect(() => zeckendorfRepresentation(11.5)).toThrow(TypeError) |
| 50 | + }) |
| 51 | +}) |
0 commit comments