/*****************************************************************************
 * $Id: file.cc,v 1.3 2006/07/02 17:05:24 devel Exp $
 * @file
 * @author	Jim E. Brooks  http://www.jimbrooks.org
 * @brief	Test of file functions: ReadFile(), FileSize(), RemoveFile(), etc.
 *//*
 * 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 <sstream>
#include "test.hh"
#include "gfx_file.hh"

/*****************************************************************************
 * Test.
 *****************************************************************************/
bool
Test( void )
{
	// Create dummy file.
	string fname = "temp.dat";
	fstream fs;
	fs.open( fname.c_str(), ios::out|ios::binary|ios::trunc );
	if ( ! fs.good() || ! fs.is_open() ) return false;

	// Write dummy data to string stream.
	ostringstream os;
	for ( int i = 0; i < 100; ++i ) os << "abc123\n";

	// Write to dummy file.
	//fs << os;
	fs << os.str();
	if ( ! fs.good() || ! fs.is_open() ) return false;

	// Close dummy file.
	fs.flush();
	fs.close();

	// Read the file.
	string ibuf;
	if ( ! ReadFile( fname, ibuf ) ) return false;
	if ( ibuf != os.str() ) return false;
	if ( ibuf.size() == 0 || os.str().size() == 0 ) return false;

	// Now try error cases (missing file).
	if ( ReadFile( "MISSING_FILE", ibuf ) ) return false;
	// Skip FileSize(FILE*) as fopen("MISSING_FILE") would return NULL (core dump).

	// Cleanup.
	if ( ! RemoveFile( fname ) ) return false;

	return true;
}

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