//------------------------------------------------------------------------------
// Lamp : Open source game middleware
// Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//------------------------------------------------------------------------------

/** @file
 * バイナリ入出力テスト実装
 * @author Junpee
 */

#include "Test/stdafx.h"
#include "Core/InputOutput/BinaryIOTest.h"
#include <Core/InputOutput/BinaryFileWriter.h>
#include <Core/InputOutput/BinaryFileReader.h>
#include <Core/InputOutput/BinaryMemoryWriter.h>
#include <Core/InputOutput/BinaryMemoryReader.h>

//------------------------------------------------------------------------------
// テストスィートの取得
Test* BinaryIOTest::suite(){
	TestSuite* suite = new TestSuite("BinaryIOTest");
	suite->addTest(new TestCaller(BinaryIOTest, testFileBasic));
	suite->addTest(new TestCaller(BinaryIOTest, testFileIO));
	suite->addTest(new TestCaller(BinaryIOTest, testMemoryBasic));
	suite->addTest(new TestCaller(BinaryIOTest, testMemoryIO));
	return suite;
}
//------------------------------------------------------------------------------
// ファイル基本機能テスト
void BinaryIOTest::testFileBasic(){
	// 基本機能書き込み
	BinaryFileWriter* writer = new BinaryFileWriter(
		"Resource/Temporary/BinaryIOTest_testFileIO_0.txt");
	basicWrite(writer);
	delete writer;
	// 基本機能読み込み
	BinaryFileReader* reader = new BinaryFileReader(
		"Resource/Temporary/BinaryIOTest_testFileIO_0.txt");
	basicRead(reader);
	delete reader;
}
//------------------------------------------------------------------------------
// ファイル入出力テスト
void BinaryIOTest::testFileIO(){
	// 通常書き込み
	BinaryFileWriter* writer = new BinaryFileWriter(
		"Resource/Temporary/BinaryIOTest_testFileIO_1.txt");
	binaryWrite(writer);
	delete writer;
	// 通常読み込み
	BinaryFileReader* reader = new BinaryFileReader(
		"Resource/Temporary/BinaryIOTest_testFileIO_1.txt");
	binaryRead(reader);
	delete reader;
}
//------------------------------------------------------------------------------
// メモリ基本機能テスト
void BinaryIOTest::testMemoryBasic(){
	// 基本機能書き込み
	BinaryMemoryWriter* writer =
		new BinaryMemoryWriter(1);
	basicWrite(writer);
	// 基本機能読み込み
	BinaryMemoryReader* reader =
		new BinaryMemoryReader(writer->getBuffer(), writer->getSize());
	basicRead(reader);
	delete reader;
	delete writer;
}
//------------------------------------------------------------------------------
// メモリ入出力テスト
void BinaryIOTest::testMemoryIO(){
	// 通常書き込み
	BinaryMemoryWriter* writer =
		new BinaryMemoryWriter(1);
	binaryWrite(writer);
	// 通常読み込み
	BinaryMemoryReader* reader =
		new BinaryMemoryReader(writer->getBuffer(), writer->getSize());
	binaryRead(reader);
	delete reader;
	delete writer;
}
//------------------------------------------------------------------------------
// 基本書き込み
void BinaryIOTest::basicWrite(BinaryWriter* writer){
	int value = 123456;
	writer->writeBytes(&value, sizeof(int));
	writer->skip(2);
	writer->writeBytes(&value, sizeof(int));
	int result = writer->align(4);
	TestEquals(2, result);
	writer->writeBytes(&value, sizeof(int));
	writer->flush();
	result = writer->getPosition();
	TestEquals(16, result);
	writer->setPosition(4);
	result = writer->getPosition();
	TestEquals(4, result);
}
//------------------------------------------------------------------------------
// 基本読み込み
void BinaryIOTest::basicRead(BinaryReader* reader){
	int value, result;
	TestEquals(16, reader->getSize());
	reader->readBytes(&value, sizeof(int));
	TestEquals(123456, value);
	reader->skip(2);
	reader->readBytes(&value, sizeof(int));
	TestEquals(123456, value);
	result = reader->align(4);
	TestEquals(2, result);
	reader->readBytes(&value, sizeof(int));
	TestEquals(123456, value);
	result = reader->getPosition();
	TestEquals(16, result);
	reader->setPosition(4);
	result = reader->getPosition();
	TestEquals(4, result);
	reader->setPosition(16);
	TestAssert(reader->isEnd());
}
//------------------------------------------------------------------------------
// バイナリ書き込み
void BinaryIOTest::binaryWrite(BinaryWriter* writer){
	int array[] = {1, 2, 3, 4, 5, 6, 7, 8};
	writer->writeChar(127);
	writer->writeChar(-128);
	writer->writeUChar(0);
	writer->writeUChar(255);
	writer->writeShort(32767);
	writer->writeShort(-32768);
	writer->writeUShort(0);
	writer->writeUShort(65535);
	writer->writeInt(2147483647);
	writer->writeInt(-2147483647 - 1);
	writer->writeUInt(0u);
	writer->writeUInt(4294967295u);
	writer->writeFloat(0.123456f);
	writer->writeDouble(0.1234567890123456);
	writer->writeString("abc\nあいう");
	writer->writeArray(array, sizeof(int), 8);
}
//------------------------------------------------------------------------------
// バイナリ読み込み
void BinaryIOTest::binaryRead(BinaryReader* reader){
	int array[] = {0, 0, 0, 0, 0, 0, 0, 0};
	TestEquals(127, reader->readChar());
	TestEquals(-128, reader->readChar());
	TestEquals(0, reader->readUChar());
	TestEquals(255, reader->readUChar());
	TestEquals(32767, reader->readShort());
	TestEquals(-32768, reader->readShort());
	TestEquals(0, reader->readUShort());
	TestEquals(65535, reader->readUShort());
	TestEquals(2147483647, reader->readInt());
	TestEquals(-2147483647 - 1, reader->readInt());
	TestEquals(0u, reader->readUInt());
	TestEquals(4294967295u, reader->readUInt());
	TestEquals(0.123456f, reader->readFloat());
	TestEquals(0.1234567890123456, reader->readDouble());
	TestEquals("abc\nあいう", reader->readString());
	reader->readArray(array, sizeof(int), 8);
	for(int i = 0;i < 8;i++){ TestEquals(i + 1, array[i]); }
	TestAssert(reader->isEnd());
}
//------------------------------------------------------------------------------
