annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3326
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
1 /**
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
6 *
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
8 * modify it under the terms of the GNU General Public License as
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
9 * published by the Free Software Foundation, either version 3 of the
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
10 * License, or (at your option) any later version.
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
11 *
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
12 * In addition, as a special exception, the copyright holders of this
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
13 * program give permission to link the code of its release with the
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
15 * that use the same license as the "OpenSSL" library), and distribute
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
16 * the linked executables. You must obey the GNU General Public License
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
17 * in all respects for all of the code used other than "OpenSSL". If you
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
18 * modify file(s) with this exception, you may extend this exception to
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
19 * your version of the file(s), but you are not obligated to do so. If
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
20 * you do not wish to do so, delete this exception statement from your
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
21 * version. If you delete this exception statement from all source files
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
22 * in the program, then also delete it here.
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
23 *
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
24 * This program is distributed in the hope that it will be useful, but
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
27 * General Public License for more details.
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
28 *
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
29 * You should have received a copy of the GNU General Public License
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
31 **/
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
32
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
33
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
34 #include "PrecompiledHeadersUnitTests.h"
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
35 #include "gtest/gtest.h"
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
36 #include "../Core/Toolbox.h"
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
37
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
38 using namespace Orthanc;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
39
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
40 TEST(Toolbox, Base64_allByteValues)
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
41 {
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
42 std::string toEncode;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
43 std::string base64Result;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
44 std::string decodedResult;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
45
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
46 size_t size = 2*256;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
47 toEncode.reserve(size);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
48 for (size_t i = 0; i < size; i++)
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
49 toEncode.push_back(i % 256);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
50
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
51 Toolbox::EncodeBase64(base64Result, toEncode);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
52 Toolbox::DecodeBase64(decodedResult, base64Result);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
53
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
54 ASSERT_EQ(toEncode, decodedResult);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
55 }
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
56
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
57 TEST(Toolbox, Base64_multipleSizes)
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
58 {
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
59 std::string toEncode;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
60 std::string base64Result;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
61 std::string decodedResult;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
62
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
63 for (size_t size = 0; size <= 5; size++)
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
64 {
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
65 printf("base64, testing size %zu\n", size);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
66 toEncode.clear();
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
67 toEncode.reserve(size);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
68 for (size_t i = 0; i < size; i++)
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
69 toEncode.push_back(i % 256);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
70
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
71 Toolbox::EncodeBase64(base64Result, toEncode);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
72 Toolbox::DecodeBase64(decodedResult, base64Result);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
73
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
74 ASSERT_EQ(toEncode, decodedResult);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
75 }
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
76 }
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
77
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
78 static std::string EncodeBase64Bis(const std::string& s)
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
79 {
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
80 std::string result;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
81 Toolbox::EncodeBase64(result, s);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
82 return result;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
83 }
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
84
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
85
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
86 TEST(Toolbox, Base64)
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
87 {
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
88 ASSERT_EQ("", EncodeBase64Bis(""));
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
89 ASSERT_EQ("YQ==", EncodeBase64Bis("a"));
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
90
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
91 const std::string hello = "SGVsbG8gd29ybGQ=";
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
92 ASSERT_EQ(hello, EncodeBase64Bis("Hello world"));
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
93
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
94 std::string decoded;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
95 Toolbox::DecodeBase64(decoded, hello);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
96 ASSERT_EQ("Hello world", decoded);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
97
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
98 // Invalid character
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
99 ASSERT_THROW(Toolbox::DecodeBase64(decoded, "?"), OrthancException);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
100
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
101 // All the allowed characters
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
102 Toolbox::DecodeBase64(decoded, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=");
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
103 }
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
104
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
105
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
106 #if 0 // enable only when compiling in Release with a C++ 11 compiler
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
107 #include <chrono> // I had troubles to link with boost::chrono ...
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
108
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
109 TEST(Toolbox, Base64_largeString)
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
110 {
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
111 std::string toEncode;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
112 std::string base64Result;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
113 std::string decodedResult;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
114
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
115 size_t size = 10 * 1024 * 1024;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
116 toEncode.reserve(size);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
117 for (size_t i = 0; i < size; i++)
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
118 toEncode.push_back(i % 256);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
119
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
120 std::chrono::high_resolution_clock::time_point start;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
121 std::chrono::high_resolution_clock::time_point afterEncoding;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
122 std::chrono::high_resolution_clock::time_point afterDecoding;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
123
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
124 start = std::chrono::high_resolution_clock::now();
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
125 Orthanc::Toolbox::EncodeBase64(base64Result, toEncode);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
126 afterEncoding = std::chrono::high_resolution_clock::now();
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
127 Orthanc::Toolbox::DecodeBase64(decodedResult, base64Result);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
128 afterDecoding = std::chrono::high_resolution_clock::now();
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
129
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
130 ASSERT_EQ(toEncode, decodedResult);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
131
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
132 printf("encoding took %zu ms\n", (std::chrono::duration_cast<std::chrono::milliseconds>(afterEncoding - start)));
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
133 printf("decoding took %zu ms\n", (std::chrono::duration_cast<std::chrono::milliseconds>(afterDecoding - afterEncoding)));
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
134 }
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
135 #endif