|
| 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