Rob Landley | 32526f2 | 2013-06-05 00:59:01 -0500 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | [ -f testing.sh ] && . testing.sh |
| 4 | |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 5 | testing "integer" "expr 5" "5\n" "" "" |
| 6 | testing "integer negative" "expr -5" "-5\n" "" "" |
| 7 | testing "string" "expr astring" "astring\n" "" "" |
Andy Chu | e6912f9 | 2016-03-15 13:52:25 -0700 | [diff] [blame] | 8 | testing "addition" "expr 1 + 3" "4\n" "" "" |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 9 | testing "5 + 6 * 3" "expr 5 + 6 \* 3" "23\n" "" "" |
| 10 | testing "( 5 + 6 ) * 3" "expr \( 5 + 6 \) \* 3" "33\n" "" "" |
Andy Chu | e6912f9 | 2016-03-15 13:52:25 -0700 | [diff] [blame] | 11 | testing ">" "expr 3 \> 2" "1\n" "" "" |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 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" "" "" |
Andy Chu | 14c91c1 | 2016-03-15 13:42:30 -0700 | [diff] [blame] | 21 | |
Rob Landley | 6aba814 | 2016-03-20 20:39:27 -0500 | [diff] [blame] | 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" "" "" |
Rob Landley | 1af3bad | 2016-03-16 12:33:32 -0500 | [diff] [blame] | 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" "" "" |
Andy Chu | 14c91c1 | 2016-03-15 13:42:30 -0700 | [diff] [blame] | 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" "" "" |
Andy Chu | e6912f9 | 2016-03-15 13:52:25 -0700 | [diff] [blame] | 31 | testing "long str" "expr abcdefghijklmnopqrstuvwxyz : '\(.*\)' = a" "0\n" "" "" |
Andy Chu | 14c91c1 | 2016-03-15 13:42:30 -0700 | [diff] [blame] | 32 | |
| 33 | # result of ':' regex match can subsequently be used for arithmetic |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 34 | testing "string becomes integer" "expr ab21xx : '[^0-9]*\([0-9]*\)' + 3" \ |
Rob Landley | ef6a773 | 2014-12-01 03:15:25 -0600 | [diff] [blame] | 35 | "24\n" "" "" |
Andy Chu | 14c91c1 | 2016-03-15 13:42:30 -0700 | [diff] [blame] | 36 | |
| 37 | testing "integer comparison" "expr -3 \< -2" "1\n" "" "" |
| 38 | testing "string comparison" "expr -3 \< -2s" "0\n" "" "" |
Andy Chu | e6912f9 | 2016-03-15 13:52:25 -0700 | [diff] [blame] | 39 | testing "integer expression comparison" "expr 2 - 5 \< -2" "1\n" "" "" |
Andy Chu | 14c91c1 | 2016-03-15 13:42:30 -0700 | [diff] [blame] | 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" "" "" |
Rob Landley | 1af3bad | 2016-03-16 12:33:32 -0500 | [diff] [blame] | 47 | testing "exit code with syntax error" "expr \( 2>/dev/null; echo \$?" \ |
| 48 | "2\n" "" "" |
Andy Chu | 14c91c1 | 2016-03-15 13:42:30 -0700 | [diff] [blame] | 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 |
Rob Landley | 1af3bad | 2016-03-16 12:33:32 -0500 | [diff] [blame] | 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" "" "" |