|
1 | 1 | package com.thealgorithms.others;
|
| 2 | + |
2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
3 |
| -import static org.junit.jupiter.api.Assertions.assertFalse; |
4 |
| -import static org.junit.jupiter.api.Assertions.assertTrue; |
5 | 4 |
|
6 |
| -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; |
| 9 | + |
7 | 10 | public class LineSweepTest {
|
| 11 | + private record OverlapTestCase(int[][] ranges, boolean expected) { |
| 12 | + } |
8 | 13 |
|
9 |
| - @Test |
10 |
| - void testForOverlap() { |
11 |
| - int[][] arr = {{0, 10}, {7, 20}, {15, 24}}; |
12 |
| - assertTrue(LineSweep.isOverlap(arr)); |
| 14 | + private record MaximumEndPointTestCase(int[][] ranges, int expected) { |
13 | 15 | }
|
14 | 16 |
|
15 |
| - @Test |
16 |
| - void testForNoOverlap() { |
17 |
| - int[][] arr = {{0, 10}, {11, 20}, {21, 24}}; |
18 |
| - assertFalse(LineSweep.isOverlap(arr)); |
| 17 | + @ParameterizedTest |
| 18 | + @MethodSource("provideOverlapTestData") |
| 19 | + void testIsOverlap(OverlapTestCase testCase) { |
| 20 | + assertEquals(testCase.expected(), LineSweep.isOverlap(testCase.ranges())); |
19 | 21 | }
|
20 |
| - @Test |
21 |
| - void testForOverlapWhenEndAEqualsStartBAndViceVersa() { |
22 |
| - int[][] arr = {{0, 10}, {10, 20}, {21, 24}}; |
23 |
| - assertTrue(LineSweep.isOverlap(arr)); |
| 22 | + |
| 23 | + private static Stream<Arguments> provideOverlapTestData() { |
| 24 | + return Stream.of(Arguments.of(new OverlapTestCase(new int[][] {{0, 10}, {7, 20}, {15, 24}}, true)), Arguments.of(new OverlapTestCase(new int[][] {{0, 10}, {11, 20}, {21, 24}}, false)), Arguments.of(new OverlapTestCase(new int[][] {{0, 10}, {10, 20}, {21, 24}}, true)), |
| 25 | + Arguments.of(new OverlapTestCase(new int[][] {{5, 10}}, false)), Arguments.of(new OverlapTestCase(new int[][] {{1, 5}, {1, 5}, {1, 5}}, true)), Arguments.of(new OverlapTestCase(new int[][] {{1, 1}, {2, 2}, {3, 3}}, false)), Arguments.of(new OverlapTestCase(new int[][] {}, false))); |
24 | 26 | }
|
25 |
| - @Test |
26 |
| - void testForMaximumEndPoint() { |
27 |
| - int[][] arr = {{10, 20}, {1, 100}, {14, 16}, {1, 8}}; |
28 |
| - assertEquals(100, LineSweep.findMaximumEndPoint(arr)); |
| 27 | + |
| 28 | + @ParameterizedTest |
| 29 | + @MethodSource("provideMaximumEndPointTestData") |
| 30 | + void testFindMaximumEndPoint(MaximumEndPointTestCase testCase) { |
| 31 | + assertEquals(testCase.expected(), LineSweep.findMaximumEndPoint(testCase.ranges())); |
| 32 | + } |
| 33 | + |
| 34 | + private static Stream<Arguments> provideMaximumEndPointTestData() { |
| 35 | + return Stream.of(Arguments.of(new MaximumEndPointTestCase(new int[][] {{10, 20}, {1, 100}, {14, 16}, {1, 8}}, 100))); |
29 | 36 | }
|
30 | 37 | }
|
0 commit comments