|
1 | 1 | package com.thealgorithms.maths; |
2 | 2 |
|
3 | | -import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
4 | | -import static org.junit.jupiter.api.Assertions.assertThrows; |
5 | | - |
| 3 | +import org.junit.jupiter.api.Assertions; |
6 | 4 | import org.junit.jupiter.api.Test; |
7 | 5 |
|
8 | | -class SieveOfEratosthenesTest { |
9 | | - @Test |
10 | | - public void testfFindPrimesTill1() { |
11 | | - assertArrayEquals(new int[] {}, SieveOfEratosthenes.findPrimesTill(1)); |
12 | | - } |
13 | | - |
14 | | - @Test |
15 | | - public void testfFindPrimesTill2() { |
16 | | - assertArrayEquals(new int[] {2}, SieveOfEratosthenes.findPrimesTill(2)); |
17 | | - } |
18 | | - |
19 | | - @Test |
20 | | - public void testfFindPrimesTill4() { |
21 | | - var primesTill4 = new int[] {2, 3}; |
22 | | - assertArrayEquals(primesTill4, SieveOfEratosthenes.findPrimesTill(3)); |
23 | | - assertArrayEquals(primesTill4, SieveOfEratosthenes.findPrimesTill(4)); |
24 | | - } |
25 | | - |
26 | | - @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)); |
33 | | - } |
| 6 | +/** |
| 7 | + * Unit tests for {@link SieveOfEratosthenes}. |
| 8 | + */ |
| 9 | +public final class SieveOfEratosthenesTest { |
34 | 10 |
|
35 | 11 | @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)); |
| 12 | + void testPrimesUpTo30() { |
| 13 | + int[] expected = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; |
| 14 | + Assertions.assertArrayEquals(expected, SieveOfEratosthenes.sieve(30)); |
40 | 15 | } |
41 | 16 |
|
42 | 17 | @Test |
43 | | - public void testFindPrimesTillThrowsExceptionForNonPositiveInput() { |
44 | | - assertThrows(IllegalArgumentException.class, () -> SieveOfEratosthenes.findPrimesTill(0)); |
| 18 | + void testLessThanTwo() { |
| 19 | + Assertions.assertArrayEquals(new int[0], SieveOfEratosthenes.sieve(1)); |
| 20 | + Assertions.assertArrayEquals(new int[0], SieveOfEratosthenes.sieve(0)); |
| 21 | + Assertions.assertArrayEquals(new int[0], SieveOfEratosthenes.sieve(-5)); |
45 | 22 | } |
46 | 23 | } |
0 commit comments