Skip to content

Commit 009c2b3

Browse files
authored
Enhance docs, add more tests in ArrayCombination (#5842)
1 parent 788f4d8 commit 009c2b3

File tree

2 files changed

+51
-21
lines changed

2 files changed

+51
-21
lines changed

src/main/java/com/thealgorithms/bitmanipulation/IsPowerTwo.java

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
package com.thealgorithms.bitmanipulation;
22

33
/**
4-
* Is number power of 2
4+
* Utility class for checking if a number is a power of two.
5+
* A power of two is a number that can be expressed as 2^n where n is a non-negative integer.
6+
* This class provides a method to determine if a given integer is a power of two using bit manipulation.
7+
*
58
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
69
*/
7-
810
public final class IsPowerTwo {
911
private IsPowerTwo() {
1012
}
13+
14+
/**
15+
* Checks if the given integer is a power of two.
16+
*
17+
* A number is considered a power of two if it is greater than zero and
18+
* has exactly one '1' bit in its binary representation. This method
19+
* uses the property that for any power of two (n), the expression
20+
* (n & (n - 1)) will be zero.
21+
*
22+
* @param number the integer to check
23+
* @return true if the number is a power of two, false otherwise
24+
*/
1125
public static boolean isPowerTwo(int number) {
1226
if (number <= 0) {
1327
return false;

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

+35-19
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,49 @@
33
import static org.junit.jupiter.api.Assertions.assertFalse;
44
import static org.junit.jupiter.api.Assertions.assertTrue;
55

6-
import org.junit.jupiter.api.Test;
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
710

811
/**
912
* Test case for IsPowerTwo class
1013
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
1114
*/
1215

1316
public class IsPowerTwoTest {
14-
@Test
15-
public void testIsPowerTwo() {
16-
// test some positive powers of 2
17-
assertTrue(IsPowerTwo.isPowerTwo(1));
18-
assertTrue(IsPowerTwo.isPowerTwo(2));
19-
assertTrue(IsPowerTwo.isPowerTwo(4));
20-
assertTrue(IsPowerTwo.isPowerTwo(16));
21-
assertTrue(IsPowerTwo.isPowerTwo(1024));
2217

23-
// test some negative numbers
24-
assertFalse(IsPowerTwo.isPowerTwo(-1));
25-
assertFalse(IsPowerTwo.isPowerTwo(-2));
26-
assertFalse(IsPowerTwo.isPowerTwo(-4));
18+
@ParameterizedTest
19+
@MethodSource("provideNumbersForPowerTwo")
20+
public void testIsPowerTwo(int number, boolean expected) {
21+
if (expected) {
22+
assertTrue(IsPowerTwo.isPowerTwo(number));
23+
} else {
24+
assertFalse(IsPowerTwo.isPowerTwo(number));
25+
}
26+
}
2727

28-
// test some numbers that are not powers of 2
29-
assertFalse(IsPowerTwo.isPowerTwo(0));
30-
assertFalse(IsPowerTwo.isPowerTwo(3));
31-
assertFalse(IsPowerTwo.isPowerTwo(5));
32-
assertFalse(IsPowerTwo.isPowerTwo(15));
33-
assertFalse(IsPowerTwo.isPowerTwo(1000));
28+
private static Stream<Arguments> provideNumbersForPowerTwo() {
29+
return Stream.of(Arguments.of(1, Boolean.TRUE), // 2^0
30+
Arguments.of(2, Boolean.TRUE), // 2^1
31+
Arguments.of(4, Boolean.TRUE), // 2^2
32+
Arguments.of(8, Boolean.TRUE), // 2^3
33+
Arguments.of(16, Boolean.TRUE), // 2^4
34+
Arguments.of(32, Boolean.TRUE), // 2^5
35+
Arguments.of(64, Boolean.TRUE), // 2^6
36+
Arguments.of(128, Boolean.TRUE), // 2^7
37+
Arguments.of(256, Boolean.TRUE), // 2^8
38+
Arguments.of(1024, Boolean.TRUE), // 2^10
39+
Arguments.of(0, Boolean.FALSE), // 0 is not a power of two
40+
Arguments.of(-1, Boolean.FALSE), // Negative number
41+
Arguments.of(-2, Boolean.FALSE), // Negative number
42+
Arguments.of(-4, Boolean.FALSE), // Negative number
43+
Arguments.of(3, Boolean.FALSE), // 3 is not a power of two
44+
Arguments.of(5, Boolean.FALSE), // 5 is not a power of two
45+
Arguments.of(6, Boolean.FALSE), // 6 is not a power of two
46+
Arguments.of(15, Boolean.FALSE), // 15 is not a power of two
47+
Arguments.of(1000, Boolean.FALSE), // 1000 is not a power of two
48+
Arguments.of(1023, Boolean.FALSE) // 1023 is not a power of two
49+
);
3450
}
3551
}

0 commit comments

Comments
 (0)