|
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 | class NumberAppearingOddTimesTest {
|
8 | 11 |
|
9 |
| - @Test |
10 |
| - void testFindOddOccurrence() { |
11 |
| - int[] arr1 = {5, 6, 7, 8}; |
12 |
| - assertEquals(12, NumberAppearingOddTimes.findOddOccurrence(arr1)); |
| 12 | + /** |
| 13 | + * Parameterized test for findOddOccurrence method. Tests multiple |
| 14 | + * input arrays and their expected results. |
| 15 | + */ |
| 16 | + @ParameterizedTest |
| 17 | + @MethodSource("provideTestCases") |
| 18 | + void testFindOddOccurrence(int[] input, int expected) { |
| 19 | + assertEquals(expected, NumberAppearingOddTimes.findOddOccurrence(input)); |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Provides test cases for the parameterized test. |
| 24 | + * Each test case consists of an input array and the expected result. |
| 25 | + */ |
| 26 | + private static Stream<Arguments> provideTestCases() { |
| 27 | + return Stream.of( |
| 28 | + // Single element appearing odd times (basic case) |
| 29 | + Arguments.of(new int[] {5, 6, 7, 8, 6, 7, 5}, 8), |
| 30 | + |
| 31 | + // More complex case with multiple pairs |
| 32 | + Arguments.of(new int[] {2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2}, 5), |
| 33 | + |
| 34 | + // Case with only one element appearing once |
| 35 | + Arguments.of(new int[] {10, 10, 20, 20, 30}, 30), |
| 36 | + |
| 37 | + // Negative numbers with an odd occurrence |
| 38 | + Arguments.of(new int[] {-5, -5, -3, -3, -7, -7, -7}, -7), |
13 | 39 |
|
14 |
| - int[] arr2 = {2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2}; |
15 |
| - assertEquals(5, NumberAppearingOddTimes.findOddOccurrence(arr2)); |
| 40 | + // All elements cancel out to 0 (even occurrences of all elements) |
| 41 | + Arguments.of(new int[] {1, 2, 1, 2}, 0), |
16 | 42 |
|
17 |
| - int[] arr3 = {10, 10, 20, 20, 30}; |
18 |
| - assertEquals(30, NumberAppearingOddTimes.findOddOccurrence(arr3)); |
| 43 | + // Array with a single element (trivial case) |
| 44 | + Arguments.of(new int[] {42}, 42), |
19 | 45 |
|
20 |
| - int[] arr4 = {-5, -5, -3, -3, -7, -7, -7}; |
21 |
| - assertEquals(-7, NumberAppearingOddTimes.findOddOccurrence(arr4)); |
| 46 | + // Large array with repeated patterns |
| 47 | + Arguments.of(new int[] {1, 1, 2, 2, 3, 3, 3, 4, 4}, 3)); |
22 | 48 | }
|
23 | 49 | }
|
0 commit comments