From 55f08cc0139579760964e786364e2239cc65a8d9 Mon Sep 17 00:00:00 2001 From: Bhishmadev Ghosh <111000117+bhishma620@users.noreply.github.com> Date: Sat, 27 Jan 2024 00:00:26 +0530 Subject: [PATCH] Add tests `SumOfSubset` (#5021) * Updated main and test * removed * style: reorder test cases --------- Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> --- .../{Sum_Of_Subset.java => SumOfSubset.java} | 13 +------------ .../dynamicprogramming/SumOfSubsetTest.java | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 12 deletions(-) rename src/main/java/com/thealgorithms/dynamicprogramming/{Sum_Of_Subset.java => SumOfSubset.java} (54%) create mode 100644 src/test/java/com/thealgorithms/dynamicprogramming/SumOfSubsetTest.java 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) { 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)); + } +}