blob: e4ae278fb24b95791322478f37f64f84b1a10bfc [file] [log] [blame]
#!/system/bin/sh
#
# Copyright 2015 Nest Labs, Inc. All rights reserved.
PACKAGE="boost-regex-tests"
TEST_ROOT="$(dirname $0)"
grep -vE "^/" "$TEST_ROOT" &> /dev/null
if [ $? -eq 0 ] ; then
TEST_ROOT="$(pwd)/${TEST_ROOT}"
fi
THIS_SCRIPT="$(basename $0)"
###
### GLOBAL VARIABLES
###
### only export variables that mutate
###
# Increment when a test passes
export GLOBAL_PASS=0
# Increment when a test fails
export GLOBAL_FAIL=0
# The directory where helper test programs are installed
TEST_BIN="${TEST_ROOT}/bin"
# The directory where helper test data is installed
TEST_DATA="${TEST_ROOT}/data"
# The temporary directory where raw program output is placed
TMPDIR=/storage
# The log file with all raw program output
LOGFILE=$(mktemp --tmpdir="${TMPDIR}" ${PACKAGE}-log.XXXXXXXX)
# A temporary file, usually used for staging raw program output for grep
TMPFILE=$(mktemp --tmpdir="${TMPDIR}" ${PACKAGE}-tmp.XXXXXXXX)
# The scripts need their PATH set, too
export PATH="${TEST_BIN}:${PATH}"
# run_test
#
# This is a /bin/sh port of add_gflags_test that is implemented by
# cmake/utils.cmake and cmake/execute_test.cmake
#
# Usage: run_test <test-name> <expected-return-code> <expected-text-output> <unexpected-text-output> <cmd> [args...]
#
# test-name: A symbolic name for the test for logging
#
# expected-return-code: the number passed back to the operating system. default: 0
# if a blank string is given.
#
# expected-text-output: if a non-empty string, it is expected that the command will
# output this string. It must be seen in order to pass.
#
# unexpected-text-output: if a non-empty string, it is expected that the command
# will NOT output this string. If it is seen, the test will fail.
#
# cmd: executable to run (full path or in $PATH)
#
# args: all arguments that need to be passed on to the executable.
#
run_test()
{
TEST_NAME="$1"
shift
EXPECTED_RC="$1"
shift
EXPECTED_OUTPUT="$1"
shift
UNEXPECTED_OUTPUT="$1"
shift
if [ $# -eq 0 ] ; then
GLOBAL_RETURN_CODE=1
echo "FATAL ERROR: invalid arguments passed to run_test for test='$TEST_NAME'"
return 1
fi
if [ -z "$EXPECTED_RC" ] ; then
EXPECTED_RC="0"
fi
VERDICT="pass"
REASON="default"
echo "RUNNING TEST '${TEST_NAME}' >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" > "$TMPFILE"
echo "CMDLINE: $@" >> "$TMPFILE"
"$@" &>> "$TMPFILE"
RC=$?
echo "RC=$RC" >> "$TMPFILE"
echo "END OF TEST '${TEST_NAME}' <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" >> "$TMPFILE"
EXPECTED_FOUND=1
UNEXPECTED_FOUND=0
if [ -n "$EXPECTED_OUTPUT" ] ; then
grep -- "$EXPECTED_OUTPUT" "$TMPFILE" &> /dev/null
if [ $? -ne 0 ] ; then
EXPECTED_FOUND=0
fi
fi
if [ -n "$UNEXPECTED_OUTPUT" ] ; then
grep -- "$UNEXPECTED_OUTPUT" "$TMPFILE" &> /dev/null
if [ $? -eq 0 ] ; then
UNEXPECTED_FOUND=1
fi
fi
if [ "$RC" != "$EXPECTED_RC" ] ; then
VERDICT="fail"
REASON="unexpected return code (got ${RC}, expected ${EXPECTED_RC})"
elif [ "$EXPECTED_FOUND" = "0" ] ; then
VERDICT="fail"
REASON="missing expected output, expected '${EXPECTED_OUTPUT}', see '$LOGFILE'"
elif [ "$UNEXPECTED_FOUND" = "1" ] ; then
VERDICT="fail"
REASON="Unexpected output detected: '${UNEXPECTED_OUTPUT}', see '$LOGFILE'"
fi
cat "$TMPFILE" >> "$LOGFILE"
if [ "$VERDICT" = "pass" ] ; then
echo "${TEST_NAME}: pass"
GLOBAL_PASS=$(($GLOBAL_PASS + 1))
return 0
else
echo "${TEST_NAME}: fail ($REASON)"
GLOBAL_FAIL=$(($GLOBAL_FAIL + 1))
return 1
fi
}
# Tests must be executed from here:
cd "$TEST_ROOT"
###
### TEST CASES
###
TEST_CASES="regex_regress \
regex_regress_threaded \
posix_api_check \
wide_posix_api_check_c \
posix_api_check_cpp \
wide_posix_api_check_cpp \
bad_expression_test \
recursion_test \
named_subexpressions_test \
unicode_iterator_test_utf8 \
unicode_iterator_test_utf16 \
static_mutex_test \
object_cache_test \
regex_config_info \
regex_dll_config_info \
collate_info \
concept_check \
captures_test \
regex_regress_recursive \
regex_regress_noeh"
# Test this script
run_test "the_truth" 0 "" "" true
run_test "the_lie" 1 "" "" false
run_test "the_word" "" "really unlikely" "" echo "really unlikely"
for T in $TEST_CASES ; do
if [ ! -f "${TEST_BIN}/${T}" ] ; then
continue
fi
if [ ! -x "${TEST_BIN}/${T}" ] ; then
echo "${T}: fail (file in bin/ folder without execute permission)"
continue
fi
run_test "$T" 0 "" "" "${TEST_BIN}/$T"
done
###
### EXAMPLES
###
run_test "regex_timer" 0 "" "" ${TEST_BIN}/regex_timer_example ${TEST_DATA}/input_script.txt
run_test "credit_card_example" 0 "" "" ${TEST_BIN}/credit_card_example
run_test "mfc_example" 0 "" "" ${TEST_BIN}/mfc_example
run_test "icu_examplee" 0 "" "" ${TEST_BIN}/icu_example
run_test "partial_regex_grep" 0 "total tag count was 19" "" ${TEST_BIN}/partial_regex_grep ${TEST_DATA}/index_small.html
run_test "partial_regex_iterate" 0 "total tag count was 19" "" ${TEST_BIN}/partial_regex_iterate ${TEST_DATA}/index_small.html
run_test "partial_regex_match" 0 "Got a partial match..." "" ${TEST_BIN}/partial_regex_match 1234-5678-8765-4
run_test "regex_grep_example_1" 0 "4 matches found" "" ${TEST_BIN}/regex_grep_example_1 ${TEST_DATA}/rational.hpp
run_test "regex_grep_example_2" 0 "4 matches found" "" ${TEST_BIN}/regex_grep_example_2 ${TEST_DATA}/rational.hpp
run_test "regex_grep_example_3" 0 "4 matches found" "" ${TEST_BIN}/regex_grep_example_3 ${TEST_DATA}/rational.hpp
run_test "regex_grep_example_4" 0 "" "" ${TEST_BIN}/regex_grep_example_4 ${TEST_DATA}/rational.hpp
run_test "regex_match_example" 0 "Match found:" "Match not found" ${TEST_BIN}/regex_match_example -auto
run_test "regex_merge_example" 0 "" "" ${TEST_BIN}/regex_merge_example ${TEST_DATA}/rational.hpp
run_test "regex_replace_example" 0 "" "" ${TEST_BIN}/regex_replace_example ${TEST_DATA}/rational.hpp
run_test "regex_search_example" 0 "4 matches found" "" ${TEST_BIN}/regex_search_example ${TEST_DATA}/rational.hpp
run_test "regex_split_example_1" 0 "6 tokens found" "" ${TEST_BIN}/regex_split_example_1 -auto
run_test "regex_split_example_2" 0 "" "" ${TEST_BIN}/regex_split_example_2 ${TEST_DATA}/index.html
run_test "regex_token_iterator_eg_1" 0 "There were 6 tokens found." "" ${TEST_BIN}/regex_token_iterator_eg_1 -auto
run_test "regex_token_iterator_eg_2" 0 "" "" ${TEST_BIN}/regex_token_iterator_eg_2 ${TEST_DATA}/index.html
run_test "regex_iterator_example" 0 "4 matches found" "" ${TEST_BIN}/regex_iterator_example ${TEST_DATA}/rational.hpp
run_test "captures_example" 0 "" "** No Match found **" ${TEST_BIN}/captures_example
###
### RESULTS
###
if [ $GLOBAL_FAIL -ne 0 ] ; then
RETURN_CODE=1
else
RETURN_CODE=0
fi
TOTAL_TESTS=$(($GLOBAL_PASS + $GLOBAL_FAIL))
echo "${GLOBAL_PASS}/${TOTAL_TESTS} passed. See '$LOGFILE'"
rm "$TMPFILE"
exit $RETURN_CODE