comparison UnitTestsSources/MemoryCache.cpp @ 632:17815b9d4280

rename the UnitTests directory to avoid clashes in filenames
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 28 Oct 2013 16:26:51 +0100
parents UnitTests/MemoryCache.cpp@3b735fdf320b
children 3d6f9b7d0add
comparison
equal deleted inserted replaced
631:fc6ad5b97219 632:17815b9d4280
1 #include "gtest/gtest.h"
2
3 #include <glog/logging.h>
4 #include <memory>
5 #include <boost/thread.hpp>
6 #include <boost/lexical_cast.hpp>
7 #include "../Core/IDynamicObject.h"
8 #include "../Core/Cache/MemoryCache.h"
9
10
11 TEST(LRU, Basic)
12 {
13 Orthanc::LeastRecentlyUsedIndex<std::string> r;
14
15 r.Add("d");
16 r.Add("a");
17 r.Add("c");
18 r.Add("b");
19
20 r.MakeMostRecent("a");
21 r.MakeMostRecent("d");
22 r.MakeMostRecent("b");
23 r.MakeMostRecent("c");
24 r.MakeMostRecent("d");
25 r.MakeMostRecent("c");
26
27 ASSERT_EQ("a", r.GetOldest());
28 ASSERT_EQ("a", r.RemoveOldest());
29 ASSERT_EQ("b", r.GetOldest());
30 ASSERT_EQ("b", r.RemoveOldest());
31 ASSERT_EQ("d", r.GetOldest());
32 ASSERT_EQ("d", r.RemoveOldest());
33 ASSERT_EQ("c", r.GetOldest());
34 ASSERT_EQ("c", r.RemoveOldest());
35
36 ASSERT_TRUE(r.IsEmpty());
37
38 ASSERT_THROW(r.GetOldest(), Orthanc::OrthancException);
39 ASSERT_THROW(r.RemoveOldest(), Orthanc::OrthancException);
40 }
41
42
43 TEST(LRU, Payload)
44 {
45 Orthanc::LeastRecentlyUsedIndex<std::string, int> r;
46
47 r.Add("a", 420);
48 r.Add("b", 421);
49 r.Add("c", 422);
50 r.Add("d", 423);
51
52 r.MakeMostRecent("a");
53 r.MakeMostRecent("d");
54 r.MakeMostRecent("b");
55 r.MakeMostRecent("c");
56 r.MakeMostRecent("d");
57 r.MakeMostRecent("c");
58
59 ASSERT_TRUE(r.Contains("b"));
60 ASSERT_EQ(421, r.Invalidate("b"));
61 ASSERT_FALSE(r.Contains("b"));
62
63 int p;
64 ASSERT_TRUE(r.Contains("a", p)); ASSERT_EQ(420, p);
65 ASSERT_TRUE(r.Contains("c", p)); ASSERT_EQ(422, p);
66 ASSERT_TRUE(r.Contains("d", p)); ASSERT_EQ(423, p);
67
68 ASSERT_EQ("a", r.GetOldest());
69 ASSERT_EQ(420, r.GetOldestPayload());
70 ASSERT_EQ("a", r.RemoveOldest(p)); ASSERT_EQ(420, p);
71
72 ASSERT_EQ("d", r.GetOldest());
73 ASSERT_EQ(423, r.GetOldestPayload());
74 ASSERT_EQ("d", r.RemoveOldest(p)); ASSERT_EQ(423, p);
75
76 ASSERT_EQ("c", r.GetOldest());
77 ASSERT_EQ(422, r.GetOldestPayload());
78 ASSERT_EQ("c", r.RemoveOldest(p)); ASSERT_EQ(422, p);
79
80 ASSERT_TRUE(r.IsEmpty());
81 }
82
83
84 TEST(LRU, PayloadUpdate)
85 {
86 Orthanc::LeastRecentlyUsedIndex<std::string, int> r;
87
88 r.Add("a", 420);
89 r.Add("b", 421);
90 r.Add("d", 423);
91
92 r.MakeMostRecent("a", 424);
93 r.MakeMostRecent("d", 421);
94
95 ASSERT_EQ("b", r.GetOldest());
96 ASSERT_EQ(421, r.GetOldestPayload());
97 r.RemoveOldest();
98
99 ASSERT_EQ("a", r.GetOldest());
100 ASSERT_EQ(424, r.GetOldestPayload());
101 r.RemoveOldest();
102
103 ASSERT_EQ("d", r.GetOldest());
104 ASSERT_EQ(421, r.GetOldestPayload());
105 r.RemoveOldest();
106
107 ASSERT_TRUE(r.IsEmpty());
108 }
109
110
111
112 TEST(LRU, PayloadUpdateBis)
113 {
114 Orthanc::LeastRecentlyUsedIndex<std::string, int> r;
115
116 r.AddOrMakeMostRecent("a", 420);
117 r.AddOrMakeMostRecent("b", 421);
118 r.AddOrMakeMostRecent("d", 423);
119 r.AddOrMakeMostRecent("a", 424);
120 r.AddOrMakeMostRecent("d", 421);
121
122 ASSERT_EQ("b", r.GetOldest());
123 ASSERT_EQ(421, r.GetOldestPayload());
124 r.RemoveOldest();
125
126 ASSERT_EQ("a", r.GetOldest());
127 ASSERT_EQ(424, r.GetOldestPayload());
128 r.RemoveOldest();
129
130 ASSERT_EQ("d", r.GetOldest());
131 ASSERT_EQ(421, r.GetOldestPayload());
132 r.RemoveOldest();
133
134 ASSERT_TRUE(r.IsEmpty());
135 }
136
137
138
139
140 namespace
141 {
142 class Integer : public Orthanc::IDynamicObject
143 {
144 private:
145 std::string& log_;
146 int value_;
147
148 public:
149 Integer(std::string& log, int v) : log_(log), value_(v)
150 {
151 }
152
153 virtual ~Integer()
154 {
155 LOG(INFO) << "Removing cache entry for " << value_;
156 log_ += boost::lexical_cast<std::string>(value_) + " ";
157 }
158
159 int GetValue() const
160 {
161 return value_;
162 }
163 };
164
165 class IntegerProvider : public Orthanc::ICachePageProvider
166 {
167 public:
168 std::string log_;
169
170 Orthanc::IDynamicObject* Provide(const std::string& s)
171 {
172 LOG(INFO) << "Providing " << s;
173 return new Integer(log_, boost::lexical_cast<int>(s));
174 }
175 };
176 }
177
178
179 TEST(MemoryCache, Basic)
180 {
181 IntegerProvider provider;
182
183 {
184 Orthanc::MemoryCache cache(provider, 3);
185 cache.Access("42"); // 42 -> exit
186 cache.Access("43"); // 43, 42 -> exit
187 cache.Access("45"); // 45, 43, 42 -> exit
188 cache.Access("42"); // 42, 45, 43 -> exit
189 cache.Access("43"); // 43, 42, 45 -> exit
190 cache.Access("47"); // 45 is removed; 47, 43, 42 -> exit
191 cache.Access("44"); // 42 is removed; 44, 47, 43 -> exit
192 cache.Access("42"); // 43 is removed; 42, 44, 47 -> exit
193 // Closing the cache: 47, 44, 42 are successively removed
194 }
195
196 ASSERT_EQ("45 42 43 47 44 42 ", provider.log_);
197 }