comparison UnitTests/MemoryCache.cpp @ 286:727a6d766dde

merge
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 13 Dec 2012 14:52:57 +0100
parents 4031f73fe0e4
children f59e4518fd57
comparison
equal deleted inserted replaced
281:e5402c368b21 286:727a6d766dde
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(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;
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
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
69
70
71 namespace
72 {
73 class Integer : public Orthanc::IDynamicObject
74 {
75 private:
76 std::string& log_;
77 int value_;
78
79 public:
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
96 class IntegerProvider : public Orthanc::ICachePageProvider
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 }
106 };
107 }
108
109
110 TEST(MemoryCache, Basic)
111 {
112 IntegerProvider provider;
113
114 {
115 Orthanc::MemoryCache cache(provider, 3);
116 cache.Access("42"); // 42 -> exit
117 cache.Access("43"); // 43, 42 -> exit
118 cache.Access("45"); // 45, 43, 42 -> exit
119 cache.Access("42"); // 42, 45, 43 -> exit
120 cache.Access("43"); // 43, 42, 45 -> exit
121 cache.Access("47"); // 45 is removed; 47, 43, 42 -> exit
122 cache.Access("44"); // 42 is removed; 44, 47, 43 -> exit
123 cache.Access("42"); // 43 is removed; 42, 44, 47 -> exit
124 // Closing the cache: 47, 44, 42 are successively removed
125 }
126
127 ASSERT_EQ("45 42 43 47 44 42 ", provider.log_);
128 }