mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-23 20:44:39 +08:00
Add tests, remove main
in ThreeSumProblem
(#5781)
This commit is contained in:
@ -7,29 +7,10 @@ import java.util.HashMap;
|
||||
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>>();
|
||||
|
||||
|
@ -0,0 +1,52 @@
|
||||
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 java.util.stream.Stream;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
public class ThreeSumProblemTest {
|
||||
|
||||
private ThreeSumProblem tsp;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
tsp = new ThreeSumProblem();
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("bruteForceTestProvider")
|
||||
public void testBruteForce(int[] nums, int target, List<List<Integer>> expected) {
|
||||
assertEquals(expected, tsp.bruteForce(nums, target));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("twoPointerTestProvider")
|
||||
public void testTwoPointer(int[] nums, int target, List<List<Integer>> expected) {
|
||||
assertEquals(expected, tsp.twoPointer(nums, target));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("hashMapTestProvider")
|
||||
public void testHashMap(int[] nums, int target, List<List<Integer>> expected) {
|
||||
assertEquals(expected, tsp.hashMap(nums, target));
|
||||
}
|
||||
|
||||
private static Stream<Arguments> bruteForceTestProvider() {
|
||||
return Stream.of(Arguments.of(new int[] {1, 2, -3, 4, -2, -1}, 0, Arrays.asList(Arrays.asList(-3, 1, 2), Arrays.asList(-3, -1, 4))), Arguments.of(new int[] {1, 2, 3, 4, 5}, 50, new ArrayList<>()));
|
||||
}
|
||||
|
||||
private static Stream<Arguments> twoPointerTestProvider() {
|
||||
return Stream.of(Arguments.of(new int[] {0, -1, 2, -3, 1}, 0, Arrays.asList(Arrays.asList(-3, 1, 2), Arrays.asList(-1, 0, 1))), Arguments.of(new int[] {-5, -4, -3, -2, -1}, -10, Arrays.asList(Arrays.asList(-5, -4, -1), Arrays.asList(-5, -3, -2))));
|
||||
}
|
||||
|
||||
private static Stream<Arguments> hashMapTestProvider() {
|
||||
return Stream.of(Arguments.of(new int[] {1, 2, -1, -4, 3, 0}, 2, Arrays.asList(Arrays.asList(-1, 0, 3), Arrays.asList(-1, 1, 2))), Arguments.of(new int[] {5, 7, 9, 11}, 10, new ArrayList<>()), Arguments.of(new int[] {}, 0, new ArrayList<>()));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user