import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Map; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; public class SimpleTest { private static RationalKnapsack rk; private static final double TOL = 1e-8; @BeforeClass public static void init() { rk = new RationalKnapsack(); } @Test public void test1() { List items = Arrays.asList( new KnapsackItem(5.1,6.1), new KnapsackItem(3.0,7.3), new KnapsackItem(4.5,2.0), new KnapsackItem(1.1,2.7), new KnapsackItem(9.0,7.6) ); double capaciteit = 11.0; Map antw = rk.select(Collections.unmodifiableList(items), capaciteit); // Controleren of alle waarden van de sleutels in antw overeenkomen met deze in opl. check(antw, items.get(0), 1.0); check(antw, items.get(1), 1.0); check(antw, items.get(2), 0.0); check(antw, items.get(3), 1.0); check(antw, items.get(4), 0.2); } @Test public void test2() { List items = Arrays.asList( new KnapsackItem(7.1,8.0), new KnapsackItem(3.0,3.0), new KnapsackItem(4.3,3.0), new KnapsackItem(1.0,2.7), new KnapsackItem(1.1,5.6), new KnapsackItem(3.0,3.0), new KnapsackItem(9.0,8.2) ); double capaciteit = 23.0; Map antw = rk.select(Collections.unmodifiableList(items), capaciteit); // Controleren of alle waarden van de sleutels in antw overeenkomen met deze in opl. check(antw, items.get(0), 1.0); check(antw, items.get(1), 1.0); check(antw, items.get(2), 0.0); check(antw, items.get(3), 1.0); check(antw, items.get(4), 1.0); check(antw, items.get(5), 1.0); check(antw, items.get(6), 13.0/15.0); } @Test public void test3() { List items = Arrays.asList( new KnapsackItem(14.5,8.0), new KnapsackItem(9.0,10.0), new KnapsackItem(15.3,3.0), new KnapsackItem(12.0,15.5), new KnapsackItem(14.1,10.6), new KnapsackItem(8.0,9.0) ); double capaciteit = 20.0; Map antw = rk.select(Collections.unmodifiableList(items), capaciteit); // Controleren of alle waarden van de sleutels in antw overeenkomen met deze in opl. check(antw, items.get(0), 0.0); check(antw, items.get(1), 0.0); check(antw, items.get(2), 0.0); check(antw, items.get(3), 1.0); check(antw, items.get(4), 0.0); check(antw, items.get(5), 1.0); } private void check(Map opl, KnapsackItem item, Double ratio){ Assert.assertEquals(ratio, opl.getOrDefault(item, 0.0), TOL); } }