Mercurial > hg > orthanc
comparison UnitTestsSources/FileStorage.cpp @ 762:45b16f67259c lua-scripting
integration mainline -> lua-scripting
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 22 Apr 2014 16:47:21 +0200 |
parents | 9e3f21441903 |
children | 78b9f64b6b7b |
comparison
equal
deleted
inserted
replaced
409:63f707278fc8 | 762:45b16f67259c |
---|---|
1 #include "gtest/gtest.h" | |
2 | |
3 #include <ctype.h> | |
4 #include <glog/logging.h> | |
5 | |
6 #include "../Core/FileStorage/FileStorage.h" | |
7 #include "../OrthancServer/ServerIndex.h" | |
8 #include "../Core/Toolbox.h" | |
9 #include "../Core/OrthancException.h" | |
10 #include "../Core/Uuid.h" | |
11 #include "../Core/HttpServer/FilesystemHttpSender.h" | |
12 #include "../Core/HttpServer/BufferHttpSender.h" | |
13 #include "../Core/FileStorage/FileStorageAccessor.h" | |
14 #include "../Core/FileStorage/CompressedFileStorageAccessor.h" | |
15 | |
16 using namespace Orthanc; | |
17 | |
18 | |
19 static void StringToVector(std::vector<uint8_t>& v, | |
20 const std::string& s) | |
21 { | |
22 v.resize(s.size()); | |
23 for (size_t i = 0; i < s.size(); i++) | |
24 v[i] = s[i]; | |
25 } | |
26 | |
27 | |
28 TEST(FileStorage, Basic) | |
29 { | |
30 FileStorage s("FileStorageUnitTests"); | |
31 | |
32 std::string data = Toolbox::GenerateUuid(); | |
33 std::string uid = s.Create(data); | |
34 std::string d; | |
35 s.ReadFile(d, uid); | |
36 ASSERT_EQ(d.size(), data.size()); | |
37 ASSERT_FALSE(memcmp(&d[0], &data[0], data.size())); | |
38 ASSERT_EQ(s.GetCompressedSize(uid), data.size()); | |
39 } | |
40 | |
41 TEST(FileStorage, Basic2) | |
42 { | |
43 FileStorage s("FileStorageUnitTests"); | |
44 | |
45 std::vector<uint8_t> data; | |
46 StringToVector(data, Toolbox::GenerateUuid()); | |
47 std::string uid = s.Create(data); | |
48 std::string d; | |
49 s.ReadFile(d, uid); | |
50 ASSERT_EQ(d.size(), data.size()); | |
51 ASSERT_FALSE(memcmp(&d[0], &data[0], data.size())); | |
52 ASSERT_EQ(s.GetCompressedSize(uid), data.size()); | |
53 } | |
54 | |
55 TEST(FileStorage, EndToEnd) | |
56 { | |
57 FileStorage s("FileStorageUnitTests"); | |
58 s.Clear(); | |
59 | |
60 std::list<std::string> u; | |
61 for (unsigned int i = 0; i < 10; i++) | |
62 { | |
63 u.push_back(s.Create(Toolbox::GenerateUuid())); | |
64 } | |
65 | |
66 std::set<std::string> ss; | |
67 s.ListAllFiles(ss); | |
68 ASSERT_EQ(10u, ss.size()); | |
69 | |
70 unsigned int c = 0; | |
71 for (std::list<std::string>::iterator | |
72 i = u.begin(); i != u.end(); i++, c++) | |
73 { | |
74 ASSERT_TRUE(ss.find(*i) != ss.end()); | |
75 if (c < 5) | |
76 s.Remove(*i); | |
77 } | |
78 | |
79 s.ListAllFiles(ss); | |
80 ASSERT_EQ(5u, ss.size()); | |
81 | |
82 s.Clear(); | |
83 s.ListAllFiles(ss); | |
84 ASSERT_EQ(0u, ss.size()); | |
85 } | |
86 | |
87 | |
88 TEST(FileStorageAccessor, Simple) | |
89 { | |
90 FileStorage s("FileStorageUnitTests"); | |
91 FileStorageAccessor accessor(s); | |
92 | |
93 std::string data = "Hello world"; | |
94 FileInfo info = accessor.Write(data, FileContentType_Dicom); | |
95 | |
96 std::string r; | |
97 accessor.Read(r, info.GetUuid()); | |
98 | |
99 ASSERT_EQ(data, r); | |
100 ASSERT_EQ(CompressionType_None, info.GetCompressionType()); | |
101 ASSERT_EQ(11u, info.GetUncompressedSize()); | |
102 ASSERT_EQ(11u, info.GetCompressedSize()); | |
103 ASSERT_EQ(FileContentType_Dicom, info.GetContentType()); | |
104 } | |
105 | |
106 | |
107 TEST(FileStorageAccessor, NoCompression) | |
108 { | |
109 FileStorage s("FileStorageUnitTests"); | |
110 CompressedFileStorageAccessor accessor(s); | |
111 | |
112 accessor.SetCompressionForNextOperations(CompressionType_None); | |
113 std::string data = "Hello world"; | |
114 FileInfo info = accessor.Write(data, FileContentType_Dicom); | |
115 | |
116 std::string r; | |
117 accessor.Read(r, info.GetUuid()); | |
118 | |
119 ASSERT_EQ(data, r); | |
120 ASSERT_EQ(CompressionType_None, info.GetCompressionType()); | |
121 ASSERT_EQ(11u, info.GetUncompressedSize()); | |
122 ASSERT_EQ(11u, info.GetCompressedSize()); | |
123 ASSERT_EQ(FileContentType_Dicom, info.GetContentType()); | |
124 } | |
125 | |
126 | |
127 TEST(FileStorageAccessor, NoCompression2) | |
128 { | |
129 FileStorage s("FileStorageUnitTests"); | |
130 CompressedFileStorageAccessor accessor(s); | |
131 | |
132 accessor.SetCompressionForNextOperations(CompressionType_None); | |
133 std::vector<uint8_t> data; | |
134 StringToVector(data, "Hello world"); | |
135 FileInfo info = accessor.Write(data, FileContentType_Dicom); | |
136 | |
137 std::string r; | |
138 accessor.Read(r, info.GetUuid()); | |
139 | |
140 ASSERT_EQ(0, memcmp(&r[0], &data[0], data.size())); | |
141 ASSERT_EQ(CompressionType_None, info.GetCompressionType()); | |
142 ASSERT_EQ(11u, info.GetUncompressedSize()); | |
143 ASSERT_EQ(11u, info.GetCompressedSize()); | |
144 ASSERT_EQ(FileContentType_Dicom, info.GetContentType()); | |
145 } | |
146 | |
147 | |
148 TEST(FileStorageAccessor, Compression) | |
149 { | |
150 FileStorage s("FileStorageUnitTests"); | |
151 CompressedFileStorageAccessor accessor(s); | |
152 | |
153 accessor.SetCompressionForNextOperations(CompressionType_Zlib); | |
154 std::string data = "Hello world"; | |
155 FileInfo info = accessor.Write(data, FileContentType_Dicom); | |
156 | |
157 std::string r; | |
158 accessor.Read(r, info.GetUuid()); | |
159 | |
160 ASSERT_EQ(data, r); | |
161 ASSERT_EQ(CompressionType_Zlib, info.GetCompressionType()); | |
162 ASSERT_EQ(11u, info.GetUncompressedSize()); | |
163 ASSERT_EQ(FileContentType_Dicom, info.GetContentType()); | |
164 } | |
165 | |
166 | |
167 TEST(FileStorageAccessor, Mix) | |
168 { | |
169 FileStorage s("FileStorageUnitTests"); | |
170 CompressedFileStorageAccessor accessor(s); | |
171 | |
172 std::string r; | |
173 std::string compressedData = "Hello"; | |
174 std::string uncompressedData = "HelloWorld"; | |
175 | |
176 accessor.SetCompressionForNextOperations(CompressionType_Zlib); | |
177 FileInfo compressedInfo = accessor.Write(compressedData, FileContentType_Dicom); | |
178 | |
179 accessor.SetCompressionForNextOperations(CompressionType_None); | |
180 FileInfo uncompressedInfo = accessor.Write(uncompressedData, FileContentType_Dicom); | |
181 | |
182 accessor.SetCompressionForNextOperations(CompressionType_Zlib); | |
183 accessor.Read(r, compressedInfo.GetUuid()); | |
184 ASSERT_EQ(compressedData, r); | |
185 | |
186 accessor.SetCompressionForNextOperations(CompressionType_None); | |
187 accessor.Read(r, compressedInfo.GetUuid()); | |
188 ASSERT_NE(compressedData, r); | |
189 | |
190 /* | |
191 // This test is too slow on Windows | |
192 accessor.SetCompressionForNextOperations(CompressionType_Zlib); | |
193 ASSERT_THROW(accessor.Read(r, uncompressedInfo.GetUuid()), OrthancException); | |
194 */ | |
195 } |