root/trunk/0012_String/StringTest.cpp

User picture

Author: Setsu

Revision: 348 («Previous)


File Size: 9.3 KB

(June 16, 2010 10:56 UTC) Almost 2 years ago

0012_String

 
Show/hide line numbers
//
//  StringTest.cpp
//
//  Created by Setsu on 5/10/10.
//  Copyright 2010 RoundSquare Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
// 
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//

//----------------------------------------
//	include
//----------------------------------------
#include <Poco/Logger.h>
#include <Poco/PatternFormatter.h>
#include <Poco/FormattingChannel.h>
#include <Poco/ConsoleChannel.h>
#include <Poco/Format.h>
#include <Poco/String.h>

#include <string>

#include "ScopedLogMessage.h"

//----------------------------------------
//	TestTrimLeft
//----------------------------------------
void TestTrimLeft(void)
{
	ScopedLogMessage msg("TestTrimLeft ", "start", "end\n");

	std::string str(" a b c ");
	msg.Message(Poco::format("    source  string: \"%s\"", str));

	msg.Message(Poco::format("  trimLeft(\"%s\"):", str));
	msg.Message(Poco::format("    changed string: \"%s\"", Poco::trimLeft(str)));
	msg.Message(Poco::format("    source  string: \"%s\"", str));

	msg.Message(Poco::format("  trimLeftInPlace(\"%s\"):", str));
	msg.Message(Poco::format("    changed string: \"%s\"", Poco::trimLeftInPlace(str)));
	msg.Message(Poco::format("    source  string: \"%s\"", str));
}

//----------------------------------------
//	TestTrimRight
//----------------------------------------
void TestTrimRight(void)
{
	ScopedLogMessage msg("TestTrimRight ", "start", "end\n");

	std::string str(" a b c ");
	msg.Message(Poco::format("    source  string: \"%s\"", str));

	msg.Message(Poco::format("  trimRight(\"%s\"):", str));
	msg.Message(Poco::format("    changed string: \"%s\"", Poco::trimRight(str)));
	msg.Message(Poco::format("    source  string: \"%s\"", str));

	msg.Message(Poco::format("  trimRightInPlace(\"%s\"):", str));
	msg.Message(Poco::format("    changed string: \"%s\"", Poco::trimRightInPlace(str)));
	msg.Message(Poco::format("    source  string: \"%s\"", str));
}

//----------------------------------------
//	TestTrim
//----------------------------------------
void TestTrim(void)
{
	ScopedLogMessage msg("TestTrim ", "start", "end\n");

	std::string str(" a b c ");
	msg.Message(Poco::format("    source  string: \"%s\"", str));

	msg.Message(Poco::format("  trim(\"%s\"):", str));
	msg.Message(Poco::format("    changed string: \"%s\"", Poco::trim(str)));
	msg.Message(Poco::format("    source  string: \"%s\"", str));

	msg.Message(Poco::format("  trimInPlace(\"%s\"):", str));
	msg.Message(Poco::format("    changed string: \"%s\"", Poco::trimInPlace(str)));
	msg.Message(Poco::format("    source  string: \"%s\"", str));
}

//----------------------------------------
//	TestToUpper
//----------------------------------------
void TestToUpper(void)
{
	ScopedLogMessage msg("TestToUpper ", "start", "end\n");

	std::string str(" a b c ");
	msg.Message(Poco::format("    source  string: \"%s\"", str));

	msg.Message(Poco::format("  toUpper(\"%s\"):", str));
	msg.Message(Poco::format("    changed string: \"%s\"", Poco::toUpper(str)));
	msg.Message(Poco::format("    source  string: \"%s\"", str));

	msg.Message(Poco::format("  toUpperInPlace(\"%s\"):", str));
	msg.Message(Poco::format("    changed string: \"%s\"", Poco::toUpperInPlace(str)));
	msg.Message(Poco::format("    source  string: \"%s\"", str));
}

//----------------------------------------
//	TestToLower
//----------------------------------------
void TestToLower(void)
{
	ScopedLogMessage msg("TestToLower ", "start", "end\n");

	std::string str(" a b c ");
	msg.Message(Poco::format("    source  string: \"%s\"", str));

	msg.Message(Poco::format("  toLower(\"%s\"):", str));
	msg.Message(Poco::format("    changed string: \"%s\"", Poco::toLower(str)));
	msg.Message(Poco::format("    source  string: \"%s\"", str));

	msg.Message(Poco::format("  toLowerInPlace(\"%s\"):", str));
	msg.Message(Poco::format("    changed string: \"%s\"", Poco::toLowerInPlace(str)));
	msg.Message(Poco::format("    source  string: \"%s\"", str));
}

//----------------------------------------
//	TestICompare
//----------------------------------------
void TestICompare(void)
{
	ScopedLogMessage msg("TestICompare ", "start", "end\n");

	std::string str1(" A B C ");
	std::string str2(" a b c ");
	msg.Message(Poco::format("    source  string 1: \"%s\"", str1));
	msg.Message(Poco::format("    source  string 2: \"%s\"", str2));

	msg.Message(Poco::format("  iCompare(\"%s\", \"%s\"):", str1, str2));
	msg.Message(Poco::format("    result: \"%s\"", std::string(Poco::icompare(str1, str2) ? "false":"true")));
}

//----------------------------------------
//	TestTranslate
//----------------------------------------
void TestTranslate(void)
{
	ScopedLogMessage msg("TestTranslate ", "start", "end\n");

	std::string str(" a b c ");
	std::string from("abc");
	std::string to("ABC");
	msg.Message(Poco::format("    source  string: \"%s\"", str));

	msg.Message(Poco::format("  translate(\"%s\", \"%s\", \"%s\"):", str, from, to));
	msg.Message(Poco::format("    changed string: \"%s\"", Poco::translate(str, from, to)));
	msg.Message(Poco::format("    source  string: \"%s\"", str));

	msg.Message(Poco::format("  translateInPlace(\"%s\", \"%s\", \"%s\"):", str, from, to));
	msg.Message(Poco::format("    changed string: \"%s\"", Poco::translateInPlace(str, from, to)));
	msg.Message(Poco::format("    source  string: \"%s\"", str));
}

//----------------------------------------
//	TestReplace
//----------------------------------------
void TestReplace(void)
{
	ScopedLogMessage msg("TestReplace ", "start", "end\n");

	std::string str("The quick brown fox jumps over the lazy dog.");
	std::string from("dog");
	std::string to("cat");
	msg.Message(Poco::format("    source  string: \"%s\"", str));

	msg.Message(Poco::format("  replace(\"%s\", \"%s\", \"%s\"):", str, from, to));
	msg.Message(Poco::format("    changed string: \"%s\"", Poco::replace(str, from, to)));
	msg.Message(Poco::format("    source  string: \"%s\"", str));

	msg.Message(Poco::format("  replaceInPlace(\"%s\", \"%s\", \"%s\"):", str, from, to));
	msg.Message(Poco::format("    changed string: \"%s\"", Poco::replaceInPlace(str, from, to)));
	msg.Message(Poco::format("    source  string: \"%s\"", str));
}

//----------------------------------------
//	TestCat
//----------------------------------------
void TestCat(void)
{
	ScopedLogMessage msg("TestCat ", "start", "end\n");

	const char* strs[] = {	"Pack "
						,	"my box "
						,	"with "
						,	"five dozen "
						,	"liquor "
						,	"jugs."};
	
	msg.Message(Poco::format("  cat(\"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\")",
								std::string(strs[0])
							,	std::string(strs[1])
							,	std::string(strs[2])
							,	std::string(strs[3])
							,	std::string(strs[4])
							,	std::string(strs[5])));
	msg.Message(Poco::format("    combined string: \"%s\"",
							Poco::cat(
								std::string(strs[0])
							,	std::string(strs[1])
							,	std::string(strs[2])
							,	std::string(strs[3])
							,	std::string(strs[4])
							,	std::string(strs[5]))));
}

//----------------------------------------
//	typedef
//----------------------------------------
typedef void (*TestProc)(void);

//----------------------------------------
//	table
//----------------------------------------
TestProc testProcs[] = {	TestTrimLeft
						,	TestTrimRight
						,	TestTrim
						,	TestToUpper
						,	TestToLower
						,	TestICompare
						,	TestTranslate
						,	TestReplace
						,	TestCat
						};

//----------------------------------------
//	PrepareConsoleLogger
//----------------------------------------
void PrepareConsoleLogger(const std::string& name, int level=Poco::Message::PRIO_INFORMATION)
{
	Poco::FormattingChannel* pFCConsole = new Poco::FormattingChannel(new Poco::PatternFormatter("%t"));
	pFCConsole->setChannel(new Poco::ConsoleChannel);
	pFCConsole->open();

	Poco::Logger::create(name, pFCConsole, level);
}

//----------------------------------------
//	main
//----------------------------------------
int main(int /*argc*/, char** /*argv*/)
{
	PrepareConsoleLogger(Poco::Logger::ROOT, Poco::Message::PRIO_INFORMATION);

	ScopedLogMessage msg("StringTest ", "start\n", "end");

	for(std::size_t i=0; i<sizeof(testProcs)/sizeof(testProcs[0]); ++i)
	{
		testProcs[i]();
	}

	return 0;
}