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
|
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class FractionalKnapsackTest {
@Test
void test1() {
int[] values = new int[] { 60, 100, 120 };
int[] weights = new int[] { 20, 50, 30 };
int capacity = 50;
assertEquals(180.0000, FractionalKnapsack.getOptimalValue(capacity, values, weights));
}
@Test
void test2() {
int[] values = new int[] { 60, 100, 120 };
int[] weights = new int[] { 20, 50, 30 };
int[] sortedIndexArray = new int[] { 1, 0, 2 };
assertArrayEquals(sortedIndexArray, FractionalKnapsack.getSortedIndexArray(values, weights));
}
@Test
void test3() {
int[] values = new int[] { 500};
int[] weights = new int[] { 30 };
int capacity = 10;
assertEquals(166.6667, FractionalKnapsack.getOptimalValue(capacity, values, weights), 0.0001);
}
}
|