

package TestGraham;
use DB_File;
use BSD::Resource;


BEGIN {
  setrlimit(RLIMIT_VMEM, 50_000_000, 50_000_000);
    open SAVE_STDOUT, ">&STDOUT" or die $!;
  open STDOUT, ">", "/dev/null" or die $!;
  open STDIN, "<", "/dev/null" or die $!;
  open 'main::DATA', "<", "/dev/null" or die $!;
  @ARGV = ();
  alarm 660;
}

# INIT { exit }

END {
  $| = 1;
  open STDOUT, ">&SAVE_STDOUT" or die $!;
  my $s = time;
  my $N = 1;

  tie %g, DB_File, 'Graham', O_RDONLY or die $!;
  for my $q (sort {$a <=> $b} keys %g) {
    my $x = $g{$q};
    my $a = ::Graham($q);
    if ($a == $x) {
      print "ok $N\n";
    } else {
      print "not ok $N  # G($q) = $x but function said $a\n";
      $BAD++;
    }
    $N++;
  }
  $N--;
  if ($BAD) {
    print "# failed $BAD/$N tests\n";
  } else {
    print "# all $N tests passed\n";
  }
  $s = time - $s;
  print "# $s sec elapsed\n";
}

"Cogito, ergo sum.";

