#!/usr/bin/perl -s

## from a exercise-id -->
##   shows the questions from  ../MEX/mex.db
##   accepts an answer
##   validates the answers according to  ../MEX/mex.db
##   saves the user inf  ../MEX/user.db --> histório de cada aluno
##   /MEX/mex.db --> perguntas e rexpostas

our($debug, $id, $passarola_dir,$t);  ## debug, exid, passarola_dir, test number

use strict;
use utf8::all;
use Exercise::Gen::Verify;
use Data::Dumper; $Data::Dumper::Terse=2; $Data::Dumper::Indent=0;

if($t =~ /1/){
  set_exercise_answer(123456,
      { q1a => {cot=>1,v=> "batatas",type=>"s"},
        q1b => {cot=>2,v=>"batatas",type=>'icstr'},
        q0a => {cot=>2,v=>"verdade", type=>'bool'},
        q0b => {cot=>2,v=>"false"  , type=>'bool'},
        q2  => {cot=>3,v=>3.001,},
        q3a => {       v=>[1,2,3]},
        q3b => {cot=>2,v=>[1,[2,3,3],3]},
        q3c => {cot=>2,v=>[1,'rui', 'joão']},
        q3d => {cot=>8,v=>[1,2,3]},
        q3e => {       v=>[1/3,2/3,3]},
        q4  => {cot=>5,v=>[1,2,[5,5]],type=>'memberof'},
        q5  => {cot=>2,func=>q{sub{my ($a,$b)=@_; $b =~ /\b(dog|cat)\b/}}},
        q6  => {cot=>1,v=> undef},                  # undef = any
        q7a => {cot=>2,v=>[[9,3],[16,4],[100,10]],type => "tabfun"},
        q7b => {cot=>2,v=>[[9,3,6],[16,4,12],[100,10,90]],type => "tabfun"},
        q8a => {cot=>5,v=>"sin(x)",type => "r_rfunmax"},
        q8b => {cot=>2,v=>"x^2+-3+x+x",type => "r_rfunmax"},
        q8c => {cot=>2,v=>"(x^2+x*3)/x",type => "r_rfunmax"},
      });
}
if($t =~ /2/){
   _printtab( verify_exercise(
      { version_id => 123456,
        q1a => "batatas",
        q1b => "Batatas ",                   # eq with ic and accents
        q0a => "t",
        q0b => "0",
        q2  => 3.001000001,                  # float comparation
        q3a => [1,2,3],
        q3b => "[1,[2,3,3],3]",              #
        q3c => "[1,'rui', 'joão']",              # list compare
        q3e => [1/3,2/3,3],
        q4  => "[5,5]",                      # [5,5] ∈ correct option
        q5  => "John has a cat and a bird",  # contains word cat or dog
        q5b => "John has a cat and a bird",  # contains word cat or dog
        q6  => 33,                           # undef = 33 OK
        q7a => sub{my $x=shift; $x**2},
        q7b => sub{my ($x,$y)=@_; $x+$y},
        q8a => "sin( x)",
        q8b => "x^2+x*2-3",
        q8c => "x+3",
      }))
}
##TEST 3
if($t =~ /3/){
 _printtab( verify_exercise( { q1a => "batatas",
        q1b => "Batatas ",                   # eq with ic and accents
        q0a => "t",
        q0b => "0",
        q2  => 3.001000001,                  # float comparation
        q3a => [1,2,3],
        q3b => "[1,[2,3,3],3]",              #
        q3c => "[1,rui, joão]",              # list compare
        q3e => [1/3,2/3,3],
        q4  => "[5,5]",                      # [5,5] ∈ correct option
        q5  => "John has a cat and a bird",  # contains word cat or dog
        q5b => "John has a cat and a bird",  # contains word cat or dog
        q6  => 33,                           # undef = 33 OK
        q7a => sub{my $x=shift; $x**2},
        q7b => sub{my ($x,$y)=@_; $x+$y},
        q7c => "batatas",
        q8a => "sin( x)",
        q8b => "x^2+x*2-3",
        q8c => "x+3",
      },
      { q1a => {cot=>1,v=> "batatas",type=>"s"},
        q1b => {cot=>2,v=>"batatas",type=>'icstr'},
        q0a => {cot=>2,v=>"verdade", type=>'bool'},
        q0b => {cot=>2,v=>"false"  , type=>'bool'},
        q2  => {cot=>3,v=>3.001,},
        q3a => {       v=>[1,2,3]},
        q3b => {cot=>2,v=>[1,[2,3,3],3]},
        q3c => {cot=>2,v=>[1,'rui', 'joão']},
        q3d => {cot=>8,v=>[1,2,3]},
        q3e => {       v=>[1/3,2/3,3]},
        q4  => {cot=>5,v=>[1,2,[5,5]],type=>'memberof'},
        q5  => {cot=>2,func=>  sub{my ($a,$b)=@_; $b =~ /\b(dog|cat)\b/} },
        q5b => {cot=>2,func=>q{sub{my ($a,$b)=@_; $b =~ /\b(dog|cat)\b/}}},
        q6  => {cot=>1,v=> undef},                  # undef = any
        q7a => {cot=>2,v=>[[9,3],[16,4],[100,10]],type => "tabfun"},
        q7b => {cot=>2,v=>[[9,3,6],[16,4,12],[100,10,90]],type => "tabfun"},
        q7c => {cot=>2,v=>[[9,3,6],[16,4,12],[100,10,90]],type => "tabfun"},
        q8a => {cot=>5,v=>"sin(x)",type => "r_rfunmax"},
        q8b => {cot=>2,v=>"x^2+-3+x+x",type => "r_rfunmax"},
        q8c => {cot=>2,v=>"(x^2+x*3)/x",type => "r_rfunmax"},
      })) ;
}
##TEST 4
if($t =~ /4/){
   mk_exercise_base(123456);
}
##TEST 5
if($t =~ /5/){
   my $submit = parse_submission("#version_id 123456\n#q8a cos(%pi/2 - x)\n");
   _printtab( $submit);
   _printtab( verify_exercise($submit));
}
##TEST 6
if($t =~ /6/){
   _printtab( verify_exercise("#version_id 123456\n#q8a sin ( x)\n\n#q2 3.001\n"));
}
##TEST 7
if($t =~ /7/){
   my $submit = parse_submission();
   _printtab( verify_exercise($submit));
}


sub _printtab{
 for my $tab(@_){
   if   (ref $tab eq "ARRAY"){ print map {Dumper($_)."\n"} @$tab}
   elsif(ref $tab eq "HASH" ){ print map {Dumper($_,$tab->{$_})."\n"} keys %$tab}
   else { print "$tab\n" }
 }
}

__END__
