annotate UnitTestsSources/ToolboxTests.cpp @ 3370:872bd3b6ec72

"/modalities/{id}/query": New argument "Normalize"
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 14 May 2019 10:30:43 +0200
parents a600e5e8bd1c
children 4a8e8a96b233
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"
3331
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3326
diff changeset
36 #include "../Core/OrthancException.h"
3326
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
37 #include "../Core/Toolbox.h"
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
38
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
39 using namespace Orthanc;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
40
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
41 TEST(Toolbox, Base64_allByteValues)
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
42 {
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
43 std::string toEncode;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
44 std::string base64Result;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
45 std::string decodedResult;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
46
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
47 size_t size = 2*256;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
48 toEncode.reserve(size);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
49 for (size_t i = 0; i < size; i++)
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
50 toEncode.push_back(i % 256);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
51
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
52 Toolbox::EncodeBase64(base64Result, toEncode);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
53 Toolbox::DecodeBase64(decodedResult, base64Result);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
54
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
55 ASSERT_EQ(toEncode, decodedResult);
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
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
58 TEST(Toolbox, Base64_multipleSizes)
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
59 {
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
60 std::string toEncode;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
61 std::string base64Result;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
62 std::string decodedResult;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
63
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
64 for (size_t size = 0; size <= 5; size++)
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
65 {
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
66 printf("base64, testing size %zu\n", size);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
67 toEncode.clear();
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
68 toEncode.reserve(size);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
69 for (size_t i = 0; i < size; i++)
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
70 toEncode.push_back(i % 256);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
71
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
72 Toolbox::EncodeBase64(base64Result, toEncode);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
73 Toolbox::DecodeBase64(decodedResult, base64Result);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
74
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
75 ASSERT_EQ(toEncode, decodedResult);
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
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
79 static std::string EncodeBase64Bis(const std::string& s)
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
80 {
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
81 std::string result;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
82 Toolbox::EncodeBase64(result, s);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
83 return result;
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
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
87 TEST(Toolbox, Base64)
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
88 {
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
89 ASSERT_EQ("", EncodeBase64Bis(""));
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
90 ASSERT_EQ("YQ==", EncodeBase64Bis("a"));
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
91
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
92 const std::string hello = "SGVsbG8gd29ybGQ=";
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
93 ASSERT_EQ(hello, EncodeBase64Bis("Hello world"));
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
94
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
95 std::string decoded;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
96 Toolbox::DecodeBase64(decoded, hello);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
97 ASSERT_EQ("Hello world", decoded);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
98
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
99 // Invalid character
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
100 ASSERT_THROW(Toolbox::DecodeBase64(decoded, "?"), OrthancException);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
101
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
102 // All the allowed characters
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
103 Toolbox::DecodeBase64(decoded, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=");
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
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
107 #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
108 #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
109
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
110 TEST(Toolbox, Base64_largeString)
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
111 {
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
112 std::string toEncode;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
113 std::string base64Result;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
114 std::string decodedResult;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
115
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
116 size_t size = 10 * 1024 * 1024;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
117 toEncode.reserve(size);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
118 for (size_t i = 0; i < size; i++)
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
119 toEncode.push_back(i % 256);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
120
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
121 std::chrono::high_resolution_clock::time_point start;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
122 std::chrono::high_resolution_clock::time_point afterEncoding;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
123 std::chrono::high_resolution_clock::time_point afterDecoding;
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
124
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
125 start = std::chrono::high_resolution_clock::now();
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
126 Orthanc::Toolbox::EncodeBase64(base64Result, toEncode);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
127 afterEncoding = std::chrono::high_resolution_clock::now();
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
128 Orthanc::Toolbox::DecodeBase64(decodedResult, base64Result);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
129 afterDecoding = std::chrono::high_resolution_clock::now();
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
130
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
131 ASSERT_EQ(toEncode, decodedResult);
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
132
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
133 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
134 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
135 }
b21d4cc8e5d1 speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
diff changeset
136 #endif