comparison UnitTestsSources/MemoryCacheTests.cpp @ 967:dfc076546821

add suffix Tests to unit test sources
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 27 Jun 2014 15:36:38 +0200
parents UnitTestsSources/MemoryCache.cpp@84513f2ee1f3
children 6e7e5ed91c2d
comparison
equal deleted inserted replaced
966:886652370ff2 967:dfc076546821
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege,
4 * Belgium
5 *
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * In addition, as a special exception, the copyright holders of this
12 * program give permission to link the code of its release with the
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
14 * that use the same license as the "OpenSSL" library), and distribute
15 * the linked executables. You must obey the GNU General Public License
16 * in all respects for all of the code used other than "OpenSSL". If you
17 * modify file(s) with this exception, you may extend this exception to
18 * your version of the file(s), but you are not obligated to do so. If
19 * you do not wish to do so, delete this exception statement from your
20 * version. If you delete this exception statement from all source files
21 * in the program, then also delete it here.
22 *
23 * This program is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 **/
31
32
33 #include "PrecompiledHeadersUnitTests.h"
34 #include "gtest/gtest.h"
35
36 #include <glog/logging.h>
37 #include <memory>
38 #include <boost/thread.hpp>
39 #include <boost/lexical_cast.hpp>
40 #include "../Core/IDynamicObject.h"
41 #include "../Core/Cache/MemoryCache.h"
42
43
44 TEST(LRU, Basic)
45 {
46 Orthanc::LeastRecentlyUsedIndex<std::string> r;
47
48 r.Add("d");
49 r.Add("a");
50 r.Add("c");
51 r.Add("b");
52
53 r.MakeMostRecent("a");
54 r.MakeMostRecent("d");
55 r.MakeMostRecent("b");
56 r.MakeMostRecent("c");
57 r.MakeMostRecent("d");
58 r.MakeMostRecent("c");
59
60 ASSERT_EQ("a", r.GetOldest());
61 ASSERT_EQ("a", r.RemoveOldest());
62 ASSERT_EQ("b", r.GetOldest());
63 ASSERT_EQ("b", r.RemoveOldest());
64 ASSERT_EQ("d", r.GetOldest());
65 ASSERT_EQ("d", r.RemoveOldest());
66 ASSERT_EQ("c", r.GetOldest());
67 ASSERT_EQ("c", r.RemoveOldest());
68
69 ASSERT_TRUE(r.IsEmpty());
70
71 ASSERT_THROW(r.GetOldest(), Orthanc::OrthancException);
72 ASSERT_THROW(r.RemoveOldest(), Orthanc::OrthancException);
73 }
74
75
76 TEST(LRU, Payload)
77 {
78 Orthanc::LeastRecentlyUsedIndex<std::string, int> r;
79
80 r.Add("a", 420);
81 r.Add("b", 421);
82 r.Add("c", 422);
83 r.Add("d", 423);
84
85 r.MakeMostRecent("a");
86 r.MakeMostRecent("d");
87 r.MakeMostRecent("b");
88 r.MakeMostRecent("c");
89 r.MakeMostRecent("d");
90 r.MakeMostRecent("c");
91
92 ASSERT_TRUE(r.Contains("b"));
93 ASSERT_EQ(421, r.Invalidate("b"));
94 ASSERT_FALSE(r.Contains("b"));
95
96 int p;
97 ASSERT_TRUE(r.Contains("a", p)); ASSERT_EQ(420, p);
98 ASSERT_TRUE(r.Contains("c", p)); ASSERT_EQ(422, p);
99 ASSERT_TRUE(r.Contains("d", p)); ASSERT_EQ(423, p);
100
101 ASSERT_EQ("a", r.GetOldest());
102 ASSERT_EQ(420, r.GetOldestPayload());
103 ASSERT_EQ("a", r.RemoveOldest(p)); ASSERT_EQ(420, p);
104
105 ASSERT_EQ("d", r.GetOldest());
106 ASSERT_EQ(423, r.GetOldestPayload());
107 ASSERT_EQ("d", r.RemoveOldest(p)); ASSERT_EQ(423, p);
108
109 ASSERT_EQ("c", r.GetOldest());
110 ASSERT_EQ(422, r.GetOldestPayload());
111 ASSERT_EQ("c", r.RemoveOldest(p)); ASSERT_EQ(422, p);
112
113 ASSERT_TRUE(r.IsEmpty());
114 }
115
116
117 TEST(LRU, PayloadUpdate)
118 {
119 Orthanc::LeastRecentlyUsedIndex<std::string, int> r;
120
121 r.Add("a", 420);
122 r.Add("b", 421);
123 r.Add("d", 423);
124
125 r.MakeMostRecent("a", 424);
126 r.MakeMostRecent("d", 421);
127
128 ASSERT_EQ("b", r.GetOldest());
129 ASSERT_EQ(421, r.GetOldestPayload());
130 r.RemoveOldest();
131
132 ASSERT_EQ("a", r.GetOldest());
133 ASSERT_EQ(424, r.GetOldestPayload());
134 r.RemoveOldest();
135
136 ASSERT_EQ("d", r.GetOldest());
137 ASSERT_EQ(421, r.GetOldestPayload());
138 r.RemoveOldest();
139
140 ASSERT_TRUE(r.IsEmpty());
141 }
142
143
144
145 TEST(LRU, PayloadUpdateBis)
146 {
147 Orthanc::LeastRecentlyUsedIndex<std::string, int> r;
148
149 r.AddOrMakeMostRecent("a", 420);
150 r.AddOrMakeMostRecent("b", 421);
151 r.AddOrMakeMostRecent("d", 423);
152 r.AddOrMakeMostRecent("a", 424);
153 r.AddOrMakeMostRecent("d", 421);
154
155 ASSERT_EQ("b", r.GetOldest());
156 ASSERT_EQ(421, r.GetOldestPayload());
157 r.RemoveOldest();
158
159 ASSERT_EQ("a", r.GetOldest());
160 ASSERT_EQ(424, r.GetOldestPayload());
161 r.RemoveOldest();
162
163 ASSERT_EQ("d", r.GetOldest());
164 ASSERT_EQ(421, r.GetOldestPayload());
165 r.RemoveOldest();
166
167 ASSERT_TRUE(r.IsEmpty());
168 }
169
170
171
172
173 namespace
174 {
175 class Integer : public Orthanc::IDynamicObject
176 {
177 private:
178 std::string& log_;
179 int value_;
180
181 public:
182 Integer(std::string& log, int v) : log_(log), value_(v)
183 {
184 }
185
186 virtual ~Integer()
187 {
188 LOG(INFO) << "Removing cache entry for " << value_;
189 log_ += boost::lexical_cast<std::string>(value_) + " ";
190 }
191
192 int GetValue() const
193 {
194 return value_;
195 }
196 };
197
198 class IntegerProvider : public Orthanc::ICachePageProvider
199 {
200 public:
201 std::string log_;
202
203 Orthanc::IDynamicObject* Provide(const std::string& s)
204 {
205 LOG(INFO) << "Providing " << s;
206 return new Integer(log_, boost::lexical_cast<int>(s));
207 }
208 };
209 }
210
211
212 TEST(MemoryCache, Basic)
213 {
214 IntegerProvider provider;
215
216 {
217 Orthanc::MemoryCache cache(provider, 3);
218 cache.Access("42"); // 42 -> exit
219 cache.Access("43"); // 43, 42 -> exit
220 cache.Access("45"); // 45, 43, 42 -> exit
221 cache.Access("42"); // 42, 45, 43 -> exit
222 cache.Access("43"); // 43, 42, 45 -> exit
223 cache.Access("47"); // 45 is removed; 47, 43, 42 -> exit
224 cache.Access("44"); // 42 is removed; 44, 47, 43 -> exit
225 cache.Access("42"); // 43 is removed; 42, 44, 47 -> exit
226 // Closing the cache: 47, 44, 42 are successively removed
227 }
228
229 ASSERT_EQ("45 42 43 47 44 42 ", provider.log_);
230 }