Skip to content

Commit 25aaa6e

Browse files
refactor: OnesComplement Tests Using Parameterized Inputs (TheAlgorithms#6375)
refactor: OnesComplement Tests Using Parameterized Inputs Co-authored-by: Deniz Altunkapan <[email protected]>
1 parent 933e929 commit 25aaa6e

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

src/main/java/com/thealgorithms/bitmanipulation/OnesComplement.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,24 @@ public final class OnesComplement {
1212
private OnesComplement() {
1313
}
1414

15-
// Function to get the 1's complement of a binary number
15+
/**
16+
* Returns the 1's complement of a binary string.
17+
*
18+
* @param binary A string representing a binary number (e.g., "1010").
19+
* @return A string representing the 1's complement.
20+
* @throws IllegalArgumentException if the input is null or contains characters other than '0' or '1'.
21+
*/
1622
public static String onesComplement(String binary) {
17-
StringBuilder complement = new StringBuilder();
18-
// Invert each bit to get the 1's complement
19-
for (int i = 0; i < binary.length(); i++) {
20-
if (binary.charAt(i) == '0') {
21-
complement.append('1');
22-
} else {
23-
complement.append('0');
23+
if (binary == null || binary.isEmpty()) {
24+
throw new IllegalArgumentException("Input must be a non-empty binary string.");
25+
}
26+
27+
StringBuilder complement = new StringBuilder(binary.length());
28+
for (char bit : binary.toCharArray()) {
29+
switch (bit) {
30+
case '0' -> complement.append('1');
31+
case '1' -> complement.append('0');
32+
default -> throw new IllegalArgumentException("Input must contain only '0' and '1'. Found: " + bit);
2433
}
2534
}
2635
return complement.toString();

src/test/java/com/thealgorithms/bitmanipulation/OnesComplementTest.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package com.thealgorithms.bitmanipulation;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
46

57
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.NullAndEmptySource;
610

711
/**
812
* Test case for Highest Set Bit
@@ -39,9 +43,16 @@ public void testOnesComplementMixedBits() {
3943
assertEquals("1001", OnesComplement.onesComplement("0110"));
4044
}
4145

46+
@ParameterizedTest
47+
@NullAndEmptySource
48+
public void testOnesComplementNullOrEmptyInputThrowsException(String input) {
49+
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> OnesComplement.onesComplement(input));
50+
assertEquals("Input must be a non-empty binary string.", exception.getMessage());
51+
}
52+
4253
@Test
43-
public void testOnesComplementEmptyString() {
44-
// Test empty string scenario
45-
assertEquals("", OnesComplement.onesComplement(""));
54+
public void testOnesComplementInvalidCharactersThrowsException() {
55+
Exception exception = assertThrows(IllegalArgumentException.class, () -> OnesComplement.onesComplement("10a1"));
56+
assertTrue(exception.getMessage().startsWith("Input must contain only '0' and '1'"));
4657
}
4758
}

0 commit comments

Comments
 (0)