|
| 1 | +package com.thealgorithms.misc; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.List; |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +public class ThreeSumProblemTest { |
| 12 | + |
| 13 | + private ThreeSumProblem tsp; |
| 14 | + |
| 15 | + @BeforeEach |
| 16 | + public void setup() { |
| 17 | + tsp = new ThreeSumProblem(); // Initialize the class before each test |
| 18 | + } |
| 19 | + |
| 20 | + @Test |
| 21 | + public void testBruteForce_ValidTriplets() { |
| 22 | + int[] nums = {1, 2, -3, 4, -2, -1}; |
| 23 | + int target = 0; |
| 24 | + List<List<Integer>> expected = Arrays.asList(Arrays.asList(-3, 1, 2), Arrays.asList(-3, -1, 4)); |
| 25 | + assertEquals(expected, tsp.bruteForce(nums, target)); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + public void testBruteForce_NoTripletFound() { |
| 30 | + int[] nums = {1, 2, 3, 4, 5}; |
| 31 | + int target = 50; // No valid triplet exists |
| 32 | + List<List<Integer>> expected = new ArrayList<>(); // Expecting an empty list |
| 33 | + assertEquals(expected, tsp.bruteForce(nums, target)); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testTwoPointer_ValidTriplets() { |
| 38 | + int[] nums = {0, -1, 2, -3, 1}; |
| 39 | + int target = 0; |
| 40 | + List<List<Integer>> expected = Arrays.asList(Arrays.asList(-3, 1, 2), Arrays.asList(-1, 0, 1)); |
| 41 | + assertEquals(expected, tsp.twoPointer(nums, target)); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testTwoPointer_NegativeNumbers() { |
| 46 | + int[] nums = {-5, -4, -3, -2, -1}; |
| 47 | + int target = -10; |
| 48 | + List<List<Integer>> expected = Arrays.asList(Arrays.asList(-5, -4, -1), Arrays.asList(-5, -3, -2)); |
| 49 | + assertEquals(expected, tsp.twoPointer(nums, target)); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testHashMap_ValidTriplets() { |
| 54 | + int[] nums = {1, 2, -1, -4, 3, 0}; |
| 55 | + int target = 2; |
| 56 | + List<List<Integer>> expected = Arrays.asList(Arrays.asList(-1, 0, 3), Arrays.asList(-1, 1, 2) // Check for distinct triplets |
| 57 | + ); |
| 58 | + assertEquals(expected, tsp.hashMap(nums, target)); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testHashMap_NoTripletFound() { |
| 63 | + int[] nums = {5, 7, 9, 11}; |
| 64 | + int target = 10; |
| 65 | + List<List<Integer>> expected = new ArrayList<>(); |
| 66 | + assertEquals(expected, tsp.hashMap(nums, target)); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testHashMap_EmptyArray() { |
| 71 | + int[] nums = {}; |
| 72 | + int target = 0; |
| 73 | + List<List<Integer>> expected = new ArrayList<>(); |
| 74 | + assertEquals(expected, tsp.hashMap(nums, target)); |
| 75 | + } |
| 76 | +} |
0 commit comments