OSDN Git Service

Remove 'exit' from xargs test too.
[android-x86/external-toybox.git] / tests / expr.test
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 "/ 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
25 testing "regex position" "expr ab21xx : '[^0-9]*[0-9]*'" "4\n" "" ""
26 testing "regex extraction" "expr ab21xx : '[^0-9]*\([0-9]*\).*'" "21\n" "" ""
27 testing "regex no match" "expr ab21xx : x" "0\n" "" ""
28 testing "long str" "expr abcdefghijklmnopqrstuvwxyz : '\(.*\)' = a" "0\n" "" ""
29
30 # result of ':' regex match can subsequently be used for arithmetic
31 testing "string becomes integer" "expr ab21xx : '[^0-9]*\([0-9]*\)' + 3" \
32         "24\n" "" ""
33
34 testing "integer comparison" "expr -3 \< -2" "1\n" "" ""
35 testing "string comparison" "expr -3 \< -2s" "0\n" "" ""
36 testing "integer expression comparison" "expr 2 - 5 \< -2" "1\n" "" ""
37 # result of arithmetic can subsequently be compared as a string
38 testing "string expr comparison" "expr 2 - 5 \< -2s" "0\n" "" ""
39
40 testing "parens around literal" "expr \( a \)" "a\n" "" ""
41
42 testing "exit code when true" "expr a; echo \$?" "a\n0\n" "" ""
43 testing "exit code when false" "expr 0; echo \$?" "0\n1\n" "" ""
44 testing "exit code with syntax error" "expr \( 2>/dev/null; echo \$?" \
45         "2\n" "" ""
46 testing "exit code when evaluating to 0" "expr -1 + 1; echo \$?" "0\n1\n" "" ""
47
48 # BUG: segfaults because '3' is coerced to integer and regexc gets NULL
49 testing "regex segfault" "expr 3 : '\(.\)'" "3\n" "" ""
50
51 # syntax errors
52 testing "no expression" "expr 2>/dev/null; echo \$?" "2\n" "" ""
53 testing "empty ()" "expr \( \) 2>/dev/null; echo \$?" "2\n" "" ""
54 testing "missing )" "expr \( 1 2>/dev/null; echo \$?" "2\n" "" ""
55 testing "missing outer )" "expr \( 1 + \( 2 \* 3 \) 2>/dev/null; echo \$?" \
56         "2\n" "" ""
57 testing "bad operator" "expr 1 @ 2 2>/dev/null; echo \$?" "2\n" "" ""
58 testing "adjacent literals" "expr 1 2 2>/dev/null; echo \$?" "2\n" "" ""
59 testing "non-integer argument" "expr 1 + a 2>/dev/null; echo \$?" "2\n" "" ""