annotate UnitTests/MemoryCache.cpp @ 478:888f8a778e70

move
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 16 Jul 2013 09:00:25 +0200
parents 4031f73fe0e4
children f59e4518fd57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 #include "gtest/gtest.h"
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
3 #include <glog/logging.h>
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 #include <memory>
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
5 #include <boost/thread.hpp>
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
6 #include <boost/lexical_cast.hpp>
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 #include "../Core/IDynamicObject.h"
284
06aa7b7b6723 implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 283
diff changeset
8 #include "../Core/Cache/MemoryCache.h"
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 TEST(CacheIndex, Basic)
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 {
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 Orthanc::CacheIndex<std::string> r;
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 r.Add("d");
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 r.Add("a");
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 r.Add("c");
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 r.Add("b");
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 r.TagAsMostRecent("a");
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 r.TagAsMostRecent("d");
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 r.TagAsMostRecent("b");
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 r.TagAsMostRecent("c");
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 r.TagAsMostRecent("d");
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 r.TagAsMostRecent("c");
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 ASSERT_EQ("a", r.RemoveOldest());
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 ASSERT_EQ("b", r.RemoveOldest());
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 ASSERT_EQ("d", r.RemoveOldest());
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 ASSERT_EQ("c", r.RemoveOldest());
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 ASSERT_TRUE(r.IsEmpty());
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 }
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 TEST(CacheIndex, Payload)
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 {
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 Orthanc::CacheIndex<std::string, int> r;
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 r.Add("a", 420);
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 r.Add("b", 421);
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 r.Add("c", 422);
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 r.Add("d", 423);
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 r.TagAsMostRecent("a");
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 r.TagAsMostRecent("d");
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 r.TagAsMostRecent("b");
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 r.TagAsMostRecent("c");
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 r.TagAsMostRecent("d");
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 r.TagAsMostRecent("c");
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 ASSERT_TRUE(r.Contains("b"));
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 ASSERT_EQ(421, r.Invalidate("b"));
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 ASSERT_FALSE(r.Contains("b"));
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 int p;
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
57 ASSERT_TRUE(r.Contains("a", p)); ASSERT_EQ(420, p);
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
58 ASSERT_TRUE(r.Contains("c", p)); ASSERT_EQ(422, p);
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
59 ASSERT_TRUE(r.Contains("d", p)); ASSERT_EQ(423, p);
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
60
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 ASSERT_EQ("a", r.RemoveOldest(p)); ASSERT_EQ(420, p);
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 ASSERT_EQ("d", r.RemoveOldest(p)); ASSERT_EQ(423, p);
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 ASSERT_EQ("c", r.RemoveOldest(p)); ASSERT_EQ(422, p);
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 ASSERT_TRUE(r.IsEmpty());
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 }
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
69
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
70
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
71 namespace
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
72 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
73 class Integer : public Orthanc::IDynamicObject
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
74 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
75 private:
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
76 std::string& log_;
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
77 int value_;
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 public:
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
80 Integer(std::string& log, int v) : log_(log), value_(v)
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
81 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
82 }
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
83
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
84 virtual ~Integer()
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
85 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
86 LOG(INFO) << "Removing cache entry for " << value_;
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
87 log_ += boost::lexical_cast<std::string>(value_) + " ";
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
88 }
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
89
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
90 int GetValue() const
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
91 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
92 return value_;
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
93 }
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
94 };
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
95
284
06aa7b7b6723 implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 283
diff changeset
96 class IntegerProvider : public Orthanc::ICachePageProvider
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
97 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
98 public:
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
99 std::string log_;
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
100
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
101 Orthanc::IDynamicObject* Provide(const std::string& s)
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
102 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
103 LOG(INFO) << "Providing " << s;
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
104 return new Integer(log_, boost::lexical_cast<int>(s));
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
105 }
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 };
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 }
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
108
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
109
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
110 TEST(MemoryCache, Basic)
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
111 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
112 IntegerProvider provider;
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
113
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
114 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
115 Orthanc::MemoryCache cache(provider, 3);
285
4031f73fe0e4 access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 284
diff changeset
116 cache.Access("42"); // 42 -> exit
4031f73fe0e4 access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 284
diff changeset
117 cache.Access("43"); // 43, 42 -> exit
4031f73fe0e4 access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 284
diff changeset
118 cache.Access("45"); // 45, 43, 42 -> exit
4031f73fe0e4 access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 284
diff changeset
119 cache.Access("42"); // 42, 45, 43 -> exit
4031f73fe0e4 access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 284
diff changeset
120 cache.Access("43"); // 43, 42, 45 -> exit
4031f73fe0e4 access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 284
diff changeset
121 cache.Access("47"); // 45 is removed; 47, 43, 42 -> exit
4031f73fe0e4 access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 284
diff changeset
122 cache.Access("44"); // 42 is removed; 44, 47, 43 -> exit
4031f73fe0e4 access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 284
diff changeset
123 cache.Access("42"); // 43 is removed; 42, 44, 47 -> exit
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
124 // Closing the cache: 47, 44, 42 are successively removed
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
125 }
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
126
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
127 ASSERT_EQ("45 42 43 47 44 42 ", provider.log_);
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
128 }