|
| 1 | +package com.thealgorithms.bitmanipulation; |
| 2 | +import static org.junit.jupiter.api.Assertions.*; |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import com.thealgorithms.bitmanipulation.LowestSetBit; |
| 5 | + |
| 6 | +public class LowestSetBitTest { |
| 7 | + |
| 8 | + LowestSetBit lowestSetBit = new LowestSetBit(); |
| 9 | + |
| 10 | + @Test |
| 11 | + public void testIsolateLowestSetBit() { |
| 12 | + // Test with positive number |
| 13 | + assertEquals(1, lowestSetBit.isolateLowestSetBit(5)); // 5 in binary is 101, the lowest set bit is 1 |
| 14 | + assertEquals(2, lowestSetBit.isolateLowestSetBit(6)); // 6 in binary is 110, the lowest set bit is 2 |
| 15 | + assertEquals(4, lowestSetBit.isolateLowestSetBit(12)); // 12 in binary is 1100, the lowest set bit is 4 |
| 16 | + |
| 17 | + // Test with negative number |
| 18 | + assertEquals(1, lowestSetBit.isolateLowestSetBit(-5)); // -5 in binary (two's complement) still has 1 as the lowest set bit |
| 19 | + assertEquals(2, lowestSetBit.isolateLowestSetBit(-6)); // -6 in binary has 2 as the lowest set bit |
| 20 | + |
| 21 | + // Test with zero |
| 22 | + assertEquals(0, lowestSetBit.isolateLowestSetBit(0)); // Edge case: 0 should return 0 |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + public void testClearLowestSetBit() { |
| 27 | + // Test with positive number |
| 28 | + assertEquals(4, lowestSetBit.clearLowestSetBit(5)); // 5 in binary is 101, clearing lowest set bit gives 100 (which is 4) |
| 29 | + assertEquals(4, lowestSetBit.clearLowestSetBit(6)); // 6 in binary is 110, clearing lowest set bit gives 100 (which is 4) |
| 30 | + assertEquals(8, lowestSetBit.clearLowestSetBit(12)); // 12 in binary is 1100, clearing lowest set bit gives 1000 (which is 8) |
| 31 | + |
| 32 | + // Test with negative number |
| 33 | + assertEquals(-6, lowestSetBit.clearLowestSetBit(-5)); // -5 in binary, clearing lowest set bit should give -6 |
| 34 | + assertEquals(-8, lowestSetBit.clearLowestSetBit(-6)); // -6 in binary, clearing lowest set bit should give -8 |
| 35 | + |
| 36 | + // Test with zero |
| 37 | + assertEquals(0, lowestSetBit.clearLowestSetBit(0)); // Edge case: 0 should return 0 |
| 38 | + } |
| 39 | +} |
0 commit comments