1#!/bin/sh 2## 3## Copyright (c) 2014 The WebM project authors. All Rights Reserved. 4## 5## Use of this source code is governed by a BSD-style license 6## that can be found in the LICENSE file in the root of the source 7## tree. An additional intellectual property rights grant can be found 8## in the file PATENTS. All contributing project authors may 9## be found in the AUTHORS file in the root of the source tree. 10## 11## This file contains shell code shared by test scripts for libvpx tools. 12 13# Use $VPX_TEST_TOOLS_COMMON_SH as a pseudo include guard. 14if [ -z "${VPX_TEST_TOOLS_COMMON_SH}" ]; then 15VPX_TEST_TOOLS_COMMON_SH=included 16 17set -e 18devnull='> /dev/null 2>&1' 19VPX_TEST_PREFIX="" 20 21elog() { 22 echo "$@" 1>&2 23} 24 25vlog() { 26 if [ "${VPX_TEST_VERBOSE_OUTPUT}" = "yes" ]; then 27 echo "$@" 28 fi 29} 30 31# Sets $VPX_TOOL_TEST to the name specified by positional parameter one. 32test_begin() { 33 VPX_TOOL_TEST="${1}" 34} 35 36# Clears the VPX_TOOL_TEST variable after confirming that $VPX_TOOL_TEST matches 37# positional parameter one. 38test_end() { 39 if [ "$1" != "${VPX_TOOL_TEST}" ]; then 40 echo "FAIL completed test mismatch!." 41 echo " completed test: ${1}" 42 echo " active test: ${VPX_TOOL_TEST}." 43 return 1 44 fi 45 VPX_TOOL_TEST='<unset>' 46} 47 48# Echoes the target configuration being tested. 49test_configuration_target() { 50 vpx_config_mk="${LIBVPX_CONFIG_PATH}/config.mk" 51 # Find the TOOLCHAIN line, split it using ':=' as the field separator, and 52 # print the last field to get the value. Then pipe the value to tr to consume 53 # any leading/trailing spaces while allowing tr to echo the output to stdout. 54 awk -F ':=' '/TOOLCHAIN/ { print $NF }' "${vpx_config_mk}" | tr -d ' ' 55} 56 57# Trap function used for failure reports and tool output directory removal. 58# When the contents of $VPX_TOOL_TEST do not match the string '<unset>', reports 59# failure of test stored in $VPX_TOOL_TEST. 60cleanup() { 61 if [ -n "${VPX_TOOL_TEST}" ] && [ "${VPX_TOOL_TEST}" != '<unset>' ]; then 62 echo "FAIL: $VPX_TOOL_TEST" 63 fi 64 if [ -n "${VPX_TEST_OUTPUT_DIR}" ] && [ -d "${VPX_TEST_OUTPUT_DIR}" ]; then 65 rm -rf "${VPX_TEST_OUTPUT_DIR}" 66 fi 67} 68 69# Echoes the git hash portion of the VERSION_STRING variable defined in 70# $LIBVPX_CONFIG_PATH/config.mk to stdout, or the version number string when 71# no git hash is contained in VERSION_STRING. 72config_hash() { 73 vpx_config_mk="${LIBVPX_CONFIG_PATH}/config.mk" 74 # Find VERSION_STRING line, split it with "-g" and print the last field to 75 # output the git hash to stdout. 76 vpx_version=$(awk -F -g '/VERSION_STRING/ {print $NF}' "${vpx_config_mk}") 77 # Handle two situations here: 78 # 1. The default case: $vpx_version is a git hash, so echo it unchanged. 79 # 2. When being run a non-dev tree, the -g portion is not present in the 80 # version string: It's only the version number. 81 # In this case $vpx_version is something like 'VERSION_STRING=v1.3.0', so 82 # we echo only what is after the '='. 83 echo "${vpx_version##*=}" 84} 85 86# Echoes the short form of the current git hash. 87current_hash() { 88 if git --version > /dev/null 2>&1; then 89 (cd "$(dirname "${0}")" 90 git rev-parse --short HEAD) 91 else 92 # Return the config hash if git is unavailable: Fail silently, git hashes 93 # are used only for warnings. 94 config_hash 95 fi 96} 97 98# Echoes warnings to stdout when git hash in vpx_config.h does not match the 99# current git hash. 100check_git_hashes() { 101 hash_at_configure_time=$(config_hash) 102 hash_now=$(current_hash) 103 104 if [ "${hash_at_configure_time}" != "${hash_now}" ]; then 105 echo "Warning: git hash has changed since last configure." 106 fi 107} 108 109# $1 is the name of an environment variable containing a directory name to 110# test. 111test_env_var_dir() { 112 local dir=$(eval echo "\${$1}") 113 if [ ! -d "${dir}" ]; then 114 elog "'${dir}': No such directory" 115 elog "The $1 environment variable must be set to a valid directory." 116 return 1 117 fi 118} 119 120# This script requires that the LIBVPX_BIN_PATH, LIBVPX_CONFIG_PATH, and 121# LIBVPX_TEST_DATA_PATH variables are in the environment: Confirm that 122# the variables are set and that they all evaluate to directory paths. 123verify_vpx_test_environment() { 124 test_env_var_dir "LIBVPX_BIN_PATH" \ 125 && test_env_var_dir "LIBVPX_CONFIG_PATH" \ 126 && test_env_var_dir "LIBVPX_TEST_DATA_PATH" 127} 128 129# Greps vpx_config.h in LIBVPX_CONFIG_PATH for positional parameter one, which 130# should be a LIBVPX preprocessor flag. Echoes yes to stdout when the feature 131# is available. 132vpx_config_option_enabled() { 133 vpx_config_option="${1}" 134 vpx_config_file="${LIBVPX_CONFIG_PATH}/vpx_config.h" 135 config_line=$(grep "${vpx_config_option}" "${vpx_config_file}") 136 if echo "${config_line}" | grep -E -q '1$'; then 137 echo yes 138 fi 139} 140 141# Echoes yes when output of test_configuration_target() contains win32 or win64. 142is_windows_target() { 143 if test_configuration_target \ 144 | grep -q -e win32 -e win64 > /dev/null 2>&1; then 145 echo yes 146 fi 147} 148 149# Echoes path to $1 when it's executable and exists in ${LIBVPX_BIN_PATH}, or an 150# empty string. Caller is responsible for testing the string once the function 151# returns. 152vpx_tool_path() { 153 local tool_name="$1" 154 local tool_path="${LIBVPX_BIN_PATH}/${tool_name}${VPX_TEST_EXE_SUFFIX}" 155 if [ ! -x "${tool_path}" ]; then 156 # Try one directory up: when running via examples.sh the tool could be in 157 # the parent directory of $LIBVPX_BIN_PATH. 158 tool_path="${LIBVPX_BIN_PATH}/../${tool_name}${VPX_TEST_EXE_SUFFIX}" 159 fi 160 161 if [ ! -x "${tool_path}" ]; then 162 tool_path="" 163 fi 164 echo "${tool_path}" 165} 166 167# Echoes yes to stdout when the file named by positional parameter one exists 168# in LIBVPX_BIN_PATH, and is executable. 169vpx_tool_available() { 170 local tool_name="$1" 171 local tool="${LIBVPX_BIN_PATH}/${tool_name}${VPX_TEST_EXE_SUFFIX}" 172 [ -x "${tool}" ] && echo yes 173} 174 175# Echoes yes to stdout when vpx_config_option_enabled() reports yes for 176# CONFIG_VP8_DECODER. 177vp8_decode_available() { 178 [ "$(vpx_config_option_enabled CONFIG_VP8_DECODER)" = "yes" ] && echo yes 179} 180 181# Echoes yes to stdout when vpx_config_option_enabled() reports yes for 182# CONFIG_VP8_ENCODER. 183vp8_encode_available() { 184 [ "$(vpx_config_option_enabled CONFIG_VP8_ENCODER)" = "yes" ] && echo yes 185} 186 187# Echoes yes to stdout when vpx_config_option_enabled() reports yes for 188# CONFIG_VP9_DECODER. 189vp9_decode_available() { 190 [ "$(vpx_config_option_enabled CONFIG_VP9_DECODER)" = "yes" ] && echo yes 191} 192 193# Echoes yes to stdout when vpx_config_option_enabled() reports yes for 194# CONFIG_VP9_ENCODER. 195vp9_encode_available() { 196 [ "$(vpx_config_option_enabled CONFIG_VP9_ENCODER)" = "yes" ] && echo yes 197} 198 199# Echoes yes to stdout when vpx_config_option_enabled() reports yes for 200# CONFIG_WEBM_IO. 201webm_io_available() { 202 [ "$(vpx_config_option_enabled CONFIG_WEBM_IO)" = "yes" ] && echo yes 203} 204 205# Filters strings from $1 using the filter specified by $2. Filter behavior 206# depends on the presence of $3. When $3 is present, strings that match the 207# filter are excluded. When $3 is omitted, strings matching the filter are 208# included. 209# The filtered result is echoed to stdout. 210filter_strings() { 211 strings=${1} 212 filter=${2} 213 exclude=${3} 214 215 if [ -n "${exclude}" ]; then 216 # When positional parameter three exists the caller wants to remove strings. 217 # Tell grep to invert matches using the -v argument. 218 exclude='-v' 219 else 220 unset exclude 221 fi 222 223 if [ -n "${filter}" ]; then 224 for s in ${strings}; do 225 if echo "${s}" | grep -E -q ${exclude} "${filter}" > /dev/null 2>&1; then 226 filtered_strings="${filtered_strings} ${s}" 227 fi 228 done 229 else 230 filtered_strings="${strings}" 231 fi 232 echo "${filtered_strings}" 233} 234 235# Runs user test functions passed via positional parameters one and two. 236# Functions in positional parameter one are treated as environment verification 237# functions and are run unconditionally. Functions in positional parameter two 238# are run according to the rules specified in vpx_test_usage(). 239run_tests() { 240 local env_tests="verify_vpx_test_environment $1" 241 local tests_to_filter="$2" 242 local test_name="${VPX_TEST_NAME}" 243 244 if [ -z "${test_name}" ]; then 245 test_name="$(basename "${0%.*}")" 246 fi 247 248 if [ "${VPX_TEST_RUN_DISABLED_TESTS}" != "yes" ]; then 249 # Filter out DISABLED tests. 250 tests_to_filter=$(filter_strings "${tests_to_filter}" ^DISABLED exclude) 251 fi 252 253 if [ -n "${VPX_TEST_FILTER}" ]; then 254 # Remove tests not matching the user's filter. 255 tests_to_filter=$(filter_strings "${tests_to_filter}" ${VPX_TEST_FILTER}) 256 fi 257 258 # User requested test listing: Dump test names and return. 259 if [ "${VPX_TEST_LIST_TESTS}" = "yes" ]; then 260 for test_name in $tests_to_filter; do 261 echo ${test_name} 262 done 263 return 264 fi 265 266 # Don't bother with the environment tests if everything else was disabled. 267 [ -z "${tests_to_filter}" ] && return 268 269 # Combine environment and actual tests. 270 local tests_to_run="${env_tests} ${tests_to_filter}" 271 272 check_git_hashes 273 274 # Run tests. 275 for test in ${tests_to_run}; do 276 test_begin "${test}" 277 vlog " RUN ${test}" 278 "${test}" 279 vlog " PASS ${test}" 280 test_end "${test}" 281 done 282 283 # C vs SIMD tests are run for x86 32-bit, 64-bit and ARM platform 284 if [ "${test_name}" = "vp9_c_vs_simd_encode" ]; then 285 local tested_config="$(current_hash)" 286 else 287 local tested_config="$(test_configuration_target) @ $(current_hash)" 288 fi 289 echo "${test_name}: Done, all tests pass for ${tested_config}." 290} 291 292vpx_test_usage() { 293cat << EOF 294 Usage: ${0##*/} [arguments] 295 --bin-path <path to libvpx binaries directory> 296 --config-path <path to libvpx config directory> 297 --filter <filter>: User test filter. Only tests matching filter are run. 298 --run-disabled-tests: Run disabled tests. 299 --help: Display this message and exit. 300 --test-data-path <path to libvpx test data directory> 301 --show-program-output: Shows output from all programs being tested. 302 --prefix: Allows for a user specified prefix to be inserted before all test 303 programs. Grants the ability, for example, to run test programs 304 within valgrind. 305 --list-tests: List all test names and exit without actually running tests. 306 --verbose: Verbose output. 307 308 When the --bin-path option is not specified the script attempts to use 309 \$LIBVPX_BIN_PATH and then the current directory. 310 311 When the --config-path option is not specified the script attempts to use 312 \$LIBVPX_CONFIG_PATH and then the current directory. 313 314 When the -test-data-path option is not specified the script attempts to use 315 \$LIBVPX_TEST_DATA_PATH and then the current directory. 316EOF 317} 318 319# Returns non-zero (failure) when required environment variables are empty 320# strings. 321vpx_test_check_environment() { 322 if [ -z "${LIBVPX_BIN_PATH}" ] || \ 323 [ -z "${LIBVPX_CONFIG_PATH}" ] || \ 324 [ -z "${LIBVPX_TEST_DATA_PATH}" ]; then 325 return 1 326 fi 327} 328 329# Parse the command line. 330while [ -n "$1" ]; do 331 case "$1" in 332 --bin-path) 333 LIBVPX_BIN_PATH="$2" 334 shift 335 ;; 336 --config-path) 337 LIBVPX_CONFIG_PATH="$2" 338 shift 339 ;; 340 --filter) 341 VPX_TEST_FILTER="$2" 342 shift 343 ;; 344 --run-disabled-tests) 345 VPX_TEST_RUN_DISABLED_TESTS=yes 346 ;; 347 --help) 348 vpx_test_usage 349 exit 350 ;; 351 --test-data-path) 352 LIBVPX_TEST_DATA_PATH="$2" 353 shift 354 ;; 355 --prefix) 356 VPX_TEST_PREFIX="$2" 357 shift 358 ;; 359 --verbose) 360 VPX_TEST_VERBOSE_OUTPUT=yes 361 ;; 362 --show-program-output) 363 devnull= 364 ;; 365 --list-tests) 366 VPX_TEST_LIST_TESTS=yes 367 ;; 368 *) 369 vpx_test_usage 370 exit 1 371 ;; 372 esac 373 shift 374done 375 376# Handle running the tests from a build directory without arguments when running 377# the tests on *nix/macosx. 378LIBVPX_BIN_PATH="${LIBVPX_BIN_PATH:-.}" 379LIBVPX_CONFIG_PATH="${LIBVPX_CONFIG_PATH:-.}" 380LIBVPX_TEST_DATA_PATH="${LIBVPX_TEST_DATA_PATH:-.}" 381 382# Create a temporary directory for output files, and a trap to clean it up. 383if [ -n "${TMPDIR}" ]; then 384 VPX_TEST_TEMP_ROOT="${TMPDIR}" 385elif [ -n "${TEMPDIR}" ]; then 386 VPX_TEST_TEMP_ROOT="${TEMPDIR}" 387else 388 VPX_TEST_TEMP_ROOT=/tmp 389fi 390 391VPX_TEST_OUTPUT_DIR="${VPX_TEST_TEMP_ROOT}/vpx_test_$$" 392 393if ! mkdir -p "${VPX_TEST_OUTPUT_DIR}" || \ 394 [ ! -d "${VPX_TEST_OUTPUT_DIR}" ]; then 395 echo "${0##*/}: Cannot create output directory, giving up." 396 echo "${0##*/}: VPX_TEST_OUTPUT_DIR=${VPX_TEST_OUTPUT_DIR}" 397 exit 1 398fi 399 400if [ "$(is_windows_target)" = "yes" ]; then 401 VPX_TEST_EXE_SUFFIX=".exe" 402fi 403 404# Variables shared by tests. 405VP8_IVF_FILE="${LIBVPX_TEST_DATA_PATH}/vp80-00-comprehensive-001.ivf" 406VP9_IVF_FILE="${LIBVPX_TEST_DATA_PATH}/vp90-2-09-subpixel-00.ivf" 407 408VP9_WEBM_FILE="${LIBVPX_TEST_DATA_PATH}/vp90-2-00-quantizer-00.webm" 409VP9_FPM_WEBM_FILE="${LIBVPX_TEST_DATA_PATH}/vp90-2-07-frame_parallel-1.webm" 410VP9_LT_50_FRAMES_WEBM_FILE="${LIBVPX_TEST_DATA_PATH}/vp90-2-02-size-32x08.webm" 411 412VP9_RAW_FILE="${LIBVPX_TEST_DATA_PATH}/crbug-1539.rawfile" 413 414YUV_RAW_INPUT="${LIBVPX_TEST_DATA_PATH}/hantro_collage_w352h288.yuv" 415YUV_RAW_INPUT_WIDTH=352 416YUV_RAW_INPUT_HEIGHT=288 417 418Y4M_NOSQ_PAR_INPUT="${LIBVPX_TEST_DATA_PATH}/park_joy_90p_8_420_a10-1.y4m" 419Y4M_720P_INPUT="${LIBVPX_TEST_DATA_PATH}/niklas_1280_720_30.y4m" 420Y4M_720P_INPUT_WIDTH=1280 421Y4M_720P_INPUT_HEIGHT=720 422 423# Setup a trap function to clean up after tests complete. 424trap cleanup EXIT 425 426vlog "$(basename "${0%.*}") test configuration: 427 LIBVPX_BIN_PATH=${LIBVPX_BIN_PATH} 428 LIBVPX_CONFIG_PATH=${LIBVPX_CONFIG_PATH} 429 LIBVPX_TEST_DATA_PATH=${LIBVPX_TEST_DATA_PATH} 430 VP8_IVF_FILE=${VP8_IVF_FILE} 431 VP9_IVF_FILE=${VP9_IVF_FILE} 432 VP9_WEBM_FILE=${VP9_WEBM_FILE} 433 VPX_TEST_EXE_SUFFIX=${VPX_TEST_EXE_SUFFIX} 434 VPX_TEST_FILTER=${VPX_TEST_FILTER} 435 VPX_TEST_LIST_TESTS=${VPX_TEST_LIST_TESTS} 436 VPX_TEST_OUTPUT_DIR=${VPX_TEST_OUTPUT_DIR} 437 VPX_TEST_PREFIX=${VPX_TEST_PREFIX} 438 VPX_TEST_RUN_DISABLED_TESTS=${VPX_TEST_RUN_DISABLED_TESTS} 439 VPX_TEST_SHOW_PROGRAM_OUTPUT=${VPX_TEST_SHOW_PROGRAM_OUTPUT} 440 VPX_TEST_TEMP_ROOT=${VPX_TEST_TEMP_ROOT} 441 VPX_TEST_VERBOSE_OUTPUT=${VPX_TEST_VERBOSE_OUTPUT} 442 YUV_RAW_INPUT=${YUV_RAW_INPUT} 443 YUV_RAW_INPUT_WIDTH=${YUV_RAW_INPUT_WIDTH} 444 YUV_RAW_INPUT_HEIGHT=${YUV_RAW_INPUT_HEIGHT} 445 Y4M_NOSQ_PAR_INPUT=${Y4M_NOSQ_PAR_INPUT}" 446 447fi # End $VPX_TEST_TOOLS_COMMON_SH pseudo include guard. 448