|
1 | 1 | package com.thealgorithms.maths; |
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
4 | 5 |
|
5 | 6 | import org.junit.jupiter.api.Test; |
6 | 7 |
|
7 | | -public class LucasSeriesTest { |
| 8 | +/** |
| 9 | + * Test cases for {@link LucasSeries} class. |
| 10 | + * Tests both recursive and iterative implementations for correctness, |
| 11 | + * edge cases, and error handling. |
| 12 | + */ |
| 13 | +class LucasSeriesTest { |
| 14 | + |
| 15 | + /** |
| 16 | + * Test the first Lucas number L(1) = 2 |
| 17 | + */ |
8 | 18 | @Test |
9 | | - void lucasSeriesTwo() { |
| 19 | + void testFirstLucasNumber() { |
10 | 20 | assertEquals(2, LucasSeries.lucasSeries(1)); |
11 | 21 | assertEquals(2, LucasSeries.lucasSeriesIteration(1)); |
12 | 22 | } |
| 23 | + |
| 24 | + /** |
| 25 | + * Test the second Lucas number L(2) = 1 |
| 26 | + */ |
13 | 27 | @Test |
14 | | - void lucasSeriesOne() { |
| 28 | + void testSecondLucasNumber() { |
15 | 29 | assertEquals(1, LucasSeries.lucasSeries(2)); |
16 | 30 | assertEquals(1, LucasSeries.lucasSeriesIteration(2)); |
17 | 31 | } |
| 32 | + |
| 33 | + /** |
| 34 | + * Test the third Lucas number L(3) = 3 |
| 35 | + */ |
| 36 | + @Test |
| 37 | + void testThirdLucasNumber() { |
| 38 | + assertEquals(3, LucasSeries.lucasSeries(3)); |
| 39 | + assertEquals(3, LucasSeries.lucasSeriesIteration(3)); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Test the fourth Lucas number L(4) = 4 |
| 44 | + */ |
18 | 45 | @Test |
19 | | - void lucasSeriesSeven() { |
| 46 | + void testFourthLucasNumber() { |
| 47 | + assertEquals(4, LucasSeries.lucasSeries(4)); |
| 48 | + assertEquals(4, LucasSeries.lucasSeriesIteration(4)); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Test the fifth Lucas number L(5) = 7 |
| 53 | + */ |
| 54 | + @Test |
| 55 | + void testFifthLucasNumber() { |
20 | 56 | assertEquals(7, LucasSeries.lucasSeries(5)); |
21 | 57 | assertEquals(7, LucasSeries.lucasSeriesIteration(5)); |
22 | 58 | } |
| 59 | + |
| 60 | + /** |
| 61 | + * Test the sixth Lucas number L(6) = 11 |
| 62 | + */ |
| 63 | + @Test |
| 64 | + void testSixthLucasNumber() { |
| 65 | + assertEquals(11, LucasSeries.lucasSeries(6)); |
| 66 | + assertEquals(11, LucasSeries.lucasSeriesIteration(6)); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Test the seventh Lucas number L(7) = 18 |
| 71 | + */ |
| 72 | + @Test |
| 73 | + void testSeventhLucasNumber() { |
| 74 | + assertEquals(18, LucasSeries.lucasSeries(7)); |
| 75 | + assertEquals(18, LucasSeries.lucasSeriesIteration(7)); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Test the eighth Lucas number L(8) = 29 |
| 80 | + */ |
| 81 | + @Test |
| 82 | + void testEighthLucasNumber() { |
| 83 | + assertEquals(29, LucasSeries.lucasSeries(8)); |
| 84 | + assertEquals(29, LucasSeries.lucasSeriesIteration(8)); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Test the ninth Lucas number L(9) = 47 |
| 89 | + */ |
23 | 90 | @Test |
24 | | - void lucasSeriesEleven() { |
| 91 | + void testNinthLucasNumber() { |
| 92 | + assertEquals(47, LucasSeries.lucasSeries(9)); |
| 93 | + assertEquals(47, LucasSeries.lucasSeriesIteration(9)); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Test the tenth Lucas number L(10) = 76 |
| 98 | + */ |
| 99 | + @Test |
| 100 | + void testTenthLucasNumber() { |
| 101 | + assertEquals(76, LucasSeries.lucasSeries(10)); |
| 102 | + assertEquals(76, LucasSeries.lucasSeriesIteration(10)); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Test the eleventh Lucas number L(11) = 123 |
| 107 | + */ |
| 108 | + @Test |
| 109 | + void testEleventhLucasNumber() { |
25 | 110 | assertEquals(123, LucasSeries.lucasSeries(11)); |
26 | 111 | assertEquals(123, LucasSeries.lucasSeriesIteration(11)); |
27 | 112 | } |
| 113 | + |
| 114 | + /** |
| 115 | + * Test larger Lucas numbers to ensure correctness |
| 116 | + */ |
| 117 | + @Test |
| 118 | + void testLargerLucasNumbers() { |
| 119 | + assertEquals(199, LucasSeries.lucasSeries(12)); |
| 120 | + assertEquals(199, LucasSeries.lucasSeriesIteration(12)); |
| 121 | + assertEquals(322, LucasSeries.lucasSeries(13)); |
| 122 | + assertEquals(322, LucasSeries.lucasSeriesIteration(13)); |
| 123 | + assertEquals(521, LucasSeries.lucasSeries(14)); |
| 124 | + assertEquals(521, LucasSeries.lucasSeriesIteration(14)); |
| 125 | + assertEquals(843, LucasSeries.lucasSeries(15)); |
| 126 | + assertEquals(843, LucasSeries.lucasSeriesIteration(15)); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Test that both methods produce the same results |
| 131 | + */ |
| 132 | + @Test |
| 133 | + void testRecursiveAndIterativeConsistency() { |
| 134 | + for (int i = 1; i <= 15; i++) { |
| 135 | + assertEquals(LucasSeries.lucasSeries(i), LucasSeries.lucasSeriesIteration(i), "Mismatch at position " + i); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Test invalid input: zero |
| 141 | + */ |
| 142 | + @Test |
| 143 | + void testZeroInputThrowsException() { |
| 144 | + assertThrows(IllegalArgumentException.class, () -> LucasSeries.lucasSeries(0)); |
| 145 | + assertThrows(IllegalArgumentException.class, () -> LucasSeries.lucasSeriesIteration(0)); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Test invalid input: negative number |
| 150 | + */ |
| 151 | + @Test |
| 152 | + void testNegativeInputThrowsException() { |
| 153 | + assertThrows(IllegalArgumentException.class, () -> LucasSeries.lucasSeries(-1)); |
| 154 | + assertThrows(IllegalArgumentException.class, () -> LucasSeries.lucasSeriesIteration(-1)); |
| 155 | + assertThrows(IllegalArgumentException.class, () -> LucasSeries.lucasSeries(-5)); |
| 156 | + assertThrows(IllegalArgumentException.class, () -> LucasSeries.lucasSeriesIteration(-5)); |
| 157 | + } |
28 | 158 | } |
0 commit comments