aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/cgeo/geocaching/utils/LeastRecentlyUsedSetTest.java
blob: 2b6f8a0a2555ba2aec08e81360c4fb84ce2c8eac (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
package cgeo.geocaching.utils;

import static org.assertj.core.api.Assertions.assertThat;

import cgeo.geocaching.Geocache;

import java.util.Set;

public class LeastRecentlyUsedSetTest extends AbstractLRUTest {

    public static void testLruMode() {
        final Set<String> set = new LeastRecentlyUsedSet<String>(5);
        set.add("one");
        set.add("two");
        set.add("three");
        // read does not change anything
        set.contains("one");
        set.add("four");
        // re-put should update the order
        set.add("three");
        set.add("five");
        // read does not change anything
        set.contains("one");
        set.add("six");
        set.add("seven");

        assertEquals("four, three, five, six, seven", colToStr(set));
    }

    public static void testRemoveEldestEntry() {
        final LeastRecentlyUsedSet<Geocache> caches = new LeastRecentlyUsedSet<Geocache>(10);
        final Geocache first = new Geocache();
        first.setGeocode("1");
        assertThat(caches.add(first)).isTrue();

        final Geocache second = new Geocache();
        second.setGeocode("2");
        assertThat(caches.add(second)).isTrue();

        assertThat(caches).hasSize(2);
        assertThat(caches.contains(first)).isTrue();
        assertThat(caches.contains(second)).isTrue();

        // adding first cache again does not change set
        assertThat(caches.add(first)).isFalse();
        assertThat(caches).hasSize(2);

        for (int i = 3; i <= 10; i++) {
            final Geocache cache = new Geocache();
            cache.setGeocode(Integer.toString(i));
            assertThat(caches.add(cache)).isTrue();
        }

        assertThat(caches).hasSize(10);
        assertThat(caches.contains(first)).isTrue();
        assertThat(caches.contains(second)).isTrue();

        final Geocache c11 = new Geocache();
        c11.setGeocode("11");
        assertThat(caches.add(c11)).isTrue();

        assertThat(caches).hasSize(10);

        // first was used again, there second is the oldest and has been overwritten by 11
        assertThat(caches.contains(first)).isTrue();
        assertThat(caches.contains(second)).isFalse();
    }
}