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