|
1 |
| -package com.thealgorithms.bitmanipulation; |
| 1 | + package com.thealgorithms.bitmanipulation; |
2 | 2 |
|
3 |
| -import java.util.List; |
4 |
| - |
5 |
| -/** |
6 |
| - * Implements various Boolean algebra gates (AND, OR, NOT, XOR, NAND, NOR) |
7 |
| - * using Java, generalized to handle multiple inputs. |
8 |
| - */ |
9 |
| -public final class BooleanAlgebraGates { |
10 |
| - |
11 |
| - private BooleanAlgebraGates() { |
12 |
| - // Private constructor to prevent instantiation |
13 |
| - } |
| 3 | + import java.util.List; |
14 | 4 |
|
15 | 5 | /**
|
16 |
| - * Interface representing a Boolean gate that takes multiple inputs and returns a result. |
| 6 | + * Implements various Boolean algebra gates (AND, OR, NOT, XOR, NAND, NOR) |
17 | 7 | */
|
18 |
| - interface BooleanGate { |
19 |
| - boolean evaluate(List<Boolean> inputs); |
20 |
| - } |
| 8 | + public final class BooleanAlgebraGates { |
21 | 9 |
|
22 |
| - /** |
23 |
| - * AND Gate implementation. |
24 |
| - * Returns true if all inputs are true, otherwise false. |
25 |
| - */ |
26 |
| - static class ANDGate implements BooleanGate { |
27 |
| - @Override |
28 |
| - public boolean evaluate(List<Boolean> inputs) { |
29 |
| - for (boolean input : inputs) { |
30 |
| - if (!input) { |
31 |
| - return false; |
| 10 | + private BooleanAlgebraGates() { |
| 11 | + // Private constructor to prevent instantiation |
| 12 | + } |
| 13 | + |
| 14 | + /** |
| 15 | + * Interface representing a Boolean gate that takes multiple inputs and returns a result. |
| 16 | + */ |
| 17 | + interface BooleanGate { |
| 18 | + boolean evaluate(List<Boolean> inputs); |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * AND Gate implementation. |
| 23 | + * Returns true if all inputs are true, otherwise false. |
| 24 | + * |
| 25 | + * Test cases for AND Gate: |
| 26 | + * |
| 27 | + * AND([true, true]) should return true. |
| 28 | + * AND([true, false]) should return false. |
| 29 | + * AND([false, false]) should return false. |
| 30 | + * |
| 31 | + * Multiple inputs: |
| 32 | + * AND([true, true, true]) should return true. |
| 33 | + * AND([true, false, true]) should return false. |
| 34 | + * |
| 35 | + * Edge case (empty input list): |
| 36 | + * AND([]) can either return true (since the conjunction of no values |
| 37 | + * is true in some interpretations) or throw an exception depending |
| 38 | + * your design. |
| 39 | + */ |
| 40 | + |
| 41 | + static class ANDGate implements BooleanGate { |
| 42 | + @Override |
| 43 | + public boolean evaluate(List<Boolean> inputs) { |
| 44 | + for (boolean input : inputs) { |
| 45 | + if (!input) { |
| 46 | + return false; |
| 47 | + } |
32 | 48 | }
|
| 49 | + return true; |
33 | 50 | }
|
34 |
| - return true; |
35 | 51 | }
|
36 |
| - } |
37 | 52 |
|
38 |
| - /** |
39 |
| - * OR Gate implementation. |
40 |
| - * Returns true if at least one input is true, otherwise false. |
41 |
| - */ |
42 |
| - static class ORGate implements BooleanGate { |
43 |
| - @Override |
44 |
| - public boolean evaluate(List<Boolean> inputs) { |
45 |
| - for (boolean input : inputs) { |
46 |
| - if (input) { |
47 |
| - return true; |
| 53 | + /** |
| 54 | + * OR Gate implementation. |
| 55 | + * Returns true if at least one input is true, otherwise false. |
| 56 | + * |
| 57 | + * Test cases for OR Gate: |
| 58 | + * |
| 59 | + * OR([true, false]) should return true. |
| 60 | + * OR([false, false]) should return false. |
| 61 | + * |
| 62 | + * Multiple inputs: |
| 63 | + * OR([true, true, false]) should return true. |
| 64 | + * OR([false, false, false]) should return false. |
| 65 | + * |
| 66 | + * Edge case (empty input list): |
| 67 | + * OR([]) can either return false (since the disjunction of no values |
| 68 | + * is false in some interpretations) or throw an exception. |
| 69 | + */ |
| 70 | + static class ORGate implements BooleanGate { |
| 71 | + @Override |
| 72 | + public boolean evaluate(List<Boolean> inputs) { |
| 73 | + for (boolean input : inputs) { |
| 74 | + if (input) { |
| 75 | + return true; |
| 76 | + } |
48 | 77 | }
|
| 78 | + return false; |
49 | 79 | }
|
50 |
| - return false; |
51 | 80 | }
|
52 |
| - } |
53 | 81 |
|
54 |
| - /** |
55 |
| - * NOT Gate implementation (Unary operation). |
56 |
| - * Only accepts a single input and returns the negation. |
57 |
| - */ |
58 |
| - static class NOTGate { |
59 | 82 | /**
|
60 |
| - * Evaluates the negation of a single input. |
61 |
| - * |
62 |
| - * @param input The input value to be negated. |
63 |
| - * @return The negation of the input. |
| 83 | + * NOT Gate implementation (Unary operation). |
| 84 | + * Only accepts a single input and returns the negation. |
| 85 | + * |
| 86 | + * Test cases for NOT Gate: |
| 87 | + * |
| 88 | + * NOT(true) should return false. |
| 89 | + * NOT(false) should return true. |
| 90 | + * |
| 91 | + * Edge case: |
| 92 | + * Not applicable, as NOT is a unary operation and requires a single |
| 93 | + * input. |
64 | 94 | */
|
65 |
| - public boolean evaluate(boolean input) { |
66 |
| - return !input; |
| 95 | + static class NOTGate { |
| 96 | + /** |
| 97 | + * Evaluates the negation of a single input. |
| 98 | + * |
| 99 | + * @param input The input value to be negated. |
| 100 | + * @return The negation of the input. |
| 101 | + */ |
| 102 | + public boolean evaluate(boolean input) { |
| 103 | + return !input; |
| 104 | + } |
67 | 105 | }
|
68 |
| - } |
69 | 106 |
|
70 |
| - /** |
71 |
| - * XOR Gate implementation. |
72 |
| - * Returns true if an odd number of inputs are true, otherwise false. |
73 |
| - */ |
74 |
| - static class XORGate implements BooleanGate { |
75 |
| - @Override |
76 |
| - public boolean evaluate(List<Boolean> inputs) { |
77 |
| - boolean result = false; |
78 |
| - for (boolean input : inputs) { |
79 |
| - result ^= input; |
| 107 | + /** |
| 108 | + * XOR Gate implementation. |
| 109 | + * Returns true if an odd number of inputs are true, otherwise false. |
| 110 | + * |
| 111 | + * Test cases for XOR Gate: |
| 112 | + * |
| 113 | + * XOR([true, false]) should return true. |
| 114 | + * XOR([true, true]) should return false. |
| 115 | + * XOR([false, false]) should return false. |
| 116 | + * |
| 117 | + * Multiple inputs: |
| 118 | + * XOR([true, true, true]) should return true. |
| 119 | + * XOR([true, false, true]) should return false (since the XOR of an |
| 120 | + * odd number of true inputs is true). |
| 121 | + * |
| 122 | + * Edge case: |
| 123 | + * XOR([]) can either return false (if no inputs are given) or throw an |
| 124 | + * exception. |
| 125 | + */ |
| 126 | + static class XORGate implements BooleanGate { |
| 127 | + @Override |
| 128 | + public boolean evaluate(List<Boolean> inputs) { |
| 129 | + boolean result = false; |
| 130 | + for (boolean input : inputs) { |
| 131 | + result ^= input; |
| 132 | + } |
| 133 | + return result; |
80 | 134 | }
|
81 |
| - return result; |
82 | 135 | }
|
83 |
| - } |
84 | 136 |
|
85 |
| - /** |
86 |
| - * NAND Gate implementation. |
87 |
| - * Returns true if at least one input is false, otherwise false. |
88 |
| - */ |
89 |
| - static class NANDGate implements BooleanGate { |
90 |
| - @Override |
91 |
| - public boolean evaluate(List<Boolean> inputs) { |
92 |
| - return !new ANDGate().evaluate(inputs); // Equivalent to negation of AND |
| 137 | + /** |
| 138 | + * NAND Gate implementation. |
| 139 | + * Returns true if at least one input is false, otherwise false. |
| 140 | + * |
| 141 | + * Test cases for NAND Gate: |
| 142 | + * |
| 143 | + * NAND([true, true]) should return false. |
| 144 | + * NAND([true, false]) should return true. |
| 145 | + * NAND([false, false]) should return true. |
| 146 | + * |
| 147 | + * Multiple inputs: |
| 148 | + * NAND([true, true, true]) should return false. |
| 149 | + * NAND([true, true, false]) should return true. |
| 150 | + * |
| 151 | + * Edge case: |
| 152 | + * NAND([]) can either return true or throw an exception depending on |
| 153 | + * your design. |
| 154 | + */ |
| 155 | + static class NANDGate implements BooleanGate { |
| 156 | + @Override |
| 157 | + public boolean evaluate(List<Boolean> inputs) { |
| 158 | + return !new ANDGate().evaluate(inputs); // Equivalent to negation of AND |
| 159 | + } |
93 | 160 | }
|
94 |
| - } |
95 | 161 |
|
96 |
| - /** |
97 |
| - * NOR Gate implementation. |
98 |
| - * Returns true if all inputs are false, otherwise false. |
99 |
| - */ |
100 |
| - static class NORGate implements BooleanGate { |
101 |
| - @Override |
102 |
| - public boolean evaluate(List<Boolean> inputs) { |
103 |
| - return !new ORGate().evaluate(inputs); // Equivalent to negation of OR |
| 162 | + /** |
| 163 | + * NOR Gate implementation. |
| 164 | + * Returns true if all inputs are false, otherwise false. |
| 165 | + * |
| 166 | + * Test cases for NOR Gate: |
| 167 | + * |
| 168 | + * NOR([true, false]) should return false. |
| 169 | + * NOR([false, false]) should return true. |
| 170 | + * |
| 171 | + * Multiple inputs: |
| 172 | + * NOR([false, false, false]) should return true. |
| 173 | + * NOR([true, true, false]) should return false. |
| 174 | + * |
| 175 | + * Edge case: |
| 176 | + * NOR([]) can either return true or throw an exception. |
| 177 | + */ |
| 178 | + static class NORGate implements BooleanGate { |
| 179 | + @Override |
| 180 | + public boolean evaluate(List<Boolean> inputs) { |
| 181 | + return !new ORGate().evaluate(inputs); // Equivalent to negation of OR |
| 182 | + } |
104 | 183 | }
|
| 184 | + /** |
| 185 | + * Edge Cases and Special Scenarios: |
| 186 | + * |
| 187 | + * 1. Empty input list: |
| 188 | + * - Test handling of empty input lists for multi-input gates. |
| 189 | + * - Should throw an exception if the design assumes an empty list is * invalid. |
| 190 | + * |
| 191 | + * 2. Single input for multi-input gates: |
| 192 | + * - AND([true]) -> true |
| 193 | + * - OR([false]) -> false |
| 194 | + * - XOR([true]) -> true |
| 195 | + * - Test behavior with single input as a corner case. |
| 196 | + * |
| 197 | + * 3. Mixed inputs: |
| 198 | + * - AND([true, false, false, true]) -> false |
| 199 | + * - Similar tests for OR, XOR, and other gates. |
| 200 | + * |
| 201 | + * 4. Large input lists: |
| 202 | + * - AND([true, true, ..., true]) with 1,000 true values -> true. |
| 203 | + * - OR with mostly false and one true -> true. |
| 204 | + * |
| 205 | + * 5. Randomized tests: |
| 206 | + * - Generate random true/false input lists. |
| 207 | + * - Validate expected gate outputs, especially for XOR. |
| 208 | + * |
| 209 | + * 6. Invalid input handling: |
| 210 | + * - Test behavior when null is passed as input or within the list. |
| 211 | + * - Should either throw an exception or handle per defined behavior. |
| 212 | + */ |
| 213 | + |
105 | 214 | }
|
106 |
| -} |
|
0 commit comments