|
| 1 | +package com.thealgorithms.misc; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import static org.junit.jupiter.api.Assertions.*; |
| 5 | + |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +public class FourSumProblemTest { |
| 10 | + |
| 11 | + private final FourSumProblem fourSumProblem = new FourSumProblem(); |
| 12 | + |
| 13 | + @Test |
| 14 | + public void testFourSum_WithValidInput() { |
| 15 | + int[] nums = {1, 0, -1, 0, -2, 2}; |
| 16 | + int target = 0; |
| 17 | + List<List<Integer>> expected = Arrays.asList( |
| 18 | + Arrays.asList(-2, 0, 0, 2), |
| 19 | + Arrays.asList(-1, 0, 0, 1) |
| 20 | + ); |
| 21 | + |
| 22 | + List<List<Integer>> result = fourSumProblem.fourSum(nums, target); |
| 23 | + assertEquals(expected.size(), result.size()); |
| 24 | + assertTrue(result.containsAll(expected)); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void testFourSum_WithNoSolution() { |
| 29 | + int[] nums = {1, 2, 3, 4}; |
| 30 | + int target = 100; |
| 31 | + List<List<Integer>> result = fourSumProblem.fourSum(nums, target); |
| 32 | + assertTrue(result.isEmpty()); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testFourSum_WithDuplicates() { |
| 37 | + int[] nums = {1, 1, 1, 1}; |
| 38 | + int target = 4; |
| 39 | + List<List<Integer>> expected = Arrays.asList( |
| 40 | + Arrays.asList(1, 1, 1, 1) |
| 41 | + ); |
| 42 | + |
| 43 | + List<List<Integer>> result = fourSumProblem.fourSum(nums, target); |
| 44 | + assertEquals(expected.size(), result.size()); |
| 45 | + assertTrue(result.containsAll(expected)); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testFourSum_WithNegativeNumbers() { |
| 50 | + int[] nums = {-3, -2, -1, 0, 0, 1, 2, 3}; |
| 51 | + int target = 0; |
| 52 | + List<List<Integer>> expected = Arrays.asList( |
| 53 | + Arrays.asList(-3, 1, 1, 1), |
| 54 | + Arrays.asList(-2, 0, 0, 2), |
| 55 | + Arrays.asList(-1, 0, 0, 1) |
| 56 | + ); |
| 57 | + |
| 58 | + List<List<Integer>> result = fourSumProblem.fourSum(nums, target); |
| 59 | + assertEquals(expected.size(), result.size()); |
| 60 | + assertTrue(result.containsAll(expected)); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testFourSum_WithEmptyArray() { |
| 65 | + int[] nums = {}; |
| 66 | + int target = 0; |
| 67 | + List<List<Integer>> result = fourSumProblem.fourSum(nums, target); |
| 68 | + assertTrue(result.isEmpty()); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testFourSum_WithLessThanFourElements() { |
| 73 | + int[] nums = {1, 2, 3}; |
| 74 | + int target = 6; |
| 75 | + List<List<Integer>> result = fourSumProblem.fourSum(nums, target); |
| 76 | + assertTrue(result.isEmpty()); |
| 77 | + } |
| 78 | +} |
0 commit comments