#! /bin/sh
# This script tests: pnmquant
# Also requires: ppmhist

echo "Test 1"

# Count colors in output image.

pnmquant 256 testimg.ppm | ppmhist | \
  awk 'NR==1 {print ($2 <= 256) , ($2 > 256 * 0.95)}'

pnmquant -center 256 testimg.ppm | ppmhist | \
  awk 'NR==1 {print ($2 <= 256) , ($2 > 256 * 0.95)}'

pnmquant -spreadbrightness 256 testimg.ppm | ppmhist | \
  awk 'NR==1 {print ($2 <= 256) , ($2 > 256 * 0.95)}'

pnmquant -spreadluminosity 128 testimg.ppm | ppmhist | \
  awk 'NR==1 {print ($2 <= 128) , ($2 > 128 * 0.95)}'

pnmquant -floyd -randomseed=0 128 testimg.ppm | ppmhist | \
  awk 'NR==1 {print ($2 <= 128) , ($2 > 128 * 0.95)}'

pnmquant 64 -meanpixel testimg.ppm | ppmhist | \
  awk 'NR==1 {print ($2 <= 64) , ($2 > 64 * 0.9)}'

pnmquant 16 -meancolor testimg.ppm | ppmhist | \
  awk 'NR==1 {print ($2 <= 16) , ($2 > 16 * 0.9)}'


pnmquant 2 testgrid.pbm | ppmhist | \
  awk 'NR==1 {print ($2 <= 2) , ($2 == 2) }'

echo "Test Invalid"

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

tmpdir=${tmpdir:-/tmp}
test_out=${tmpdir}/test_out

pnmquant 0 testimg.ppm   > ${test_out} || \
  printf "Expected failure 1 "
  test -s ${test_out} && echo "unexpected output" || echo "(no output)"
  rm -f ${test_out}

pnmquant testimg.ppm  > ${test_out} || \
  printf "Expected failure 2 "
  test -s ${test_out} && echo "unexpected output" || echo "(no output)"
  rm -f ${test_out}

pnmquant 10.5 testimg.ppm > ${test_out} || \
  printf "Expected failure 3 "
  test -s ${test_out} && echo "unexpected output" || echo "(no output)"
  rm -f ${test_out}

pnmquant -center -meancolor 16 testimg.ppm > ${test_out} || \
  printf "Expected failure 4 "
  test -s ${test_out} && echo "unexpected output" || echo "(no output)"
  rm -f ${test_out}

pnmquant -center -meanpixel 16 testimg.ppm    > ${test_out} || \
  printf "Expected failure 5 "
  test -s ${test_out} && echo "unexpected output" || echo "(no output)"
  rm -f ${test_out}

pnmquant -meancolor -meanpixel 16 testimg.ppm > ${test_out} || \
  printf "Expected failure 6 "
  test -s ${test_out} && echo "unexpected output" || echo "(no output)"
  rm -f ${test_out}

pnmquant -spreadbrightness -spreadluminosity 16 testimg.ppm > ${test_out} || \
  printf "Expected failure 7 "
  test -s ${test_out} && echo "unexpected output" || echo "(no output)"
  rm -f ${test_out}

pnmquant -randomseed 1 -norandom 10 testimg.ppm > ${test_out} || \
  printf "Expected failure 8 "
  test -s ${test_out} && echo "unexpected output" || echo "(no output)"
  rm -f ${test_out}
