|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
4 | 4 |
|
5 |
| -import org.junit.jupiter.api.Test; |
| 5 | +import java.util.stream.Stream; |
| 6 | +import org.junit.jupiter.params.ParameterizedTest; |
| 7 | +import org.junit.jupiter.params.provider.Arguments; |
| 8 | +import org.junit.jupiter.params.provider.MethodSource; |
6 | 9 |
|
7 |
| -public class SingleBitOperationsTest { |
| 10 | +class SingleBitOperationsTest { |
8 | 11 |
|
9 |
| - @Test |
10 |
| - public void flipBitTest() { |
11 |
| - assertEquals(1, SingleBitOperations.flipBit(3, 1)); |
12 |
| - assertEquals(11, SingleBitOperations.flipBit(3, 3)); |
| 12 | + @ParameterizedTest |
| 13 | + @MethodSource("provideFlipBitTestCases") |
| 14 | + void testFlipBit(int input, int bit, int expected) { |
| 15 | + assertEquals(expected, SingleBitOperations.flipBit(input, bit)); |
13 | 16 | }
|
14 | 17 |
|
15 |
| - @Test |
16 |
| - public void setBitTest() { |
17 |
| - assertEquals(5, SingleBitOperations.setBit(4, 0)); |
18 |
| - assertEquals(4, SingleBitOperations.setBit(4, 2)); |
19 |
| - assertEquals(5, SingleBitOperations.setBit(5, 0)); |
20 |
| - assertEquals(14, SingleBitOperations.setBit(10, 2)); |
21 |
| - assertEquals(15, SingleBitOperations.setBit(15, 3)); |
22 |
| - assertEquals(2, SingleBitOperations.setBit(0, 1)); |
| 18 | + private static Stream<Arguments> provideFlipBitTestCases() { |
| 19 | + return Stream.of(Arguments.of(3, 1, 1), // Binary: 11 -> 01 |
| 20 | + Arguments.of(3, 3, 11) // Binary: 11 -> 1011 |
| 21 | + ); |
23 | 22 | }
|
24 | 23 |
|
25 |
| - @Test |
26 |
| - public void clearBitTest() { |
27 |
| - assertEquals(5, SingleBitOperations.clearBit(7, 1)); |
28 |
| - assertEquals(5, SingleBitOperations.clearBit(5, 1)); |
| 24 | + @ParameterizedTest |
| 25 | + @MethodSource("provideSetBitTestCases") |
| 26 | + void testSetBit(int input, int bit, int expected) { |
| 27 | + assertEquals(expected, SingleBitOperations.setBit(input, bit)); |
29 | 28 | }
|
30 | 29 |
|
31 |
| - @Test |
32 |
| - public void getBitTest() { |
33 |
| - assertEquals(0, SingleBitOperations.getBit(6, 0)); |
34 |
| - assertEquals(1, SingleBitOperations.getBit(7, 1)); |
| 30 | + private static Stream<Arguments> provideSetBitTestCases() { |
| 31 | + return Stream.of(Arguments.of(4, 0, 5), // 100 -> 101 |
| 32 | + Arguments.of(4, 2, 4), // 100 -> 100 (bit already set) |
| 33 | + Arguments.of(0, 1, 2), // 00 -> 10 |
| 34 | + Arguments.of(10, 2, 14) // 1010 -> 1110 |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + @ParameterizedTest |
| 39 | + @MethodSource("provideClearBitTestCases") |
| 40 | + void testClearBit(int input, int bit, int expected) { |
| 41 | + assertEquals(expected, SingleBitOperations.clearBit(input, bit)); |
| 42 | + } |
| 43 | + |
| 44 | + private static Stream<Arguments> provideClearBitTestCases() { |
| 45 | + return Stream.of(Arguments.of(7, 1, 5), // 111 -> 101 |
| 46 | + Arguments.of(5, 1, 5) // 101 -> 101 (bit already cleared) |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + @ParameterizedTest |
| 51 | + @MethodSource("provideGetBitTestCases") |
| 52 | + void testGetBit(int input, int bit, int expected) { |
| 53 | + assertEquals(expected, SingleBitOperations.getBit(input, bit)); |
| 54 | + } |
| 55 | + |
| 56 | + private static Stream<Arguments> provideGetBitTestCases() { |
| 57 | + return Stream.of(Arguments.of(6, 0, 0), // 110 -> Bit 0: 0 |
| 58 | + Arguments.of(7, 1, 1) // 111 -> Bit 1: 1 |
| 59 | + ); |
35 | 60 | }
|
36 | 61 | }
|
0 commit comments