import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.util.*; public class SimpleTest { private static Caching caching; @BeforeClass 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) { Assert.assertEquals("Verkeerd aantal stappen.", items.size(), evictions.size()); 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 { Assert.assertTrue("Gevraagde item zit nog niet in cache.", cache.contains(items.get(i))); } } Assert.assertEquals("Aantal uitwijzingen is niet minimaal.", optimal, count); } }