t_expr.sh revision 1.12 1 # $NetBSD: t_expr.sh,v 1.12 2025/03/15 14:33:39 rillig 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 : "${expr_prog:=expr}"
29
30 # usage: test_expr operand ... result|error
31 test_expr() {
32 i=1
33 while [ $((i++)) -lt $# ]; do
34 set -- "$@" "$1"
35 shift
36 done
37 expected="$1"
38 shift
39
40 # shellcheck disable=SC2003
41 actual=$("$expr_prog" "$@" 2>&1 || :)
42
43 printf "%s => '%s'\n" "$*" "$expected" >> expected
44 printf "%s => '%s'\n" "$*" "$actual" >> actual
45 }
46
47 test_finish() {
48 atf_check -o file:expected cat actual
49 }
50
51 atf_test_case lang
52 lang_head() {
53 atf_set "descr" "Test that expr(1) works with non-C LANG (PR bin/2486)"
54 }
55 lang_body() {
56
57 export LANG=nonexistent
58 atf_check -s exit:0 -o inline:"21\n" -e empty -x "expr 10 + 11"
59
60 export LANG=ru_RU.KOI8-R
61 atf_check -s exit:0 -o inline:"21\n" -e empty -x "expr 10 + 11"
62 }
63
64 atf_test_case overflow
65 overflow_head() {
66 atf_set "descr" "Test overflow cases"
67 }
68 overflow_body() {
69 test_expr 4611686018427387904 + 4611686018427387903 \
70 '9223372036854775807'
71 test_expr 4611686018427387904 + 4611686018427387904 \
72 "expr: integer overflow or underflow occurred for operation '4611686018427387904 + 4611686018427387904'"
73 test_expr 4611686018427387904 - -4611686018427387904 \
74 "expr: integer overflow or underflow occurred for operation '4611686018427387904 - -4611686018427387904'"
75 test_expr -4611686018427387904 - 4611686018427387903 \
76 '-9223372036854775807'
77 test_expr -4611686018427387904 - 4611686018427387905 \
78 "expr: integer overflow or underflow occurred for operation '-4611686018427387904 - 4611686018427387905'"
79 test_expr -4611686018427387904 \* 1 '-4611686018427387904'
80 test_expr -4611686018427387904 \* -1 '4611686018427387904'
81 test_expr -4611686018427387904 \* 2 '-9223372036854775808'
82 test_expr -4611686018427387904 \* 3 \
83 "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * 3'"
84 test_expr -4611686018427387904 \* -2 \
85 "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * -2'"
86 test_expr 4611686018427387904 \* 1 '4611686018427387904'
87 test_expr 4611686018427387904 \* 2 \
88 "expr: integer overflow or underflow occurred for operation '4611686018427387904 * 2'"
89 test_expr 4611686018427387904 \* 3 \
90 "expr: integer overflow or underflow occurred for operation '4611686018427387904 * 3'"
91 test_expr -9223372036854775808 % -1 \
92 "expr: integer overflow or underflow occurred for operation '-9223372036854775808 % -1'"
93 test_expr -9223372036854775808 / -1 \
94 "expr: integer overflow or underflow occurred for operation '-9223372036854775808 / -1'"
95 test_expr 0 + -9223372036854775808 '-9223372036854775808'
96 test_expr 0 + -1 '-1'
97 test_expr 0 + 0 '0'
98 test_expr 0 + 1 '1'
99 test_expr 0 + 9223372036854775807 '9223372036854775807'
100 test_expr -9223372036854775808 + 0 '-9223372036854775808'
101 test_expr 9223372036854775807 + 0 '9223372036854775807'
102 test_expr 4611686018427387904 \* -1 '-4611686018427387904'
103 test_expr 4611686018427387904 \* -2 '-9223372036854775808'
104 test_expr 4611686018427387904 \* -3 \
105 "expr: integer overflow or underflow occurred for operation '4611686018427387904 * -3'"
106 test_expr -4611686018427387904 \* -1 '4611686018427387904'
107 test_expr -4611686018427387904 \* -2 \
108 "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * -2'"
109 test_expr -4611686018427387904 \* -3 \
110 "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * -3'"
111 test_expr 0 \* -1 '0'
112 test_expr 0 \* 0 '0'
113 test_expr 0 \* 1 '0'
114
115 test_finish
116 }
117
118 atf_test_case gtkmm
119 gtkmm_head() {
120 atf_set "descr" "Tests from gtk-- configure that cause problems on old expr"
121 }
122 gtkmm_body() {
123 test_expr 3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 5 '1'
124 test_expr 3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 6 '0'
125 test_expr 3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 3 \& 5 \>= 5 '0'
126 test_expr 3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 2 \& 4 = 4 \& 5 \>= 5 '0'
127 test_expr 3 \> 2 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 6 '1'
128 test_expr 3 \> 3 \| 3 = 3 \& 4 \> 3 \| 3 = 3 \& 4 = 4 \& 5 \>= 5 '1'
129
130 test_finish
131 }
132
133 atf_test_case colon_vs_math
134 colon_vs_math_head() {
135 atf_set "descr" "Basic precendence test with the : operator vs. math"
136 }
137 colon_vs_math_body() {
138 test_expr 2 : 4 / 2 '0'
139 test_expr 4 : 4 % 3 '1'
140
141 test_finish
142 }
143
144 atf_test_case arithmetic_ops
145 arithmetic_ops_head() {
146 atf_set "descr" "Dangling arithmetic operator"
147 }
148 arithmetic_ops_body() {
149 test_expr .java_wrapper : / '0'
150 test_expr 4 : \* '0'
151 test_expr 4 : + '0'
152 test_expr 4 : - '0'
153 test_expr 4 : / '0'
154 test_expr 4 : % '0'
155
156 test_finish
157 }
158
159 atf_test_case basic_math
160 basic_math_head() {
161 atf_set "descr" "Basic math test"
162 }
163 basic_math_body() {
164 test_expr 2 + 4 \* 5 '22'
165
166 test_finish
167 }
168
169 atf_test_case basic_functional
170 basic_functional_head() {
171 atf_set "descr" "Basic functional tests"
172 }
173 basic_functional_body() {
174 test_expr 2 '2'
175 test_expr -4 '-4'
176 test_expr hello 'hello'
177 test_expr -- double-dash 'double-dash'
178 test_expr -- -- -- six-dashes 'expr: syntax error'
179 test_expr 3 -- + 4 'expr: syntax error'
180
181 test_finish
182 }
183
184 atf_test_case compare_ops_precedence
185 compare_ops_precedence_head() {
186 atf_set "descr" "Compare operator precendence test"
187 }
188 compare_ops_precedence_body() {
189 test_expr 2 \> 1 \* 17 '0'
190
191 test_finish
192 }
193
194 atf_test_case compare_ops
195 compare_ops_head() {
196 atf_set "descr" "Compare operator tests"
197 }
198 compare_ops_body() {
199 test_expr 2 \!= 5 '1'
200 test_expr 2 \!= 2 '0'
201 test_expr 2 \<= 3 '1'
202 test_expr 2 \<= 2 '1'
203 test_expr 2 \<= 1 '0'
204 test_expr 2 \< 3 '1'
205 test_expr 2 \< 2 '0'
206 test_expr 2 = 2 '1'
207 test_expr 2 = 4 '0'
208 test_expr 2 \>= 1 '1'
209 test_expr 2 \>= 2 '1'
210 test_expr 2 \>= 3 '0'
211 test_expr 2 \> 1 '1'
212 test_expr 2 \> 2 '0'
213
214 test_finish
215 }
216
217 atf_test_case multiply
218 multiply_head() {
219 atf_set "descr" "Test the multiply operator (PR bin/12838)"
220 }
221 multiply_body() {
222 test_expr 1 \* -1 '-1'
223 test_expr 2 \> 1 \* 17 '0'
224
225 test_finish
226 }
227
228 atf_test_case negative
229 negative_head() {
230 atf_set "descr" "Test the additive inverse"
231 }
232 negative_body() {
233 test_expr -1 + 5 '4'
234 test_expr - 1 + 5 'expr: syntax error'
235
236 test_expr 5 + -1 '4'
237 test_expr 5 + - 1 'expr: syntax error'
238
239 test_expr 1 - -5 '6'
240
241 test_finish
242 }
243
244 atf_test_case math_precedence
245 math_precedence_head() {
246 atf_set "descr" "More complex math test for precedence"
247 }
248 math_precedence_body() {
249 test_expr -3 + -1 \* 4 + 3 / -6 '-7'
250
251 test_finish
252 }
253
254 atf_test_case precedence
255 precedence_head() {
256 atf_set "descr" "Test precedence between ':' and '|'"
257 }
258 precedence_body() {
259 test_expr X1/2/3 : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| . : '\(.\)' '1/2'
260
261 test_finish
262 }
263
264 atf_test_case regex
265 regex_head() {
266 atf_set "descr" "Test proper () returning \1 from a regex"
267 }
268 regex_body() {
269 test_expr 1/2 : '.*/\(.*\)' '2'
270
271 test_finish
272 }
273
274 atf_test_case short_circuit
275 short_circuit_head() {
276 atf_set "descr" "Test short-circuit evaluation of '|' and '&'"
277 }
278 short_circuit_body() {
279 test_expr 0 \| 1 / 0 "expr: second argument to '/' must not be zero"
280 test_expr 123 \| 1 / 0 '123'
281 test_expr 123 \| a : '***' '123'
282
283 test_expr 0 \& 1 / 0 '0'
284 test_expr 0 \& a : '***' '0'
285 test_expr 123 \& 1 / 0 "expr: second argument to '/' must not be zero"
286
287 test_finish
288 }
289
290 atf_test_case string_length
291 string_length_head() {
292 atf_set "descr" "Test the string length operator"
293 }
294 string_length_body() {
295 # The 'length' operator is an extension to POSIX 2024.
296 test_expr length "" '0'
297 test_expr length + 'expr: syntax error'
298 test_expr length \! '1'
299 test_expr length ++ '2'
300 test_expr length length '6'
301
302 test_finish
303 }
304
305 atf_init_test_cases()
306 {
307 atf_add_test_case lang
308 atf_add_test_case overflow
309 atf_add_test_case gtkmm
310 atf_add_test_case colon_vs_math
311 atf_add_test_case arithmetic_ops
312 atf_add_test_case basic_math
313 atf_add_test_case basic_functional
314 atf_add_test_case compare_ops_precedence
315 atf_add_test_case compare_ops
316 atf_add_test_case multiply
317 atf_add_test_case negative
318 atf_add_test_case math_precedence
319 atf_add_test_case precedence
320 atf_add_test_case regex
321 atf_add_test_case short_circuit
322 atf_add_test_case string_length
323 }
324