expr.test 2.9 KB

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