root/trunk/0017_FileStream/FileStreamTest.cpp

User picture

Author: Setsu

Revision: 348 («Previous)


File Size: 9.38 KB

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

0017_FileStream

 
Show/hide line numbers
//
//  FileStreamTest.cpp
//
//  Created by Setsu on 5/20/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/FileStream.h>
#include <Poco/File.h>
#include <Poco/StreamCopier.h>
#include <Poco/TeeStream.h>
#include <Poco/InflatingStream.h>
#include <Poco/DeflatingStream.h>
#include <Poco/Base64Encoder.h>
#include <Poco/Base64Decoder.h>
#include <Poco/HexBinaryEncoder.h>
#include <Poco/HexBinaryDecoder.h>
#include <Poco/CountingStream.h>
#include <Poco/Net/QuotedPrintableEncoder.h>
#include <Poco/Net/QuotedPrintableDecoder.h>
#include <Poco/Format.h>
#include <Poco/TemporaryFile.h>

#include <string>
#include <sstream>

#include "ScopedLogMessage.h"

//----------------------------------------
//	typedef
//----------------------------------------
typedef void (*TestRoutine)(ScopedLogMessage& msg);

//----------------------------------------
//	constant
//----------------------------------------
const char* const kSampleText =
	"THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n"
	"IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n"
	"FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT\n"
	"SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE\n"
	"FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,\n"
	"ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER\n"
	"DEALINGS IN THE SOFTWARE.";

//----------------------------------------
//	enum
//----------------------------------------
enum
{
	eStandard = 0
,	eGZIP
,	eZLIB
};

//----------------------------------------
//	TestSimple
//----------------------------------------
void TestSimple(ScopedLogMessage& msg)
{
	Poco::TemporaryFile file;
	std::ostringstream ostrBackup;

	msg.Message(" --- Simple file wrie/read ---");
	{
		Poco::FileOutputStream ostr(file.path());
		Poco::TeeOutputStream tos(ostr);
		tos.addStream(ostrBackup);
		tos << kSampleText;
	}
	msg.Message(Poco::format("  File size        : %z bytes", static_cast<std::size_t>(file.getSize())));
	{
		Poco::FileInputStream istr(file.path());
		std::string str;
		Poco::StreamCopier::copyToString(istr, str);
		msg.Message(Poco::format("  Write/Read check : %s", std::string(0 == str.compare(ostrBackup.str()) ? "OK":"NG")));
	}
}

//----------------------------------------
//	TestCountingStream
//----------------------------------------
void TestCountingStream(ScopedLogMessage& msg)
{
	Poco::TemporaryFile file;
	std::ostringstream ostrBackup;

	msg.Message(" --- Simple file wrie/read with counting stream ---");
	{
		Poco::FileOutputStream ostr(file.path());
		Poco::CountingOutputStream countingOstr(ostr);
		Poco::TeeOutputStream tos(countingOstr);
		tos.addStream(ostrBackup);
		tos << kSampleText;

		msg.Message(Poco::format("  Characters       : %d", countingOstr.chars()));
		msg.Message(Poco::format("  Lines            : %d", countingOstr.lines()));
	}
	msg.Message(Poco::format("  File size        : %z bytes", static_cast<std::size_t>(file.getSize())));
	{
		Poco::FileInputStream istr(file.path());
		Poco::CountingInputStream countingIstr(istr);
		std::string str;
		Poco::StreamCopier::copyToString(countingIstr, str);
		msg.Message(Poco::format("  Write/Read check : %s", std::string(0 == str.compare(ostrBackup.str()) ? "OK":"NG")));
		msg.Message(Poco::format("  Characters       : %d", countingIstr.chars()));
		msg.Message(Poco::format("  Lines            : %d", countingIstr.lines()));
	}
}

//----------------------------------------
//	TestEncodeDecode
//----------------------------------------
template <class Tenc, class Tdec, int sel>
class TestEncodeDecode
{
public:
	void Test(ScopedLogMessage& msg, const std::string& text)
	{
		Poco::TemporaryFile file;
		std::ostringstream ostrBackup;

		msg.Message(text);
		{
			Poco::FileOutputStream ostr(file.path());
			Tenc* encoder = Encoder(ostr, Int2Type<sel>());
			Poco::TeeOutputStream tos(*encoder);
			tos.addStream(ostrBackup);
			tos << kSampleText;
			delete encoder;
		}
		msg.Message(Poco::format("  File size        : %z bytes (%5.2f%% of original)"
								,	static_cast<std::size_t>(file.getSize())
								,	(100.0*file.getSize())/(std::string(kSampleText)).length()));
		{
			Poco::FileInputStream istr(file.path());
			Tdec* decoder = Decoder(istr, Int2Type<sel>());
			std::string str;
			Poco::StreamCopier::copyToString(*decoder, str);
			msg.Message(Poco::format("  Write/Read check : %s"
									, std::string(0 == str.compare(ostrBackup.str()) ? "OK":"NG")));
			delete decoder;
		}
	}

private:
	template <int v>
	struct Int2Type
	{
		enum {value = v};
	};

	Tenc* Encoder(Poco::FileOutputStream& ostr, Int2Type<eStandard>)
	{
		return (new Tenc(ostr));
	}
	Tenc* Encoder(Poco::FileOutputStream& ostr, Int2Type<eGZIP>)
	{
		return (new Tenc(ostr, Poco::DeflatingStreamBuf::STREAM_GZIP));
	}
	Tenc* Encoder(Poco::FileOutputStream& ostr, Int2Type<eZLIB>)
	{
		return (new Tenc(ostr, Poco::DeflatingStreamBuf::STREAM_ZLIB));
	}
	Tdec* Decoder(Poco::FileInputStream& istr, Int2Type<eStandard>)
	{
		return (new Tdec(istr));
	}
	Tdec* Decoder(Poco::FileInputStream& istr, Int2Type<eGZIP>)
	{
		return (new Tdec(istr, Poco::InflatingStreamBuf::STREAM_GZIP));
	}
	Tdec* Decoder(Poco::FileInputStream& istr, Int2Type<eZLIB>)
	{
		return (new Tdec(istr, Poco::InflatingStreamBuf::STREAM_ZLIB));
	}
};

//----------------------------------------
//	TestGZIPCompressed
//----------------------------------------
void TestGZIPCompressed(ScopedLogMessage& msg)
{
	TestEncodeDecode<Poco::DeflatingOutputStream, Poco::InflatingInputStream, eGZIP> test;
	test.Test(msg, " --- GZIP compressing file wrie/read ---");
}

//----------------------------------------
//	TestZLIBCompressed
//----------------------------------------
void TestZLIBCompressed(ScopedLogMessage& msg)
{
	TestEncodeDecode<Poco::DeflatingOutputStream, Poco::InflatingInputStream, eZLIB> test;
	test.Test(msg, " --- ZLIB compressing file wrie/read ---");
}

//----------------------------------------
//	TestBase64Encoding
//----------------------------------------
void TestBase64Encoding(ScopedLogMessage& msg)
{
	TestEncodeDecode<Poco::Base64Encoder, Poco::Base64Decoder, eStandard> test;
	test.Test(msg, " --- Base64 encoding file wrie/read ---");
}

//----------------------------------------
//	TestHexBinaryEncoding
//----------------------------------------
void TestHexBinaryEncoding(ScopedLogMessage& msg)
{
	TestEncodeDecode<Poco::HexBinaryEncoder, Poco::HexBinaryDecoder, eStandard> test;
	test.Test(msg, " --- HexBinary encoding file wrie/read ---");
}

//----------------------------------------
//	TestQuotedPrintableEncoding
//----------------------------------------
void TestQuotedPrintableEncoding(ScopedLogMessage& msg)
{
	TestEncodeDecode<Poco::Net::QuotedPrintableEncoder, Poco::Net::QuotedPrintableDecoder, eStandard> test;
	test.Test(msg, " --- QuotedPrintable encoding file wrie/read ---");
}

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

	TestRoutine tests[] = {	TestSimple
						,	TestCountingStream
						,	TestGZIPCompressed
						,	TestZLIBCompressed
						,	TestBase64Encoding
						,	TestHexBinaryEncoding
						,	TestQuotedPrintableEncoding	};

	for(std::size_t i=0; i<sizeof(tests)/sizeof(tests[0]); ++i)
	{
		msg.Message("");
		tests[i](msg);
	}
	msg.Message("");

	return 0;
}