Module Name: src Committed By: rillig Date: Sat Mar 15 06:53:06 UTC 2025
Modified Files: src/tests/bin/expr: t_expr.sh Log Message: tests/expr: reduce the amount of shell escaping To generate a diff of this commit: cvs rdiff -u -r1.8 -r1.9 src/tests/bin/expr/t_expr.sh Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/tests/bin/expr/t_expr.sh diff -u src/tests/bin/expr/t_expr.sh:1.8 src/tests/bin/expr/t_expr.sh:1.9 --- src/tests/bin/expr/t_expr.sh:1.8 Fri Mar 14 22:12:00 2025 +++ src/tests/bin/expr/t_expr.sh Sat Mar 15 06:53:06 2025 @@ -1,4 +1,4 @@ -# $NetBSD: t_expr.sh,v 1.8 2025/03/14 22:12:00 rillig Exp $ +# $NetBSD: t_expr.sh,v 1.9 2025/03/15 06:53:06 rillig Exp $ # # Copyright (c) 2007 The NetBSD Foundation, Inc. # All rights reserved. @@ -25,15 +25,25 @@ # POSSIBILITY OF SUCH DAMAGE. # -# The first arg will get eval'd so escape any meta characters -# The 2nd arg is an expected string/response from expr for that op. +# usage: test_expr operand ... result|error test_expr() { - echo "Expression '${1}', expecting '${2}'" - res=`eval expr $1 2>&1` - if [ "$res" != "$2" ]; then - atf_fail "Expected $2, got $res from expression: " \ - "`eval echo $1`" - fi + i=1 + while [ $((i++)) -lt $# ]; do + set -- "$@" "$1" + shift + done + expected="$1" + shift + + # shellcheck disable=SC2003 + actual=$(expr "$@" 2>&1 || :) + + printf "%s => '%s'\n" "$*" "$expected" >> expected + printf "%s => '%s'\n" "$*" "$actual" >> actual +} + +test_finish() { + atf_check -o file:expected cat actual } atf_test_case lang @@ -54,51 +64,53 @@ overflow_head() { atf_set "descr" "Test overflow cases" } overflow_body() { - test_expr '4611686018427387904 + 4611686018427387903' \ + test_expr 4611686018427387904 + 4611686018427387903 \ '9223372036854775807' - test_expr '4611686018427387904 + 4611686018427387904' \ + test_expr 4611686018427387904 + 4611686018427387904 \ "expr: integer overflow or underflow occurred for operation '4611686018427387904 + 4611686018427387904'" - test_expr '4611686018427387904 - -4611686018427387904' \ + test_expr 4611686018427387904 - -4611686018427387904 \ "expr: integer overflow or underflow occurred for operation '4611686018427387904 - -4611686018427387904'" - test_expr '-4611686018427387904 - 4611686018427387903' \ + test_expr -4611686018427387904 - 4611686018427387903 \ '-9223372036854775807' - test_expr '-4611686018427387904 - 4611686018427387905' \ + test_expr -4611686018427387904 - 4611686018427387905 \ "expr: integer overflow or underflow occurred for operation '-4611686018427387904 - 4611686018427387905'" - test_expr '-4611686018427387904 \* 1' '-4611686018427387904' - test_expr '-4611686018427387904 \* -1' '4611686018427387904' - test_expr '-4611686018427387904 \* 2' '-9223372036854775808' - test_expr '-4611686018427387904 \* 3' \ + test_expr -4611686018427387904 \* 1 '-4611686018427387904' + test_expr -4611686018427387904 \* -1 '4611686018427387904' + test_expr -4611686018427387904 \* 2 '-9223372036854775808' + test_expr -4611686018427387904 \* 3 \ "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * 3'" - test_expr '-4611686018427387904 \* -2' \ + test_expr -4611686018427387904 \* -2 \ "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * -2'" - test_expr '4611686018427387904 \* 1' '4611686018427387904' - test_expr '4611686018427387904 \* 2' \ + test_expr 4611686018427387904 \* 1 '4611686018427387904' + test_expr 4611686018427387904 \* 2 \ "expr: integer overflow or underflow occurred for operation '4611686018427387904 * 2'" - test_expr '4611686018427387904 \* 3' \ + test_expr 4611686018427387904 \* 3 \ "expr: integer overflow or underflow occurred for operation '4611686018427387904 * 3'" - test_expr '-9223372036854775808 % -1' \ + test_expr -9223372036854775808 % -1 \ "expr: integer overflow or underflow occurred for operation '-9223372036854775808 % -1'" - test_expr '-9223372036854775808 / -1' \ + test_expr -9223372036854775808 / -1 \ "expr: integer overflow or underflow occurred for operation '-9223372036854775808 / -1'" - test_expr '0 + -9223372036854775808' '-9223372036854775808' - test_expr '0 + -1' '-1' - test_expr '0 + 0' '0' - test_expr '0 + 1' '1' - test_expr '0 + 9223372036854775807' '9223372036854775807' - test_expr '-9223372036854775808 + 0' '-9223372036854775808' - test_expr '9223372036854775807 + 0' '9223372036854775807' - test_expr '4611686018427387904 \* -1' '-4611686018427387904' - test_expr '4611686018427387904 \* -2' '-9223372036854775808' - test_expr '4611686018427387904 \* -3' \ + test_expr 0 + -9223372036854775808 '-9223372036854775808' + test_expr 0 + -1 '-1' + test_expr 0 + 0 '0' + test_expr 0 + 1 '1' + test_expr 0 + 9223372036854775807 '9223372036854775807' + test_expr -9223372036854775808 + 0 '-9223372036854775808' + test_expr 9223372036854775807 + 0 '9223372036854775807' + test_expr 4611686018427387904 \* -1 '-4611686018427387904' + test_expr 4611686018427387904 \* -2 '-9223372036854775808' + test_expr 4611686018427387904 \* -3 \ "expr: integer overflow or underflow occurred for operation '4611686018427387904 * -3'" - test_expr '-4611686018427387904 \* -1' '4611686018427387904' - test_expr '-4611686018427387904 \* -2' \ + test_expr -4611686018427387904 \* -1 '4611686018427387904' + test_expr -4611686018427387904 \* -2 \ "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * -2'" - test_expr '-4611686018427387904 \* -3' \ + test_expr -4611686018427387904 \* -3 \ "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * -3'" - test_expr '0 \* -1' '0' - test_expr '0 \* 0' '0' - test_expr '0 \* 1' '0' + test_expr 0 \* -1 '0' + test_expr 0 \* 0 '0' + test_expr 0 \* 1 '0' + + test_finish } atf_test_case gtkmm @@ -106,12 +118,14 @@ gtkmm_head() { atf_set "descr" "Tests from gtk-- configure that cause problems on old expr" } gtkmm_body() { - test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 5' '1' - test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 6' '0' - test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 3 \& 5 \>= 5' '0' - test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 2 \& 4 = 4 \& 5 \>= 5' '0' - test_expr '3 \> 2 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 6' '1' - test_expr '3 \> 3 \| 3 = 3 \& 4 \> 3 \| 3 = 3 \& 4 = 4 \& 5 \>= 5' '1' + test_expr 3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 5 '1' + test_expr 3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 6 '0' + test_expr 3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 3 \& 5 \>= 5 '0' + test_expr 3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 2 \& 4 = 4 \& 5 \>= 5 '0' + test_expr 3 \> 2 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 6 '1' + test_expr 3 \> 3 \| 3 = 3 \& 4 \> 3 \| 3 = 3 \& 4 = 4 \& 5 \>= 5 '1' + + test_finish } atf_test_case colon_vs_math @@ -119,8 +133,10 @@ colon_vs_math_head() { atf_set "descr" "Basic precendence test with the : operator vs. math" } colon_vs_math_body() { - test_expr '2 : 4 / 2' '0' - test_expr '4 : 4 % 3' '1' + test_expr 2 : 4 / 2 '0' + test_expr 4 : 4 % 3 '1' + + test_finish } atf_test_case arithmetic_ops @@ -128,12 +144,14 @@ arithmetic_ops_head() { atf_set "descr" "Dangling arithmetic operator" } arithmetic_ops_body() { - test_expr '.java_wrapper : /' '0' - test_expr '4 : \*' '0' - test_expr '4 : +' '0' - test_expr '4 : -' '0' - test_expr '4 : /' '0' - test_expr '4 : %' '0' + test_expr .java_wrapper : / '0' + test_expr 4 : \* '0' + test_expr 4 : + '0' + test_expr 4 : - '0' + test_expr 4 : / '0' + test_expr 4 : % '0' + + test_finish } atf_test_case basic_math @@ -141,7 +159,9 @@ basic_math_head() { atf_set "descr" "Basic math test" } basic_math_body() { - test_expr '2 + 4 \* 5' '22' + test_expr 2 + 4 \* 5 '22' + + test_finish } atf_test_case basic_functional @@ -149,9 +169,11 @@ basic_functional_head() { atf_set "descr" "Basic functional tests" } basic_functional_body() { - test_expr '2' '2' - test_expr '-4' '-4' - test_expr 'hello' 'hello' + test_expr 2 '2' + test_expr -4 '-4' + test_expr hello 'hello' + + test_finish } atf_test_case compare_ops_precedence @@ -159,7 +181,9 @@ compare_ops_precedence_head() { atf_set "descr" "Compare operator precendence test" } compare_ops_precedence_body() { - test_expr '2 \> 1 \* 17' '0' + test_expr 2 \> 1 \* 17 '0' + + test_finish } atf_test_case compare_ops @@ -167,20 +191,22 @@ compare_ops_head() { atf_set "descr" "Compare operator tests" } compare_ops_body() { - test_expr '2 \!= 5' '1' - test_expr '2 \!= 2' '0' - test_expr '2 \<= 3' '1' - test_expr '2 \<= 2' '1' - test_expr '2 \<= 1' '0' - test_expr '2 \< 3' '1' - test_expr '2 \< 2' '0' - test_expr '2 = 2' '1' - test_expr '2 = 4' '0' - test_expr '2 \>= 1' '1' - test_expr '2 \>= 2' '1' - test_expr '2 \>= 3' '0' - test_expr '2 \> 1' '1' - test_expr '2 \> 2' '0' + test_expr 2 \!= 5 '1' + test_expr 2 \!= 2 '0' + test_expr 2 \<= 3 '1' + test_expr 2 \<= 2 '1' + test_expr 2 \<= 1 '0' + test_expr 2 \< 3 '1' + test_expr 2 \< 2 '0' + test_expr 2 = 2 '1' + test_expr 2 = 4 '0' + test_expr 2 \>= 1 '1' + test_expr 2 \>= 2 '1' + test_expr 2 \>= 3 '0' + test_expr 2 \> 1 '1' + test_expr 2 \> 2 '0' + + test_finish } atf_test_case multiply @@ -188,8 +214,10 @@ multiply_head() { atf_set "descr" "Test the multiply operator (PR bin/12838)" } multiply_body() { - test_expr '1 \* -1' '-1' - test_expr '2 \> 1 \* 17' '0' + test_expr 1 \* -1 '-1' + test_expr 2 \> 1 \* 17 '0' + + test_finish } atf_test_case negative @@ -197,13 +225,15 @@ negative_head() { atf_set "descr" "Test the additive inverse" } negative_body() { - test_expr '-1 + 5' '4' - test_expr '- 1 + 5' 'expr: syntax error' + test_expr -1 + 5 '4' + test_expr - 1 + 5 'expr: syntax error' + + test_expr 5 + -1 '4' + test_expr 5 + - 1 'expr: syntax error' - test_expr '5 + -1' '4' - test_expr '5 + - 1' 'expr: syntax error' + test_expr 1 - -5 '6' - test_expr '1 - -5' '6' + test_finish } atf_test_case math_precedence @@ -211,16 +241,19 @@ math_precedence_head() { atf_set "descr" "More complex math test for precedence" } math_precedence_body() { - test_expr '-3 + -1 \* 4 + 3 / -6' '-7' + test_expr -3 + -1 \* 4 + 3 / -6 '-7' + + test_finish } atf_test_case precedence precedence_head() { - atf_set "descr" "Test precedence" + atf_set "descr" "Test precedence between ':' and '|'" } precedence_body() { - # This is messy but the shell escapes cause that - test_expr 'X1/2/3 : X\\\(.\*[^/]\\\)//\*[^/][^/]\*/\*$ \| . : \\\(.\\\)' '1/2' + test_expr X1/2/3 : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| . : '\(.\)' '1/2' + + test_finish } atf_test_case regex @@ -228,8 +261,9 @@ regex_head() { atf_set "descr" "Test proper () returning \1 from a regex" } regex_body() { - # This is messy but the shell escapes cause that - test_expr '1/2 : .\*/\\\(.\*\\\)' '2' + test_expr 1/2 : '.*/\(.*\)' '2' + + test_finish } atf_test_case string_length @@ -237,13 +271,15 @@ string_length_head() { atf_set "descr" "Test the string length operator" } string_length_body() { - test_expr 'length ""' '0' - test_expr 'length +' 'expr: syntax error' - test_expr 'length \!' '1' - test_expr 'length ++' '2' + test_expr length "" '0' + test_expr length + 'expr: syntax error' + test_expr length \! '1' + test_expr length ++ '2' # POSIX says "unspecified results" - test_expr 'length length' '6' + test_expr length length '6' + + test_finish } atf_init_test_cases()