|
| 1 | +package com.thealgorithms.bitmanipulation; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.ANDGate; |
| 6 | +import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.BooleanGate; |
| 7 | +import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.NANDGate; |
| 8 | +import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.NORGate; |
| 9 | +import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.NOTGate; |
| 10 | +import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.ORGate; |
| 11 | +import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.XORGate; |
| 12 | +import java.util.Arrays; |
| 13 | +import java.util.Collections; |
| 14 | +import java.util.List; |
| 15 | +import java.util.stream.Stream; |
| 16 | +import org.junit.jupiter.params.ParameterizedTest; |
| 17 | +import org.junit.jupiter.params.provider.CsvSource; |
| 18 | +import org.junit.jupiter.params.provider.MethodSource; |
| 19 | + |
| 20 | +class BooleanAlgebraGatesTest { |
| 21 | + |
| 22 | + @ParameterizedTest(name = "ANDGate Test Case {index}: inputs={0} -> expected={1}") |
| 23 | + @MethodSource("provideAndGateTestCases") |
| 24 | + void testANDGate(List<Boolean> inputs, boolean expected) { |
| 25 | + BooleanGate gate = new ANDGate(); |
| 26 | + assertEquals(expected, gate.evaluate(inputs)); |
| 27 | + } |
| 28 | + |
| 29 | + @ParameterizedTest(name = "ORGate Test Case {index}: inputs={0} -> expected={1}") |
| 30 | + @MethodSource("provideOrGateTestCases") |
| 31 | + void testORGate(List<Boolean> inputs, boolean expected) { |
| 32 | + BooleanGate gate = new ORGate(); |
| 33 | + assertEquals(expected, gate.evaluate(inputs)); |
| 34 | + } |
| 35 | + |
| 36 | + @ParameterizedTest(name = "NOTGate Test Case {index}: input={0} -> expected={1}") |
| 37 | + @CsvSource({"true, false", "false, true"}) |
| 38 | + void testNOTGate(boolean input, boolean expected) { |
| 39 | + NOTGate gate = new NOTGate(); |
| 40 | + assertEquals(expected, gate.evaluate(input)); |
| 41 | + } |
| 42 | + |
| 43 | + @ParameterizedTest(name = "XORGate Test Case {index}: inputs={0} -> expected={1}") |
| 44 | + @MethodSource("provideXorGateTestCases") |
| 45 | + void testXORGate(List<Boolean> inputs, boolean expected) { |
| 46 | + BooleanGate gate = new XORGate(); |
| 47 | + assertEquals(expected, gate.evaluate(inputs)); |
| 48 | + } |
| 49 | + |
| 50 | + @ParameterizedTest(name = "NANDGate Test Case {index}: inputs={0} -> expected={1}") |
| 51 | + @MethodSource("provideNandGateTestCases") |
| 52 | + void testNANDGate(List<Boolean> inputs, boolean expected) { |
| 53 | + BooleanGate gate = new NANDGate(); |
| 54 | + assertEquals(expected, gate.evaluate(inputs)); |
| 55 | + } |
| 56 | + |
| 57 | + @ParameterizedTest(name = "NORGate Test Case {index}: inputs={0} -> expected={1}") |
| 58 | + @MethodSource("provideNorGateTestCases") |
| 59 | + void testNORGate(List<Boolean> inputs, boolean expected) { |
| 60 | + BooleanGate gate = new NORGate(); |
| 61 | + assertEquals(expected, gate.evaluate(inputs)); |
| 62 | + } |
| 63 | + |
| 64 | + // Helper methods to provide test data for each gate |
| 65 | + |
| 66 | + static Stream<Object[]> provideAndGateTestCases() { |
| 67 | + return Stream.of(new Object[] {Arrays.asList(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE), Boolean.TRUE}, new Object[] {Arrays.asList(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE), Boolean.FALSE}, new Object[] {Arrays.asList(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE), Boolean.FALSE}, |
| 68 | + new Object[] {Collections.emptyList(), Boolean.TRUE} // AND over no inputs is true |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + static Stream<Object[]> provideOrGateTestCases() { |
| 73 | + return Stream.of(new Object[] {Arrays.asList(Boolean.TRUE, Boolean.FALSE, Boolean.FALSE), Boolean.TRUE}, new Object[] {Arrays.asList(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE), Boolean.FALSE}, new Object[] {Arrays.asList(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE), Boolean.TRUE}, |
| 74 | + new Object[] {Collections.emptyList(), Boolean.FALSE} // OR over no inputs is false |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + static Stream<Object[]> provideXorGateTestCases() { |
| 79 | + return Stream.of(new Object[] {Arrays.asList(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE), Boolean.FALSE}, // XOR over odd true |
| 80 | + new Object[] {Arrays.asList(Boolean.TRUE, Boolean.FALSE, Boolean.FALSE), Boolean.TRUE}, // XOR over single true |
| 81 | + new Object[] {Arrays.asList(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE), Boolean.FALSE}, // XOR over all false |
| 82 | + new Object[] {Arrays.asList(Boolean.TRUE, Boolean.TRUE), Boolean.FALSE} // XOR over even true |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + static Stream<Object[]> provideNandGateTestCases() { |
| 87 | + return Stream.of(new Object[] {Arrays.asList(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE), Boolean.FALSE}, // NAND of all true is false |
| 88 | + new Object[] {Arrays.asList(Boolean.TRUE, Boolean.FALSE), Boolean.TRUE}, // NAND with one false is true |
| 89 | + new Object[] {Arrays.asList(Boolean.FALSE, Boolean.FALSE), Boolean.TRUE}, // NAND of all false is true |
| 90 | + new Object[] {Collections.emptyList(), Boolean.FALSE} // NAND over no inputs is false (negation of AND) |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + static Stream<Object[]> provideNorGateTestCases() { |
| 95 | + return Stream.of(new Object[] {Arrays.asList(Boolean.FALSE, Boolean.FALSE), Boolean.TRUE}, // NOR of all false is true |
| 96 | + new Object[] {Arrays.asList(Boolean.FALSE, Boolean.TRUE), Boolean.FALSE}, // NOR with one true is false |
| 97 | + new Object[] {Arrays.asList(Boolean.TRUE, Boolean.TRUE), Boolean.FALSE}, // NOR of all true is false |
| 98 | + new Object[] {Collections.emptyList(), Boolean.TRUE} // NOR over no inputs is true (negation of OR) |
| 99 | + ); |
| 100 | + } |
| 101 | +} |
0 commit comments