/*****************************************************************************
 * $LastChangedDate: 2011-04-23 21:07:07 -0400 (Sat, 23 Apr 2011) $
 * @file
 * @author  Jim E. Brooks  http://www.palomino3d.org
 * @brief   Math test (deprecated functions).
 *//*
 * LEGAL:   COPYRIGHT (C) 2010 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
 *****************************************************************************/

#define TESTING 1
#include "test.hh"
#include "base/module.hh"
using namespace base;
#include "math/module.hh"
#include "math/funcs_deprecated.hh"
using namespace math;

/*****************************************************************************
 * Test FP_EQ(a,b,fractionalDigits).
 *
 * This was deprecated because increasing fractionalDigits beyond 3
 * had a tendency to miscompare numbers that were nearly equal.
 * So the faster FP_EQ() variant is better than this
 * if this has to be restricted to fractionalDigits <= 3 
 *****************************************************************************/
bool
TestFP_EQ_fractionalDigits( void )
{
    CDEBUG << "============================================\n";
    CDEBUG << "Testing FP_EQ(a,b,fractionalDigits). \n";
    CDEBUG << "============================================\n";

 {
    // fp literals:
    if ( not FP_EQ( 100.0f, 100.001f, 2 ) ) return Fail();  // near misses
    if ( not FP_EQ( 100.0f, 100.0001f, 3 ) ) return Fail();
    if ( not FP_EQ( 100.0f, 100.00001f, 4 ) ) return Fail();

    // fpx literals:
    if ( not FP_EQ( 100.0, 100.001, 2 ) ) return Fail();  // near misses
    if ( not FP_EQ( 100.0, 100.0001, 3 ) ) return Fail();
    if ( not FP_EQ( 100.0, 100.00001, 4 ) ) return Fail();
 }

 {
    fp a = 1.2345f;
    fp b = 1.2345f;
    if ( ! FP_EQ(a,b,4) ) return Fail();
    if ( ! FP_EQ(a,b,0) ) return Fail();
    if ( ! FP_EQ(a,b,1) ) return Fail();
    if ( ! FP_EQ(a,b,6) ) return Fail();
 }

 {
    fpx a = 1.2345;
    fpx b = 1.2345;
    if ( ! FP_EQ(a,b,4) ) return Fail();
    if ( ! FP_EQ(a,b,0) ) return Fail();
    if ( ! FP_EQ(a,b,1) ) return Fail();
    if ( ! FP_EQ(a,b,6) ) return Fail();
 }

 {
    fp a = 1.22f;
    fp b = 1.23f;
    if ( ! FP_EQ(a,b,0) ) return Fail();
    if ( ! FP_EQ(a,b,1) ) return Fail();
    if (   FP_EQ(a,b,3) ) return Fail();  // differs
 }

 {
    fp a = 1.22f;
    fp b = 2.22f;
    if ( FP_EQ(a,b,0) ) return Fail();
    if ( FP_EQ(a,b,1) ) return Fail();
    if ( FP_EQ(a,b,2) ) return Fail();
 }

 {
    fp a = -3.21f;
    fp b =  3.21f;
    if ( FP_EQ(a,b,0) ) return Fail();
    if ( FP_EQ(a,b,1) ) return Fail();
    if ( FP_EQ(a,b,2) ) return Fail();
 }

 {
    fp a = 0.999999999f;  // near limit of fp precision
    fp b = 1.000000000f;
    if ( ! FP_EQ(a,b,0) ) return Fail();
    if ( ! FP_EQ(a,b,1) ) return Fail();
    if ( ! FP_EQ(a,b,2) ) return Fail();
    if ( ! FP_EQ(a,b,5) ) return Fail();
 }

 {
    fpx a = 0.999999999;
    fpx b = 1.000000000;
    if ( ! FP_EQ(a,b,0) ) return Fail();
    if ( ! FP_EQ(a,b,1) ) return Fail();
    if ( ! FP_EQ(a,b,2) ) return Fail();
    if ( ! FP_EQ(a,b,5) ) return Fail();
 }

 {
    fp a =  99999.9999f; // near limit of fp precision
    fp b = 100000.0000f;
    if ( ! FP_EQ(a,b,0) ) return Fail();
    if ( ! FP_EQ(a,b,1) ) return Fail();
    if ( ! FP_EQ(a,b,2) ) return Fail();
    if ( ! FP_EQ(a,b,5) ) return Fail();  // hits limit of fp precision
 }

 {
    fpx a =  99999.9999;
    fpx b = 100000.0000;
    if ( ! FP_EQ(a,b,0) ) return Fail();
    if ( ! FP_EQ(a,b,1) ) return Fail();
    if ( ! FP_EQ(a,b,2) ) return Fail();
  //if ( ! FP_EQ(a,b,5) ) return Fail();  // test case fails, diff appears with fpx, (why the func was deprecated)
 }

 {
    fpx a = -0.999999999;  // negative
    fpx b = 1.000000000;
    if ( FP_EQ(a,b,0) ) return Fail();
    if ( FP_EQ(a,b,1) ) return Fail();
    if ( FP_EQ(a,b,2) ) return Fail();
    if ( FP_EQ(a,b,5) ) return Fail();
 }

 {
    fpx a =   999.999;
    fpx b = -1000.000;  // negative
    if ( FP_EQ(a,b,0) ) return Fail();
    if ( FP_EQ(a,b,1) ) return Fail();
    if ( FP_EQ(a,b,2) ) return Fail();
    if ( FP_EQ(a,b,5) ) return Fail();
 }

    // Test FP_EQ(a,b,fractionalDigits) more thoroughly.
 {
    if ( not FP_EQ( 1.2345f, 1.2349f, 3 ) ) return Fail();
    if (     FP_EQ( 1.2345f, 1.2349f, 4 ) ) return Fail();
    if ( not FP_EQ( 900.123456789, 900.123456789, 9 ) ) return Fail();
    if (     FP_EQ( 900.1234567891111, 900.1234567892222, 10 ) ) return Fail();
    if (     FP_EQ( -900.123456789, 900.123456789, 3 ) ) return Fail();
    if ( not FP_EQ( 0.123456789, 0.123456789, 3 ) ) return Fail();
    if ( not FP_EQ( 0.123456789, 0.123456789, 9 ) ) return Fail();
    if ( not FP_EQ( 123456789.0, 123456789.0, 9 ) ) return Fail();
 }

    return true;
}

/*****************************************************************************
 * main.
 *****************************************************************************/
int main( int argc, char** argv )
{
    RUN_TEST( "math", TEST_ERROR_MODE_ABORT,
             TestFP_EQ_fractionalDigits() )
}
