|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
4 | 4 |
|
5 |
| -import org.junit.jupiter.api.Test; |
| 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; |
6 | 9 |
|
7 | 10 | public class MedianOfTwoSortedArraysTest {
|
8 | 11 |
|
9 |
| - @Test |
10 |
| - void testFindMedianSortedArrays() { |
11 |
| - // Test case 1: Arrays of equal length |
12 |
| - assertEquals(2.5, MedianOfTwoSortedArrays.findMedianSortedArrays(new int[] {1, 3}, new int[] {2, 4})); |
| 12 | + @ParameterizedTest |
| 13 | + @MethodSource("provideTestCases") |
| 14 | + void testFindMedianSortedArrays(int[] nums1, int[] nums2, double expectedMedian) { |
| 15 | + assertEquals(expectedMedian, MedianOfTwoSortedArrays.findMedianSortedArrays(nums1, nums2)); |
| 16 | + } |
| 17 | + |
| 18 | + // MethodSource that provides test cases as streams of arguments |
| 19 | + private static Stream<Arguments> provideTestCases() { |
| 20 | + return Stream.of( |
| 21 | + // Test case 1: Arrays of equal length |
| 22 | + Arguments.of(new int[] {1, 3}, new int[] {2, 4}, 2.5), |
13 | 23 |
|
14 |
| - // Test case 2: Arrays of different lengths |
15 |
| - assertEquals(2.0, MedianOfTwoSortedArrays.findMedianSortedArrays(new int[] {1, 3}, new int[] {2})); |
| 24 | + // Test case 2: Arrays of different lengths |
| 25 | + Arguments.of(new int[] {1, 3}, new int[] {2}, 2.0), |
16 | 26 |
|
17 |
| - // Test case 3: Arrays with even total length |
18 |
| - assertEquals(4.5, MedianOfTwoSortedArrays.findMedianSortedArrays(new int[] {1, 2, 8}, new int[] {3, 4, 5, 6, 7})); |
| 27 | + // Test case 3: Arrays with even total length |
| 28 | + Arguments.of(new int[] {1, 2, 8}, new int[] {3, 4, 5, 6, 7}, 4.5), |
19 | 29 |
|
20 |
| - // Test case 4: Arrays with odd total length |
21 |
| - assertEquals(3.5, MedianOfTwoSortedArrays.findMedianSortedArrays(new int[] {1, 2, 8}, new int[] {3, 4, 5})); |
| 30 | + // Test case 4: Arrays with odd total length |
| 31 | + Arguments.of(new int[] {1, 2, 8}, new int[] {3, 4, 5}, 3.5), |
22 | 32 |
|
23 |
| - // Test case 5: Single element arrays |
24 |
| - assertEquals(2.0, MedianOfTwoSortedArrays.findMedianSortedArrays(new int[] {1}, new int[] {3})); |
| 33 | + // Test case 5: Single element arrays |
| 34 | + Arguments.of(new int[] {1}, new int[] {3}, 2.0), |
25 | 35 |
|
26 |
| - // Test case 6: Empty arrays |
27 |
| - assertEquals(0.0, MedianOfTwoSortedArrays.findMedianSortedArrays(new int[] {}, new int[] {0})); |
| 36 | + // Test case 6: Empty arrays |
| 37 | + Arguments.of(new int[] {}, new int[] {0}, 0.0), |
28 | 38 |
|
29 |
| - // Test case 7: Same element arrays |
30 |
| - assertEquals(2.0, MedianOfTwoSortedArrays.findMedianSortedArrays(new int[] {2, 2, 2}, new int[] {2, 2, 2})); |
| 39 | + // Test case 7: Same element arrays |
| 40 | + Arguments.of(new int[] {2, 2, 2}, new int[] {2, 2, 2}, 2.0) |
| 41 | + ); |
31 | 42 | }
|
32 | 43 | }
|
0 commit comments