From 6de154d218a54186dfc8efefc45aab862b086aee Mon Sep 17 00:00:00 2001 From: Piotr Idzik <65706193+vil02@users.noreply.github.com> Date: Fri, 26 Apr 2024 08:40:01 +0200 Subject: [PATCH] tests: add tests of `Mode` (#5104) --- .../java/com/thealgorithms/maths/Mode.java | 14 +++---------- .../com/thealgorithms/maths/ModeTest.java | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+), 11 deletions(-) create mode 100644 src/test/java/com/thealgorithms/maths/ModeTest.java diff --git a/src/main/java/com/thealgorithms/maths/Mode.java b/src/main/java/com/thealgorithms/maths/Mode.java index 7333380b0..a92f404c6 100644 --- a/src/main/java/com/thealgorithms/maths/Mode.java +++ b/src/main/java/com/thealgorithms/maths/Mode.java @@ -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; } diff --git a/src/test/java/com/thealgorithms/maths/ModeTest.java b/src/test/java/com/thealgorithms/maths/ModeTest.java new file mode 100644 index 000000000..629fd8bd5 --- /dev/null +++ b/src/test/java/com/thealgorithms/maths/ModeTest.java @@ -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 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})); + } +}