File tree Expand file tree Collapse file tree 2 files changed +31
-11
lines changed
main/java/com/thealgorithms/bitmanipulation
test/java/com/thealgorithms/bitmanipulation Expand file tree Collapse file tree 2 files changed +31
-11
lines changed Original file line number Diff line number Diff 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 ();
Original file line number Diff line number Diff line change 11package com .thealgorithms .bitmanipulation ;
22
33import 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
57import 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}
You can’t perform that action at this time.
0 commit comments