Skip to content

Commit b74a362

Browse files
added test cases
1 parent 9708632 commit b74a362

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,43 @@
11
package com.thealgorithms.bitmanipulation;
2-
import static org.junit.jupiter.api.Assertions.*;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
34
import org.junit.jupiter.api.Test;
4-
import com.thealgorithms.bitmanipulation.LowestSetBit;
55

66
public class LowestSetBitTest {
77

88
LowestSetBit lowestSetBit = new LowestSetBit();
99

1010
@Test
1111
public void testIsolateLowestSetBit() {
12-
// Test with positive number
12+
// Test with regular positive numbers
1313
assertEquals(1, lowestSetBit.isolateLowestSetBit(5)); // 5 in binary is 101, the lowest set bit is 1
1414
assertEquals(2, lowestSetBit.isolateLowestSetBit(6)); // 6 in binary is 110, the lowest set bit is 2
1515
assertEquals(4, lowestSetBit.isolateLowestSetBit(12)); // 12 in binary is 1100, the lowest set bit is 4
1616

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
1823
assertEquals(1, lowestSetBit.isolateLowestSetBit(-5)); // -5 in binary (two's complement) still has 1 as the lowest set bit
1924
assertEquals(2, lowestSetBit.isolateLowestSetBit(-6)); // -6 in binary has 2 as the lowest set bit
2025

2126
// Test with zero
2227
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
2331
}
2432

2533
@Test
2634
public void testClearLowestSetBit() {
27-
// Test with positive number
35+
// Test with positive numbers
2836
assertEquals(4, lowestSetBit.clearLowestSetBit(5)); // 5 in binary is 101, clearing lowest set bit gives 100 (which is 4)
2937
assertEquals(4, lowestSetBit.clearLowestSetBit(6)); // 6 in binary is 110, clearing lowest set bit gives 100 (which is 4)
3038
assertEquals(8, lowestSetBit.clearLowestSetBit(12)); // 12 in binary is 1100, clearing lowest set bit gives 1000 (which is 8)
3139

32-
// Test with negative number
40+
// Test with negative numbers
3341
assertEquals(-6, lowestSetBit.clearLowestSetBit(-5)); // -5 in binary, clearing lowest set bit should give -6
3442
assertEquals(-8, lowestSetBit.clearLowestSetBit(-6)); // -6 in binary, clearing lowest set bit should give -8
3543

0 commit comments

Comments
 (0)