-
Notifications
You must be signed in to change notification settings - Fork 20k
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
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7b09785
Add tests, remove `main` in `RangeInSortedArray`
Hardvan 05193b6
Update directory
Hardvan 85d76b9
Fix
Hardvan d7ab24b
Merge remote-tracking branch 'origin/three_sum_add_tests' into three_…
Hardvan 10e7bc7
Merge branch 'master' into three_sum_add_tests
Hardvan 2bec18f
Update directory
Hardvan 30fa372
Fix
Hardvan 630f6c7
Merge remote-tracking branch 'origin/three_sum_add_tests' into three_…
Hardvan 91420eb
Fix
Hardvan 335598e
Merge branch 'master' into three_sum_add_tests
alxkm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/test/java/com/thealgorithms/misc/ThreeSumProblemTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.