Skip to content

Commit 17e2f0c

Browse files
committed
test: add tests for class lfu cache
1 parent a37ce7b commit 17e2f0c

File tree

1 file changed

+89
-10
lines changed

1 file changed

+89
-10
lines changed

others/lfu_cache.cpp

Lines changed: 89 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,15 @@
1919
* @author [Karan Sharma](https://github.com/deDSeC00720)
2020
*/
2121

22-
#include <iostream>
23-
#include <unordered_map>
22+
#include <cassert> // for assert
23+
#include <iostream> // for IO operations
24+
#include <unordered_map> // for hash map
2425

26+
/**
27+
* @namespace
28+
* @brief Other algorithms
29+
*/
30+
namespace others {
2531
namespace Cache {
2632

2733
template <typename T>
@@ -31,7 +37,7 @@ class D_Node {
3137
D_Node<T> *prev;
3238
D_Node<T> *next;
3339

34-
D_Node(T data) : data(data), prev(nullptr), next(nullptr) {}
40+
explicit D_Node(T data) : data(data), prev(nullptr), next(nullptr) {}
3541
};
3642

3743
template <typename K, typename V>
@@ -65,8 +71,9 @@ class LFUCache {
6571
void push(int freq, CacheNode<K, V> *node) {
6672
// if freq is not present, then make a new list with node as the head as
6773
// well as tail.
68-
if (!freq_map.count(key)) {
74+
if (!freq_map.count(freq)) {
6975
freq_map[freq] = {node, node};
76+
return;
7077
}
7178

7279
std::pair<CacheNode<K, V> *, CacheNode<K, V> *> &p = freq_map[freq];
@@ -116,7 +123,7 @@ class LFUCache {
116123
}
117124
}
118125
push(freq + 1, node);
119-
p_node.second++;
126+
++p_node.second;
120127
}
121128

122129
/**
@@ -177,7 +184,7 @@ class LFUCache {
177184
*/
178185
V get(K key) {
179186
if (!node_map.count(key)) {
180-
throw std::exception("key is not present in the cache");
187+
throw std::runtime_error("key is not present in the cache");
181188
}
182189

183190
// increase the frequency and return the value
@@ -190,19 +197,19 @@ class LFUCache {
190197
* @brief Returns the number of items present in the cache.
191198
* @return number of items in the cache
192199
*/
193-
int size() { return node_map.size(); }
200+
int size() const { return node_map.size(); }
194201

195202
/**
196203
* @brief Returns the total capacity of the cache
197204
* @return Total capacity of the cache
198205
*/
199-
int capacity() { return _capacity; }
206+
int capacity() const { return _capacity; }
200207

201208
/**
202209
* @brief returns true if the cache is empty, false otherwise.
203210
* @return true if the cache is empty, false otherwise.
204211
*/
205-
bool empty() { return node_map.size() == 0; }
212+
bool empty() const { return node_map.empty(); }
206213

207214
/**
208215
* @brief destructs the cache, iterates on the map and deletes every node
@@ -212,8 +219,80 @@ class LFUCache {
212219
auto it = node_map.begin();
213220
while (it != node_map.end()) {
214221
delete it->second.first;
215-
it++;
222+
++it;
216223
}
217224
}
218225
};
219226
} // namespace Cache
227+
} // namespace others
228+
229+
/**
230+
* @brief A simple test case
231+
* @return void
232+
*/
233+
void test1() {
234+
others::Cache::LFUCache<int, int> cache(5);
235+
assert(cache.size() == 0);
236+
assert(cache.capacity() == 5);
237+
assert(cache.empty());
238+
cache.put(1, 10);
239+
cache.put(2, 20);
240+
241+
assert(cache.size() == 2);
242+
assert(cache.capacity() == 5);
243+
assert(!cache.empty());
244+
245+
cache.put(3, 30);
246+
cache.put(4, 40);
247+
cache.put(5, 50);
248+
cache.put(6, 60);
249+
250+
assert(cache.size() == 5);
251+
assert(cache.capacity() == 5);
252+
assert(!cache.empty());
253+
254+
std::cout << "test1 - passed\n";
255+
}
256+
257+
/**
258+
* @brief A simple test case
259+
* @return void
260+
*/
261+
void test2() {
262+
others::Cache::LFUCache<int, int> cache(5);
263+
cache.put(1, 10);
264+
cache.put(2, 20);
265+
cache.put(3, 30);
266+
267+
assert(cache.get(1) == 10);
268+
cache.get(2);
269+
cache.get(3);
270+
271+
cache.put(4, 40);
272+
cache.put(5, 50);
273+
cache.put(6, 60);
274+
275+
assert(cache.get(5) == 50);
276+
277+
std::cout << "test2 - passed\n";
278+
}
279+
280+
/**
281+
* @brief test method with 2 tests
282+
* @return void
283+
*/
284+
285+
void test() {
286+
test1();
287+
test2();
288+
}
289+
290+
/**
291+
* @brief main function
292+
* @return 0 on exit
293+
*/
294+
295+
int main() {
296+
test();
297+
return 0;
298+
}

0 commit comments

Comments
 (0)