Repository: TheAlgorithms/Java
File: src/test/java/com/thealgorithms/dynamicprogramming/KnapsackTest.java (Java)

Code before:
    public void testKnapSackNoCapacity() {
        int[] weights = {2, 3, 4};
        int[] values = {3, 4, 5};
        int weightCapacity = 0;
        int expected = 0; // With no capacity, the result should be 0.
        int result = Knapsack.knapSack(weightCapacity, weights, values);
        assertEquals(expected, result);
    }

    @Test
    public void testKnapSackMaxCapacity() {
        int[] weights = {2, 3, 4, 5};
        int[] values = {3, 4, 5, 6};
        int weightCapacity = 10;
        int expected = 13; // Maximum value should be 13 (items 1, 3, and 4).
        int result = Knapsack.knapSack(weightCapacity, weights, values);
        assertEquals(expected, result);
    }

    @Test
    public void testKnapSackInvalidInput() {
        int[] weights = {2, 3, 4};
        int[] values = {3, 4, 5, 6}; // Different length values array.
        int weightCapacity = 5;
        // This test should throw an IllegalArgumentException.
        assertThrows(IllegalArgumentException.class, () -> { Knapsack.knapSack(weightCapacity, weights, values); });
    }

    @Test
    public void testKnapSackNullInput() {
        int[] weights = {2, 3, 4};
        int[] values = {3, 4, 6};
        int weightCapacity = 5;
        // This test should throw an IllegalArgumentException.
        assertThrows(IllegalArgumentException.class, () -> { Knapsack.knapSack(weightCapacity, null, values); });
        assertThrows(IllegalArgumentException.class, () -> { Knapsack.knapSack(weightCapacity, weights, null); });
    }
}


Review comment (suggestion):
```suggestion
    @Test
    public void testKnapSackThrowsForInputsOfDifferentLength() {
        int[] weights = {2, 3, 4};
        int[] values = {3, 4, 5, 6}; // Different length values array.
        int weightCapacity = 5;
        assertThrows(IllegalArgumentException.class, () -> { Knapsack.knapSack(weightCapacity, weights, values); });
    }
```

Code after:
    public void testKnapSackNoCapacity() {
        int[] weights = {2, 3, 4};
        int[] values = {3, 4, 5};
        int weightCapacity = 0;
        int expected = 0; // With no capacity, the result should be 0.
        int result = Knapsack.knapSack(weightCapacity, weights, values);
        assertEquals(expected, result);
    }

    @Test
    public void testKnapSackMaxCapacity() {
        int[] weights = {2, 3, 4, 5};
        int[] values = {3, 4, 5, 6};
        int weightCapacity = 10;
        int expected = 13; // Maximum value should be 13 (items 1, 3, and 4).
        int result = Knapsack.knapSack(weightCapacity, weights, values);
        assertEquals(expected, result);
    }

    @Test
    public void testKnapSackThrowsForInputsOfDifferentLength() {
        int[] weights = {2, 3, 4};
        int[] values = {3, 4, 5, 6}; // Different length values array.
        int weightCapacity = 5;
        assertThrows(IllegalArgumentException.class, () -> { Knapsack.knapSack(weightCapacity, weights, values); });
    }

    @Test
    public void testKnapSackThrowsForNullInputs() {
        int[] weights = {2, 3, 4};
        int[] values = {3, 4, 6};
        int weightCapacity = 5;
        assertThrows(IllegalArgumentException.class, () -> { Knapsack.knapSack(weightCapacity, null, values); });
        assertThrows(IllegalArgumentException.class, () -> { Knapsack.knapSack(weightCapacity, weights, null); });
    }

    @Test
    public void testKnapSackThrowsForNegativeCapacity() {
        int[] weights = {2, 3, 4, 5};
        int[] values = {3, 4, 5, 6};
        int weightCapacity = -5;
        assertThrows(IllegalArgumentException.class, () -> { Knapsack.knapSack(weightCapacity, weights, values); });
    }

    @Test
    public void testKnapSackThrowsForNegativeWeight() {
        int[] weights = {2, 0, 4};
        int[] values = {3, 4, 6};
        int weightCapacity = 5;
        assertThrows(IllegalArgumentException.class, () -> { Knapsack.knapSack(weightCapacity, weights, values); });
    }