tests: add tests of Mode (#5104)

This commit is contained in:
Piotr Idzik 2024-04-26 08:40:01 +02:00 committed by GitHub
parent 7a42f68b66
commit 6de154d218
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 11 deletions

View File

@ -1,7 +1,6 @@
package com.thealgorithms.maths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
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,
* or the most frequently occurring numbers if there are multiple numbers with the same frequency
*/
public class 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});
public final class Mode {
private Mode() {
}
/*
@ -28,7 +20,7 @@ public class Mode {
* @param numbers array of integers
* @return mode of the array
*/
public static int[] mode(int[] numbers) {
public static int[] mode(final int[] numbers) {
if (numbers.length == 0) {
return null;
}

View 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}));
}
}