t_expr.sh revision 1.14 1 # $NetBSD: t_expr.sh,v 1.14 2025/03/15 15:36:12 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 # When setlocale fails, ensure that no error message is printed,
57 # like for most other utilities.
58
59 atf_check -o inline:"21\n" \
60 env LANG=nonexistent "$expr_prog" 10 + 11
61 atf_check -o inline:"21\n" \
62 env LANG=ru_RU.KOI8-R "$expr_prog" 10 + 11
63 }
64
65 atf_test_case overflow
66 overflow_head() {
67 atf_set "descr" "Test overflow cases"
68 }
69 overflow_body() {
70 test_expr 4611686018427387904 + 4611686018427387903 \
71 '9223372036854775807'
72 test_expr 4611686018427387904 + 4611686018427387904 \
73 "expr: integer overflow or underflow occurred for operation '4611686018427387904 + 4611686018427387904'"
74 test_expr 4611686018427387904 - -4611686018427387904 \
75 "expr: integer overflow or underflow occurred for operation '4611686018427387904 - -4611686018427387904'"
76 test_expr -4611686018427387904 - 4611686018427387903 \
77 '-9223372036854775807'
78 test_expr -4611686018427387904 - 4611686018427387905 \
79 "expr: integer overflow or underflow occurred for operation '-4611686018427387904 - 4611686018427387905'"
80 test_expr -4611686018427387904 \* 1 '-4611686018427387904'
81 test_expr -4611686018427387904 \* -1 '4611686018427387904'
82 test_expr -4611686018427387904 \* 2 '-9223372036854775808'
83 test_expr -4611686018427387904 \* 3 \
84 "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * 3'"
85 test_expr -4611686018427387904 \* -2 \
86 "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * -2'"
87 test_expr 4611686018427387904 \* 1 '4611686018427387904'
88 test_expr 4611686018427387904 \* 2 \
89 "expr: integer overflow or underflow occurred for operation '4611686018427387904 * 2'"
90 test_expr 4611686018427387904 \* 3 \
91 "expr: integer overflow or underflow occurred for operation '4611686018427387904 * 3'"
92 test_expr -9223372036854775808 % -1 \
93 "expr: integer overflow or underflow occurred for operation '-9223372036854775808 % -1'"
94 test_expr -9223372036854775808 / -1 \
95 "expr: integer overflow or underflow occurred for operation '-9223372036854775808 / -1'"
96 test_expr 0 + -9223372036854775808 '-9223372036854775808'
97 test_expr 0 + -1 '-1'
98 test_expr 0 + 0 '0'
99 test_expr 0 + 1 '1'
100 test_expr 0 + 9223372036854775807 '9223372036854775807'
101 test_expr -9223372036854775808 + 0 '-9223372036854775808'
102 test_expr 9223372036854775807 + 0 '9223372036854775807'
103 test_expr 4611686018427387904 \* -1 '-4611686018427387904'
104 test_expr 4611686018427387904 \* -2 '-9223372036854775808'
105 test_expr 4611686018427387904 \* -3 \
106 "expr: integer overflow or underflow occurred for operation '4611686018427387904 * -3'"
107 test_expr -4611686018427387904 \* -1 '4611686018427387904'
108 test_expr -4611686018427387904 \* -2 \
109 "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * -2'"
110 test_expr -4611686018427387904 \* -3 \
111 "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * -3'"
112 test_expr 0 \* -1 '0'
113 test_expr 0 \* 0 '0'
114 test_expr 0 \* 1 '0'
115
116 test_finish
117 }
118
119 atf_test_case gtkmm
120 gtkmm_head() {
121 atf_set "descr" "Tests from gtk-- configure that cause problems on old expr"
122 }
123 gtkmm_body() {
124 test_expr 3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 5 '1'
125 test_expr 3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 6 '0'
126 test_expr 3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 3 \& 5 \>= 5 '0'
127 test_expr 3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 2 \& 4 = 4 \& 5 \>= 5 '0'
128 test_expr 3 \> 2 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 6 '1'
129 test_expr 3 \> 3 \| 3 = 3 \& 4 \> 3 \| 3 = 3 \& 4 = 4 \& 5 \>= 5 '1'
130
131 test_finish
132 }
133
134 atf_test_case arithmetic_ops
135 arithmetic_ops_head() {
136 atf_set "descr" "Dangling arithmetic operator"
137 }
138 arithmetic_ops_body() {
139 test_expr .java_wrapper : / '0'
140 test_expr 4 : \* '0'
141 test_expr 4 : + '0'
142 test_expr 4 : - '0'
143 test_expr 4 : / '0'
144 test_expr 4 : % '0'
145
146 test_finish
147 }
148
149 atf_test_case basic_functional
150 basic_functional_head() {
151 atf_set "descr" "Basic functional tests"
152 }
153 basic_functional_body() {
154 test_expr 2 '2'
155 test_expr -4 '-4'
156 test_expr hello 'hello'
157 test_expr -- double-dash 'double-dash'
158 test_expr -- -- -- six-dashes 'expr: syntax error'
159 test_expr 3 -- + 4 'expr: syntax error'
160 test_expr 0000005 '0000005'
161 test_expr 0 + 0000005 '5'
162
163 test_expr 111 \& 222 \& 333 '111'
164 test_expr 111 \& 222 \& 0 '0'
165
166 test_expr 1111 \| 2222 '1111'
167 test_expr 1111 \| 00 '1111'
168 test_expr 0000 \| 2222 '2222'
169 test_expr 0000 \| 00 '00'
170 test_expr 0000 \| '' '0'
171
172 test_finish
173 }
174
175 atf_test_case compare_ops
176 compare_ops_head() {
177 atf_set "descr" "Compare operator tests"
178 }
179 compare_ops_body() {
180 test_expr 2 \!= 5 '1'
181 test_expr 2 \!= 2 '0'
182 test_expr 2 \<= 3 '1'
183 test_expr 2 \<= 2 '1'
184 test_expr 2 \<= 1 '0'
185 test_expr 2 \< 3 '1'
186 test_expr 2 \< 2 '0'
187 test_expr 2 = 2 '1'
188 test_expr 2 = 4 '0'
189 test_expr 2 \>= 1 '1'
190 test_expr 2 \>= 2 '1'
191 test_expr 2 \>= 3 '0'
192 test_expr 2 \> 1 '1'
193 test_expr 2 \> 2 '0'
194
195 test_finish
196 }
197
198 atf_test_case multiply
199 multiply_head() {
200 atf_set "descr" "Test the multiply operator (PR bin/12838)"
201 }
202 multiply_body() {
203 test_expr 1 \* -1 '-1'
204 test_expr 2 \> 1 \* 17 '0'
205
206 test_finish
207 }
208
209 atf_test_case negative
210 negative_head() {
211 atf_set "descr" "Test the additive inverse"
212 }
213 negative_body() {
214 test_expr -1 + 5 '4'
215 test_expr - 1 + 5 'expr: syntax error'
216
217 test_expr 5 + -1 '4'
218 test_expr 5 + - 1 'expr: syntax error'
219
220 test_expr 1 - -5 '6'
221
222 test_finish
223 }
224
225 atf_test_case precedence
226 precedence_head() {
227 atf_set "descr" "Tests for operator precedence"
228 }
229 precedence_body() {
230 test_expr or \| '' \& and 'or'
231 test_expr '' \& and \| or 'or'
232 test_expr X1/2/3 : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| . : '\(.\)' '1/2'
233 test_expr and \& 001 = 00001 'and'
234 test_expr 001 = 00001 \& and '1'
235 test_expr 1 = 2 = 3 = 4 = 5 '0'
236 test_expr 1 = 2 = 3 = 4 = 0 '1'
237 test_expr 2 \> 1 \* 17 '0'
238 test_expr 900 + 101 = 1000 + 1 '1'
239 test_expr 1000 - 101 = 900 - 1 '1'
240 test_expr 1 + 100 - 10 + 1000 '1091'
241 test_expr 50 + 3 \* 4 + 80 '142'
242 test_expr 12345 / 1000 \* 1000 '12000'
243 test_expr 12345 % 1000 / 10 '34'
244 test_expr 2 : 4 / 2 '0'
245 test_expr 4 : 4 % 3 '1'
246 test_expr 6 \* 1111100 : 1\* '30'
247 test_expr -3 + -1 \* 4 + 3 / -6 '-7'
248 test_expr 10 \* \( 3 + 5 \) '80'
249 test_expr length 123456 : '\([1236]*\)' '6'
250 test_expr length \( 123456 : '\([1236]*\)' \) '3'
251
252 test_finish
253 }
254
255 atf_test_case regex
256 regex_head() {
257 atf_set "descr" "Test proper () returning \1 from a regex"
258 }
259 regex_body() {
260 test_expr 1/2 : '.*/\(.*\)' '2'
261
262 test_finish
263 }
264
265 atf_test_case short_circuit
266 short_circuit_head() {
267 atf_set "descr" "Test short-circuit evaluation of '|' and '&'"
268 }
269 short_circuit_body() {
270 test_expr 0 \| 1 / 0 "expr: second argument to '/' must not be zero"
271 test_expr 123 \| 1 / 0 '123'
272 test_expr 123 \| a : '***' '123'
273
274 test_expr 0 \& 1 / 0 '0'
275 test_expr 0 \& a : '***' '0'
276 test_expr 123 \& 1 / 0 "expr: second argument to '/' must not be zero"
277
278 test_finish
279 }
280
281 atf_test_case string_length
282 string_length_head() {
283 atf_set "descr" "Test the string length operator"
284 }
285 string_length_body() {
286 # The 'length' operator is an extension to POSIX 2024.
287 test_expr length "" '0'
288 test_expr length + 'expr: syntax error'
289 test_expr length \! '1'
290 test_expr length ++ '2'
291 test_expr length length '6'
292
293 test_finish
294 }
295
296 atf_init_test_cases()
297 {
298 atf_add_test_case lang
299 atf_add_test_case overflow
300 atf_add_test_case gtkmm
301 atf_add_test_case arithmetic_ops
302 atf_add_test_case basic_functional
303 atf_add_test_case compare_ops
304 atf_add_test_case multiply
305 atf_add_test_case negative
306 atf_add_test_case precedence
307 atf_add_test_case regex
308 atf_add_test_case short_circuit
309 atf_add_test_case string_length
310 }
311