Mercurial > hg > orthanc
annotate UnitTests/MemoryCache.cpp @ 492:f3d4193c571a
switch to jsoncpp-0.6.0-rc2
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 17 Jul 2013 11:19:09 +0200 |
parents | 4031f73fe0e4 |
children | f59e4518fd57 |
rev | line source |
---|---|
282 | 1 #include "gtest/gtest.h" |
2 | |
283 | 3 #include <glog/logging.h> |
282 | 4 #include <memory> |
283 | 5 #include <boost/thread.hpp> |
6 #include <boost/lexical_cast.hpp> | |
282 | 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 | 9 |
10 | |
11 TEST(CacheIndex, Basic) | |
12 { | |
13 Orthanc::CacheIndex<std::string> r; | |
14 | |
15 r.Add("d"); | |
16 r.Add("a"); | |
17 r.Add("c"); | |
18 r.Add("b"); | |
19 | |
20 r.TagAsMostRecent("a"); | |
21 r.TagAsMostRecent("d"); | |
22 r.TagAsMostRecent("b"); | |
23 r.TagAsMostRecent("c"); | |
24 r.TagAsMostRecent("d"); | |
25 r.TagAsMostRecent("c"); | |
26 | |
27 ASSERT_EQ("a", r.RemoveOldest()); | |
28 ASSERT_EQ("b", r.RemoveOldest()); | |
29 ASSERT_EQ("d", r.RemoveOldest()); | |
30 ASSERT_EQ("c", r.RemoveOldest()); | |
31 | |
32 ASSERT_TRUE(r.IsEmpty()); | |
33 } | |
34 | |
35 | |
36 TEST(CacheIndex, Payload) | |
37 { | |
38 Orthanc::CacheIndex<std::string, int> r; | |
39 | |
40 r.Add("a", 420); | |
41 r.Add("b", 421); | |
42 r.Add("c", 422); | |
43 r.Add("d", 423); | |
44 | |
45 r.TagAsMostRecent("a"); | |
46 r.TagAsMostRecent("d"); | |
47 r.TagAsMostRecent("b"); | |
48 r.TagAsMostRecent("c"); | |
49 r.TagAsMostRecent("d"); | |
50 r.TagAsMostRecent("c"); | |
51 | |
52 ASSERT_TRUE(r.Contains("b")); | |
53 ASSERT_EQ(421, r.Invalidate("b")); | |
54 ASSERT_FALSE(r.Contains("b")); | |
55 | |
56 int p; | |
283 | 57 ASSERT_TRUE(r.Contains("a", p)); ASSERT_EQ(420, p); |
58 ASSERT_TRUE(r.Contains("c", p)); ASSERT_EQ(422, p); | |
59 ASSERT_TRUE(r.Contains("d", p)); ASSERT_EQ(423, p); | |
60 | |
282 | 61 ASSERT_EQ("a", r.RemoveOldest(p)); ASSERT_EQ(420, p); |
62 ASSERT_EQ("d", r.RemoveOldest(p)); ASSERT_EQ(423, p); | |
63 ASSERT_EQ("c", r.RemoveOldest(p)); ASSERT_EQ(422, p); | |
64 | |
65 ASSERT_TRUE(r.IsEmpty()); | |
66 } | |
67 | |
68 | |
283 | 69 |
70 | |
71 namespace | |
72 { | |
73 class Integer : public Orthanc::IDynamicObject | |
74 { | |
75 private: | |
76 std::string& log_; | |
77 int value_; | |
282 | 78 |
79 public: | |
283 | 80 Integer(std::string& log, int v) : log_(log), value_(v) |
81 { | |
82 } | |
83 | |
84 virtual ~Integer() | |
85 { | |
86 LOG(INFO) << "Removing cache entry for " << value_; | |
87 log_ += boost::lexical_cast<std::string>(value_) + " "; | |
88 } | |
89 | |
90 int GetValue() const | |
91 { | |
92 return value_; | |
93 } | |
94 }; | |
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 | 97 { |
98 public: | |
99 std::string log_; | |
100 | |
101 Orthanc::IDynamicObject* Provide(const std::string& s) | |
102 { | |
103 LOG(INFO) << "Providing " << s; | |
104 return new Integer(log_, boost::lexical_cast<int>(s)); | |
105 } | |
282 | 106 }; |
107 } | |
283 | 108 |
109 | |
110 TEST(MemoryCache, Basic) | |
111 { | |
112 IntegerProvider provider; | |
113 | |
114 { | |
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 | 124 // Closing the cache: 47, 44, 42 are successively removed |
125 } | |
126 | |
127 ASSERT_EQ("45 42 43 47 44 42 ", provider.log_); | |
128 } |