summaryrefslogtreecommitdiff
path: root/regress/bin/expr/expr.sh
blob: da8bf0f13675e5a00fd3b46c331e494103fc3dde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/ksh
# $OpenBSD: expr.sh,v 1.1 2018/03/31 14:50:56 tobias Exp $

: ${EXPR=expr}

function test_expr {
	#echo "Testing: `eval echo $1`"
	res=`eval $EXPR $1 2>&1`
	if [ "$res" != "$2" ]; then
		echo "Expected $2, got $res from expression: `eval echo $1`"
		exit 1
	fi
}
	
# 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.

# Test overflow cases
test_expr '4611686018427387904 + 4611686018427387903' '9223372036854775807'
test_expr '4611686018427387904 + 4611686018427387904' "expr: overflow"
test_expr '4611686018427387904 - -4611686018427387904' "expr: overflow"
test_expr '-4611686018427387904 - 4611686018427387903' '-9223372036854775807'
test_expr '-4611686018427387904 - 4611686018427387905' "expr: overflow"
test_expr '-4611686018427387904 \* 1' '-4611686018427387904'
test_expr '-4611686018427387904 \* -1' '4611686018427387904'
test_expr '-4611686018427387904 \* 2' '-9223372036854775808'
test_expr '-4611686018427387904 \* 3' "expr: overflow"
test_expr '-4611686018427387904 \* -2' "expr: overflow"
test_expr '4611686018427387904 \* 1' '4611686018427387904'
test_expr '4611686018427387904 \* 2' "expr: overflow"
test_expr '4611686018427387904 \* 3' "expr: overflow"

# Test from gtk-- configure that cause problems on old expr
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'

# Basic precendence test with the : operator vs. math
test_expr '2 : 4 / 2' '0'
test_expr '4 : 4 % 3' '1'

# Dangling arithemtic operator
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'

# Basic math test
test_expr '2 + 4 \* 5' '22'

# Basic functional tests
test_expr '2' '2'
test_expr '-4' '-4'
test_expr 'hello' 'hello'

# Compare operator precendence test
test_expr '2 \> 1 \* 17' '0'

# Compare operator tests
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'

# Known failure once
test_expr '1 \* -1' '-1'

# Test a known case that should fail
test_expr '- 1 + 5' 'expr: syntax error'

# Double check negative numbers
test_expr '1 - -5' '6'

# More complex math test for precedence
test_expr '-3 + -1 \* 4 + 3 / -6' '-7'

# The next two are messy but the shell escapes cause that. 
# Test precendence
test_expr 'X1/2/3 : X\\\(.\*[^/]\\\)//\*[^/][^/]\*/\*$ \| . : \\\(.\\\)' '1/2'

# Test proper () returning \1 from a regex
test_expr '1/2 : .\*/\\\(.\*\\\)' '2'

# Test integer edge cases
test_expr '-9223372036854775807 + 0' '-9223372036854775807'
test_expr '-9223372036854775807 - 1' '-9223372036854775808'
test_expr '-9223372036854775808 + 0' '-9223372036854775808'
test_expr '-9223372036854775807 - 2' 'expr: overflow'
test_expr '-9223372036854775808 - 1' 'expr: overflow'
test_expr '-9223372036854775809 + 0' \
    'expr: number "-9223372036854775809" is too small'
test_expr '-36854775808 - \( -9223372036854775807 - 1 \)' '9223372000000000000'

exit 0