|
1 | 1 | package com.thealgorithms.maths;
|
2 | 2 |
|
3 |
| -import static org.junit.jupiter.api.Assertions.assertAll; |
4 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
5 | 4 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
6 | 5 |
|
| 6 | +import java.util.stream.Stream; |
7 | 7 | import org.junit.jupiter.api.Test;
|
| 8 | +import org.junit.jupiter.params.ParameterizedTest; |
| 9 | +import org.junit.jupiter.params.provider.MethodSource; |
8 | 10 |
|
9 | 11 | /**
|
10 | 12 | * Test class for CatalanNumbers
|
11 | 13 | */
|
12 | 14 | class CatalanNumbersTest {
|
13 | 15 |
|
14 |
| - @Test |
15 |
| - void testCatalanNumbers() { |
16 |
| - assertEquals(1, CatalanNumbers.catalan(0)); // C(0) = 1 |
17 |
| - assertEquals(1, CatalanNumbers.catalan(1)); // C(1) = 1 |
18 |
| - assertEquals(2, CatalanNumbers.catalan(2)); // C(2) = 2 |
19 |
| - assertEquals(5, CatalanNumbers.catalan(3)); // C(3) = 5 |
20 |
| - assertEquals(14, CatalanNumbers.catalan(4)); // C(4) = 14 |
21 |
| - assertEquals(42, CatalanNumbers.catalan(5)); // C(5) = 42 |
22 |
| - assertEquals(132, CatalanNumbers.catalan(6)); // C(6) = 132 |
23 |
| - assertEquals(429, CatalanNumbers.catalan(7)); // C(7) = 429 |
24 |
| - assertEquals(1430, CatalanNumbers.catalan(8)); // C(8) = 1430 |
25 |
| - assertEquals(4862, CatalanNumbers.catalan(9)); // C(9) = 4862 |
26 |
| - assertEquals(16796, CatalanNumbers.catalan(10)); // C(10) = 16796 |
| 16 | + /** |
| 17 | + * Provides test data for the parameterized Catalan number test. |
| 18 | + * Each array contains two elements: |
| 19 | + * [input number, expected Catalan number for that input] |
| 20 | + */ |
| 21 | + static Stream<Object[]> catalanNumbersProvider() { |
| 22 | + return Stream.of( |
| 23 | + new Object[]{0, 1}, |
| 24 | + new Object[]{1, 1}, |
| 25 | + new Object[]{2, 2}, |
| 26 | + new Object[]{3, 5}, |
| 27 | + new Object[]{4, 14}, |
| 28 | + new Object[]{5, 42}, |
| 29 | + new Object[]{6, 132}, |
| 30 | + new Object[]{7, 429}, |
| 31 | + new Object[]{8, 1430}, |
| 32 | + new Object[]{9, 4862}, |
| 33 | + new Object[]{10, 16796} |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Parameterized test for checking the correctness of Catalan numbers. |
| 39 | + * Uses the data from the provider method 'catalanNumbersProvider'. |
| 40 | + */ |
| 41 | + @ParameterizedTest |
| 42 | + @MethodSource("catalanNumbersProvider") |
| 43 | + void testCatalanNumbers(int input, int expected) { |
| 44 | + assertEquals(expected, CatalanNumbers.catalan(input), |
| 45 | + () -> String.format("Catalan number for input %d should be %d", input, expected)); |
27 | 46 | }
|
28 | 47 |
|
| 48 | + /** |
| 49 | + * Test for invalid inputs which should throw an IllegalArgumentException. |
| 50 | + */ |
29 | 51 | @Test
|
30 | 52 | void testIllegalInput() {
|
31 |
| - assertAll(() -> assertThrows(IllegalArgumentException.class, () -> CatalanNumbers.catalan(-1)), () -> assertThrows(IllegalArgumentException.class, () -> CatalanNumbers.catalan(-5))); |
| 53 | + assertThrows(IllegalArgumentException.class, () -> CatalanNumbers.catalan(-1)); |
| 54 | + assertThrows(IllegalArgumentException.class, () -> CatalanNumbers.catalan(-5)); |
32 | 55 | }
|
33 | 56 | }
|
0 commit comments