|
| 1 | +package com.thealgorithms.bitmanipulation; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +/** |
| 8 | + * Test case for Lowest Set Bit |
| 9 | + * @author Prayas Kumar (https://github.com/prayas7102) |
| 10 | + */ |
| 11 | + |
| 12 | +public class LowestSetBitTest { |
| 13 | + |
| 14 | + @Test |
| 15 | + void testLowestSetBitWithPositiveNumber() { |
| 16 | + // Test with a general positive number |
| 17 | + assertEquals(2, LowestSetBit.isolateLowestSetBit(18)); // 18 in binary: 10010, lowest bit is 2 |
| 18 | + } |
| 19 | + |
| 20 | + @Test |
| 21 | + void testLowestSetBitWithZero() { |
| 22 | + // Test with zero (edge case, no set bits) |
| 23 | + assertEquals(0, LowestSetBit.isolateLowestSetBit(0)); // 0 has no set bits, result should be 0 |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + void testLowestSetBitWithOne() { |
| 28 | + // Test with number 1 (lowest set bit is 1 itself) |
| 29 | + assertEquals(1, LowestSetBit.isolateLowestSetBit(1)); // 1 in binary: 0001, lowest bit is 1 |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + void testLowestSetBitWithPowerOfTwo() { |
| 34 | + // Test with a power of two (only one set bit) |
| 35 | + assertEquals(16, LowestSetBit.isolateLowestSetBit(16)); // 16 in binary: 10000, lowest bit is 16 |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void testLowestSetBitWithAllBitsSet() { |
| 40 | + // Test with a number with multiple set bits (like 7) |
| 41 | + assertEquals(1, LowestSetBit.isolateLowestSetBit(7)); // 7 in binary: 111, lowest bit is 1 |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void testLowestSetBitWithNegativeNumber() { |
| 46 | + // Test with a negative number (-1 in two's complement has all bits set) |
| 47 | + assertEquals(1, LowestSetBit.isolateLowestSetBit(-1)); // -1 in two's complement is all 1s, lowest bit is 1 |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void testLowestSetBitWithLargeNumber() { |
| 52 | + // Test with a large number |
| 53 | + assertEquals(64, LowestSetBit.isolateLowestSetBit(448)); // 448 in binary: 111000000, lowest bit is 64 |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void testClearLowestSetBitFor18() { |
| 58 | + // n = 18 (binary: 10010), expected result = 16 (binary: 10000) |
| 59 | + assertEquals(16, LowestSetBit.clearLowestSetBit(18)); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void testClearLowestSetBitFor10() { |
| 64 | + // n = 10 (binary: 1010), expected result = 8 (binary: 1000) |
| 65 | + assertEquals(8, LowestSetBit.clearLowestSetBit(10)); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void testClearLowestSetBitFor7() { |
| 70 | + // n = 7 (binary: 0111), expected result = 6 (binary: 0110) |
| 71 | + assertEquals(6, LowestSetBit.clearLowestSetBit(7)); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void testClearLowestSetBitFor0() { |
| 76 | + // n = 0 (binary: 0000), no set bits to clear, expected result = 0 |
| 77 | + assertEquals(0, LowestSetBit.clearLowestSetBit(0)); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void testClearLowestSetBitForNegativeNumber() { |
| 82 | + // Test negative number to see how it behaves with two's complement |
| 83 | + // n = -1 (binary: all 1s in two's complement), expected result = -2 (clearing lowest set bit) |
| 84 | + assertEquals(-2, LowestSetBit.clearLowestSetBit(-1)); |
| 85 | + } |
| 86 | +} |
0 commit comments