<commit_before>#!/bin/zsh

# STC RUN-TESTS

# See About.txt for notes

# Defaults
EXIT_ON_FAIL=1
MAX_TESTS=-1 # by default, unlimited
PATTERN=""
SKIP_COUNT=0
VERBOSE=0
STC_OPT_LEVEL=1 #-O level for STC
ADDTL_STC_ARGS=()

# Speed up the tests
if [ -z ${ADLB_EXHAUST_TIME} ]; then
    export ADLB_EXHAUST_TIME=1
fi

while getopts "ck:n:p:VO:t:T:" OPTION
do
  case ${OPTION}
    in
    c)
      # continue after failure
      EXIT_ON_FAIL=0
      ;;
    k)
      # skip some tests
      SKIP_COUNT=${OPTARG}
      ;;
    n)
      # run a limited number of tests
      MAX_TESTS=${OPTARG}
      ;;
    p)
      # run only tests that match the pattern
      PATTERN=${OPTARG}
      ;;
    t)
      ADDTL_STC_ARGS+="-t${OPTARG}"
      ;;
    T)
      ADDTL_STC_ARGS+="-T${OPTARG}"
      ;;
    V)
      VERBOSE=1
      ;;
    O)
      STC_OPT_LEVEL=${OPTARG}
      ;;
    *)
      # ZSH already prints an error message
      exit 1
  esac
done

if (( VERBOSE ))
then
  set -x
fi

crash()
{
  MSG=$1
  print ${MSG}
  exit 1
}

export STC_TESTS_DIR=$( cd $( dirname $0 ) ; /bin/pwd )
STC_ROOT_DIR=$( dirname $STC_TESTS_DIR )
STC_TRIES=( ${STC_ROOT_DIR}/code ${STC_ROOT_DIR} )
STC=""
for D in ${STC_TRIES}
do
  if [[ -x ${D}/bin/stc && -r ${D}/conf/stc-env.sh ]]
    then
    STC=${D}/bin/stc
    break
  fi
done
if [[ ${STC} == "" ]]
then
  STC=$( which stc )
fi
if [[ ${STC} == "" ]]
then
  print "Could not find STC!"
  exit 1
fi
print "using stc: ${STC}\n"

RUN_TEST=${STC_TESTS_DIR}/run-test.zsh

compile_test()
# Translate test
{
  print "compiling: $( basename ${SWIFT_FILE} )"

  ${STC} -l ${STC_LOG_FILE} -O ${STC_OPT_LEVEL} -C ${STC_IC_FILE} \
            ${ADDTL_STC_ARGS} \
            ${SWIFT_FILE} ${TCL_FILE} \
            > ${STC_OUT_FILE} 2> ${STC_ERR_FILE}
  EXIT_CODE=${?}
}

run_test()
# Run test under Turbine/MPI
{
  SETUP_SCRIPT=${TEST_PATH}.setup.sh
  CHECK_SCRIPT=${TEST_PATH}.check.sh

  SETUP_OUTPUT=${SETUP_SCRIPT%.sh}.out
  CHECK_OUTPUT=${CHECK_SCRIPT%.sh}.out
  EXP_OUTPUT=${TEST_PATH}.exp
  TURBINE_OUTPUT=${TCL_FILE%.tcl}.out
  export TURBINE_OUTPUT

  ARGS=""
  ARGS_FILE=${TEST_PATH}.args

  # Set up the test
  if [ -x ${SETUP_SCRIPT} ]
  then
    print "executing: $( basename ${SETUP_SCRIPT} )"
    ${SETUP_SCRIPT} >& ${SETUP_OUTPUT} || return 1
  fi

  # Get test command-line arguments
  if [ -r ${ARGS_FILE} ]
  then
    read ARGS < ${ARGS_FILE}
  fi

  # Run the test
  print "running: $( basename ${TCL_FILE} )"
  ${RUN_TEST} ${TCL_FILE} ${TURBINE_OUTPUT} ${ARGS}
  EXIT_CODE=${?}

  if grep -q "THIS-TEST-SHOULD-NOT-RUN" ${SWIFT_FILE}
  then
    # This test was intended to fail at run time
    EXIT_CODE=$(( ! EXIT_CODE ))
  fi
  (( EXIT_CODE )) && return ${EXIT_CODE}

  # Check for unexecuted transforms
  grep -q "WAITING TRANSFORMS" ${TURBINE_OUTPUT}
  # This is 0 if nothing was found
  WAITING_TRANSFORMS=$(( ! ${?} ))
  if (( WAITING_TRANSFORMS ))
    then
    print "Transforms were left in the rule engine!"
    return 1
  fi

  # Check the test output with the test-specific check script
  if [ -x ${CHECK_SCRIPT} ]
  then
    print "executing: $( basename ${CHECK_SCRIPT} )"
    ${CHECK_SCRIPT} >& ${CHECK_OUTPUT} || return 1
  fi

  # Check the output for expected lines
  if [ -f ${EXP_OUTPUT} ]
  then
    local LINE_MISSING=false
    while read line
    do
      grep -q "${line}" "${TURBINE_OUTPUT}"
      if  (( $? != 0 ))
      then
        print "'${line}' wasn't present in output"
        LINE_MISSING=true
      fi
    done <  ${EXP_OUTPUT}
    if [ $LINE_MISSING = true ]; then
      return 1
    fi
  fi
  return 0
}

report_result()
{<commit_msg>Don't fail test if it was meant to fail and there were unexecuted transforms<commit_after>#!/bin/zsh

# STC RUN-TESTS

# See About.txt for notes

# Defaults
EXIT_ON_FAIL=1
MAX_TESTS=-1 # by default, unlimited
PATTERN=""
SKIP_COUNT=0
VERBOSE=0
STC_OPT_LEVEL=1 #-O level for STC
ADDTL_STC_ARGS=()

# Speed up the tests
if [ -z ${ADLB_EXHAUST_TIME} ]; then
    export ADLB_EXHAUST_TIME=1
fi

while getopts "ck:n:p:VO:t:T:" OPTION
do
  case ${OPTION}
    in
    c)
      # continue after failure
      EXIT_ON_FAIL=0
      ;;
    k)
      # skip some tests
      SKIP_COUNT=${OPTARG}
      ;;
    n)
      # run a limited number of tests
      MAX_TESTS=${OPTARG}
      ;;
    p)
      # run only tests that match the pattern
      PATTERN=${OPTARG}
      ;;
    t)
      ADDTL_STC_ARGS+="-t${OPTARG}"
      ;;
    T)
      ADDTL_STC_ARGS+="-T${OPTARG}"
      ;;
    V)
      VERBOSE=1
      ;;
    O)
      STC_OPT_LEVEL=${OPTARG}
      ;;
    *)
      # ZSH already prints an error message
      exit 1
  esac
done

if (( VERBOSE ))
then
  set -x
fi

crash()
{
  MSG=$1
  print ${MSG}
  exit 1
}

export STC_TESTS_DIR=$( cd $( dirname $0 ) ; /bin/pwd )
STC_ROOT_DIR=$( dirname $STC_TESTS_DIR )
STC_TRIES=( ${STC_ROOT_DIR}/code ${STC_ROOT_DIR} )
STC=""
for D in ${STC_TRIES}
do
  if [[ -x ${D}/bin/stc && -r ${D}/conf/stc-env.sh ]]
    then
    STC=${D}/bin/stc
    break
  fi
done
if [[ ${STC} == "" ]]
then
  STC=$( which stc )
fi
if [[ ${STC} == "" ]]
then
  print "Could not find STC!"
  exit 1
fi
print "using stc: ${STC}\n"

RUN_TEST=${STC_TESTS_DIR}/run-test.zsh

compile_test()
# Translate test
{
  print "compiling: $( basename ${SWIFT_FILE} )"

  ${STC} -l ${STC_LOG_FILE} -O ${STC_OPT_LEVEL} -C ${STC_IC_FILE} \
            ${ADDTL_STC_ARGS} \
            ${SWIFT_FILE} ${TCL_FILE} \
            > ${STC_OUT_FILE} 2> ${STC_ERR_FILE}
  EXIT_CODE=${?}
}

run_test()
# Run test under Turbine/MPI
{
  SETUP_SCRIPT=${TEST_PATH}.setup.sh
  CHECK_SCRIPT=${TEST_PATH}.check.sh

  SETUP_OUTPUT=${SETUP_SCRIPT%.sh}.out
  CHECK_OUTPUT=${CHECK_SCRIPT%.sh}.out
  EXP_OUTPUT=${TEST_PATH}.exp
  TURBINE_OUTPUT=${TCL_FILE%.tcl}.out
  export TURBINE_OUTPUT

  ARGS=""
  ARGS_FILE=${TEST_PATH}.args

  # Set up the test
  if [ -x ${SETUP_SCRIPT} ]
  then
    print "executing: $( basename ${SETUP_SCRIPT} )"
    ${SETUP_SCRIPT} >& ${SETUP_OUTPUT} || return 1
  fi

  # Get test command-line arguments
  if [ -r ${ARGS_FILE} ]
  then
    read ARGS < ${ARGS_FILE}
  fi

  # Run the test
  print "running: $( basename ${TCL_FILE} )"
  ${RUN_TEST} ${TCL_FILE} ${TURBINE_OUTPUT} ${ARGS}
  EXIT_CODE=${?}

  if grep -q "THIS-TEST-SHOULD-NOT-RUN" ${SWIFT_FILE}
  then
    # This test was intended to fail at run time
    EXIT_CODE=$(( ! EXIT_CODE ))
  else
    # Check for unexecuted transforms
    grep -q "WAITING TRANSFORMS" ${TURBINE_OUTPUT}
    # This is 0 if nothing was found
    WAITING_TRANSFORMS=$(( ! ${?} ))
    if (( WAITING_TRANSFORMS ))
      then
      print "Transforms were left in the rule engine!"
      return 1
    fi
  fi
  (( EXIT_CODE )) && return ${EXIT_CODE}

  # Check the test output with the test-specific check script
  if [ -x ${CHECK_SCRIPT} ]
  then
    print "executing: $( basename ${CHECK_SCRIPT} )"
    ${CHECK_SCRIPT} >& ${CHECK_OUTPUT} || return 1
  fi

  # Check the output for expected lines
  if [ -f ${EXP_OUTPUT} ]
  then
    local LINE_MISSING=false
    while read line
    do
      grep -q "${line}" "${TURBINE_OUTPUT}"
      if  (( $? != 0 ))
      then
        print "'${line}' wasn't present in output"
        LINE_MISSING=true
      fi
    done <  ${EXP_OUTPUT}
    if [ $LINE_MISSING = true ]; then
      return 1
    fi
  fi
  return 0
}

report_result()
{
{'fraction_non_alphanumeric': 0.14454034799287574, 'fraction_numerical': 0.005754212905877518, 'mean_word_length': 2.7018255578093306, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6676671', 'n_tokens_mistral': 2998, 'n_tokens_neox': 2869, 'n_words': 702}