Files
Java/src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java
2024-10-29 18:17:30 +00:00

42 lines
1.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.thealgorithms.bitmanipulation;
import static org.junit.jupiter.api.Assertions.assertEquals;
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;
class ReverseBitsTest {
@ParameterizedTest
@MethodSource("provideTestCases")
void testReverseBits(int input, int expected) {
assertEquals(expected, ReverseBits.reverseBits(input));
}
private static Stream<Arguments> provideTestCases() {
return Stream.of(
// Edge case: All bits are 0
Arguments.of(0, 0),
// Edge case: All bits are 1 (Twos complement representation of -1)
Arguments.of(-1, -1),
// Case with random number 43261596
Arguments.of(43261596, 964176192),
// Case with maximum positive value for 32-bit integer
Arguments.of(Integer.MAX_VALUE, -2),
// Case with minimum value (all bits 1 except the sign bit)
Arguments.of(Integer.MIN_VALUE, 1),
// Case with a single bit set (2^0 = 1)
Arguments.of(1, Integer.MIN_VALUE),
// Case with alternating bits: 0b101010...10 (in binary)
Arguments.of(0xAAAAAAAA, 0x55555555));
}
}