From cf35351693051875f3124fc2398435b21f3dc523 Mon Sep 17 00:00:00 2001 From: Swetank Mohanty Date: Fri, 20 Sep 2024 19:16:56 +0530 Subject: [PATCH] Added more tests in MinimumBinaryFlips with some modifications --- .../leetcode/MinimumBinaryFlips.java | 12 ++- .../leetcode/MinimumBinaryFlipsTest.java | 85 ++++++++++++++++++- 2 files changed, 92 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/shortthirdman/quickstart/leetcode/MinimumBinaryFlips.java b/src/main/java/com/shortthirdman/quickstart/leetcode/MinimumBinaryFlips.java index c43e3ea..1eb0378 100644 --- a/src/main/java/com/shortthirdman/quickstart/leetcode/MinimumBinaryFlips.java +++ b/src/main/java/com/shortthirdman/quickstart/leetcode/MinimumBinaryFlips.java @@ -1,5 +1,7 @@ package com.shortthirdman.quickstart.leetcode; +import java.util.Objects; + /** * A password string, pwd, consists of binary characters (0s and 1s). A cyber-security expert is trying to determine the * minimum number of changes required to make the password secure. @@ -18,9 +20,13 @@ public class MinimumBinaryFlips { * @param text the binary string * @return the minimum number of flips to make the division possible */ - public Integer getMinFlips(String text) { + public int getMinFlips(String text) { int minFlips = Integer.MAX_VALUE; + if (Objects.isNull(text)) { + throw new NullPointerException("Input text password can not be null"); + } + int len = text.length(); int flipsToMakeAllZeros = flipsToMakeAllSame(text, '0'); @@ -41,11 +47,11 @@ public Integer getMinFlips(String text) { minFlips = Math.min(minFlips, Math.min(flipsToZeroPart1 + flipsToOnePart2, flipsToOnePart1 + flipsToZeroPart2)); } - return Integer.valueOf(minFlips); + return minFlips; } - public int flipsToMakeAllSame(String str, char targetChar) { + private int flipsToMakeAllSame(String str, char targetChar) { int flips = 0; for (char c : str.toCharArray()) { if (c != targetChar) { diff --git a/src/test/java/com/shortthirdman/quickstart/leetcode/MinimumBinaryFlipsTest.java b/src/test/java/com/shortthirdman/quickstart/leetcode/MinimumBinaryFlipsTest.java index 1aef8dc..3876b1a 100644 --- a/src/test/java/com/shortthirdman/quickstart/leetcode/MinimumBinaryFlipsTest.java +++ b/src/test/java/com/shortthirdman/quickstart/leetcode/MinimumBinaryFlipsTest.java @@ -1,10 +1,10 @@ package com.shortthirdman.quickstart.leetcode; -import static org.junit.jupiter.api.Assertions.assertEquals; - import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + public class MinimumBinaryFlipsTest { MinimumBinaryFlips app; @@ -24,4 +24,85 @@ public void testGetMinFlips_defaultScenario() { assertEquals(2, app.getMinFlips(secondPwd)); assertEquals(3, app.getMinFlips(thirdPwd)); } + + // Positive Test Cases + @Test + void testAllZeros() { + assertEquals(0, app.getMinFlips("0000")); + } + + @Test + void testAllOnes() { + assertEquals(0, app.getMinFlips("1111")); + } + + @Test + void testAlternatingCharacters() { + assertEquals(3, app.getMinFlips("010101")); + assertNotEquals(2, app.getMinFlips("010101")); + } + + @Test + void testMixedCharacters() { + assertEquals(0, app.getMinFlips("1100")); + assertNotEquals(1, app.getMinFlips("1100")); + } + + @Test + void testShortString() { + assertEquals(0, app.getMinFlips("1")); // Edge case with a single character + } + + @Test + void testTwoDifferentChars() { + assertEquals(1, app.getMinFlips("01")); + } + + // Negative Test Cases + @Test + void testEmptyString() { + assertEquals(0, app.getMinFlips("")); + } + + @Test + void testSingleCharacterZero() { + assertEquals(0, app.getMinFlips("0")); + } + + @Test + void testSingleCharacterOne() { + assertEquals(0, app.getMinFlips("1")); + } + + @Test + void testTwoSameChars() { + assertEquals(0, app.getMinFlips("00")); // Both are zeros + assertEquals(0, app.getMinFlips("11")); // Both are ones + } + + // Edge Cases + @Test + void testLargeInput() { + StringBuilder largeInput = new StringBuilder(); + for (int i = 0; i < 10000; i++) { + largeInput.append(i % 2 == 0 ? '0' : '1'); + } + assertEquals(5000, app.getMinFlips(largeInput.toString())); + } + + @Test + void testLongSameChars() { + String longZeros = "0".repeat(10000); + assertEquals(0, app.getMinFlips(longZeros)); + + String longOnes = "1".repeat(10000); + assertEquals(0, app.getMinFlips(longOnes)); + } + + // Exception Handling Test Cases + @Test + void testNullInput() { + Exception exception = assertThrows(NullPointerException.class, () -> app.getMinFlips(null)); + assertEquals("Input text password can not be null", exception.getMessage()); + } }