Skip to content

Commit 350f149

Browse files
authored
Add parameterized tests for BitSwap (#6347)
1 parent 9b4dec0 commit 350f149

File tree

2 files changed

+73
-9
lines changed

2 files changed

+73
-9
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
package com.thealgorithms.bitmanipulation;
22

3+
/**
4+
* Utility class for performing bit-swapping operations on integers.
5+
* This class cannot be instantiated.
6+
*/
37
public final class BitSwap {
48
private BitSwap() {
59
}
6-
/*
7-
* @brief Swaps the bits at the position posA and posB from data
10+
11+
/**
12+
* Swaps two bits at specified positions in an integer.
13+
*
14+
* @param data The input integer whose bits need to be swapped
15+
* @param posA The position of the first bit (0-based, from least significant)
16+
* @param posB The position of the second bit (0-based, from least significant)
17+
* @return The modified value with swapped bits
18+
* @throws IllegalArgumentException if either position is negative or ≥ 32
819
*/
920
public static int bitSwap(int data, final int posA, final int posB) {
21+
if (posA < 0 || posA >= Integer.SIZE || posB < 0 || posB >= Integer.SIZE) {
22+
throw new IllegalArgumentException("Bit positions must be between 0 and 31");
23+
}
24+
1025
if (SingleBitOperations.getBit(data, posA) != SingleBitOperations.getBit(data, posB)) {
1126
data ^= (1 << posA) ^ (1 << posB);
1227
}
Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,62 @@
11
package com.thealgorithms.bitmanipulation;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

5-
import org.junit.jupiter.api.Test;
6-
public class BitSwapTest {
7-
@Test
8-
void testHighestSetBit() {
9-
assertEquals(3, BitSwap.bitSwap(3, 0, 1));
10-
assertEquals(5, BitSwap.bitSwap(6, 0, 1));
11-
assertEquals(7, BitSwap.bitSwap(7, 1, 1));
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;
10+
11+
class BitSwapTest {
12+
13+
@ParameterizedTest(name = "Additional cases: data={0}, posA={1}, posB={2} -> expected={3}")
14+
@MethodSource("provideAdditionalCases")
15+
void testAdditionalCases(int data, int posA, int posB, int expected) {
16+
assertEquals(expected, BitSwap.bitSwap(data, posA, posB));
17+
}
18+
19+
@ParameterizedTest(name = "Swap different bits: data={0}, posA={1}, posB={2} -> expected={3}")
20+
@MethodSource("provideDifferentBitsCases")
21+
void swapDifferentBits(int data, int posA, int posB, int expected) {
22+
assertEquals(expected, BitSwap.bitSwap(data, posA, posB));
23+
}
24+
25+
@ParameterizedTest(name = "Swap same bits: data={0}, posA={1}, posB={2} should not change")
26+
@MethodSource("provideSameBitsCases")
27+
void swapSameBits(int data, int posA, int posB) {
28+
assertEquals(data, BitSwap.bitSwap(data, posA, posB));
29+
}
30+
31+
@ParameterizedTest(name = "Edge cases: data={0}, posA={1}, posB={2} -> expected={3}")
32+
@MethodSource("provideEdgeCases")
33+
void testEdgeCases(int data, int posA, int posB, int expected) {
34+
assertEquals(expected, BitSwap.bitSwap(data, posA, posB));
35+
}
36+
37+
@ParameterizedTest(name = "Invalid positions: data={0}, posA={1}, posB={2} should throw")
38+
@MethodSource("provideInvalidPositions")
39+
void invalidPositionThrowsException(int data, int posA, int posB) {
40+
assertThrows(IllegalArgumentException.class, () -> BitSwap.bitSwap(data, posA, posB));
41+
}
42+
43+
static Stream<Arguments> provideAdditionalCases() {
44+
return Stream.of(Arguments.of(3, 0, 1, 3), Arguments.of(6, 0, 1, 5), Arguments.of(7, 1, 1, 7));
45+
}
46+
47+
static Stream<Arguments> provideDifferentBitsCases() {
48+
return Stream.of(Arguments.of(0b01, 0, 1, 0b10));
49+
}
50+
51+
static Stream<Arguments> provideSameBitsCases() {
52+
return Stream.of(Arguments.of(0b111, 0, 2), Arguments.of(0b0, 1, 3), Arguments.of(0b1010, 1, 3), Arguments.of(-1, 5, 5));
53+
}
54+
55+
static Stream<Arguments> provideEdgeCases() {
56+
return Stream.of(Arguments.of(Integer.MIN_VALUE, 31, 0, 1), Arguments.of(0, 0, 31, 0));
57+
}
58+
59+
static Stream<Arguments> provideInvalidPositions() {
60+
return Stream.of(Arguments.of(0, -1, 0), Arguments.of(0, 0, 32), Arguments.of(0, -5, 33), Arguments.of(0, Integer.MIN_VALUE, Integer.MAX_VALUE));
1261
}
1362
}

0 commit comments

Comments
 (0)