- Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathBooleanAlgebraGatesTest.java
101 lines (86 loc) · 5.2 KB
/
BooleanAlgebraGatesTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
packagecom.thealgorithms.bitmanipulation;
importstaticorg.junit.jupiter.api.Assertions.assertEquals;
importcom.thealgorithms.bitmanipulation.BooleanAlgebraGates.ANDGate;
importcom.thealgorithms.bitmanipulation.BooleanAlgebraGates.BooleanGate;
importcom.thealgorithms.bitmanipulation.BooleanAlgebraGates.NANDGate;
importcom.thealgorithms.bitmanipulation.BooleanAlgebraGates.NORGate;
importcom.thealgorithms.bitmanipulation.BooleanAlgebraGates.NOTGate;
importcom.thealgorithms.bitmanipulation.BooleanAlgebraGates.ORGate;
importcom.thealgorithms.bitmanipulation.BooleanAlgebraGates.XORGate;
importjava.util.Arrays;
importjava.util.Collections;
importjava.util.List;
importjava.util.stream.Stream;
importorg.junit.jupiter.params.ParameterizedTest;
importorg.junit.jupiter.params.provider.CsvSource;
importorg.junit.jupiter.params.provider.MethodSource;
classBooleanAlgebraGatesTest {
@ParameterizedTest(name = "ANDGate Test Case {index}: inputs={0} -> expected={1}")
@MethodSource("provideAndGateTestCases")
voidtestANDGate(List<Boolean> inputs, booleanexpected) {
BooleanGategate = newANDGate();
assertEquals(expected, gate.evaluate(inputs));
}
@ParameterizedTest(name = "ORGate Test Case {index}: inputs={0} -> expected={1}")
@MethodSource("provideOrGateTestCases")
voidtestORGate(List<Boolean> inputs, booleanexpected) {
BooleanGategate = newORGate();
assertEquals(expected, gate.evaluate(inputs));
}
@ParameterizedTest(name = "NOTGate Test Case {index}: input={0} -> expected={1}")
@CsvSource({"true, false", "false, true"})
voidtestNOTGate(booleaninput, booleanexpected) {
NOTGategate = newNOTGate();
assertEquals(expected, gate.evaluate(input));
}
@ParameterizedTest(name = "XORGate Test Case {index}: inputs={0} -> expected={1}")
@MethodSource("provideXorGateTestCases")
voidtestXORGate(List<Boolean> inputs, booleanexpected) {
BooleanGategate = newXORGate();
assertEquals(expected, gate.evaluate(inputs));
}
@ParameterizedTest(name = "NANDGate Test Case {index}: inputs={0} -> expected={1}")
@MethodSource("provideNandGateTestCases")
voidtestNANDGate(List<Boolean> inputs, booleanexpected) {
BooleanGategate = newNANDGate();
assertEquals(expected, gate.evaluate(inputs));
}
@ParameterizedTest(name = "NORGate Test Case {index}: inputs={0} -> expected={1}")
@MethodSource("provideNorGateTestCases")
voidtestNORGate(List<Boolean> inputs, booleanexpected) {
BooleanGategate = newNORGate();
assertEquals(expected, gate.evaluate(inputs));
}
// Helper methods to provide test data for each gate
staticStream<Object[]> provideAndGateTestCases() {
returnStream.of(newObject[] {Arrays.asList(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE), Boolean.TRUE}, newObject[] {Arrays.asList(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE), Boolean.FALSE}, newObject[] {Arrays.asList(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE), Boolean.FALSE},
newObject[] {Collections.emptyList(), Boolean.TRUE} // AND over no inputs is true
);
}
staticStream<Object[]> provideOrGateTestCases() {
returnStream.of(newObject[] {Arrays.asList(Boolean.TRUE, Boolean.FALSE, Boolean.FALSE), Boolean.TRUE}, newObject[] {Arrays.asList(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE), Boolean.FALSE}, newObject[] {Arrays.asList(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE), Boolean.TRUE},
newObject[] {Collections.emptyList(), Boolean.FALSE} // OR over no inputs is false
);
}
staticStream<Object[]> provideXorGateTestCases() {
returnStream.of(newObject[] {Arrays.asList(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE), Boolean.FALSE}, // XOR over odd true
newObject[] {Arrays.asList(Boolean.TRUE, Boolean.FALSE, Boolean.FALSE), Boolean.TRUE}, // XOR over single true
newObject[] {Arrays.asList(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE), Boolean.FALSE}, // XOR over all false
newObject[] {Arrays.asList(Boolean.TRUE, Boolean.TRUE), Boolean.FALSE} // XOR over even true
);
}
staticStream<Object[]> provideNandGateTestCases() {
returnStream.of(newObject[] {Arrays.asList(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE), Boolean.FALSE}, // NAND of all true is false
newObject[] {Arrays.asList(Boolean.TRUE, Boolean.FALSE), Boolean.TRUE}, // NAND with one false is true
newObject[] {Arrays.asList(Boolean.FALSE, Boolean.FALSE), Boolean.TRUE}, // NAND of all false is true
newObject[] {Collections.emptyList(), Boolean.FALSE} // NAND over no inputs is false (negation of AND)
);
}
staticStream<Object[]> provideNorGateTestCases() {
returnStream.of(newObject[] {Arrays.asList(Boolean.FALSE, Boolean.FALSE), Boolean.TRUE}, // NOR of all false is true
newObject[] {Arrays.asList(Boolean.FALSE, Boolean.TRUE), Boolean.FALSE}, // NOR with one true is false
newObject[] {Arrays.asList(Boolean.TRUE, Boolean.TRUE), Boolean.FALSE}, // NOR of all true is false
newObject[] {Collections.emptyList(), Boolean.TRUE} // NOR over no inputs is true (negation of OR)
);
}
}