Skip to content

Commit 100d592

Browse files
committed
Refactor to Parameterized tests
1 parent 3d362ef commit 100d592

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

src/test/java/com/thealgorithms/divideandconquer/MedianOfTwoSortedArraysTest.java

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,42 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

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;
69

710
public class MedianOfTwoSortedArraysTest {
811

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),
1323

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),
1626

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),
1929

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),
2232

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),
2535

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),
2838

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+
);
3142
}
3243
}

0 commit comments

Comments
 (0)