3booleans (#12287)
* BAEL-4605: code and tests for composing constraints (#12180) * BAEL-4605: code and tests for composing constraints * BAEL-4605: small fix and formatted the code * BAEL-4605: code review * BAEL-4605: code and tests for composing constraints (#12180) * BAEL-4605: code and tests for composing constraints * BAEL-4605: small fix and formatted the code * BAEL-4605: code review Co-authored-by: etrandafir93 <75391049+etrandafir93@users.noreply.github.com>
This commit is contained in:
parent
4d6f72f2a3
commit
f2f8c0d0ac
|
@ -0,0 +1,45 @@
|
|||
package com.baeldung.threebool;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class ThreeBooleans {
|
||||
public static boolean twoOrMoreAreTrueByLoop(boolean a, boolean b, boolean c) {
|
||||
int count = 0;
|
||||
for (boolean i : new Boolean[] { a, b, c }) {
|
||||
count += i ? 1 : 0;
|
||||
if (count >= 2)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean xOrMoreAreTrueByLoop(boolean[] booleans, int x) {
|
||||
int count = 0;
|
||||
for (boolean i : booleans) {
|
||||
count += i ? 1 : 0;
|
||||
if (count >= x)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean twoOrMoreAreTrueBySum(boolean a, boolean b, boolean c) {
|
||||
return (a ? 1 : 0) + (b ? 1 : 0) + (c ? 1 : 0) >= 2;
|
||||
}
|
||||
|
||||
public static boolean xOrMoreAreTrueBySum(Boolean[] booleans, int x) {
|
||||
return Arrays.stream(booleans).mapToInt(b -> Boolean.TRUE.equals(b) ? 1 : 0).sum() >= x;
|
||||
}
|
||||
|
||||
public static boolean twoorMoreAreTrueByKarnaughMap(boolean a, boolean b, boolean c) {
|
||||
return (c && (a || b)) || (a && b);
|
||||
}
|
||||
|
||||
public static boolean twoOrMoreAreTrueByOperators(boolean a, boolean b, boolean c) {
|
||||
return (a && b) || (a && c) || (b && c);
|
||||
}
|
||||
|
||||
public static boolean twoOrMoreAreTrueByXor(boolean a, boolean b, boolean c) {
|
||||
return a ^ b ? c : a;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.baeldung.threebool;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import java.util.Map;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ThreeBooleansUnitTest {
|
||||
// @formatter:off
|
||||
private static final Map<boolean[], Boolean> TEST_CASES_AND_EXPECTED = ImmutableMap.of(
|
||||
new boolean[]{true, true, true}, true,
|
||||
new boolean[]{true, true, false}, true,
|
||||
new boolean[]{true, false, false}, false,
|
||||
new boolean[]{false, false, false}, false
|
||||
);
|
||||
// @formatter:on
|
||||
|
||||
@Test
|
||||
void given3Booleans_whenCallingTwoOrMoreAreTrueByLoop_thenGetExpectedResult() {
|
||||
TEST_CASES_AND_EXPECTED.forEach((array, expected) -> assertThat(ThreeBooleans.twoOrMoreAreTrueByLoop(array[0], array[1], array[2])).isEqualTo(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
void given3Booleans_whenCallingTwoOrMoreAreTrueByCounting_thenGetExpectedResult() {
|
||||
TEST_CASES_AND_EXPECTED.forEach((array, expected) -> assertThat(ThreeBooleans.twoOrMoreAreTrueBySum(array[0], array[1], array[2])).isEqualTo(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
void given3Booleans_whenCallingTwoOrMoreAreTrueByKarnaughMap_thenGetExpectedResult() {
|
||||
TEST_CASES_AND_EXPECTED.forEach((array, expected) -> assertThat(ThreeBooleans.twoorMoreAreTrueByKarnaughMap(array[0], array[1], array[2])).isEqualTo(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
void given3Booleans_whenCallingTwoOrMoreAreTrueByOperators_thenGetExpectedResult() {
|
||||
TEST_CASES_AND_EXPECTED.forEach((array, expected) -> assertThat(ThreeBooleans.twoOrMoreAreTrueByOperators(array[0], array[1], array[2])).isEqualTo(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
void given3Booleans_whenCallingTwoOrMoreAreTrueByXor_thenGetExpectedResult() {
|
||||
TEST_CASES_AND_EXPECTED.forEach((array, expected) -> assertThat(ThreeBooleans.twoOrMoreAreTrueByXor(array[0], array[1], array[2])).isEqualTo(expected));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue