import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; public class SimpleTest { public static TruckLoader loader; @BeforeClass public static void init() { loader = new MyTruckLoader(); } @Test public void test1() { List input = Collections.unmodifiableList(Arrays.asList(12, 12, 12)); List antw = loader.pack(input, () -> new TestTruck(12)); checkInput(input, antw); Assert.assertEquals("Probeer het gebruikte aantal vrachtwagens te reduceren.", antw.size(), 3); } @Test public void test2() { List input = Collections.unmodifiableList(Arrays.asList(4, 3, 4, 6, 2, 4, 5, 1, 1, 2, 2, 2, 4)); List antw = loader.pack(input, () -> new TestTruck(10)); checkInput(input, antw); Assert.assertEquals("Probeer het gebruikte aantal vrachtwagens te reduceren.", antw.size(), 4); } @Test public void test3() { List input = Collections.unmodifiableList(Arrays.asList(1, 1, 1, 1, 8, 7, 6, 6)); List antw = loader.pack(input, () -> new TestTruck(9)); checkInput(input, antw); Assert.assertTrue("Probeer het gebruikte aantal vrachtwagens in jouw antwoord (" + antw.size() + ") te reduceren tot maximaal 5.", antw.size() <= 5); } @Test public void test4() { List input = Collections.unmodifiableList(Arrays.asList(7, 2, 2, 2, 2, 2, 3)); List antw = loader.pack(input, () -> new TestTruck(10)); checkInput(input, antw); Assert.assertTrue("Probeer het gebruikte aantal vrachtwagens in jouw antwoord (" + antw.size() + ") te reduceren tot maximaal 3.", antw.size() <= 3); } private void checkInput(List input, List antw) { List antwItems = antw.stream().flatMap((truck)->truck.getItems().stream()).collect(Collectors.toList()); List inputKopie = new ArrayList<>(input); Collections.sort(inputKopie); Collections.sort(antwItems); Assert.assertEquals("Items in vrachtwagens komen niet overeen met de gegeven objecten.", inputKopie, antwItems); } // Private klasse TestTruck, implementatie van Fleet.Truck. item stelt een vrachtwagen voor. // Nieuwe vrachtwagen aanmaken via "fleet.createTruck()". private class TestTruck implements Fleet.Truck { private List items; private int remainingCapacity; private TestTruck(int capacity) { remainingCapacity = capacity; items = new ArrayList<>(); } public void add(int item) { if (!canAdd(item)) { throw new Fleet.OverloadException(); } remainingCapacity -= item; items.add(item); } public int getRemainingCapacity() { return remainingCapacity; } public boolean canAdd(int item) { return item <= remainingCapacity; } public List getItems() { return Collections.unmodifiableList(items); } @Override public String toString() { return items.toString(); } } }