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