-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlrucache.cpp
More file actions
89 lines (74 loc) · 2.7 KB
/
Copy pathlrucache.cpp
File metadata and controls
89 lines (74 loc) · 2.7 KB
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
/*
* Copyright (C) 2024 Jimmy Aguilar Mena
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "utils.h"
#include <argparser.hpp>
#include <iostream>
#include "lrucache.hpp"
int main(int argc, char **argv)
{
argparser::init(argc, argv);
const size_t limit = argparser::cl<int>("cache_size");
lrucache<int, std::string> cache(limit);
std::cout << "Initial: " << cache << std::endl;
for (size_t i = 0; i < limit; ++i) {
cache.push(i, std::to_string(i));
myassert(cache.size() <= limit);
}
std::cout << "Insert 0->limit: " << cache << std::endl;
myassert(cache.size() == limit);
for (size_t i = limit; i < limit + 5; ++i) {
cache.push(i, std::to_string(i));
myassert(cache.size() == limit);
}
std::cout << "Insert limit-> limit+5: " << cache << std::endl;
// After inserting 5 elements more we must only have limit elements
myassert(cache.size() == limit);
// Assert it overrided the first 5 elements
for (size_t i = 0; i < 5; ++i) {
myassert(cache.find(i) == cache.end());
}
// Assert that the elements from 5->limit are there
// check all the elements to preserve cache order.
for (size_t i = 5; i < limit + 5; ++i) {
myassert(cache.find(i) != cache.end());
}
// test the operator[] (and register an access to 5)
string val = cache[5];
std::cout << "Get 5: " << cache << std::endl;
myassert(val == "5");
// push 1 more element to assert 5 is preserved and 6 is removed.
cache.push(limit + 10, "limit+10");
std::cout << "Push limit+10: " << cache << std::endl;
myassert(cache.find(5) != cache.end());
std::cout << "Find 5: " << cache << std::endl;
myassert(cache.find(6) == cache.end());
myassert(cache.size() == limit);
// Remove the element 7
cache.erase(7);
std::cout << "Erase 7: " << cache << std::endl;
myassert(cache.size() == limit-1);
// Remove the element using iterator
auto it = cache.find(11);
std::cout << "Find 11: " << cache << std::endl;
cache.erase(it);
std::cout << "Erase it(11): " << cache << std::endl;
myassert(cache.size() == limit-2);
// Tests iteration
for (auto it : cache)
std::cout << it.first << "\t" << (std::string)it.second << std::endl;
return 0;
}