Skip to content

Add tests, remove main in ThreeSumProblem #5781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,7 @@
* [MedianOfRunningArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MedianOfRunningArrayTest.java)
* [MirrorOfMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MirrorOfMatrixTest.java)
* [PalindromeSinglyLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/PalindromeSinglyLinkedListTest.java)
* [ThreeSumProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/ThreeSumProblemTest.java)
* [TwoSumProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/TwoSumProblemTest.java)
* others
* [ArrayLeftRotationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java)
Expand Down
19 changes: 0 additions & 19 deletions src/main/java/com/thealgorithms/misc/ThreeSumProblem.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,10 @@
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class ThreeSumProblem {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter the target sum ");
int ts = scan.nextInt();
System.out.print("Enter the number of elements in the array ");
int n = scan.nextInt();
System.out.println("Enter all your array elements:");
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = scan.nextInt();
}
ThreeSumProblem th = new ThreeSumProblem();
System.out.println("Brute Force Approach\n" + (th.bruteForce(arr, ts)) + "\n");
System.out.println("Two Pointer Approach\n" + (th.twoPointer(arr, ts)) + "\n");
System.out.println("Hashmap Approach\n" + (th.hashMap(arr, ts)));
scan.close();
}

public List<List<Integer>> bruteForce(int[] nums, int target) {
List<List<Integer>> arr = new ArrayList<List<Integer>>();

Expand Down
76 changes: 76 additions & 0 deletions src/test/java/com/thealgorithms/misc/ThreeSumProblemTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.thealgorithms.misc;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class ThreeSumProblemTest {

private ThreeSumProblem tsp;

@BeforeEach
public void setup() {
tsp = new ThreeSumProblem(); // Initialize the class before each test
}

@Test
public void testBruteForceValidTriplets() {
int[] nums = {1, 2, -3, 4, -2, -1};
int target = 0;
List<List<Integer>> expected = Arrays.asList(Arrays.asList(-3, 1, 2), Arrays.asList(-3, -1, 4));
assertEquals(expected, tsp.bruteForce(nums, target));
}

@Test
public void testBruteForceNoTripletFound() {
int[] nums = {1, 2, 3, 4, 5};
int target = 50; // No valid triplet exists
List<List<Integer>> expected = new ArrayList<>(); // Expecting an empty list
assertEquals(expected, tsp.bruteForce(nums, target));
}

@Test
public void testTwoPointerValidTriplets() {
int[] nums = {0, -1, 2, -3, 1};
int target = 0;
List<List<Integer>> expected = Arrays.asList(Arrays.asList(-3, 1, 2), Arrays.asList(-1, 0, 1));
assertEquals(expected, tsp.twoPointer(nums, target));
}

@Test
public void testTwoPointerNegativeNumbers() {
int[] nums = {-5, -4, -3, -2, -1};
int target = -10;
List<List<Integer>> expected = Arrays.asList(Arrays.asList(-5, -4, -1), Arrays.asList(-5, -3, -2));
assertEquals(expected, tsp.twoPointer(nums, target));
}

@Test
public void testHashMapValidTriplets() {
int[] nums = {1, 2, -1, -4, 3, 0};
int target = 2;
List<List<Integer>> expected = Arrays.asList(Arrays.asList(-1, 0, 3), Arrays.asList(-1, 1, 2) // Check for distinct triplets
);
assertEquals(expected, tsp.hashMap(nums, target));
}

@Test
public void testHashMapNoTripletFound() {
int[] nums = {5, 7, 9, 11};
int target = 10;
List<List<Integer>> expected = new ArrayList<>();
assertEquals(expected, tsp.hashMap(nums, target));
}

@Test
public void testHashMapEmptyArray() {
int[] nums = {};
int target = 0;
List<List<Integer>> expected = new ArrayList<>();
assertEquals(expected, tsp.hashMap(nums, target));
}
}