📄 hexbinarytest.cpp
字号:
//
// HexBinaryTest.cpp
//
// $Id: //poco/Main/Foundation/testsuite/src/HexBinaryTest.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 "HexBinaryTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Foundation/HexBinaryEncoder.h"
#include "Foundation/HexBinaryDecoder.h"
#include "Foundation/Exception.h"
#include <sstream>
using Foundation::HexBinaryEncoder;
using Foundation::HexBinaryDecoder;
using Foundation::DataFormatException;
HexBinaryTest::HexBinaryTest(const std::string& name): CppUnit::TestCase(name)
{
}
HexBinaryTest::~HexBinaryTest()
{
}
void HexBinaryTest::testEncoder()
{
{
std::ostringstream str;
HexBinaryEncoder encoder(str);
encoder << std::string("\00\01\02\03\04\05", 6);
encoder.close();
assert (str.str() == "000102030405");
}
{
std::ostringstream str;
HexBinaryEncoder encoder(str);
encoder << std::string("\00\01\02\03", 4);
encoder.close();
assert (str.str() == "00010203");
}
{
std::ostringstream str;
HexBinaryEncoder encoder(str);
encoder << "ABCDEF";
encoder << char(0xaa) << char(0xbb);
encoder.close();
assert (str.str() == "414243444546aabb");
}
}
void HexBinaryTest::testDecoder()
{
{
std::istringstream istr("000102030405");
HexBinaryDecoder 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("0001020304");
HexBinaryDecoder 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("0a0bcdef");
HexBinaryDecoder decoder(istr);
assert (decoder.good() && decoder.get() == 0x0a);
assert (decoder.good() && decoder.get() == 0x0b);
assert (decoder.good() && decoder.get() == 0xcd);
assert (decoder.good() && decoder.get() == 0xef);
assert (decoder.good() && decoder.get() == -1);
}
{
std::istringstream istr("0A0BCDEF");
HexBinaryDecoder decoder(istr);
assert (decoder.good() && decoder.get() == 0x0a);
assert (decoder.good() && decoder.get() == 0x0b);
assert (decoder.good() && decoder.get() == 0xcd);
assert (decoder.good() && decoder.get() == 0xef);
assert (decoder.good() && decoder.get() == -1);
}
{
std::istringstream istr("00 01 02 03");
HexBinaryDecoder 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("414243444546");
HexBinaryDecoder decoder(istr);
std::string s;
decoder >> s;
assert (s == "ABCDEF");
assert (decoder.eof());
assert (!decoder.fail());
}
{
std::istringstream istr("4041\r\n4243\r\n4445");
HexBinaryDecoder decoder(istr);
std::string s;
decoder >> s;
assert (s == "@ABCDE");
assert (decoder.eof());
assert (!decoder.fail());
}
{
std::istringstream istr("AABB#CCDD");
HexBinaryDecoder decoder(istr);
std::string s;
try
{
decoder >> s;
assert (decoder.bad());
}
catch (DataFormatException&)
{
}
assert (!decoder.eof());
}
}
void HexBinaryTest::testEncodeDecode()
{
{
std::stringstream str;
HexBinaryEncoder encoder(str);
encoder << "The quick brown fox ";
encoder << "jumped over the lazy dog.";
encoder.close();
HexBinaryDecoder 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;
HexBinaryEncoder encoder(str);
encoder.write(src.data(), (std::streamsize) src.size());
encoder.close();
HexBinaryDecoder decoder(str);
std::string s;
int c = decoder.get();
while (c != -1) { s += char(c); c = decoder.get(); }
assert (s == src);
}
}
void HexBinaryTest::setUp()
{
}
void HexBinaryTest::tearDown()
{
}
CppUnit::Test* HexBinaryTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("HexBinaryTest");
CppUnit_addTest(pSuite, HexBinaryTest, testEncoder);
CppUnit_addTest(pSuite, HexBinaryTest, testDecoder);
CppUnit_addTest(pSuite, HexBinaryTest, testEncodeDecode);
return pSuite;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -