mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 01:35:16 +08:00
Add tests, add IllegalArgumentException
in RodCutting
(#5661)
This commit is contained in:
@ -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)
|
||||
|
@ -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;
|
||||
|
@ -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.");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user