|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
4 | 4 | import static org.junit.jupiter.api.Assertions.assertThrows; |
5 | | -import static org.junit.jupiter.api.Assertions.assertTrue; |
6 | 5 |
|
7 | 6 | import java.util.ArrayList; |
8 | 7 | import java.util.Arrays; |
9 | 8 | import java.util.List; |
| 9 | +import java.util.stream.Stream; |
10 | 10 | import org.junit.jupiter.api.Test; |
| 11 | +import org.junit.jupiter.params.ParameterizedTest; |
| 12 | +import org.junit.jupiter.params.provider.Arguments; |
| 13 | +import org.junit.jupiter.params.provider.MethodSource; |
11 | 14 |
|
12 | 15 | class MergeSortedArrayListTest { |
13 | 16 |
|
14 | | - @Test |
15 | | - void testMergeTwoSortedLists() { |
16 | | - List<Integer> listA = Arrays.asList(1, 3, 5, 7, 9); |
17 | | - List<Integer> listB = Arrays.asList(2, 4, 6, 8, 10); |
| 17 | + @ParameterizedTest(name = "{3}") |
| 18 | + @MethodSource("provideMergeTestData") |
| 19 | + void testMergeParameterizedScenarios(List<Integer> listA, List<Integer> listB, List<Integer> expected, String scenarioName) { |
18 | 20 | List<Integer> result = new ArrayList<>(); |
19 | | - |
20 | 21 | MergeSortedArrayList.merge(listA, listB, result); |
21 | | - |
22 | | - List<Integer> expected = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
23 | | - assertEquals(expected, result, "Merged list should be sorted and contain all elements from both input lists."); |
| 22 | + assertEquals(expected, result, () -> "Failed scenario: " + scenarioName); |
24 | 23 | } |
25 | 24 |
|
26 | | - @Test |
27 | | - void testMergeWithEmptyList() { |
28 | | - List<Integer> listA = Arrays.asList(1, 2, 3); |
29 | | - List<Integer> listB = new ArrayList<>(); // Empty list |
30 | | - List<Integer> result = new ArrayList<>(); |
31 | | - |
32 | | - MergeSortedArrayList.merge(listA, listB, result); |
33 | | - |
34 | | - List<Integer> expected = Arrays.asList(1, 2, 3); |
35 | | - assertEquals(expected, result, "Merged list should match listA when listB is empty."); |
36 | | - } |
37 | | - |
38 | | - @Test |
39 | | - void testMergeWithBothEmptyLists() { |
40 | | - List<Integer> listA = new ArrayList<>(); // Empty list |
41 | | - List<Integer> listB = new ArrayList<>(); // Empty list |
42 | | - List<Integer> result = new ArrayList<>(); |
43 | | - |
44 | | - MergeSortedArrayList.merge(listA, listB, result); |
45 | | - |
46 | | - assertTrue(result.isEmpty(), "Merged list should be empty when both input lists are empty."); |
| 25 | + private static Stream<Arguments> provideMergeTestData() { |
| 26 | + return Stream.of(Arguments.of(Arrays.asList(1, 3, 5, 7, 9), Arrays.asList(2, 4, 6, 8, 10), Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), "Standard alternating sorted lists"), Arguments.of(Arrays.asList(1, 2, 3), new ArrayList<>(), Arrays.asList(1, 2, 3), "Merge with empty second list"), |
| 27 | + Arguments.of(new ArrayList<>(), Arrays.asList(4, 5, 6), Arrays.asList(4, 5, 6), "Merge with empty first list"), Arguments.of(new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), "Merge with both lists empty"), |
| 28 | + Arguments.of(Arrays.asList(1, 2, 2, 3), Arrays.asList(2, 3, 4), Arrays.asList(1, 2, 2, 2, 3, 3, 4), "Handling duplicate elements gracefully"), |
| 29 | + Arguments.of(Arrays.asList(-3, -1, 2), Arrays.asList(-2, 0, 3), Arrays.asList(-3, -2, -1, 0, 2, 3), "Handling negative numbers mixed with positive numbers")); |
47 | 30 | } |
48 | 31 |
|
49 | 32 | @Test |
50 | | - void testMergeWithDuplicateElements() { |
51 | | - List<Integer> listA = Arrays.asList(1, 2, 2, 3); |
52 | | - List<Integer> listB = Arrays.asList(2, 3, 4); |
| 33 | + void testMergeThrowsExceptionWhenListAIsNull() { |
| 34 | + List<Integer> listB = Arrays.asList(1, 2, 3); |
53 | 35 | List<Integer> result = new ArrayList<>(); |
54 | | - |
55 | | - MergeSortedArrayList.merge(listA, listB, result); |
56 | | - |
57 | | - List<Integer> expected = Arrays.asList(1, 2, 2, 2, 3, 3, 4); |
58 | | - assertEquals(expected, result, "Merged list should correctly handle and include duplicate elements."); |
| 36 | + assertThrows(NullPointerException.class, () -> MergeSortedArrayList.merge(null, listB, result)); |
59 | 37 | } |
60 | 38 |
|
61 | 39 | @Test |
62 | | - void testMergeWithNegativeAndPositiveNumbers() { |
63 | | - List<Integer> listA = Arrays.asList(-3, -1, 2); |
64 | | - List<Integer> listB = Arrays.asList(-2, 0, 3); |
| 40 | + void testMergeThrowsExceptionWhenListBIsNull() { |
| 41 | + List<Integer> listA = Arrays.asList(1, 2, 3); |
65 | 42 | List<Integer> result = new ArrayList<>(); |
66 | | - |
67 | | - MergeSortedArrayList.merge(listA, listB, result); |
68 | | - |
69 | | - List<Integer> expected = Arrays.asList(-3, -2, -1, 0, 2, 3); |
70 | | - assertEquals(expected, result, "Merged list should correctly handle negative and positive numbers."); |
| 43 | + assertThrows(NullPointerException.class, () -> MergeSortedArrayList.merge(listA, null, result)); |
71 | 44 | } |
72 | 45 |
|
73 | 46 | @Test |
74 | | - void testMergeThrowsExceptionOnNullInput() { |
75 | | - List<Integer> listA = null; |
76 | | - List<Integer> listB = Arrays.asList(1, 2, 3); |
77 | | - List<Integer> result = new ArrayList<>(); |
78 | | - |
79 | | - List<Integer> finalListB = listB; |
80 | | - List<Integer> finalListA = listA; |
81 | | - List<Integer> finalResult = result; |
82 | | - assertThrows(NullPointerException.class, () -> MergeSortedArrayList.merge(finalListA, finalListB, finalResult), "Should throw NullPointerException if any input list is null."); |
83 | | - |
84 | | - listA = Arrays.asList(1, 2, 3); |
85 | | - listB = null; |
86 | | - List<Integer> finalListA1 = listA; |
87 | | - List<Integer> finalListB1 = listB; |
88 | | - List<Integer> finalResult1 = result; |
89 | | - assertThrows(NullPointerException.class, () -> MergeSortedArrayList.merge(finalListA1, finalListB1, finalResult1), "Should throw NullPointerException if any input list is null."); |
90 | | - |
91 | | - listA = Arrays.asList(1, 2, 3); |
92 | | - listB = Arrays.asList(4, 5, 6); |
93 | | - result = null; |
94 | | - List<Integer> finalListA2 = listA; |
95 | | - List<Integer> finalListB2 = listB; |
96 | | - List<Integer> finalResult2 = result; |
97 | | - assertThrows(NullPointerException.class, () -> MergeSortedArrayList.merge(finalListA2, finalListB2, finalResult2), "Should throw NullPointerException if the result collection is null."); |
| 47 | + void testMergeThrowsExceptionWhenResultCollectionIsNull() { |
| 48 | + List<Integer> listA = Arrays.asList(1, 2, 3); |
| 49 | + List<Integer> listB = Arrays.asList(4, 5, 6); |
| 50 | + assertThrows(NullPointerException.class, () -> MergeSortedArrayList.merge(listA, listB, null)); |
98 | 51 | } |
99 | 52 | } |
0 commit comments