1 # $NetBSD: t_expr.sh,v 1.17 2025/06/29 00:24:23 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 i=$((i + 1)) 35 set -- "$@" "$1" 36 shift 37 done 38 expected="$1" 39 shift 40 41 # shellcheck disable=SC2003 42 actual=$("$expr_prog" "$@" 2>&1 || :) 43 44 printf "%s => '%s'\n" "$*" "$expected" >> expected 45 printf "%s => '%s'\n" "$*" "$actual" >> actual 46 } 47 48 test_finish() { 49 atf_check -o file:expected cat actual 50 } 51 52 atf_test_case lang 53 lang_head() { 54 atf_set "descr" "Test that expr(1) works with non-C LANG (PR bin/2486)" 55 } 56 lang_body() { 57 # When setlocale fails, ensure that no error message is printed, 58 # like for most other utilities. 59 60 atf_check -o inline:"21\n" \ 61 env LANG=nonexistent "$expr_prog" 10 + 11 62 atf_check -o inline:"21\n" \ 63 env LANG=ru_RU.KOI8-R "$expr_prog" 10 + 11 64 } 65 66 atf_test_case overflow 67 overflow_head() { 68 atf_set "descr" "Test overflow cases" 69 } 70 overflow_body() { 71 test_expr 4611686018427387904 + 4611686018427387903 \ 72 '9223372036854775807' 73 test_expr 4611686018427387904 + 4611686018427387904 \ 74 "expr: integer overflow or underflow occurred for operation '4611686018427387904 + 4611686018427387904'" 75 test_expr 4611686018427387904 - -4611686018427387904 \ 76 "expr: integer overflow or underflow occurred for operation '4611686018427387904 - -4611686018427387904'" 77 test_expr -4611686018427387904 - 4611686018427387903 \ 78 '-9223372036854775807' 79 test_expr -4611686018427387904 - 4611686018427387905 \ 80 "expr: integer overflow or underflow occurred for operation '-4611686018427387904 - 4611686018427387905'" 81 test_expr -4611686018427387904 \* 1 '-4611686018427387904' 82 test_expr -4611686018427387904 \* -1 '4611686018427387904' 83 test_expr -4611686018427387904 \* 2 '-9223372036854775808' 84 test_expr -4611686018427387904 \* 3 \ 85 "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * 3'" 86 test_expr -4611686018427387904 \* -2 \ 87 "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * -2'" 88 test_expr 4611686018427387904 \* 1 '4611686018427387904' 89 test_expr 4611686018427387904 \* 2 \ 90 "expr: integer overflow or underflow occurred for operation '4611686018427387904 * 2'" 91 test_expr 4611686018427387904 \* 3 \ 92 "expr: integer overflow or underflow occurred for operation '4611686018427387904 * 3'" 93 test_expr -9223372036854775808 % -1 \ 94 "expr: integer overflow or underflow occurred for operation '-9223372036854775808 % -1'" 95 test_expr -9223372036854775808 / -1 \ 96 "expr: integer overflow or underflow occurred for operation '-9223372036854775808 / -1'" 97 test_expr 0 + -9223372036854775808 '-9223372036854775808' 98 test_expr 0 + -1 '-1' 99 test_expr 0 + 0 '0' 100 test_expr 0 + 1 '1' 101 test_expr 0 + 9223372036854775807 '9223372036854775807' 102 test_expr -9223372036854775808 + 0 '-9223372036854775808' 103 test_expr 9223372036854775807 + 0 '9223372036854775807' 104 test_expr 4611686018427387904 \* -1 '-4611686018427387904' 105 test_expr 4611686018427387904 \* -2 '-9223372036854775808' 106 test_expr 4611686018427387904 \* -3 \ 107 "expr: integer overflow or underflow occurred for operation '4611686018427387904 * -3'" 108 test_expr -4611686018427387904 \* -1 '4611686018427387904' 109 test_expr -4611686018427387904 \* -2 \ 110 "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * -2'" 111 test_expr -4611686018427387904 \* -3 \ 112 "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * -3'" 113 test_expr 0 \* -1 '0' 114 test_expr 0 \* 0 '0' 115 test_expr 0 \* 1 '0' 116 117 test_finish 118 } 119 120 atf_test_case gtkmm 121 gtkmm_head() { 122 atf_set "descr" "Tests from gtk-- configure that cause problems on old expr" 123 } 124 gtkmm_body() { 125 test_expr 3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 5 '1' 126 test_expr 3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 6 '0' 127 test_expr 3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 3 \& 5 \>= 5 '0' 128 test_expr 3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 2 \& 4 = 4 \& 5 \>= 5 '0' 129 test_expr 3 \> 2 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 6 '1' 130 test_expr 3 \> 3 \| 3 = 3 \& 4 \> 3 \| 3 = 3 \& 4 = 4 \& 5 \>= 5 '1' 131 132 test_finish 133 } 134 135 atf_test_case arithmetic_ops 136 arithmetic_ops_head() { 137 atf_set "descr" "Dangling arithmetic operator" 138 } 139 arithmetic_ops_body() { 140 test_expr .java_wrapper : / '0' 141 test_expr 4 : \* '0' 142 test_expr 4 : + '0' 143 test_expr 4 : - '0' 144 test_expr 4 : / '0' 145 test_expr 4 : % '0' 146 147 test_finish 148 } 149 150 atf_test_case basic_functional 151 basic_functional_head() { 152 atf_set "descr" "Basic functional tests" 153 } 154 basic_functional_body() { 155 test_expr 2 '2' 156 test_expr -4 '-4' 157 test_expr hello 'hello' 158 test_expr -- double-dash 'double-dash' 159 test_expr -- -- -- six-dashes 'expr: syntax error' 160 test_expr 3 -- + 4 'expr: syntax error' 161 test_expr 0000005 '0000005' 162 test_expr 0 + 0000005 '5' 163 164 test_expr 111 \& 222 \& 333 '111' 165 test_expr 111 \& 222 \& 0 '0' 166 167 test_expr 1111 \| 2222 '1111' 168 test_expr 1111 \| 00 '1111' 169 test_expr 0000 \| 2222 '2222' 170 test_expr 0000 \| 00 '00' 171 test_expr 0000 \| '' '0' 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 LC_ALL=en_US.UTF-8 test_expr aaa : '.*' '6' 264 LC_ALL=C test_expr aaa : '.*' '9' 265 266 test_finish 267 } 268 269 atf_test_case short_circuit 270 short_circuit_head() { 271 atf_set "descr" "Test short-circuit evaluation of '|' and '&'" 272 } 273 short_circuit_body() { 274 test_expr 0 \| 1 / 0 "expr: second argument to '/' must not be zero" 275 test_expr 123 \| 1 / 0 '123' 276 test_expr 123 \| a : '***' '123' 277 278 test_expr 0 \& 1 / 0 '0' 279 test_expr 0 \& a : '***' '0' 280 test_expr 123 \& 1 / 0 "expr: second argument to '/' must not be zero" 281 282 test_finish 283 } 284 285 atf_test_case string_length 286 string_length_head() { 287 atf_set "descr" "Test the string length operator" 288 } 289 string_length_body() { 290 # The 'length' operator is an extension to POSIX 2024. 291 test_expr length "" '0' 292 test_expr length + 'expr: syntax error' 293 test_expr length \! '1' 294 test_expr length ++ '2' 295 test_expr length length '6' 296 297 LC_ALL=en_US.UTF-8 test_expr length aaa '6' 298 LC_ALL=C test_expr length aaa '9' 299 300 test_finish 301 } 302 303 atf_init_test_cases() 304 { 305 atf_add_test_case lang 306 atf_add_test_case overflow 307 atf_add_test_case gtkmm 308 atf_add_test_case arithmetic_ops 309 atf_add_test_case basic_functional 310 atf_add_test_case compare_ops 311 atf_add_test_case multiply 312 atf_add_test_case negative 313 atf_add_test_case precedence 314 atf_add_test_case regex 315 atf_add_test_case short_circuit 316 atf_add_test_case string_length 317 } 318