|
1 | 1 | package com.thealgorithms.maths; |
2 | 2 |
|
3 | | -import static org.junit.jupiter.api.Assertions.assertEquals; |
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 | | -public class LeonardoNumberTest { |
| 6 | +/** |
| 7 | + * Test cases for {@link LeonardoNumber} class. |
| 8 | + * <p> |
| 9 | + * Tests both recursive and iterative implementations with various input values |
| 10 | + * including edge cases and boundary conditions. |
| 11 | + */ |
| 12 | +class LeonardoNumberTest { |
| 13 | + |
| 14 | + // Tests for recursive implementation |
| 15 | + |
| 16 | + @Test |
| 17 | + void testLeonardoNumberNegative() { |
| 18 | + Assertions.assertThrows(IllegalArgumentException.class, () -> LeonardoNumber.leonardoNumber(-1)); |
| 19 | + } |
| 20 | + |
| 21 | + @Test |
| 22 | + void testLeonardoNumberNegativeLarge() { |
| 23 | + Assertions.assertThrows(IllegalArgumentException.class, () -> LeonardoNumber.leonardoNumber(-100)); |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + void testLeonardoNumberZero() { |
| 28 | + Assertions.assertEquals(1, LeonardoNumber.leonardoNumber(0)); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + void testLeonardoNumberOne() { |
| 33 | + Assertions.assertEquals(1, LeonardoNumber.leonardoNumber(1)); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void testLeonardoNumberTwo() { |
| 38 | + Assertions.assertEquals(3, LeonardoNumber.leonardoNumber(2)); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + void testLeonardoNumberThree() { |
| 43 | + Assertions.assertEquals(5, LeonardoNumber.leonardoNumber(3)); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void testLeonardoNumberFour() { |
| 48 | + Assertions.assertEquals(9, LeonardoNumber.leonardoNumber(4)); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void testLeonardoNumberFive() { |
| 53 | + Assertions.assertEquals(15, LeonardoNumber.leonardoNumber(5)); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void testLeonardoNumberSix() { |
| 58 | + Assertions.assertEquals(25, LeonardoNumber.leonardoNumber(6)); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void testLeonardoNumberSeven() { |
| 63 | + Assertions.assertEquals(41, LeonardoNumber.leonardoNumber(7)); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void testLeonardoNumberEight() { |
| 68 | + Assertions.assertEquals(67, LeonardoNumber.leonardoNumber(8)); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void testLeonardoNumberTen() { |
| 73 | + Assertions.assertEquals(177, LeonardoNumber.leonardoNumber(10)); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void testLeonardoNumberFifteen() { |
| 78 | + Assertions.assertEquals(1973, LeonardoNumber.leonardoNumber(15)); |
| 79 | + } |
| 80 | + |
9 | 81 | @Test |
10 | | - void leonardoNumberNegative() { |
11 | | - assertThrows(ArithmeticException.class, () -> LeonardoNumber.leonardoNumber(-1)); |
| 82 | + void testLeonardoNumberTwenty() { |
| 83 | + Assertions.assertEquals(21891, LeonardoNumber.leonardoNumber(20)); |
12 | 84 | } |
| 85 | + |
| 86 | + // Tests for iterative implementation |
| 87 | + |
| 88 | + @Test |
| 89 | + void testLeonardoNumberIterativeNegative() { |
| 90 | + Assertions.assertThrows(IllegalArgumentException.class, () -> LeonardoNumber.leonardoNumberIterative(-1)); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void testLeonardoNumberIterativeNegativeLarge() { |
| 95 | + Assertions.assertThrows(IllegalArgumentException.class, () -> LeonardoNumber.leonardoNumberIterative(-50)); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void testLeonardoNumberIterativeZero() { |
| 100 | + Assertions.assertEquals(1, LeonardoNumber.leonardoNumberIterative(0)); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void testLeonardoNumberIterativeOne() { |
| 105 | + Assertions.assertEquals(1, LeonardoNumber.leonardoNumberIterative(1)); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + void testLeonardoNumberIterativeTwo() { |
| 110 | + Assertions.assertEquals(3, LeonardoNumber.leonardoNumberIterative(2)); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + void testLeonardoNumberIterativeThree() { |
| 115 | + Assertions.assertEquals(5, LeonardoNumber.leonardoNumberIterative(3)); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + void testLeonardoNumberIterativeFour() { |
| 120 | + Assertions.assertEquals(9, LeonardoNumber.leonardoNumberIterative(4)); |
| 121 | + } |
| 122 | + |
13 | 123 | @Test |
14 | | - void leonardoNumberZero() { |
15 | | - assertEquals(1, LeonardoNumber.leonardoNumber(0)); |
| 124 | + void testLeonardoNumberIterativeFive() { |
| 125 | + Assertions.assertEquals(15, LeonardoNumber.leonardoNumberIterative(5)); |
16 | 126 | } |
| 127 | + |
17 | 128 | @Test |
18 | | - void leonardoNumberOne() { |
19 | | - assertEquals(1, LeonardoNumber.leonardoNumber(1)); |
| 129 | + void testLeonardoNumberIterativeSix() { |
| 130 | + Assertions.assertEquals(25, LeonardoNumber.leonardoNumberIterative(6)); |
20 | 131 | } |
| 132 | + |
21 | 133 | @Test |
22 | | - void leonardoNumberFive() { |
23 | | - assertEquals(15, LeonardoNumber.leonardoNumber(5)); |
| 134 | + void testLeonardoNumberIterativeSeven() { |
| 135 | + Assertions.assertEquals(41, LeonardoNumber.leonardoNumberIterative(7)); |
24 | 136 | } |
| 137 | + |
| 138 | + @Test |
| 139 | + void testLeonardoNumberIterativeEight() { |
| 140 | + Assertions.assertEquals(67, LeonardoNumber.leonardoNumberIterative(8)); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + void testLeonardoNumberIterativeTen() { |
| 145 | + Assertions.assertEquals(177, LeonardoNumber.leonardoNumberIterative(10)); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + void testLeonardoNumberIterativeFifteen() { |
| 150 | + Assertions.assertEquals(1973, LeonardoNumber.leonardoNumberIterative(15)); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + void testLeonardoNumberIterativeTwenty() { |
| 155 | + Assertions.assertEquals(21891, LeonardoNumber.leonardoNumberIterative(20)); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + void testLeonardoNumberIterativeTwentyFive() { |
| 160 | + Assertions.assertEquals(242785, LeonardoNumber.leonardoNumberIterative(25)); |
| 161 | + } |
| 162 | + |
| 163 | + // Consistency tests between recursive and iterative implementations |
| 164 | + |
25 | 165 | @Test |
26 | | - void leonardoNumberTwenty() { |
27 | | - assertEquals(21891, LeonardoNumber.leonardoNumber(20)); |
| 166 | + void testConsistencyBetweenImplementations() { |
| 167 | + for (int i = 0; i <= 15; i++) { |
| 168 | + Assertions.assertEquals(LeonardoNumber.leonardoNumber(i), LeonardoNumber.leonardoNumberIterative(i), "Mismatch at index " + i); |
| 169 | + } |
28 | 170 | } |
29 | 171 | } |
0 commit comments