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. |
30 |
test_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 |
|
39 |
atf_test_case lang |
40 |
lang_ops_head() { |
41 |
atf_set "descr" "Test that expr(1) works with non-C LANG (PR bin/2486)" |
42 |
} |
43 |
lang_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 |
|
52 |
atf_test_case overflow |
53 |
overflow_head() { |
54 |
atf_set "descr" "Test overflow cases" |
55 |
} |
56 |
overflow_body() { |
57 |
test_expr '4611686018427387904 + 4611686018427387903' \ |
58 |
'9223372036854775807' |
59 |
test_expr '4611686018427387904 + 4611686018427387904' \ |
60 |
"expr: integer overflow or underflow occurred for operation '4611686018427387904 + 4611686018427387904'" |
61 |
test_expr '4611686018427387904 - -4611686018427387904' \ |
62 |
"expr: integer overflow or underflow occurred for operation '4611686018427387904 - -4611686018427387904'" |
63 |
test_expr '-4611686018427387904 - 4611686018427387903' \ |
64 |
'-9223372036854775807' |
65 |
test_expr '-4611686018427387904 - 4611686018427387905' \ |
66 |
"expr: integer overflow or underflow occurred for operation '-4611686018427387904 - 4611686018427387905'" |
67 |
test_expr '-4611686018427387904 \* 1' '-4611686018427387904' |
68 |
test_expr '-4611686018427387904 \* -1' '4611686018427387904' |
69 |
test_expr '-4611686018427387904 \* 2' '-9223372036854775808' |
70 |
test_expr '-4611686018427387904 \* 3' \ |
71 |
"expr: integer overflow or underflow occurred for operation '-4611686018427387904 * 3'" |
72 |
test_expr '-4611686018427387904 \* -2' \ |
73 |
"expr: integer overflow or underflow occurred for operation '-4611686018427387904 * -2'" |
74 |
test_expr '4611686018427387904 \* 1' '4611686018427387904' |
75 |
test_expr '4611686018427387904 \* 2' \ |
76 |
"expr: integer overflow or underflow occurred for operation '4611686018427387904 * 2'" |
77 |
test_expr '4611686018427387904 \* 3' \ |
78 |
"expr: integer overflow or underflow occurred for operation '4611686018427387904 * 3'" |
79 |
} |
80 |
|
81 |
atf_test_case gtkmm |
82 |
gtkmm_head() { |
83 |
atf_set "descr" "Test from gtk-- configure that cause problems on old expr" |
84 |
} |
85 |
gtkmm_body() { |
86 |
test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 5' '1' |
87 |
test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 6' '0' |
88 |
test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 3 \& 5 \>= 5' '0' |
89 |
test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 2 \& 4 = 4 \& 5 \>= 5' '0' |
90 |
test_expr '3 \> 2 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 6' '1' |
91 |
test_expr '3 \> 3 \| 3 = 3 \& 4 \> 3 \| 3 = 3 \& 4 = 4 \& 5 \>= 5' '1' |
92 |
} |
93 |
|
94 |
atf_test_case colon_vs_math |
95 |
colon_vs_math_head() { |
96 |
atf_set "descr" "Basic precendence test with the : operator vs. math" |
97 |
} |
98 |
colon_vs_math_body() { |
99 |
test_expr '2 : 4 / 2' '0' |
100 |
test_expr '4 : 4 % 3' '1' |
101 |
} |
102 |
|
103 |
atf_test_case arithmetic_ops |
104 |
arithmetic_ops_head() { |
105 |
atf_set "descr" "Dangling arithemtic operator" |
106 |
} |
107 |
arithmetic_ops_body() { |
108 |
# Begin FreeBSD |
109 |
atf_expect_fail "the following testcases fail with syntax errors on FreeBSD" |
110 |
# End FreeBSD |
111 |
test_expr '.java_wrapper : /' '0' |
112 |
test_expr '4 : \*' '0' |
113 |
test_expr '4 : +' '0' |
114 |
test_expr '4 : -' '0' |
115 |
test_expr '4 : /' '0' |
116 |
test_expr '4 : %' '0' |
117 |
} |
118 |
|
119 |
atf_test_case basic_math |
120 |
basic_math_head() { |
121 |
atf_set "descr" "Basic math test" |
122 |
} |
123 |
basic_math_body() { |
124 |
test_expr '2 + 4 \* 5' '22' |
125 |
} |
126 |
|
127 |
atf_test_case basic_functional |
128 |
basic_functional_head() { |
129 |
atf_set "descr" "Basic functional tests" |
130 |
} |
131 |
basic_functional_body() { |
132 |
test_expr '2' '2' |
133 |
test_expr '-4' '-4' |
134 |
test_expr 'hello' 'hello' |
135 |
} |
136 |
|
137 |
atf_test_case compare_ops_precedence |
138 |
compare_ops_precedence_head() { |
139 |
atf_set "descr" "Compare operator precendence test" |
140 |
} |
141 |
compare_ops_precedence_body() { |
142 |
test_expr '2 \> 1 \* 17' '0' |
143 |
} |
144 |
|
145 |
atf_test_case compare_ops |
146 |
compare_ops_head() { |
147 |
atf_set "descr" "Compare operator tests" |
148 |
} |
149 |
compare_ops_body() { |
150 |
test_expr '2 \!= 5' '1' |
151 |
test_expr '2 \!= 2' '0' |
152 |
test_expr '2 \<= 3' '1' |
153 |
test_expr '2 \<= 2' '1' |
154 |
test_expr '2 \<= 1' '0' |
155 |
test_expr '2 \< 3' '1' |
156 |
test_expr '2 \< 2' '0' |
157 |
test_expr '2 = 2' '1' |
158 |
test_expr '2 = 4' '0' |
159 |
test_expr '2 \>= 1' '1' |
160 |
test_expr '2 \>= 2' '1' |
161 |
test_expr '2 \>= 3' '0' |
162 |
test_expr '2 \> 1' '1' |
163 |
test_expr '2 \> 2' '0' |
164 |
} |
165 |
|
166 |
atf_test_case multiply |
167 |
multiply_head() { |
168 |
atf_set "descr" "Test the multiply operator (PR bin/12838)" |
169 |
} |
170 |
multiply_body() { |
171 |
test_expr '1 \* -1' '-1' |
172 |
test_expr '2 \> 1 \* 17' '0' |
173 |
} |
174 |
|
175 |
atf_test_case negative |
176 |
negative_head() { |
177 |
atf_set "descr" "Test the additive inverse" |
178 |
} |
179 |
negative_body() { |
180 |
test_expr '-1 + 5' '4' |
181 |
test_expr '- 1 + 5' 'expr: syntax error' |
182 |
|
183 |
test_expr '5 + -1' '4' |
184 |
test_expr '5 + - 1' 'expr: syntax error' |
185 |
|
186 |
test_expr '1 - -5' '6' |
187 |
} |
188 |
|
189 |
atf_test_case math_precedence |
190 |
math_precedence_head() { |
191 |
atf_set "descr" "More complex math test for precedence" |
192 |
} |
193 |
math_precedence_body() { |
194 |
test_expr '-3 + -1 \* 4 + 3 / -6' '-7' |
195 |
} |
196 |
|
197 |
atf_test_case precedence |
198 |
precedence_head() { |
199 |
atf_set "descr" "Test precedence" |
200 |
} |
201 |
precedence_body() { |
202 |
# This is messy but the shell escapes cause that |
203 |
test_expr 'X1/2/3 : X\\\(.\*[^/]\\\)//\*[^/][^/]\*/\*$ \| . : \\\(.\\\)' '1/2' |
204 |
} |
205 |
|
206 |
atf_test_case regex |
207 |
regex_head() { |
208 |
atf_set "descr" "Test proper () returning \1 from a regex" |
209 |
} |
210 |
regex_body() { |
211 |
# This is messy but the shell escapes cause that |
212 |
test_expr '1/2 : .\*/\\\(.\*\\\)' '2' |
213 |
} |
214 |
|
215 |
atf_init_test_cases() |
216 |
{ |
217 |
atf_add_test_case lang |
218 |
atf_add_test_case overflow |
219 |
atf_add_test_case gtkmm |
220 |
atf_add_test_case colon_vs_math |
221 |
atf_add_test_case arithmetic_ops |
222 |
atf_add_test_case basic_math |
223 |
atf_add_test_case basic_functional |
224 |
atf_add_test_case compare_ops_precedence |
225 |
atf_add_test_case compare_ops |
226 |
atf_add_test_case multiply |
227 |
atf_add_test_case negative |
228 |
atf_add_test_case math_precedence |
229 |
atf_add_test_case precedence |
230 |
atf_add_test_case regex |
231 |
} |