⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 textconvertertest.cpp

📁 This software aims to create an applet and panel tools to manage a wireless interface card, such as
💻 CPP
字号:
//
// TextConverterTest.cpp
//
// $Id: //poco/Main/Foundation/testsuite/src/TextConverterTest.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 "TextConverterTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Foundation/TextConverter.h"
#include "Foundation/ASCIIEncoding.h"
#include "Foundation/Latin1Encoding.h"
#include "Foundation/UTF8Encoding.h"


using Foundation::TextConverter;
using Foundation::Latin1Encoding;
using Foundation::UTF8Encoding;
using Foundation::ASCIIEncoding;


TextConverterTest::TextConverterTest(const std::string& name): CppUnit::TestCase(name)
{
}


TextConverterTest::~TextConverterTest()
{
}


void TextConverterTest::testIdentityASCII()
{
	ASCIIEncoding encoding;
	TextConverter converter(encoding, encoding);
	
	std::string empty;
	std::string result0;
	int errors = converter.convert(empty, result0);
	assert (result0 == empty);
	assert (errors == 0);
	
	std::string fooBar = "foo bar";
	std::string result1;
	errors = converter.convert(fooBar, result1);
	assert (result1 == fooBar);
	assert (errors == 0);

	std::string result2;
	errors = converter.convert(fooBar.data(), (int) fooBar.length(), result2);
	assert (result2 == fooBar);
	assert (errors == 0);

	std::string result3;
	errors = converter.convert("", 0, result3);
	assert (result3.empty());
	assert (errors == 0);
	
	std::string x = "x";
	std::string result4;
	errors = converter.convert(x, result4);
	assert (result4 == x);
	assert (errors == 0);

	std::string result5;
	errors = converter.convert("x", 1, result5);
	assert (result5 == x);
	assert (errors == 0);
}


void TextConverterTest::testIdentityUTF8()
{
	UTF8Encoding encoding;
	TextConverter converter(encoding, encoding);
	
	std::string empty;
	std::string result0;
	int errors = converter.convert(empty, result0);
	assert (result0 == empty);
	assert (errors == 0);
	
	std::string fooBar = "foo bar";
	std::string result1;
	errors = converter.convert(fooBar, result1);
	assert (result1 == fooBar);
	assert (errors == 0);

	std::string result2;
	errors = converter.convert(fooBar.data(), (int) fooBar.length(), result2);
	assert (result2 == fooBar);
	assert (errors == 0);

	std::string result3;
	errors = converter.convert("", 0, result3);
	assert (result3.empty());
	assert (errors == 0);
	
	const unsigned char greek[] = {0x20, 0xce, 0xba, 0xe1, 0xbd, 0xb9, 0xcf, 0x83, 0xce, 0xbc, 0xce, 0xb5, 0x20, 0x00};
	std::string text((const char*) greek);
	
	std::string result4;
	errors = converter.convert(text, result4);
	assert (result4 == text);
	assert (errors == 0);
	
	std::string result5;
	errors = converter.convert((char*) greek, 13, result5);
	assert (result5 == text);
	assert (errors == 0);
	
	std::string x = "x";
	std::string result6;
	errors = converter.convert(x, result6);
	assert (result6 == x);
	assert (errors == 0);

	std::string result7;
	errors = converter.convert("x", 1, result7);
	assert (result7 == x);
	assert (errors == 0);
	
	std::string utfChar((char*) greek + 1, 2);
	std::string result8;
	errors = converter.convert(utfChar, result8);
	assert (result8 == utfChar);
	assert (errors == 0);
	
	std::string result9;
	errors = converter.convert((char*) greek + 1, 2, result9);
	assert (result9 == utfChar);
	assert (errors == 0);
}


void TextConverterTest::testUTF8toASCII()
{
	UTF8Encoding utf8Encoding;
	ASCIIEncoding asciiEncoding;
	TextConverter converter(utf8Encoding, asciiEncoding);

	const unsigned char greek[] = {0x20, 0xce, 0xba, 0xe1, 0xbd, 0xb9, 0xcf, 0x83, 0xce, 0xbc, 0xce, 0xb5, 0x20, 0x41, 0x42, 0x00};
	std::string text((const char*) greek);
	std::string result0;
	int errors = converter.convert(text, result0);
	assert (result0 == " ????? AB");
	assert (errors == 0);
	
	std::string result1;
	errors = converter.convert("abcde", 5, result1);
	assert (result1 == "abcde");
}


void TextConverterTest::testLatin1toUTF8()
{
	Latin1Encoding latin1Encoding;
	UTF8Encoding utf8Encoding;
	TextConverter converter(latin1Encoding, utf8Encoding);
	
	const unsigned char latin1Chars[] = {'g', 252, 'n', 't', 'e', 'r', 0};
	const unsigned char utf8Chars[]   = {'g', 195, 188, 'n', 't', 'e', 'r', 0};
	std::string latin1Text((const char*) latin1Chars);
	std::string utf8Text((const char*) utf8Chars);
	
	std::string result0;
	int errors = converter.convert(latin1Text, result0);
	assert (result0 == utf8Text);
	assert (errors == 0);
	
	std::string result1;
	errors = converter.convert(latin1Chars, 6, result1);
	assert (result0 == utf8Text);
	assert (errors == 0);
}


void TextConverterTest::testErrors()
{
	UTF8Encoding utf8Encoding;
	Latin1Encoding latin1Encoding;
	TextConverter converter(utf8Encoding, latin1Encoding);

	const unsigned char badChars[] = {'a', 'b', 255, 'c', 254, 0};
	std::string badText((const char*) badChars);
	
	std::string result;
	int errors = converter.convert(badText, result);
	assert (errors == 2);
}


void TextConverterTest::setUp()
{
}


void TextConverterTest::tearDown()
{
}


CppUnit::Test* TextConverterTest::suite()
{
	CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("TextConverterTest");

	CppUnit_addTest(pSuite, TextConverterTest, testIdentityASCII);
	CppUnit_addTest(pSuite, TextConverterTest, testIdentityUTF8);
	CppUnit_addTest(pSuite, TextConverterTest, testUTF8toASCII);
	CppUnit_addTest(pSuite, TextConverterTest, testLatin1toUTF8);
	CppUnit_addTest(pSuite, TextConverterTest, testErrors);

	return pSuite;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -