|
| 1 | +package com.thealgorithms.datastructures.heaps; |
| 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 MergeKSortedArraysTest { |
| 11 | + |
| 12 | + /** |
| 13 | + * Parameterized test for merging multiple sorted arrays. |
| 14 | + * Each test case provides input arrays and the expected merged output. |
| 15 | + * |
| 16 | + * @param arrays the input 2D array of sorted arrays |
| 17 | + * @param expected the expected merged sorted array |
| 18 | + */ |
| 19 | + @ParameterizedTest |
| 20 | + @MethodSource("provideTestCases") |
| 21 | + public void testMergeKArrays(int[][] arrays, int[] expected) { |
| 22 | + assertArrayEquals(expected, MergeKSortedArrays.mergeKArrays(arrays)); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Provides various test cases including edge cases for merging sorted arrays. |
| 27 | + * |
| 28 | + * @return a stream of test arguments containing input arrays and expected outputs |
| 29 | + */ |
| 30 | + private static Stream<Arguments> provideTestCases() { |
| 31 | + return Stream.of( |
| 32 | + // Basic test case with multiple arrays |
| 33 | + Arguments.of(new int[][] {{1, 4, 5}, {1, 3, 4}, {2, 6}}, new int[] {1, 1, 2, 3, 4, 4, 5, 6}), |
| 34 | + |
| 35 | + // Edge case: All arrays are empty |
| 36 | + Arguments.of(new int[][] {{}, {}, {}}, new int[] {}), |
| 37 | + |
| 38 | + // Edge case: One array is empty |
| 39 | + Arguments.of(new int[][] {{1, 3, 5}, {}, {2, 4, 6}}, new int[] {1, 2, 3, 4, 5, 6}), |
| 40 | + |
| 41 | + // Single array |
| 42 | + Arguments.of(new int[][] {{1, 2, 3}}, new int[] {1, 2, 3}), |
| 43 | + |
| 44 | + // Arrays with negative numbers |
| 45 | + Arguments.of(new int[][] {{-5, 1, 3}, {-10, 0, 2}}, new int[] {-10, -5, 0, 1, 2, 3}), |
| 46 | + |
| 47 | + // Arrays with duplicate elements |
| 48 | + Arguments.of(new int[][] {{1, 1, 2}, {1, 3, 3}, {2, 2, 4}}, new int[] {1, 1, 1, 2, 2, 2, 3, 3, 4}), |
| 49 | + |
| 50 | + // Edge case: Arrays of varying lengths |
| 51 | + Arguments.of(new int[][] {{1, 2}, {3}, {4, 5, 6, 7}}, new int[] {1, 2, 3, 4, 5, 6, 7})); |
| 52 | + } |
| 53 | +} |
0 commit comments