Skip to content

Commit aa2daea

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

File tree

1 file changed

+119
-76
lines changed

1 file changed

+119
-76
lines changed

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

Lines changed: 119 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,44 @@
44

55
/**
66
* 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.
79
*/
810
public final class BooleanAlgebraGates {
911

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

1415
/**
1516
* Interface representing a Boolean gate that takes multiple inputs and returns a result.
1617
*/
1718
interface BooleanGate {
19+
/**
20+
* Evaluates the Boolean gate with the provided inputs.
21+
*
22+
* @param inputs a list of Boolean values representing the gate inputs.
23+
* @return the result of the Boolean operation.
24+
*/
1825
boolean evaluate(List<Boolean> inputs);
1926
}
2027

2128
/**
2229
* AND Gate implementation.
23-
* Returns true if all inputs are true; otherwise, false.
30+
* Returns true if all inputs are true; otherwise, returns false.
2431
*
25-
* Test cases for AND Gate:
26-
* - AND([true, true]) should return true.
27-
* - AND([true, false]) should return false.
28-
* - AND([false, false]) should return false.
29-
* - AND([true, true, true]) should return true.
30-
* - AND([true, false, true]) should return false.
31-
*
32-
* Edge case (empty input list):
33-
* - AND([]) can either return true or throw an exception depending on your design.
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>
3445
*/
3546
static class ANDGate implements BooleanGate {
3647
@Override
@@ -46,16 +57,20 @@ public boolean evaluate(List<Boolean> inputs) {
4657

4758
/**
4859
* OR Gate implementation.
49-
* Returns true if at least one input is true; otherwise, false.
60+
* Returns true if at least one input is true; otherwise, returns false.
5061
*
51-
* Test cases for OR Gate:
52-
* - OR([true, false]) should return true.
53-
* - OR([false, false]) should return false.
54-
* - OR([true, true, false]) should return true.
55-
* - OR([false, false, false]) should return false.
56-
*
57-
* Edge case (empty input list):
58-
* - OR([]) can either return false or throw an exception.
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>
5974
*/
6075
static class ORGate implements BooleanGate {
6176
@Override
@@ -73,12 +88,16 @@ public boolean evaluate(List<Boolean> inputs) {
7388
* NOT Gate implementation (Unary operation).
7489
* Only accepts a single input and returns the negation.
7590
*
76-
* Test cases for NOT Gate:
77-
* - NOT(true) should return false.
78-
* - NOT(false) should return true.
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>
7996
*
80-
* Edge case:
81-
* Not applicable, as NOT is a unary operation and requires a single input.
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>
82101
*/
83102
static class NOTGate {
84103
/**
@@ -94,17 +113,21 @@ public boolean evaluate(boolean input) {
94113

95114
/**
96115
* XOR Gate implementation.
97-
* Returns true if an odd number of inputs are true; otherwise, false.
116+
* Returns true if an odd number of inputs are true; otherwise, returns false.
98117
*
99-
* Test cases for XOR Gate:
100-
* - XOR([true, false]) should return true.
101-
* - XOR([true, true]) should return false.
102-
* - XOR([false, false]) should return false.
103-
* - XOR([true, true, true]) should return true.
104-
* - XOR([true, false, true]) should return false.
105-
*
106-
* Edge case:
107-
* - XOR([]) can either return false or throw an exception.
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>
108131
*/
109132
static class XORGate implements BooleanGate {
110133
@Override
@@ -119,17 +142,21 @@ public boolean evaluate(List<Boolean> inputs) {
119142

120143
/**
121144
* NAND Gate implementation.
122-
* Returns true if at least one input is false; otherwise, false.
145+
* Returns true if at least one input is false; otherwise, returns false.
123146
*
124-
* Test cases for NAND Gate:
125-
* - NAND([true, true]) should return false.
126-
* - NAND([true, false]) should return true.
127-
* - NAND([false, false]) should return true.
128-
* - NAND([true, true, true]) should return false.
129-
* - NAND([true, true, false]) should return true.
130-
*
131-
* Edge case:
132-
* - NAND([]) can either return true or throw an exception.
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>
133160
*/
134161
static class NANDGate implements BooleanGate {
135162
@Override
@@ -140,16 +167,20 @@ public boolean evaluate(List<Boolean> inputs) {
140167

141168
/**
142169
* NOR Gate implementation.
143-
* Returns true if all inputs are false; otherwise, false.
170+
* Returns true if all inputs are false; otherwise, returns false.
144171
*
145-
* Test cases for NOR Gate:
146-
* - NOR([true, false]) should return false.
147-
* - NOR([false, false]) should return true.
148-
* - NOR([false, false, false]) should return true.
149-
* - NOR([true, true, false]) should return false.
150-
*
151-
* Edge case:
152-
* - NOR([]) can either return true or throw an exception.
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>
153184
*/
154185
static class NORGate implements BooleanGate {
155186
@Override
@@ -161,30 +192,42 @@ public boolean evaluate(List<Boolean> inputs) {
161192
/**
162193
* Edge Cases and Special Scenarios:
163194
*
164-
* 1. Empty input list:
165-
* - Test handling of empty input lists for multi-input gates.
166-
* - Should throw an exception if the design assumes an empty list is invalid.
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>
167200
*
168-
* 2. Single input for multi-input gates:
169-
* - AND([true]) -> true
170-
* - OR([false]) -> false
171-
* - XOR([true]) -> true
172-
* - Test behavior with single input as a corner case.
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>
173208
*
174-
* 3. Mixed inputs:
175-
* - AND([true, false, false, true]) -> false
176-
* - Similar tests for OR, XOR, and other gates.
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>
177214
*
178-
* 4. Large input lists:
179-
* - AND([true, true, ..., true]) with 1,000 true values -> true.
180-
* - OR with mostly false and one true -> true.
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>
181220
*
182-
* 5. Randomized tests:
183-
* - Generate random true/false input lists.
184-
* - Validate expected gate outputs, especially for XOR.
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>
185226
*
186-
* 6. Invalid input handling:
187-
* - Test behavior when null is passed as input or within the list.
188-
* - Should either throw an exception or handle per defined behavior.
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>
189232
*/
190233
}

0 commit comments

Comments
 (0)