Add algo for BooleanGateslogic
Adding algorithm logic for Logicgates like or and #5680
- [ ] I have read CONTRIBUTING.md.
- [ ] This pull request is all my own work -- I have not plagiarized it.
- [ ] All filenames are in PascalCase.
- [ ] All functions and variable names follow Java naming conventions.
- [ ] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [ ] All new code is formatted with
clang-format -i --style=file path/to/your/file.java
Codecov Report
All modified and coverable lines are covered by tests :white_check_mark:
Project coverage is 63.91%. Comparing base (
1617ed1) to head (1b24895).
Additional details and impacted files
@@ Coverage Diff @@
## master #5717 +/- ##
============================================
+ Coverage 63.85% 63.91% +0.05%
Complexity 4167 4167
============================================
Files 581 582 +1
Lines 16278 16302 +24
Branches 3140 3148 +8
============================================
+ Hits 10395 10420 +25
+ Misses 5457 5456 -1
Partials 426 426
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
Please add tests and use proper javadoc standard found here
@saahil-mahato @alxkm not able to understand why i got clang format issue
@saahil-mahato @alxkm not able to understand why i got clang format issue
You can see details by clicking to the job details: https://github.com/TheAlgorithms/Java/actions/runs/11296030042/job/31420026439?pr=5717 Looks like there are redundant whitespaces.
But you added test descriptions to javadocs, which is wrong. You should have added standard Java tests for the test cases I described in the review comment.
This is the implementation:
package com.thealgorithms.bitmanipulation;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.ANDGate;
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.BooleanGate;
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.NANDGate;
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.NORGate;
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.NOTGate;
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.ORGate;
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.XORGate;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;
class BooleanAlgebraGatesTest {
@ParameterizedTest(name = "ANDGate Test Case {index}: inputs={0} -> expected={1}")
@MethodSource("provideAndGateTestCases")
void testANDGate(List<Boolean> inputs, boolean expected) {
BooleanGate gate = new ANDGate();
assertEquals(expected, gate.evaluate(inputs));
}
@ParameterizedTest(name = "ORGate Test Case {index}: inputs={0} -> expected={1}")
@MethodSource("provideOrGateTestCases")
void testORGate(List<Boolean> inputs, boolean expected) {
BooleanGate gate = new ORGate();
assertEquals(expected, gate.evaluate(inputs));
}
@ParameterizedTest(name = "NOTGate Test Case {index}: input={0} -> expected={1}")
@CsvSource({
"true, false",
"false, true"
})
void testNOTGate(boolean input, boolean expected) {
NOTGate gate = new NOTGate();
assertEquals(expected, gate.evaluate(input));
}
@ParameterizedTest(name = "XORGate Test Case {index}: inputs={0} -> expected={1}")
@MethodSource("provideXorGateTestCases")
void testXORGate(List<Boolean> inputs, boolean expected) {
BooleanGate gate = new XORGate();
assertEquals(expected, gate.evaluate(inputs));
}
@ParameterizedTest(name = "NANDGate Test Case {index}: inputs={0} -> expected={1}")
@MethodSource("provideNandGateTestCases")
void testNANDGate(List<Boolean> inputs, boolean expected) {
BooleanGate gate = new NANDGate();
assertEquals(expected, gate.evaluate(inputs));
}
@ParameterizedTest(name = "NORGate Test Case {index}: inputs={0} -> expected={1}")
@MethodSource("provideNorGateTestCases")
void testNORGate(List<Boolean> inputs, boolean expected) {
BooleanGate gate = new NORGate();
assertEquals(expected, gate.evaluate(inputs));
}
// Helper methods to provide test data for each gate
static Stream<Object[]> provideAndGateTestCases() {
return Stream.of(
new Object[]{Arrays.asList(true, true, true), true},
new Object[]{Arrays.asList(true, false, true), false},
new Object[]{Arrays.asList(false, false, false), false},
new Object[]{Collections.emptyList(), true} // AND over no inputs is true
);
}
static Stream<Object[]> provideOrGateTestCases() {
return Stream.of(
new Object[]{Arrays.asList(true, false, false), true},
new Object[]{Arrays.asList(false, false, false), false},
new Object[]{Arrays.asList(true, true, true), true},
new Object[]{Collections.emptyList(), false} // OR over no inputs is false
);
}
static Stream<Object[]> provideXorGateTestCases() {
return Stream.of(
new Object[]{Arrays.asList(true, false, true), false}, // XOR over odd true
new Object[]{Arrays.asList(true, false, false), true}, // XOR over single true
new Object[]{Arrays.asList(false, false, false), false},// XOR over all false
new Object[]{Arrays.asList(true, true), false} // XOR over even true
);
}
static Stream<Object[]> provideNandGateTestCases() {
return Stream.of(
new Object[]{Arrays.asList(true, true, true), false}, // NAND of all true is false
new Object[]{Arrays.asList(true, false), true}, // NAND with one false is true
new Object[]{Arrays.asList(false, false), true}, // NAND of all false is true
new Object[]{Collections.emptyList(), false} // NAND over no inputs is false (negation of AND)
);
}
static Stream<Object[]> provideNorGateTestCases() {
return Stream.of(
new Object[]{Arrays.asList(false, false), true}, // NOR of all false is true
new Object[]{Arrays.asList(false, true), false}, // NOR with one true is false
new Object[]{Arrays.asList(true, true), false}, // NOR of all true is false
new Object[]{Collections.emptyList(), true} // NOR over no inputs is true (negation of OR)
);
}
}
It may be necessary to correct some input data and the import order.
@alxkm github action told that there is error I don't understand I check it in my local setup it don't show any error