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