|
| 1 | +import { collatzSequence } from '../collatz_sequence' |
| 2 | + |
| 3 | +describe('collatzSequence', () => { |
| 4 | + test('should generate the correct Collatz sequence for n = 5', () => { |
| 5 | + const result = collatzSequence(5) |
| 6 | + expect(result).toEqual([5, 16, 8, 4, 2, 1]) |
| 7 | + }) |
| 8 | + |
| 9 | + test('should generate the correct Collatz sequence for n = 1', () => { |
| 10 | + const result = collatzSequence(1) |
| 11 | + expect(result).toEqual([1]) |
| 12 | + }) |
| 13 | + |
| 14 | + test('should handle large inputs correctly (n = 27)', () => { |
| 15 | + const result = collatzSequence(27) |
| 16 | + // The exact sequence for n = 27 is very long; check key parts |
| 17 | + expect(result[0]).toBe(27) |
| 18 | + expect(result[result.length - 1]).toBe(1) |
| 19 | + expect(result.length).toBeGreaterThan(100) |
| 20 | + }) |
| 21 | + |
| 22 | + test('should throw an error for n = 0', () => { |
| 23 | + expect(() => collatzSequence(0)).toThrow( |
| 24 | + 'Sequence only defined for positive integers.' |
| 25 | + ) |
| 26 | + }) |
| 27 | + |
| 28 | + test('should throw an error for negative inputs (n = -5)', () => { |
| 29 | + expect(() => collatzSequence(-5)).toThrow( |
| 30 | + 'Sequence only defined for positive integers.' |
| 31 | + ) |
| 32 | + }) |
| 33 | + |
| 34 | + test('should throw an error for non-integer inputs (n = 5.5)', () => { |
| 35 | + expect(() => collatzSequence(5.5)).toThrow( |
| 36 | + 'Sequence only defined for positive integers.' |
| 37 | + ) |
| 38 | + }) |
| 39 | +}) |
0 commit comments