|
1 | 1 | package com.thealgorithms.maths; |
2 | 2 |
|
3 | | -import static org.junit.jupiter.api.Assertions.assertFalse; |
4 | | -import static org.junit.jupiter.api.Assertions.assertTrue; |
5 | | - |
| 3 | +import org.junit.jupiter.api.Assertions; |
6 | 4 | import org.junit.jupiter.api.Test; |
7 | 5 |
|
8 | | -public class HarshadNumberTest { |
| 6 | +/** |
| 7 | + * Test class for {@link HarshadNumber}. |
| 8 | + * Tests various scenarios including positive cases, edge cases, and exception |
| 9 | + * handling. |
| 10 | + */ |
| 11 | +class HarshadNumberTest { |
| 12 | + |
| 13 | + @Test |
| 14 | + void testValidHarshadNumbers() { |
| 15 | + // Single digit Harshad numbers (all single digits except 0 are Harshad numbers) |
| 16 | + Assertions.assertTrue(HarshadNumber.isHarshad(1)); |
| 17 | + Assertions.assertTrue(HarshadNumber.isHarshad(2)); |
| 18 | + Assertions.assertTrue(HarshadNumber.isHarshad(3)); |
| 19 | + Assertions.assertTrue(HarshadNumber.isHarshad(4)); |
| 20 | + Assertions.assertTrue(HarshadNumber.isHarshad(5)); |
| 21 | + Assertions.assertTrue(HarshadNumber.isHarshad(6)); |
| 22 | + Assertions.assertTrue(HarshadNumber.isHarshad(7)); |
| 23 | + Assertions.assertTrue(HarshadNumber.isHarshad(8)); |
| 24 | + Assertions.assertTrue(HarshadNumber.isHarshad(9)); |
| 25 | + |
| 26 | + // Two digit Harshad numbers |
| 27 | + Assertions.assertTrue(HarshadNumber.isHarshad(10)); // 10 / (1 + 0) = 10 |
| 28 | + Assertions.assertTrue(HarshadNumber.isHarshad(12)); // 12 / (1 + 2) = 4 |
| 29 | + Assertions.assertTrue(HarshadNumber.isHarshad(18)); // 18 / (1 + 8) = 2 |
| 30 | + Assertions.assertTrue(HarshadNumber.isHarshad(20)); // 20 / (2 + 0) = 10 |
| 31 | + Assertions.assertTrue(HarshadNumber.isHarshad(21)); // 21 / (2 + 1) = 7 |
| 32 | + |
| 33 | + // Three digit Harshad numbers |
| 34 | + Assertions.assertTrue(HarshadNumber.isHarshad(100)); // 100 / (1 + 0 + 0) = 100 |
| 35 | + Assertions.assertTrue(HarshadNumber.isHarshad(102)); // 102 / (1 + 0 + 2) = 34 |
| 36 | + Assertions.assertTrue(HarshadNumber.isHarshad(108)); // 108 / (1 + 0 + 8) = 12 |
| 37 | + |
| 38 | + // Large Harshad numbers |
| 39 | + Assertions.assertTrue(HarshadNumber.isHarshad(1000)); // 1000 / (1 + 0 + 0 + 0) = 1000 |
| 40 | + Assertions.assertTrue(HarshadNumber.isHarshad(1002)); // 1002 / (1 + 0 + 0 + 2) = 334 |
| 41 | + Assertions.assertTrue(HarshadNumber.isHarshad(999999999)); // 999999999 / (9*9) = 12345679 |
| 42 | + } |
9 | 43 |
|
10 | 44 | @Test |
11 | | - public void harshadNumber() { |
| 45 | + void testInvalidHarshadNumbers() { |
| 46 | + // Numbers that are not Harshad numbers |
| 47 | + Assertions.assertFalse(HarshadNumber.isHarshad(11)); // 11 / (1 + 1) = 5.5 |
| 48 | + Assertions.assertFalse(HarshadNumber.isHarshad(13)); // 13 / (1 + 3) = 3.25 |
| 49 | + Assertions.assertFalse(HarshadNumber.isHarshad(17)); // 17 / (1 + 7) = 2.125 |
| 50 | + Assertions.assertFalse(HarshadNumber.isHarshad(19)); // 19 / (1 + 9) = 1.9 |
| 51 | + Assertions.assertFalse(HarshadNumber.isHarshad(23)); // 23 / (2 + 3) = 4.6 |
| 52 | + Assertions.assertFalse(HarshadNumber.isHarshad(101)); // 101 / (1 + 0 + 1) = 50.5 |
| 53 | + } |
12 | 54 |
|
13 | | - assertTrue(HarshadNumber.isHarshad(18)); |
14 | | - assertFalse(HarshadNumber.isHarshad(-18)); |
15 | | - assertFalse(HarshadNumber.isHarshad(19)); |
16 | | - assertTrue(HarshadNumber.isHarshad(999999999)); |
17 | | - assertFalse(HarshadNumber.isHarshad(0)); |
| 55 | + @Test |
| 56 | + void testZeroThrowsException() { |
| 57 | + Assertions.assertThrows(IllegalArgumentException.class, () -> HarshadNumber.isHarshad(0)); |
| 58 | + } |
18 | 59 |
|
19 | | - assertTrue(HarshadNumber.isHarshad("18")); |
20 | | - assertFalse(HarshadNumber.isHarshad("-18")); |
21 | | - assertFalse(HarshadNumber.isHarshad("19")); |
22 | | - assertTrue(HarshadNumber.isHarshad("999999999")); |
23 | | - assertTrue(HarshadNumber.isHarshad("99999999999100")); |
| 60 | + @Test |
| 61 | + void testNegativeNumbersThrowException() { |
| 62 | + Assertions.assertThrows(IllegalArgumentException.class, () -> HarshadNumber.isHarshad(-1)); |
| 63 | + Assertions.assertThrows(IllegalArgumentException.class, () -> HarshadNumber.isHarshad(-18)); |
| 64 | + Assertions.assertThrows(IllegalArgumentException.class, () -> HarshadNumber.isHarshad(-100)); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void testValidHarshadNumbersWithString() { |
| 69 | + // Single digit Harshad numbers |
| 70 | + Assertions.assertTrue(HarshadNumber.isHarshad("1")); |
| 71 | + Assertions.assertTrue(HarshadNumber.isHarshad("2")); |
| 72 | + Assertions.assertTrue(HarshadNumber.isHarshad("9")); |
| 73 | + |
| 74 | + // Two digit Harshad numbers |
| 75 | + Assertions.assertTrue(HarshadNumber.isHarshad("10")); |
| 76 | + Assertions.assertTrue(HarshadNumber.isHarshad("12")); |
| 77 | + Assertions.assertTrue(HarshadNumber.isHarshad("18")); |
| 78 | + |
| 79 | + // Large Harshad numbers |
| 80 | + Assertions.assertTrue(HarshadNumber.isHarshad("1000")); |
| 81 | + Assertions.assertTrue(HarshadNumber.isHarshad("999999999")); |
| 82 | + Assertions.assertTrue(HarshadNumber.isHarshad("99999999999100")); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void testInvalidHarshadNumbersWithString() { |
| 87 | + // Numbers that are not Harshad numbers |
| 88 | + Assertions.assertFalse(HarshadNumber.isHarshad("11")); |
| 89 | + Assertions.assertFalse(HarshadNumber.isHarshad("13")); |
| 90 | + Assertions.assertFalse(HarshadNumber.isHarshad("19")); |
| 91 | + Assertions.assertFalse(HarshadNumber.isHarshad("23")); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void testStringWithZeroThrowsException() { |
| 96 | + Assertions.assertThrows(IllegalArgumentException.class, () -> HarshadNumber.isHarshad("0")); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void testStringWithNegativeNumbersThrowsException() { |
| 101 | + Assertions.assertThrows(IllegalArgumentException.class, () -> HarshadNumber.isHarshad("-1")); |
| 102 | + Assertions.assertThrows(IllegalArgumentException.class, () -> HarshadNumber.isHarshad("-18")); |
| 103 | + Assertions.assertThrows(IllegalArgumentException.class, () -> HarshadNumber.isHarshad("-100")); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void testNullStringThrowsException() { |
| 108 | + Assertions.assertThrows(IllegalArgumentException.class, () -> HarshadNumber.isHarshad(null)); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void testEmptyStringThrowsException() { |
| 113 | + Assertions.assertThrows(IllegalArgumentException.class, () -> HarshadNumber.isHarshad("")); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + void testInvalidStringThrowsException() { |
| 118 | + Assertions.assertThrows(IllegalArgumentException.class, () -> HarshadNumber.isHarshad("abc")); |
| 119 | + Assertions.assertThrows(IllegalArgumentException.class, () -> HarshadNumber.isHarshad("12.5")); |
| 120 | + Assertions.assertThrows(IllegalArgumentException.class, () -> HarshadNumber.isHarshad("12a")); |
| 121 | + Assertions.assertThrows(IllegalArgumentException.class, () -> HarshadNumber.isHarshad(" 12 ")); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + void testMaxLongValue() { |
| 126 | + // Test with a large number close to Long.MAX_VALUE |
| 127 | + long largeHarshadCandidate = 9223372036854775800L; |
| 128 | + // This specific number may or may not be Harshad, just testing it doesn't crash |
| 129 | + try { |
| 130 | + HarshadNumber.isHarshad(largeHarshadCandidate); |
| 131 | + } catch (Exception e) { |
| 132 | + Assertions.fail("Should not throw exception for valid large numbers"); |
| 133 | + } |
24 | 134 | } |
25 | 135 | } |
0 commit comments