#!/usr/bin/perl

use strict;
use warnings;

# This file can autogenerate tests for canonicalRepresentative. Make a new
# subfolder and move this script in there. You need to set the following
# variables:
my $name = "cube_4";
# The name: There have to be files $name.dat and $name.canonicals in the
# folder.
my $ntests = 500; # The maximum number of tests to generate.
#
# It is assumed that the file $name.canonicals contains lines of the format
# {{triangulation}} something can(gkz): [...] {{canonical triangulation}}
# To achieve this, mptopcom needs to be tweaked according to the following
# diff in APC.diff

my $entire = `cat $name.dat`;
for(my $i=0; $i<10; $i++){
   chomp $entire;
}
print $entire,"\n";

open(my $canonicals, "<$name.canonicals") or die "Could not open results file";
my $i=0;
my $total = `wc -l $name.canonicals`;
while(my $line = <$canonicals>){
   print $line;
   if((rand $total) < $ntests){
      my($input, $gkz, $canonical) = $line =~ m/(\{\{.*\}\}).*can\(gkz\)\:.*(\[.*\]).*(\{\{.*\}\})/;
      print "Input: $input\n";
      print "Canonical gkz: $gkz\n";
      print "Canonical rep: $canonical\n";
      if(!-e "$name.$i.IN"){
         open(my $inputfile, "> $i.IN") or die "Could not open input file.";
         print $inputfile $entire;
         print $inputfile "\n";
         print $inputfile $input;
         close $inputfile;
      }
      if(!-e "$name.$i.OUT"){
         open(my $outputfile, "> $i.OUT") or die "Could not open output file.";
         print $outputfile $gkz;
         print $outputfile "\n";
         print $outputfile $canonical;
         close $outputfile;
      }
      $i++;
   }
   if($i == $ntests){ last; }
}
close $canonicals;
