Skip to content

Commit c18dbc4

Browse files
authored
Add tests, add IllegalArgumentException in RodCutting (#5661)
1 parent 41f7e6a commit c18dbc4

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,7 @@
824824
* [PalindromicPartitioningTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/PalindromicPartitioningTest.java)
825825
* [PartitionProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/PartitionProblemTest.java)
826826
* [RegexMatchingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/RegexMatchingTest.java)
827+
* [RodCuttingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java)
827828
* [ShortestCommonSupersequenceLengthTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/ShortestCommonSupersequenceLengthTest.java)
828829
* [SubsetCountTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/SubsetCountTest.java)
829830
* [SubsetSumTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/SubsetSumTest.java)

src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ private RodCutting() {
1515
* @param price An array representing the prices of different pieces, where price[i-1]
1616
* represents the price of a piece of length i.
1717
* @param n The length of the rod to be cut.
18+
* @throws IllegalArgumentException if the price array is null or empty, or if n is less than 0.
1819
* @return The maximum obtainable value.
1920
*/
2021
public static int cutRod(int[] price, int n) {
22+
if (price == null || price.length == 0) {
23+
throw new IllegalArgumentException("Price array cannot be null or empty.");
24+
}
2125
// Create an array to store the maximum obtainable values for each rod length.
2226
int[] val = new int[n + 1];
2327
val[0] = 0;
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
/**
9+
* Unit tests for the RodCutting class.
10+
* This test class verifies the correctness of the cutRod method for various input cases.
11+
*/
12+
class RodCuttingTest {
13+
14+
/**
15+
* Test case for cutting a rod of length 1.
16+
* The expected maximum obtainable value is the price of the piece of length 1.
17+
*/
18+
@Test
19+
void testCutRodLength1() {
20+
int[] prices = {1}; // Price for piece of length 1
21+
int length = 1;
22+
int expectedValue = 1;
23+
assertEquals(expectedValue, RodCutting.cutRod(prices, length), "The maximum obtainable value for a rod of length 1 should be 1.");
24+
}
25+
26+
/**
27+
* Test case for cutting a rod of length 2.
28+
* The expected maximum obtainable value is the best price combination for length 2.
29+
*/
30+
@Test
31+
void testCutRodLength2() {
32+
int[] prices = {1, 5}; // Prices for lengths 1 and 2
33+
int length = 2;
34+
int expectedValue = 5; // Best value is to cut it into a single piece of length 2
35+
assertEquals(expectedValue, RodCutting.cutRod(prices, length), "The maximum obtainable value for a rod of length 2 should be 5.");
36+
}
37+
38+
/**
39+
* Test case for cutting a rod of length 3.
40+
* The expected maximum obtainable value is the best price combination for length 3.
41+
*/
42+
@Test
43+
void testCutRodLength3() {
44+
int[] prices = {1, 5, 8}; // Prices for lengths 1, 2, and 3
45+
int length = 3;
46+
int expectedValue = 8; // Best value is to cut it into a single piece of length 3
47+
assertEquals(expectedValue, RodCutting.cutRod(prices, length), "The maximum obtainable value for a rod of length 3 should be 8.");
48+
}
49+
50+
/**
51+
* Test case for cutting a rod of length 4.
52+
* The expected maximum obtainable value is the best price combination for length 4.
53+
*/
54+
@Test
55+
void testCutRodLength4() {
56+
int[] prices = {1, 5, 8, 9}; // Prices for lengths 1, 2, 3, and 4
57+
int length = 4;
58+
int expectedValue = 10; // Best value is to cut it into two pieces of length 2
59+
assertEquals(expectedValue, RodCutting.cutRod(prices, length), "The maximum obtainable value for a rod of length 4 should be 10.");
60+
}
61+
62+
/**
63+
* Test case for cutting a rod of length 5.
64+
* The expected maximum obtainable value is the best price combination for length 5.
65+
*/
66+
@Test
67+
void testCutRodLength5() {
68+
int[] prices = {1, 5, 8, 9, 10}; // Prices for lengths 1, 2, 3, 4, and 5
69+
int length = 5;
70+
int expectedValue = 13; // Best value is to cut it into pieces of lengths 2 and 3
71+
assertEquals(expectedValue, RodCutting.cutRod(prices, length), "The maximum obtainable value for a rod of length 5 should be 13.");
72+
}
73+
74+
/**
75+
* Test case for cutting a rod of length 0.
76+
* The expected maximum obtainable value should be 0 since the rod has no length.
77+
*/
78+
@Test
79+
void testCutRodLength0() {
80+
int[] prices = {1, 5, 8, 9, 10}; // Prices are irrelevant for length 0
81+
int length = 0;
82+
int expectedValue = 0; // No value obtainable from a rod of length 0
83+
assertEquals(expectedValue, RodCutting.cutRod(prices, length), "The maximum obtainable value for a rod of length 0 should be 0.");
84+
}
85+
86+
/**
87+
* Test case for an empty prices array.
88+
* The expected maximum obtainable value should still be 0 for any length.
89+
*/
90+
@Test
91+
void testCutRodEmptyPrices() {
92+
int[] prices = {};
93+
int length = 5;
94+
assertThrows(IllegalArgumentException.class, () -> RodCutting.cutRod(prices, length), "An empty prices array should throw an IllegalArgumentException.");
95+
}
96+
}

0 commit comments

Comments
 (0)