Skip to content

Commit 28a12f8

Browse files
committed
refactor: add parameterized tests for BitSwap
1 parent 9b4dec0 commit 28a12f8

File tree

2 files changed

+95
-9
lines changed

2 files changed

+95
-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
}

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

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,83 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

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));
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.Arguments;
7+
import org.junit.jupiter.params.provider.MethodSource;
8+
9+
import java.util.stream.Stream;
10+
11+
import static org.junit.jupiter.api.Assertions.assertThrows;
12+
13+
class BitSwapTest {
14+
15+
@ParameterizedTest(name = "Additional cases: data={0}, posA={1}, posB={2} -> expected={3}")
16+
@MethodSource("provideAdditionalCases")
17+
void testAdditionalCases(int data, int posA, int posB, int expected) {
18+
assertEquals(expected, BitSwap.bitSwap(data, posA, posB));
19+
}
20+
21+
@ParameterizedTest(name = "Swap different bits: data={0}, posA={1}, posB={2} -> expected={3}")
22+
@MethodSource("provideDifferentBitsCases")
23+
void swapDifferentBits(int data, int posA, int posB, int expected) {
24+
assertEquals(expected, BitSwap.bitSwap(data, posA, posB));
25+
}
26+
27+
@ParameterizedTest(name = "Swap same bits: data={0}, posA={1}, posB={2} should not change")
28+
@MethodSource("provideSameBitsCases")
29+
void swapSameBits(int data, int posA, int posB) {
30+
assertEquals(data, BitSwap.bitSwap(data, posA, posB));
31+
}
32+
33+
@ParameterizedTest(name = "Edge cases: data={0}, posA={1}, posB={2} -> expected={3}")
34+
@MethodSource("provideEdgeCases")
35+
void testEdgeCases(int data, int posA, int posB, int expected) {
36+
assertEquals(expected, BitSwap.bitSwap(data, posA, posB));
37+
}
38+
39+
@ParameterizedTest(name = "Invalid positions: data={0}, posA={1}, posB={2} should throw")
40+
@MethodSource("provideInvalidPositions")
41+
void invalidPositionThrowsException(int data, int posA, int posB) {
42+
assertThrows(IllegalArgumentException.class,
43+
() -> BitSwap.bitSwap(data, posA, posB));
44+
}
45+
46+
static Stream<Arguments> provideAdditionalCases() {
47+
return Stream.of(
48+
Arguments.of(3, 0, 1, 3),
49+
Arguments.of(6, 0, 1, 5),
50+
Arguments.of(7, 1, 1, 7)
51+
);
52+
}
53+
54+
static Stream<Arguments> provideDifferentBitsCases() {
55+
return Stream.of(
56+
Arguments.of(0b01, 0, 1, 0b10)
57+
);
58+
}
59+
60+
static Stream<Arguments> provideSameBitsCases() {
61+
return Stream.of(
62+
Arguments.of(0b111, 0, 2),
63+
Arguments.of(0b0, 1, 3),
64+
Arguments.of(0b1010, 1, 3),
65+
Arguments.of(-1, 5, 5)
66+
);
67+
}
68+
69+
static Stream<Arguments> provideEdgeCases() {
70+
return Stream.of(
71+
Arguments.of(Integer.MIN_VALUE, 31, 0, 1),
72+
Arguments.of(0, 0, 31, 0)
73+
);
74+
}
75+
76+
static Stream<Arguments> provideInvalidPositions() {
77+
return Stream.of(
78+
Arguments.of(0, -1, 0),
79+
Arguments.of(0, 0, 32),
80+
Arguments.of(0, -5, 33),
81+
Arguments.of(0, Integer.MIN_VALUE, Integer.MAX_VALUE)
82+
);
1283
}
1384
}

0 commit comments

Comments
 (0)