|
1 | 1 | package com.thealgorithms.maths; |
2 | 2 |
|
3 | | -import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
4 | 4 | import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
5 | 6 |
|
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.List; |
6 | 9 | import org.junit.jupiter.api.Test; |
7 | 10 |
|
| 11 | +/** |
| 12 | + * Test cases for Sieve of Eratosthenes algorithm |
| 13 | + * |
| 14 | + * @author Navadeep0007 |
| 15 | + */ |
8 | 16 | class SieveOfEratosthenesTest { |
| 17 | + |
| 18 | + @Test |
| 19 | + void testPrimesUpTo10() { |
| 20 | + List<Integer> expected = Arrays.asList(2, 3, 5, 7); |
| 21 | + assertEquals(expected, SieveOfEratosthenes.findPrimes(10)); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + void testPrimesUpTo30() { |
| 26 | + List<Integer> expected = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29); |
| 27 | + assertEquals(expected, SieveOfEratosthenes.findPrimes(30)); |
| 28 | + } |
| 29 | + |
9 | 30 | @Test |
10 | | - public void testfFindPrimesTill1() { |
11 | | - assertArrayEquals(new int[] {}, SieveOfEratosthenes.findPrimesTill(1)); |
| 31 | + void testPrimesUpTo2() { |
| 32 | + List<Integer> expected = Arrays.asList(2); |
| 33 | + assertEquals(expected, SieveOfEratosthenes.findPrimes(2)); |
12 | 34 | } |
13 | 35 |
|
14 | 36 | @Test |
15 | | - public void testfFindPrimesTill2() { |
16 | | - assertArrayEquals(new int[] {2}, SieveOfEratosthenes.findPrimesTill(2)); |
| 37 | + void testPrimesUpTo1() { |
| 38 | + assertTrue(SieveOfEratosthenes.findPrimes(1).isEmpty()); |
17 | 39 | } |
18 | 40 |
|
19 | 41 | @Test |
20 | | - public void testfFindPrimesTill4() { |
21 | | - var primesTill4 = new int[] {2, 3}; |
22 | | - assertArrayEquals(primesTill4, SieveOfEratosthenes.findPrimesTill(3)); |
23 | | - assertArrayEquals(primesTill4, SieveOfEratosthenes.findPrimesTill(4)); |
| 42 | + void testPrimesUpTo0() { |
| 43 | + assertTrue(SieveOfEratosthenes.findPrimes(0).isEmpty()); |
24 | 44 | } |
25 | 45 |
|
26 | 46 | @Test |
27 | | - public void testfFindPrimesTill40() { |
28 | | - var primesTill40 = new int[] {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}; |
29 | | - assertArrayEquals(primesTill40, SieveOfEratosthenes.findPrimesTill(37)); |
30 | | - assertArrayEquals(primesTill40, SieveOfEratosthenes.findPrimesTill(38)); |
31 | | - assertArrayEquals(primesTill40, SieveOfEratosthenes.findPrimesTill(39)); |
32 | | - assertArrayEquals(primesTill40, SieveOfEratosthenes.findPrimesTill(40)); |
| 47 | + void testNegativeInput() { |
| 48 | + assertThrows(IllegalArgumentException.class, () -> { SieveOfEratosthenes.findPrimes(-1); }); |
33 | 49 | } |
34 | 50 |
|
35 | 51 | @Test |
36 | | - public void testfFindPrimesTill240() { |
37 | | - var primesTill240 = new int[] {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239}; |
38 | | - assertArrayEquals(primesTill240, SieveOfEratosthenes.findPrimesTill(239)); |
39 | | - assertArrayEquals(primesTill240, SieveOfEratosthenes.findPrimesTill(240)); |
| 52 | + void testCountPrimes() { |
| 53 | + assertEquals(4, SieveOfEratosthenes.countPrimes(10)); |
| 54 | + assertEquals(25, SieveOfEratosthenes.countPrimes(100)); |
40 | 55 | } |
41 | 56 |
|
42 | 57 | @Test |
43 | | - public void testFindPrimesTillThrowsExceptionForNonPositiveInput() { |
44 | | - assertThrows(IllegalArgumentException.class, () -> SieveOfEratosthenes.findPrimesTill(0)); |
| 58 | + void testLargeNumber() { |
| 59 | + List<Integer> primes = SieveOfEratosthenes.findPrimes(1000); |
| 60 | + assertEquals(168, primes.size()); // There are 168 primes up to 1000 |
| 61 | + assertEquals(2, primes.get(0)); // First prime |
| 62 | + assertEquals(997, primes.get(primes.size() - 1)); // Last prime up to 1000 |
45 | 63 | } |
46 | 64 | } |
0 commit comments