Home | History | Annotate | Line # | Download | only in expr
t_expr.sh revision 1.2
      1 # $NetBSD: t_expr.sh,v 1.2 2012/03/20 06:30:02 jruoho 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 overflow
     40 overflow_head() {
     41 	atf_set "descr" "Test overflow cases"
     42 }
     43 overflow_body() {
     44 	test_expr '4611686018427387904 + 4611686018427387903' \
     45 	          '9223372036854775807'
     46 	test_expr '4611686018427387904 + 4611686018427387904' \
     47 	          "expr: integer overflow or underflow occurred for operation '4611686018427387904 + 4611686018427387904'"
     48 	test_expr '4611686018427387904 - -4611686018427387904' \
     49 	          "expr: integer overflow or underflow occurred for operation '4611686018427387904 - -4611686018427387904'"
     50 	test_expr '-4611686018427387904 - 4611686018427387903' \
     51 	          '-9223372036854775807'
     52 	test_expr '-4611686018427387904 - 4611686018427387905' \
     53 	          "expr: integer overflow or underflow occurred for operation '-4611686018427387904 - 4611686018427387905'"
     54 	test_expr '-4611686018427387904 \* 1' '-4611686018427387904'
     55 	test_expr '-4611686018427387904 \* -1' '4611686018427387904'
     56 	test_expr '-4611686018427387904 \* 2' '-9223372036854775808'
     57 	test_expr '-4611686018427387904 \* 3' \
     58 	          "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * 3'"
     59 	test_expr '-4611686018427387904 \* -2' \
     60 	          "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * -2'"
     61 	test_expr '4611686018427387904 \* 1' '4611686018427387904'
     62 	test_expr '4611686018427387904 \* 2' \
     63 	          "expr: integer overflow or underflow occurred for operation '4611686018427387904 * 2'"
     64 	test_expr '4611686018427387904 \* 3' \
     65 	          "expr: integer overflow or underflow occurred for operation '4611686018427387904 * 3'"
     66 }
     67 
     68 atf_test_case gtkmm
     69 gtkmm_head() {
     70 	atf_set "descr" "Test from gtk-- configure that cause problems on old expr"
     71 }
     72 gtkmm_body() {
     73 	test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 5' '1'
     74 	test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 6' '0'
     75 	test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 3 \& 5 \>= 5' '0'
     76 	test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 2 \& 4 = 4 \& 5 \>= 5' '0'
     77 	test_expr '3 \> 2 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 6' '1'
     78 	test_expr '3 \> 3 \| 3 = 3 \& 4 \> 3 \| 3 = 3 \& 4 = 4 \& 5 \>= 5' '1'
     79 }
     80 
     81 atf_test_case colon_vs_math
     82 colon_vs_math_head() {
     83 	atf_set "descr" "Basic precendence test with the : operator vs. math"
     84 }
     85 colon_vs_math_body() {
     86 	test_expr '2 : 4 / 2' '0'
     87 	test_expr '4 : 4 % 3' '1'
     88 }
     89 
     90 atf_test_case arithmetic_ops
     91 arithmetic_ops_head() {
     92 	atf_set "descr" "Dangling arithemtic operator"
     93 }
     94 arithmetic_ops_body() {
     95 	test_expr '.java_wrapper : /' '0'
     96 	test_expr '4 : \*' '0'
     97 	test_expr '4 : +' '0'
     98 	test_expr '4 : -' '0'
     99 	test_expr '4 : /' '0'
    100 	test_expr '4 : %' '0'
    101 }
    102 
    103 atf_test_case basic_math
    104 basic_math_head() {
    105 	atf_set "descr" "Basic math test"
    106 }
    107 basic_math_body() {
    108 	test_expr '2 + 4 \* 5' '22'
    109 }
    110 
    111 atf_test_case basic_functional
    112 basic_functional_head() {
    113 	atf_set "descr" "Basic functional tests"
    114 }
    115 basic_functional_body() {
    116 	test_expr '2' '2'
    117 	test_expr '-4' '-4'
    118 	test_expr 'hello' 'hello'
    119 }
    120 
    121 atf_test_case compare_ops_precedence
    122 compare_ops_precedence_head() {
    123 	atf_set "descr" "Compare operator precendence test"
    124 }
    125 compare_ops_precedence_body() {
    126 	test_expr '2 \> 1 \* 17' '0'
    127 }
    128 
    129 atf_test_case compare_ops
    130 compare_ops_head() {
    131 	atf_set "descr" "Compare operator tests"
    132 }
    133 compare_ops_body() {
    134 	test_expr '2 \!= 5' '1'
    135 	test_expr '2 \!= 2' '0'
    136 	test_expr '2 \<= 3' '1'
    137 	test_expr '2 \<= 2' '1'
    138 	test_expr '2 \<= 1' '0'
    139 	test_expr '2 \< 3' '1'
    140 	test_expr '2 \< 2' '0'
    141 	test_expr '2 = 2' '1'
    142 	test_expr '2 = 4' '0'
    143 	test_expr '2 \>= 1' '1'
    144 	test_expr '2 \>= 2' '1'
    145 	test_expr '2 \>= 3' '0'
    146 	test_expr '2 \> 1' '1'
    147 	test_expr '2 \> 2' '0'
    148 }
    149 
    150 atf_test_case multiply
    151 multiply_head() {
    152 	atf_set "descr" "Test the multiply operator (PR bin/12838)"
    153 }
    154 multiply_body() {
    155 	test_expr '1 \* -1' '-1'
    156 	test_expr '2 \> 1 \* 17' '0'
    157 }
    158 
    159 atf_test_case negative
    160 negative_head() {
    161 	atf_set "descr" "Test the additive inverse"
    162 }
    163 negative_body() {
    164 	test_expr '-1 + 5' '4'
    165 	test_expr '- 1 + 5' 'expr: syntax error'
    166 
    167 	test_expr '5 + -1' '4'
    168 	test_expr '5 + - 1' 'expr: syntax error'
    169 
    170 	test_expr '1 - -5' '6'
    171 }
    172 
    173 atf_test_case math_precedence
    174 math_precedence_head() {
    175 	atf_set "descr" "More complex math test for precedence"
    176 }
    177 math_precedence_body() {
    178 	test_expr '-3 + -1 \* 4 + 3 / -6' '-7'
    179 }
    180 
    181 atf_test_case precedence
    182 precedence_head() {
    183 	atf_set "descr" "Test precedence"
    184 }
    185 precedence_body() {
    186 	# This is messy but the shell escapes cause that
    187 	test_expr 'X1/2/3 : X\\\(.\*[^/]\\\)//\*[^/][^/]\*/\*$ \| . : \\\(.\\\)' '1/2'
    188 }
    189 
    190 atf_test_case regex
    191 regex_head() {
    192 	atf_set "descr" "Test proper () returning \1 from a regex"
    193 }
    194 regex_body() {
    195 	# This is messy but the shell escapes cause that
    196 	test_expr '1/2 : .\*/\\\(.\*\\\)' '2'
    197 }
    198 
    199 atf_init_test_cases()
    200 {
    201 	atf_add_test_case overflow
    202 	atf_add_test_case gtkmm
    203 	atf_add_test_case colon_vs_math
    204 	atf_add_test_case arithmetic_ops
    205 	atf_add_test_case basic_math
    206 	atf_add_test_case basic_functional
    207 	atf_add_test_case compare_ops_precedence
    208 	atf_add_test_case compare_ops
    209 	atf_add_test_case multiply
    210 	atf_add_test_case negative
    211 	atf_add_test_case math_precedence
    212 	atf_add_test_case precedence
    213 	atf_add_test_case regex
    214 }
    215