/*
This program is distributed under the terms of the 'MIT license'. The text
of this licence follows...

Copyright (c) 2004-2009 J.D.Medhurst (a.k.a. Tixy)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

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 AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

/**
@file
@brief	Test code for class Fix. Main test function is #TestFix()
@test
*/

#include "common.h"
#include "test/test.h"
#include "../fix.h"

#include <stdio.h>
#include <math.h>
#include <malloc.h>



//
// Individual test functions
//

#define PI (double)3.1415926535897932384626433832795
#define SIN(angle) ((double)65536.0*sin((double)angle*PI*(double)2.0/(double)65536.0))

static void TestSin()
	{
	for(int value=-0x10000; value<=0x10000; value++)
		{
		int n = Fix::Sin(value);
		double ref = SIN(value);
		double error = (double)n-ref;
		TEST(error>=-0.56 && error<=0.56);
		}
	}


#define COS(angle) ((double)65536.0*cos((double)angle*PI*(double)2.0/(double)65536.0))

static void TestCos()
	{
	for(int value=-0x10000; value<=0x10000; value++)
		{
		int n = Fix::Cos(value);
		double ref = COS(value);
		double error = (double)n-ref;
		TEST(error>=-0.56 && error<=0.56);
		}
	}


#define ASIN(value) ((double)65536.0/(PI*(double)2.0)*asin((double)value/(double)65536.0))

static void TestASin()
	{
	int n;
	for(int value=-0x10000; value<=0x10000; value++)
		{
		n = Fix::ASin(value);
		double ref = ASIN(value);
		double error = (double)n-ref;
		TEST(error>=-0.56 && error<=0.56);
		}

	n = Fix::ASin(-0x10001);
	TEST(n==-0x4000);
	n = Fix::ASin(-0x10010);
	TEST(n==-0x4000);
	n = Fix::ASin(0x80000000);
	TEST(n==-0x4000);

	n = Fix::ASin(0x10001);
	TEST(n==0x4000);
	n = Fix::ASin(0x10010);
	TEST(n==0x4000);
	n = Fix::ASin(0x7fffffff);
	TEST(n==0x4000);
	}


#define ACOS(value) ((double)65536.0/(PI*(double)2.0)*acos((double)value/(double)65536.0))

static void TestACos()
	{
	int n;
	for(int value=-0x10000; value<=0x10000; value++)
		{
		n = Fix::ACos(value);
		double error = (double)n-ACOS(value);
		TEST(error>=-0.56 && error<=0.56);
		}

	n = Fix::ACos(-0x10001);
	TEST(n==0x8000);
	n = Fix::ACos(-0x10010);
	TEST(n==0x8000);
	n = Fix::ACos(0x80000000);
	TEST(n==0x8000);

	n = Fix::ACos(0x10001);
	TEST(n==0);
	n = Fix::ACos(0x10010);
	TEST(n==0);
	n = Fix::ACos(0x7fffffff);
	TEST(n==0);
	}


static inline uint32_t Random(uint32_t& seed)
	{
	seed = seed*69069+1;
	return seed;
	}
#define RANDOM Random(seed)


static inline void AddCheck(int a,int b)
	{
	int n = Fix::Add(a,b);
	double ref = ((double)a+(double)b);
	if(ref>=32768.0*65536.0)
		ref = 32768.0*65536.0-1;
	else if(ref<-32768.0*65536.0)
		ref = -32768.0*65536.0;
	double error = (double)n-ref;
	TEST(error>=-0.5 && error<=0.5);
	}


static void TestAdd()
	{
	uint32_t seed = 1;
	int i;
	for(i=0x100000; i>0; i--)
		AddCheck(RANDOM,RANDOM);
	}


static inline void SubCheck(int a,int b)
	{
	int n = Fix::Sub(a,b);
	double ref = ((double)a-(double)b);
	if(ref>=32768.0*65536.0)
		ref = 32768.0*65536.0-1;
	else if(ref<-32768.0*65536.0)
		ref = -32768.0*65536.0;
	double error = (double)n-ref;
	TEST(error>=-0.5 && error<=0.5);
	}


static void TestSub()
	{
	uint32_t seed = 1;
	int i;
	for(i=0x100000; i>0; i--)
		AddCheck(RANDOM,RANDOM);
	}


static inline void MulCheck(int a,int b)
	{
	int n = Fix::Mul(a,b);
	double ref = ((double)a*(double)b/65536.0);
	if(ref>=32768.0*65536.0)
		ref = 32768.0*65536.0-1;
	else if(ref<-32768.0*65536.0)
		ref = -32768.0*65536.0;
	double error = (double)n-ref;
	TEST(error>=-0.5 && error<=0.5);
	}


static void TestMul()
	{
	uint32_t seed = 1;
	int i;
	for(i=0x100000; i>0; i--)
		MulCheck(RANDOM,RANDOM);
	for(i=-0x10000; i<=0x10000; i++)
		{
		for(int n=10; n>0; n--)
			{
			MulCheck(i,RANDOM);
			MulCheck(RANDOM,i);
			}
		}
	for(i=-0x7fff0000; i<=0x7fffffff-0x10000; i+=0x10000)
		{
		for(int n=10; n>0; n--)
			{
			MulCheck(i,RANDOM);
			MulCheck(RANDOM,i);
			}
		}
	for(i=-0x7fff8000; i<=0x7fffffff-0x10000; i+=0x10000)
		{
		for(int n=10; n>0; n--)
			{
			MulCheck(i,RANDOM);
			MulCheck(RANDOM,i);
			}
		}
	for(i=-0x7fff8001; i<=0x7fffffff-0x10000; i+=0x10000)
		{
		for(int n=10; n>0; n--)
			{
			MulCheck(i,RANDOM);
			MulCheck(RANDOM,i);
			}
		}
	for(i=-0x7fff7fff; i<=0x7fffffff-0x10000; i+=0x10000)
		{
		for(int n=10; n>0; n--)
			{
			MulCheck(i,RANDOM);
			MulCheck(RANDOM,i);
			}
		}
	for(i=-0x7fff0001; i<=0x7fffffff-0x10000; i+=0x10000)
		{
		for(int n=10; n>0; n--)
			{
			MulCheck(i,RANDOM);
			MulCheck(RANDOM,i);
			}
		}
	}


static inline void MulNSCheck(int a,int b)
	{
	int n = Fix::MulNS(a,b);
	double ref = ((double)a*(double)b/65536.0);
	if(ref>=32768.0*65536.0)
		return;
	else if(ref<-32768.0*65536.0)
		return;
	double error = (double)n-ref;
	TEST(error>=-0.5 && error<=0.5);
	}


static void TestMulNS()
	{
	uint32_t seed = 1;
	int i;
	for(i=0x100000; i>0; i--)
		MulNSCheck(RANDOM,RANDOM);
	for(i=-0x10000; i<=0x10000; i++)
		{
		for(int n=10; n>0; n--)
			{
			MulNSCheck(i,RANDOM);
			MulNSCheck(RANDOM,i);
			}
		}
	for(i=-0x7fff0000; i<=0x7fffffff-0x10000; i+=0x10000)
		{
		for(int n=10; n>0; n--)
			{
			MulNSCheck(i,RANDOM);
			MulNSCheck(RANDOM,i);
			}
		}
	for(i=-0x7fff8000; i<=0x7fffffff-0x10000; i+=0x10000)
		{
		for(int n=10; n>0; n--)
			{
			MulNSCheck(i,RANDOM);
			MulNSCheck(RANDOM,i);
			}
		}
	for(i=-0x7fff8001; i<=0x7fffffff-0x10000; i+=0x10000)
		{
		for(int n=10; n>0; n--)
			{
			MulNSCheck(i,RANDOM);
			MulNSCheck(RANDOM,i);
			}
		}
	for(i=-0x7fff7fff; i<=0x7fffffff-0x10000; i+=0x10000)
		{
		for(int n=10; n>0; n--)
			{
			MulNSCheck(i,RANDOM);
			MulNSCheck(RANDOM,i);
			}
		}
	for(i=-0x7fff0001; i<=0x7fffffff-0x10000; i+=0x10000)
		{
		for(int n=10; n>0; n--)
			{
			MulNSCheck(i,RANDOM);
			MulNSCheck(RANDOM,i);
			}
		}
	}


static inline void DivCheck(int a,int b)
	{
	int n = Fix::Div(a,b);
	double ref = ((double)a*65536.0/(double)b);
	if(ref>=32768.0*65536.0)
		ref = 32768.0*65536.0-1;
	else if(ref<-32768.0*65536.0)
		ref = -32768.0*65536.0;
	double error = (double)n-ref;
	TEST(error>=-1 && error<=1);
	}


static void TestDiv()
	{
	Fix::Div(0xf1b231d1,0xffff1b24);
	uint32_t seed = 1;
	int i;
	for(i=0x100000; i>0; i--)
		DivCheck(RANDOM,RANDOM);
	for(i=-0x10000; i<=0x10000; i++)
		{
		for(int n=10; n>0; n--)
			{
			DivCheck(i,RANDOM);
			DivCheck(RANDOM,i);
			}
		}
	for(i=(int)-0x80000000; i<=0x7fffffff-0x10000; i+=0x10000)
		{
		for(int n=10; n>0; n--)
			{
			DivCheck(i,RANDOM);
			DivCheck(RANDOM,i);
			}
		}
	for(i=-0x7fff8000; i<=0x7fffffff-0x10000; i+=0x10000)
		{
		for(int n=10; n>0; n--)
			{
			DivCheck(i,RANDOM);
			DivCheck(RANDOM,i);
			}
		}
	for(i=-0x7fff8001; i<=0x7fffffff-0x10000; i+=0x10000)
		{
		for(int n=10; n>0; n--)
			{
			DivCheck(i,RANDOM);
			DivCheck(RANDOM,i);
			}
		}
	for(i=-0x7fff7fff; i<=0x7fffffff-0x10000; i+=0x10000)
		{
		for(int n=10; n>0; n--)
			{
			DivCheck(i,RANDOM);
			DivCheck(RANDOM,i);
			}
		}
	for(i=-0x7fff0001; i<=0x7fffffff-0x10000; i+=0x10000)
		{
		for(int n=10; n>0; n--)
			{
			DivCheck(i,RANDOM);
			DivCheck(RANDOM,i);
			}
		}
	}


static inline void SqrtCheck(unsigned a)
	{
	int n = Fix::Sqrt(a);
	double ref = pow((double)a/(double)65536.0,(double)0.5)*(double)65536.0;
	if(ref>=65536.0*65536.0)
		ref = 65536.0*65536.0-1;
	double error = (double)n-ref;
	TEST(error>=-0.5 && error<=0.5);
	}


static void TestSqrt()
	{
	uint32_t seed = 1;
	int i;
	for(i=0x100000; i>0; i--)
		SqrtCheck(RANDOM);
	}


#define E ((double)2.7182818284590452353602874713527)
#define DOUBLE_LOG2(value) ((double)65536.0*log((double)value/(double)65536.0)/log((double)2.0))

static inline void Log2Check(unsigned a)
	{
	int n = Fix::Log2(a);
	double ref = DOUBLE_LOG2(a);
	if(ref<-32768.0*65536.0)
		ref = -32768.0*65536.0;
	double error = (double)n-ref;
	TEST(error>=-0.55 && error<=0.55);
	}


static void TestLog2()
	{
	uint32_t seed = 1;
	int i;
	for(i=0x100000; i>0; i--)
		Log2Check(RANDOM);
	for(i=0; i<32; i++)
		Log2Check(1<<i);
	Log2Check(0);
	}



#define EXP2(value) ((double)65536.0*pow((double)2,(double)value/(double)65536.0))

static void TestExp2()
	{
	for(int a=-17*0x10000; a<=16*0x10000; a++)
		{
		unsigned n = Fix::Exp2(a);
		double ref = EXP2(a);
		if(ref>=65536.0*65536.0)
			ref = 65536.0*65536.0-1;
		double error = (double)n-ref;
		TEST(error>=-0.75 && error<=0.75);
		}
	}


#define TAN(angle) ((double)65536.0*tan((double)angle*PI*(double)2.0/(double)65536.0))

static void TestTan()
	{
	for(int value=-0x10000; value<=0x10000; value++)
		{
		int n = Fix::Tan(value);
		double ref=TAN(value);
		if(ref>=(double)32768*(double)65536)
			ref = (double)32768*(double)65536-(double)1;
		if(ref<(double)-32768*(double)65536)
			ref = (double)-32768*(double)65536;
		double error = (double)n-ref;
		TEST(error>=-0.66 && error<=0.66);
		}
	}


#define ATAN(value) ((double)65536.0/PI/(double)2.0*atan((double)value/(double)65536.0))

static void TestATan()
	{
	for(int value=-0x10000; value<=0x10000*0x10; value++)
		{
		int n = Fix::ATan(value);
		TEST(n!=0x4000);
		double ref = ATAN(value);
		double error = (double)n-ref;
		TEST(error>=-0.59 && error<=0.59);
		}
	}


static void TestRandom()
	{
	uint32_t seed = 0;
	fix first = Fix::Random(seed);
	fix n;
	uint32_t i=0xffffffffu;
	do
		{
		TEST((n=Fix::Random(seed))!=first);
		}
	while(--i);
	TEST((n=Fix::Random(seed))==first);

	for(i=0; i<32; i++)
		{
		ufix range = 1<<i;
		for(int j=1<<20; j>=0; j--)
			{
			TEST(Fix::Random(seed,range)<range);
			}
		}
	}



//
// Top level test function
//

void TestFix()
	{
	TestAdd();
	TestSub();
	TestMul();
	TestMulNS();
	TestDiv();
	TestSqrt();
	TestLog2();
	TestExp2();
	TestSin();
	TestCos();
	TestTan();
	TestASin();
	TestACos();
	TestATan();
	TestRandom();
	}
