mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
Enhance docs, add more tests in SingleBitOperations (#5860)
This commit is contained in:
@@ -2,35 +2,60 @@ package com.thealgorithms.bitmanipulation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
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;
|
||||
|
||||
public class SingleBitOperationsTest {
|
||||
class SingleBitOperationsTest {
|
||||
|
||||
@Test
|
||||
public void flipBitTest() {
|
||||
assertEquals(1, SingleBitOperations.flipBit(3, 1));
|
||||
assertEquals(11, SingleBitOperations.flipBit(3, 3));
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideFlipBitTestCases")
|
||||
void testFlipBit(int input, int bit, int expected) {
|
||||
assertEquals(expected, SingleBitOperations.flipBit(input, bit));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setBitTest() {
|
||||
assertEquals(5, SingleBitOperations.setBit(4, 0));
|
||||
assertEquals(4, SingleBitOperations.setBit(4, 2));
|
||||
assertEquals(5, SingleBitOperations.setBit(5, 0));
|
||||
assertEquals(14, SingleBitOperations.setBit(10, 2));
|
||||
assertEquals(15, SingleBitOperations.setBit(15, 3));
|
||||
assertEquals(2, SingleBitOperations.setBit(0, 1));
|
||||
private static Stream<Arguments> provideFlipBitTestCases() {
|
||||
return Stream.of(Arguments.of(3, 1, 1), // Binary: 11 -> 01
|
||||
Arguments.of(3, 3, 11) // Binary: 11 -> 1011
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clearBitTest() {
|
||||
assertEquals(5, SingleBitOperations.clearBit(7, 1));
|
||||
assertEquals(5, SingleBitOperations.clearBit(5, 1));
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideSetBitTestCases")
|
||||
void testSetBit(int input, int bit, int expected) {
|
||||
assertEquals(expected, SingleBitOperations.setBit(input, bit));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getBitTest() {
|
||||
assertEquals(0, SingleBitOperations.getBit(6, 0));
|
||||
assertEquals(1, SingleBitOperations.getBit(7, 1));
|
||||
private static Stream<Arguments> provideSetBitTestCases() {
|
||||
return Stream.of(Arguments.of(4, 0, 5), // 100 -> 101
|
||||
Arguments.of(4, 2, 4), // 100 -> 100 (bit already set)
|
||||
Arguments.of(0, 1, 2), // 00 -> 10
|
||||
Arguments.of(10, 2, 14) // 1010 -> 1110
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideClearBitTestCases")
|
||||
void testClearBit(int input, int bit, int expected) {
|
||||
assertEquals(expected, SingleBitOperations.clearBit(input, bit));
|
||||
}
|
||||
|
||||
private static Stream<Arguments> provideClearBitTestCases() {
|
||||
return Stream.of(Arguments.of(7, 1, 5), // 111 -> 101
|
||||
Arguments.of(5, 1, 5) // 101 -> 101 (bit already cleared)
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideGetBitTestCases")
|
||||
void testGetBit(int input, int bit, int expected) {
|
||||
assertEquals(expected, SingleBitOperations.getBit(input, bit));
|
||||
}
|
||||
|
||||
private static Stream<Arguments> provideGetBitTestCases() {
|
||||
return Stream.of(Arguments.of(6, 0, 0), // 110 -> Bit 0: 0
|
||||
Arguments.of(7, 1, 1) // 111 -> Bit 1: 1
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user