/*
	Тест классов, производных от oess_1::io::ibinbuffer_t,
	oess_1::io::obinbuffer_t.
*/

#include <iostream>
#include <string>
#include <exception>

#include <oess_1/io/h/bstring_buf.hpp>
#include <oess_1/io/h/fixed_mem_buf.hpp>

void
test_obstring_1()
{
	std::cout << "test_obstring_1" << std::endl;

	std::string	to;
	oess_1::io::obstring_t stream( to );

	std::cout << "before: "
		<< "\n\tcapacity: " << stream.capacity()
		<< "\n\tsize: " << stream.size()
		<< "\n\tpos: " << stream.pos()
		<< std::endl;

	const oess_1::uint_t ui_1 = 0;
	const oess_1::ushort_t ush_1 = 0;
	const oess_1::uchar_t uch_1 = 0;
	stream << ui_1 << ush_1 << uch_1;

	std::cout << "after: "
		<< "\n\tcapacity: " << stream.capacity()
		<< "\n\tsize: " << stream.size()
		<< "\n\tpos: " << stream.pos()
		<< std::endl;
}

void
test_obstring_2()
{
	std::cout << "test_obstring_2" << std::endl;

	std::string	to( "Value that already in the  buffer" );
	oess_1::io::obstring_t stream( to );

	std::cout << "before: "
		<< "\n\tcapacity: " << stream.capacity()
		<< "\n\tsize: " << stream.size()
		<< "\n\tpos: " << stream.pos()
		<< std::endl;

	const oess_1::uint_t ui_1 = 0;
	const oess_1::ushort_t ush_1 = 0;
	const oess_1::uchar_t uch_1 = 0;
	stream << ui_1 << ush_1 << uch_1;

	std::cout << "after: "
		<< "\n\tcapacity: " << stream.capacity()
		<< "\n\tsize: " << stream.size()
		<< "\n\tpos: " << stream.pos()
		<< std::endl;
}

void
test_ifixed_mem_buf()
{
	std::cout << "test_ifixed_mem_buf" << std::endl;

	oess_1::char_t buf[ 16 ];
	oess_1::io::ifixed_mem_buf_t stream(
		buf, sizeof( buf ) / sizeof( buf[ 0 ] ) );

	std::cout << "capacity: " << stream.capacity()
		<< ", size: " << stream.size()
		<< ", pos: " << stream.pos() << std::endl;

	oess_1::uint_t ui_1;
	stream >> ui_1;

	std::cout << "capacity: " << stream.capacity()
		<< ", size: " << stream.size()
		<< ", pos: " << stream.pos() << std::endl;
}

void
test_ofixed_mem_buf()
{
	std::cout << "test_ofixed_mem_buf" << std::endl;

	oess_1::char_t buf[ 16 ];
	oess_1::io::ofixed_mem_buf_t stream(
		buf, sizeof( buf ) / sizeof( buf[ 0 ] ) );

	std::cout << "capacity: " << stream.capacity()
		<< ", size: " << stream.size()
		<< ", pos: " << stream.pos() << std::endl;

	std::cout << "pos() and size() must be same" << std::endl;
	oess_1::uint_t ui_1 = 0;
	stream << ui_1;
	std::cout << "capacity: " << stream.capacity()
		<< ", size: " << stream.size()
		<< ", pos: " << stream.pos() << std::endl;

	std::cout << "pos() must be less than size()" << std::endl;
	oess_1::ushort_t ush_1 = 0;
	stream.set_pos( 0 );
	stream << ush_1;
	std::cout << "capacity: " << stream.capacity()
		<< ", size: " << stream.size()
		<< ", pos: " << stream.pos() << std::endl;

	std::cout << "pos() and size() must be same" << std::endl;
	stream << ui_1;
	std::cout << "capacity: " << stream.capacity()
		<< ", size: " << stream.size()
		<< ", pos: " << stream.pos() << std::endl;

	std::cout << "pos() and size() must be same" << std::endl;
	stream.set_pos( 23456 );
	std::cout << "capacity: " << stream.capacity()
		<< ", size: " << stream.size()
		<< ", pos: " << stream.pos() << std::endl;

	std::cout << "pos(), size(), capacity must be same" << std::endl;
	stream.set_pos( 0 );
	stream << ui_1 << ui_1 << ui_1 << ui_1;
	std::cout << "capacity: " << stream.capacity()
		<< ", size: " << stream.size()
		<< ", pos: " << stream.pos() << std::endl;
}

int
main( int, char ** )
{
	try
	{
		test_obstring_1();
		test_obstring_2();
		test_ifixed_mem_buf();
		test_ofixed_mem_buf();
	}
	catch( std::exception & x )
	{
		std::cerr << x.what() << std::endl;
	}

	return 0;
}
