base64test.cpp
来自「This software aims to create an applet a」· C++ 代码 · 共 213 行
CPP
213 行
//
// Base64Test.cpp
//
// $Id: //poco/Main/Foundation/testsuite/src/Base64Test.cpp#5 $
//
// Copyright (c) 2004, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Redistributions in any form must be accompanied by information on
// how to obtain complete source code for this software and any
// accompanying software that uses this software. The source code
// must either be included in the distribution or be available for no
// more than the cost of distribution plus a nominal fee, and must be
// freely redistributable under reasonable conditions. For an
// executable file, complete source code means the source code for all
// modules it contains. It does not include source code for modules or
// files that typically accompany the major components of the operating
// system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#include "Base64Test.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Foundation/Base64Encoder.h"
#include "Foundation/Base64Decoder.h"
#include "Foundation/Exception.h"
#include <sstream>
using Foundation::Base64Encoder;
using Foundation::Base64Decoder;
using Foundation::DataFormatException;
Base64Test::Base64Test(const std::string& name): CppUnit::TestCase(name)
{
}
Base64Test::~Base64Test()
{
}
void Base64Test::testEncoder()
{
{
std::ostringstream str;
Base64Encoder encoder(str);
encoder << std::string("\00\01\02\03\04\05", 6);
encoder.close();
assert (str.str() == "AAECAwQF");
}
{
std::ostringstream str;
Base64Encoder encoder(str);
encoder << std::string("\00\01\02\03", 4);
encoder.close();
assert (str.str() == "AAECAw==");
}
{
std::ostringstream str;
Base64Encoder encoder(str);
encoder << "ABCDEF";
encoder.close();
assert (str.str() == "QUJDREVG");
}
}
void Base64Test::testDecoder()
{
{
std::istringstream istr("AAECAwQF");
Base64Decoder decoder(istr);
assert (decoder.good() && decoder.get() == 0);
assert (decoder.good() && decoder.get() == 1);
assert (decoder.good() && decoder.get() == 2);
assert (decoder.good() && decoder.get() == 3);
assert (decoder.good() && decoder.get() == 4);
assert (decoder.good() && decoder.get() == 5);
assert (decoder.good() && decoder.get() == -1);
}
{
std::istringstream istr("AAECAwQ=");
Base64Decoder decoder(istr);
assert (decoder.good() && decoder.get() == 0);
assert (decoder.good() && decoder.get() == 1);
assert (decoder.good() && decoder.get() == 2);
assert (decoder.good() && decoder.get() == 3);
assert (decoder.good() && decoder.get() == 4);
assert (decoder.good() && decoder.get() == -1);
}
{
std::istringstream istr("AAECAw==");
Base64Decoder decoder(istr);
assert (decoder.good() && decoder.get() == 0);
assert (decoder.good() && decoder.get() == 1);
assert (decoder.good() && decoder.get() == 2);
assert (decoder.good() && decoder.get() == 3);
assert (decoder.good() && decoder.get() == -1);
}
{
std::istringstream istr("QUJDREVG");
Base64Decoder decoder(istr);
std::string s;
decoder >> s;
assert (s == "ABCDEF");
assert (decoder.eof());
assert (!decoder.fail());
}
{
std::istringstream istr("QUJ\r\nDRE\r\nVG");
Base64Decoder decoder(istr);
std::string s;
decoder >> s;
assert (s == "ABCDEF");
assert (decoder.eof());
assert (!decoder.fail());
}
{
std::istringstream istr("QUJD#REVG");
Base64Decoder decoder(istr);
std::string s;
try
{
decoder >> s;
assert (decoder.bad());
}
catch (DataFormatException&)
{
}
assert (!decoder.eof());
}
}
void Base64Test::testEncodeDecode()
{
{
std::stringstream str;
Base64Encoder encoder(str);
encoder << "The quick brown fox ";
encoder << "jumped over the lazy dog.";
encoder.close();
Base64Decoder decoder(str);
std::string s;
int c = decoder.get();
while (c != -1) { s += char(c); c = decoder.get(); }
assert (s == "The quick brown fox jumped over the lazy dog.");
}
{
std::string src;
for (int i = 0; i < 255; ++i) src += char(i);
std::stringstream str;
Base64Encoder encoder(str);
encoder.write(src.data(), (std::streamsize) src.size());
encoder.close();
Base64Decoder decoder(str);
std::string s;
int c = decoder.get();
while (c != -1) { s += char(c); c = decoder.get(); }
assert (s == src);
}
}
void Base64Test::setUp()
{
}
void Base64Test::tearDown()
{
}
CppUnit::Test* Base64Test::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("Base64Test");
CppUnit_addTest(pSuite, Base64Test, testEncoder);
CppUnit_addTest(pSuite, Base64Test, testDecoder);
CppUnit_addTest(pSuite, Base64Test, testEncodeDecode);
return pSuite;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?