|
| 1 | +package com.thealgorithms.greedyalgorithms; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +public class MergeIntervalsTest { |
| 8 | + |
| 9 | + @Test |
| 10 | + public void testMergeIntervalsWithOverlappingIntervals() { |
| 11 | + // Test case where some intervals overlap and should be merged |
| 12 | + int[][] intervals = { { 1, 3 }, { 2, 6 }, { 8, 10 }, { 15, 18 } }; |
| 13 | + int[][] expected = { { 1, 6 }, { 8, 10 }, { 15, 18 } }; |
| 14 | + int[][] result = MergeIntervals.merge(intervals); |
| 15 | + assertArrayEquals(expected, result); |
| 16 | + } |
| 17 | + |
| 18 | + @Test |
| 19 | + public void testMergeIntervalsWithNoOverlap() { |
| 20 | + // Test case where intervals do not overlap |
| 21 | + int[][] intervals = { { 1, 2 }, { 3, 4 }, { 5, 6 } }; |
| 22 | + int[][] expected = { { 1, 2 }, { 3, 4 }, { 5, 6 } }; |
| 23 | + int[][] result = MergeIntervals.merge(intervals); |
| 24 | + assertArrayEquals(expected, result); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void testMergeIntervalsWithCompleteOverlap() { |
| 29 | + // Test case where intervals completely overlap |
| 30 | + int[][] intervals = { { 1, 5 }, { 2, 4 }, { 3, 6 } }; |
| 31 | + int[][] expected = { { 1, 6 } }; |
| 32 | + int[][] result = MergeIntervals.merge(intervals); |
| 33 | + assertArrayEquals(expected, result); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testMergeIntervalsWithSingleInterval() { |
| 38 | + // Test case where only one interval is given |
| 39 | + int[][] intervals = { { 1, 2 } }; |
| 40 | + int[][] expected = { { 1, 2 } }; |
| 41 | + int[][] result = MergeIntervals.merge(intervals); |
| 42 | + assertArrayEquals(expected, result); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testMergeIntervalsWithEmptyArray() { |
| 47 | + // Test case where the input array is empty |
| 48 | + int[][] intervals = {}; |
| 49 | + int[][] expected = {}; |
| 50 | + int[][] result = MergeIntervals.merge(intervals); |
| 51 | + assertArrayEquals(expected, result); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testMergeIntervalsWithIdenticalIntervals() { |
| 56 | + // Test case where multiple identical intervals are given |
| 57 | + int[][] intervals = { { 1, 3 }, { 1, 3 }, { 1, 3 } }; |
| 58 | + int[][] expected = { { 1, 3 } }; |
| 59 | + int[][] result = MergeIntervals.merge(intervals); |
| 60 | + assertArrayEquals(expected, result); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testMergeIntervalsWithRandomIntervals() { |
| 65 | + // Test case with a mix of overlapping and non-overlapping intervals |
| 66 | + int[][] intervals = { { 1, 4 }, { 5, 7 }, { 2, 6 }, { 8, 10 } }; |
| 67 | + int[][] expected = { { 1, 6 }, { 5, 7 }, { 8, 10 } }; |
| 68 | + int[][] result = MergeIntervals.merge(intervals); |
| 69 | + assertArrayEquals(expected, result); |
| 70 | + } |
| 71 | +} |
0 commit comments