Skip to content

Commit c5b64d9

Browse files
committed
tests: add tests of Mode
1 parent 295ac4b commit c5b64d9

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/main/java/com/thealgorithms/maths/Mode.java

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.thealgorithms.maths;
22

33
import java.util.ArrayList;
4-
import java.util.Arrays;
54
import java.util.Collections;
65
import java.util.HashMap;
76

@@ -11,15 +10,8 @@
1110
* The mode of an array of numbers is the most frequently occurring number in the array,
1211
* or the most frequently occurring numbers if there are multiple numbers with the same frequency
1312
*/
14-
public class Mode {
15-
16-
public static void main(String[] args) {
17-
/* Test array of integers */
18-
assert (mode(new int[] {})) == null;
19-
assert Arrays.equals(mode(new int[] {5}), new int[] {5});
20-
assert Arrays.equals(mode(new int[] {1, 2, 3, 4, 5}), new int[] {1, 2, 3, 4, 5});
21-
assert Arrays.equals(mode(new int[] {7, 9, 9, 4, 5, 6, 7, 7, 8}), new int[] {7});
22-
assert Arrays.equals(mode(new int[] {7, 9, 9, 4, 5, 6, 7, 7, 9}), new int[] {7, 9});
13+
public final class Mode {
14+
private Mode() {
2315
}
2416

2517
/*
@@ -28,7 +20,7 @@ public static void main(String[] args) {
2820
* @param numbers array of integers
2921
* @return mode of the array
3022
*/
31-
public static int[] mode(int[] numbers) {
23+
public static int[] mode(final int[] numbers) {
3224
if (numbers.length == 0) {
3325
return null;
3426
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
9+
10+
public class ModeTest {
11+
@ParameterizedTest
12+
@MethodSource("tcStream")
13+
void basicTest(final int[] expected, final int[] numbers) {
14+
assertArrayEquals(expected, Mode.mode(numbers));
15+
}
16+
17+
private static Stream<Arguments> tcStream() {
18+
return Stream.of(Arguments.of(null, new int[] {}), Arguments.of(new int[] {5}, new int[] {5}), Arguments.of(new int[] {1, 2, 3, 4, 5}, new int[] {1, 2, 3, 4, 5}), Arguments.of(new int[] {1, 2, 3, 4, 5}, new int[] {5, 4, 3, 2, 1}),
19+
Arguments.of(new int[] {7}, new int[] {7, 9, 9, 4, 5, 6, 7, 7, 8}), Arguments.of(new int[] {7, 9}, new int[] {7, 9, 9, 4, 5, 6, 7, 7, 9}));
20+
}
21+
}

0 commit comments

Comments
 (0)