You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LRUMapTest.java 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * Copyright (C) 2018, Konrad Windszus <konrad_w@gmx.de> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.util;
  11. import static org.hamcrest.MatcherAssert.assertThat;
  12. import java.util.LinkedHashMap;
  13. import java.util.Map;
  14. import org.hamcrest.collection.IsIterableContainingInOrder;
  15. import org.junit.Test;
  16. public class LRUMapTest {
  17. @SuppressWarnings("boxing")
  18. @Test
  19. public void testLRUEntriesAreEvicted() {
  20. Map<Integer, Integer> map = new LRUMap<>(3, 3);
  21. for (int i = 0; i < 3; i++) {
  22. map.put(i, i);
  23. }
  24. // access the last ones
  25. map.get(2);
  26. map.get(0);
  27. // put another one which exceeds the limit (entry with key "1" is
  28. // evicted)
  29. map.put(3, 3);
  30. Map<Integer, Integer> expectedMap = new LinkedHashMap<>();
  31. expectedMap.put(2, 2);
  32. expectedMap.put(0, 0);
  33. expectedMap.put(3, 3);
  34. assertThat(map.entrySet(), IsIterableContainingInOrder
  35. .contains(expectedMap.entrySet().toArray()));
  36. }
  37. }