#! /bin/sh
# This script tests: pamflip
# Also requires: pbmmake pbmnoise

tmpdir=${tmpdir:-/tmp}
dot_pbm=${tmpdir}/dot.pbm
noise_pbm=${tmpdir}/noise.pbm

# define functions
cmptarget () { cmp -s - $1 && echo "match" || echo "no match";  }
cmptargetN () { cmp -s - $2 && echo $1 ": match" || echo $1 ": no match";  }

echo "Test 1.  Should print match twenty-five times"

pamflip -null maze.pbm | cmptarget maze.pbm

for flip in -null -lr -tb -r180 -xy -reflect
  do
  pamflip ${flip} maze.pbm | pamflip ${flip} | cmptarget maze.pbm    
  pamflip ${flip} maze.pbm | pamflip ${flip} -inverse | cmptarget maze.pbm    
  done

for flip in -r90 -r270
  do
  pamflip ${flip} maze.pbm | pamflip ${flip} -inverse | cmptarget maze.pbm
  done

pamflip -cw  maze.pbm | pamflip -ccw | cmptarget maze.pbm
pamflip -ccw maze.pbm | pamflip -cw  | cmptarget maze.pbm

pamflip -r90 maze.pbm | pamflip -r90 | pamflip -r90 | pamflip -r90 | \
    cmptarget maze.pbm
pamflip -r270 maze.pbm | pamflip -r270 | pamflip -r270 | pamflip -r270 | \
    cmptarget maze.pbm

# the next six take complicated paths
pamflip -cw maze.pbm | pamflip -tb | pamflip -ccw | pamflip -lr | cmptarget maze.pbm
pamflip -lr maze.pbm | pamflip -cw | pamflip -tb | pamflip -ccw | cmptarget maze.pbm
pamflip -tb maze.pbm | pamflip -xy | pamflip -lr | pamflip -xy | cmptarget maze.pbm
pamflip -xy maze.pbm | pamflip -lr | pamflip -xy | pamflip -tb | cmptarget maze.pbm
pamflip -tb maze.pbm | pamflip -reflect | \
  pamflip -lr | pamflip -reflect | cmptarget maze.pbm
pamflip -tb maze.pbm | pamflip -lr | pamflip -r180 | cmptarget maze.pbm


echo "Test 2.  Should print match seventeen times"

pbmmake -b 1 1 > ${dot_pbm}
pamflip -null ${dot_pbm} | cmp -s - ${dot_pbm} && echo "match" || echo "no match"

# the next seven aren't round-trip
for flip in -lr -tb -r90 -r180 -r270 -xy -reflect
  do
  pamflip ${flip} ${dot_pbm} | cmptarget ${dot_pbm}    
  done

for flip in -null -lr -tb -r180 -xy -reflect
  do
  pamflip ${flip} ${dot_pbm} | pamflip ${flip} | cmptarget ${dot_pbm}    
  done

pamflip -ccw ${dot_pbm} | pamflip -cw | cmptarget ${dot_pbm}
pamflip -r90 ${dot_pbm} | pamflip -r90 | pamflip -r90 | pamflip -r90 | \
    cmptarget ${dot_pbm}
pamflip -r270 ${dot_pbm} | pamflip -r270 | pamflip -r270 | pamflip -r270 | \
    cmptarget ${dot_pbm}
     
rm ${dot_pbm}

echo "Test 3.  Should print N : match thirty-two times"

for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 \
         20 21 22 23 24 25 26 27 28 29 30 31 32 
  do
  pbmnoise -randomseed=${i} -pack ${i} 3 > ${noise_pbm} && \
    pamflip -lr ${noise_pbm} | pamflip -lr | cmptargetN $i ${noise_pbm}
  rm ${noise_pbm}
  done

echo "Test 4.  Should print N : match thirty-two times"

for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 \
         20 21 22 23 24 25 26 27 28 29 30 31 32 
  do
  pbmnoise -randomseed=${i} -pack 7 ${i} > ${noise_pbm} && \
    pamflip -tb ${noise_pbm} | pamflip -tb | cmptargetN $i ${noise_pbm}
  rm ${noise_pbm}
  done


echo "Test 5.  Should print N : match thirty-two times"

for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 \
         20 21 22 23 24 25 26 27 28 29 30 31 32 
  do
  pbmnoise -randomseed=${i} -pack ${i} ${i} > ${noise_pbm} && \
    pamflip -xy ${noise_pbm} | pamflip -xy | cmptargetN $i ${noise_pbm}
  rm ${noise_pbm}
  done
