Mercurial > hg > orthanc
annotate OrthancFramework/UnitTestsSources/FrameworkTests.cpp @ 4055:9214e3a7b0a2 framework
moving FromDcmtkTests.cpp from OrthancServer to OrthancFramework
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 11 Jun 2020 12:52:09 +0200 |
parents | 05b8fd21089c |
children | 0953b3dc3261 |
rev | line source |
---|---|
3987 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU General Public License as | |
9 * published by the Free Software Foundation, either version 3 of the | |
10 * License, or (at your option) any later version. | |
11 * | |
12 * In addition, as a special exception, the copyright holders of this | |
13 * program give permission to link the code of its release with the | |
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
15 * that use the same license as the "OpenSSL" library), and distribute | |
16 * the linked executables. You must obey the GNU General Public License | |
17 * in all respects for all of the code used other than "OpenSSL". If you | |
18 * modify file(s) with this exception, you may extend this exception to | |
19 * your version of the file(s), but you are not obligated to do so. If | |
20 * you do not wish to do so, delete this exception statement from your | |
21 * version. If you delete this exception statement from all source files | |
22 * in the program, then also delete it here. | |
23 * | |
24 * This program is distributed in the hope that it will be useful, but | |
25 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
27 * General Public License for more details. | |
28 * | |
29 * You should have received a copy of the GNU General Public License | |
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
31 **/ | |
32 | |
33 | |
3992
f9863630ec7f
working on the shared library for Orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3987
diff
changeset
|
34 #if ORTHANC_UNIT_TESTS_LINK_FRAMEWORK == 1 |
4014
27628b0f6ada
merging logging code for plugins and files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3992
diff
changeset
|
35 # include <OrthancFramework.h> |
3992
f9863630ec7f
working on the shared library for Orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3987
diff
changeset
|
36 #endif |
f9863630ec7f
working on the shared library for Orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3987
diff
changeset
|
37 |
4045 | 38 #include "../Sources/EnumerationDictionary.h" |
3987 | 39 |
40 #include "gtest/gtest.h" | |
41 | |
42 #include <ctype.h> | |
43 | |
4045 | 44 #include "../Sources/DicomFormat/DicomTag.h" |
45 #include "../Sources/FileBuffer.h" | |
46 #include "../Sources/HttpServer/HttpToolbox.h" | |
47 #include "../Sources/Logging.h" | |
48 #include "../Sources/MetricsRegistry.h" | |
49 #include "../Sources/OrthancException.h" | |
50 #include "../Sources/SystemToolbox.h" | |
51 #include "../Sources/TemporaryFile.h" | |
52 #include "../Sources/Toolbox.h" | |
3987 | 53 |
54 | |
55 using namespace Orthanc; | |
56 | |
57 | |
58 TEST(Uuid, Generation) | |
59 { | |
60 for (int i = 0; i < 10; i++) | |
61 { | |
62 std::string s = Toolbox::GenerateUuid(); | |
63 ASSERT_TRUE(Toolbox::IsUuid(s)); | |
64 } | |
65 } | |
66 | |
67 TEST(Uuid, Test) | |
68 { | |
69 ASSERT_FALSE(Toolbox::IsUuid("")); | |
70 ASSERT_FALSE(Toolbox::IsUuid("012345678901234567890123456789012345")); | |
71 ASSERT_TRUE(Toolbox::IsUuid("550e8400-e29b-41d4-a716-446655440000")); | |
72 ASSERT_FALSE(Toolbox::IsUuid("550e8400-e29b-41d4-a716-44665544000_")); | |
73 ASSERT_FALSE(Toolbox::IsUuid("01234567890123456789012345678901234_")); | |
74 ASSERT_FALSE(Toolbox::StartsWithUuid("550e8400-e29b-41d4-a716-44665544000")); | |
75 ASSERT_TRUE(Toolbox::StartsWithUuid("550e8400-e29b-41d4-a716-446655440000")); | |
76 ASSERT_TRUE(Toolbox::StartsWithUuid("550e8400-e29b-41d4-a716-446655440000 ok")); | |
77 ASSERT_FALSE(Toolbox::StartsWithUuid("550e8400-e29b-41d4-a716-446655440000ok")); | |
78 } | |
79 | |
80 TEST(Toolbox, IsSHA1) | |
81 { | |
82 ASSERT_FALSE(Toolbox::IsSHA1("")); | |
83 ASSERT_FALSE(Toolbox::IsSHA1("01234567890123456789012345678901234567890123")); | |
84 ASSERT_FALSE(Toolbox::IsSHA1("012345678901234567890123456789012345678901234")); | |
85 ASSERT_TRUE(Toolbox::IsSHA1("b5ed549f-956400ce-69a8c063-bf5b78be-2732a4b9")); | |
86 | |
87 std::string sha = " b5ed549f-956400ce-69a8c063-bf5b78be-2732a4b9 "; | |
88 ASSERT_TRUE(Toolbox::IsSHA1(sha)); | |
89 sha[3] = '\0'; | |
90 sha[53] = '\0'; | |
91 ASSERT_TRUE(Toolbox::IsSHA1(sha)); | |
92 sha[40] = '\0'; | |
93 ASSERT_FALSE(Toolbox::IsSHA1(sha)); | |
94 ASSERT_FALSE(Toolbox::IsSHA1(" ")); | |
95 | |
96 ASSERT_TRUE(Toolbox::IsSHA1("16738bc3-e47ed42a-43ce044c-a3414a45-cb069bd0")); | |
97 | |
98 std::string s; | |
99 Toolbox::ComputeSHA1(s, "The quick brown fox jumps over the lazy dog"); | |
100 ASSERT_TRUE(Toolbox::IsSHA1(s)); | |
101 ASSERT_EQ("2fd4e1c6-7a2d28fc-ed849ee1-bb76e739-1b93eb12", s); | |
102 | |
103 ASSERT_FALSE(Toolbox::IsSHA1("b5ed549f-956400ce-69a8c063-bf5b78be-2732a4b_")); | |
104 } | |
105 | |
106 | |
107 TEST(ParseGetArguments, Basic) | |
108 { | |
109 IHttpHandler::GetArguments b; | |
110 HttpToolbox::ParseGetArguments(b, "aaa=baaa&bb=a&aa=c"); | |
111 | |
112 IHttpHandler::Arguments a; | |
113 HttpToolbox::CompileGetArguments(a, b); | |
114 | |
115 ASSERT_EQ(3u, a.size()); | |
116 ASSERT_EQ(a["aaa"], "baaa"); | |
117 ASSERT_EQ(a["bb"], "a"); | |
118 ASSERT_EQ(a["aa"], "c"); | |
119 } | |
120 | |
121 TEST(ParseGetArguments, BasicEmpty) | |
122 { | |
123 IHttpHandler::GetArguments b; | |
124 HttpToolbox::ParseGetArguments(b, "aaa&bb=aa&aa"); | |
125 | |
126 IHttpHandler::Arguments a; | |
127 HttpToolbox::CompileGetArguments(a, b); | |
128 | |
129 ASSERT_EQ(3u, a.size()); | |
130 ASSERT_EQ(a["aaa"], ""); | |
131 ASSERT_EQ(a["bb"], "aa"); | |
132 ASSERT_EQ(a["aa"], ""); | |
133 } | |
134 | |
135 TEST(ParseGetArguments, Single) | |
136 { | |
137 IHttpHandler::GetArguments b; | |
138 HttpToolbox::ParseGetArguments(b, "aaa=baaa"); | |
139 | |
140 IHttpHandler::Arguments a; | |
141 HttpToolbox::CompileGetArguments(a, b); | |
142 | |
143 ASSERT_EQ(1u, a.size()); | |
144 ASSERT_EQ(a["aaa"], "baaa"); | |
145 } | |
146 | |
147 TEST(ParseGetArguments, SingleEmpty) | |
148 { | |
149 IHttpHandler::GetArguments b; | |
150 HttpToolbox::ParseGetArguments(b, "aaa"); | |
151 | |
152 IHttpHandler::Arguments a; | |
153 HttpToolbox::CompileGetArguments(a, b); | |
154 | |
155 ASSERT_EQ(1u, a.size()); | |
156 ASSERT_EQ(a["aaa"], ""); | |
157 } | |
158 | |
159 TEST(ParseGetQuery, Test1) | |
160 { | |
161 UriComponents uri; | |
162 IHttpHandler::GetArguments b; | |
163 HttpToolbox::ParseGetQuery(uri, b, "/instances/test/world?aaa=baaa&bb=a&aa=c"); | |
164 | |
165 IHttpHandler::Arguments a; | |
166 HttpToolbox::CompileGetArguments(a, b); | |
167 | |
168 ASSERT_EQ(3u, uri.size()); | |
169 ASSERT_EQ("instances", uri[0]); | |
170 ASSERT_EQ("test", uri[1]); | |
171 ASSERT_EQ("world", uri[2]); | |
172 ASSERT_EQ(3u, a.size()); | |
173 ASSERT_EQ(a["aaa"], "baaa"); | |
174 ASSERT_EQ(a["bb"], "a"); | |
175 ASSERT_EQ(a["aa"], "c"); | |
176 } | |
177 | |
178 TEST(ParseGetQuery, Test2) | |
179 { | |
180 UriComponents uri; | |
181 IHttpHandler::GetArguments b; | |
182 HttpToolbox::ParseGetQuery(uri, b, "/instances/test/world"); | |
183 | |
184 IHttpHandler::Arguments a; | |
185 HttpToolbox::CompileGetArguments(a, b); | |
186 | |
187 ASSERT_EQ(3u, uri.size()); | |
188 ASSERT_EQ("instances", uri[0]); | |
189 ASSERT_EQ("test", uri[1]); | |
190 ASSERT_EQ("world", uri[2]); | |
191 ASSERT_EQ(0u, a.size()); | |
192 } | |
193 | |
194 TEST(Uri, SplitUriComponents) | |
195 { | |
196 UriComponents c, d; | |
197 Toolbox::SplitUriComponents(c, "/cou/hello/world"); | |
198 ASSERT_EQ(3u, c.size()); | |
199 ASSERT_EQ("cou", c[0]); | |
200 ASSERT_EQ("hello", c[1]); | |
201 ASSERT_EQ("world", c[2]); | |
202 | |
203 Toolbox::SplitUriComponents(c, "/cou/hello/world/"); | |
204 ASSERT_EQ(3u, c.size()); | |
205 ASSERT_EQ("cou", c[0]); | |
206 ASSERT_EQ("hello", c[1]); | |
207 ASSERT_EQ("world", c[2]); | |
208 | |
209 Toolbox::SplitUriComponents(c, "/cou/hello/world/a"); | |
210 ASSERT_EQ(4u, c.size()); | |
211 ASSERT_EQ("cou", c[0]); | |
212 ASSERT_EQ("hello", c[1]); | |
213 ASSERT_EQ("world", c[2]); | |
214 ASSERT_EQ("a", c[3]); | |
215 | |
216 Toolbox::SplitUriComponents(c, "/"); | |
217 ASSERT_EQ(0u, c.size()); | |
218 | |
219 Toolbox::SplitUriComponents(c, "/hello"); | |
220 ASSERT_EQ(1u, c.size()); | |
221 ASSERT_EQ("hello", c[0]); | |
222 | |
223 Toolbox::SplitUriComponents(c, "/hello/"); | |
224 ASSERT_EQ(1u, c.size()); | |
225 ASSERT_EQ("hello", c[0]); | |
226 | |
227 ASSERT_THROW(Toolbox::SplitUriComponents(c, ""), OrthancException); | |
228 ASSERT_THROW(Toolbox::SplitUriComponents(c, "a"), OrthancException); | |
229 ASSERT_THROW(Toolbox::SplitUriComponents(c, "/coucou//coucou"), OrthancException); | |
230 | |
231 c.clear(); | |
232 c.push_back("test"); | |
233 ASSERT_EQ("/", Toolbox::FlattenUri(c, 10)); | |
234 } | |
235 | |
236 | |
237 TEST(Uri, Truncate) | |
238 { | |
239 UriComponents c, d; | |
240 Toolbox::SplitUriComponents(c, "/cou/hello/world"); | |
241 | |
242 Toolbox::TruncateUri(d, c, 0); | |
243 ASSERT_EQ(3u, d.size()); | |
244 ASSERT_EQ("cou", d[0]); | |
245 ASSERT_EQ("hello", d[1]); | |
246 ASSERT_EQ("world", d[2]); | |
247 | |
248 Toolbox::TruncateUri(d, c, 1); | |
249 ASSERT_EQ(2u, d.size()); | |
250 ASSERT_EQ("hello", d[0]); | |
251 ASSERT_EQ("world", d[1]); | |
252 | |
253 Toolbox::TruncateUri(d, c, 2); | |
254 ASSERT_EQ(1u, d.size()); | |
255 ASSERT_EQ("world", d[0]); | |
256 | |
257 Toolbox::TruncateUri(d, c, 3); | |
258 ASSERT_EQ(0u, d.size()); | |
259 | |
260 Toolbox::TruncateUri(d, c, 4); | |
261 ASSERT_EQ(0u, d.size()); | |
262 | |
263 Toolbox::TruncateUri(d, c, 5); | |
264 ASSERT_EQ(0u, d.size()); | |
265 } | |
266 | |
267 | |
268 TEST(Uri, Child) | |
269 { | |
270 UriComponents c1; Toolbox::SplitUriComponents(c1, "/hello/world"); | |
271 UriComponents c2; Toolbox::SplitUriComponents(c2, "/hello/hello"); | |
272 UriComponents c3; Toolbox::SplitUriComponents(c3, "/hello"); | |
273 UriComponents c4; Toolbox::SplitUriComponents(c4, "/world"); | |
274 UriComponents c5; Toolbox::SplitUriComponents(c5, "/"); | |
275 | |
276 ASSERT_TRUE(Toolbox::IsChildUri(c1, c1)); | |
277 ASSERT_FALSE(Toolbox::IsChildUri(c1, c2)); | |
278 ASSERT_FALSE(Toolbox::IsChildUri(c1, c3)); | |
279 ASSERT_FALSE(Toolbox::IsChildUri(c1, c4)); | |
280 ASSERT_FALSE(Toolbox::IsChildUri(c1, c5)); | |
281 | |
282 ASSERT_FALSE(Toolbox::IsChildUri(c2, c1)); | |
283 ASSERT_TRUE(Toolbox::IsChildUri(c2, c2)); | |
284 ASSERT_FALSE(Toolbox::IsChildUri(c2, c3)); | |
285 ASSERT_FALSE(Toolbox::IsChildUri(c2, c4)); | |
286 ASSERT_FALSE(Toolbox::IsChildUri(c2, c5)); | |
287 | |
288 ASSERT_TRUE(Toolbox::IsChildUri(c3, c1)); | |
289 ASSERT_TRUE(Toolbox::IsChildUri(c3, c2)); | |
290 ASSERT_TRUE(Toolbox::IsChildUri(c3, c3)); | |
291 ASSERT_FALSE(Toolbox::IsChildUri(c3, c4)); | |
292 ASSERT_FALSE(Toolbox::IsChildUri(c3, c5)); | |
293 | |
294 ASSERT_FALSE(Toolbox::IsChildUri(c4, c1)); | |
295 ASSERT_FALSE(Toolbox::IsChildUri(c4, c2)); | |
296 ASSERT_FALSE(Toolbox::IsChildUri(c4, c3)); | |
297 ASSERT_TRUE(Toolbox::IsChildUri(c4, c4)); | |
298 ASSERT_FALSE(Toolbox::IsChildUri(c4, c5)); | |
299 | |
300 ASSERT_TRUE(Toolbox::IsChildUri(c5, c1)); | |
301 ASSERT_TRUE(Toolbox::IsChildUri(c5, c2)); | |
302 ASSERT_TRUE(Toolbox::IsChildUri(c5, c3)); | |
303 ASSERT_TRUE(Toolbox::IsChildUri(c5, c4)); | |
304 ASSERT_TRUE(Toolbox::IsChildUri(c5, c5)); | |
305 } | |
306 | |
307 TEST(Uri, AutodetectMimeType) | |
308 { | |
309 ASSERT_EQ(MimeType_Binary, SystemToolbox::AutodetectMimeType("../NOTES")); | |
310 ASSERT_EQ(MimeType_Binary, SystemToolbox::AutodetectMimeType("")); | |
311 ASSERT_EQ(MimeType_Binary, SystemToolbox::AutodetectMimeType("/")); | |
312 ASSERT_EQ(MimeType_Binary, SystemToolbox::AutodetectMimeType("a/a")); | |
313 ASSERT_EQ(MimeType_Binary, SystemToolbox::AutodetectMimeType("..\\a\\")); | |
314 ASSERT_EQ(MimeType_Binary, SystemToolbox::AutodetectMimeType("..\\a\\a")); | |
315 | |
316 ASSERT_EQ(MimeType_PlainText, SystemToolbox::AutodetectMimeType("../NOTES.txt")); | |
317 ASSERT_EQ(MimeType_PlainText, SystemToolbox::AutodetectMimeType("../coucou.xml/NOTES.txt")); | |
318 ASSERT_EQ(MimeType_Xml, SystemToolbox::AutodetectMimeType("..\\coucou.\\NOTES.xml")); | |
319 ASSERT_EQ(MimeType_Xml, SystemToolbox::AutodetectMimeType("../.xml")); | |
320 ASSERT_EQ(MimeType_Xml, SystemToolbox::AutodetectMimeType("../.XmL")); | |
321 | |
322 ASSERT_EQ(MimeType_JavaScript, SystemToolbox::AutodetectMimeType("NOTES.js")); | |
323 ASSERT_EQ(MimeType_Json, SystemToolbox::AutodetectMimeType("NOTES.json")); | |
324 ASSERT_EQ(MimeType_Pdf, SystemToolbox::AutodetectMimeType("NOTES.pdf")); | |
325 ASSERT_EQ(MimeType_Css, SystemToolbox::AutodetectMimeType("NOTES.css")); | |
326 ASSERT_EQ(MimeType_Html, SystemToolbox::AutodetectMimeType("NOTES.html")); | |
327 ASSERT_EQ(MimeType_PlainText, SystemToolbox::AutodetectMimeType("NOTES.txt")); | |
328 ASSERT_EQ(MimeType_Xml, SystemToolbox::AutodetectMimeType("NOTES.xml")); | |
329 ASSERT_EQ(MimeType_Gif, SystemToolbox::AutodetectMimeType("NOTES.gif")); | |
330 ASSERT_EQ(MimeType_Jpeg, SystemToolbox::AutodetectMimeType("NOTES.jpg")); | |
331 ASSERT_EQ(MimeType_Jpeg, SystemToolbox::AutodetectMimeType("NOTES.jpeg")); | |
332 ASSERT_EQ(MimeType_Png, SystemToolbox::AutodetectMimeType("NOTES.png")); | |
333 ASSERT_EQ(MimeType_NaCl, SystemToolbox::AutodetectMimeType("NOTES.nexe")); | |
334 ASSERT_EQ(MimeType_Json, SystemToolbox::AutodetectMimeType("NOTES.nmf")); | |
335 ASSERT_EQ(MimeType_PNaCl, SystemToolbox::AutodetectMimeType("NOTES.pexe")); | |
336 ASSERT_EQ(MimeType_Svg, SystemToolbox::AutodetectMimeType("NOTES.svg")); | |
337 ASSERT_EQ(MimeType_Woff, SystemToolbox::AutodetectMimeType("NOTES.woff")); | |
338 ASSERT_EQ(MimeType_Woff2, SystemToolbox::AutodetectMimeType("NOTES.woff2")); | |
339 | |
340 // Test primitives from the "RegisterDefaultExtensions()" that was | |
341 // present in the sample "Serve Folders plugin" of Orthanc 1.4.2 | |
342 ASSERT_STREQ("application/javascript", EnumerationToString(SystemToolbox::AutodetectMimeType(".js"))); | |
343 ASSERT_STREQ("application/json", EnumerationToString(SystemToolbox::AutodetectMimeType(".json"))); | |
344 ASSERT_STREQ("application/json", EnumerationToString(SystemToolbox::AutodetectMimeType(".nmf"))); | |
345 ASSERT_STREQ("application/octet-stream", EnumerationToString(SystemToolbox::AutodetectMimeType(""))); | |
346 ASSERT_STREQ("application/wasm", EnumerationToString(SystemToolbox::AutodetectMimeType(".wasm"))); | |
347 ASSERT_STREQ("application/x-font-woff", EnumerationToString(SystemToolbox::AutodetectMimeType(".woff"))); | |
348 ASSERT_STREQ("application/x-nacl", EnumerationToString(SystemToolbox::AutodetectMimeType(".nexe"))); | |
349 ASSERT_STREQ("application/x-pnacl", EnumerationToString(SystemToolbox::AutodetectMimeType(".pexe"))); | |
350 ASSERT_STREQ("application/xml", EnumerationToString(SystemToolbox::AutodetectMimeType(".xml"))); | |
351 ASSERT_STREQ("font/woff2", EnumerationToString(SystemToolbox::AutodetectMimeType(".woff2"))); | |
352 ASSERT_STREQ("image/gif", EnumerationToString(SystemToolbox::AutodetectMimeType(".gif"))); | |
353 ASSERT_STREQ("image/jpeg", EnumerationToString(SystemToolbox::AutodetectMimeType(".jpeg"))); | |
354 ASSERT_STREQ("image/jpeg", EnumerationToString(SystemToolbox::AutodetectMimeType(".jpg"))); | |
355 ASSERT_STREQ("image/png", EnumerationToString(SystemToolbox::AutodetectMimeType(".png"))); | |
356 ASSERT_STREQ("image/svg+xml", EnumerationToString(SystemToolbox::AutodetectMimeType(".svg"))); | |
357 ASSERT_STREQ("text/css", EnumerationToString(SystemToolbox::AutodetectMimeType(".css"))); | |
358 ASSERT_STREQ("text/html", EnumerationToString(SystemToolbox::AutodetectMimeType(".html"))); | |
359 } | |
360 | |
361 TEST(Toolbox, ComputeMD5) | |
362 { | |
363 std::string s; | |
364 | |
365 // # echo -n "Hello" | md5sum | |
366 | |
367 Toolbox::ComputeMD5(s, "Hello"); | |
368 ASSERT_EQ("8b1a9953c4611296a827abf8c47804d7", s); | |
369 Toolbox::ComputeMD5(s, ""); | |
370 ASSERT_EQ("d41d8cd98f00b204e9800998ecf8427e", s); | |
371 } | |
372 | |
373 TEST(Toolbox, ComputeSHA1) | |
374 { | |
375 std::string s; | |
376 | |
377 Toolbox::ComputeSHA1(s, "The quick brown fox jumps over the lazy dog"); | |
378 ASSERT_EQ("2fd4e1c6-7a2d28fc-ed849ee1-bb76e739-1b93eb12", s); | |
379 Toolbox::ComputeSHA1(s, ""); | |
380 ASSERT_EQ("da39a3ee-5e6b4b0d-3255bfef-95601890-afd80709", s); | |
381 } | |
382 | |
383 TEST(Toolbox, PathToExecutable) | |
384 { | |
385 printf("[%s]\n", SystemToolbox::GetPathToExecutable().c_str()); | |
386 printf("[%s]\n", SystemToolbox::GetDirectoryOfExecutable().c_str()); | |
387 } | |
388 | |
389 TEST(Toolbox, StripSpaces) | |
390 { | |
391 ASSERT_EQ("", Toolbox::StripSpaces(" \t \r \n ")); | |
392 ASSERT_EQ("coucou", Toolbox::StripSpaces(" coucou \t \r \n ")); | |
393 ASSERT_EQ("cou cou", Toolbox::StripSpaces(" cou cou \n ")); | |
394 ASSERT_EQ("c", Toolbox::StripSpaces(" \n\t c\r \n ")); | |
395 } | |
396 | |
397 TEST(Toolbox, Case) | |
398 { | |
399 std::string s = "CoU"; | |
400 std::string ss; | |
401 | |
402 Toolbox::ToUpperCase(ss, s); | |
403 ASSERT_EQ("COU", ss); | |
404 Toolbox::ToLowerCase(ss, s); | |
405 ASSERT_EQ("cou", ss); | |
406 | |
407 s = "CoU"; | |
408 Toolbox::ToUpperCase(s); | |
409 ASSERT_EQ("COU", s); | |
410 | |
411 s = "CoU"; | |
412 Toolbox::ToLowerCase(s); | |
413 ASSERT_EQ("cou", s); | |
414 } | |
415 | |
416 | |
417 TEST(Logger, Basic) | |
418 { | |
419 LOG(INFO) << "I say hello"; | |
420 } | |
421 | |
422 TEST(Toolbox, ConvertFromLatin1) | |
423 { | |
424 // This is a Latin-1 test string | |
425 const unsigned char data[10] = { 0xe0, 0xe9, 0xea, 0xe7, 0x26, 0xc6, 0x61, 0x62, 0x63, 0x00 }; | |
426 | |
427 std::string s((char*) &data[0], 10); | |
428 ASSERT_EQ("&abc", Toolbox::ConvertToAscii(s)); | |
429 | |
430 // Open in Emacs, then save with UTF-8 encoding, then "hexdump -C" | |
431 std::string utf8 = Toolbox::ConvertToUtf8(s, Encoding_Latin1, false); | |
432 ASSERT_EQ(15u, utf8.size()); | |
433 ASSERT_EQ(0xc3, static_cast<unsigned char>(utf8[0])); | |
434 ASSERT_EQ(0xa0, static_cast<unsigned char>(utf8[1])); | |
435 ASSERT_EQ(0xc3, static_cast<unsigned char>(utf8[2])); | |
436 ASSERT_EQ(0xa9, static_cast<unsigned char>(utf8[3])); | |
437 ASSERT_EQ(0xc3, static_cast<unsigned char>(utf8[4])); | |
438 ASSERT_EQ(0xaa, static_cast<unsigned char>(utf8[5])); | |
439 ASSERT_EQ(0xc3, static_cast<unsigned char>(utf8[6])); | |
440 ASSERT_EQ(0xa7, static_cast<unsigned char>(utf8[7])); | |
441 ASSERT_EQ(0x26, static_cast<unsigned char>(utf8[8])); | |
442 ASSERT_EQ(0xc3, static_cast<unsigned char>(utf8[9])); | |
443 ASSERT_EQ(0x86, static_cast<unsigned char>(utf8[10])); | |
444 ASSERT_EQ(0x61, static_cast<unsigned char>(utf8[11])); | |
445 ASSERT_EQ(0x62, static_cast<unsigned char>(utf8[12])); | |
446 ASSERT_EQ(0x63, static_cast<unsigned char>(utf8[13])); | |
447 ASSERT_EQ(0x00, static_cast<unsigned char>(utf8[14])); // Null-terminated string | |
448 } | |
449 | |
450 | |
451 TEST(Toolbox, FixUtf8) | |
452 { | |
453 // This is a Latin-1 test string: "crane" with a circumflex accent | |
454 const unsigned char latin1[] = { 0x63, 0x72, 0xe2, 0x6e, 0x65 }; | |
455 | |
456 std::string s((char*) &latin1[0], sizeof(latin1) / sizeof(char)); | |
457 | |
458 ASSERT_EQ(s, Toolbox::ConvertFromUtf8(Toolbox::ConvertToUtf8(s, Encoding_Latin1, false), Encoding_Latin1)); | |
459 ASSERT_EQ("cre", Toolbox::ConvertToUtf8(s, Encoding_Utf8, false)); | |
460 } | |
461 | |
462 | |
463 static int32_t GetUnicode(const uint8_t* data, | |
464 size_t size, | |
465 size_t expectedLength) | |
466 { | |
467 std::string s((char*) &data[0], size); | |
468 uint32_t unicode; | |
469 size_t length; | |
470 Toolbox::Utf8ToUnicodeCharacter(unicode, length, s, 0); | |
471 if (length != expectedLength) | |
472 { | |
473 return -1; // Error case | |
474 } | |
475 else | |
476 { | |
477 return unicode; | |
478 } | |
479 } | |
480 | |
481 | |
482 TEST(Toolbox, Utf8ToUnicode) | |
483 { | |
484 // https://en.wikipedia.org/wiki/UTF-8 | |
485 | |
486 ASSERT_EQ(1u, sizeof(char)); | |
487 ASSERT_EQ(1u, sizeof(uint8_t)); | |
488 | |
489 { | |
490 const uint8_t data[] = { 0x24 }; | |
491 ASSERT_EQ(0x24, GetUnicode(data, 1, 1)); | |
492 ASSERT_THROW(GetUnicode(data, 0, 1), OrthancException); | |
493 } | |
494 | |
495 { | |
496 const uint8_t data[] = { 0xc2, 0xa2 }; | |
497 ASSERT_EQ(0xa2, GetUnicode(data, 2, 2)); | |
498 ASSERT_THROW(GetUnicode(data, 1, 2), OrthancException); | |
499 } | |
500 | |
501 { | |
502 const uint8_t data[] = { 0xe0, 0xa4, 0xb9 }; | |
503 ASSERT_EQ(0x0939, GetUnicode(data, 3, 3)); | |
504 ASSERT_THROW(GetUnicode(data, 2, 3), OrthancException); | |
505 } | |
506 | |
507 { | |
508 const uint8_t data[] = { 0xe2, 0x82, 0xac }; | |
509 ASSERT_EQ(0x20ac, GetUnicode(data, 3, 3)); | |
510 ASSERT_THROW(GetUnicode(data, 2, 3), OrthancException); | |
511 } | |
512 | |
513 { | |
514 const uint8_t data[] = { 0xf0, 0x90, 0x8d, 0x88 }; | |
515 ASSERT_EQ(0x010348, GetUnicode(data, 4, 4)); | |
516 ASSERT_THROW(GetUnicode(data, 3, 4), OrthancException); | |
517 } | |
518 | |
519 { | |
520 const uint8_t data[] = { 0xe0 }; | |
521 ASSERT_THROW(GetUnicode(data, 1, 1), OrthancException); | |
522 } | |
523 } | |
524 | |
525 | |
526 TEST(Toolbox, UrlDecode) | |
527 { | |
528 std::string s; | |
529 | |
530 s = "Hello%20World"; | |
531 Toolbox::UrlDecode(s); | |
532 ASSERT_EQ("Hello World", s); | |
533 | |
534 s = "%21%23%24%26%27%28%29%2A%2B%2c%2f%3A%3b%3d%3f%40%5B%5D%90%ff"; | |
535 Toolbox::UrlDecode(s); | |
536 std::string ss = "!#$&'()*+,/:;=?@[]"; | |
537 ss.push_back((char) 144); | |
538 ss.push_back((char) 255); | |
539 ASSERT_EQ(ss, s); | |
540 | |
541 s = "(2000%2C00A4)+Other"; | |
542 Toolbox::UrlDecode(s); | |
543 ASSERT_EQ("(2000,00A4) Other", s); | |
544 } | |
545 | |
546 | |
547 TEST(Toolbox, IsAsciiString) | |
548 { | |
549 std::string s = "Hello 12 /"; | |
550 ASSERT_EQ(10u, s.size()); | |
551 ASSERT_TRUE(Toolbox::IsAsciiString(s)); | |
552 ASSERT_TRUE(Toolbox::IsAsciiString(s.c_str(), 10)); | |
553 ASSERT_FALSE(Toolbox::IsAsciiString(s.c_str(), 11)); // Taking the trailing hidden '\0' | |
554 | |
555 s[2] = '\0'; | |
556 ASSERT_EQ(10u, s.size()); | |
557 ASSERT_FALSE(Toolbox::IsAsciiString(s)); | |
558 | |
559 ASSERT_TRUE(Toolbox::IsAsciiString("Hello\nworld")); | |
560 ASSERT_FALSE(Toolbox::IsAsciiString("Hello\rworld")); | |
561 | |
562 ASSERT_EQ("Hello\nworld", Toolbox::ConvertToAscii("Hello\nworld")); | |
563 ASSERT_EQ("Helloworld", Toolbox::ConvertToAscii("Hello\r\tworld")); | |
564 } | |
565 | |
566 | |
567 #if defined(__linux__) | |
568 TEST(Toolbox, AbsoluteDirectory) | |
569 { | |
570 ASSERT_EQ("/tmp/hello", SystemToolbox::InterpretRelativePath("/tmp", "hello")); | |
571 ASSERT_EQ("/tmp", SystemToolbox::InterpretRelativePath("/tmp", "/tmp")); | |
572 } | |
573 #endif | |
574 | |
575 | |
576 TEST(Toolbox, WriteFile) | |
577 { | |
578 std::string path; | |
579 | |
580 { | |
581 TemporaryFile tmp; | |
582 path = tmp.GetPath(); | |
583 | |
584 std::string s; | |
585 s.append("Hello"); | |
586 s.push_back('\0'); | |
587 s.append("World"); | |
588 ASSERT_EQ(11u, s.size()); | |
589 | |
590 SystemToolbox::WriteFile(s, path.c_str()); | |
591 | |
592 std::string t; | |
593 SystemToolbox::ReadFile(t, path.c_str()); | |
594 | |
595 ASSERT_EQ(11u, t.size()); | |
596 ASSERT_EQ(0, t[5]); | |
597 ASSERT_EQ(0, memcmp(s.c_str(), t.c_str(), s.size())); | |
598 | |
599 std::string h; | |
600 ASSERT_EQ(true, SystemToolbox::ReadHeader(h, path.c_str(), 1)); | |
601 ASSERT_EQ(1u, h.size()); | |
602 ASSERT_EQ('H', h[0]); | |
603 ASSERT_TRUE(SystemToolbox::ReadHeader(h, path.c_str(), 0)); | |
604 ASSERT_EQ(0u, h.size()); | |
605 ASSERT_FALSE(SystemToolbox::ReadHeader(h, path.c_str(), 32)); | |
606 ASSERT_EQ(11u, h.size()); | |
607 ASSERT_EQ(0, memcmp(s.c_str(), h.c_str(), s.size())); | |
608 } | |
609 | |
610 std::string u; | |
611 ASSERT_THROW(SystemToolbox::ReadFile(u, path.c_str()), OrthancException); | |
612 } | |
613 | |
614 | |
615 TEST(Toolbox, FileBuffer) | |
616 { | |
617 FileBuffer f; | |
618 f.Append("a", 1); | |
619 f.Append("", 0); | |
620 f.Append("bc", 2); | |
621 | |
622 std::string s; | |
623 f.Read(s); | |
624 ASSERT_EQ("abc", s); | |
625 | |
626 ASSERT_THROW(f.Append("d", 1), OrthancException); // File is closed | |
627 } | |
628 | |
629 | |
630 TEST(Toolbox, Wildcard) | |
631 { | |
632 ASSERT_EQ("abcd", Toolbox::WildcardToRegularExpression("abcd")); | |
633 ASSERT_EQ("ab.*cd", Toolbox::WildcardToRegularExpression("ab*cd")); | |
634 ASSERT_EQ("ab..cd", Toolbox::WildcardToRegularExpression("ab??cd")); | |
635 ASSERT_EQ("a.*b.c.*d", Toolbox::WildcardToRegularExpression("a*b?c*d")); | |
636 ASSERT_EQ("a\\{b\\]", Toolbox::WildcardToRegularExpression("a{b]")); | |
637 } | |
638 | |
639 | |
640 TEST(Toolbox, Tokenize) | |
641 { | |
642 std::vector<std::string> t; | |
643 | |
644 Toolbox::TokenizeString(t, "", ','); | |
645 ASSERT_EQ(1u, t.size()); | |
646 ASSERT_EQ("", t[0]); | |
647 | |
648 Toolbox::TokenizeString(t, "abc", ','); | |
649 ASSERT_EQ(1u, t.size()); | |
650 ASSERT_EQ("abc", t[0]); | |
651 | |
652 Toolbox::TokenizeString(t, "ab,cd,ef,", ','); | |
653 ASSERT_EQ(4u, t.size()); | |
654 ASSERT_EQ("ab", t[0]); | |
655 ASSERT_EQ("cd", t[1]); | |
656 ASSERT_EQ("ef", t[2]); | |
657 ASSERT_EQ("", t[3]); | |
658 } | |
659 | |
660 TEST(Toolbox, Enumerations) | |
661 { | |
662 ASSERT_EQ(Encoding_Utf8, StringToEncoding(EnumerationToString(Encoding_Utf8))); | |
663 ASSERT_EQ(Encoding_Ascii, StringToEncoding(EnumerationToString(Encoding_Ascii))); | |
664 ASSERT_EQ(Encoding_Latin1, StringToEncoding(EnumerationToString(Encoding_Latin1))); | |
665 ASSERT_EQ(Encoding_Latin2, StringToEncoding(EnumerationToString(Encoding_Latin2))); | |
666 ASSERT_EQ(Encoding_Latin3, StringToEncoding(EnumerationToString(Encoding_Latin3))); | |
667 ASSERT_EQ(Encoding_Latin4, StringToEncoding(EnumerationToString(Encoding_Latin4))); | |
668 ASSERT_EQ(Encoding_Latin5, StringToEncoding(EnumerationToString(Encoding_Latin5))); | |
669 ASSERT_EQ(Encoding_Cyrillic, StringToEncoding(EnumerationToString(Encoding_Cyrillic))); | |
670 ASSERT_EQ(Encoding_Arabic, StringToEncoding(EnumerationToString(Encoding_Arabic))); | |
671 ASSERT_EQ(Encoding_Greek, StringToEncoding(EnumerationToString(Encoding_Greek))); | |
672 ASSERT_EQ(Encoding_Hebrew, StringToEncoding(EnumerationToString(Encoding_Hebrew))); | |
673 ASSERT_EQ(Encoding_Japanese, StringToEncoding(EnumerationToString(Encoding_Japanese))); | |
674 ASSERT_EQ(Encoding_Chinese, StringToEncoding(EnumerationToString(Encoding_Chinese))); | |
675 ASSERT_EQ(Encoding_Thai, StringToEncoding(EnumerationToString(Encoding_Thai))); | |
676 ASSERT_EQ(Encoding_Korean, StringToEncoding(EnumerationToString(Encoding_Korean))); | |
677 ASSERT_EQ(Encoding_JapaneseKanji, StringToEncoding(EnumerationToString(Encoding_JapaneseKanji))); | |
678 ASSERT_EQ(Encoding_SimplifiedChinese, StringToEncoding(EnumerationToString(Encoding_SimplifiedChinese))); | |
679 | |
680 ASSERT_EQ(ResourceType_Patient, StringToResourceType(EnumerationToString(ResourceType_Patient))); | |
681 ASSERT_EQ(ResourceType_Study, StringToResourceType(EnumerationToString(ResourceType_Study))); | |
682 ASSERT_EQ(ResourceType_Series, StringToResourceType(EnumerationToString(ResourceType_Series))); | |
683 ASSERT_EQ(ResourceType_Instance, StringToResourceType(EnumerationToString(ResourceType_Instance))); | |
684 | |
685 ASSERT_EQ(ImageFormat_Png, StringToImageFormat(EnumerationToString(ImageFormat_Png))); | |
686 | |
687 ASSERT_EQ(PhotometricInterpretation_ARGB, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_ARGB))); | |
688 ASSERT_EQ(PhotometricInterpretation_CMYK, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_CMYK))); | |
689 ASSERT_EQ(PhotometricInterpretation_HSV, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_HSV))); | |
690 ASSERT_EQ(PhotometricInterpretation_Monochrome1, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_Monochrome1))); | |
691 ASSERT_EQ(PhotometricInterpretation_Monochrome2, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_Monochrome2))); | |
692 ASSERT_EQ(PhotometricInterpretation_Palette, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_Palette))); | |
693 ASSERT_EQ(PhotometricInterpretation_RGB, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_RGB))); | |
694 ASSERT_EQ(PhotometricInterpretation_YBRFull, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_YBRFull))); | |
695 ASSERT_EQ(PhotometricInterpretation_YBRFull422, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_YBRFull422))); | |
696 ASSERT_EQ(PhotometricInterpretation_YBRPartial420, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_YBRPartial420))); | |
697 ASSERT_EQ(PhotometricInterpretation_YBRPartial422, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_YBRPartial422))); | |
698 ASSERT_EQ(PhotometricInterpretation_YBR_ICT, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_YBR_ICT))); | |
699 ASSERT_EQ(PhotometricInterpretation_YBR_RCT, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_YBR_RCT))); | |
700 | |
701 ASSERT_STREQ("Unknown", EnumerationToString(PhotometricInterpretation_Unknown)); | |
702 ASSERT_THROW(StringToPhotometricInterpretation("Unknown"), OrthancException); | |
703 | |
704 ASSERT_EQ(DicomVersion_2008, StringToDicomVersion(EnumerationToString(DicomVersion_2008))); | |
705 ASSERT_EQ(DicomVersion_2017c, StringToDicomVersion(EnumerationToString(DicomVersion_2017c))); | |
706 | |
707 for (int i = static_cast<int>(ValueRepresentation_ApplicationEntity); | |
708 i < static_cast<int>(ValueRepresentation_NotSupported); i += 1) | |
709 { | |
710 ValueRepresentation vr = static_cast<ValueRepresentation>(i); | |
711 ASSERT_EQ(vr, StringToValueRepresentation(EnumerationToString(vr), true)); | |
712 } | |
713 | |
714 ASSERT_THROW(StringToValueRepresentation("nope", true), OrthancException); | |
715 | |
716 ASSERT_EQ(JobState_Pending, StringToJobState(EnumerationToString(JobState_Pending))); | |
717 ASSERT_EQ(JobState_Running, StringToJobState(EnumerationToString(JobState_Running))); | |
718 ASSERT_EQ(JobState_Success, StringToJobState(EnumerationToString(JobState_Success))); | |
719 ASSERT_EQ(JobState_Failure, StringToJobState(EnumerationToString(JobState_Failure))); | |
720 ASSERT_EQ(JobState_Paused, StringToJobState(EnumerationToString(JobState_Paused))); | |
721 ASSERT_EQ(JobState_Retry, StringToJobState(EnumerationToString(JobState_Retry))); | |
722 ASSERT_THROW(StringToJobState("nope"), OrthancException); | |
723 | |
724 ASSERT_EQ(MimeType_Binary, StringToMimeType(EnumerationToString(MimeType_Binary))); | |
725 ASSERT_EQ(MimeType_Css, StringToMimeType(EnumerationToString(MimeType_Css))); | |
726 ASSERT_EQ(MimeType_Dicom, StringToMimeType(EnumerationToString(MimeType_Dicom))); | |
727 ASSERT_EQ(MimeType_Gif, StringToMimeType(EnumerationToString(MimeType_Gif))); | |
728 ASSERT_EQ(MimeType_Gzip, StringToMimeType(EnumerationToString(MimeType_Gzip))); | |
729 ASSERT_EQ(MimeType_Html, StringToMimeType(EnumerationToString(MimeType_Html))); | |
730 ASSERT_EQ(MimeType_JavaScript, StringToMimeType(EnumerationToString(MimeType_JavaScript))); | |
731 ASSERT_EQ(MimeType_Jpeg, StringToMimeType(EnumerationToString(MimeType_Jpeg))); | |
732 ASSERT_EQ(MimeType_Jpeg2000, StringToMimeType(EnumerationToString(MimeType_Jpeg2000))); | |
733 ASSERT_EQ(MimeType_Json, StringToMimeType(EnumerationToString(MimeType_Json))); | |
734 ASSERT_EQ(MimeType_NaCl, StringToMimeType(EnumerationToString(MimeType_NaCl))); | |
735 ASSERT_EQ(MimeType_PNaCl, StringToMimeType(EnumerationToString(MimeType_PNaCl))); | |
736 ASSERT_EQ(MimeType_Pam, StringToMimeType(EnumerationToString(MimeType_Pam))); | |
737 ASSERT_EQ(MimeType_Pdf, StringToMimeType(EnumerationToString(MimeType_Pdf))); | |
738 ASSERT_EQ(MimeType_PlainText, StringToMimeType(EnumerationToString(MimeType_PlainText))); | |
739 ASSERT_EQ(MimeType_Png, StringToMimeType(EnumerationToString(MimeType_Png))); | |
740 ASSERT_EQ(MimeType_Svg, StringToMimeType(EnumerationToString(MimeType_Svg))); | |
741 ASSERT_EQ(MimeType_WebAssembly, StringToMimeType(EnumerationToString(MimeType_WebAssembly))); | |
742 ASSERT_EQ(MimeType_Xml, StringToMimeType("application/xml")); | |
743 ASSERT_EQ(MimeType_Xml, StringToMimeType("text/xml")); | |
744 ASSERT_EQ(MimeType_Xml, StringToMimeType(EnumerationToString(MimeType_Xml))); | |
745 ASSERT_EQ(MimeType_DicomWebJson, StringToMimeType(EnumerationToString(MimeType_DicomWebJson))); | |
746 ASSERT_EQ(MimeType_DicomWebXml, StringToMimeType(EnumerationToString(MimeType_DicomWebXml))); | |
747 ASSERT_THROW(StringToMimeType("nope"), OrthancException); | |
748 | |
749 ASSERT_TRUE(IsResourceLevelAboveOrEqual(ResourceType_Patient, ResourceType_Patient)); | |
750 ASSERT_TRUE(IsResourceLevelAboveOrEqual(ResourceType_Patient, ResourceType_Study)); | |
751 ASSERT_TRUE(IsResourceLevelAboveOrEqual(ResourceType_Patient, ResourceType_Series)); | |
752 ASSERT_TRUE(IsResourceLevelAboveOrEqual(ResourceType_Patient, ResourceType_Instance)); | |
753 | |
754 ASSERT_FALSE(IsResourceLevelAboveOrEqual(ResourceType_Study, ResourceType_Patient)); | |
755 ASSERT_TRUE(IsResourceLevelAboveOrEqual(ResourceType_Study, ResourceType_Study)); | |
756 ASSERT_TRUE(IsResourceLevelAboveOrEqual(ResourceType_Study, ResourceType_Series)); | |
757 ASSERT_TRUE(IsResourceLevelAboveOrEqual(ResourceType_Study, ResourceType_Instance)); | |
758 | |
759 ASSERT_FALSE(IsResourceLevelAboveOrEqual(ResourceType_Series, ResourceType_Patient)); | |
760 ASSERT_FALSE(IsResourceLevelAboveOrEqual(ResourceType_Series, ResourceType_Study)); | |
761 ASSERT_TRUE(IsResourceLevelAboveOrEqual(ResourceType_Series, ResourceType_Series)); | |
762 ASSERT_TRUE(IsResourceLevelAboveOrEqual(ResourceType_Series, ResourceType_Instance)); | |
763 | |
764 ASSERT_FALSE(IsResourceLevelAboveOrEqual(ResourceType_Instance, ResourceType_Patient)); | |
765 ASSERT_FALSE(IsResourceLevelAboveOrEqual(ResourceType_Instance, ResourceType_Study)); | |
766 ASSERT_FALSE(IsResourceLevelAboveOrEqual(ResourceType_Instance, ResourceType_Series)); | |
767 ASSERT_TRUE(IsResourceLevelAboveOrEqual(ResourceType_Instance, ResourceType_Instance)); | |
768 } | |
769 | |
770 | |
771 #if defined(__linux__) || defined(__OpenBSD__) | |
772 #include <endian.h> | |
773 #elif defined(__FreeBSD__) | |
774 #include <machine/endian.h> | |
775 #endif | |
776 | |
777 | |
778 TEST(Toolbox, Endianness) | |
779 { | |
780 // Parts of this test come from Adam Conrad | |
781 // http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=728822#5 | |
782 | |
783 | |
784 /** | |
785 * Windows and OS X are assumed to always little-endian. | |
786 **/ | |
787 | |
788 #if defined(_WIN32) || defined(__APPLE__) | |
789 ASSERT_EQ(Endianness_Little, Toolbox::DetectEndianness()); | |
790 | |
791 | |
792 /** | |
793 * FreeBSD. | |
794 **/ | |
795 | |
796 #elif defined(__FreeBSD__) || defined(__OpenBSD__) | |
797 # if _BYTE_ORDER == _BIG_ENDIAN | |
798 ASSERT_EQ(Endianness_Big, Toolbox::DetectEndianness()); | |
799 # else // _LITTLE_ENDIAN | |
800 ASSERT_EQ(Endianness_Little, Toolbox::DetectEndianness()); | |
801 # endif | |
802 | |
803 | |
804 /** | |
805 * Linux. | |
806 **/ | |
807 | |
808 #elif defined(__linux__) || defined(__FreeBSD_kernel__) | |
809 | |
810 #if !defined(__BYTE_ORDER) | |
811 # error Support your platform here | |
812 #endif | |
813 | |
814 # if __BYTE_ORDER == __BIG_ENDIAN | |
815 ASSERT_EQ(Endianness_Big, Toolbox::DetectEndianness()); | |
816 # else // __LITTLE_ENDIAN | |
817 ASSERT_EQ(Endianness_Little, Toolbox::DetectEndianness()); | |
818 # endif | |
819 | |
820 #else | |
821 #error Support your platform here | |
822 #endif | |
823 } | |
824 | |
825 | |
4045 | 826 #include "../Sources/Endianness.h" |
3987 | 827 |
828 static void ASSERT_EQ16(uint16_t a, uint16_t b) | |
829 { | |
830 #ifdef __MINGW32__ | |
831 // This cast solves a linking problem with MinGW | |
832 ASSERT_EQ(static_cast<unsigned int>(a), static_cast<unsigned int>(b)); | |
833 #else | |
834 ASSERT_EQ(a, b); | |
835 #endif | |
836 } | |
837 | |
838 static void ASSERT_NE16(uint16_t a, uint16_t b) | |
839 { | |
840 #ifdef __MINGW32__ | |
841 // This cast solves a linking problem with MinGW | |
842 ASSERT_NE(static_cast<unsigned int>(a), static_cast<unsigned int>(b)); | |
843 #else | |
844 ASSERT_NE(a, b); | |
845 #endif | |
846 } | |
847 | |
848 static void ASSERT_EQ32(uint32_t a, uint32_t b) | |
849 { | |
850 #ifdef __MINGW32__ | |
851 // This cast solves a linking problem with MinGW | |
852 ASSERT_EQ(static_cast<unsigned int>(a), static_cast<unsigned int>(b)); | |
853 #else | |
854 ASSERT_EQ(a, b); | |
855 #endif | |
856 } | |
857 | |
858 static void ASSERT_NE32(uint32_t a, uint32_t b) | |
859 { | |
860 #ifdef __MINGW32__ | |
861 // This cast solves a linking problem with MinGW | |
862 ASSERT_NE(static_cast<unsigned int>(a), static_cast<unsigned int>(b)); | |
863 #else | |
864 ASSERT_NE(a, b); | |
865 #endif | |
866 } | |
867 | |
868 static void ASSERT_EQ64(uint64_t a, uint64_t b) | |
869 { | |
870 #ifdef __MINGW32__ | |
871 // This cast solves a linking problem with MinGW | |
872 ASSERT_EQ(static_cast<unsigned int>(a), static_cast<unsigned int>(b)); | |
873 #else | |
874 ASSERT_EQ(a, b); | |
875 #endif | |
876 } | |
877 | |
878 static void ASSERT_NE64(uint64_t a, uint64_t b) | |
879 { | |
880 #ifdef __MINGW32__ | |
881 // This cast solves a linking problem with MinGW | |
882 ASSERT_NE(static_cast<unsigned long long>(a), static_cast<unsigned long long>(b)); | |
883 #else | |
884 ASSERT_NE(a, b); | |
885 #endif | |
886 } | |
887 | |
888 | |
889 | |
890 TEST(Toolbox, EndiannessConversions16) | |
891 { | |
892 Endianness e = Toolbox::DetectEndianness(); | |
893 | |
894 for (unsigned int i = 0; i < 65536; i += 17) | |
895 { | |
896 uint16_t v = static_cast<uint16_t>(i); | |
897 ASSERT_EQ16(v, be16toh(htobe16(v))); | |
898 ASSERT_EQ16(v, le16toh(htole16(v))); | |
899 | |
900 const uint8_t* bytes = reinterpret_cast<const uint8_t*>(&v); | |
901 if (bytes[0] != bytes[1]) | |
902 { | |
903 ASSERT_NE16(v, le16toh(htobe16(v))); | |
904 ASSERT_NE16(v, be16toh(htole16(v))); | |
905 } | |
906 else | |
907 { | |
908 ASSERT_EQ16(v, le16toh(htobe16(v))); | |
909 ASSERT_EQ16(v, be16toh(htole16(v))); | |
910 } | |
911 | |
912 switch (e) | |
913 { | |
914 case Endianness_Little: | |
915 ASSERT_EQ16(v, htole16(v)); | |
916 if (bytes[0] != bytes[1]) | |
917 { | |
918 ASSERT_NE16(v, htobe16(v)); | |
919 } | |
920 else | |
921 { | |
922 ASSERT_EQ16(v, htobe16(v)); | |
923 } | |
924 break; | |
925 | |
926 case Endianness_Big: | |
927 ASSERT_EQ16(v, htobe16(v)); | |
928 if (bytes[0] != bytes[1]) | |
929 { | |
930 ASSERT_NE16(v, htole16(v)); | |
931 } | |
932 else | |
933 { | |
934 ASSERT_EQ16(v, htole16(v)); | |
935 } | |
936 break; | |
937 | |
938 default: | |
939 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
940 } | |
941 } | |
942 } | |
943 | |
944 | |
945 TEST(Toolbox, EndiannessConversions32) | |
946 { | |
947 const uint32_t v = 0xff010203u; | |
948 const uint32_t r = 0x030201ffu; | |
949 ASSERT_EQ32(v, be32toh(htobe32(v))); | |
950 ASSERT_EQ32(v, le32toh(htole32(v))); | |
951 ASSERT_NE32(v, be32toh(htole32(v))); | |
952 ASSERT_NE32(v, le32toh(htobe32(v))); | |
953 | |
954 switch (Toolbox::DetectEndianness()) | |
955 { | |
956 case Endianness_Little: | |
957 ASSERT_EQ32(r, htobe32(v)); | |
958 ASSERT_EQ32(v, htole32(v)); | |
959 ASSERT_EQ32(r, be32toh(v)); | |
960 ASSERT_EQ32(v, le32toh(v)); | |
961 break; | |
962 | |
963 case Endianness_Big: | |
964 ASSERT_EQ32(v, htobe32(v)); | |
965 ASSERT_EQ32(r, htole32(v)); | |
966 ASSERT_EQ32(v, be32toh(v)); | |
967 ASSERT_EQ32(r, le32toh(v)); | |
968 break; | |
969 | |
970 default: | |
971 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
972 } | |
973 } | |
974 | |
975 | |
976 TEST(Toolbox, EndiannessConversions64) | |
977 { | |
978 const uint64_t v = 0xff01020304050607LL; | |
979 const uint64_t r = 0x07060504030201ffLL; | |
980 ASSERT_EQ64(v, be64toh(htobe64(v))); | |
981 ASSERT_EQ64(v, le64toh(htole64(v))); | |
982 ASSERT_NE64(v, be64toh(htole64(v))); | |
983 ASSERT_NE64(v, le64toh(htobe64(v))); | |
984 | |
985 switch (Toolbox::DetectEndianness()) | |
986 { | |
987 case Endianness_Little: | |
988 ASSERT_EQ64(r, htobe64(v)); | |
989 ASSERT_EQ64(v, htole64(v)); | |
990 ASSERT_EQ64(r, be64toh(v)); | |
991 ASSERT_EQ64(v, le64toh(v)); | |
992 break; | |
993 | |
994 case Endianness_Big: | |
995 ASSERT_EQ64(v, htobe64(v)); | |
996 ASSERT_EQ64(r, htole64(v)); | |
997 ASSERT_EQ64(v, be64toh(v)); | |
998 ASSERT_EQ64(r, le64toh(v)); | |
999 break; | |
1000 | |
1001 default: | |
1002 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
1003 } | |
1004 } | |
1005 | |
1006 | |
1007 TEST(Toolbox, Now) | |
1008 { | |
1009 LOG(WARNING) << "Local time: " << SystemToolbox::GetNowIsoString(false); | |
1010 LOG(WARNING) << "Universal time: " << SystemToolbox::GetNowIsoString(true); | |
1011 | |
1012 std::string date, time; | |
1013 SystemToolbox::GetNowDicom(date, time, false); | |
1014 LOG(WARNING) << "Local DICOM time: [" << date << "] [" << time << "]"; | |
1015 | |
1016 SystemToolbox::GetNowDicom(date, time, true); | |
1017 LOG(WARNING) << "Universal DICOM time: [" << date << "] [" << time << "]"; | |
1018 } | |
1019 | |
1020 | |
1021 | |
1022 #if ORTHANC_ENABLE_PUGIXML == 1 | |
1023 TEST(Toolbox, Xml) | |
1024 { | |
1025 Json::Value a; | |
1026 a["hello"] = "world"; | |
1027 a["42"] = 43; | |
1028 a["b"] = Json::arrayValue; | |
1029 a["b"].append("test"); | |
1030 a["b"].append("test2"); | |
1031 | |
1032 std::string s; | |
1033 Toolbox::JsonToXml(s, a); | |
1034 | |
1035 std::cout << s; | |
1036 } | |
1037 #endif | |
1038 | |
1039 | |
1040 #if !defined(_WIN32) | |
1041 TEST(Toolbox, ExecuteSystemCommand) | |
1042 { | |
1043 std::vector<std::string> args(2); | |
1044 args[0] = "Hello"; | |
1045 args[1] = "World"; | |
1046 | |
1047 SystemToolbox::ExecuteSystemCommand("echo", args); | |
1048 } | |
1049 #endif | |
1050 | |
1051 | |
1052 TEST(Toolbox, IsInteger) | |
1053 { | |
1054 ASSERT_TRUE(Toolbox::IsInteger("00236")); | |
1055 ASSERT_TRUE(Toolbox::IsInteger("-0042")); | |
1056 ASSERT_TRUE(Toolbox::IsInteger("0")); | |
1057 ASSERT_TRUE(Toolbox::IsInteger("-0")); | |
1058 | |
1059 ASSERT_FALSE(Toolbox::IsInteger("")); | |
1060 ASSERT_FALSE(Toolbox::IsInteger("42a")); | |
1061 ASSERT_FALSE(Toolbox::IsInteger("42-")); | |
1062 } | |
1063 | |
1064 | |
1065 TEST(Toolbox, StartsWith) | |
1066 { | |
1067 ASSERT_TRUE(Toolbox::StartsWith("hello world", "")); | |
1068 ASSERT_TRUE(Toolbox::StartsWith("hello world", "hello")); | |
1069 ASSERT_TRUE(Toolbox::StartsWith("hello world", "h")); | |
1070 ASSERT_FALSE(Toolbox::StartsWith("hello world", "H")); | |
1071 ASSERT_FALSE(Toolbox::StartsWith("h", "hello")); | |
1072 ASSERT_TRUE(Toolbox::StartsWith("h", "h")); | |
1073 ASSERT_FALSE(Toolbox::StartsWith("", "h")); | |
1074 } | |
1075 | |
1076 | |
1077 TEST(Toolbox, UriEncode) | |
1078 { | |
1079 std::string s; | |
1080 | |
1081 // Unreserved characters must not be modified | |
1082 std::string t = "aAzZ09.-~_"; | |
1083 Toolbox::UriEncode(s, t); | |
1084 ASSERT_EQ(t, s); | |
1085 | |
1086 Toolbox::UriEncode(s, "!#$&'()*+,/:;=?@[]"); ASSERT_EQ("%21%23%24%26%27%28%29%2A%2B%2C%2F%3A%3B%3D%3F%40%5B%5D", s); | |
1087 Toolbox::UriEncode(s, "%"); ASSERT_EQ("%25", s); | |
1088 | |
1089 // Encode characters from UTF-8. This is the test string from the | |
1090 // file "../Resources/EncodingTests.py" | |
1091 Toolbox::UriEncode(s, "\x54\x65\x73\x74\xc3\xa9\xc3\xa4\xc3\xb6\xc3\xb2\xd0\x94\xce\x98\xc4\x9d\xd7\x93\xd8\xb5\xc4\xb7\xd1\x9b\xe0\xb9\x9b\xef\xbe\x88\xc4\xb0"); | |
1092 ASSERT_EQ("Test%C3%A9%C3%A4%C3%B6%C3%B2%D0%94%CE%98%C4%9D%D7%93%D8%B5%C4%B7%D1%9B%E0%B9%9B%EF%BE%88%C4%B0", s); | |
1093 } | |
1094 | |
1095 | |
1096 TEST(Toolbox, AccessJson) | |
1097 { | |
1098 Json::Value v = Json::arrayValue; | |
1099 ASSERT_EQ("nope", Toolbox::GetJsonStringField(v, "hello", "nope")); | |
1100 | |
1101 v = Json::objectValue; | |
1102 ASSERT_EQ("nope", Toolbox::GetJsonStringField(v, "hello", "nope")); | |
1103 ASSERT_EQ(-10, Toolbox::GetJsonIntegerField(v, "hello", -10)); | |
1104 ASSERT_EQ(10u, Toolbox::GetJsonUnsignedIntegerField(v, "hello", 10)); | |
1105 ASSERT_TRUE(Toolbox::GetJsonBooleanField(v, "hello", true)); | |
1106 | |
1107 v["hello"] = "world"; | |
1108 ASSERT_EQ("world", Toolbox::GetJsonStringField(v, "hello", "nope")); | |
1109 ASSERT_THROW(Toolbox::GetJsonIntegerField(v, "hello", -10), OrthancException); | |
1110 ASSERT_THROW(Toolbox::GetJsonUnsignedIntegerField(v, "hello", 10), OrthancException); | |
1111 ASSERT_THROW(Toolbox::GetJsonBooleanField(v, "hello", true), OrthancException); | |
1112 | |
1113 v["hello"] = -42; | |
1114 ASSERT_THROW(Toolbox::GetJsonStringField(v, "hello", "nope"), OrthancException); | |
1115 ASSERT_EQ(-42, Toolbox::GetJsonIntegerField(v, "hello", -10)); | |
1116 ASSERT_THROW(Toolbox::GetJsonUnsignedIntegerField(v, "hello", 10), OrthancException); | |
1117 ASSERT_THROW(Toolbox::GetJsonBooleanField(v, "hello", true), OrthancException); | |
1118 | |
1119 v["hello"] = 42; | |
1120 ASSERT_THROW(Toolbox::GetJsonStringField(v, "hello", "nope"), OrthancException); | |
1121 ASSERT_EQ(42, Toolbox::GetJsonIntegerField(v, "hello", -10)); | |
1122 ASSERT_EQ(42u, Toolbox::GetJsonUnsignedIntegerField(v, "hello", 10)); | |
1123 ASSERT_THROW(Toolbox::GetJsonBooleanField(v, "hello", true), OrthancException); | |
1124 | |
1125 v["hello"] = false; | |
1126 ASSERT_THROW(Toolbox::GetJsonStringField(v, "hello", "nope"), OrthancException); | |
1127 ASSERT_THROW(Toolbox::GetJsonIntegerField(v, "hello", -10), OrthancException); | |
1128 ASSERT_THROW(Toolbox::GetJsonUnsignedIntegerField(v, "hello", 10), OrthancException); | |
1129 ASSERT_FALSE(Toolbox::GetJsonBooleanField(v, "hello", true)); | |
1130 } | |
1131 | |
1132 | |
1133 TEST(Toolbox, LinesIterator) | |
1134 { | |
1135 std::string s; | |
1136 | |
1137 { | |
1138 std::string content; | |
1139 Toolbox::LinesIterator it(content); | |
1140 ASSERT_FALSE(it.GetLine(s)); | |
1141 } | |
1142 | |
1143 { | |
1144 std::string content = "\n\r"; | |
1145 Toolbox::LinesIterator it(content); | |
1146 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s); | |
1147 ASSERT_FALSE(it.GetLine(s)); | |
1148 } | |
1149 | |
1150 { | |
1151 std::string content = "\n Hello \n\nWorld\n\n"; | |
1152 Toolbox::LinesIterator it(content); | |
1153 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s); | |
1154 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ(" Hello ", s); | |
1155 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s); | |
1156 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("World", s); | |
1157 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s); | |
1158 ASSERT_FALSE(it.GetLine(s)); it.Next(); | |
1159 ASSERT_FALSE(it.GetLine(s)); | |
1160 } | |
1161 | |
1162 { | |
1163 std::string content = "\r Hello \r\rWorld\r\r"; | |
1164 Toolbox::LinesIterator it(content); | |
1165 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s); | |
1166 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ(" Hello ", s); | |
1167 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s); | |
1168 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("World", s); | |
1169 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s); | |
1170 ASSERT_FALSE(it.GetLine(s)); it.Next(); | |
1171 ASSERT_FALSE(it.GetLine(s)); | |
1172 } | |
1173 | |
1174 { | |
1175 std::string content = "\n\r Hello \n\r\n\rWorld\n\r\n\r"; | |
1176 Toolbox::LinesIterator it(content); | |
1177 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s); | |
1178 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ(" Hello ", s); | |
1179 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s); | |
1180 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("World", s); | |
1181 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s); | |
1182 ASSERT_FALSE(it.GetLine(s)); it.Next(); | |
1183 ASSERT_FALSE(it.GetLine(s)); | |
1184 } | |
1185 | |
1186 { | |
1187 std::string content = "\r\n Hello \r\n\r\nWorld\r\n\r\n"; | |
1188 Toolbox::LinesIterator it(content); | |
1189 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s); | |
1190 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ(" Hello ", s); | |
1191 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s); | |
1192 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("World", s); | |
1193 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s); | |
1194 ASSERT_FALSE(it.GetLine(s)); it.Next(); | |
1195 ASSERT_FALSE(it.GetLine(s)); | |
1196 } | |
1197 } | |
1198 | |
1199 | |
1200 TEST(Toolbox, SubstituteVariables) | |
1201 { | |
1202 std::map<std::string, std::string> env; | |
1203 env["NOPE"] = "nope"; | |
1204 env["WORLD"] = "world"; | |
1205 | |
1206 ASSERT_EQ("Hello world\r\nWorld \r\nDone world\r\n", | |
1207 Toolbox::SubstituteVariables( | |
1208 "Hello ${WORLD}\r\nWorld ${HELLO}\r\nDone ${WORLD}\r\n", | |
1209 env)); | |
1210 | |
1211 ASSERT_EQ("world A a B world C 'c' D {\"a\":\"b\"} E ", | |
1212 Toolbox::SubstituteVariables( | |
1213 "${WORLD} A ${WORLD2:-a} B ${WORLD:-b} C ${WORLD2:-\"'c'\"} D ${WORLD2:-'{\"a\":\"b\"}'} E ${WORLD2:-}", | |
1214 env)); | |
1215 | |
1216 SystemToolbox::GetEnvironmentVariables(env); | |
1217 ASSERT_TRUE(env.find("NOPE") == env.end()); | |
1218 | |
1219 // The "PATH" environment variable should always be available on | |
1220 // machines running the unit tests | |
1221 ASSERT_TRUE(env.find("PATH") != env.end() /* Case used by UNIX */ || | |
1222 env.find("Path") != env.end() /* Case used by Windows */); | |
1223 | |
1224 env["PATH"] = "hello"; | |
1225 ASSERT_EQ("AhelloB", | |
1226 Toolbox::SubstituteVariables("A${PATH}B", env)); | |
1227 } | |
1228 | |
1229 | |
1230 TEST(MetricsRegistry, Basic) | |
1231 { | |
1232 { | |
1233 MetricsRegistry m; | |
1234 m.SetEnabled(false); | |
1235 m.SetValue("hello.world", 42.5f); | |
1236 | |
1237 std::string s; | |
1238 m.ExportPrometheusText(s); | |
1239 ASSERT_TRUE(s.empty()); | |
1240 } | |
1241 | |
1242 { | |
1243 MetricsRegistry m; | |
1244 m.Register("hello.world", MetricsType_Default); | |
1245 | |
1246 std::string s; | |
1247 m.ExportPrometheusText(s); | |
1248 ASSERT_TRUE(s.empty()); | |
1249 } | |
1250 | |
1251 { | |
1252 MetricsRegistry m; | |
1253 m.SetValue("hello.world", 42.5f); | |
1254 ASSERT_EQ(MetricsType_Default, m.GetMetricsType("hello.world")); | |
1255 ASSERT_THROW(m.GetMetricsType("nope"), OrthancException); | |
1256 | |
1257 std::string s; | |
1258 m.ExportPrometheusText(s); | |
1259 | |
1260 std::vector<std::string> t; | |
1261 Toolbox::TokenizeString(t, s, '\n'); | |
1262 ASSERT_EQ(2u, t.size()); | |
1263 ASSERT_EQ("hello.world 42.5 ", t[0].substr(0, 17)); | |
1264 ASSERT_TRUE(t[1].empty()); | |
1265 } | |
1266 | |
1267 { | |
1268 MetricsRegistry m; | |
1269 m.Register("hello.max", MetricsType_MaxOver10Seconds); | |
1270 m.SetValue("hello.max", 10); | |
1271 m.SetValue("hello.max", 20); | |
1272 m.SetValue("hello.max", -10); | |
1273 m.SetValue("hello.max", 5); | |
1274 | |
1275 m.Register("hello.min", MetricsType_MinOver10Seconds); | |
1276 m.SetValue("hello.min", 10); | |
1277 m.SetValue("hello.min", 20); | |
1278 m.SetValue("hello.min", -10); | |
1279 m.SetValue("hello.min", 5); | |
1280 | |
1281 m.Register("hello.default", MetricsType_Default); | |
1282 m.SetValue("hello.default", 10); | |
1283 m.SetValue("hello.default", 20); | |
1284 m.SetValue("hello.default", -10); | |
1285 m.SetValue("hello.default", 5); | |
1286 | |
1287 ASSERT_EQ(MetricsType_MaxOver10Seconds, m.GetMetricsType("hello.max")); | |
1288 ASSERT_EQ(MetricsType_MinOver10Seconds, m.GetMetricsType("hello.min")); | |
1289 ASSERT_EQ(MetricsType_Default, m.GetMetricsType("hello.default")); | |
1290 | |
1291 std::string s; | |
1292 m.ExportPrometheusText(s); | |
1293 | |
1294 std::vector<std::string> t; | |
1295 Toolbox::TokenizeString(t, s, '\n'); | |
1296 ASSERT_EQ(4u, t.size()); | |
1297 ASSERT_TRUE(t[3].empty()); | |
1298 | |
1299 std::map<std::string, std::string> u; | |
1300 for (size_t i = 0; i < t.size() - 1; i++) | |
1301 { | |
1302 std::vector<std::string> v; | |
1303 Toolbox::TokenizeString(v, t[i], ' '); | |
1304 u[v[0]] = v[1]; | |
1305 } | |
1306 | |
1307 ASSERT_EQ("20", u["hello.max"]); | |
1308 ASSERT_EQ("-10", u["hello.min"]); | |
1309 ASSERT_EQ("5", u["hello.default"]); | |
1310 } | |
1311 | |
1312 { | |
1313 MetricsRegistry m; | |
1314 | |
1315 m.SetValue("a", 10); | |
1316 m.SetValue("b", 10, MetricsType_MinOver10Seconds); | |
1317 | |
1318 m.Register("c", MetricsType_MaxOver10Seconds); | |
1319 m.SetValue("c", 10, MetricsType_MinOver10Seconds); | |
1320 | |
1321 m.Register("d", MetricsType_MaxOver10Seconds); | |
1322 m.Register("d", MetricsType_Default); | |
1323 | |
1324 ASSERT_EQ(MetricsType_Default, m.GetMetricsType("a")); | |
1325 ASSERT_EQ(MetricsType_MinOver10Seconds, m.GetMetricsType("b")); | |
1326 ASSERT_EQ(MetricsType_MaxOver10Seconds, m.GetMetricsType("c")); | |
1327 ASSERT_EQ(MetricsType_Default, m.GetMetricsType("d")); | |
1328 } | |
1329 | |
1330 { | |
1331 MetricsRegistry m; | |
1332 | |
1333 { | |
1334 MetricsRegistry::Timer t1(m, "a"); | |
1335 MetricsRegistry::Timer t2(m, "b", MetricsType_MinOver10Seconds); | |
1336 } | |
1337 | |
1338 ASSERT_EQ(MetricsType_MaxOver10Seconds, m.GetMetricsType("a")); | |
1339 ASSERT_EQ(MetricsType_MinOver10Seconds, m.GetMetricsType("b")); | |
1340 } | |
1341 } |