t_expr.sh revision 1.13 1 # $NetBSD: t_expr.sh,v 1.13 2025/03/15 15:33:00 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 # FIXME: POSIX says the result must be zero.
171 test_expr 0000 \| '' ''
172
173 test_finish
174 }
175
176 atf_test_case compare_ops
177 compare_ops_head() {
178 atf_set "descr" "Compare operator tests"
179 }
180 compare_ops_body() {
181 test_expr 2 \!= 5 '1'
182 test_expr 2 \!= 2 '0'
183 test_expr 2 \<= 3 '1'
184 test_expr 2 \<= 2 '1'
185 test_expr 2 \<= 1 '0'
186 test_expr 2 \< 3 '1'
187 test_expr 2 \< 2 '0'
188 test_expr 2 = 2 '1'
189 test_expr 2 = 4 '0'
190 test_expr 2 \>= 1 '1'
191 test_expr 2 \>= 2 '1'
192 test_expr 2 \>= 3 '0'
193 test_expr 2 \> 1 '1'
194 test_expr 2 \> 2 '0'
195
196 test_finish
197 }
198
199 atf_test_case multiply
200 multiply_head() {
201 atf_set "descr" "Test the multiply operator (PR bin/12838)"
202 }
203 multiply_body() {
204 test_expr 1 \* -1 '-1'
205 test_expr 2 \> 1 \* 17 '0'
206
207 test_finish
208 }
209
210 atf_test_case negative
211 negative_head() {
212 atf_set "descr" "Test the additive inverse"
213 }
214 negative_body() {
215 test_expr -1 + 5 '4'
216 test_expr - 1 + 5 'expr: syntax error'
217
218 test_expr 5 + -1 '4'
219 test_expr 5 + - 1 'expr: syntax error'
220
221 test_expr 1 - -5 '6'
222
223 test_finish
224 }
225
226 atf_test_case precedence
227 precedence_head() {
228 atf_set "descr" "Tests for operator precedence"
229 }
230 precedence_body() {
231 test_expr or \| '' \& and 'or'
232 test_expr '' \& and \| or 'or'
233 test_expr X1/2/3 : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| . : '\(.\)' '1/2'
234 test_expr and \& 001 = 00001 'and'
235 test_expr 001 = 00001 \& and '1'
236 test_expr 1 = 2 = 3 = 4 = 5 '0'
237 test_expr 1 = 2 = 3 = 4 = 0 '1'
238 test_expr 2 \> 1 \* 17 '0'
239 test_expr 900 + 101 = 1000 + 1 '1'
240 test_expr 1000 - 101 = 900 - 1 '1'
241 test_expr 1 + 100 - 10 + 1000 '1091'
242 test_expr 50 + 3 \* 4 + 80 '142'
243 test_expr 12345 / 1000 \* 1000 '12000'
244 test_expr 12345 % 1000 / 10 '34'
245 test_expr 2 : 4 / 2 '0'
246 test_expr 4 : 4 % 3 '1'
247 test_expr 6 \* 1111100 : 1\* '30'
248 test_expr -3 + -1 \* 4 + 3 / -6 '-7'
249 test_expr 10 \* \( 3 + 5 \) '80'
250 test_expr length 123456 : '\([1236]*\)' '6'
251 test_expr length \( 123456 : '\([1236]*\)' \) '3'
252
253 test_finish
254 }
255
256 atf_test_case regex
257 regex_head() {
258 atf_set "descr" "Test proper () returning \1 from a regex"
259 }
260 regex_body() {
261 test_expr 1/2 : '.*/\(.*\)' '2'
262
263 test_finish
264 }
265
266 atf_test_case short_circuit
267 short_circuit_head() {
268 atf_set "descr" "Test short-circuit evaluation of '|' and '&'"
269 }
270 short_circuit_body() {
271 test_expr 0 \| 1 / 0 "expr: second argument to '/' must not be zero"
272 test_expr 123 \| 1 / 0 '123'
273 test_expr 123 \| a : '***' '123'
274
275 test_expr 0 \& 1 / 0 '0'
276 test_expr 0 \& a : '***' '0'
277 test_expr 123 \& 1 / 0 "expr: second argument to '/' must not be zero"
278
279 test_finish
280 }
281
282 atf_test_case string_length
283 string_length_head() {
284 atf_set "descr" "Test the string length operator"
285 }
286 string_length_body() {
287 # The 'length' operator is an extension to POSIX 2024.
288 test_expr length "" '0'
289 test_expr length + 'expr: syntax error'
290 test_expr length \! '1'
291 test_expr length ++ '2'
292 test_expr length length '6'
293
294 test_finish
295 }
296
297 atf_init_test_cases()
298 {
299 atf_add_test_case lang
300 atf_add_test_case overflow
301 atf_add_test_case gtkmm
302 atf_add_test_case arithmetic_ops
303 atf_add_test_case basic_functional
304 atf_add_test_case compare_ops
305 atf_add_test_case multiply
306 atf_add_test_case negative
307 atf_add_test_case precedence
308 atf_add_test_case regex
309 atf_add_test_case short_circuit
310 atf_add_test_case string_length
311 }
312