Skip to content

Commit 4a03f42

Browse files
authored
Add algo for BooleanGateslogic (#5717)
1 parent 1617ed1 commit 4a03f42

File tree

2 files changed

+212
-0
lines changed

2 files changed

+212
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
import java.util.List;
4+
5+
/**
6+
* Implements various Boolean algebra gates (AND, OR, NOT, XOR, NAND, NOR)
7+
*/
8+
public final class BooleanAlgebraGates {
9+
10+
private BooleanAlgebraGates() {
11+
// Prevent instantiation
12+
}
13+
14+
/**
15+
* Represents a Boolean gate that takes multiple inputs and returns a result.
16+
*/
17+
interface BooleanGate {
18+
/**
19+
* Evaluates the gate with the given inputs.
20+
*
21+
* @param inputs The input values for the gate.
22+
* @return The result of the evaluation.
23+
*/
24+
boolean evaluate(List<Boolean> inputs);
25+
}
26+
27+
/**
28+
* AND Gate implementation.
29+
* Returns true if all inputs are true; otherwise, false.
30+
*/
31+
static class ANDGate implements BooleanGate {
32+
@Override
33+
public boolean evaluate(List<Boolean> inputs) {
34+
for (boolean input : inputs) {
35+
if (!input) {
36+
return false;
37+
}
38+
}
39+
return true;
40+
}
41+
}
42+
43+
/**
44+
* OR Gate implementation.
45+
* Returns true if at least one input is true; otherwise, false.
46+
*/
47+
static class ORGate implements BooleanGate {
48+
@Override
49+
public boolean evaluate(List<Boolean> inputs) {
50+
for (boolean input : inputs) {
51+
if (input) {
52+
return true;
53+
}
54+
}
55+
return false;
56+
}
57+
}
58+
59+
/**
60+
* NOT Gate implementation (Unary operation).
61+
* Negates a single input value.
62+
*/
63+
static class NOTGate {
64+
/**
65+
* Evaluates the negation of the input.
66+
*
67+
* @param input The input value to be negated.
68+
* @return The negated value.
69+
*/
70+
public boolean evaluate(boolean input) {
71+
return !input;
72+
}
73+
}
74+
75+
/**
76+
* XOR Gate implementation.
77+
* Returns true if an odd number of inputs are true; otherwise, false.
78+
*/
79+
static class XORGate implements BooleanGate {
80+
@Override
81+
public boolean evaluate(List<Boolean> inputs) {
82+
boolean result = false;
83+
for (boolean input : inputs) {
84+
result ^= input;
85+
}
86+
return result;
87+
}
88+
}
89+
90+
/**
91+
* NAND Gate implementation.
92+
* Returns true if at least one input is false; otherwise, false.
93+
*/
94+
static class NANDGate implements BooleanGate {
95+
@Override
96+
public boolean evaluate(List<Boolean> inputs) {
97+
return !new ANDGate().evaluate(inputs); // Equivalent to negation of AND
98+
}
99+
}
100+
101+
/**
102+
* NOR Gate implementation.
103+
* Returns true if all inputs are false; otherwise, false.
104+
*/
105+
static class NORGate implements BooleanGate {
106+
@Override
107+
public boolean evaluate(List<Boolean> inputs) {
108+
return !new ORGate().evaluate(inputs); // Equivalent to negation of OR
109+
}
110+
}
111+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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

Comments
 (0)