root/trunk/0011_Format/FormatTest.cpp

User picture

Author: Setsu

Revision: 348 («Previous)


File Size: 12.4 KB

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

0011_Format

 
Show/hide line numbers
//
//  FormatTest.cpp
//
//  Created by Setsu on 5/8/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 "ScopedLogMessage.h"

//----------------------------------------
//	FormatTestCommon
//----------------------------------------
void FormatTestCommon(std::string& str, const std::string& fmt, ScopedLogMessage& msg)
{
	const std::size_t kstrLength = 32;

	do {
		str += " ";
	} while(str.length() < kstrLength);
	str += "[fmt=\""+fmt+"\"]";
	msg.Message(str);
}

//----------------------------------------
//	FormatTest1
//----------------------------------------
template <class T>
void FormatTest1(const T& data, const std::string& fmt, ScopedLogMessage& msg)
{
	std::string str = Poco::format("  "+fmt, data);
	FormatTestCommon(str, fmt, msg);
}

//----------------------------------------
//	FormatTest2
//----------------------------------------
template <class T>
void FormatTest2(const T& data1, const T& data2, const std::string& fmt, ScopedLogMessage& msg)
{
	std::string str = Poco::format("  "+fmt, data1, data2);
	FormatTestCommon(str, fmt, msg);
}

//----------------------------------------
//	BadCastExceptionTest
//----------------------------------------
template <class T>
void BadCastExceptionTest(const T& data, const std::string& fmt, const std::string& type, ScopedLogMessage& msg)
{
	try
	{
		msg.Message(Poco::format(fmt, data));
	}
	catch (Poco::BadCastException&)
	{
		msg.Message("  [fmt=\""+fmt+"\"] does not accept "+type);
	}
}

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

	bool t = true;
	bool f = false;

	FormatTest2(t, f, "true=%b, false=%b", msg);
	FormatTest2(t, f, "true=%-2b, false=%2b", msg);

	BadCastExceptionTest(static_cast<int>(1), "true=%b", "int", msg);
}

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

	char c = 'a';

	FormatTest1(c, "char=%c", msg);
	FormatTest2(c, c, "char=%-2c, char=%2c", msg);

	BadCastExceptionTest(std::string("foo"), "char=%c", "std::string", msg);
}

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

	const int i = 12;

	FormatTest1(i, "int=%d", msg);
	FormatTest2(i, i, "int=%-3d, int=%3d", msg);
	FormatTest2(i, -1*i, "int=%+4d, int=%+4d", msg);

	BadCastExceptionTest(static_cast<short>(56), "int=%d", "short", msg);
}

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

	const unsigned i = 12;

	FormatTest1(i, "unsigned=%u", msg);
	FormatTest2(i, i, "unsigned=%-3u, unsigned=%3u", msg);

	BadCastExceptionTest(static_cast<int>(56), "unsigned=%u", "int", msg);
}

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

	const short i = 12;

	FormatTest1(i, "short=%hd", msg);
	FormatTest2(i, i, "short=%-3hd, short=%3hd", msg);
	FormatTest2(i, static_cast<short>(-1*i), "short=%+4hd, short=%+4hd", msg);

	BadCastExceptionTest(static_cast<int>(56), "short=%hd", "int", msg);
}

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

	const unsigned short i = 12;

	FormatTest1(i, "ushort=%hu", msg);
	FormatTest2(i, i, "ushort=%-3hu, ushort=%3hu", msg);

	BadCastExceptionTest(static_cast<int>(56), "ushort=%hu", "int", msg);
}

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

	const long i = 12;

	FormatTest1(i, "long=%ld", msg);
	FormatTest2(i, i, "long=%-3ld, long=%3ld", msg);
	FormatTest2(i, static_cast<long>(-1*i), "long=%+4ld, long=%+4ld", msg);

	BadCastExceptionTest(static_cast<int>(56), "long=%ld", "int", msg);
}

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

	const unsigned long i = 12;

	FormatTest1(i, "ulong=%lu", msg);
	FormatTest2(i, i, "ulong=%-3lu, ulong=%3lu", msg);

	BadCastExceptionTest(static_cast<unsigned>(56), "ulong=%lu", "unsigned", msg);
}

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

	const Poco::Int64 i = 12;

	FormatTest1(i, "Int64=%Ld", msg);
	FormatTest2(i, i, "Int64=%-3Ld, Int64=%3Ld", msg);
	FormatTest2(i, static_cast<Poco::Int64>(-1*i), "Int64=%+4Ld, Int64=%+4Ld", msg);

	BadCastExceptionTest(static_cast<int>(56), "Int64=%Ld", "int", msg);
}

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

	const Poco::UInt64 i = 12;

	FormatTest1(i, "UInt64=%Lu", msg);
	FormatTest2(i, i, "UInt64=%-3Lu, UInt64=%3Lu", msg);

	BadCastExceptionTest(static_cast<unsigned>(56), "UInt64=%Lu", "unsigned", msg);
}

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

	const unsigned i = 012;

	FormatTest1(i, "octal=%o", msg);
	FormatTest1(i, "octal=%#o", msg);
	FormatTest2(i, i, "octal=%4o, octal=%04o", msg);

	BadCastExceptionTest(static_cast<int>(56), "octal=%o", "int", msg);
}

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

	const unsigned i = 0x1A;

	FormatTest1(i, "hex=%x", msg);
	FormatTest1(i, "hex=%X", msg);
	FormatTest1(i, "hex=%#x", msg);
	FormatTest1(i, "hex=%#X", msg);
	FormatTest2(i, i, "hex=%4x, hex=%04x", msg);
	FormatTest2(i, i, "hex=%4X, hex=%04X", msg);

	BadCastExceptionTest(static_cast<short>(56), "hex=%x", "short", msg);
}

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

	const double i = 1.2;

	FormatTest1(i, "double=%f", msg);
	FormatTest2(i, i, "double=%-5.2f, double=%5.2f", msg);

	FormatTest1(i, "double=%e", msg);
	FormatTest2(i, i, "double=%-9.2e, double=%9.2e", msg);

	BadCastExceptionTest(static_cast<float>(1.2), "double=%f", "float", msg);
}

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

	const float i = 1.2f;

	FormatTest1(i, "float=%hf", msg);
	FormatTest2(i, i, "float=%-5.2hf, float=%5.2hf", msg);

	FormatTest1(i, "float=%he", msg);
	FormatTest2(i, i, "float=%-9.2he, float=%9.2he", msg);

	BadCastExceptionTest(static_cast<double>(1.2), "float=%hf", "double", msg);
}

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

	const std::string str("bar");

	FormatTest1(str, "string=%s", msg);
	FormatTest2(str, str, "string=%-4s, string=%4s", msg);

	BadCastExceptionTest(static_cast<const char*>("test"), "string=%s", "const char*", msg);
}

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

	const std::size_t i = 12;

	FormatTest1(i, "size_t=%z", msg);
	FormatTest2(i, i, "size_t=%-3z, size_t=%3z", msg);

	BadCastExceptionTest(static_cast<int>(12), "size_t=%z", "int", msg);
}

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

	FormatTest1(static_cast<bool>(true),				"bool=%?i",				msg);
	FormatTest1(static_cast<char>(42),					"char=%?i",				msg);
	FormatTest1(static_cast<signed char>(-42),			"signed char=%?i",		msg);
	FormatTest1(static_cast<unsigned char>(65),			"unsigned char=%?i",	msg);
	FormatTest1(static_cast<short>(-134),				"short=%?i",			msg);
	FormatTest1(static_cast<unsigned short>(200),		"unsigned short=%?i",	msg);
	FormatTest1(static_cast<int>(-12345),				"int=%?i",				msg);
	FormatTest1(static_cast<unsigned>(12345),			"unsigned=%?i",			msg);
	FormatTest1(static_cast<long>(-54321),				"long=%?i",				msg);
	FormatTest1(static_cast<unsigned long>(54321),		"unsigned long=%?i",	msg);
	FormatTest1(static_cast<Poco::Int64>(-12345678),	"Poco::Int64=%?i",		msg);
	FormatTest1(static_cast<Poco::UInt64>(12345678),	"Poco::UInt64=%?i",		msg);
	FormatTest1(static_cast<short>(012),				"octal=%#?o",			msg);
	FormatTest1(static_cast<short>(127),				"hex=%#?x",				msg);
}

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

//----------------------------------------
//	table
//----------------------------------------
TestProc testProcs[] = {	TestBoolean
						,	TestCharacter
						,	TestSignedInteger
						,	TestUnsignedInteger
						,	TestSignedShortInteger
						,	TestUnsignedShortInteger
						,	TestSignedLongInteger
						,	TestUnsignedLongInteger
						,	TestSignedLongLongInteger
						,	TestUnsignedLongLongInteger
						,	TestUnsignedOctalInteger
						,	TestUnsignedHexInteger
						,	TestDouble
						,	TestFloat
						,	TestString
						,	TestSize_t
						,	TestAnyInteger	};

//----------------------------------------
//	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("FormatTest ", "start\n", "end");

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

	return 0;
}