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