diff options
author | Haidong Ji | 2018-09-05 22:19:43 -0500 |
---|---|---|
committer | Haidong Ji | 2018-09-05 22:19:43 -0500 |
commit | bddfc348f5dbf69c004049f74939f2c837cca945 (patch) | |
tree | 8dae6c5c044ffe167cca80c465582af0d62e3641 /AlgoDesignAndTechniqueEdxJava/tests | |
parent | 2a23f32bd6db02252509d0d9fdddc0b44b10056a (diff) |
Buggy FractionalKnapsack for now. Work on a python version first.
I may need to do an exhaustive testing.
Diffstat (limited to 'AlgoDesignAndTechniqueEdxJava/tests')
-rw-r--r-- | AlgoDesignAndTechniqueEdxJava/tests/FractionalKnapsackTest.java | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/AlgoDesignAndTechniqueEdxJava/tests/FractionalKnapsackTest.java b/AlgoDesignAndTechniqueEdxJava/tests/FractionalKnapsackTest.java new file mode 100644 index 0000000..54dd4f7 --- /dev/null +++ b/AlgoDesignAndTechniqueEdxJava/tests/FractionalKnapsackTest.java @@ -0,0 +1,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); + } + +} |