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:
Oleksandr Klymenko
2025-07-14 08:51:17 +03:00
committed by GitHub
parent 933e929b54
commit 25aaa6e064
2 changed files with 31 additions and 11 deletions

View File

@@ -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'"));
}
}