summaryrefslogtreecommitdiff
path: root/AlgoDesignAndTechniqueEdxJava/tests/FractionalKnapsackTest.java
blob: 6215219056bf7e96215fc30553441de4aae882c3 (plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

class FractionalKnapsackTest {

	@Test
	void testGetSortedIndexArray() {
		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 testGetBestItem1() {
		int[] values = new int[] { 60, 100, 120 };
		int[] weights = new int[] { 20, 50, 30 };
		assertEquals(2, FractionalKnapsack.getBestItem(values, weights));
	}

	@Test
	void testGetBestItem2() {
		int[] values = new int[] { 500 };
		int[] weights = new int[] { 30 };
		assertEquals(0, FractionalKnapsack.getBestItem(values, weights));
	}

	@Test
	void testNaive1() {
		int[] values = new int[] { 60, 100, 120 };
		int[] weights = new int[] { 20, 50, 30 };
		int capacity = 50;
		assertEquals(180.0000, FractionalKnapsack.getOptimalValueNaive(capacity, values, weights));
	}


	@Test
	void testNaive2() {
		int[] values = new int[] { 500 };
		int[] weights = new int[] { 30 };
		int capacity = 10;
		assertEquals(166.6667, FractionalKnapsack.getOptimalValueNaive(capacity, values, weights), 0.0001);
	}

	@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[] { 500 };
		int[] weights = new int[] { 30 };
		int capacity = 10;
		assertEquals(166.6667, FractionalKnapsack.getOptimalValue(capacity, values, weights), 0.0001);
	}

	@Test
	void testStress1() {
		int[] values = new int[] { 11, 43, 4, 35, 21 };
		int[] weights = new int[] { 45, 11, 40, 19, 10 };
		int capacity = 50;
		assertEquals(101.4444, FractionalKnapsack.getOptimalValue(capacity, values, weights), 0.0001);
	}

	@Test
	void testStress2() {
		int[] values = new int[] { 11, 43, 4, 35, 21 };
		int[] weights = new int[] { 45, 11, 40, 19, 10 };
		int capacity = 50;
		assertEquals(101.4444, FractionalKnapsack.getOptimalValueNaive(capacity, values, weights), 0.0001);
	}

	@Test
	void testStress3() {
		int[] values = new int[] { 44, 26, 31 };
		int[] weights = new int[] { 6, 28, 38 };
		int capacity = 50;
		assertEquals(83.0526, FractionalKnapsack.getOptimalValue(capacity, values, weights), 0.0001);
	}

}