#!/usr/bin/env bash

# Makes Frobby report all error messages and other messages and
# compare this to reference outputs. Parameters to this script should
# only be parameters of ../testHelper, e.g. _generate or _valgrind,
# not options for Frobby.

t="./runtest"
noInput="There is no input for this computation."

if [ "$1" = "_full" ]; then
  shift;
fi

#######################################
# The help action
helpTopics="help hilbert irrdecom alexdual assoprimes transform ptransform io
            intersection genideal  frobgrob frobdyn genfrob analyze latformat
            optimize maxstandard dimension";
for topic in $helpTopics;
do
  $t help "$noInput" help-$topic _expectExitCode 0 $* $topic
  if [ $? != 0 ]; then exit 1; fi
done

$t help "$noInput" help-unknownTopic $* unknownTopic
if [ $? != 0 ]; then exit 1; fi

$t help "$noInput" help-noparam $* _expectExitCode 0 # no help topic
if [ $? != 0 ]; then exit 1; fi

$t help "$noInput" help-twoparams $* help topic2 $* # two topics
if [ $? != 0 ]; then exit 1; fi

#######################################
# Processing of options

$t alexdual "$noInput" option-nodash $* time $* # no dash (-) on option
if [ $? != 0 ]; then exit 1; fi

$t alexdual "$noInput" option-unknown $* -unknownOption
if [ $? != 0 ]; then exit 1; fi

$t hilb "$noInput" option-ambiguous $* -s #ambigous prefix
if [ $? != 0 ]; then exit 1; fi

$t alexdual "$noInput" option-unknownSplit $* -split unknownSplit
if [ $? != 0 ]; then exit 1; fi

$t alexdual "$noInput" option-ambiguousSplitSplit $* -split m
if [ $? != 0 ]; then exit 1; fi

$t alexdual "$noInput" option-frobNotHere $* -split frob
if [ $? != 0 ]; then exit 1; fi

$t hilbert "$noInput" option-labelNotHere $* -split varlabel -algorithm slice
if [ $? != 0 ]; then exit 1; fi

$t alexdual "$noInput" option-notonoff $* -time not_on_or_off
if [ $? != 0 ]; then exit 1; fi

$t alexdual "$noInput" option-unknownInput $* -iformat unknownFormat
if [ $? != 0 ]; then exit 1; fi

$t alexdual "$noInput" option-unknownOutput $* -oformat unknownFormat
if [ $? != 0 ]; then exit 1; fi

$t alexdual "$noInput" option-tooManyParams $* -oformat o1 o2
if [ $? != 0 ]; then exit 1; fi

$t hilbert "$noInput" option-noPolyOutput $* -oformat monos -iformat null
if [ $? != 0 ]; then exit 1; fi

$t ptransform "$noInput" option-noPolyInput $* -oformat null -iformat monos
if [ $? != 0 ]; then exit 1; fi

$t genideal "$noInput" option-notInteger $* -varCount notInteger
if [ $? != 0 ]; then exit 1; fi

# This does not actually get interpreted as a negative integer, since
# the minus in front makes it look like another option entirely.
$t genideal "$noInput" option-negative $* -varCount -1
if [ $? != 0 ]; then exit 1; fi

$t genideal "$noInput" option-tooBig $* -varCount 4294967296
if [ $? != 0 ]; then exit 1; fi

$t genideal "$noInput" option-unknownIdealType $* -type unknownType
if [ $? != 0 ]; then exit 1; fi

$t latformat "$noInput" latformat-unknownInput $* -iformat unknownFormat
if [ $? != 0 ]; then exit 1; fi

$t latformat "$noInput" latformat-unknownOutput $* -oformat unknownFormat
if [ $? != 0 ]; then exit 1; fi

$t hilbert "$noInput" option-unknownBigattiSplit $* -algorithm bigatti -split unknownSplit
if [ $? != 0 ]; then exit 1; fi

$t hilbert "$noInput" option-unknownEnumerationOrder $* -algorithm deform -enum unknownOrder
if [ $? != 0 ]; then exit 1; fi

#######################################
# action prefixes
$t unknownAction "$noInput" action-unknown $* # Unknown action
if [ $? != 0 ]; then exit 1; fi

$t a "$noInput" action-ambigous $* # Ambigous prefix
if [ $? != 0 ]; then exit 1; fi


#######################################
# Syntax errors

$t transform "Use R::=Q[x[1..1]];Names:=[\" x\"];I:=Ideal();" \
  cocoa4-varSpace1 $* -iformat cocoa4
if [ $? != 0 ]; then exit 1; fi

$t transform "Use R::=Q[x[1..1]];Names:=[\"x \"];I:=Ideal();" \
  cocoa4-varSpace2 $* -iformat cocoa4
if [ $? != 0 ]; then exit 1; fi

$t transform "Use R::=Q[x[1..2]];Names:=[\"x\",\"x\"];I:=Ideal();" \
  cocoa4-doubleVar $* -iformat cocoa4
if [ $? != 0 ]; then exit 1; fi

$t transform "Use R::=Q[x[1..1]];Names:=[\"x\"];I:=Ideal(x[2]);" \
  cocoa4-unknownVar $* -iformat cocoa4
if [ $? != 0 ]; then exit 1; fi

$t transform "Use R::=Q[x[1..1]];Names:=[\"x\"];I:=Ideal(x[1]x[1]);" \
  cocoa4-doubleVar2 $* -iformat cocoa4
if [ $? != 0 ]; then exit 1; fi

$t transform "Use R::=Q[x[1..1]];Names:=[\"x\"];I:=Ideal(x[1]^-1);" \
  cocoa4-negativeExponent $* -iformat cocoa4
if [ $? != 0 ]; then exit 1; fi

$t ptransform "Use R::=Q[x[1..1]];Names:=[\"x\"];p:=++1;" \
  cocoa4-tooManySigns $* -iformat cocoa4
if [ $? != 0 ]; then exit 1; fi

$t transform "ring R=0,(x1,x1)" \
  singular-doubleVar $* -iformat singular
if [ $? != 0 ]; then exit 1; fi

$t transform "ring R=0,(a,b),lp;int noVars=1;" \
  singular-hasNoVars $* -iformat singular
if [ $? != 0 ]; then exit 1; fi

$t transform "ring R=0,(a),lp;int noVars=1;" \
  singular-hasNoVars2 $* -iformat singular
if [ $? != 0 ]; then exit 1; fi

$t transform "ring R=0,(a,b),lp;int noVars=2;" \
  singular-invalidNoVars $* -iformat singular
if [ $? != 0 ]; then exit 1; fi

$t latformat "[[1][]]" \
  latformat-unevenRows $* -iformat fplll

$t transform "R=QQ[a,a];" \
  macaulay2-doubleVar $* -iformat m2

$t transform "vars a,a;" \
  monos-doubleVar $* -iformat monos

$t transform "(monomial-ideal-with-order(lex-order a a))" \
  newmonos-doubleVar $* -iformat newmonos

$t ptransform "0 0" 4ti2-invalidPoly $*
if [ $? != 0 ]; then exit 1; fi

$t frobgrob "0 0" frobgrob-notEnoughColumns $*
if [ $? != 0 ]; then exit 1; fi

$t frobgrob "1 1 1" frobgrob-firstPositive $*
if [ $? != 0 ]; then exit 1; fi

$t frobgrob "1 1 -1" frobgrob-noFrob $*
if [ $? != 0 ]; then exit 1; fi

$t frobgrob "1 1 -1 2 3" frobgrob-frobMismatch $*
if [ $? != 0 ]; then exit 1; fi

$t frobgrob "1 1 -1 1" frobgrob-frobHasOne $*
if [ $? != 0 ]; then exit 1; fi

$t frobgrob "1 1 -1 2 2" frobgrob-frobNotPrime $*
if [ $? != 0 ]; then exit 1; fi

$t alexdual "R=QQ[a];I=monomialIdeal(a^2);a" alexdual-invalidPoint $*
if [ $? != 0 ]; then exit 1; fi

$t transform "R=QQ[a];I=monomialIdeal(a);R=QQ[b];I=monomialIdeal(b);" \
  transform-mismatchedProduct $* -product
if [ $? != 0 ]; then exit 1; fi

$t transform "q" m2-invalidChar1 $* -iformat m2
if [ $? != 0 ]; then exit 1; fi

$t transform "R" m2-invalidChar2 $* -iformat m2
if [ $? != 0 ]; then exit 1; fi

$t transform "R=Qxx x" m2-invalidString1 $* -iformat m2
if [ $? != 0 ]; then exit 1; fi

$t transform "R=" m2-invalidString2 $* -iformat m2
if [ $? != 0 ]; then exit 1; fi

$t hilbert "R=QQ[a];I=monomialIdeal(a);extra" m2-extra $* -iformat m2
if [ $? != 0 ]; then exit 1; fi

$t transform "1 1" 4ti2-noInteger $* -iformat 4ti2
if [ $? != 0 ]; then exit 1; fi

$t hilbert "R=QQ[a];I=monomialIdeal(" m2-noIdentifier $* -iformat m2
if [ $? != 0 ]; then exit 1; fi

$t hilbert "R=QQ[a,b];I=monomialIdeal(ab" m2-unknownVariable $* -iformat m2
if [ $? != 0 ]; then exit 1; fi

$t optimize "R=QQ[a,b];I=monomialIdeal(a);3" opt-incompleteVector $*
