|
| 1 | +import isPrime from './isPrime'; |
| 2 | + |
| 3 | +describe('isPrime TypeScript function tests', () => { |
| 4 | + |
| 5 | + test('should return false for numbers less than or equal to 1', () => { |
| 6 | + expect(isPrime(-5)).toBe(false); |
| 7 | + expect(isPrime(0)).toBe(false); |
| 8 | + expect(isPrime(1)).toBe(false); |
| 9 | + }); |
| 10 | + |
| 11 | + test('should return true for prime number 2', () => { |
| 12 | + expect(isPrime(2)).toBe(true); |
| 13 | + }); |
| 14 | + |
| 15 | + test('should return true for prime number 3', () => { |
| 16 | + expect(isPrime(3)).toBe(true); |
| 17 | + }); |
| 18 | + |
| 19 | + test('should return true for small prime numbers', () => { |
| 20 | + expect(isPrime(5)).toBe(true); |
| 21 | + expect(isPrime(7)).toBe(true); |
| 22 | + expect(isPrime(11)).toBe(true); |
| 23 | + expect(isPrime(13)).toBe(true); |
| 24 | + expect(isPrime(17)).toBe(true); |
| 25 | + expect(isPrime(19)).toBe(true); |
| 26 | + }); |
| 27 | + |
| 28 | + test('should return false for small composite numbers', () => { |
| 29 | + expect(isPrime(4)).toBe(false); |
| 30 | + expect(isPrime(6)).toBe(false); |
| 31 | + expect(isPrime(8)).toBe(false); |
| 32 | + expect(isPrime(9)).toBe(false); |
| 33 | + expect(isPrime(10)).toBe(false); |
| 34 | + expect(isPrime(12)).toBe(false); |
| 35 | + expect(isPrime(14)).toBe(false); |
| 36 | + expect(isPrime(15)).toBe(false); |
| 37 | + expect(isPrime(16)).toBe(false); |
| 38 | + expect(isPrime(18)).toBe(false); |
| 39 | + }); |
| 40 | + |
| 41 | + test('should return true for larger prime numbers', () => { |
| 42 | + expect(isPrime(23)).toBe(true); |
| 43 | + expect(isPrime(29)).toBe(true); |
| 44 | + expect(isPrime(31)).toBe(true); |
| 45 | + expect(isPrime(37)).toBe(true); |
| 46 | + expect(isPrime(97)).toBe(true); |
| 47 | + expect(isPrime(101)).toBe(true); |
| 48 | + }); |
| 49 | + |
| 50 | + test('should return false for larger composite numbers', () => { |
| 51 | + expect(isPrime(21)).toBe(false); |
| 52 | + expect(isPrime(25)).toBe(false); |
| 53 | + expect(isPrime(49)).toBe(false); |
| 54 | + expect(isPrime(100)).toBe(false); |
| 55 | + expect(isPrime(121)).toBe(false); // 11^2 |
| 56 | + expect(isPrime(169)).toBe(false); // 13^2 |
| 57 | + }); |
| 58 | + |
| 59 | + test('should handle perfect squares correctly', () => { |
| 60 | + expect(isPrime(4)).toBe(false); // 2^2 |
| 61 | + expect(isPrime(9)).toBe(false); // 3^2 |
| 62 | + expect(isPrime(25)).toBe(false); // 5^2 |
| 63 | + expect(isPrime(49)).toBe(false); // 7^2 |
| 64 | + }); |
| 65 | + |
| 66 | + test('should work efficiently with larger numbers', () => { |
| 67 | + expect(isPrime(997)).toBe(true); // Large prime |
| 68 | + expect(isPrime(1009)).toBe(true); // Large prime |
| 69 | + expect(isPrime(1000)).toBe(false); // Large composite |
| 70 | + expect(isPrime(1001)).toBe(false); // 7 × 11 × 13 |
| 71 | + }); |
| 72 | + |
| 73 | + test('should handle numbers divisible by 2 or 3', () => { |
| 74 | + expect(isPrime(6)).toBe(false); // divisible by both 2 and 3 |
| 75 | + expect(isPrime(12)).toBe(false); // divisible by both 2 and 3 |
| 76 | + expect(isPrime(18)).toBe(false); // divisible by both 2 and 3 |
| 77 | + }); |
| 78 | +}); |
0 commit comments