Skip to content

Commit b70e79a

Browse files
authored
Merge branch 'master' into refactor/Mode
2 parents b7f22b8 + 25aaa6e commit b70e79a

File tree

4 files changed

+72
-27
lines changed

4 files changed

+72
-27
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();
Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
11
package com.thealgorithms.maths;
22

3+
/**
4+
* Utility class to compute the ceiling of a given number.
5+
*/
36
public final class Ceil {
7+
48
private Ceil() {
59
}
610

711
/**
8-
* Returns the smallest (closest to negative infinity)
12+
* Returns the smallest double value that is greater than or equal to the input.
13+
* Equivalent to mathematical ⌈x⌉ (ceiling function).
914
*
10-
* @param number the number
11-
* @return the smallest (closest to negative infinity) of given
12-
* {@code number}
15+
* @param number the number to ceil
16+
* @return the smallest double greater than or equal to {@code number}
1317
*/
1418
public static double ceil(double number) {
15-
if (number - (int) number == 0) {
19+
if (Double.isNaN(number) || Double.isInfinite(number) || number == 0.0 || number < Integer.MIN_VALUE || number > Integer.MAX_VALUE) {
1620
return number;
17-
} else if (number - (int) number > 0) {
18-
return (int) (number + 1);
21+
}
22+
23+
if (number < 0.0 && number > -1.0) {
24+
return -0.0;
25+
}
26+
27+
long intPart = (long) number;
28+
if (number > 0 && number != intPart) {
29+
return intPart + 1.0;
1930
} else {
20-
return (int) number;
31+
return intPart;
2132
}
2233
}
2334
}

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
}

src/test/java/com/thealgorithms/maths/CeilTest.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,30 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5-
import org.junit.jupiter.api.Test;
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.CsvSource;
8+
import org.junit.jupiter.params.provider.MethodSource;
69

710
public class CeilTest {
811

9-
@Test
10-
void testCeil() {
11-
assertEquals(8, Ceil.ceil(7.057));
12-
assertEquals(8, Ceil.ceil(7.004));
13-
assertEquals(-13, Ceil.ceil(-13.004));
14-
assertEquals(1, Ceil.ceil(.98));
15-
assertEquals(-11, Ceil.ceil(-11.357));
12+
@ParameterizedTest
13+
@CsvSource({"7.057, 8", "7.004, 8", "-13.004, -13", "0.98, 1", "-11.357, -11"})
14+
void testCeil(double input, int expected) {
15+
assertEquals(expected, Ceil.ceil(input));
16+
}
17+
18+
@ParameterizedTest
19+
@MethodSource("edgeCaseProvider")
20+
void testEdgeCases(TestData data) {
21+
assertEquals(Ceil.ceil(data.input), data.expected);
22+
}
23+
24+
record TestData(double input, double expected) {
25+
}
26+
27+
static Stream<TestData> edgeCaseProvider() {
28+
return Stream.of(new TestData(Double.MAX_VALUE, Double.MAX_VALUE), new TestData(Double.MIN_VALUE, Math.ceil(Double.MIN_VALUE)), new TestData(0.0, Math.ceil(0.0)), new TestData(-0.0, Math.ceil(-0.0)), new TestData(Double.NaN, Math.ceil(Double.NaN)),
29+
new TestData(Double.NEGATIVE_INFINITY, Math.ceil(Double.NEGATIVE_INFINITY)), new TestData(Double.POSITIVE_INFINITY, Math.ceil(Double.POSITIVE_INFINITY)));
1630
}
1731
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy