|
| 1 | +package com.thealgorithms.bitmanipulation; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 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; |
| 9 | + |
| 10 | +public final class FindNthBitTest { |
| 11 | + |
| 12 | + /** |
| 13 | + * A parameterized test that checks the value of the Nth bit for different inputs. |
| 14 | + * |
| 15 | + * @param num the number whose Nth bit is being tested |
| 16 | + * @param n the bit position |
| 17 | + * @param expected the expected value of the Nth bit (0 or 1) |
| 18 | + */ |
| 19 | + @ParameterizedTest |
| 20 | + @MethodSource("provideTestCases") |
| 21 | + void findNthBitParameterizedTest(int num, int n, int expected) { |
| 22 | + assertEquals(expected, FindNthBit.findNthBit(num, n)); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Provides the test cases as a stream of arguments for the parameterized test. |
| 27 | + * |
| 28 | + * @return a stream of test cases where each case consists of a number, the bit position, |
| 29 | + * and the expected result. |
| 30 | + */ |
| 31 | + private static Stream<Arguments> provideTestCases() { |
| 32 | + return Stream.of(Arguments.of(13, 2, 0), // binary: 1101, 2nd bit is 0 |
| 33 | + Arguments.of(13, 3, 1), // binary: 1101, 3rd bit is 1 |
| 34 | + Arguments.of(4, 2, 0), // binary: 100, 2nd bit is 0 |
| 35 | + Arguments.of(4, 3, 1), // binary: 100, 3rd bit is 1 |
| 36 | + Arguments.of(1, 1, 1) // binary: 1, 1st bit is 1 |
| 37 | + ); |
| 38 | + } |
| 39 | +} |
0 commit comments