ProteoWizard
Base64Test.cpp
Go to the documentation of this file.
1//
2// $Id$
3//
4//
5// Original author: Darren Kessner <darren@proteowizard.org>
6//
7// Copyright 2007 Spielberg Family Center for Applied Proteomics
8// Cedars Sinai Medical Center, Los Angeles, California 90048
9//
10// Licensed under the Apache License, Version 2.0 (the "License");
11// you may not use this file except in compliance with the License.
12// You may obtain a copy of the License at
13//
14// http://www.apache.org/licenses/LICENSE-2.0
15//
16// Unless required by applicable law or agreed to in writing, software
17// distributed under the License is distributed on an "AS IS" BASIS,
18// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19// See the License for the specific language governing permissions and
20// limitations under the License.
21//
22
23
24#include "Std.hpp"
25#include "Base64.hpp"
26#include "unit.hpp"
27#include <cstring>
28
29
30using namespace pwiz::util;
31
32
33ostream* os_ = 0;
34
35
37{
38 const char* binary;
39 const char* text;
40};
41
42
44{
45 {"", ""},
46 {"A", "QQ=="},
47 {"AB", "QUI="},
48 {"ABC", "QUJD"},
49 {"The quick brown fox jumped over the lazy dog.",
50 "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2cu"},
51 {"Darren", "RGFycmVu"},
52};
53
54
55const int testPairCount_ = sizeof(testPairs_)/sizeof(TestPair);
56
57
58void checkTestPair(const TestPair& testPair)
59{
60 const string& from = testPair.binary;
61 const string& to = testPair.text;
62 if (os_) *os_ << from << " <--> " << to << endl;
63
64 // convert binary -> text
65 vector<char> textBuffer;
66 textBuffer.resize(Base64::binaryToTextSize(from.size()) + 1, '\0');
67 size_t textCount = Base64::binaryToText(from.c_str(), from.size(), &textBuffer[0]);
68
69 // verify binary -> text
70 string textString = !textBuffer.empty() ? &textBuffer[0] : "";
71 unit_assert(textCount == (unsigned int)to.size());
72 unit_assert(textString == to);
73
74 // convert text -> binary
75 vector<char> binaryBuffer;
76 binaryBuffer.resize(Base64::textToBinarySize(to.size()) + 1, '\0');
77 size_t binaryCount = Base64::textToBinary(to.c_str(), to.size(), &binaryBuffer[0]);
78
79 // verify text -> binary
80 string binaryString = !binaryBuffer.empty() ? &binaryBuffer[0] : "";
81 unit_assert(binaryCount == (unsigned int)from.size());
82 unit_assert(binaryString == from);
83}
84
85
86void test256()
87{
88 if (os_) *os_ << "test256()\n" << flush;
89
90 // chars from 0 to 255
91 vector<char> from(256);
92 for (int i=0; i<256; i++)
93 from[i] = (char)i;
94
95 char to[] = "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIj"
96 "JCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZH"
97 "SElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWpr"
98 "bG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6P"
99 "kJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKz"
100 "tLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX"
101 "2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7"
102 "/P3+/w==";
103
104 // convert binary -> text
105 vector<char> textBuffer;
106 textBuffer.resize(Base64::binaryToTextSize(from.size()) + 1, '\0');
107 size_t textCount = Base64::binaryToText(&from[0], 256, &textBuffer[0]);
108 textBuffer[textCount] = '\0';
109 unit_assert(textCount == (unsigned int)strlen(to));
110 unit_assert(!strcmp(to, &textBuffer[0]));
111
112 // convert text -> binary
113 vector<char> binaryBuffer;
114 binaryBuffer.resize(300);
115 size_t binaryCount = Base64::textToBinary(to, strlen(to), &binaryBuffer[0]);
116 unit_assert(binaryCount == 256);
117 for (int i=0; i<256; i++)
118 unit_assert(binaryBuffer[i] == from[i]);
119}
120
121
122void test()
123{
125 test256();
126}
127
128
129int main(int argc, char* argv[])
130{
131 TEST_PROLOG(argc, argv)
132
133 try
134 {
135 if (argc>1 && !strcmp(argv[1],"-v")) // verbose
136 os_ = &cout;
137
138 if (os_) *os_ << "Base64Test\n";
139
140 test();
141 }
142 catch (exception& e)
143 {
144 TEST_FAILED(e.what())
145 }
146 catch (...)
147 {
148 TEST_FAILED("Caught unknown exception.")
149 }
150
152}
153
int main(int argc, char *argv[])
void test256()
ostream * os_
void checkTestPair(const TestPair &testPair)
void test()
const int testPairCount_
TestPair testPairs_[]
PWIZ_API_DECL size_t textToBinary(const char *from, size_t charCount, void *to)
text -> binary conversion
PWIZ_API_DECL size_t binaryToText(const void *from, size_t byteCount, char *to)
binary -> text conversion
PWIZ_API_DECL size_t textToBinarySize(size_t charCount)
Returns sufficient buffer size for text->binary conversion.
PWIZ_API_DECL size_t binaryToTextSize(size_t byteCount)
Returns buffer size required by binary->text conversion.
const char * text
const char * binary
#define unit_assert(x)
Definition unit.hpp:85
#define TEST_EPILOG
Definition unit.hpp:183
#define TEST_FAILED(x)
Definition unit.hpp:177
#define TEST_PROLOG(argc, argv)
Definition unit.hpp:175