Skip to content

Add algo for BooleanGateslogic #5717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Oct 12, 2024
Merged

Conversation

VANSH3104
Copy link
Contributor

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-commenter
Copy link

codecov-commenter commented Oct 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

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              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@saahil-mahato
Copy link
Contributor

Please add tests and use proper javadoc standard found here

@VANSH3104
Copy link
Contributor Author

VANSH3104 commented Oct 11, 2024

@saahil-mahato @alxkm not able to understand why i got clang format issue

@alxkm
Copy link
Contributor

alxkm commented Oct 11, 2024

@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.

@VANSH3104
Copy link
Contributor Author

@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

@alxkm alxkm merged commit 4a03f42 into TheAlgorithms:master Oct 12, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants