Skip to content

Add tests, add IllegalArgumentException in RodCutting #5661

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 7 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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.");
}
}