Skip to content

refactor: Enhance docs, add more tests in ArrayCombination #5842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/main/java/com/thealgorithms/bitmanipulation/IsPowerTwo.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
package com.thealgorithms.bitmanipulation;

/**
* Is number power of 2
* Utility class for checking if a number is a power of two.
* A power of two is a number that can be expressed as 2^n where n is a non-negative integer.
* This class provides a method to determine if a given integer is a power of two using bit manipulation.
*
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
*/

public final class IsPowerTwo {
private IsPowerTwo() {
}

/**
* Checks if the given integer is a power of two.
*
* A number is considered a power of two if it is greater than zero and
* has exactly one '1' bit in its binary representation. This method
* uses the property that for any power of two (n), the expression
* (n & (n - 1)) will be zero.
*
* @param number the integer to check
* @return true if the number is a power of two, false otherwise
*/
public static boolean isPowerTwo(int number) {
if (number <= 0) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,49 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

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

public class IsPowerTwoTest {
@Test
public void testIsPowerTwo() {
// test some positive powers of 2
assertTrue(IsPowerTwo.isPowerTwo(1));
assertTrue(IsPowerTwo.isPowerTwo(2));
assertTrue(IsPowerTwo.isPowerTwo(4));
assertTrue(IsPowerTwo.isPowerTwo(16));
assertTrue(IsPowerTwo.isPowerTwo(1024));

// test some negative numbers
assertFalse(IsPowerTwo.isPowerTwo(-1));
assertFalse(IsPowerTwo.isPowerTwo(-2));
assertFalse(IsPowerTwo.isPowerTwo(-4));
@ParameterizedTest
@MethodSource("provideNumbersForPowerTwo")
public void testIsPowerTwo(int number, boolean expected) {
if (expected) {
assertTrue(IsPowerTwo.isPowerTwo(number));
} else {
assertFalse(IsPowerTwo.isPowerTwo(number));
}
}

// test some numbers that are not powers of 2
assertFalse(IsPowerTwo.isPowerTwo(0));
assertFalse(IsPowerTwo.isPowerTwo(3));
assertFalse(IsPowerTwo.isPowerTwo(5));
assertFalse(IsPowerTwo.isPowerTwo(15));
assertFalse(IsPowerTwo.isPowerTwo(1000));
private static Stream<Arguments> provideNumbersForPowerTwo() {
return Stream.of(Arguments.of(1, Boolean.TRUE), // 2^0
Arguments.of(2, Boolean.TRUE), // 2^1
Arguments.of(4, Boolean.TRUE), // 2^2
Arguments.of(8, Boolean.TRUE), // 2^3
Arguments.of(16, Boolean.TRUE), // 2^4
Arguments.of(32, Boolean.TRUE), // 2^5
Arguments.of(64, Boolean.TRUE), // 2^6
Arguments.of(128, Boolean.TRUE), // 2^7
Arguments.of(256, Boolean.TRUE), // 2^8
Arguments.of(1024, Boolean.TRUE), // 2^10
Arguments.of(0, Boolean.FALSE), // 0 is not a power of two
Arguments.of(-1, Boolean.FALSE), // Negative number
Arguments.of(-2, Boolean.FALSE), // Negative number
Arguments.of(-4, Boolean.FALSE), // Negative number
Arguments.of(3, Boolean.FALSE), // 3 is not a power of two
Arguments.of(5, Boolean.FALSE), // 5 is not a power of two
Arguments.of(6, Boolean.FALSE), // 6 is not a power of two
Arguments.of(15, Boolean.FALSE), // 15 is not a power of two
Arguments.of(1000, Boolean.FALSE), // 1000 is not a power of two
Arguments.of(1023, Boolean.FALSE) // 1023 is not a power of two
);
}
}