mirror of
https://github.com/TheAlgorithms/Java.git
synced 2026-03-13 08:40:43 +08:00
refactor: OnesComplement Tests Using Parameterized Inputs (#6375)
refactor: OnesComplement Tests Using Parameterized Inputs Co-authored-by: Deniz Altunkapan <93663085+DenizAltunkapan@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
933e929b54
commit
25aaa6e064
@@ -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();
|
||||
|
||||
@@ -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 class OnesComplementTest {
|
||||
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'"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user