import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; public class SimpleTest { private static DiscreteKnapsack dk; @BeforeAll public static void init() { dk = new BBDiscreteKnapsack(); } @Test public void test1() { KnapsackItem k0 = new KnapsackItem(6.3, 5.0); KnapsackItem k1 = new KnapsackItem(7.2, 3.5); KnapsackItem k2 = new KnapsackItem(Math.PI, 4.7); KnapsackItem k3 = new KnapsackItem(2.7, 1.2); KnapsackItem k4 = new KnapsackItem(7.1, 9.1); Set items = new HashSet<>(Arrays.asList( k0, k1, k2, k3, k4) ); double capacity = 11.7; Collection antw = dk.select(Collections.unmodifiableSet(items), capacity); // Geldigheid controleren: capacity mag niet overschreden worden. checkCapacity(antw, capacity); // Controleren of antw de items van opl bevat. checkItems(antw, k2,k4); } @Test public void test2() { KnapsackItem k0 = new KnapsackItem(8.7, 7.1); KnapsackItem k1 = new KnapsackItem(3.1, 3.8); KnapsackItem k2 = new KnapsackItem(3.6, 4.3); KnapsackItem k3 = new KnapsackItem(2.9, 1.0); KnapsackItem k4 = new KnapsackItem(5.1, 1.1); KnapsackItem k5 = new KnapsackItem(3.7, 3.0); KnapsackItem k6 = new KnapsackItem(8.2, 9.0); List items = Arrays.asList( k0, k1, k2, k3, k4, k5, k6 ); double capacity = 8*Math.E; Collection antw = dk.select(Collections.unmodifiableList(items), capacity); // Geldigheid controleren: capacity mag niet overschreden worden. checkCapacity(antw, capacity); // Controleren of antw de items van opl bevat. checkItems(antw, k1, k2, k6, k5, k3); } @Test public void test3() { KnapsackItem k0 = new KnapsackItem(8.2, 14.5); KnapsackItem k1 = new KnapsackItem(10.1, 9.0); KnapsackItem k2 = new KnapsackItem(3.9, 15.3); KnapsackItem k3 = new KnapsackItem(15.1, 12.0); KnapsackItem k4 = new KnapsackItem(10.7, 14.1); KnapsackItem k5 = new KnapsackItem(9.5, 8.0); KnapsackItem k6 = new KnapsackItem(5.8, 8.0); Set items = new HashSet<>(Arrays.asList( k0, k1, k2, k3, k4, k5, k6) ); double capacity = 25.1; Collection antw = dk.select(Collections.unmodifiableSet(items), capacity); // Geldigheid controleren: capacity mag niet overschreden worden. checkCapacity(antw, capacity); // Controleren of antw de items van opl bevat. checkItems(antw, k2, k0, k4); } @Test public void test4() { KnapsackItem k0 = new KnapsackItem(8.0, 9.9); KnapsackItem k1 = new KnapsackItem(11.0, 11.7); KnapsackItem k2 = new KnapsackItem(3.0, 5.1); KnapsackItem k3 = new KnapsackItem(17.0, 17.0); KnapsackItem k4 = new KnapsackItem(13.0, 16.1); KnapsackItem k5 = new KnapsackItem(9.0, 8.9); KnapsackItem k6 = new KnapsackItem(5.0, 8.0); KnapsackItem k7 = new KnapsackItem(2.0, 4.0); KnapsackItem k8 = new KnapsackItem(10.0, 10.1); KnapsackItem k9 = new KnapsackItem(6.0, 9.2); List items = Arrays.asList( k0, k1, k2, k3, k4, k5, k6, k7, k8, k9 ); double capacity = 30.2; Collection antw = dk.select(Collections.unmodifiableList(items), capacity); // Geldigheid controleren: capacity mag niet overschreden worden. checkCapacity(antw, capacity); // Controleren of antw de items van opl bevat. checkItems(antw, k2, k7, k6, k9, k4); } @Test public void testAll() { KnapsackItem k0 = new KnapsackItem(8.0, 9.9); KnapsackItem k1 = new KnapsackItem(11.0, 11.7); KnapsackItem k2 = new KnapsackItem(3.0, 5.1); KnapsackItem k3 = new KnapsackItem(17.0, 17.0); KnapsackItem k4 = new KnapsackItem(13.0, 16.1); List items = Arrays.asList( k0, k1, k2, k3, k4 ); double capacity = 55.0; Collection antw = dk.select(Collections.unmodifiableList(items), capacity); // Geldigheid controleren: capacity mag niet overschreden worden. checkCapacity(antw, capacity); // Controleren of antw de items van opl bevat. checkItems(antw, k0, k1, k2, k3, k4); } public void checkCapacity(Collection antw, double capacity){ double gewicht = 0; for(KnapsackItem item : antw){ gewicht += item.size(); } Assertions.assertTrue(gewicht <= capacity, "De capaciteit wordt overschreden in jouw oplossing: " + antw); } public void checkItems(Collection antw, KnapsackItem... items){ for(KnapsackItem item : items){ Assertions.assertTrue(antw.contains(item), " Het volgende item: " + item + " ontbreekt in jouw oplossing: " + antw); } } }