test_serial()
{
  TESTNAME=$1
  shift

  [ -d run_test_${TESTNAME} ] && rm -r run_test_${TESTNAME}
  mkdir run_test_${TESTNAME}

  SIONDUMP=../siondump

  (
    cd run_test_${TESTNAME}

    export LSION_DEBUG=test_${TESTNAME}
    export LSION_DEBUG_MASK=1023
    export LSION_DEBUG_RANK1=0
    export LSION_DEBUG_RANK2=1
    export LSION_ERROR_MSG_RANK=0

    for arg in "$@"
    do
      hook=$(expr "${arg}" : "pre_hook:\(.*\)")
      if [ -n "${hook}" ]
      then
        ${hook}
      fi
    done

    for arg in "$@"
    do
      file=$(expr "${arg}" : "copy:\(.*\)")
      if [ -n "${file}" ]
      then
        cp "../${file}" .
      fi
    done

    ../test_${TESTNAME} 1> stdout.log 2> stderr_t.log

    for arg in "$@"
    do
      hook=$(expr "${arg}" : "post_hook:\(.*\)")
      if [ -n "${hook}" ]
      then
        ${hook}
      fi
    done

    grep -v 'Writing debug output' stderr_t.log > stderr.log

    for arg in "$@"
    do
      file=$(expr "${arg}" : "siondump:\(.*\)")
      if [ -n "${file}" ]
      then
        export LSION_DEBUG=siondump_${TESTNAME}
        ${SIONDUMP} -a -l "${file}" 1> stdoutdump.log 2> stderrdump.log
        grep 'siondump:' stdoutdump.log | grep -v 'endianness' | grep -v 'SIONlib version' >> stdout.log
        grep 'Task ' stdoutdump.log >> stdout.log
      fi
    done

    failed=False
    for arg in "$@" diff:stderr.log:../test_${TESTNAME}_stderr.log diff:stdout.log:../test_${TESTNAME}_stdout.log
    do
      file1=$(expr "${arg}" : "diff:\(.*\):.*")
      file2=$(expr "${arg}" : "diff:.*:\(.*\)")
      if [ -n "${file1}" -a -n "${file2}" ]
      then
        if ! diff "${file1}" "${file2}" > /dev/null
        then
          echo "     test_${TESTNAME}              ... FAILED (diff in ${file1})"
          diff -u "${file2}" "${file1}"
          failed=True
        fi
      fi
    done
    if [ ${failed} = "True" ]
    then
      exit 1
    fi
  )
}
