Home | History | Annotate | Download | only in tests
      1 #!/bin/bash
      2 
      3 [ -f testing.sh ] && . testing.sh
      4 
      5 testing "integer" "expr 5" "5\n" "" ""
      6 testing "integer negative" "expr -5" "-5\n" "" ""
      7 testing "string" "expr astring" "astring\n" "" ""
      8 testing "addition" "expr 1 + 3" "4\n" "" ""
      9 testing "5 + 6 * 3" "expr 5 + 6 \* 3" "23\n" "" ""
     10 testing "( 5 + 6 ) * 3" "expr \( 5 + 6 \) \* 3" "33\n" "" ""
     11 testing ">" "expr 3 \> 2" "1\n" "" ""
     12 testing "* / same priority" "expr 4 \* 3 / 2"  "6\n" "" ""
     13 testing "/ * same priority" "expr 3 / 2 \* 4" "4\n" "" ""
     14 testing "& before |" "expr 0 \| 1 \& 0" "0\n" "" ""
     15 testing "| after &" "expr 1 \| 0 \& 0" "1\n" "" ""
     16 testing "| & same priority" "expr 0 \& 0 \| 1" "1\n" "" ""
     17 testing "% * same priority" "expr 3 % 2 \* 4" "4\n" "" ""
     18 testing "* % same priority" "expr 3 \* 2 % 4" "2\n" "" ""
     19 testing "= > same priority" "expr 0 = 2 \> 3" "0\n" "" ""
     20 testing "> = same priority" "expr 3 \> 2 = 1" "1\n" "" ""
     21 
     22 testing "00 | 1" "expr 00 \| 1" "1\n" "" ""
     23 testing "-0 | 1" "expr -0 \| 2" "2\n" "" ""
     24 testing '"" | 1' 'expr "" \| 3' "3\n" "" ""
     25 testing "/ by zero" "expr 1 / 0 2>/dev/null; echo \$?" "2\n" "" ""
     26 testing "% by zero" "expr 1 % 0 2>/dev/null; echo \$?" "2\n" "" ""
     27 
     28 testing "regex position" "expr ab21xx : '[^0-9]*[0-9]*'" "4\n" "" ""
     29 testing "regex extraction" "expr ab21xx : '[^0-9]*\([0-9]*\).*'" "21\n" "" ""
     30 testing "regex no match" "expr ab21xx : x" "0\n" "" ""
     31 testing "long str" "expr abcdefghijklmnopqrstuvwxyz : '\(.*\)' = a" "0\n" "" ""
     32 
     33 # result of ':' regex match can subsequently be used for arithmetic
     34 testing "string becomes integer" "expr ab21xx : '[^0-9]*\([0-9]*\)' + 3" \
     35 	"24\n" "" ""
     36 
     37 testing "integer comparison" "expr -3 \< -2" "1\n" "" ""
     38 testing "string comparison" "expr -3 \< -2s" "0\n" "" ""
     39 testing "integer expression comparison" "expr 2 - 5 \< -2" "1\n" "" ""
     40 # result of arithmetic can subsequently be compared as a string
     41 testing "string expr comparison" "expr 2 - 5 \< -2s" "0\n" "" ""
     42 
     43 testing "parens around literal" "expr \( a \)" "a\n" "" ""
     44 
     45 testing "exit code when true" "expr a; echo \$?" "a\n0\n" "" ""
     46 testing "exit code when false" "expr 0; echo \$?" "0\n1\n" "" ""
     47 testing "exit code with syntax error" "expr \( 2>/dev/null; echo \$?" \
     48 	"2\n" "" ""
     49 testing "exit code when evaluating to 0" "expr -1 + 1; echo \$?" "0\n1\n" "" ""
     50 
     51 # BUG: segfaults because '3' is coerced to integer and regexc gets NULL
     52 testing "regex segfault" "expr 3 : '\(.\)'" "3\n" "" ""
     53 
     54 # syntax errors
     55 testing "no expression" "expr 2>/dev/null; echo \$?" "2\n" "" ""
     56 testing "empty ()" "expr \( \) 2>/dev/null; echo \$?" "2\n" "" ""
     57 testing "missing )" "expr \( 1 2>/dev/null; echo \$?" "2\n" "" ""
     58 testing "missing outer )" "expr \( 1 + \( 2 \* 3 \) 2>/dev/null; echo \$?" \
     59 	"2\n" "" ""
     60 testing "bad operator" "expr 1 @ 2 2>/dev/null; echo \$?" "2\n" "" ""
     61 testing "adjacent literals" "expr 1 2 2>/dev/null; echo \$?" "2\n" "" ""
     62 testing "non-integer argument" "expr 1 + a 2>/dev/null; echo \$?" "2\n" "" ""
     63