Skip to content

Commit 260f707

Browse files
add sign function
1 parent a37d4ec commit 260f707

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.examplehub.maths;
2+
3+
public class SignFunction {
4+
5+
/**
6+
* Sign function algorithm.
7+
*
8+
* @param number the number to be checked.
9+
* @return {@code 1} if number is positive, {@code 0} returned if number is zero, otherwise {@code -1} returned.
10+
*/
11+
public static int signFunction(int number) {
12+
return Integer.compare(number, 0);
13+
}
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.examplehub.maths;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class SignFunctionTest {
8+
@Test
9+
void testSignFunction() {
10+
for (int i = 1; i <= 100; i++) {
11+
assertEquals(1, SignFunction.signFunction(i));
12+
assertEquals(-1, SignFunction.signFunction(-i));
13+
}
14+
assertEquals(0, SignFunction.signFunction(0));
15+
}
16+
}

0 commit comments

Comments
 (0)