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 {
12
12
private OnesComplement () {
13
13
}
14
14
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
+ */
16
22
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 );
24
33
}
25
34
}
26
35
return complement .toString ();
Original file line number Diff line number Diff line change 1
1
package com .thealgorithms .bitmanipulation ;
2
2
3
3
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 ;
4
6
5
7
import org .junit .jupiter .api .Test ;
8
+ import org .junit .jupiter .params .ParameterizedTest ;
9
+ import org .junit .jupiter .params .provider .NullAndEmptySource ;
6
10
7
11
/**
8
12
* Test case for Highest Set Bit
@@ -39,9 +43,16 @@ public void testOnesComplementMixedBits() {
39
43
assertEquals ("1001" , OnesComplement .onesComplement ("0110" ));
40
44
}
41
45
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
+
42
53
@ 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' " ));
46
57
}
47
58
}
You can’t perform that action at this time.
0 commit comments