|
| 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 | +public class KnapsackTest { |
| 9 | + @Test |
| 10 | + public void testKnapSackBasic() { |
| 11 | + int[] weights = {2, 3, 4, 5}; |
| 12 | + int[] values = {3, 4, 5, 6}; |
| 13 | + int weightCapacity = 5; |
| 14 | + int expected = 7; // Maximum value should be 7 (items 1 and 4). |
| 15 | + int result = Knapsack.knapSack(weightCapacity, weights, values); |
| 16 | + assertEquals(expected, result); |
| 17 | + } |
| 18 | + |
| 19 | + @Test |
| 20 | + public void testKnapSackEmpty() { |
| 21 | + int[] weights = {}; |
| 22 | + int[] values = {}; |
| 23 | + int weightCapacity = 10; |
| 24 | + int expected = 0; // With no items, the result should be 0. |
| 25 | + int result = Knapsack.knapSack(weightCapacity, weights, values); |
| 26 | + assertEquals(expected, result); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void testKnapSackNoCapacity() { |
| 31 | + int[] weights = {2, 3, 4}; |
| 32 | + int[] values = {3, 4, 5}; |
| 33 | + int weightCapacity = 0; |
| 34 | + int expected = 0; // With no capacity, the result should be 0. |
| 35 | + int result = Knapsack.knapSack(weightCapacity, weights, values); |
| 36 | + assertEquals(expected, result); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testKnapSackMaxCapacity() { |
| 41 | + int[] weights = {2, 3, 4, 5}; |
| 42 | + int[] values = {3, 4, 5, 6}; |
| 43 | + int weightCapacity = 10; |
| 44 | + int expected = 13; // Maximum value should be 13 (items 1, 3, and 4). |
| 45 | + int result = Knapsack.knapSack(weightCapacity, weights, values); |
| 46 | + assertEquals(expected, result); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testKnapSackThrowsForInputsOfDifferentLength() { |
| 51 | + int[] weights = {2, 3, 4}; |
| 52 | + int[] values = {3, 4, 5, 6}; // Different length values array. |
| 53 | + int weightCapacity = 5; |
| 54 | + assertThrows(IllegalArgumentException.class, () -> { Knapsack.knapSack(weightCapacity, weights, values); }); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testKnapSackThrowsForNullInputs() { |
| 59 | + int[] weights = {2, 3, 4}; |
| 60 | + int[] values = {3, 4, 6}; |
| 61 | + int weightCapacity = 5; |
| 62 | + assertThrows(IllegalArgumentException.class, () -> { Knapsack.knapSack(weightCapacity, null, values); }); |
| 63 | + assertThrows(IllegalArgumentException.class, () -> { Knapsack.knapSack(weightCapacity, weights, null); }); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testKnapSackThrowsForNegativeCapacity() { |
| 68 | + int[] weights = {2, 3, 4, 5}; |
| 69 | + int[] values = {3, 4, 5, 6}; |
| 70 | + int weightCapacity = -5; |
| 71 | + assertThrows(IllegalArgumentException.class, () -> { Knapsack.knapSack(weightCapacity, weights, values); }); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testKnapSackThrowsForNegativeWeight() { |
| 76 | + int[] weights = {2, 0, 4}; |
| 77 | + int[] values = {3, 4, 6}; |
| 78 | + int weightCapacity = 5; |
| 79 | + assertThrows(IllegalArgumentException.class, () -> { Knapsack.knapSack(weightCapacity, weights, values); }); |
| 80 | + } |
| 81 | +} |
0 commit comments