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