/
test_lrucache.cpp
244 lines (194 loc) · 6.09 KB
/
test_lrucache.cpp
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*****************************************************************************/
/* Milliways - B+ trees and key-value store C++ library */
/* */
/* Copyright 2016 Marco Pantaleoni and J CUBE Inc. Tokyo, Japan. */
/* */
/* Author: Marco Pantaleoni <marco.pantaleoni@gmail.com> */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); */
/* you may not use this file except in compliance with the License. */
/* You may obtain a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/*****************************************************************************/
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#include "catch.hpp"
#include <string>
#include <vector>
#include <utility>
#include "LRUCache.h"
#define LRU_SIZE 4
#define INVALID_INT_KEY -1000000
bool startsWith(const std::string& s, const std::string& prefix)
{
return (s.size() >= prefix.size()) && (s.substr(0, prefix.size()) == prefix);
}
bool endsWith(const std::string& s, const std::string& suffix)
{
return (s.size() >= suffix.size()) && (s.substr(s.size() - suffix.size(), suffix.size()) == suffix);
}
bool contains(const std::string& s, const std::string& infix)
{
return s.find(infix) != std::string::npos;
}
TEST_CASE( "LRU Cache", "[LRUCache]" ) {
typedef milliways::LRUCache<LRU_SIZE, int, std::string> lru_t;
lru_t lru(INVALID_INT_KEY);
REQUIRE(lru.size() == 0);
REQUIRE(lru.max_size() == LRU_SIZE);
SECTION( "starts empty" ) {
REQUIRE(lru.size() == 0);
REQUIRE(lru.empty());
}
SECTION( "can add elements" ) {
REQUIRE(lru.size() == 0);
lru[128] = "128";
lru[24] = "24";
REQUIRE(lru.size() == 2);
}
SECTION( "can retrieve elements" ) {
REQUIRE(lru.size() == 0);
lru[128] = "128";
lru[24] = "24";
lru[256] = "256";
REQUIRE(lru.size() == 3);
REQUIRE(lru[24] == "24");
REQUIRE(lru[256] == "256");
REQUIRE(lru[128] == "128");
}
SECTION( "can pop LRU item" ) {
REQUIRE(lru.size() == 0);
lru[128] = "128";
lru[24] = "24";
lru[256] = "256";
REQUIRE(lru.size() == 3);
lru_t::value_type item;
item = lru.pop();
REQUIRE(item.first == 128);
item = lru.pop();
REQUIRE(item.first == 24);
item = lru.pop();
REQUIRE(item.first == 256);
REQUIRE(lru.size() == 0);
}
SECTION( "pushing over size butts LRU item" ) {
REQUIRE(lru.size() == 0);
lru[128] = "128";
lru[24] = "24";
lru[256] = "256";
lru[15] = "15";
lru[30] = "30";
REQUIRE(lru.size() == LRU_SIZE);
lru_t::value_type item;
item = lru.pop();
REQUIRE(item.first == 24);
item = lru.pop();
REQUIRE(item.first == 256);
item = lru.pop();
REQUIRE(item.first == 15);
item = lru.pop();
REQUIRE(item.first == 30);
REQUIRE(lru.size() == 0);
}
}
static std::string miss_string(int value) {
std::ostringstream ss;
ss << "M-" << static_cast<int>(value);
return ss.str();
}
static std::string hit_string(int value) {
std::ostringstream ss;
ss << "hit-" << static_cast<int>(value);
return ss.str();
}
class ExtLRUCache : public milliways::LRUCache<LRU_SIZE, int, std::string>
{
public:
typedef milliways::LRUCache<LRU_SIZE, int, std::string> base_type;
typedef int key_type;
typedef std::string mapped_type;
ExtLRUCache() :
milliways::LRUCache<LRU_SIZE, int, std::string>(INVALID_INT_KEY),
m_n_miss(0), m_n_miss_get(0), m_n_miss_set(0), m_n_miss_sub(0),
m_n_set(0), m_n_eviction(0)
{ }
virtual ~ExtLRUCache() { evict_all(); }
long n_miss() const { return m_n_miss; }
long n_miss_get() const { return m_n_miss_get; }
long n_miss_set() const { return m_n_miss_set; }
long n_miss_sub() const { return m_n_miss_sub; }
long n_set() const { return m_n_set; }
long n_eviction() const { return m_n_eviction; }
bool on_miss(XTYPENAME base_type::op_type op, const key_type& key, mapped_type& value)
{
m_n_miss++;
switch (op)
{
case base_type::op_get:
m_n_miss_get++;
value = miss_string(key);
break;
case base_type::op_set:
m_n_miss_set++;
break;
case base_type::op_sub:
m_n_miss_sub++;
value = miss_string(key);
break;
}
return true;
}
bool on_set(const key_type& key, const mapped_type& value)
{
m_n_set++;
return true;
}
//bool on_delete(const key_type& key);
bool on_eviction(const key_type& key, mapped_type& value)
{
m_n_eviction++;
return true;
}
private:
long m_n_miss;
long m_n_miss_get;
long m_n_miss_set;
long m_n_miss_sub;
long m_n_set;
long m_n_eviction;
};
TEST_CASE( "Extended LRU Cache", "[ExtendedLRUCache]" ) {
static const int TEST_SET_SIZE = 20;
SECTION("adding values") {
ExtLRUCache lru;
for (int i = 0; i < TEST_SET_SIZE; i++)
lru[i] = hit_string(i);
}
SECTION("adding and checking values") {
ExtLRUCache lru;
REQUIRE(lru.size() == 0);
for (int i = 0; i < TEST_SET_SIZE; i++)
{
lru[i] = hit_string(i);
REQUIRE(lru[i] == hit_string(i));
}
REQUIRE(lru.size() == 4);
REQUIRE(lru.n_miss_sub() > 0);
REQUIRE(lru.n_eviction() > 0);
}
SECTION("hit and miss") {
ExtLRUCache lru;
REQUIRE(lru.size() == 0);
for (int i = 0; i < TEST_SET_SIZE; i++)
lru[i] = hit_string(i);
REQUIRE(lru.n_miss_sub() == TEST_SET_SIZE);
REQUIRE(lru.n_eviction() == TEST_SET_SIZE - 4);
std::cerr << "lru[8373]:" << lru[8373] << std::endl;
}
}