#!/usr/bin/perl -I../lib
#  Test that our MD5 library works as expected.
#  This will fail if there is no Digest::MD5 installed.  *shrug*

use gnump3d::MD5;

eval {
    require Digest::MD5;
    import Digest::MD5 'md5_hex'
};
if ($@) { 
	print "Skipping test.";
	exit 1;
}


my $DEBUG = 0;
my $count = 0;


while( $count < 20 )
{
	my $text = getRandomString();

	$DEBUG && print "Text is $text\n";

	# Get the hash using Digest::MD5
	my $dmd5 = Digest::MD5->new;
	$dmd5->add($text);
	my $dmd5out = $dmd5->b64digest;

	$DEBUG && print "Digest is $dmd5out\n";

	# Get the hash using gnump3d::MD5
	my $gmd5 = gnump3d::MD5->new;
	$gmd5->add($text);
	my $gmd5out = $gmd5->b64digest;

	$DEBUG && print "GNUMP3d is $gmd5out\n";

    # Compare
	if ( $gmd5out != $dmd5out )
	{
		$DEBUG && print "Failed for $text\n";
		exit 1;
	}

	$count ++;
}


exit 0;



# Generate a `random` string for hashing purposes.
sub getRandomString()
{
	my @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9, qw(! @ $ % ^ & *) );
	return( join("", @chars[ map { rand @chars } ( 1 .. 12 ) ]) );
}
