t_expr.sh revision 1.14 1 1.14 rillig # $NetBSD: t_expr.sh,v 1.14 2025/03/15 15:36:12 rillig Exp $
2 1.1 jruoho #
3 1.1 jruoho # Copyright (c) 2007 The NetBSD Foundation, Inc.
4 1.1 jruoho # All rights reserved.
5 1.1 jruoho #
6 1.1 jruoho # Redistribution and use in source and binary forms, with or without
7 1.1 jruoho # modification, are permitted provided that the following conditions
8 1.1 jruoho # are met:
9 1.1 jruoho # 1. Redistributions of source code must retain the above copyright
10 1.1 jruoho # notice, this list of conditions and the following disclaimer.
11 1.1 jruoho # 2. Redistributions in binary form must reproduce the above copyright
12 1.1 jruoho # notice, this list of conditions and the following disclaimer in the
13 1.1 jruoho # documentation and/or other materials provided with the distribution.
14 1.1 jruoho #
15 1.1 jruoho # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 1.1 jruoho # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 1.1 jruoho # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 1.1 jruoho # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 1.1 jruoho # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 1.1 jruoho # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 1.1 jruoho # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 1.1 jruoho # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 1.1 jruoho # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 1.1 jruoho # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1 jruoho # POSSIBILITY OF SUCH DAMAGE.
26 1.1 jruoho #
27 1.1 jruoho
28 1.12 rillig : "${expr_prog:=expr}"
29 1.12 rillig
30 1.9 rillig # usage: test_expr operand ... result|error
31 1.1 jruoho test_expr() {
32 1.9 rillig i=1
33 1.9 rillig while [ $((i++)) -lt $# ]; do
34 1.9 rillig set -- "$@" "$1"
35 1.9 rillig shift
36 1.9 rillig done
37 1.9 rillig expected="$1"
38 1.9 rillig shift
39 1.9 rillig
40 1.9 rillig # shellcheck disable=SC2003
41 1.12 rillig actual=$("$expr_prog" "$@" 2>&1 || :)
42 1.9 rillig
43 1.9 rillig printf "%s => '%s'\n" "$*" "$expected" >> expected
44 1.9 rillig printf "%s => '%s'\n" "$*" "$actual" >> actual
45 1.9 rillig }
46 1.9 rillig
47 1.9 rillig test_finish() {
48 1.9 rillig atf_check -o file:expected cat actual
49 1.1 jruoho }
50 1.1 jruoho
51 1.3 jruoho atf_test_case lang
52 1.7 gutterid lang_head() {
53 1.3 jruoho atf_set "descr" "Test that expr(1) works with non-C LANG (PR bin/2486)"
54 1.3 jruoho }
55 1.3 jruoho lang_body() {
56 1.13 rillig # When setlocale fails, ensure that no error message is printed,
57 1.13 rillig # like for most other utilities.
58 1.3 jruoho
59 1.13 rillig atf_check -o inline:"21\n" \
60 1.13 rillig env LANG=nonexistent "$expr_prog" 10 + 11
61 1.13 rillig atf_check -o inline:"21\n" \
62 1.13 rillig env LANG=ru_RU.KOI8-R "$expr_prog" 10 + 11
63 1.3 jruoho }
64 1.3 jruoho
65 1.1 jruoho atf_test_case overflow
66 1.1 jruoho overflow_head() {
67 1.1 jruoho atf_set "descr" "Test overflow cases"
68 1.1 jruoho }
69 1.1 jruoho overflow_body() {
70 1.9 rillig test_expr 4611686018427387904 + 4611686018427387903 \
71 1.1 jruoho '9223372036854775807'
72 1.9 rillig test_expr 4611686018427387904 + 4611686018427387904 \
73 1.1 jruoho "expr: integer overflow or underflow occurred for operation '4611686018427387904 + 4611686018427387904'"
74 1.9 rillig test_expr 4611686018427387904 - -4611686018427387904 \
75 1.1 jruoho "expr: integer overflow or underflow occurred for operation '4611686018427387904 - -4611686018427387904'"
76 1.9 rillig test_expr -4611686018427387904 - 4611686018427387903 \
77 1.1 jruoho '-9223372036854775807'
78 1.9 rillig test_expr -4611686018427387904 - 4611686018427387905 \
79 1.1 jruoho "expr: integer overflow or underflow occurred for operation '-4611686018427387904 - 4611686018427387905'"
80 1.9 rillig test_expr -4611686018427387904 \* 1 '-4611686018427387904'
81 1.9 rillig test_expr -4611686018427387904 \* -1 '4611686018427387904'
82 1.9 rillig test_expr -4611686018427387904 \* 2 '-9223372036854775808'
83 1.9 rillig test_expr -4611686018427387904 \* 3 \
84 1.1 jruoho "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * 3'"
85 1.9 rillig test_expr -4611686018427387904 \* -2 \
86 1.1 jruoho "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * -2'"
87 1.9 rillig test_expr 4611686018427387904 \* 1 '4611686018427387904'
88 1.9 rillig test_expr 4611686018427387904 \* 2 \
89 1.1 jruoho "expr: integer overflow or underflow occurred for operation '4611686018427387904 * 2'"
90 1.9 rillig test_expr 4611686018427387904 \* 3 \
91 1.1 jruoho "expr: integer overflow or underflow occurred for operation '4611686018427387904 * 3'"
92 1.9 rillig test_expr -9223372036854775808 % -1 \
93 1.4 kamil "expr: integer overflow or underflow occurred for operation '-9223372036854775808 % -1'"
94 1.9 rillig test_expr -9223372036854775808 / -1 \
95 1.4 kamil "expr: integer overflow or underflow occurred for operation '-9223372036854775808 / -1'"
96 1.9 rillig test_expr 0 + -9223372036854775808 '-9223372036854775808'
97 1.9 rillig test_expr 0 + -1 '-1'
98 1.9 rillig test_expr 0 + 0 '0'
99 1.9 rillig test_expr 0 + 1 '1'
100 1.9 rillig test_expr 0 + 9223372036854775807 '9223372036854775807'
101 1.9 rillig test_expr -9223372036854775808 + 0 '-9223372036854775808'
102 1.9 rillig test_expr 9223372036854775807 + 0 '9223372036854775807'
103 1.9 rillig test_expr 4611686018427387904 \* -1 '-4611686018427387904'
104 1.9 rillig test_expr 4611686018427387904 \* -2 '-9223372036854775808'
105 1.9 rillig test_expr 4611686018427387904 \* -3 \
106 1.5 kamil "expr: integer overflow or underflow occurred for operation '4611686018427387904 * -3'"
107 1.9 rillig test_expr -4611686018427387904 \* -1 '4611686018427387904'
108 1.9 rillig test_expr -4611686018427387904 \* -2 \
109 1.5 kamil "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * -2'"
110 1.9 rillig test_expr -4611686018427387904 \* -3 \
111 1.5 kamil "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * -3'"
112 1.9 rillig test_expr 0 \* -1 '0'
113 1.9 rillig test_expr 0 \* 0 '0'
114 1.9 rillig test_expr 0 \* 1 '0'
115 1.9 rillig
116 1.9 rillig test_finish
117 1.1 jruoho }
118 1.1 jruoho
119 1.1 jruoho atf_test_case gtkmm
120 1.1 jruoho gtkmm_head() {
121 1.8 rillig atf_set "descr" "Tests from gtk-- configure that cause problems on old expr"
122 1.1 jruoho }
123 1.1 jruoho gtkmm_body() {
124 1.9 rillig test_expr 3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 5 '1'
125 1.9 rillig test_expr 3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 6 '0'
126 1.9 rillig test_expr 3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 3 \& 5 \>= 5 '0'
127 1.9 rillig test_expr 3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 2 \& 4 = 4 \& 5 \>= 5 '0'
128 1.9 rillig test_expr 3 \> 2 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 6 '1'
129 1.9 rillig test_expr 3 \> 3 \| 3 = 3 \& 4 \> 3 \| 3 = 3 \& 4 = 4 \& 5 \>= 5 '1'
130 1.9 rillig
131 1.9 rillig test_finish
132 1.1 jruoho }
133 1.1 jruoho
134 1.1 jruoho atf_test_case arithmetic_ops
135 1.1 jruoho arithmetic_ops_head() {
136 1.8 rillig atf_set "descr" "Dangling arithmetic operator"
137 1.1 jruoho }
138 1.1 jruoho arithmetic_ops_body() {
139 1.9 rillig test_expr .java_wrapper : / '0'
140 1.9 rillig test_expr 4 : \* '0'
141 1.9 rillig test_expr 4 : + '0'
142 1.9 rillig test_expr 4 : - '0'
143 1.9 rillig test_expr 4 : / '0'
144 1.9 rillig test_expr 4 : % '0'
145 1.9 rillig
146 1.9 rillig test_finish
147 1.1 jruoho }
148 1.1 jruoho
149 1.1 jruoho atf_test_case basic_functional
150 1.1 jruoho basic_functional_head() {
151 1.1 jruoho atf_set "descr" "Basic functional tests"
152 1.1 jruoho }
153 1.1 jruoho basic_functional_body() {
154 1.13 rillig test_expr 2 '2'
155 1.13 rillig test_expr -4 '-4'
156 1.13 rillig test_expr hello 'hello'
157 1.13 rillig test_expr -- double-dash 'double-dash'
158 1.13 rillig test_expr -- -- -- six-dashes 'expr: syntax error'
159 1.13 rillig test_expr 3 -- + 4 'expr: syntax error'
160 1.13 rillig test_expr 0000005 '0000005'
161 1.13 rillig test_expr 0 + 0000005 '5'
162 1.13 rillig
163 1.13 rillig test_expr 111 \& 222 \& 333 '111'
164 1.13 rillig test_expr 111 \& 222 \& 0 '0'
165 1.13 rillig
166 1.13 rillig test_expr 1111 \| 2222 '1111'
167 1.13 rillig test_expr 1111 \| 00 '1111'
168 1.13 rillig test_expr 0000 \| 2222 '2222'
169 1.13 rillig test_expr 0000 \| 00 '00'
170 1.14 rillig test_expr 0000 \| '' '0'
171 1.9 rillig
172 1.9 rillig test_finish
173 1.1 jruoho }
174 1.1 jruoho
175 1.1 jruoho atf_test_case compare_ops
176 1.1 jruoho compare_ops_head() {
177 1.1 jruoho atf_set "descr" "Compare operator tests"
178 1.1 jruoho }
179 1.1 jruoho compare_ops_body() {
180 1.9 rillig test_expr 2 \!= 5 '1'
181 1.9 rillig test_expr 2 \!= 2 '0'
182 1.9 rillig test_expr 2 \<= 3 '1'
183 1.9 rillig test_expr 2 \<= 2 '1'
184 1.9 rillig test_expr 2 \<= 1 '0'
185 1.9 rillig test_expr 2 \< 3 '1'
186 1.9 rillig test_expr 2 \< 2 '0'
187 1.9 rillig test_expr 2 = 2 '1'
188 1.9 rillig test_expr 2 = 4 '0'
189 1.9 rillig test_expr 2 \>= 1 '1'
190 1.9 rillig test_expr 2 \>= 2 '1'
191 1.9 rillig test_expr 2 \>= 3 '0'
192 1.9 rillig test_expr 2 \> 1 '1'
193 1.9 rillig test_expr 2 \> 2 '0'
194 1.9 rillig
195 1.9 rillig test_finish
196 1.1 jruoho }
197 1.1 jruoho
198 1.1 jruoho atf_test_case multiply
199 1.1 jruoho multiply_head() {
200 1.2 jruoho atf_set "descr" "Test the multiply operator (PR bin/12838)"
201 1.1 jruoho }
202 1.1 jruoho multiply_body() {
203 1.9 rillig test_expr 1 \* -1 '-1'
204 1.9 rillig test_expr 2 \> 1 \* 17 '0'
205 1.9 rillig
206 1.9 rillig test_finish
207 1.1 jruoho }
208 1.1 jruoho
209 1.1 jruoho atf_test_case negative
210 1.1 jruoho negative_head() {
211 1.1 jruoho atf_set "descr" "Test the additive inverse"
212 1.1 jruoho }
213 1.1 jruoho negative_body() {
214 1.9 rillig test_expr -1 + 5 '4'
215 1.9 rillig test_expr - 1 + 5 'expr: syntax error'
216 1.9 rillig
217 1.9 rillig test_expr 5 + -1 '4'
218 1.9 rillig test_expr 5 + - 1 'expr: syntax error'
219 1.1 jruoho
220 1.9 rillig test_expr 1 - -5 '6'
221 1.1 jruoho
222 1.9 rillig test_finish
223 1.1 jruoho }
224 1.1 jruoho
225 1.1 jruoho atf_test_case precedence
226 1.1 jruoho precedence_head() {
227 1.13 rillig atf_set "descr" "Tests for operator precedence"
228 1.1 jruoho }
229 1.1 jruoho precedence_body() {
230 1.13 rillig test_expr or \| '' \& and 'or'
231 1.13 rillig test_expr '' \& and \| or 'or'
232 1.9 rillig test_expr X1/2/3 : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| . : '\(.\)' '1/2'
233 1.13 rillig test_expr and \& 001 = 00001 'and'
234 1.13 rillig test_expr 001 = 00001 \& and '1'
235 1.13 rillig test_expr 1 = 2 = 3 = 4 = 5 '0'
236 1.13 rillig test_expr 1 = 2 = 3 = 4 = 0 '1'
237 1.13 rillig test_expr 2 \> 1 \* 17 '0'
238 1.13 rillig test_expr 900 + 101 = 1000 + 1 '1'
239 1.13 rillig test_expr 1000 - 101 = 900 - 1 '1'
240 1.13 rillig test_expr 1 + 100 - 10 + 1000 '1091'
241 1.13 rillig test_expr 50 + 3 \* 4 + 80 '142'
242 1.13 rillig test_expr 12345 / 1000 \* 1000 '12000'
243 1.13 rillig test_expr 12345 % 1000 / 10 '34'
244 1.13 rillig test_expr 2 : 4 / 2 '0'
245 1.13 rillig test_expr 4 : 4 % 3 '1'
246 1.13 rillig test_expr 6 \* 1111100 : 1\* '30'
247 1.13 rillig test_expr -3 + -1 \* 4 + 3 / -6 '-7'
248 1.13 rillig test_expr 10 \* \( 3 + 5 \) '80'
249 1.13 rillig test_expr length 123456 : '\([1236]*\)' '6'
250 1.13 rillig test_expr length \( 123456 : '\([1236]*\)' \) '3'
251 1.9 rillig
252 1.9 rillig test_finish
253 1.1 jruoho }
254 1.1 jruoho
255 1.1 jruoho atf_test_case regex
256 1.1 jruoho regex_head() {
257 1.1 jruoho atf_set "descr" "Test proper () returning \1 from a regex"
258 1.1 jruoho }
259 1.1 jruoho regex_body() {
260 1.9 rillig test_expr 1/2 : '.*/\(.*\)' '2'
261 1.9 rillig
262 1.9 rillig test_finish
263 1.1 jruoho }
264 1.1 jruoho
265 1.10 rillig atf_test_case short_circuit
266 1.10 rillig short_circuit_head() {
267 1.10 rillig atf_set "descr" "Test short-circuit evaluation of '|' and '&'"
268 1.10 rillig }
269 1.10 rillig short_circuit_body() {
270 1.10 rillig test_expr 0 \| 1 / 0 "expr: second argument to '/' must not be zero"
271 1.11 rillig test_expr 123 \| 1 / 0 '123'
272 1.11 rillig test_expr 123 \| a : '***' '123'
273 1.10 rillig
274 1.11 rillig test_expr 0 \& 1 / 0 '0'
275 1.11 rillig test_expr 0 \& a : '***' '0'
276 1.10 rillig test_expr 123 \& 1 / 0 "expr: second argument to '/' must not be zero"
277 1.10 rillig
278 1.10 rillig test_finish
279 1.10 rillig }
280 1.10 rillig
281 1.8 rillig atf_test_case string_length
282 1.8 rillig string_length_head() {
283 1.8 rillig atf_set "descr" "Test the string length operator"
284 1.8 rillig }
285 1.8 rillig string_length_body() {
286 1.10 rillig # The 'length' operator is an extension to POSIX 2024.
287 1.9 rillig test_expr length "" '0'
288 1.9 rillig test_expr length + 'expr: syntax error'
289 1.9 rillig test_expr length \! '1'
290 1.9 rillig test_expr length ++ '2'
291 1.9 rillig test_expr length length '6'
292 1.9 rillig
293 1.9 rillig test_finish
294 1.8 rillig }
295 1.8 rillig
296 1.1 jruoho atf_init_test_cases()
297 1.1 jruoho {
298 1.3 jruoho atf_add_test_case lang
299 1.1 jruoho atf_add_test_case overflow
300 1.1 jruoho atf_add_test_case gtkmm
301 1.1 jruoho atf_add_test_case arithmetic_ops
302 1.1 jruoho atf_add_test_case basic_functional
303 1.1 jruoho atf_add_test_case compare_ops
304 1.1 jruoho atf_add_test_case multiply
305 1.1 jruoho atf_add_test_case negative
306 1.1 jruoho atf_add_test_case precedence
307 1.1 jruoho atf_add_test_case regex
308 1.10 rillig atf_add_test_case short_circuit
309 1.8 rillig atf_add_test_case string_length
310 1.1 jruoho }
311