/*****************************************************************************
 * $Id: random.cc,v 1.3 2006/07/02 17:05:24 devel Exp $
 * @file
 * @author	Jim E. Brooks  http://www.jimbrooks.org
 * @brief	Test of random functions.
 *//*
 * LEGAL:	COPYRIGHT (C) 2005 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE (GPL).
 *****************************************************************************/

#define TESTING 1
#include "test.hh"
#include "gfx_types.hh"
#include "gfx_random.hh"

const uint loops = 8*1000*1000;

/*****************************************************************************
 * Test random functions (non-range).
 *****************************************************************************/
bool
TestRandom1( void )
{
	cout << "Testing random_ui(void) and random_f(void)" << endl;

	randomize( random_true() );

	for ( uint i = 0; i < loops; ++i )
	{
		if ( random_ui() > RAND_MAX )
			return false;

		if ( random_f() > 1.0 )
			return false;
	}

	return true;
}

/*****************************************************************************
 * Test random functions (range).
 * The goal is to ensure no random value is returned that is greater than
 * the specified range.  Otherwise, since random values are sometimes
 * used as array indexs, the error could cause out-of-bound bugs
 *****************************************************************************/
bool
TestRandomRange( uint range )
{
	cout << "Testing random_ui(" << range << ") and random_f(" << range << ")" << endl;

	randomize( random_true() );

	for ( uint i = 0; i < loops; ++i )
	{
		if ( random_ui( range ) > range )
			return false;

		if ( random_f( range ) > range )
			return false;
	}

	return true;
}

/*****************************************************************************
 * main.
 *****************************************************************************/
int main( int argc, char** argv )
{
	GFX_INIT( argv[0] );
	int res = 1;
	if (    TestRandom1()
         && TestRandomRange( 1 )
         && TestRandomRange( 2 )
         && TestRandomRange( 30 )
         && TestRandomRange( 1000 ) 
         && TestRandomRange( 0x7fffffff )
         && TestRandomRange( 0xf0000000 ) )
	{
		cerr << "Test PASSED.\n";
		res = 0;
	}
	else
	{
		cerr << "Test FAILED! \n";
		res = 1;
	}
	GFX_CLEANUP();
	exit( res );
}
