forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSieveOfEratosthenesTest.java
More file actions
64 lines (53 loc) · 1.79 KB
/
SieveOfEratosthenesTest.java
File metadata and controls
64 lines (53 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.thealgorithms.maths;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
/**
* Test cases for Sieve of Eratosthenes algorithm
*
* @author Navadeep0007
*/
class SieveOfEratosthenesTest {
@Test
void testPrimesUpTo10() {
List<Integer> expected = Arrays.asList(2, 3, 5, 7);
assertEquals(expected, SieveOfEratosthenes.findPrimes(10));
}
@Test
void testPrimesUpTo30() {
List<Integer> expected = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29);
assertEquals(expected, SieveOfEratosthenes.findPrimes(30));
}
@Test
void testPrimesUpTo2() {
List<Integer> expected = Arrays.asList(2);
assertEquals(expected, SieveOfEratosthenes.findPrimes(2));
}
@Test
void testPrimesUpTo1() {
assertTrue(SieveOfEratosthenes.findPrimes(1).isEmpty());
}
@Test
void testPrimesUpTo0() {
assertTrue(SieveOfEratosthenes.findPrimes(0).isEmpty());
}
@Test
void testNegativeInput() {
assertThrows(IllegalArgumentException.class, () -> { SieveOfEratosthenes.findPrimes(-1); });
}
@Test
void testCountPrimes() {
assertEquals(4, SieveOfEratosthenes.countPrimes(10));
assertEquals(25, SieveOfEratosthenes.countPrimes(100));
}
@Test
void testLargeNumber() {
List<Integer> primes = SieveOfEratosthenes.findPrimes(1000);
assertEquals(168, primes.size()); // There are 168 primes up to 1000
assertEquals(2, primes.get(0)); // First prime
assertEquals(997, primes.get(primes.size() - 1)); // Last prime up to 1000
}
}