1b50a997fStobias#!/bin/ksh 2*e102c3fbSschwarze# $OpenBSD: expr.sh,v 1.2 2018/03/31 15:52:31 schwarze Exp $ 3b50a997fStobias 4b50a997fStobias: ${EXPR=expr} 5b50a997fStobias 6b50a997fStobiasfunction test_expr { 7b50a997fStobias #echo "Testing: `eval echo $1`" 8b50a997fStobias res=`eval $EXPR $1 2>&1` 9b50a997fStobias if [ "$res" != "$2" ]; then 10b50a997fStobias echo "Expected $2, got $res from expression: `eval echo $1`" 11b50a997fStobias exit 1 12b50a997fStobias fi 13b50a997fStobias} 14b50a997fStobias 15b50a997fStobias# The first arg will get eval'd so escape any meta characters 16b50a997fStobias# The 2nd arg is an expected string/response from expr for that op. 17b50a997fStobias 18b50a997fStobias# Test overflow cases 19b50a997fStobiastest_expr '4611686018427387904 + 4611686018427387903' '9223372036854775807' 20b50a997fStobiastest_expr '4611686018427387904 + 4611686018427387904' "expr: overflow" 21*e102c3fbSschwarzetest_expr '-4611686018427387904 + -4611686018427387904' '-9223372036854775808' 22*e102c3fbSschwarzetest_expr '-4611686018427387904 + -4611686018427387905' 'expr: overflow' 23*e102c3fbSschwarzetest_expr '4611686018427387904 - -4611686018427387903' '9223372036854775807' 24b50a997fStobiastest_expr '4611686018427387904 - -4611686018427387904' "expr: overflow" 25b50a997fStobiastest_expr '-4611686018427387904 - 4611686018427387903' '-9223372036854775807' 26*e102c3fbSschwarzetest_expr '-4611686018427387904 - 4611686018427387904' '-9223372036854775808' 27b50a997fStobiastest_expr '-4611686018427387904 - 4611686018427387905' "expr: overflow" 28b50a997fStobiastest_expr '-4611686018427387904 \* 1' '-4611686018427387904' 29b50a997fStobiastest_expr '-4611686018427387904 \* -1' '4611686018427387904' 30b50a997fStobiastest_expr '-4611686018427387904 \* 2' '-9223372036854775808' 31b50a997fStobiastest_expr '-4611686018427387904 \* 3' "expr: overflow" 32b50a997fStobiastest_expr '-4611686018427387904 \* -2' "expr: overflow" 33b50a997fStobiastest_expr '4611686018427387904 \* 1' '4611686018427387904' 34b50a997fStobiastest_expr '4611686018427387904 \* 2' "expr: overflow" 35b50a997fStobiastest_expr '4611686018427387904 \* 3' "expr: overflow" 36*e102c3fbSschwarzetest_expr '4611686018427387903 \* 2' '9223372036854775806' 37*e102c3fbSschwarzetest_expr '-4611686018427387905 \* 2' 'expr: overflow' 38*e102c3fbSschwarzetest_expr '4611686018427387904 / 0' 'expr: division by zero' 39*e102c3fbSschwarzetest_expr '-9223372036854775808 / -1' 'expr: overflow' 40*e102c3fbSschwarzetest_expr '-9223372036854775808 % -1' '0' 41b50a997fStobias 42b50a997fStobias# Test from gtk-- configure that cause problems on old expr 43b50a997fStobiastest_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 5' '1' 44b50a997fStobiastest_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 6' '0' 45b50a997fStobiastest_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 3 \& 5 \>= 5' '0' 46b50a997fStobiastest_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 2 \& 4 = 4 \& 5 \>= 5' '0' 47b50a997fStobiastest_expr '3 \> 2 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 6' '1' 48b50a997fStobiastest_expr '3 \> 3 \| 3 = 3 \& 4 \> 3 \| 3 = 3 \& 4 = 4 \& 5 \>= 5' '1' 49b50a997fStobias 50b50a997fStobias# Basic precendence test with the : operator vs. math 51b50a997fStobiastest_expr '2 : 4 / 2' '0' 52b50a997fStobiastest_expr '4 : 4 % 3' '1' 53b50a997fStobias 54b50a997fStobias# Dangling arithemtic operator 55b50a997fStobiastest_expr '.java_wrapper : /' '0' 56b50a997fStobiastest_expr '4 : \*' '0' 57b50a997fStobiastest_expr '4 : +' '0' 58b50a997fStobiastest_expr '4 : -' '0' 59b50a997fStobiastest_expr '4 : /' '0' 60b50a997fStobiastest_expr '4 : %' '0' 61b50a997fStobias 62b50a997fStobias# Basic math test 63b50a997fStobiastest_expr '2 + 4 \* 5' '22' 64b50a997fStobias 65b50a997fStobias# Basic functional tests 66b50a997fStobiastest_expr '2' '2' 67b50a997fStobiastest_expr '-4' '-4' 68b50a997fStobiastest_expr 'hello' 'hello' 69b50a997fStobias 70b50a997fStobias# Compare operator precendence test 71b50a997fStobiastest_expr '2 \> 1 \* 17' '0' 72b50a997fStobias 73b50a997fStobias# Compare operator tests 74b50a997fStobiastest_expr '2 \!= 5' '1' 75b50a997fStobiastest_expr '2 \!= 2' '0' 76b50a997fStobiastest_expr '2 \<= 3' '1' 77b50a997fStobiastest_expr '2 \<= 2' '1' 78b50a997fStobiastest_expr '2 \<= 1' '0' 79b50a997fStobiastest_expr '2 \< 3' '1' 80b50a997fStobiastest_expr '2 \< 2' '0' 81b50a997fStobiastest_expr '2 = 2' '1' 82b50a997fStobiastest_expr '2 = 4' '0' 83b50a997fStobiastest_expr '2 \>= 1' '1' 84b50a997fStobiastest_expr '2 \>= 2' '1' 85b50a997fStobiastest_expr '2 \>= 3' '0' 86b50a997fStobiastest_expr '2 \> 1' '1' 87b50a997fStobiastest_expr '2 \> 2' '0' 88b50a997fStobias 89b50a997fStobias# Known failure once 90b50a997fStobiastest_expr '1 \* -1' '-1' 91b50a997fStobias 92b50a997fStobias# Test a known case that should fail 93b50a997fStobiastest_expr '- 1 + 5' 'expr: syntax error' 94b50a997fStobias 95b50a997fStobias# Double check negative numbers 96b50a997fStobiastest_expr '1 - -5' '6' 97b50a997fStobias 98b50a997fStobias# More complex math test for precedence 99b50a997fStobiastest_expr '-3 + -1 \* 4 + 3 / -6' '-7' 100b50a997fStobias 101b50a997fStobias# The next two are messy but the shell escapes cause that. 102b50a997fStobias# Test precendence 103b50a997fStobiastest_expr 'X1/2/3 : X\\\(.\*[^/]\\\)//\*[^/][^/]\*/\*$ \| . : \\\(.\\\)' '1/2' 104b50a997fStobias 105b50a997fStobias# Test proper () returning \1 from a regex 106b50a997fStobiastest_expr '1/2 : .\*/\\\(.\*\\\)' '2' 107b50a997fStobias 108b50a997fStobias# Test integer edge cases 109*e102c3fbSschwarzetest_expr '9223372036854775806 + 0' '9223372036854775806' 110*e102c3fbSschwarzetest_expr '9223372036854775806 + 1' '9223372036854775807' 111*e102c3fbSschwarzetest_expr '9223372036854775807 + 0' '9223372036854775807' 112*e102c3fbSschwarzetest_expr '9223372036854775806 + 2' 'expr: overflow' 113*e102c3fbSschwarzetest_expr '9223372036854775807 + 1' 'expr: overflow' 114*e102c3fbSschwarzetest_expr '9223372036854775808 + 0' \ 115*e102c3fbSschwarze 'expr: number "9223372036854775808" is too large' 116b50a997fStobiastest_expr '-9223372036854775807 + 0' '-9223372036854775807' 117b50a997fStobiastest_expr '-9223372036854775807 - 1' '-9223372036854775808' 118b50a997fStobiastest_expr '-9223372036854775808 + 0' '-9223372036854775808' 119b50a997fStobiastest_expr '-9223372036854775807 - 2' 'expr: overflow' 120b50a997fStobiastest_expr '-9223372036854775808 - 1' 'expr: overflow' 121b50a997fStobiastest_expr '-9223372036854775809 + 0' \ 122b50a997fStobias 'expr: number "-9223372036854775809" is too small' 123*e102c3fbSschwarzetest_expr '0 - -9223372036854775807' '9223372036854775807' 124*e102c3fbSschwarzetest_expr '1 - -9223372036854775806' '9223372036854775807' 125*e102c3fbSschwarzetest_expr '0 - -9223372036854775808' 'expr: overflow' 126*e102c3fbSschwarzetest_expr '1 - -9223372036854775807' 'expr: overflow' 127b50a997fStobiastest_expr '-36854775808 - \( -9223372036854775807 - 1 \)' '9223372000000000000' 128b50a997fStobias 129b50a997fStobiasexit 0 130