Skip to content

Added test cases for RodCuttingTest.java #4744

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

Closed
wants to merge 14 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
* obtainable price for a rod of length n and price[] as prices of different
* pieces
*/
public class RodCutting {
public final class RodCutting {

private static int cutRod(int[] price, int n) {
private RodCutting() {
}

public static int cutRod(int[] price, int n) {
int[] val = new int[n + 1];
val[0] = 0;

Expand All @@ -22,11 +25,4 @@ private static int cutRod(int[] price, int n) {

return val[n];
}

// main function to test
public static void main(String[] args) {
int[] arr = new int[] {2, 5, 13, 19, 20};
int result = cutRod(arr, arr.length);
System.out.println("Maximum Obtainable Value is " + result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.thealgorithms.dynamicprogramming;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

public class RodCuttingTest {

@Test
public void testCutRodWithSinglePiece() {
// Test case 1: Rod length is 1 with a single piece
int[] price1 = {2};
assertEquals(2, RodCutting.cutRod(price1, 1));
}

@Test
public void testCutRodWithTwoPieces() {
// Test case 2: Rod length is 2 with two pieces
int[] price2 = {2, 5};
assertEquals(5, RodCutting.cutRod(price2, 2));
}

@Test
public void testCutRodWithThreePieces() {
// Test case 3: Rod length is 3 with three pieces
int[] price3 = {2, 5, 13};
assertEquals(13, RodCutting.cutRod(price3, 3));
}

@Test
public void testCutRodWithFourPieces() {
// Test case 4: Rod length is 4 with four pieces
int[] price4 = {2, 5, 13, 19};
assertEquals(19, RodCutting.cutRod(price4, 4));
}

@Test
public void testCutRodWithFivePieces() {
// Test case 5: Rod length is 5 with five pieces
int[] price5 = {2, 5, 13, 19, 20};
assertEquals(21, RodCutting.cutRod(price5, 5));
}

@Test
public void testCutRodWithNegativeInput() {
// Test case 6: Negative rod length (should throw an IllegalArgumentException)
int[] negativePrice = {2, 5, 13};
assertThrows(NegativeArraySizeException.class, () -> RodCutting.cutRod(negativePrice, -3));

// Test case 7: Negative prices in the price array (should return 13)
int[] negativePrices = {2, -5, 13};
assertEquals(13, RodCutting.cutRod(negativePrices, 3));
}

@Test
public void testCutRodWithZeroLength() {
// Test case 8: Rod length is greater than the length of the price array (invalid input)
int[] price6 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
assertThrows(ArrayIndexOutOfBoundsException.class, () -> RodCutting.cutRod(price6, 20));

// Test case 9: Rod length is 0 (should return 0 as the rod cannot be cut)
int[] emptyPrice = {};
assertEquals(0, RodCutting.cutRod(emptyPrice, 0));
}

@Test
public void testCutRodWithLargeInput() {
// Test case 10: Large rod length
int[] price7 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
assertEquals(10, RodCutting.cutRod(price7, 10));
}
Comment on lines +9 to +71
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did you verify these test cases?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sunilnitdgp this is still pending.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide a source of the test data or the method used to create it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sunilnitdgp what is the progress?

}