ProteoWizard
Classes | Functions | Variables
Base64Test.cpp File Reference
#include "Std.hpp"
#include "Base64.hpp"
#include "unit.hpp"
#include <cstring>

Go to the source code of this file.

Classes

struct  TestPair
 

Functions

void checkTestPair (const TestPair &testPair)
 
void test256 ()
 
void test ()
 
int main (int argc, char *argv[])
 

Variables

ostream * os_ = 0
 
TestPair testPairs_ []
 
const int testPairCount_ = sizeof(testPairs_)/sizeof(TestPair)
 

Function Documentation

§ checkTestPair()

void checkTestPair ( const TestPair testPair)

Definition at line 58 of file Base64Test.cpp.

References TestPair::binary, pwiz::util::Base64::binaryToText(), pwiz::util::Base64::binaryToTextSize(), os_, TestPair::text, pwiz::util::Base64::textToBinary(), pwiz::util::Base64::textToBinarySize(), and unit_assert.

Referenced by test().

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 }
ostream * os_
Definition: Base64Test.cpp:33
PWIZ_API_DECL size_t textToBinarySize(size_t charCount)
Returns sufficient buffer size for text->binary conversion.
PWIZ_API_DECL size_t binaryToText(const void *from, size_t byteCount, char *to)
binary -> text conversion
const char * text
Definition: Base64Test.cpp:39
const char * binary
Definition: Base64Test.cpp:38
PWIZ_API_DECL size_t textToBinary(const char *from, size_t charCount, void *to)
text -> binary conversion
#define unit_assert(x)
Definition: unit.hpp:85
PWIZ_API_DECL size_t binaryToTextSize(size_t byteCount)
Returns buffer size required by binary->text conversion.

§ test256()

void test256 ( )

Definition at line 86 of file Base64Test.cpp.

References pwiz::util::Base64::binaryToText(), pwiz::util::Base64::binaryToTextSize(), os_, pwiz::util::Base64::textToBinary(), and unit_assert.

Referenced by test().

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 }
ostream * os_
Definition: Base64Test.cpp:33
PWIZ_API_DECL size_t binaryToText(const void *from, size_t byteCount, char *to)
binary -> text conversion
PWIZ_API_DECL size_t textToBinary(const char *from, size_t charCount, void *to)
text -> binary conversion
#define unit_assert(x)
Definition: unit.hpp:85
PWIZ_API_DECL size_t binaryToTextSize(size_t byteCount)
Returns buffer size required by binary->text conversion.

§ test()

void test ( )

Definition at line 122 of file Base64Test.cpp.

References checkTestPair(), test256(), and testPairCount_.

Referenced by main().

123 {
125  test256();
126 }
TestPair testPairs_[]
Definition: Base64Test.cpp:43
const int testPairCount_
Definition: Base64Test.cpp:55
void test256()
Definition: Base64Test.cpp:86
void checkTestPair(const TestPair &testPair)
Definition: Base64Test.cpp:58

§ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 129 of file Base64Test.cpp.

References os_, test(), TEST_EPILOG, TEST_FAILED, and TEST_PROLOG.

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 }
ostream * os_
Definition: Base64Test.cpp:33
#define TEST_EPILOG
Definition: unit.hpp:182
void test()
Definition: Base64Test.cpp:122
#define TEST_FAILED(x)
Definition: unit.hpp:176
#define TEST_PROLOG(argc, argv)
Definition: unit.hpp:174

Variable Documentation

§ os_

ostream* os_ = 0

Definition at line 33 of file Base64Test.cpp.

Referenced by checkTestPair(), main(), and test256().

§ testPairs_

TestPair testPairs_[]
Initial value:
=
{
{"", ""},
{"A", "QQ=="},
{"AB", "QUI="},
{"ABC", "QUJD"},
{"The quick brown fox jumped over the lazy dog.",
"VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2cu"},
{"Darren", "RGFycmVu"},
}

Definition at line 43 of file Base64Test.cpp.

§ testPairCount_

const int testPairCount_ = sizeof(testPairs_)/sizeof(TestPair)

Definition at line 55 of file Base64Test.cpp.

Referenced by test().