Mercurial > hg > orthanc
comparison UnitTestsSources/ToolboxTests.cpp @ 3326:b21d4cc8e5d1
speed up base64 decoding + added tests
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Thu, 21 Mar 2019 11:41:03 +0100 |
parents | |
children | a600e5e8bd1c |
comparison
equal
deleted
inserted
replaced
3325:2e7c5c15ba25 | 3326:b21d4cc8e5d1 |
---|---|
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-2019 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 | |
34 #include "PrecompiledHeadersUnitTests.h" | |
35 #include "gtest/gtest.h" | |
36 #include "../Core/Toolbox.h" | |
37 | |
38 using namespace Orthanc; | |
39 | |
40 TEST(Toolbox, Base64_allByteValues) | |
41 { | |
42 std::string toEncode; | |
43 std::string base64Result; | |
44 std::string decodedResult; | |
45 | |
46 size_t size = 2*256; | |
47 toEncode.reserve(size); | |
48 for (size_t i = 0; i < size; i++) | |
49 toEncode.push_back(i % 256); | |
50 | |
51 Toolbox::EncodeBase64(base64Result, toEncode); | |
52 Toolbox::DecodeBase64(decodedResult, base64Result); | |
53 | |
54 ASSERT_EQ(toEncode, decodedResult); | |
55 } | |
56 | |
57 TEST(Toolbox, Base64_multipleSizes) | |
58 { | |
59 std::string toEncode; | |
60 std::string base64Result; | |
61 std::string decodedResult; | |
62 | |
63 for (size_t size = 0; size <= 5; size++) | |
64 { | |
65 printf("base64, testing size %zu\n", size); | |
66 toEncode.clear(); | |
67 toEncode.reserve(size); | |
68 for (size_t i = 0; i < size; i++) | |
69 toEncode.push_back(i % 256); | |
70 | |
71 Toolbox::EncodeBase64(base64Result, toEncode); | |
72 Toolbox::DecodeBase64(decodedResult, base64Result); | |
73 | |
74 ASSERT_EQ(toEncode, decodedResult); | |
75 } | |
76 } | |
77 | |
78 static std::string EncodeBase64Bis(const std::string& s) | |
79 { | |
80 std::string result; | |
81 Toolbox::EncodeBase64(result, s); | |
82 return result; | |
83 } | |
84 | |
85 | |
86 TEST(Toolbox, Base64) | |
87 { | |
88 ASSERT_EQ("", EncodeBase64Bis("")); | |
89 ASSERT_EQ("YQ==", EncodeBase64Bis("a")); | |
90 | |
91 const std::string hello = "SGVsbG8gd29ybGQ="; | |
92 ASSERT_EQ(hello, EncodeBase64Bis("Hello world")); | |
93 | |
94 std::string decoded; | |
95 Toolbox::DecodeBase64(decoded, hello); | |
96 ASSERT_EQ("Hello world", decoded); | |
97 | |
98 // Invalid character | |
99 ASSERT_THROW(Toolbox::DecodeBase64(decoded, "?"), OrthancException); | |
100 | |
101 // All the allowed characters | |
102 Toolbox::DecodeBase64(decoded, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="); | |
103 } | |
104 | |
105 | |
106 #if 0 // enable only when compiling in Release with a C++ 11 compiler | |
107 #include <chrono> // I had troubles to link with boost::chrono ... | |
108 | |
109 TEST(Toolbox, Base64_largeString) | |
110 { | |
111 std::string toEncode; | |
112 std::string base64Result; | |
113 std::string decodedResult; | |
114 | |
115 size_t size = 10 * 1024 * 1024; | |
116 toEncode.reserve(size); | |
117 for (size_t i = 0; i < size; i++) | |
118 toEncode.push_back(i % 256); | |
119 | |
120 std::chrono::high_resolution_clock::time_point start; | |
121 std::chrono::high_resolution_clock::time_point afterEncoding; | |
122 std::chrono::high_resolution_clock::time_point afterDecoding; | |
123 | |
124 start = std::chrono::high_resolution_clock::now(); | |
125 Orthanc::Toolbox::EncodeBase64(base64Result, toEncode); | |
126 afterEncoding = std::chrono::high_resolution_clock::now(); | |
127 Orthanc::Toolbox::DecodeBase64(decodedResult, base64Result); | |
128 afterDecoding = std::chrono::high_resolution_clock::now(); | |
129 | |
130 ASSERT_EQ(toEncode, decodedResult); | |
131 | |
132 printf("encoding took %zu ms\n", (std::chrono::duration_cast<std::chrono::milliseconds>(afterEncoding - start))); | |
133 printf("decoding took %zu ms\n", (std::chrono::duration_cast<std::chrono::milliseconds>(afterDecoding - afterEncoding))); | |
134 } | |
135 #endif |