root/trunk/0053_Tuple/TupleTest.cpp

User picture

Author: Setsu

Revision: 348 («Previous)


File Size: 5.1 KB

(July 31, 2010 01:40 UTC) Almost 2 years ago

0053_Tuple added to repository.

 
Show/hide line numbers
//
//  TupleTest.cpp
//
//  Created by Setsu on 7/30/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/Runnable.h>
#include <Poco/Thread.h>
#include <Poco/Tuple.h>
#include <Poco/NamedTuple.h>

#include <string>
#include <set>

#include "ScopedLogMessage.h"

//----------------------------------------
//	TestGetSet
//----------------------------------------
void TestGetSet(ScopedLogMessage& msg)
{
	msg.Message(" --- Test get/set ---");

	typedef Poco::Tuple<short, long, double> MyTupleType;

	MyTupleType aTuple(1, 2, 3.0);
	msg.Message(Poco::format("  aTuple: (%+hd, %+ld, %+4.2f), length=%i"
							,	aTuple.get<0>()
							,	aTuple.get<1>()
							,	aTuple.get<2>()
							,	static_cast<int>(aTuple.length) ));

	aTuple.set<0>(-1 * aTuple.get<0>());
	aTuple.set<1>(-1 * aTuple.get<1>());
	aTuple.set<2>(-1 * aTuple.get<2>());

	msg.Message(Poco::format("  aTuple: (%+hd, %+ld, %+4.2f), length=%i"
							,	aTuple.get<0>()
							,	aTuple.get<1>()
							,	aTuple.get<2>()
							,	static_cast<int>(aTuple.length) ));
}

//----------------------------------------
//	TestOperatorLT
//----------------------------------------
void TestOperatorLT(ScopedLogMessage& msg)
{
	msg.Message(" --- Test operator less than ---");

	typedef Poco::Tuple<std::string, int, long, float> MyTupleType;
	typedef std::set<MyTupleType> MyTupleSet;

	MyTupleType aTuple1("1", 1, 111, 1.11f);
	MyTupleType aTuple2("2", 2, 222, 2.22f);
	MyTupleType aTuple3("3", 3, 333, 3.33f);

	MyTupleSet testSet;
	testSet.insert(aTuple3);
	testSet.insert(aTuple2);
	testSet.insert(aTuple1);

	MyTupleSet::iterator itr = testSet.begin();
	MyTupleSet::iterator itrEnd = testSet.end();
	int count = 0;
	while(itr != itrEnd)
	{
		msg.Message(Poco::format("  testSet #%i: (\"%s\", %i, %ld, %4.2hf), length=%i"
								,	count++
								,	itr->get<0>()
								,	itr->get<1>()
								,	itr->get<2>()
								,	itr->get<3>()
								,	static_cast<int>(itr->length) ));
		++itr;
	}
}

//----------------------------------------
//	TestNamedTuple
//----------------------------------------
void TestNamedTuple(ScopedLogMessage& msg)
{
	msg.Message(" --- Test NamedTuple ---");

	typedef Poco::NamedTuple<std::string, int, long, float> MyTupleType;

	MyTupleType aTuple("1", 1, 111, 1.11f);

	msg.Message("  names for aTuple elements:");
	for(std::size_t i=0; i<aTuple.names()->size(); ++i)
	{
		msg.Message(Poco::format("   %z: %s", i, aTuple.names()->at(i)));
	}
	msg.Message("  get elements by name:");
	msg.Message(Poco::format("   aTuple: (\"%s\", %i, %ld, %4.2hf), length=%i"
							,	aTuple[aTuple.names()->at(0)].convert<std::string>()
							,	aTuple[aTuple.names()->at(1)].convert<int>()
							,	aTuple[aTuple.names()->at(2)].convert<long>()
							,	aTuple[aTuple.names()->at(3)].convert<float>()
							,	static_cast<int>(aTuple.length) ));
}

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

	TestGetSet(msg);
	TestOperatorLT(msg);
	TestNamedTuple(msg);

	return 0;
}