root/trunk/0028_BinaryWriterReader/BinaryWriterReaderTest.cpp

User picture

Author: Setsu

Revision: 348 («Previous)


File Size: 7.61 KB

(June 16, 2010 11:09 UTC) Almost 2 years ago

0028_BinaryWriterReader

 
Show/hide line numbers
//
//  BinaryWriterReaderTest.cpp
//
//  Created by Setsu on 6/11/2010.
//  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/Random.h>
#include <Poco/BinaryWriter.h>
#include <Poco/BinaryReader.h>
#include <Poco/TemporaryFile.h>
#include <Poco/FileStream.h>

#include <string>
#include <iostream>

#include "ScopedLogMessage.h"

//----------------------------------------
//	const
//----------------------------------------
const int kNumLoops = 2;

//----------------------------------------
//	MyClass
//----------------------------------------
class MyClass
{
public:
	MyClass() :
		m_count		(0)
	,	m_bool		(false)
	,	m_char		(0)
	,	m_uChar		(0)
	,	m_short		(0)
	,	m_uShort	(0)
	,	m_int32		(0)
	,	m_uInt32	(0)
	,	m_int64		(0)
	,	m_uInt64	(0)
	,	m_double	(0.0)
	,	m_float		(0.0f)
	,	m_str		("")
	{
	}
	~MyClass()
	{
	}
	void Randomize(void)
	{
		m_bool		= m_rnd.nextBool();
		m_char		= static_cast<char>(m_rnd.next(0x7F) * (m_rnd.nextBool() ? -1:1));
		m_uChar		= static_cast<unsigned char>(m_rnd.nextChar());
		m_short		= static_cast<short>(m_rnd.next(0x7FFF) * (m_rnd.nextBool() ? -1:1));
		m_uShort	= static_cast<unsigned short>(m_rnd.next(0xFFFF));
		m_int32		= m_rnd.next(0x7FFFFFFF) * (m_rnd.nextBool() ? -1:1);
		m_uInt32	= m_rnd.next();
		m_int64		= m_rnd.next(0x7FFFFFFF);
		m_int64		= (m_int64 << 32) + m_rnd.next();
		m_uInt64	= m_rnd.next();
		m_uInt64	= ((m_uInt64 << 32) + m_rnd.next()) * (m_rnd.nextBool() ? -1:1);
		m_double	= static_cast<double>(m_rnd.next()/1000.0);
		m_float		= static_cast<float>(m_rnd.next()/1000.0);
		m_str		= Poco::format("MyClass #%z", ++m_count);
	}
	friend std::ostream& operator << (std::ostream& lhs, MyClass& rhs)
	{
		lhs << Poco::format("m_count  = %z",	rhs.m_count)	<< std::endl;
		lhs << Poco::format("m_bool   = %b",	rhs.m_bool)		<< std::endl;
		lhs << Poco::format("m_char   = %c",	rhs.m_char)		<< std::endl;
		lhs << Poco::format("m_uChar  = %?d",	rhs.m_uChar)	<< std::endl;
		lhs << Poco::format("m_short  = %hd",	rhs.m_short)	<< std::endl;
		lhs << Poco::format("m_uShort = %hu",	rhs.m_uShort)	<< std::endl;
		lhs << Poco::format("m_int32  = %d",	rhs.m_int32)	<< std::endl;
		lhs << Poco::format("m_uInt32 = %u",	rhs.m_uInt32)	<< std::endl;
		lhs << Poco::format("m_int64  = %Ld",	rhs.m_int64)	<< std::endl;
		lhs << Poco::format("m_uInt64 = %Lu",	rhs.m_uInt64)	<< std::endl;
		lhs << Poco::format("m_double = %f",	rhs.m_double)	<< std::endl;
		lhs << Poco::format("m_float  = %hf",	rhs.m_float)	<< std::endl;
		lhs << Poco::format("m_str    = %s",	rhs.m_str)		<< std::endl;
		return lhs;
	}
	friend Poco::BinaryWriter& operator << (Poco::BinaryWriter& lhs, MyClass& rhs)
	{
		lhs << rhs.m_count
			<< rhs.m_bool
			<< rhs.m_char
			<< rhs.m_uChar
			<< rhs.m_short
			<< rhs.m_uShort
			<< rhs.m_int32
			<< rhs.m_uInt32
			<< rhs.m_int64
			<< rhs.m_uInt64
			<< rhs.m_double
			<< rhs.m_float
			<< rhs.m_str;
		return lhs;
	}
	friend Poco::BinaryReader& operator >> (Poco::BinaryReader& lhs, MyClass& rhs)
	{
		lhs >> rhs.m_count
			>> rhs.m_bool
			>> rhs.m_char
			>> rhs.m_uChar
			>> rhs.m_short
			>> rhs.m_uShort
			>> rhs.m_int32
			>> rhs.m_uInt32
			>> rhs.m_int64
			>> rhs.m_uInt64
			>> rhs.m_double
			>> rhs.m_float
			>> rhs.m_str;
		return lhs;
	}
	MyClass& operator = (const MyClass& other)
	{
		if(&other != this)
		{
			m_count		= other.m_count;
			m_bool		= other.m_bool;
			m_char		= other.m_char;
			m_uChar		= other.m_uChar;
			m_short		= other.m_short;
			m_uShort	= other.m_uShort;
			m_int32		= other.m_int32;
			m_uInt32	= other.m_uInt32;
			m_int64		= other.m_int64;
			m_uInt64	= other.m_uInt64;
			m_double	= other.m_double;
			m_float		= other.m_float;
			m_str		= other.m_str;
		}
		return *this;
	}
	bool operator == (const MyClass& other) const
	{
		return	(m_count	== other.m_count)	&&
				(m_bool		== other.m_bool)	&&
				(m_char		== other.m_char)	&&
				(m_uChar	== other.m_uChar)	&&
				(m_short	== other.m_short)	&&
				(m_uShort	== other.m_uShort)	&&
				(m_int32	== other.m_int32)	&&
				(m_uInt32	== other.m_uInt32)	&&
				(m_int64	== other.m_int64)	&&
				(m_uInt64	== other.m_uInt64)	&&
				(m_double	== other.m_double)	&&
				(m_float	== other.m_float)	&&
				(m_str		== other.m_str);
	}

private:
	Poco::Random		m_rnd;
	std::size_t			m_count;
	bool				m_bool;
	char				m_char;
	unsigned char		m_uChar;
	short				m_short;
	unsigned short		m_uShort;
	Poco::Int32			m_int32;
	Poco::UInt32		m_uInt32;
	Poco::Int64			m_int64;
	Poco::UInt64		m_uInt64;
	double				m_double;
	float				m_float;
	std::string			m_str;
};

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

	MyClass myClass;
	MyClass myClassCopy[kNumLoops];

	Poco::TemporaryFile file[kNumLoops];

	for(int i=0; i<kNumLoops; ++i)
	{
		Poco::FileOutputStream ostr(file[i].path());

		myClass.Randomize();

		msg.Message(Poco::format(" Write myClass #%d to temporary file #%d", i+1, i+1));
		Poco::BinaryWriter writer(ostr, Poco::BinaryWriter::NETWORK_BYTE_ORDER);
		writer.writeBOM();
		writer << myClass;
		myClassCopy[i] = myClass;

		msg.Message(Poco::format("  Dump myClass #%d", i+1));
		std::cout << myClass;
	}

	for(int i=0; i<kNumLoops; ++i)
	{
		Poco::FileInputStream istr(file[i].path());

		Poco::BinaryReader reader(istr);
		reader.readBOM();
		reader >> myClass;
		msg.Message(Poco::format(" Read  myClass #%d from temporary file #%d [%s]"
								, i+1
								, i+1
								, std::string((myClass == myClassCopy[i]) ? "OK":"NG")));

		msg.Message(Poco::format("  Dump myClass #%d", i+1));
		std::cout << myClass;
	}

	return 0;
}