Repository: TheAlgorithms/Java
Pull request #5021 (2024-01-21 16:43:15)

Add tests `SumOfSubset`
This pull request introduces the implementation of the "Sum of Subset" algorithm in the file sum_of_subset.java. The Sum of Subset problem involves finding subsets of a given set whose sum matches a specific target sum.It Reduces time complexity from O(2^N) to O(n^2) by using extra space O(N).



vil02: @bhishma620 thanks for your contribution. This case is quite delicate - you are changing the code, which is not tested. Therefore I suggest the following approach:
- add tests for existing implementation,
- change the implementation of the algorithms and make sure that the old tests pass.

Feel free to do each of the steps in separate pull-requests.

Besides that, please make sure that all of the CI checks pass. If you have problems with  `clang-format`, please have a look here: https://github.com/TheAlgorithms/Java/pull/4438#issuecomment-1741399556

In case of any questions: please do not hesitate - just ask them. 
bhishma620: > @bhishma620 thanks for your contribution. This case is quite delicate - you are changing the code, which is not tested. Therefore I suggest the following approach:
> 
> * add tests for existing implementation,
> * change the implementation of the algorithms and make sure that the old tests pass.
> 
> Feel free to do each of the steps in separate pull-requests.
> 
> Besides that, please make sure that all of the CI checks pass. If you have problems with `clang-format`, please have a look here: [#4438 (comment)](https://github.com/TheAlgorithms/Java/pull/4438#issuecomment-1741399556)
> 
> In case of any questions: please do not hesitate - just ask them.



> @bhishma620 thanks for your contribution. This case is quite delicate - you are changing the code, which is not tested. Therefore I suggest the following approach:
> 
> * add tests for existing implementation,
> * change the implementation of the algorithms and make sure that the old tests pass.
> 
> Feel free to do each of the steps in separate pull-requests.
> 
> Besides that, please make sure that all of the CI checks pass. If you have problems with `clang-format`, please have a look here: [#4438 (comment)](https://github.com/TheAlgorithms/Java/pull/4438#issuecomment-1741399556)
> 
> In case of any questions: please do not hesitate - just ask them.

Can you tell me how can i add tests ?
You said that to change the implementation of my algorithm , is there any problem with that algorithm?



vil02: > Can you tell me how can i add tests?

You did quite well. Please have a look on the review (this is only the 1st iteration).

> You said that to change the implementation of my algorithm , is there any problem with that algorithm?

I am not sure if there is a problem. But to be safe: always write test first before touching the implementation.


bhishma620: Not able to pass Build test for Test file , how to resolve ?

bhishma620: can you check now, i forgot to remove the main method from the SubOfSubset, is it necessary?
vil02: > can you check now, i forgot to remove the main method from the SubOfSubset, is it necessary?

There is not reason here to have the `main` method.
bhishma620: Can you check now?

Patch:
diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/Sum_Of_Subset.java b/src/main/java/com/thealgorithms/dynamicprogramming/SumOfSubset.java
similarity index 54%
rename from src/main/java/com/thealgorithms/dynamicprogramming/Sum_Of_Subset.java
rename to src/main/java/com/thealgorithms/dynamicprogramming/SumOfSubset.java
index 90c07889..622f8b14 100644
--- a/src/main/java/com/thealgorithms/dynamicprogramming/Sum_Of_Subset.java
+++ b/src/main/java/com/thealgorithms/dynamicprogramming/SumOfSubset.java
@@ -1,17 +1,6 @@
 package com.thealgorithms.dynamicprogramming;
 
-public class Sum_Of_Subset {
-
-    public static void main(String[] args) {
-        int[] arr = {7, 3, 2, 5, 8};
-        int Key = 14;
-
-        if (subsetSum(arr, arr.length - 1, Key)) {
-            System.out.print("Yes, that sum exists");
-        } else {
-            System.out.print("Nope, that number does not exist");
-        }
-    }
+public class SumOfSubset {
 
     public static boolean subsetSum(int[] arr, int num, int Key) {
         if (Key == 0) {


Test patch:
diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/SumOfSubsetTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/SumOfSubsetTest.java
new file mode 100644
index 00000000..53c34937
--- /dev/null
+++ b/src/test/java/com/thealgorithms/dynamicprogramming/SumOfSubsetTest.java
@@ -0,0 +1,17 @@
+package com.thealgorithms.dynamicprogramming;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Test;
+
+class SumOfSubsetTest {
+
+    @Test
+    void basicCheck() {
+        assertEquals(false, SumOfSubset.subsetSum(new int[] {1, 2, 7, 10, 9}, 4, 14));
+        assertEquals(false, SumOfSubset.subsetSum(new int[] {2, 15, 1, 6, 7}, 4, 4));
+        assertEquals(true, SumOfSubset.subsetSum(new int[] {7, 3, 2, 5, 8}, 4, 14));
+        assertEquals(true, SumOfSubset.subsetSum(new int[] {4, 3, 2, 1}, 3, 5));
+        assertEquals(true, SumOfSubset.subsetSum(new int[] {1, 7, 2, 9, 10}, 4, 13));
+    }
+}