1272343Sngie# $NetBSD: t_expr.sh,v 1.3 2012/03/27 07:23:06 jruoho Exp $
2272343Sngie#
3272343Sngie# Copyright (c) 2007 The NetBSD Foundation, Inc.
4272343Sngie# All rights reserved.
5272343Sngie#
6272343Sngie# Redistribution and use in source and binary forms, with or without
7272343Sngie# modification, are permitted provided that the following conditions
8272343Sngie# are met:
9272343Sngie# 1. Redistributions of source code must retain the above copyright
10272343Sngie#    notice, this list of conditions and the following disclaimer.
11272343Sngie# 2. Redistributions in binary form must reproduce the above copyright
12272343Sngie#    notice, this list of conditions and the following disclaimer in the
13272343Sngie#    documentation and/or other materials provided with the distribution.
14272343Sngie#
15272343Sngie# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16272343Sngie# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17272343Sngie# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18272343Sngie# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19272343Sngie# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20272343Sngie# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21272343Sngie# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22272343Sngie# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23272343Sngie# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24272343Sngie# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25272343Sngie# POSSIBILITY OF SUCH DAMAGE.
26272343Sngie#
27272343Sngie
28272343Sngie# The first arg will get eval'd so escape any meta characters
29272343Sngie# The 2nd arg is an expected string/response from expr for that op.
30272343Sngietest_expr() {
31272343Sngie	echo "Expression '${1}', expecting '${2}'"
32272343Sngie	res=`eval expr $1 2>&1`
33272343Sngie	if [ "$res" != "$2" ]; then
34272343Sngie		atf_fail "Expected $2, got $res from expression: " \
35272343Sngie		         "`eval echo $1`"
36272343Sngie	fi
37272343Sngie}
38272343Sngie
39272343Sngieatf_test_case lang
40272343Sngielang_ops_head() {
41272343Sngie	atf_set "descr" "Test that expr(1) works with non-C LANG (PR bin/2486)"
42272343Sngie}
43272343Sngielang_body() {
44272343Sngie
45272343Sngie	export LANG=nonexistent
46272343Sngie	atf_check -s exit:0 -o inline:"21\n" -e empty -x "expr 10 + 11"
47272343Sngie
48272343Sngie	export LANG=ru_RU.KOI8-R
49272343Sngie	atf_check -s exit:0 -o inline:"21\n" -e empty -x "expr 10 + 11"
50272343Sngie}
51272343Sngie
52272343Sngieatf_test_case overflow
53272343Sngieoverflow_head() {
54272343Sngie	atf_set "descr" "Test overflow cases"
55272343Sngie}
56272343Sngieoverflow_body() {
57272343Sngie	test_expr '4611686018427387904 + 4611686018427387903' \
58272343Sngie	          '9223372036854775807'
59272343Sngie	test_expr '4611686018427387904 + 4611686018427387904' \
60272343Sngie	          "expr: integer overflow or underflow occurred for operation '4611686018427387904 + 4611686018427387904'"
61272343Sngie	test_expr '4611686018427387904 - -4611686018427387904' \
62272343Sngie	          "expr: integer overflow or underflow occurred for operation '4611686018427387904 - -4611686018427387904'"
63272343Sngie	test_expr '-4611686018427387904 - 4611686018427387903' \
64272343Sngie	          '-9223372036854775807'
65272343Sngie	test_expr '-4611686018427387904 - 4611686018427387905' \
66272343Sngie	          "expr: integer overflow or underflow occurred for operation '-4611686018427387904 - 4611686018427387905'"
67272343Sngie	test_expr '-4611686018427387904 \* 1' '-4611686018427387904'
68272343Sngie	test_expr '-4611686018427387904 \* -1' '4611686018427387904'
69272343Sngie	test_expr '-4611686018427387904 \* 2' '-9223372036854775808'
70272343Sngie	test_expr '-4611686018427387904 \* 3' \
71272343Sngie	          "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * 3'"
72272343Sngie	test_expr '-4611686018427387904 \* -2' \
73272343Sngie	          "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * -2'"
74272343Sngie	test_expr '4611686018427387904 \* 1' '4611686018427387904'
75272343Sngie	test_expr '4611686018427387904 \* 2' \
76272343Sngie	          "expr: integer overflow or underflow occurred for operation '4611686018427387904 * 2'"
77272343Sngie	test_expr '4611686018427387904 \* 3' \
78272343Sngie	          "expr: integer overflow or underflow occurred for operation '4611686018427387904 * 3'"
79272343Sngie}
80272343Sngie
81272343Sngieatf_test_case gtkmm
82272343Sngiegtkmm_head() {
83272343Sngie	atf_set "descr" "Test from gtk-- configure that cause problems on old expr"
84272343Sngie}
85272343Sngiegtkmm_body() {
86272343Sngie	test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 5' '1'
87272343Sngie	test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 6' '0'
88272343Sngie	test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 3 \& 5 \>= 5' '0'
89272343Sngie	test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 2 \& 4 = 4 \& 5 \>= 5' '0'
90272343Sngie	test_expr '3 \> 2 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 6' '1'
91272343Sngie	test_expr '3 \> 3 \| 3 = 3 \& 4 \> 3 \| 3 = 3 \& 4 = 4 \& 5 \>= 5' '1'
92272343Sngie}
93272343Sngie
94272343Sngieatf_test_case colon_vs_math
95272343Sngiecolon_vs_math_head() {
96272343Sngie	atf_set "descr" "Basic precendence test with the : operator vs. math"
97272343Sngie}
98272343Sngiecolon_vs_math_body() {
99272343Sngie	test_expr '2 : 4 / 2' '0'
100272343Sngie	test_expr '4 : 4 % 3' '1'
101272343Sngie}
102272343Sngie
103272343Sngieatf_test_case arithmetic_ops
104272343Sngiearithmetic_ops_head() {
105272343Sngie	atf_set "descr" "Dangling arithemtic operator"
106272343Sngie}
107272343Sngiearithmetic_ops_body() {
108276671Sngie	# Begin FreeBSD
109276671Sngie	atf_expect_fail "the following testcases fail with syntax errors on FreeBSD"
110276671Sngie	# End FreeBSD
111272343Sngie	test_expr '.java_wrapper : /' '0'
112272343Sngie	test_expr '4 : \*' '0'
113272343Sngie	test_expr '4 : +' '0'
114272343Sngie	test_expr '4 : -' '0'
115272343Sngie	test_expr '4 : /' '0'
116272343Sngie	test_expr '4 : %' '0'
117272343Sngie}
118272343Sngie
119272343Sngieatf_test_case basic_math
120272343Sngiebasic_math_head() {
121272343Sngie	atf_set "descr" "Basic math test"
122272343Sngie}
123272343Sngiebasic_math_body() {
124272343Sngie	test_expr '2 + 4 \* 5' '22'
125272343Sngie}
126272343Sngie
127272343Sngieatf_test_case basic_functional
128272343Sngiebasic_functional_head() {
129272343Sngie	atf_set "descr" "Basic functional tests"
130272343Sngie}
131272343Sngiebasic_functional_body() {
132272343Sngie	test_expr '2' '2'
133272343Sngie	test_expr '-4' '-4'
134272343Sngie	test_expr 'hello' 'hello'
135272343Sngie}
136272343Sngie
137272343Sngieatf_test_case compare_ops_precedence
138272343Sngiecompare_ops_precedence_head() {
139272343Sngie	atf_set "descr" "Compare operator precendence test"
140272343Sngie}
141272343Sngiecompare_ops_precedence_body() {
142272343Sngie	test_expr '2 \> 1 \* 17' '0'
143272343Sngie}
144272343Sngie
145272343Sngieatf_test_case compare_ops
146272343Sngiecompare_ops_head() {
147272343Sngie	atf_set "descr" "Compare operator tests"
148272343Sngie}
149272343Sngiecompare_ops_body() {
150272343Sngie	test_expr '2 \!= 5' '1'
151272343Sngie	test_expr '2 \!= 2' '0'
152272343Sngie	test_expr '2 \<= 3' '1'
153272343Sngie	test_expr '2 \<= 2' '1'
154272343Sngie	test_expr '2 \<= 1' '0'
155272343Sngie	test_expr '2 \< 3' '1'
156272343Sngie	test_expr '2 \< 2' '0'
157272343Sngie	test_expr '2 = 2' '1'
158272343Sngie	test_expr '2 = 4' '0'
159272343Sngie	test_expr '2 \>= 1' '1'
160272343Sngie	test_expr '2 \>= 2' '1'
161272343Sngie	test_expr '2 \>= 3' '0'
162272343Sngie	test_expr '2 \> 1' '1'
163272343Sngie	test_expr '2 \> 2' '0'
164272343Sngie}
165272343Sngie
166272343Sngieatf_test_case multiply
167272343Sngiemultiply_head() {
168272343Sngie	atf_set "descr" "Test the multiply operator (PR bin/12838)"
169272343Sngie}
170272343Sngiemultiply_body() {
171272343Sngie	test_expr '1 \* -1' '-1'
172272343Sngie	test_expr '2 \> 1 \* 17' '0'
173272343Sngie}
174272343Sngie
175272343Sngieatf_test_case negative
176272343Sngienegative_head() {
177272343Sngie	atf_set "descr" "Test the additive inverse"
178272343Sngie}
179272343Sngienegative_body() {
180272343Sngie	test_expr '-1 + 5' '4'
181272343Sngie	test_expr '- 1 + 5' 'expr: syntax error'
182272343Sngie
183272343Sngie	test_expr '5 + -1' '4'
184272343Sngie	test_expr '5 + - 1' 'expr: syntax error'
185272343Sngie
186272343Sngie	test_expr '1 - -5' '6'
187272343Sngie}
188272343Sngie
189272343Sngieatf_test_case math_precedence
190272343Sngiemath_precedence_head() {
191272343Sngie	atf_set "descr" "More complex math test for precedence"
192272343Sngie}
193272343Sngiemath_precedence_body() {
194272343Sngie	test_expr '-3 + -1 \* 4 + 3 / -6' '-7'
195272343Sngie}
196272343Sngie
197272343Sngieatf_test_case precedence
198272343Sngieprecedence_head() {
199272343Sngie	atf_set "descr" "Test precedence"
200272343Sngie}
201272343Sngieprecedence_body() {
202272343Sngie	# This is messy but the shell escapes cause that
203272343Sngie	test_expr 'X1/2/3 : X\\\(.\*[^/]\\\)//\*[^/][^/]\*/\*$ \| . : \\\(.\\\)' '1/2'
204272343Sngie}
205272343Sngie
206272343Sngieatf_test_case regex
207272343Sngieregex_head() {
208272343Sngie	atf_set "descr" "Test proper () returning \1 from a regex"
209272343Sngie}
210272343Sngieregex_body() {
211272343Sngie	# This is messy but the shell escapes cause that
212272343Sngie	test_expr '1/2 : .\*/\\\(.\*\\\)' '2'
213272343Sngie}
214272343Sngie
215272343Sngieatf_init_test_cases()
216272343Sngie{
217272343Sngie	atf_add_test_case lang
218272343Sngie	atf_add_test_case overflow
219272343Sngie	atf_add_test_case gtkmm
220272343Sngie	atf_add_test_case colon_vs_math
221272343Sngie	atf_add_test_case arithmetic_ops
222272343Sngie	atf_add_test_case basic_math
223272343Sngie	atf_add_test_case basic_functional
224272343Sngie	atf_add_test_case compare_ops_precedence
225272343Sngie	atf_add_test_case compare_ops
226272343Sngie	atf_add_test_case multiply
227272343Sngie	atf_add_test_case negative
228272343Sngie	atf_add_test_case math_precedence
229272343Sngie	atf_add_test_case precedence
230272343Sngie	atf_add_test_case regex
231272343Sngie}
232