-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_lru.cpp
90 lines (72 loc) · 1.97 KB
/
test_lru.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
/*
astyle --style=attach -s2 --keep-one-line-blocks --keep-one-line-statements --pad-oper *.hpp *.cpp
c++ -std=c++11 test_lru.cpp
c++ -std=c++11 -DLRU_DEBUG=1 test_lru.cpp
*/
#define LRU_DEBUG 1
#include <iostream>
#include <string>
#include "lru.hpp"
int main () {
lru<int, int> l_int(2000);
int i1 = 1, i2 = 2;
l_int.insert(1, 2);
auto gp = l_int.get(1);
l_int.remove(1);
l_int.insert(1, 2);
l_int.insert(3, 4);
auto gp_long = l_int.get(3);
int ig = 11;
for(int i = 10; i <= 5000; ++i) {
int i3 = i * 3;
l_int.insert(i, i * 3);
auto gp = l_int.get(ig);
if (gp)
std::cout << "get " << ig << " = " << gp.get() << " " << *gp << "\n";
int ig2 = 15;
gp = l_int.get(ig2);
if (gp)
std::cout << "get " << ig2 << " = " << gp.get() << " " << *gp << "\n";
}
// 20 must be cleared because not used
int ign = 20;
gp = l_int.get(ign);
if (gp) {
std::cout << "WRONG! get " << ign << " = " << *gp << "\n";
} else {
std::cout << "OK get " << ign << " = " << gp.get() << "\n";
}
if (gp_long)
std::cout << "safe get still valid: " << *gp_long.get() << "\n";
lru<double> l_double(30000);
lru<> l1(10000);
std::string key1("a");
std::string val1(1000, 'b');
l1.insert(key1, val1);
key1 = std::string(100, 'a');
val1 = "b"; // capacity still ~1000
l1.insert(key1, val1);
val1 = "bbbbbbbbbbb";
val1.shrink_to_fit();
l1.insert(key1, val1);
std::string * tos = nullptr;
auto gps = l1.get(key1);
if (gps) {
std::cout << "OK get replaced " << key1 << " = " << *gps << "\n";
}
val1 = std::string(500, 'c');
l1.insert(key1, val1);
l1.remove(key1);
l1.remove(key1);
l1.remove(key1);
// bigger than storage, dont even try store
val1 = std::string(500000, 'c');
l1.insert(key1, val1);
//exactly as free space
lru<> l2(10000);
val1 = std::string(10000 - sizeof(l2) - 104 - 8, 'c');
l2.insert(key1, val1);
val1 = std::string(1, 'c');
val1.shrink_to_fit();
l2.insert(key1, val1);
}