|
| 1 | +package com.thealgorithms.misc; |
| 2 | +import static org.junit.jupiter.api.Assertions.*; |
| 3 | + |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.List; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +public class FourSumProblemTest { |
| 9 | + @Test |
| 10 | + public void testEmptyArray() { |
| 11 | + int[] arr = {}; |
| 12 | + int target = 10; |
| 13 | + List<List<Integer>> result = FourSumProblem.fourSumProblem(arr, target); |
| 14 | + assertTrue(result.isEmpty(), "Expected no quadruplets for an empty array"); |
| 15 | + } |
| 16 | + |
| 17 | + @Test |
| 18 | + public void testLessThanFourElements() { |
| 19 | + int[] arr = {1, 2, 3}; |
| 20 | + int target = 6; |
| 21 | + List<List<Integer>> result = FourSumProblem.fourSumProblem(arr, target); |
| 22 | + assertTrue(result.isEmpty(), "Expected no quadruplets when array has less than 4 elements"); |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + public void testNoValidQuadruplet() { |
| 27 | + int[] arr = {1, 2, 3, 4, 5}; |
| 28 | + int target = 100; |
| 29 | + List<List<Integer>> result = FourSumProblem.fourSumProblem(arr, target); |
| 30 | + assertTrue(result.isEmpty(), "Expected no quadruplets when no combination matches the target"); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testValidQuadruplet() { |
| 35 | + int[] arr = {1, 0, -1, 0, -2, 2}; |
| 36 | + int target = 0; |
| 37 | + List<List<Integer>> result = FourSumProblem.fourSumProblem(arr, target); |
| 38 | + |
| 39 | + List<List<Integer>> expected = Arrays.asList(Arrays.asList(-2, -1, 1, 2), Arrays.asList(-2, 0, 0, 2), Arrays.asList(-1, 0, 0, 1)); |
| 40 | + assertEquals(expected.size(), result.size(), "Expected 3 valid quadruplets"); |
| 41 | + assertTrue(result.containsAll(expected), "The result should contain all expected quadruplets"); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testWithDuplicates() { |
| 46 | + int[] arr = {2, 2, 2, 2, 2}; |
| 47 | + int target = 8; |
| 48 | + List<List<Integer>> result = FourSumProblem.fourSumProblem(arr, target); |
| 49 | + |
| 50 | + List<List<Integer>> expected = Arrays.asList(Arrays.asList(2, 2, 2, 2)); |
| 51 | + assertEquals(expected.size(), result.size(), "Expected 1 valid quadruplet for repeated numbers"); |
| 52 | + assertTrue(result.containsAll(expected), "The result should contain the quadruplet [2, 2, 2, 2]"); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testNegativeNumbers() { |
| 57 | + int[] arr = {-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}; |
| 58 | + int target = 0; |
| 59 | + List<List<Integer>> result = FourSumProblem.fourSumProblem(arr, target); |
| 60 | + |
| 61 | + List<List<Integer>> expected = Arrays.asList(Arrays.asList(-5, -4, 4, 5), Arrays.asList(-5, -3, 3, 5), Arrays.asList(-5, -2, 2, 5), Arrays.asList(-5, -2, 3, 4), Arrays.asList(-5, -1, 1, 5), Arrays.asList(-5, -1, 2, 4), Arrays.asList(-5, 0, 1, 4), Arrays.asList(-5, 0, 2, 3), |
| 62 | + Arrays.asList(-4, -3, 2, 5), Arrays.asList(-4, -3, 3, 4), Arrays.asList(-4, -2, 1, 5), Arrays.asList(-4, -2, 2, 4), Arrays.asList(-4, -1, 0, 5), Arrays.asList(-4, -1, 1, 4), Arrays.asList(-4, -1, 2, 3), Arrays.asList(-4, 0, 1, 3), Arrays.asList(-3, -2, 0, 5), Arrays.asList(-3, -2, 1, 4), |
| 63 | + Arrays.asList(-3, -2, 2, 3), Arrays.asList(-3, -1, 0, 4), Arrays.asList(-3, -1, 1, 3), Arrays.asList(-3, 0, 1, 2), Arrays.asList(-2, -1, 0, 3), Arrays.asList(-2, -1, 1, 2)); |
| 64 | + |
| 65 | + assertEquals(expected.size(), result.size(), "Expected 24 valid quadruplets for negative and positive numbers"); |
| 66 | + assertTrue(result.containsAll(expected), "The result should contain all expected quadruplets"); |
| 67 | + } |
| 68 | +} |
0 commit comments