forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZeroOneKnapsackTest.java
More file actions
55 lines (48 loc) · 1.56 KB
/
ZeroOneKnapsackTest.java
File metadata and controls
55 lines (48 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.thealgorithms.dynamicprogramming;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
/**
* Test class for {@code ZeroOneKnapsack}.
*/
public class ZeroOneKnapsackTest {
/**
* Tests the knapsack computation for a basic example.
*/
@Test
public void testKnapsackBasic() {
int[] val = {15, 14, 10, 45, 30};
int[] wt = {2, 5, 1, 3, 4};
int W = 7;
assertEquals(75, ZeroOneKnapsack.compute(val, wt, W, val.length), "Expected maximum value is 75.");
}
/**
* Tests the knapsack computation when the knapsack capacity is zero.
*/
@Test
public void testZeroCapacity() {
int[] val = {10, 20, 30};
int[] wt = {1, 1, 1};
int W = 0;
assertEquals(0, ZeroOneKnapsack.compute(val, wt, W, val.length), "Expected maximum value is 0 for zero capacity.");
}
/**
* Tests the knapsack computation when there are no items.
*/
@Test
public void testNoItems() {
int[] val = {};
int[] wt = {};
int W = 10;
assertEquals(0, ZeroOneKnapsack.compute(val, wt, W, 0), "Expected maximum value is 0 when no items are available.");
}
/**
* Tests the knapsack computation when items exactly fit the capacity.
*/
@Test
public void testExactFit() {
int[] val = {60, 100, 120};
int[] wt = {10, 20, 30};
int W = 50;
assertEquals(220, ZeroOneKnapsack.compute(val, wt, W, val.length), "Expected maximum value is 220 for exact fit.");
}
}