//------------------------------------------------------------------------------
// 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/TextIOTest.h"
#include <Core/InputOutput/TextFileWriter.h>
#include <Core/InputOutput/TextFileReader.h>
#include <Core/InputOutput/TextMemoryWriter.h>
#include <Core/InputOutput/TextMemoryReader.h>

//------------------------------------------------------------------------------
// テストスィートの取得
Test* TextIOTest::suite(){
	TestSuite* suite = new TestSuite("TextIOTest");
	suite->addTest(new TestCaller(TextIOTest, testFileIO));
	suite->addTest(new TestCaller(TextIOTest, testMemoryIO));
	return suite;
}
//------------------------------------------------------------------------------
// ファイル入出力テスト
void TextIOTest::testFileIO(){
	TextFileWriter* writer;
	TextFileReader* reader;
	// 通常読み書き
	writer = new TextFileWriter(
		"Resource/Temporary/TextIOTest_testFileIO_0.txt");
	basicWrite(writer);
	delete writer;
	reader = new TextFileReader(
		"Resource/Temporary/TextIOTest_testFileIO_0.txt");
	basicRead(reader);
	delete reader;

	// CRLF改行コード
	writer = new TextFileWriter(
		"Resource/Temporary/TextIOTest_testFileIO_1.txt");
	crlfWrite(writer);
	delete writer;
	reader = new TextFileReader(
		"Resource/Temporary/TextIOTest_testFileIO_1.txt");
	crlfRead(reader);
	delete reader;

	// CR改行コード
	writer = new TextFileWriter(
		"Resource/Temporary/TextIOTest_testFileIO_2.txt");
	crWrite(writer);
	delete writer;
	reader = new TextFileReader(
		"Resource/Temporary/TextIOTest_testFileIO_2.txt");
	crRead(reader);
	delete reader;

	// LF改行コード
	writer = new TextFileWriter(
		"Resource/Temporary/TextIOTest_testFileIO_3.txt");
	lfWrite(writer);
	delete writer;
	reader = new TextFileReader(
		"Resource/Temporary/TextIOTest_testFileIO_3.txt");
	lfRead(reader);
	delete reader;

	// 改行コード無し
	writer = new TextFileWriter(
		"Resource/Temporary/TextIOTest_testFileIO_4.txt");
	abcWrite(writer);
	delete writer;
	reader = new TextFileReader(
		"Resource/Temporary/TextIOTest_testFileIO_4.txt");
	abcRead(reader);
	delete reader;

	// 空
	writer = new TextFileWriter(
		"Resource/Temporary/TextIOTest_testFileIO_5.txt");
	emptyWrite(writer);
	delete writer;
	reader = new TextFileReader(
		"Resource/Temporary/TextIOTest_testFileIO_5.txt");
	emptyRead(reader);
	delete reader;

}
//------------------------------------------------------------------------------
// メモリ入出力テスト
void TextIOTest::testMemoryIO(){
	// 通常読み書き
	TextMemoryWriter* writer;
	TextMemoryReader* reader;
	writer = new TextMemoryWriter(1);
	basicWrite(writer);
	reader = new TextMemoryReader(writer->getBuffer(), writer->getSize());
	basicRead(reader);
	delete reader;
	delete writer;

	// CRLF改行コード
	writer = new TextMemoryWriter(1);
	crlfWrite(writer);
	reader = new TextMemoryReader(writer->getBuffer(), writer->getSize());
	crlfRead(reader);
	delete reader;
	delete writer;

	// CR改行コード
	writer = new TextMemoryWriter(1);
	crWrite(writer);
	reader = new TextMemoryReader(writer->getBuffer(), writer->getSize());
	crRead(reader);
	delete reader;
	delete writer;

	// LF改行コード
	writer = new TextMemoryWriter(1);
	lfWrite(writer);
	reader = new TextMemoryReader(writer->getBuffer(), writer->getSize());
	lfRead(reader);
	delete reader;
	delete writer;

	// 改行コード無し
	writer = new TextMemoryWriter(1);
	abcWrite(writer);
	reader = new TextMemoryReader(writer->getBuffer(), writer->getSize());
	abcRead(reader);
	delete reader;
	delete writer;

	// 空
	writer = new TextMemoryWriter(1);
	emptyWrite(writer);
	reader = new TextMemoryReader(writer->getBuffer(), writer->getSize());
	emptyRead(reader);
	delete reader;
	delete writer;

}
//------------------------------------------------------------------------------
// 通常書き込み
void TextIOTest::basicWrite(TextWriter* writer){
	writer->writeText("\nabc\nあいう\n");
	writer->writeText("12345\n", 6);
	writer->writeFormat("%d %.1f %s\n\r", 1 ,2.34f, "4５６");
	writer->flush();
}
//------------------------------------------------------------------------------
// 通常読み込み
void TextIOTest::basicRead(TextReader* reader){
	String string;
	string = reader->readLine();
	TestEquals("", string);
	string = reader->readLine();
	TestEquals("abc", string);
	string = reader->readLine();
	TestEquals("あいう", string);
	string = reader->readLine();
	TestEquals("12345", string);
	string = reader->readLine();
	TestEquals("1 2.3 4５６", string);
	string = reader->readLine();
	TestEquals("", string);
	TestAssert(reader->isEnd());
}
//------------------------------------------------------------------------------
// CRLF書き込み
void TextIOTest::crlfWrite(TextWriter* writer){
	writer->writeText("\r\n");
}
//------------------------------------------------------------------------------
// CRLF読み込み
void TextIOTest::crlfRead(TextReader* reader){
	String string = reader->readLine();
	TestEquals("", string);
	TestAssert(reader->isEnd());
}
//------------------------------------------------------------------------------
// CR書き込み
void TextIOTest::crWrite(TextWriter* writer){
	writer->writeText("\r");
}
//------------------------------------------------------------------------------
// CR読み込み
void TextIOTest::crRead(TextReader* reader){
	String string = reader->readLine();
	TestEquals("", string);
	TestAssert(reader->isEnd());
}
//------------------------------------------------------------------------------
// LF書き込み
void TextIOTest::lfWrite(TextWriter* writer){
	writer->writeText("\n");
}
//------------------------------------------------------------------------------
// LF読み込み
void TextIOTest::lfRead(TextReader* reader){
	String string = reader->readLine();
	TestEquals("", string);
	TestAssert(reader->isEnd());
}
//------------------------------------------------------------------------------
// abc書き込み
void TextIOTest::abcWrite(TextWriter* writer){
	writer->writeText("abcあいう");
}
//------------------------------------------------------------------------------
// abc読み込み
void TextIOTest::abcRead(TextReader* reader){
	String string = reader->readLine();
	TestEquals("abcあいう", string);
	TestAssert(reader->isEnd());
}
//------------------------------------------------------------------------------
// 空書き込み
void TextIOTest::emptyWrite(TextWriter* writer){
}
//------------------------------------------------------------------------------
// 空読み込み
void TextIOTest::emptyRead(TextReader* reader){
	TestAssert(reader->isEnd());
}
//------------------------------------------------------------------------------
