mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-11 06:04:27 +08:00
tests: add tests of Mode
(#5104)
This commit is contained in:
@ -1,7 +1,6 @@
|
|||||||
package com.thealgorithms.maths;
|
package com.thealgorithms.maths;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
@ -11,15 +10,8 @@ import java.util.HashMap;
|
|||||||
* The mode of an array of numbers is the most frequently occurring number in the array,
|
* The mode of an array of numbers is the most frequently occurring number in the array,
|
||||||
* or the most frequently occurring numbers if there are multiple numbers with the same frequency
|
* or the most frequently occurring numbers if there are multiple numbers with the same frequency
|
||||||
*/
|
*/
|
||||||
public class Mode {
|
public final class Mode {
|
||||||
|
private Mode() {
|
||||||
public static void main(String[] args) {
|
|
||||||
/* Test array of integers */
|
|
||||||
assert (mode(new int[] {})) == null;
|
|
||||||
assert Arrays.equals(mode(new int[] {5}), new int[] {5});
|
|
||||||
assert Arrays.equals(mode(new int[] {1, 2, 3, 4, 5}), new int[] {1, 2, 3, 4, 5});
|
|
||||||
assert Arrays.equals(mode(new int[] {7, 9, 9, 4, 5, 6, 7, 7, 8}), new int[] {7});
|
|
||||||
assert Arrays.equals(mode(new int[] {7, 9, 9, 4, 5, 6, 7, 7, 9}), new int[] {7, 9});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -28,7 +20,7 @@ public class Mode {
|
|||||||
* @param numbers array of integers
|
* @param numbers array of integers
|
||||||
* @return mode of the array
|
* @return mode of the array
|
||||||
*/
|
*/
|
||||||
public static int[] mode(int[] numbers) {
|
public static int[] mode(final int[] numbers) {
|
||||||
if (numbers.length == 0) {
|
if (numbers.length == 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
21
src/test/java/com/thealgorithms/maths/ModeTest.java
Normal file
21
src/test/java/com/thealgorithms/maths/ModeTest.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package com.thealgorithms.maths;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||||
|
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
|
public class ModeTest {
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("tcStream")
|
||||||
|
void basicTest(final int[] expected, final int[] numbers) {
|
||||||
|
assertArrayEquals(expected, Mode.mode(numbers));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Stream<Arguments> tcStream() {
|
||||||
|
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}),
|
||||||
|
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}));
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user