Skip to content

Commit 933e929

Browse files
authored
refactor: refactor Ceil and improved tests (#6366)
* refactor: refactor Ceil and improved tests * checkstyle: remove redundant import * refactor: fix edge cases * refactor: fix checkstyle * refactor: fix checkstyle import order
1 parent ef93cc1 commit 933e929

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed
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/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)