forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZeroOneKnapsackTabTest.java
More file actions
58 lines (47 loc) · 1.6 KB
/
ZeroOneKnapsackTabTest.java
File metadata and controls
58 lines (47 loc) · 1.6 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
56
57
58
package com.thealgorithms.dynamicprogramming;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
/**
* Test class for {@code ZeroOneKnapsackTab}.
*/
public class ZeroOneknapsackTabTest {
/**
* Tests the 0-1 Knapsack tabulation approach with known values.
*/
@Test
public void testKnownValues() {
int[] val = {60, 100, 120};
int[] wt = {10, 20, 30};
int W = 50;
int n = val.length;
// Expected result is 220 (items with weight 20 and 30)
assertEquals(220, ZeroOneKnapsackTab.compute(val, wt, W, n), "Maximum value for capacity 50 should be 220.");
}
@Test
public void testZeroCapacity() {
int[] val = {10, 20, 30};
int[] wt = {1, 1, 1};
int W = 0;
int n = val.length;
// With zero capacity, the result should be 0
assertEquals(0, ZeroOneKnapsackTab.compute(val, wt, W, n), "Maximum value for capacity 0 should be 0.");
}
@Test
public void testZeroItems() {
int[] val = {};
int[] wt = {};
int W = 10;
int n = val.length;
// With no items, the result should be 0
assertEquals(0, ZeroOneKnapsackTab.compute(val, wt, W, n), "Maximum value with no items should be 0.");
}
@Test
public void testExactFit() {
int[] val = {5, 10, 15};
int[] wt = {1, 2, 3};
int W = 6;
int n = val.length;
// All items fit exactly into capacity 6
assertEquals(30, ZeroOneKnapsackTab.compute(val, wt, W, n), "Maximum value for exact fit should be 30.");
}
}