From 7caa947c88dbfc24ed65201cf5f1a38e40889ef7 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Wed, 9 Oct 2024 11:27:20 +0530 Subject: [PATCH 1/4] Add tests, add `IllegalArgumentException` in `RodCutting` --- .../dynamicprogramming/RodCutting.java | 4 + .../dynamicprogramming/RodCuttingTest.java | 103 ++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java index f56fc4ff5641..76b341e2c823 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java @@ -15,9 +15,13 @@ private 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; 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 000000000000..a1a378c89bc3 --- /dev/null +++ b/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java @@ -0,0 +1,103 @@ +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 = {}; // Empty prices array + int length = 5; // Any length + assertThrows(IllegalArgumentException.class, () -> RodCutting.cutRod(prices, length), + "An empty prices array should throw an IllegalArgumentException."); + } +} From aac16d770ca5ee9330a8728f89aea453faa95c05 Mon Sep 17 00:00:00 2001 From: Hardvan Date: Wed, 9 Oct 2024 05:57:38 +0000 Subject: [PATCH 2/4] Update directory --- DIRECTORY.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 6042dd1b5e0d..ccf8b49a56ba 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -31,6 +31,7 @@ * [IsPowerTwo](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IsPowerTwo.java) * [LowestSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java) * [NonRepeatingNumberFinder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinder.java) + * [NumberAppearingOddTimes](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimes.java) * [NumbersDifferentSigns](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumbersDifferentSigns.java) * [ReverseBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java) * [SingleBitOperations](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SingleBitOperations.java) @@ -638,6 +639,7 @@ * [IsPowerTwoTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IsPowerTwoTest.java) * [LowestSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java) * [NonRepeatingNumberFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinderTest.java) + * [NumberAppearingOddTimesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimesTest.java) * [NumbersDifferentSignsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumbersDifferentSignsTest.java) * [ReverseBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java) * [SingleBitOperationsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleBitOperationsTest.java) @@ -796,6 +798,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) From 80ebe7afdfb548e718d029850e0d731cc7dfe980 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Wed, 9 Oct 2024 11:31:41 +0530 Subject: [PATCH 3/4] Fix --- .../com/thealgorithms/dynamicprogramming/RodCuttingTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java index a1a378c89bc3..3a106234564d 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java @@ -95,8 +95,8 @@ void testCutRodLength0() { */ @Test void testCutRodEmptyPrices() { - int[] prices = {}; // Empty prices array - int length = 5; // Any length + int[] prices = {}; + int length = 5; assertThrows(IllegalArgumentException.class, () -> RodCutting.cutRod(prices, length), "An empty prices array should throw an IllegalArgumentException."); } From 76807540f52ebd1d9548ec58269a51ff918fc10d Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Wed, 9 Oct 2024 11:33:01 +0530 Subject: [PATCH 4/4] Fix --- .../dynamicprogramming/RodCuttingTest.java | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java index 3a106234564d..39497a768397 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java @@ -20,8 +20,7 @@ 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."); + assertEquals(expectedValue, RodCutting.cutRod(prices, length), "The maximum obtainable value for a rod of length 1 should be 1."); } /** @@ -33,8 +32,7 @@ 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."); + assertEquals(expectedValue, RodCutting.cutRod(prices, length), "The maximum obtainable value for a rod of length 2 should be 5."); } /** @@ -46,8 +44,7 @@ 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."); + assertEquals(expectedValue, RodCutting.cutRod(prices, length), "The maximum obtainable value for a rod of length 3 should be 8."); } /** @@ -59,8 +56,7 @@ 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."); + assertEquals(expectedValue, RodCutting.cutRod(prices, length), "The maximum obtainable value for a rod of length 4 should be 10."); } /** @@ -72,8 +68,7 @@ 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."); + assertEquals(expectedValue, RodCutting.cutRod(prices, length), "The maximum obtainable value for a rod of length 5 should be 13."); } /** @@ -85,8 +80,7 @@ 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."); + assertEquals(expectedValue, RodCutting.cutRod(prices, length), "The maximum obtainable value for a rod of length 0 should be 0."); } /** @@ -97,7 +91,6 @@ void testCutRodLength0() { void testCutRodEmptyPrices() { int[] prices = {}; int length = 5; - assertThrows(IllegalArgumentException.class, () -> RodCutting.cutRod(prices, length), - "An empty prices array should throw an IllegalArgumentException."); + assertThrows(IllegalArgumentException.class, () -> RodCutting.cutRod(prices, length), "An empty prices array should throw an IllegalArgumentException."); } }