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