|
| 1 | +package com.thealgorithms.maths; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | + |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +/** |
| 10 | + * Test cases for {@link KeithNumber}. |
| 11 | + */ |
| 12 | +class KeithNumberTest { |
| 13 | + |
| 14 | + /** |
| 15 | + * Tests single-digit Keith numbers. |
| 16 | + * All single-digit numbers (1-9) are Keith numbers by definition. |
| 17 | + */ |
| 18 | + @Test |
| 19 | + void testSingleDigitKeithNumbers() { |
| 20 | + assertTrue(KeithNumber.isKeith(1)); |
| 21 | + assertTrue(KeithNumber.isKeith(2)); |
| 22 | + assertTrue(KeithNumber.isKeith(3)); |
| 23 | + assertTrue(KeithNumber.isKeith(4)); |
| 24 | + assertTrue(KeithNumber.isKeith(5)); |
| 25 | + assertTrue(KeithNumber.isKeith(6)); |
| 26 | + assertTrue(KeithNumber.isKeith(7)); |
| 27 | + assertTrue(KeithNumber.isKeith(8)); |
| 28 | + assertTrue(KeithNumber.isKeith(9)); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Tests two-digit Keith numbers. |
| 33 | + * Known two-digit Keith numbers: 14, 19, 28, 47, 61, 75. |
| 34 | + */ |
| 35 | + @Test |
| 36 | + void testTwoDigitKeithNumbers() { |
| 37 | + assertTrue(KeithNumber.isKeith(14)); |
| 38 | + assertTrue(KeithNumber.isKeith(19)); |
| 39 | + assertTrue(KeithNumber.isKeith(28)); |
| 40 | + assertTrue(KeithNumber.isKeith(47)); |
| 41 | + assertTrue(KeithNumber.isKeith(61)); |
| 42 | + assertTrue(KeithNumber.isKeith(75)); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Tests three-digit Keith numbers. |
| 47 | + * Known three-digit Keith numbers: 197, 742. |
| 48 | + */ |
| 49 | + @Test |
| 50 | + void testThreeDigitKeithNumbers() { |
| 51 | + assertTrue(KeithNumber.isKeith(197)); |
| 52 | + assertTrue(KeithNumber.isKeith(742)); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Tests four-digit Keith numbers. |
| 57 | + * Known four-digit Keith numbers: 1104, 1537, 2208, 2580, 3684, 4788, 7385, |
| 58 | + * 7647, 7909. |
| 59 | + */ |
| 60 | + @Test |
| 61 | + void testFourDigitKeithNumbers() { |
| 62 | + assertTrue(KeithNumber.isKeith(1104)); |
| 63 | + assertTrue(KeithNumber.isKeith(1537)); |
| 64 | + assertTrue(KeithNumber.isKeith(2208)); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Tests two-digit non-Keith numbers. |
| 69 | + */ |
| 70 | + @Test |
| 71 | + void testTwoDigitNonKeithNumbers() { |
| 72 | + assertFalse(KeithNumber.isKeith(10)); |
| 73 | + assertFalse(KeithNumber.isKeith(11)); |
| 74 | + assertFalse(KeithNumber.isKeith(12)); |
| 75 | + assertFalse(KeithNumber.isKeith(13)); |
| 76 | + assertFalse(KeithNumber.isKeith(15)); |
| 77 | + assertFalse(KeithNumber.isKeith(20)); |
| 78 | + assertFalse(KeithNumber.isKeith(30)); |
| 79 | + assertFalse(KeithNumber.isKeith(50)); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Tests three-digit non-Keith numbers. |
| 84 | + */ |
| 85 | + @Test |
| 86 | + void testThreeDigitNonKeithNumbers() { |
| 87 | + assertFalse(KeithNumber.isKeith(100)); |
| 88 | + assertFalse(KeithNumber.isKeith(123)); |
| 89 | + assertFalse(KeithNumber.isKeith(196)); |
| 90 | + assertFalse(KeithNumber.isKeith(198)); |
| 91 | + assertFalse(KeithNumber.isKeith(456)); |
| 92 | + assertFalse(KeithNumber.isKeith(741)); |
| 93 | + assertFalse(KeithNumber.isKeith(743)); |
| 94 | + assertFalse(KeithNumber.isKeith(999)); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Tests validation for edge case 14 in detail. |
| 99 | + * 14 is a Keith number: 1, 4, 5 (1+4), 9 (4+5), 14 (5+9). |
| 100 | + */ |
| 101 | + @Test |
| 102 | + void testKeithNumber14() { |
| 103 | + assertTrue(KeithNumber.isKeith(14)); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Tests validation for edge case 197 in detail. |
| 108 | + * 197 is a Keith number: 1, 9, 7, 17, 33, 57, 107, 197. |
| 109 | + */ |
| 110 | + @Test |
| 111 | + void testKeithNumber197() { |
| 112 | + assertTrue(KeithNumber.isKeith(197)); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Tests that zero throws an IllegalArgumentException. |
| 117 | + */ |
| 118 | + @Test |
| 119 | + void testZeroThrowsException() { |
| 120 | + assertThrows(IllegalArgumentException.class, () -> KeithNumber.isKeith(0)); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Tests that negative numbers throw an IllegalArgumentException. |
| 125 | + */ |
| 126 | + @Test |
| 127 | + void testNegativeNumbersThrowException() { |
| 128 | + assertThrows(IllegalArgumentException.class, () -> KeithNumber.isKeith(-1)); |
| 129 | + assertThrows(IllegalArgumentException.class, () -> KeithNumber.isKeith(-14)); |
| 130 | + assertThrows(IllegalArgumentException.class, () -> KeithNumber.isKeith(-100)); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Tests various edge cases with larger numbers. |
| 135 | + */ |
| 136 | + @Test |
| 137 | + void testLargerNumbers() { |
| 138 | + assertTrue(KeithNumber.isKeith(2208)); |
| 139 | + assertFalse(KeithNumber.isKeith(2207)); |
| 140 | + assertFalse(KeithNumber.isKeith(2209)); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Tests the expected behavior with all two-digit Keith numbers. |
| 145 | + */ |
| 146 | + @Test |
| 147 | + void testAllKnownTwoDigitKeithNumbers() { |
| 148 | + int[] knownKeithNumbers = {14, 19, 28, 47, 61, 75}; |
| 149 | + for (int number : knownKeithNumbers) { |
| 150 | + assertTrue(KeithNumber.isKeith(number), "Expected " + number + " to be a Keith number"); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments