Skip to content

Commit a250a95

Browse files
committed
Add algo for BooleanGateslogic and test cases
1 parent aa2daea commit a250a95

File tree

2 files changed

+129
-138
lines changed

2 files changed

+129
-138
lines changed

src/main/java/com/thealgorithms/bitmanipulation/BooleanAlgebraGates.java

Lines changed: 16 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,30 @@
33
import java.util.List;
44

55
/**
6-
* Implements various Boolean algebra gates (AND, OR, NOT, XOR, NAND, NOR).
7-
* This class provides the logic for fundamental Boolean operations that can be
8-
* used in various computational contexts.
6+
* Implements various Boolean algebra gates (AND, OR, NOT, XOR, NAND, NOR)
97
*/
108
public final class BooleanAlgebraGates {
119

12-
// Private constructor to prevent instantiation
13-
private BooleanAlgebraGates() {}
10+
private BooleanAlgebraGates() {
11+
// Prevent instantiation
12+
}
1413

1514
/**
16-
* Interface representing a Boolean gate that takes multiple inputs and returns a result.
15+
* Represents a Boolean gate that takes multiple inputs and returns a result.
1716
*/
1817
interface BooleanGate {
1918
/**
20-
* Evaluates the Boolean gate with the provided inputs.
19+
* Evaluates the gate with the given inputs.
2120
*
22-
* @param inputs a list of Boolean values representing the gate inputs.
23-
* @return the result of the Boolean operation.
21+
* @param inputs The input values for the gate.
22+
* @return The result of the evaluation.
2423
*/
2524
boolean evaluate(List<Boolean> inputs);
2625
}
2726

2827
/**
2928
* AND Gate implementation.
30-
* Returns true if all inputs are true; otherwise, returns false.
31-
*
32-
* <p>Test cases for AND Gate:</p>
33-
* <ul>
34-
* <li>AND([true, true]) should return true.</li>
35-
* <li>AND([true, false]) should return false.</li>
36-
* <li>AND([false, false]) should return false.</li>
37-
* <li>AND([true, true, true]) should return true.</li>
38-
* <li>AND([true, false, true]) should return false.</li>
39-
* </ul>
40-
*
41-
* <p>Edge case (empty input list):</p>
42-
* <ul>
43-
* <li>AND([]) can either return true or throw an exception depending on your design.</li>
44-
* </ul>
29+
* Returns true if all inputs are true; otherwise, false.
4530
*/
4631
static class ANDGate implements BooleanGate {
4732
@Override
@@ -57,20 +42,7 @@ public boolean evaluate(List<Boolean> inputs) {
5742

5843
/**
5944
* OR Gate implementation.
60-
* Returns true if at least one input is true; otherwise, returns false.
61-
*
62-
* <p>Test cases for OR Gate:</p>
63-
* <ul>
64-
* <li>OR([true, false]) should return true.</li>
65-
* <li>OR([false, false]) should return false.</li>
66-
* <li>OR([true, true, false]) should return true.</li>
67-
* <li>OR([false, false, false]) should return false.</li>
68-
* </ul>
69-
*
70-
* <p>Edge case (empty input list):</p>
71-
* <ul>
72-
* <li>OR([]) can either return false or throw an exception.</li>
73-
* </ul>
45+
* Returns true if at least one input is true; otherwise, false.
7446
*/
7547
static class ORGate implements BooleanGate {
7648
@Override
@@ -86,25 +58,14 @@ public boolean evaluate(List<Boolean> inputs) {
8658

8759
/**
8860
* NOT Gate implementation (Unary operation).
89-
* Only accepts a single input and returns the negation.
90-
*
91-
* <p>Test cases for NOT Gate:</p>
92-
* <ul>
93-
* <li>NOT(true) should return false.</li>
94-
* <li>NOT(false) should return true.</li>
95-
* </ul>
96-
*
97-
* <p>Edge case:</p>
98-
* <ul>
99-
* <li>Not applicable, as NOT is a unary operation and requires a single input.</li>
100-
* </ul>
61+
* Negates a single input value.
10162
*/
10263
static class NOTGate {
10364
/**
104-
* Evaluates the negation of a single input.
65+
* Evaluates the negation of the input.
10566
*
10667
* @param input The input value to be negated.
107-
* @return The negation of the input.
68+
* @return The negated value.
10869
*/
10970
public boolean evaluate(boolean input) {
11071
return !input;
@@ -113,21 +74,7 @@ public boolean evaluate(boolean input) {
11374

11475
/**
11576
* XOR Gate implementation.
116-
* Returns true if an odd number of inputs are true; otherwise, returns false.
117-
*
118-
* <p>Test cases for XOR Gate:</p>
119-
* <ul>
120-
* <li>XOR([true, false]) should return true.</li>
121-
* <li>XOR([true, true]) should return false.</li>
122-
* <li>XOR([false, false]) should return false.</li>
123-
* <li>XOR([true, true, true]) should return true.</li>
124-
* <li>XOR([true, false, true]) should return false.</li>
125-
* </ul>
126-
*
127-
* <p>Edge case:</p>
128-
* <ul>
129-
* <li>XOR([]) can either return false or throw an exception.</li>
130-
* </ul>
77+
* Returns true if an odd number of inputs are true; otherwise, false.
13178
*/
13279
static class XORGate implements BooleanGate {
13380
@Override
@@ -142,21 +89,7 @@ public boolean evaluate(List<Boolean> inputs) {
14289

14390
/**
14491
* NAND Gate implementation.
145-
* Returns true if at least one input is false; otherwise, returns false.
146-
*
147-
* <p>Test cases for NAND Gate:</p>
148-
* <ul>
149-
* <li>NAND([true, true]) should return false.</li>
150-
* <li>NAND([true, false]) should return true.</li>
151-
* <li>NAND([false, false]) should return true.</li>
152-
* <li>NAND([true, true, true]) should return false.</li>
153-
* <li>NAND([true, true, false]) should return true.</li>
154-
* </ul>
155-
*
156-
* <p>Edge case:</p>
157-
* <ul>
158-
* <li>NAND([]) can either return true or throw an exception.</li>
159-
* </ul>
92+
* Returns true if at least one input is false; otherwise, false.
16093
*/
16194
static class NANDGate implements BooleanGate {
16295
@Override
@@ -167,67 +100,12 @@ public boolean evaluate(List<Boolean> inputs) {
167100

168101
/**
169102
* NOR Gate implementation.
170-
* Returns true if all inputs are false; otherwise, returns false.
171-
*
172-
* <p>Test cases for NOR Gate:</p>
173-
* <ul>
174-
* <li>NOR([true, false]) should return false.</li>
175-
* <li>NOR([false, false]) should return true.</li>
176-
* <li>NOR([false, false, false]) should return true.</li>
177-
* <li>NOR([true, true, false]) should return false.</li>
178-
* </ul>
179-
*
180-
* <p>Edge case:</p>
181-
* <ul>
182-
* <li>NOR([]) can either return true or throw an exception.</li>
183-
* </ul>
103+
* Returns true if all inputs are false; otherwise, false.
184104
*/
185105
static class NORGate implements BooleanGate {
186106
@Override
187107
public boolean evaluate(List<Boolean> inputs) {
188108
return !new ORGate().evaluate(inputs); // Equivalent to negation of OR
189109
}
190110
}
191-
192-
/**
193-
* Edge Cases and Special Scenarios:
194-
*
195-
* <p>1. Empty input list:</p>
196-
* <ul>
197-
* <li>Test handling of empty input lists for multi-input gates.</li>
198-
* <li>Should throw an exception if the design assumes an empty list is invalid.</li>
199-
* </ul>
200-
*
201-
* <p>2. Single input for multi-input gates:</p>
202-
* <ul>
203-
* <li>AND([true]) -> true</li>
204-
* <li>OR([false]) -> false</li>
205-
* <li>XOR([true]) -> true</li>
206-
* <li>Test behavior with single input as a corner case.</li>
207-
* </ul>
208-
*
209-
* <p>3. Mixed inputs:</p>
210-
* <ul>
211-
* <li>AND([true, false, false, true]) -> false</li>
212-
* <li>Similar tests for OR, XOR, and other gates.</li>
213-
* </ul>
214-
*
215-
* <p>4. Large input lists:</p>
216-
* <ul>
217-
* <li>AND([true, true, ..., true]) with 1,000 true values -> true.</li>
218-
* <li>OR with mostly false and one true -> true.</li>
219-
* </ul>
220-
*
221-
* <p>5. Randomized tests:</p>
222-
* <ul>
223-
* <li>Generate random true/false input lists.</li>
224-
* <li>Validate expected gate outputs, especially for XOR.</li>
225-
* </ul>
226-
*
227-
* <p>6. Invalid input handling:</p>
228-
* <ul>
229-
* <li>Test behavior when null is passed as input or within the list.</li>
230-
* <li>Should either throw an exception or handle per defined behavior.</li>
231-
* </ul>
232-
*/
233111
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.Arrays;
6+
import java.util.Collections;
7+
import java.util.List;
8+
import java.util.stream.Stream;
9+
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.ANDGate;
10+
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.BooleanGate;
11+
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.NANDGate;
12+
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.NORGate;
13+
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.NOTGate;
14+
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.ORGate;
15+
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.XORGate;
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({
38+
"true, false",
39+
"false, true"
40+
})
41+
void testNOTGate(boolean input, boolean expected) {
42+
NOTGate gate = new NOTGate();
43+
assertEquals(expected, gate.evaluate(input));
44+
}
45+
46+
@ParameterizedTest(name = "XORGate Test Case {index}: inputs={0} -> expected={1}")
47+
@MethodSource("provideXorGateTestCases")
48+
void testXORGate(List<Boolean> inputs, boolean expected) {
49+
BooleanGate gate = new XORGate();
50+
assertEquals(expected, gate.evaluate(inputs));
51+
}
52+
53+
@ParameterizedTest(name = "NANDGate Test Case {index}: inputs={0} -> expected={1}")
54+
@MethodSource("provideNandGateTestCases")
55+
void testNANDGate(List<Boolean> inputs, boolean expected) {
56+
BooleanGate gate = new NANDGate();
57+
assertEquals(expected, gate.evaluate(inputs));
58+
}
59+
60+
@ParameterizedTest(name = "NORGate Test Case {index}: inputs={0} -> expected={1}")
61+
@MethodSource("provideNorGateTestCases")
62+
void testNORGate(List<Boolean> inputs, boolean expected) {
63+
BooleanGate gate = new NORGate();
64+
assertEquals(expected, gate.evaluate(inputs));
65+
}
66+
67+
// Helper methods to provide test data for each gate
68+
69+
static Stream<Object[]> provideAndGateTestCases() {
70+
return Stream.of(
71+
new Object[]{Arrays.asList(true, true, true), true},
72+
new Object[]{Arrays.asList(true, false, true), false},
73+
new Object[]{Arrays.asList(false, false, false), false},
74+
new Object[]{Collections.emptyList(), true} // AND over no inputs is true
75+
);
76+
}
77+
78+
static Stream<Object[]> provideOrGateTestCases() {
79+
return Stream.of(
80+
new Object[]{Arrays.asList(true, false, false), true},
81+
new Object[]{Arrays.asList(false, false, false), false},
82+
new Object[]{Arrays.asList(true, true, true), true},
83+
new Object[]{Collections.emptyList(), false} // OR over no inputs is false
84+
);
85+
}
86+
87+
static Stream<Object[]> provideXorGateTestCases() {
88+
return Stream.of(
89+
new Object[]{Arrays.asList(true, false, true), false}, // XOR over odd true
90+
new Object[]{Arrays.asList(true, false, false), true}, // XOR over single true
91+
new Object[]{Arrays.asList(false, false, false), false},// XOR over all false
92+
new Object[]{Arrays.asList(true, true), false} // XOR over even true
93+
);
94+
}
95+
96+
static Stream<Object[]> provideNandGateTestCases() {
97+
return Stream.of(
98+
new Object[]{Arrays.asList(true, true, true), false}, // NAND of all true is false
99+
new Object[]{Arrays.asList(true, false), true}, // NAND with one false is true
100+
new Object[]{Arrays.asList(false, false), true}, // NAND of all false is true
101+
new Object[]{Collections.emptyList(), false} // NAND over no inputs is false (negation of AND)
102+
);
103+
}
104+
105+
static Stream<Object[]> provideNorGateTestCases() {
106+
return Stream.of(
107+
new Object[]{Arrays.asList(false, false), true}, // NOR of all false is true
108+
new Object[]{Arrays.asList(false, true), false}, // NOR with one true is false
109+
new Object[]{Arrays.asList(true, true), false}, // NOR of all true is false
110+
new Object[]{Collections.emptyList(), true} // NOR over no inputs is true (negation of OR)
111+
);
112+
}
113+
}

0 commit comments

Comments
 (0)