comparison UnitTestsSources/UnitTestsMain.cpp @ 0:02f7a0400a91

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 25 Feb 2015 13:45:35 +0100
parents
children 09421764214b
comparison
equal deleted inserted replaced
-1:000000000000 0:02f7a0400a91
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 *
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU Affero General Public License
8 * as published by the Free Software Foundation, either version 3 of
9 * the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 **/
19
20
21 #include <gtest/gtest.h>
22 #include <boost/lexical_cast.hpp>
23
24 static int argc_;
25 static char** argv_;
26
27 #include "../Orthanc/OrthancException.h"
28 #include "../Orthanc/Toolbox.h"
29 #include "../Orthanc/ImageFormats/ImageBuffer.h"
30 #include "../Orthanc/ImageFormats/PngWriter.h"
31 #include "../Plugin/Cache/CacheManager.h"
32 #include "../Plugin/Cache/CacheScheduler.h"
33 #include "../Plugin/Cache/ICacheFactory.h"
34 #include "../Plugin/Cache/ICacheFactory.h"
35 #include "../Plugin/JpegWriter.h"
36
37 using namespace OrthancPlugins;
38
39
40 class CacheManagerTest : public testing::Test
41 {
42 private:
43 std::auto_ptr<Orthanc::FilesystemStorage> storage_;
44 std::auto_ptr<Orthanc::SQLite::Connection> db_;
45 std::auto_ptr<CacheManager> cache_;
46
47 public:
48 virtual void SetUp()
49 {
50 storage_.reset(new Orthanc::FilesystemStorage("UnitTestsResults"));
51 storage_->Clear();
52 Orthanc::Toolbox::RemoveFile("UnitTestsResults/cache.db");
53
54 db_.reset(new Orthanc::SQLite::Connection());
55 db_->Open("UnitTestsResults/cache.db");
56
57 cache_.reset(new CacheManager(*db_, *storage_));
58 cache_->SetSanityCheckEnabled(true);
59 }
60
61 virtual void TearDown()
62 {
63 cache_.reset(NULL);
64 db_.reset(NULL);
65 storage_.reset(NULL);
66 }
67
68 CacheManager& GetCache()
69 {
70 return *cache_;
71 }
72
73 Orthanc::FilesystemStorage& GetStorage()
74 {
75 return *storage_;
76 }
77 };
78
79
80
81 class TestF : public ICacheFactory
82 {
83 private:
84 int bundle_;
85
86 public:
87 TestF(int bundle) : bundle_(bundle)
88 {
89 }
90
91 virtual bool Create(std::string& content,
92 const std::string& key)
93 {
94 content = "Bundle " + boost::lexical_cast<std::string>(bundle_) + ", item " + key;
95 return true;
96 }
97 };
98
99
100 TEST_F(CacheManagerTest, DefaultQuota)
101 {
102 std::set<std::string> f;
103 GetStorage().ListAllFiles(f);
104 ASSERT_EQ(0, f.size());
105
106 GetCache().SetDefaultQuota(10, 0);
107 for (int i = 0; i < 30; i++)
108 {
109 GetStorage().ListAllFiles(f);
110 ASSERT_EQ(i >= 10 ? 10 : i, f.size());
111 std::string s = boost::lexical_cast<std::string>(i);
112 GetCache().Store(0, s, "Test " + s);
113 }
114
115 GetStorage().ListAllFiles(f);
116 ASSERT_EQ(10, f.size());
117
118 for (int i = 0; i < 30; i++)
119 {
120 ASSERT_EQ(i >= 20, GetCache().IsCached(0, boost::lexical_cast<std::string>(i)));
121 }
122
123 GetCache().SetDefaultQuota(5, 0);
124 GetStorage().ListAllFiles(f);
125 ASSERT_EQ(5, f.size());
126 for (int i = 0; i < 30; i++)
127 {
128 ASSERT_EQ(i >= 25, GetCache().IsCached(0, boost::lexical_cast<std::string>(i)));
129 }
130
131 for (int i = 0; i < 15; i++)
132 {
133 std::string s = boost::lexical_cast<std::string>(i);
134 GetCache().Store(0, s, "Test " + s);
135 }
136
137 GetStorage().ListAllFiles(f);
138 ASSERT_EQ(5, f.size());
139
140 for (int i = 0; i < 50; i++)
141 {
142 std::string s = boost::lexical_cast<std::string>(i);
143 if (i >= 10 && i < 15)
144 {
145 std::string tmp;
146 ASSERT_TRUE(GetCache().IsCached(0, s));
147 ASSERT_TRUE(GetCache().Access(tmp, 0, s));
148 ASSERT_EQ("Test " + s, tmp);
149 }
150 else
151 {
152 ASSERT_FALSE(GetCache().IsCached(0, s));
153 }
154 }
155 }
156
157
158
159 TEST_F(CacheManagerTest, Invalidate)
160 {
161 GetCache().SetDefaultQuota(10, 0);
162 for (int i = 0; i < 30; i++)
163 {
164 std::string s = boost::lexical_cast<std::string>(i);
165 GetCache().Store(0, s, "Test " + s);
166 }
167
168 std::set<std::string> f;
169 GetStorage().ListAllFiles(f);
170 ASSERT_EQ(10, f.size());
171
172 GetCache().Invalidate(0, "25");
173 GetStorage().ListAllFiles(f);
174 ASSERT_EQ(9, f.size());
175 for (int i = 0; i < 50; i++)
176 {
177 std::string s = boost::lexical_cast<std::string>(i);
178 ASSERT_EQ((i >= 20 && i < 30 && i != 25), GetCache().IsCached(0, s));
179 }
180
181 for (int i = 0; i < 50; i++)
182 {
183 GetCache().Invalidate(0, boost::lexical_cast<std::string>(i));
184 }
185
186 GetStorage().ListAllFiles(f);
187 ASSERT_EQ(0, f.size());
188 }
189
190
191
192 TEST(JpegWriter, Basic)
193 {
194 Orthanc::ImageBuffer img(16, 16, Orthanc::PixelFormat_Grayscale8);
195 Orthanc::ImageAccessor accessor = img.GetAccessor();
196 for (int y = 0, value = 0; y < img.GetHeight(); y++)
197 {
198 uint8_t* p = reinterpret_cast<uint8_t*>(accessor.GetRow(y));
199 for (int x = 0; x < img.GetWidth(); x++, p++)
200 {
201 *p = value++;
202 }
203 }
204
205 JpegWriter w;
206 w.WriteToFile("UnitTestsResults/hello.jpg", accessor);
207
208 std::string s;
209 w.WriteToMemory(s, accessor);
210 Orthanc::Toolbox::WriteFile(s, "UnitTestsResults/hello2.jpg");
211 }
212
213
214
215 int main(int argc, char **argv)
216 {
217 argc_ = argc;
218 argv_ = argv;
219
220 ::testing::InitGoogleTest(&argc, argv);
221
222 return RUN_ALL_TESTS();
223 }