Skip to content

Commit

Permalink
Added more tests in MinimumBinaryFlips with some modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
shortthirdman committed Sep 20, 2024
1 parent 0817e54 commit cf35351
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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');
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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());
}
}

0 comments on commit cf35351

Please sign in to comment.