//------------------------------------------------------------------------------
// 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 "Animation/RotationInterpolator/QuaternionLinearInterpolatorTest.h"
#include "Animation/RotationInterpolator/QuaternionLinearInterpolator.h"

//------------------------------------------------------------------------------
// テストスィートの取得
Test* QuaternionLinearInterpolatorTest::suite(){
	TestSuite* suite = new TestSuite("QuaternionLinearInterpolatorTest");
	suite->addTest(
		new TestCaller(QuaternionLinearInterpolatorTest, testConstructor));
	suite->addTest(
		new TestCaller(QuaternionLinearInterpolatorTest, testSetterGetter));
	suite->addTest(
		new TestCaller(QuaternionLinearInterpolatorTest, testInterpolate));
	return suite;
}
//------------------------------------------------------------------------------
// コンストラクタテスト
void QuaternionLinearInterpolatorTest::testConstructor(){
	TestEquals(16, (int)sizeof(QuaternionLinearInterpolator));
	QuaternionLinearInterpolator interpolator;
	TestAssert(interpolator.isQuaternionLinearInterpolator());
	TestAssert(interpolator.castQuaternionLinearInterpolator() != NULL);
	// データ構築
	int keyCount = 5;
	interpolator.setKeyCount(5);
	for(int i = 0; i < keyCount; i++){
		Quaternion value;
		value.setRotationAxis(Vector3::unitX, (float)i);
		float time = (float)i * 10.f;
		interpolator.setKey(i, time, value);
	}

	// コピーコンストラクタ
	QuaternionLinearInterpolator copy0(interpolator);
	TestAssert(interpolator.equals(copy0));

	// 代入演算子
	interpolator = interpolator;
	QuaternionLinearInterpolator copy1;
	copy1 = interpolator;
	TestAssert(interpolator.equals(copy1));

	// 複製
	QuaternionLinearInterpolator* duplicate =
		interpolator.duplicate()->castQuaternionLinearInterpolator();
	TestAssert(interpolator.equals(*duplicate));
	delete duplicate;

	// 比較
	QuaternionLinearInterpolator equal;
	TestAssert(!interpolator.equals(equal));
	equal.setKeyCount(5);
	for(int i = 0; i < keyCount; i++){
		TestAssert(!interpolator.equals(equal));
		float floatValue = (float)i;
		Quaternion value;
		value.setRotationAxis(Vector3::unitX, (float)i);
		float time = (float)i * 10.f;
		equal.setKey(i, time, value);
	}
	TestAssert(interpolator.equals(equal));

}
//------------------------------------------------------------------------------
// 設定、取得テスト
void QuaternionLinearInterpolatorTest::testSetterGetter(){
	QuaternionLinearInterpolator interpolator;
	int keyCount = 5;
	TestEquals(0, interpolator.getKeyCount());
	interpolator.setKeyCount(5);
	TestEquals(keyCount, interpolator.getKeyCount());
	for(int i = 0; i < keyCount; i++){
		Quaternion value;
		value.setRotationAxis(Vector3::unitX, (float)i);
		float time = (float)i * 10.f;
		interpolator.setKey(i, time, value);
		TestAssert(value == interpolator.getValue(i));
		TestEquals(time, interpolator.getTime(i));
	}
}
//------------------------------------------------------------------------------
// 補間テスト
void QuaternionLinearInterpolatorTest::testInterpolate(){
	QuaternionLinearInterpolator interpolator;
	// データ構築
	int keyCount = 6;
	interpolator.setKeyCount(keyCount);
	for(int i = 0; i < keyCount; i++){
		Quaternion value;
		value.setRotationAxis(Vector3::unitX, (float)i);
		float time = (float)i * 10.f;
		interpolator.setKey(i, time, value);
	}

	// 補間
	Quaternion quaternion;
	quaternion.setRotationAxis(Vector3::unitX, 0.f);

	TestAssert(interpolator.quaternionInterpolate(-5.f) == quaternion);
	TestAssert(interpolator.quaternionInterpolate(0.f) == quaternion);
	for(int i = 0; i < 50; i++){
		float time = (float)i;
		Quaternion value;
		value.setRotationAxis(Vector3::unitX, (float)i * 0.1f);
		TestAssert(value.epsilonEquals(
			interpolator.quaternionInterpolate(time), Math::epsilon));
	}
	quaternion.setRotationAxis(Vector3::unitX, 5.f);
	TestAssert(interpolator.quaternionInterpolate(50.f) == quaternion);
	TestAssert(interpolator.quaternionInterpolate(100.f) == quaternion);
}
//------------------------------------------------------------------------------
