#!/bin/sh
set -e

EXAMPLES_=(
  string_to_const_string
  string_to_const_string.expanded
  int_to_const_string
  string_set
)
self_="$(readlink -f "$0")"
output_dir_="${self_%/*}/output"

if [[ ! -z $1 ]]
then
  case "$1" in
    save_output)
      cd "${self_%/*/*}"
      mkdir -p "$output_dir_"
      for example_ in "${EXAMPLES_[@]}"
      do
        "tmp/$example_" > "$output_dir_/$example_"
      done
    ;;

    test)
      tmp_="$(mktemp output.XXXXXX)"
      for example_ in "${EXAMPLES_[@]}"
      do
        "./$example_" > "$tmp_"
        if diff "$tmp_" "$output_dir_/$example_" >/dev/null
        then
          # output matches
          continue
        else
          # output differs
          echo "$example_: unexpected output"
          exit 1
        fi
      done
      rm -- "$tmp_"
      echo -n "all tests passed"
    ;;

    *)
      break
    ;;
  esac
fi
