Skip to content

tests: add tests of Mode #5104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions src/main/java/com/thealgorithms/maths/Mode.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.thealgorithms.maths;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;

Expand All @@ -11,15 +10,8 @@
* 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() {
}

/*
Expand All @@ -28,7 +20,7 @@ public static void main(String[] args) {
* @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;
}
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/com/thealgorithms/maths/ModeTest.java
Original file line number Diff line number Diff line change
@@ -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}));
}
}