Skip to content

Commit 7b09785

Browse files
committed
Add tests, remove main in RangeInSortedArray
1 parent 596c614 commit 7b09785

File tree

2 files changed

+76
-19
lines changed

2 files changed

+76
-19
lines changed

src/main/java/com/thealgorithms/misc/ThreeSumProblem.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,10 @@
77
import java.util.HashSet;
88
import java.util.LinkedHashSet;
99
import java.util.List;
10-
import java.util.Scanner;
1110
import java.util.Set;
1211

1312
public class ThreeSumProblem {
1413

15-
public static void main(String[] args) {
16-
Scanner scan = new Scanner(System.in);
17-
System.out.print("Enter the target sum ");
18-
int ts = scan.nextInt();
19-
System.out.print("Enter the number of elements in the array ");
20-
int n = scan.nextInt();
21-
System.out.println("Enter all your array elements:");
22-
int[] arr = new int[n];
23-
for (int i = 0; i < n; i++) {
24-
arr[i] = scan.nextInt();
25-
}
26-
ThreeSumProblem th = new ThreeSumProblem();
27-
System.out.println("Brute Force Approach\n" + (th.bruteForce(arr, ts)) + "\n");
28-
System.out.println("Two Pointer Approach\n" + (th.twoPointer(arr, ts)) + "\n");
29-
System.out.println("Hashmap Approach\n" + (th.hashMap(arr, ts)));
30-
scan.close();
31-
}
32-
3314
public List<List<Integer>> bruteForce(int[] nums, int target) {
3415
List<List<Integer>> arr = new ArrayList<List<Integer>>();
3516

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)