Skip to content

Commit d280132

Browse files
committed
Fix comparer and add its unit tests
1 parent 65ed781 commit d280132

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,19 @@
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1515
<exec.mainClass>org.alda.Main</exec.mainClass>
1616
</properties>
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.junit.jupiter</groupId>
20+
<artifactId>junit-jupiter-api</artifactId>
21+
<version>5.10.0</version>
22+
<scope>test</scope>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.junit.jupiter</groupId>
26+
<artifactId>junit-jupiter-engine</artifactId>
27+
<version>5.10.0</version>
28+
<scope>test</scope>
29+
</dependency>
30+
</dependencies>
1731

1832
</project>

src/main/java/org/alda/common/Comparer.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,17 @@ public static <T> Integer compare(T a, T b) {
5050
// Compare as Numbers
5151
double numA = ((Number) a).doubleValue();
5252
double numB = ((Number) b).doubleValue();
53-
if (numA >= numB) {
53+
if (numA > numB) {
5454
return 1;
5555
} else if (numA < numB) {
5656
return -1;
5757
}
5858
return 0;
5959
} else {
60+
if(a == null || b == null) {
61+
throw new NullPointerException("Both arguments must be either Strings or Numbers.");
62+
}
63+
6064
throw new IllegalArgumentException("Both arguments must be either Strings or Numbers.");
6165
}
6266
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.alda.common;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
6+
/**
7+
* Unit tests for {@link Comparer} class.
8+
*/
9+
class ComparerTest {
10+
11+
@Test
12+
void testCompareStrings_LexicographicalOrder() {
13+
assertTrue(Comparer.compare("apple", "banana") < 0);
14+
assertTrue(Comparer.compare("banana", "apple") > 0);
15+
assertEquals(0, Comparer.compare("cherry", "cherry"));
16+
}
17+
18+
@Test
19+
void testCompareNumbers_NumericalOrder() {
20+
System.out.println(Comparer.compare(100, 100));
21+
assertTrue(Comparer.compare(10, 5) > 0);
22+
assertTrue(Comparer.compare(3.14, 4.2) < 0);
23+
assertEquals(0, Comparer.compare(100, 100));
24+
}
25+
26+
@Test
27+
void testCompareMixedNumberTypes() {
28+
assertTrue(Comparer.compare(42, 27.5) > 0);
29+
assertTrue(Comparer.compare(3.14, 3) > 0);
30+
assertTrue(Comparer.compare(2.5f, 3.5) < 0);
31+
}
32+
33+
@Test
34+
void testCompareWithInvalidTypes() {
35+
Exception exception1 = assertThrows(IllegalArgumentException.class, () ->
36+
Comparer.compare("apple", 42)
37+
);
38+
assertEquals("Both arguments must be either Strings or Numbers.", exception1.getMessage());
39+
40+
Exception exception2 = assertThrows(IllegalArgumentException.class, () ->
41+
Comparer.compare(42, "apple")
42+
);
43+
assertEquals("Both arguments must be either Strings or Numbers.", exception2.getMessage());
44+
}
45+
46+
@Test
47+
void testCompareWithNullValues() {
48+
Exception exception1 = assertThrows(NullPointerException.class, () ->
49+
Comparer.compare(null, "banana")
50+
);
51+
Exception exception2 = assertThrows(NullPointerException.class, () ->
52+
Comparer.compare(42, null)
53+
);
54+
Exception exception3 = assertThrows(NullPointerException.class, () ->
55+
Comparer.compare(null, null)
56+
);
57+
}
58+
}

0 commit comments

Comments
 (0)