File: test_cache_lru.cc

package info (click to toggle)
eckit 1.32.4-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 305,876 kB
  • sloc: cpp: 111,654; ansic: 2,826; yacc: 590; lex: 361; python: 237; sh: 202; makefile: 42
file content (187 lines) | stat: -rw-r--r-- 4,935 bytes parent folder | download | duplicates (3)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * (C) Copyright 1996- ECMWF.
 *
 * This software is licensed under the terms of the Apache Licence Version 2.0
 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
 * In applying this licence, ECMWF does not waive the privileges and immunities
 * granted to it by virtue of its status as an intergovernmental organisation nor
 * does it submit to any jurisdiction.
 */

#include <string>

#include "eckit/container/CacheLRU.h"
#include "eckit/exception/Exceptions.h"

#include "eckit/testing/Test.h"

using namespace std;
using namespace eckit;
using namespace eckit::testing;

namespace eckit::test {

//----------------------------------------------------------------------------------------------------------------------

static size_t purgeCalls = 0;

static void purge(std::string& key, size_t& value) {
    Log::info() << "Purged key " << key << " with value " << value << std::endl;
    ++purgeCalls;
}

//----------------------------------------------------------------------------------------------------------------------

CASE("test_cache_lru_basic") {
    eckit::CacheLRU<std::string, size_t> cache(3);

    EXPECT(cache.size() == 0);
    EXPECT(cache.capacity() == 3);

    // Add 2 items, check they're in the cache

    EXPECT(!cache.insert("ddd", 40));
    EXPECT(!cache.insert("aaa", 5));
    EXPECT(cache.insert("aaa", 10));

    EXPECT(cache.size() == 2);

    EXPECT(cache.exists("ddd"));
    EXPECT(cache.exists("aaa"));
    EXPECT(!cache.exists("bbb"));

    EXPECT(cache.access("aaa") == 10);
    EXPECT(cache.access("ddd") == 40);

    // Add 3 more items, displacing first 2

    EXPECT(!cache.insert("ccc", 30));
    EXPECT(!cache.insert("eee", 50));
    EXPECT(!cache.insert("bbb", 20));

    EXPECT(cache.size() == 3);

    EXPECT(cache.exists("bbb"));
    EXPECT(cache.exists("ccc"));
    EXPECT(cache.exists("eee"));

    EXPECT(cache.access("eee") == 50);
    EXPECT(cache.access("bbb") == 20);
    EXPECT(cache.access("ccc") == 30);

    EXPECT_THROWS_AS(cache.access("ddd"), eckit::OutOfRange);
    EXPECT_THROWS_AS(cache.access("aaa"), eckit::OutOfRange);

    // Increase capacity to 5, add 2 more items

    cache.capacity(5);
    EXPECT(cache.size() == 3);
    EXPECT(cache.capacity() == 5);

    EXPECT(!cache.insert("ddd", 40));
    EXPECT(!cache.insert("aaa", 10));

    EXPECT(cache.size() == 5);

    EXPECT(cache.exists("aaa"));
    EXPECT(cache.exists("bbb"));
    EXPECT(cache.exists("ccc"));
    EXPECT(cache.exists("ddd"));
    EXPECT(cache.exists("eee"));

    EXPECT(cache.access("aaa") == 10);
    EXPECT(cache.access("bbb") == 20);
    EXPECT(cache.access("ccc") == 30);
    EXPECT(cache.access("ddd") == 40);
    EXPECT(cache.access("eee") == 50);

    // Reduce capacity to 3, 2 items are purged

    cache.capacity(3);
    EXPECT(cache.size() == 3);
    EXPECT(cache.capacity() == 3);

    EXPECT(!cache.exists("aaa"));
    EXPECT(!cache.exists("bbb"));
    EXPECT(cache.exists("ccc"));
    EXPECT(cache.exists("ddd"));
    EXPECT(cache.exists("eee"));

    EXPECT_THROWS_AS(cache.access("aaa"), eckit::OutOfRange);
    EXPECT_THROWS_AS(cache.access("bbb"), eckit::OutOfRange);
    EXPECT(cache.access("ccc") == 30);
    EXPECT(cache.access("ddd") == 40);
    EXPECT(cache.access("eee") == 50);

    // Extract item

    EXPECT(cache.extract("ccc") == 30);
    EXPECT(cache.size() == 2);

    EXPECT(!cache.exists("ccc"));
    EXPECT(cache.exists("ddd"));
    EXPECT(cache.exists("eee"));

    EXPECT_THROWS_AS(cache.access("ccc"), eckit::OutOfRange);
    EXPECT(cache.access("ddd") == 40);
    EXPECT(cache.access("eee") == 50);

    // Remove item

    EXPECT(!cache.remove("ccc"));
    EXPECT(cache.remove("ddd"));
    EXPECT(cache.size() == 1);

    EXPECT(!cache.exists("ddd"));
    EXPECT_THROWS_AS(cache.access("ddd"), eckit::OutOfRange);

    // Clear the cache

    EXPECT_NO_THROW(cache.clear());
    EXPECT(cache.size() == 0);
    EXPECT(cache.capacity() == 3);

    EXPECT(!cache.exists("eee"));

    EXPECT_THROWS_AS(cache.access("eee"), eckit::OutOfRange);
}

CASE("test_cache_lru_purge") {
    eckit::CacheLRU<std::string, size_t> cache(4, purge);
    EXPECT(purgeCalls == 0);

    EXPECT(!cache.insert("aaa", 10));
    EXPECT(!cache.insert("bbb", 20));
    EXPECT(!cache.insert("ccc", 30));
    EXPECT(!cache.insert("ddd", 40));

    EXPECT(purgeCalls == 0);

    // Reducing capacity purges

    cache.capacity(3);
    EXPECT(purgeCalls == 1);

    // Extract does not purge

    EXPECT(cache.extract("bbb") == 20);
    EXPECT(purgeCalls == 1);

    // Remove purges

    EXPECT(cache.remove("ccc"));
    EXPECT(purgeCalls == 2);

    // Clear purges

    EXPECT_NO_THROW(cache.clear());
    EXPECT(purgeCalls == 3);
}

//----------------------------------------------------------------------------------------------------------------------

}  // namespace eckit::test

int main(int argc, char** argv) {
    return run_tests(argc, argv);
}