diff --git a/src/main/java/com/thealgorithms/bitmanipulation/OnesComplement.java b/src/main/java/com/thealgorithms/bitmanipulation/OnesComplement.java index c5c068422113..aae3a996e49d 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/OnesComplement.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/OnesComplement.java @@ -12,15 +12,24 @@ public final class OnesComplement { private OnesComplement() { } - // Function to get the 1's complement of a binary number + /** + * Returns the 1's complement of a binary string. + * + * @param binary A string representing a binary number (e.g., "1010"). + * @return A string representing the 1's complement. + * @throws IllegalArgumentException if the input is null or contains characters other than '0' or '1'. + */ public static String onesComplement(String binary) { - StringBuilder complement = new StringBuilder(); - // Invert each bit to get the 1's complement - for (int i = 0; i < binary.length(); i++) { - if (binary.charAt(i) == '0') { - complement.append('1'); - } else { - complement.append('0'); + if (binary == null || binary.isEmpty()) { + throw new IllegalArgumentException("Input must be a non-empty binary string."); + } + + StringBuilder complement = new StringBuilder(binary.length()); + for (char bit : binary.toCharArray()) { + switch (bit) { + case '0' -> complement.append('1'); + case '1' -> complement.append('0'); + default -> throw new IllegalArgumentException("Input must contain only '0' and '1'. Found: " + bit); } } return complement.toString(); diff --git a/src/test/java/com/thealgorithms/bitmanipulation/OnesComplementTest.java b/src/test/java/com/thealgorithms/bitmanipulation/OnesComplementTest.java index 6be4eb595f79..0e90ed79f587 100644 --- a/src/test/java/com/thealgorithms/bitmanipulation/OnesComplementTest.java +++ b/src/test/java/com/thealgorithms/bitmanipulation/OnesComplementTest.java @@ -1,8 +1,12 @@ package com.thealgorithms.bitmanipulation; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.NullAndEmptySource; /** * Test case for Highest Set Bit @@ -39,9 +43,16 @@ public void testOnesComplementMixedBits() { assertEquals("1001", OnesComplement.onesComplement("0110")); } + @ParameterizedTest + @NullAndEmptySource + public void testOnesComplementNullOrEmptyInputThrowsException(String input) { + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> OnesComplement.onesComplement(input)); + assertEquals("Input must be a non-empty binary string.", exception.getMessage()); + } + @Test - public void testOnesComplementEmptyString() { - // Test empty string scenario - assertEquals("", OnesComplement.onesComplement("")); + public void testOnesComplementInvalidCharactersThrowsException() { + Exception exception = assertThrows(IllegalArgumentException.class, () -> OnesComplement.onesComplement("10a1")); + assertTrue(exception.getMessage().startsWith("Input must contain only '0' and '1'")); } }