t_expr.sh revision 277549
1# $NetBSD: t_expr.sh,v 1.3 2012/03/27 07:23:06 jruoho Exp $
2#
3# Copyright (c) 2007 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25# POSSIBILITY OF SUCH DAMAGE.
26#
27
28# The first arg will get eval'd so escape any meta characters
29# The 2nd arg is an expected string/response from expr for that op.
30test_expr() {
31	echo "Expression '${1}', expecting '${2}'"
32	res=`eval expr $1 2>&1`
33	if [ "$res" != "$2" ]; then
34		atf_fail "Expected $2, got $res from expression: " \
35		         "`eval echo $1`"
36	fi
37}
38
39atf_test_case lang
40lang_ops_head() {
41	atf_set "descr" "Test that expr(1) works with non-C LANG (PR bin/2486)"
42}
43lang_body() {
44
45	export LANG=nonexistent
46	atf_check -s exit:0 -o inline:"21\n" -e empty -x "expr 10 + 11"
47
48	export LANG=ru_RU.KOI8-R
49	atf_check -s exit:0 -o inline:"21\n" -e empty -x "expr 10 + 11"
50}
51
52atf_test_case overflow
53overflow_head() {
54	atf_set "descr" "Test overflow cases"
55}
56overflow_body() {
57	# Begin FreeBSD
58	atf_expect_fail "FreeBSD's expr does not check overflow to the same degree NetBSD's expr does; see bug 196867 for more details"
59	# End FreeBSD
60	test_expr '4611686018427387904 + 4611686018427387903' \
61	          '9223372036854775807'
62	test_expr '4611686018427387904 + 4611686018427387904' \
63	          "expr: integer overflow or underflow occurred for operation '4611686018427387904 + 4611686018427387904'"
64	test_expr '4611686018427387904 - -4611686018427387904' \
65	          "expr: integer overflow or underflow occurred for operation '4611686018427387904 - -4611686018427387904'"
66	test_expr '-4611686018427387904 - 4611686018427387903' \
67	          '-9223372036854775807'
68	test_expr '-4611686018427387904 - 4611686018427387905' \
69	          "expr: integer overflow or underflow occurred for operation '-4611686018427387904 - 4611686018427387905'"
70	test_expr '-4611686018427387904 \* 1' '-4611686018427387904'
71	test_expr '-4611686018427387904 \* -1' '4611686018427387904'
72	test_expr '-4611686018427387904 \* 2' '-9223372036854775808'
73	test_expr '-4611686018427387904 \* 3' \
74	          "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * 3'"
75	test_expr '-4611686018427387904 \* -2' \
76	          "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * -2'"
77	test_expr '4611686018427387904 \* 1' '4611686018427387904'
78	test_expr '4611686018427387904 \* 2' \
79	          "expr: integer overflow or underflow occurred for operation '4611686018427387904 * 2'"
80	test_expr '4611686018427387904 \* 3' \
81	          "expr: integer overflow or underflow occurred for operation '4611686018427387904 * 3'"
82}
83
84atf_test_case gtkmm
85gtkmm_head() {
86	atf_set "descr" "Test from gtk-- configure that cause problems on old expr"
87}
88gtkmm_body() {
89	test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 5' '1'
90	test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 6' '0'
91	test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 3 \& 5 \>= 5' '0'
92	test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 2 \& 4 = 4 \& 5 \>= 5' '0'
93	test_expr '3 \> 2 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 6' '1'
94	test_expr '3 \> 3 \| 3 = 3 \& 4 \> 3 \| 3 = 3 \& 4 = 4 \& 5 \>= 5' '1'
95}
96
97atf_test_case colon_vs_math
98colon_vs_math_head() {
99	atf_set "descr" "Basic precendence test with the : operator vs. math"
100}
101colon_vs_math_body() {
102	test_expr '2 : 4 / 2' '0'
103	test_expr '4 : 4 % 3' '1'
104}
105
106atf_test_case arithmetic_ops
107arithmetic_ops_head() {
108	atf_set "descr" "Dangling arithemtic operator"
109}
110arithmetic_ops_body() {
111	# Begin FreeBSD
112	atf_expect_fail "the following testcases fail with syntax errors on FreeBSD"
113	# End FreeBSD
114	test_expr '.java_wrapper : /' '0'
115	test_expr '4 : \*' '0'
116	test_expr '4 : +' '0'
117	test_expr '4 : -' '0'
118	test_expr '4 : /' '0'
119	test_expr '4 : %' '0'
120}
121
122atf_test_case basic_math
123basic_math_head() {
124	atf_set "descr" "Basic math test"
125}
126basic_math_body() {
127	test_expr '2 + 4 \* 5' '22'
128}
129
130atf_test_case basic_functional
131basic_functional_head() {
132	atf_set "descr" "Basic functional tests"
133}
134basic_functional_body() {
135	test_expr '2' '2'
136	test_expr '-4' '-4'
137	test_expr 'hello' 'hello'
138}
139
140atf_test_case compare_ops_precedence
141compare_ops_precedence_head() {
142	atf_set "descr" "Compare operator precendence test"
143}
144compare_ops_precedence_body() {
145	test_expr '2 \> 1 \* 17' '0'
146}
147
148atf_test_case compare_ops
149compare_ops_head() {
150	atf_set "descr" "Compare operator tests"
151}
152compare_ops_body() {
153	test_expr '2 \!= 5' '1'
154	test_expr '2 \!= 2' '0'
155	test_expr '2 \<= 3' '1'
156	test_expr '2 \<= 2' '1'
157	test_expr '2 \<= 1' '0'
158	test_expr '2 \< 3' '1'
159	test_expr '2 \< 2' '0'
160	test_expr '2 = 2' '1'
161	test_expr '2 = 4' '0'
162	test_expr '2 \>= 1' '1'
163	test_expr '2 \>= 2' '1'
164	test_expr '2 \>= 3' '0'
165	test_expr '2 \> 1' '1'
166	test_expr '2 \> 2' '0'
167}
168
169atf_test_case multiply
170multiply_head() {
171	atf_set "descr" "Test the multiply operator (PR bin/12838)"
172}
173multiply_body() {
174	test_expr '1 \* -1' '-1'
175	test_expr '2 \> 1 \* 17' '0'
176}
177
178atf_test_case negative
179negative_head() {
180	atf_set "descr" "Test the additive inverse"
181}
182negative_body() {
183	test_expr '-1 + 5' '4'
184	test_expr '- 1 + 5' 'expr: syntax error'
185
186	test_expr '5 + -1' '4'
187	test_expr '5 + - 1' 'expr: syntax error'
188
189	test_expr '1 - -5' '6'
190}
191
192atf_test_case math_precedence
193math_precedence_head() {
194	atf_set "descr" "More complex math test for precedence"
195}
196math_precedence_body() {
197	test_expr '-3 + -1 \* 4 + 3 / -6' '-7'
198}
199
200atf_test_case precedence
201precedence_head() {
202	atf_set "descr" "Test precedence"
203}
204precedence_body() {
205	# This is messy but the shell escapes cause that
206	test_expr 'X1/2/3 : X\\\(.\*[^/]\\\)//\*[^/][^/]\*/\*$ \| . : \\\(.\\\)' '1/2'
207}
208
209atf_test_case regex
210regex_head() {
211	atf_set "descr" "Test proper () returning \1 from a regex"
212}
213regex_body() {
214	# This is messy but the shell escapes cause that
215	test_expr '1/2 : .\*/\\\(.\*\\\)' '2'
216}
217
218atf_init_test_cases()
219{
220	atf_add_test_case lang
221	atf_add_test_case overflow
222	atf_add_test_case gtkmm
223	atf_add_test_case colon_vs_math
224	atf_add_test_case arithmetic_ops
225	atf_add_test_case basic_math
226	atf_add_test_case basic_functional
227	atf_add_test_case compare_ops_precedence
228	atf_add_test_case compare_ops
229	atf_add_test_case multiply
230	atf_add_test_case negative
231	atf_add_test_case math_precedence
232	atf_add_test_case precedence
233	atf_add_test_case regex
234}
235