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
|
/*
* Copyright (c) 2017 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#include <string>
#include <boost/optional.hpp>
#include <Swiften/Base/LogSerializers.h>
#include <Swiften/Base/LRUCache.h>
#include <gtest/gtest.h> // This has to go after Swiften/Base/LogSerializers.h.
using namespace Swift;
namespace b = boost;
TEST(LRUCacheTest, testCacheLimit) {
LRUCache<std::string, std::string, 3> testling;
testling.insert("A", "AA");
testling.insert("B", "BB");
testling.insert("C", "CC");
ASSERT_EQ(b::optional<std::string>("AA"), testling.get("A"));
ASSERT_EQ(b::optional<std::string>("BB"), testling.get("B"));
ASSERT_EQ(b::optional<std::string>("CC"), testling.get("C"));
ASSERT_EQ(b::optional<std::string>(), testling.get("D"));
testling.insert("D", "DD");
ASSERT_EQ(b::optional<std::string>(), testling.get("A"));
ASSERT_EQ(b::optional<std::string>("BB"), testling.get("B"));
ASSERT_EQ(b::optional<std::string>("CC"), testling.get("C"));
ASSERT_EQ(b::optional<std::string>("DD"), testling.get("D"));
}
TEST(LRUCacheTest, testMoveRecentToFrontOnGet) {
LRUCache<std::string, std::string, 3> testling;
testling.insert("A", "AA");
testling.insert("B", "BB");
testling.insert("C", "CC");
ASSERT_EQ(b::optional<std::string>("AA"), testling.get("A"));
ASSERT_EQ(b::optional<std::string>("BB"), testling.get("B"));
ASSERT_EQ(b::optional<std::string>("CC"), testling.get("C"));
ASSERT_EQ(b::optional<std::string>(), testling.get("D"));
ASSERT_EQ(b::optional<std::string>("AA"), testling.get("A"));
testling.insert("D", "DD");
ASSERT_EQ(b::optional<std::string>("AA"), testling.get("A"));
ASSERT_EQ(b::optional<std::string>(), testling.get("B"));
ASSERT_EQ(b::optional<std::string>("CC"), testling.get("C"));
ASSERT_EQ(b::optional<std::string>("DD"), testling.get("D"));
}
TEST(LRUCacheTest, testMoveRecentToFrontOnReinsert) {
LRUCache<std::string, std::string, 3> testling;
testling.insert("A", "AA");
testling.insert("B", "BB");
testling.insert("C", "CC");
ASSERT_EQ(b::optional<std::string>("AA"), testling.get("A"));
ASSERT_EQ(b::optional<std::string>("BB"), testling.get("B"));
ASSERT_EQ(b::optional<std::string>("CC"), testling.get("C"));
ASSERT_EQ(b::optional<std::string>(), testling.get("D"));
testling.insert("B", "BB");
ASSERT_EQ(b::optional<std::string>("AA"), testling.get("A"));
ASSERT_EQ(b::optional<std::string>("BB"), testling.get("B"));
ASSERT_EQ(b::optional<std::string>("CC"), testling.get("C"));
ASSERT_EQ(b::optional<std::string>(), testling.get("D"));
testling.insert("D", "DD");
ASSERT_EQ(b::optional<std::string>(), testling.get("A"));
ASSERT_EQ(b::optional<std::string>("BB"), testling.get("B"));
ASSERT_EQ(b::optional<std::string>("CC"), testling.get("C"));
ASSERT_EQ(b::optional<std::string>("DD"), testling.get("D"));
}
TEST(LRUCacheTest, testCacheReturnsValuesPreviouslyInserted) {
LRUCache<std::string, std::string, 3> testling;
testling.insert("A", "AA");
testling.insert("B", "BB");
testling.insert("C", "CC");
ASSERT_EQ(b::optional<std::string>("AA"), testling.get("A"));
ASSERT_EQ(b::optional<std::string>("BB"), testling.get("B"));
ASSERT_EQ(b::optional<std::string>("CC"), testling.get("C"));
}
TEST(LRUCacheTest, testCacheMissFunctionIsUsedOnCacheMiss) {
LRUCache<std::string, std::string, 3> testling;
testling.insert("A", "AA");
testling.insert("B", "BB");
ASSERT_EQ(b::optional<std::string>("AA"), testling.get("A"));
ASSERT_EQ(b::optional<std::string>("BB"), testling.get("B"));
ASSERT_EQ(b::optional<std::string>("CC"), testling.get("C", [](const std::string&) {
return boost::optional<std::string>(std::string("CC"));
}));
ASSERT_EQ(b::optional<std::string>("CC"), testling.get("C"));
ASSERT_EQ(b::optional<std::string>(), testling.get("D", [](const std::string&) {
return boost::optional<std::string>();
}));
ASSERT_EQ(b::optional<std::string>(), testling.get("D"));
}
|