Home | History | Annotate | Line # | Download | only in expr
t_expr.sh revision 1.5
      1 # $NetBSD: t_expr.sh,v 1.5 2018/06/27 17:14:47 kamil 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 # The first arg will get eval'd so escape any meta characters
     29 # The 2nd arg is an expected string/response from expr for that op.
     30 test_expr() {
     31 	echo "Expression '${1}', expecting '${2}'"
     32 	res=`eval expr $1 2>&1`
     33 	if [ "$res" != "$2" ]; then
     34 		atf_fail "Expected $2, got $res from expression: " \
     35 		         "`eval echo $1`"
     36 	fi
     37 }
     38 
     39 atf_test_case lang
     40 lang_ops_head() {
     41 	atf_set "descr" "Test that expr(1) works with non-C LANG (PR bin/2486)"
     42 }
     43 lang_body() {
     44 
     45 	export LANG=nonexistent
     46 	atf_check -s exit:0 -o inline:"21\n" -e empty -x "expr 10 + 11"
     47 
     48 	export LANG=ru_RU.KOI8-R
     49 	atf_check -s exit:0 -o inline:"21\n" -e empty -x "expr 10 + 11"
     50 }
     51 
     52 atf_test_case overflow
     53 overflow_head() {
     54 	atf_set "descr" "Test overflow cases"
     55 }
     56 overflow_body() {
     57 	test_expr '4611686018427387904 + 4611686018427387903' \
     58 	          '9223372036854775807'
     59 	test_expr '4611686018427387904 + 4611686018427387904' \
     60 	          "expr: integer overflow or underflow occurred for operation '4611686018427387904 + 4611686018427387904'"
     61 	test_expr '4611686018427387904 - -4611686018427387904' \
     62 	          "expr: integer overflow or underflow occurred for operation '4611686018427387904 - -4611686018427387904'"
     63 	test_expr '-4611686018427387904 - 4611686018427387903' \
     64 	          '-9223372036854775807'
     65 	test_expr '-4611686018427387904 - 4611686018427387905' \
     66 	          "expr: integer overflow or underflow occurred for operation '-4611686018427387904 - 4611686018427387905'"
     67 	test_expr '-4611686018427387904 \* 1' '-4611686018427387904'
     68 	test_expr '-4611686018427387904 \* -1' '4611686018427387904'
     69 	test_expr '-4611686018427387904 \* 2' '-9223372036854775808'
     70 	test_expr '-4611686018427387904 \* 3' \
     71 	          "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * 3'"
     72 	test_expr '-4611686018427387904 \* -2' \
     73 	          "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * -2'"
     74 	test_expr '4611686018427387904 \* 1' '4611686018427387904'
     75 	test_expr '4611686018427387904 \* 2' \
     76 	          "expr: integer overflow or underflow occurred for operation '4611686018427387904 * 2'"
     77 	test_expr '4611686018427387904 \* 3' \
     78 	          "expr: integer overflow or underflow occurred for operation '4611686018427387904 * 3'"
     79 	test_expr '-9223372036854775808 % -1' \
     80 	          "expr: integer overflow or underflow occurred for operation '-9223372036854775808 % -1'"
     81 	test_expr '-9223372036854775808 / -1' \
     82 	          "expr: integer overflow or underflow occurred for operation '-9223372036854775808 / -1'"
     83 	test_expr '0 + -9223372036854775808' '-9223372036854775808'
     84 	test_expr '0 + -1' '-1'
     85 	test_expr '0 + 0' '0'
     86 	test_expr '0 + 1' '1'
     87 	test_expr '0 + 9223372036854775807' '9223372036854775807'
     88 	test_expr '-9223372036854775808 + 0' '-9223372036854775808'
     89 	test_expr '9223372036854775807 + 0' '9223372036854775807'
     90 	test_expr '4611686018427387904 \* -1' '-4611686018427387904'
     91 	test_expr '4611686018427387904 \* -2' '-9223372036854775808'
     92 	test_expr '4611686018427387904 \* -3' \
     93 	          "expr: integer overflow or underflow occurred for operation '4611686018427387904 * -3'"
     94 	test_expr '-4611686018427387904 \* -1' '4611686018427387904'
     95 	test_expr '-4611686018427387904 \* -2' \
     96 	          "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * -2'"
     97 	test_expr '-4611686018427387904 \* -3' \
     98 	          "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * -3'"
     99 }
    100 
    101 atf_test_case gtkmm
    102 gtkmm_head() {
    103 	atf_set "descr" "Test from gtk-- configure that cause problems on old expr"
    104 }
    105 gtkmm_body() {
    106 	test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 5' '1'
    107 	test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 6' '0'
    108 	test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 3 \& 5 \>= 5' '0'
    109 	test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 2 \& 4 = 4 \& 5 \>= 5' '0'
    110 	test_expr '3 \> 2 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 6' '1'
    111 	test_expr '3 \> 3 \| 3 = 3 \& 4 \> 3 \| 3 = 3 \& 4 = 4 \& 5 \>= 5' '1'
    112 }
    113 
    114 atf_test_case colon_vs_math
    115 colon_vs_math_head() {
    116 	atf_set "descr" "Basic precendence test with the : operator vs. math"
    117 }
    118 colon_vs_math_body() {
    119 	test_expr '2 : 4 / 2' '0'
    120 	test_expr '4 : 4 % 3' '1'
    121 }
    122 
    123 atf_test_case arithmetic_ops
    124 arithmetic_ops_head() {
    125 	atf_set "descr" "Dangling arithemtic operator"
    126 }
    127 arithmetic_ops_body() {
    128 	test_expr '.java_wrapper : /' '0'
    129 	test_expr '4 : \*' '0'
    130 	test_expr '4 : +' '0'
    131 	test_expr '4 : -' '0'
    132 	test_expr '4 : /' '0'
    133 	test_expr '4 : %' '0'
    134 }
    135 
    136 atf_test_case basic_math
    137 basic_math_head() {
    138 	atf_set "descr" "Basic math test"
    139 }
    140 basic_math_body() {
    141 	test_expr '2 + 4 \* 5' '22'
    142 }
    143 
    144 atf_test_case basic_functional
    145 basic_functional_head() {
    146 	atf_set "descr" "Basic functional tests"
    147 }
    148 basic_functional_body() {
    149 	test_expr '2' '2'
    150 	test_expr '-4' '-4'
    151 	test_expr 'hello' 'hello'
    152 }
    153 
    154 atf_test_case compare_ops_precedence
    155 compare_ops_precedence_head() {
    156 	atf_set "descr" "Compare operator precendence test"
    157 }
    158 compare_ops_precedence_body() {
    159 	test_expr '2 \> 1 \* 17' '0'
    160 }
    161 
    162 atf_test_case compare_ops
    163 compare_ops_head() {
    164 	atf_set "descr" "Compare operator tests"
    165 }
    166 compare_ops_body() {
    167 	test_expr '2 \!= 5' '1'
    168 	test_expr '2 \!= 2' '0'
    169 	test_expr '2 \<= 3' '1'
    170 	test_expr '2 \<= 2' '1'
    171 	test_expr '2 \<= 1' '0'
    172 	test_expr '2 \< 3' '1'
    173 	test_expr '2 \< 2' '0'
    174 	test_expr '2 = 2' '1'
    175 	test_expr '2 = 4' '0'
    176 	test_expr '2 \>= 1' '1'
    177 	test_expr '2 \>= 2' '1'
    178 	test_expr '2 \>= 3' '0'
    179 	test_expr '2 \> 1' '1'
    180 	test_expr '2 \> 2' '0'
    181 }
    182 
    183 atf_test_case multiply
    184 multiply_head() {
    185 	atf_set "descr" "Test the multiply operator (PR bin/12838)"
    186 }
    187 multiply_body() {
    188 	test_expr '1 \* -1' '-1'
    189 	test_expr '2 \> 1 \* 17' '0'
    190 }
    191 
    192 atf_test_case negative
    193 negative_head() {
    194 	atf_set "descr" "Test the additive inverse"
    195 }
    196 negative_body() {
    197 	test_expr '-1 + 5' '4'
    198 	test_expr '- 1 + 5' 'expr: syntax error'
    199 
    200 	test_expr '5 + -1' '4'
    201 	test_expr '5 + - 1' 'expr: syntax error'
    202 
    203 	test_expr '1 - -5' '6'
    204 }
    205 
    206 atf_test_case math_precedence
    207 math_precedence_head() {
    208 	atf_set "descr" "More complex math test for precedence"
    209 }
    210 math_precedence_body() {
    211 	test_expr '-3 + -1 \* 4 + 3 / -6' '-7'
    212 }
    213 
    214 atf_test_case precedence
    215 precedence_head() {
    216 	atf_set "descr" "Test precedence"
    217 }
    218 precedence_body() {
    219 	# This is messy but the shell escapes cause that
    220 	test_expr 'X1/2/3 : X\\\(.\*[^/]\\\)//\*[^/][^/]\*/\*$ \| . : \\\(.\\\)' '1/2'
    221 }
    222 
    223 atf_test_case regex
    224 regex_head() {
    225 	atf_set "descr" "Test proper () returning \1 from a regex"
    226 }
    227 regex_body() {
    228 	# This is messy but the shell escapes cause that
    229 	test_expr '1/2 : .\*/\\\(.\*\\\)' '2'
    230 }
    231 
    232 atf_init_test_cases()
    233 {
    234 	atf_add_test_case lang
    235 	atf_add_test_case overflow
    236 	atf_add_test_case gtkmm
    237 	atf_add_test_case colon_vs_math
    238 	atf_add_test_case arithmetic_ops
    239 	atf_add_test_case basic_math
    240 	atf_add_test_case basic_functional
    241 	atf_add_test_case compare_ops_precedence
    242 	atf_add_test_case compare_ops
    243 	atf_add_test_case multiply
    244 	atf_add_test_case negative
    245 	atf_add_test_case math_precedence
    246 	atf_add_test_case precedence
    247 	atf_add_test_case regex
    248 }
    249