Repository: TheAlgorithms/Java
Pull request #5661 (2024-10-09 05:57:37)

Add tests, add `IllegalArgumentException` in `RodCutting`
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5661?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `50.00000%` with `1 line` in your changes missing coverage. Please review.
> Project coverage is 58.87%. Comparing base [(`b54cc21`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/b54cc21ade112e20f429964dc86f8543a5605d98?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`7680754`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/76807540f52ebd1d9548ec58269a51ff918fc10d?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).
> Report is 2 commits behind head on master.

| [Files with missing lines](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5661?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...m/thealgorithms/dynamicprogramming/RodCutting.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5661?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fdynamicprogramming%2FRodCutting.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9keW5hbWljcHJvZ3JhbW1pbmcvUm9kQ3V0dGluZy5qYXZh) | 50.00% | [0 Missing and 1 partial :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5661?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) |

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5661      +/-   ##
============================================
+ Coverage     58.59%   58.87%   +0.27%     
- Complexity     3749     3771      +22     
============================================
  Files           554      555       +1     
  Lines         15922    15932      +10     
  Branches       3030     3036       +6     
============================================
+ Hits           9330     9380      +50     
+ Misses         6195     6148      -47     
- Partials        397      404       +7     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5661?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/DIRECTORY.md b/DIRECTORY.md
index 4afe7c78..fb5ff2fe 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -824,6 +824,7 @@
             * [PalindromicPartitioningTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/PalindromicPartitioningTest.java)
             * [PartitionProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/PartitionProblemTest.java)
             * [RegexMatchingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/RegexMatchingTest.java)
+            * [RodCuttingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java)
             * [ShortestCommonSupersequenceLengthTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/ShortestCommonSupersequenceLengthTest.java)
             * [SubsetCountTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/SubsetCountTest.java)
             * [SubsetSumTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/SubsetSumTest.java)
diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java
index f56fc4ff..76b341e2 100644
--- a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java
+++ b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java
@@ -15,9 +15,13 @@ public final class RodCutting {
      * @param price An array representing the prices of different pieces, where price[i-1]
      *              represents the price of a piece of length i.
      * @param n     The length of the rod to be cut.
+     * @throws IllegalArgumentException if the price array is null or empty, or if n is less than 0.
      * @return The maximum obtainable value.
      */
     public static int cutRod(int[] price, int n) {
+        if (price == null || price.length == 0) {
+            throw new IllegalArgumentException("Price array cannot be null or empty.");
+        }
         // Create an array to store the maximum obtainable values for each rod length.
         int[] val = new int[n + 1];
         val[0] = 0;


Test patch:
diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java
new file mode 100644
index 00000000..39497a76
--- /dev/null
+++ b/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java
@@ -0,0 +1,96 @@
+package com.thealgorithms.dynamicprogramming;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Unit tests for the RodCutting class.
+ * This test class verifies the correctness of the cutRod method for various input cases.
+ */
+class RodCuttingTest {
+
+    /**
+     * Test case for cutting a rod of length 1.
+     * The expected maximum obtainable value is the price of the piece of length 1.
+     */
+    @Test
+    void testCutRodLength1() {
+        int[] prices = {1}; // Price for piece of length 1
+        int length = 1;
+        int expectedValue = 1;
+        assertEquals(expectedValue, RodCutting.cutRod(prices, length), "The maximum obtainable value for a rod of length 1 should be 1.");
+    }
+
+    /**
+     * Test case for cutting a rod of length 2.
+     * The expected maximum obtainable value is the best price combination for length 2.
+     */
+    @Test
+    void testCutRodLength2() {
+        int[] prices = {1, 5}; // Prices for lengths 1 and 2
+        int length = 2;
+        int expectedValue = 5; // Best value is to cut it into a single piece of length 2
+        assertEquals(expectedValue, RodCutting.cutRod(prices, length), "The maximum obtainable value for a rod of length 2 should be 5.");
+    }
+
+    /**
+     * Test case for cutting a rod of length 3.
+     * The expected maximum obtainable value is the best price combination for length 3.
+     */
+    @Test
+    void testCutRodLength3() {
+        int[] prices = {1, 5, 8}; // Prices for lengths 1, 2, and 3
+        int length = 3;
+        int expectedValue = 8; // Best value is to cut it into a single piece of length 3
+        assertEquals(expectedValue, RodCutting.cutRod(prices, length), "The maximum obtainable value for a rod of length 3 should be 8.");
+    }
+
+    /**
+     * Test case for cutting a rod of length 4.
+     * The expected maximum obtainable value is the best price combination for length 4.
+     */
+    @Test
+    void testCutRodLength4() {
+        int[] prices = {1, 5, 8, 9}; // Prices for lengths 1, 2, 3, and 4
+        int length = 4;
+        int expectedValue = 10; // Best value is to cut it into two pieces of length 2
+        assertEquals(expectedValue, RodCutting.cutRod(prices, length), "The maximum obtainable value for a rod of length 4 should be 10.");
+    }
+
+    /**
+     * Test case for cutting a rod of length 5.
+     * The expected maximum obtainable value is the best price combination for length 5.
+     */
+    @Test
+    void testCutRodLength5() {
+        int[] prices = {1, 5, 8, 9, 10}; // Prices for lengths 1, 2, 3, 4, and 5
+        int length = 5;
+        int expectedValue = 13; // Best value is to cut it into pieces of lengths 2 and 3
+        assertEquals(expectedValue, RodCutting.cutRod(prices, length), "The maximum obtainable value for a rod of length 5 should be 13.");
+    }
+
+    /**
+     * Test case for cutting a rod of length 0.
+     * The expected maximum obtainable value should be 0 since the rod has no length.
+     */
+    @Test
+    void testCutRodLength0() {
+        int[] prices = {1, 5, 8, 9, 10}; // Prices are irrelevant for length 0
+        int length = 0;
+        int expectedValue = 0; // No value obtainable from a rod of length 0
+        assertEquals(expectedValue, RodCutting.cutRod(prices, length), "The maximum obtainable value for a rod of length 0 should be 0.");
+    }
+
+    /**
+     * Test case for an empty prices array.
+     * The expected maximum obtainable value should still be 0 for any length.
+     */
+    @Test
+    void testCutRodEmptyPrices() {
+        int[] prices = {};
+        int length = 5;
+        assertThrows(IllegalArgumentException.class, () -> RodCutting.cutRod(prices, length), "An empty prices array should throw an IllegalArgumentException.");
+    }
+}