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