import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import java.util.*; public class SimpleTest { private static Caching caching; @BeforeAll public static void init() { caching = new OptimalCaching(); } @Test public void test1() { List items = Arrays.asList(1, 2, 3, 1, 2, 3, 1, 2, 3); Set cache = new HashSet<>(Arrays.asList(2, 3)); List evictions = caching.optimize(Collections.unmodifiableList(items), Collections.unmodifiableSet(cache)); check(items, cache, evictions, 5); } @Test public void test2() { List items = Arrays.asList(1, 2, 1, 2, 1, 2, 1, 2); Set cache = new HashSet<>(Collections.singleton(1)); List evictions = caching.optimize(Collections.unmodifiableList(items), Collections.unmodifiableSet(cache)); check(items, cache, evictions, 7); } @Test public void test3() { List items = Arrays.asList(1, 2, 3, 2, 3, 1, 2); Set cache = new HashSet<>(Arrays.asList(1, 2)); List evictions = caching.optimize(Collections.unmodifiableList(items), Collections.unmodifiableSet(cache)); check(items, cache, evictions, 2); } @Test public void test4() { List items = Arrays.asList(1, 2, 3, 4, 1, 4, 5, 1, 4, 2, 3); Set cache = new HashSet<>(Arrays.asList(1, 2, 3)); List evictions = caching.optimize(Collections.unmodifiableList(items), Collections.unmodifiableSet(cache)); check(items, cache, evictions, 4); } @Test public void test5() { List items = Arrays.asList(1, 2, 1, 2); Set cache = new HashSet<>(Arrays.asList(1, 2)); List evictions = caching.optimize(Collections.unmodifiableList(items), Collections.unmodifiableSet(cache)); check(items, cache, evictions, 0); } static void check(List items, Set cache, List evictions, int optimal) { Assertions.assertEquals(items.size(), evictions.size(), "Verkeerd aantal stappen."); int count = 0; for (int i = 0; i < items.size(); i++) { Integer item = items.get(i); Integer evicted = evictions.get(i); if (evicted != null) { cache.remove(evicted); cache.add(items.get(i)); count++; } else { Assertions.assertTrue(cache.contains(items.get(i)), "Gevraagde item zit nog niet in cache."); } } Assertions.assertEquals(optimal, count, "Aantal uitwijzingen is niet minimaal."); } }