aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/cgeo/geocaching/utils/LeastRecentlyUsedMapTest.java
blob: 11088e2167909c96c7792f11fb933016e7c11d71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package cgeo.geocaching.utils;

import cgeo.geocaching.Geocache;

import java.util.Map;

public class LeastRecentlyUsedMapTest extends AbstractLRUTest {

    public static void testLruMode() {
        final Map<String, String> map = new LeastRecentlyUsedMap.LruCache<String, String>(4);
        map.put("one", "1");
        map.put("two", "2");
        map.put("three", "3");
        // keep in cache
        map.get("one");
        map.put("four", "4");
        map.put("five", "5");
        map.put("six", "6");
        // keep in cache
        map.get("one");
        // re-add
        map.put("five", "5");
        map.put("seven", "7");

        assertEquals("six, one, five, seven", colToStr(map.keySet()));
    }

    public static void testBoundedMode() {
        final Map<String, String> map = new LeastRecentlyUsedMap.Bounded<String, String>(5);
        map.put("one", "1");
        map.put("two", "2");
        map.put("three", "3");
        // read does not change anything
        map.get("one");
        map.put("four", "4");
        // re-put should update the order
        map.put("three", "3");
        map.put("five", "5");
        // read does not change anything
        map.get("one");
        map.put("six", "6");
        map.put("seven", "7");

        assertEquals("four, three, five, six, seven", colToStr(map.keySet()));
    }

    public static void testRemoveEldestEntry() {
        final LeastRecentlyUsedMap<String, Geocache> cache = new LeastRecentlyUsedMap.LruCache<String, Geocache>(10);
        final Geocache first = new Geocache();
        assertNull(cache.put("1", first));

        final Geocache second = new Geocache();
        assertNull(cache.put("2", second));

        assertEquals(2, cache.size());
        assertTrue(cache.containsKey("1"));
        assertTrue(cache.containsValue(first));
        assertTrue(cache.containsKey("2"));
        assertTrue(cache.containsValue(second));

        for (int i = 3; i <= 10; i++) {
            assertNull(cache.put(Integer.toString(i), new Geocache()));
        }

        assertEquals(10, cache.size());
        assertTrue(cache.containsKey("1"));
        assertTrue(cache.containsValue(first));
        assertTrue(cache.containsKey("2"));
        assertTrue(cache.containsValue(second));

        assertNotNull(cache.remove("1")); // just replacing the old entry would not work
        assertNull(cache.put("1", new Geocache()));
        assertNull(cache.put("11", new Geocache()));

        assertEquals(10, cache.size());

        // first has been overwritten by new value, but key must be in, because it is very new
        assertTrue(cache.containsKey("1"));

        // second has been overwritten by 11
        assertFalse(cache.containsKey("2"));
        assertTrue(cache.containsKey("11"));
    }

}