|
1 | 1 | package com.thealgorithms.bitmanipulation;
|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
4 | 5 |
|
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)); |
12 | 61 | }
|
13 | 62 | }
|
0 commit comments