#! /bin/sh
# This script tests: pamfunc
# Also requires: pamseq pamtopnm pbmmake

tmpdir=${tmpdir:-/tmp}
input_pgm=${tmpdir}/input.pgm

echo "Test 1"

pamseq 1 15 | pamtopnm -assume > ${input_pgm}
pamtopnm -plain ${input_pgm} | tr '\n' ' '; echo

pamfunc -adder=3 -plain ${input_pgm} | tr '\n' ' '; echo
pamfunc -subtractor=3 -plain ${input_pgm} | tr '\n' ' '; echo
pamfunc -multiplier=3 -plain ${input_pgm} | tr '\n' ' '; echo
pamfunc -divisor=2 -plain ${input_pgm} | tr '\n' ' '; echo
pamfunc -max=6 -plain ${input_pgm} | tr '\n' ' '; echo
pamfunc -min=5 -plain ${input_pgm} | tr '\n' ' '; echo

pamfunc -shiftleft  1  -plain ${input_pgm} | tr '\n' ' '; echo
pamfunc -shiftright 1  -plain ${input_pgm} | tr '\n' ' '; echo
pamfunc -shiftleft  1 ${input_pgm} | pamfunc -shiftright 1 -plain | \
  tr '\n' ' '; echo
pamfunc -shiftright 1 ${input_pgm} | pamfunc -shiftleft 1  -plain | \
  tr '\n' ' '; echo


input_pbm=${tmpdir}/input.pbm

echo "Test 2"

pbmmake -g 8 1 > ${input_pbm}
pamtopnm -plain ${input_pbm} | tr '\n' ' '; echo

pamfunc -adder=1 -plain ${input_pbm} | tr '\n' ' '; echo
pamfunc -subtractor=1 -plain ${input_pbm} | tr '\n' ' '; echo
pamfunc -multiplier=1 -plain ${input_pbm} | tr '\n' ' '; echo
pamfunc -divisor=1 -plain ${input_pbm} | tr '\n' ' '; echo
pamfunc -max=1 -plain ${input_pbm} | tr '\n' ' '; echo
pamfunc -min=1 -plain ${input_pbm} | tr '\n' ' '; echo

pamfunc -shiftleft  1  -plain ${input_pbm} | tr '\n' ' '; echo
pamfunc -shiftright 1  -plain ${input_pbm} | tr '\n' ' '; echo
pamfunc -shiftleft  1 ${input_pbm} | pamfunc -shiftright 1 -plain | \
  tr '\n' ' '; echo
pamfunc -shiftright 1 ${input_pbm} | pamfunc -shiftleft 1  -plain | \
  tr '\n' ' '; echo


echo "Test 3 (-changemaxval)"

pamfunc -divisor 2 -changemaxval ${input_pgm} -plain | \
  tr '\n' ' '; echo
pamfunc -multiplier 0.25 -changemaxval ${input_pgm} -plain | \
  tr '\n' ' '; echo
pamfunc -divisor 2 -changemaxval ${input_pbm} -plain | \
  tr '\n' ' '; echo
pamfunc -multiplier 0.25 -changemaxval ${input_pbm} -plain | \
  tr '\n' ' '; echo

rm ${input_pgm} ${input_pbm}

echo "Test 4: Should print 1926073387 101484 eight times"

cat testimg.ppm | cksum

pamfunc -not testimg.ppm | pamfunc -not | cksum
pamfunc -andmask 0xff testimg.ppm | cksum
pamfunc -ormask  0x00 testimg.ppm | cksum
pamfunc -xormask 0x00 testimg.ppm | cksum
pamfunc -xormask 0xff testimg.ppm | pamfunc -xormask 0xff | cksum
pamfunc -shiftleft  0 testimg.ppm | cksum
pamfunc -shiftright 0 testimg.ppm | cksum


echo "Test 5: Should print 281226646 481 six times"

cat maze.pbm | cksum

pamfunc -andmask  0x1 maze.pbm | cksum
pamfunc -ormask   0x0 maze.pbm | cksum
pamfunc -xormask  0x1 maze.pbm | pamfunc -xormask 0x1 | cksum

pamfunc -shiftleft  0 maze.pbm | cksum
pamfunc -shiftright 0 maze.pbm | cksum

echo "Invalid command-line argument combinations." 1>&2
echo "Error messages should appear below the line." 1>&2
echo "-----------------------------------------------------------" 1>&2

echo "Test Invalid"

test_out=${tmpdir}/test_out

pamfunc -multiplier testimg.ppm > ${test_out} || \
  echo -n "Expected failure 1"
  test -s ${test_out}; echo " "$?
  rm -f ${test_out}

pamfunc -multiplier=-1 testimg.ppm > ${test_out} || \
  echo -n "Expected failure 2"
  test -s ${test_out}; echo " "$?
  rm -f ${test_out}

pamfunc -divisor testimg.ppm > ${test_out} || \
  echo -n "Expected failure 3"
  test -s ${test_out}; echo " "$?
  rm -f ${test_out}

pamfunc -divisor=-20 testimg.ppm > ${test_out} || \
  echo -n "Expected failure 4"
  test -s ${test_out}; echo " "$?
  rm -f ${test_out}

pamfunc -adder testimg.ppm > ${test_out} || \
  echo -n "Expected failure 5"
  test -s ${test_out}; echo " "$?
  rm -f ${test_out}

pamfunc -adder 0.5 testimg.ppm > ${test_out} || \
  echo -n "Expected failure 6"
  test -s ${test_out}; echo " "$?
  rm -f ${test_out}

pamfunc -subtractor testimg.ppm > ${test_out} || \
  echo -n "Expected failure 7"
  test -s ${test_out}; echo " "$?
  rm -f ${test_out}

pamfunc -subtractor 0.1 testimg.ppm > ${test_out} || \
  echo -n "Expected failure 8"
  test -s ${test_out}; echo " "$?
  rm -f ${test_out}

pamfunc -multiplier=1 -divisor=2 testimg.ppm > ${test_out} || \
  echo -n "Expected failure 9"
  test -s ${test_out}; echo " "$?
  rm -f ${test_out}

pamfunc -adder=2 -subtractor=3 testimg.ppm > ${test_out} || \
  echo -n "Expected failure 10"
  test -s ${test_out}; echo " "$?
  rm -f ${test_out}

pamfunc -min testimg.ppm > ${test_out} || \
  echo -n "Expected failure 11"
  test -s ${test_out}; echo " "$?
  rm -f ${test_out}

pamfunc -max testimg.ppm > ${test_out} || \
  echo -n "Expected failure 12"
  test -s ${test_out}; echo " "$?
  rm -f ${test_out}

pamfunc -andmask testimg.ppm > ${test_out} || \
  echo -n "Expected failure 13"
  test -s ${test_out}; echo " "$?
  rm -f ${test_out}

pamfunc -ormask testimg.ppm > ${test_out} || \
  echo -n "Expected failure 14"
  test -s ${test_out}; echo " "$?
  rm -f ${test_out}

pamfunc -xormask testimg.ppm > ${test_out} || \
  echo -n "Expected failure 15"
  test -s ${test_out}; echo " "$?
  rm -f ${test_out}

pamfunc -not 1 testimg.ppm > ${test_out} || \
  echo -n "Expected failure 16"
  test -s ${test_out}; echo " "$?
  rm -f ${test_out}

pamfunc -min=1 -max=2 testimg.ppm > ${test_out} || \
  echo -n "Expected failure 17"
  test -s ${test_out}; echo " "$?
  rm -f ${test_out}

pamfunc -andmask=1 -ormask=0 testimg.ppm > ${test_out} || \
  echo -n "Expected failure 18"
  test -s ${test_out}; echo " "$?
  rm -f ${test_out}

pamfunc -andmask=0xffff testimg.ppm > ${test_out} || \
  echo -n "Expected failure 19"
  test -s ${test_out}; echo " "$?
  rm -f ${test_out}

pamfunc -shiftleft testimg.ppm > ${test_out} || \
  echo -n "Expected failure 20"
  test -s ${test_out}; echo " "$?
  rm -f ${test_out}

pamfunc -shiftright testimg.ppm > ${test_out} || \
  echo -n "Expected failure 21"
  test -s ${test_out}; echo " "$?
  rm -f ${test_out}

pamfunc -changemaxval testimg.ppm > ${test_out} || \
  echo -n "Expected failure 22"
  test -s ${test_out}; echo " "$?
  rm -f ${test_out}

pamfunc -shiftleft=1 -shiftright=1 testimg.ppm > ${test_out} || \
  echo -n "Expected failure 23"
  test -s ${test_out}; echo " "$?
  rm -f ${test_out}

pamfunc -multiplier=0.5 -changemaxval=65535 testimg.ppm > ${test_out} || \
  echo -n "Expected failure 24"
  test -s ${test_out}; echo " "$?
  rm -f ${test_out}
