summaryrefslogtreecommitdiff
path: root/corebinutils/expr/tests/test.sh
blob: 23728d5e3bfb96b6caebe42c455af578cbb99603 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/bin/sh
set -eu

ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
EXPR_BIN=${EXPR_BIN:-"$ROOT/out/expr"}
TMPDIR=${TMPDIR:-/tmp}
WORKDIR=$(mktemp -d "$TMPDIR/expr-test.XXXXXX")
trap 'rm -rf "$WORKDIR"' EXIT INT TERM

LC_ALL=C
LANG=C
export LC_ALL LANG

fail() {
	printf '%s\n' "FAIL: $1" >&2
	exit 1
}

assert_status() {
	name=$1
	expected=$2
	actual=$3

	[ "$expected" -eq "$actual" ] || fail "$name: expected exit $expected got $actual"
}

assert_file_match() {
	name=$1
	pattern=$2
	actual_file=$3

	if ! grep -Eq "$pattern" "$actual_file"; then
		printf '%s\n' "FAIL: $name" >&2
		printf '%s\n' "--- actual ---" >&2
		cat "$actual_file" >&2
		exit 1
	fi
}

assert_file_eq() {
	name=$1
	expected_text=$2
	actual_file=$3
	expected_file=$WORKDIR/expected.$$

	printf '%s' "$expected_text" > "$expected_file"
	if ! cmp -s "$expected_file" "$actual_file"; then
		printf '%s\n' "FAIL: $name" >&2
		printf '%s\n' "--- expected ---" >&2
		od -An -tx1 -v "$expected_file" >&2
		printf '%s\n' "--- actual ---" >&2
		od -An -tx1 -v "$actual_file" >&2
		exit 1
	fi
}

run_case() {
	name=$1
	expected_status=$2
	expected_stdout=$3
	expected_stderr=$4
	shift 4

	stdout_file=$WORKDIR/stdout
	stderr_file=$WORKDIR/stderr

	if "$EXPR_BIN" "$@" >"$stdout_file" 2>"$stderr_file"; then
		status=0
	else
		status=$?
	fi

	assert_status "$name status" "$expected_status" "$status"
	assert_file_eq "$name stdout" "$expected_stdout" "$stdout_file"
	assert_file_eq "$name stderr" "$expected_stderr" "$stderr_file"
}

run_case_match_stderr() {
	name=$1
	expected_status=$2
	expected_stdout=$3
	stderr_pattern=$4
	shift 4

	stdout_file=$WORKDIR/stdout
	stderr_file=$WORKDIR/stderr

	if "$EXPR_BIN" "$@" >"$stdout_file" 2>"$stderr_file"; then
		status=0
	else
		status=$?
	fi

	assert_status "$name status" "$expected_status" "$status"
	assert_file_eq "$name stdout" "$expected_stdout" "$stdout_file"
	assert_file_match "$name stderr" "$stderr_pattern" "$stderr_file"
}

derive_intmax_bounds() {
	power=1
	steps=0

	while next=$("$EXPR_BIN" "$power" "*" "2" 2>/dev/null); do
		power=$next
		steps=$((steps + 1))
		[ "$steps" -le 256 ] || fail "could not derive intmax bounds"
	done

	INTMAX_MAX_VALUE=$("$EXPR_BIN" "(" "$power" "-" "1" ")" "+" "$power")
	INTMAX_MIN_VALUE=$("$EXPR_BIN" "0" "-" "1" "-" "$INTMAX_MAX_VALUE")
	export INTMAX_MAX_VALUE INTMAX_MIN_VALUE
}

[ -x "$EXPR_BIN" ] || fail "missing binary: $EXPR_BIN"
derive_intmax_bounds

run_case "no arguments" 2 "" "usage: expr [-e] [--] expression
"
run_case "invalid option" 2 "" "usage: expr [-e] [--] expression
" "-x"
run_case "integer addition" 0 "7
" "" "3" "+" "4"
run_case "zero exit status" 1 "0
" "" "3" "-" "3"
run_case "parentheses precedence" 0 "21
" "" "(" "3" "+" "4" ")" "*" "3"
run_case "left associative subtraction" 0 "3
" "" "8" "-" "3" "-" "2"
run_case "left associative division" 0 "2
" "" "12" "/" "3" "/" "2"
run_case "division by zero" 2 "" "expr: division by zero
" "7" "/" "0"
run_case "remainder by zero" 2 "" "expr: division by zero
" "7" "%" "0"
run_case "type mismatch" 2 "" "expr: not a decimal number: 'abc'
" "abc" "+" "1"
run_case "too large operand" 2 "" "expr: operand too large: '999999999999999999999999999999999999'
" "999999999999999999999999999999999999" "+" "1"
run_case "addition overflow" 2 "" "expr: overflow
" "$INTMAX_MAX_VALUE" "+" "1"
run_case "subtraction overflow" 2 "" "expr: overflow
" "--" "$INTMAX_MIN_VALUE" "-" "1"
run_case "multiplication overflow" 2 "" "expr: overflow
" "$INTMAX_MAX_VALUE" "*" "2"
run_case "division overflow" 2 "" "expr: overflow
" "--" "$INTMAX_MIN_VALUE" "/" "-1"
run_case "string comparison" 0 "1
" "" "b" ">" "a"
run_case "numeric string comparison" 0 "1
" "" "--" "09" "=" "9"
run_case "strict plus stays string" 1 "0
" "" "--" "+7" "=" "7"
run_case "logical or keeps left value" 0 "left
" "" "left" "|" "right"
run_case "logical or returns right" 0 "fallback
" "" "0" "|" "fallback"
run_case "logical and keeps left value" 0 "left
" "" "left" "&" "right"
run_case "logical and returns zero" 1 "0
" "" "" "&" "right"
run_case "regex length" 0 "6
" "" "abcdef" ":" "abc.*"
run_case "regex capture" 0 "de
" "" "abcde" ":" "abc\\(..\\)"
run_case "regex anchor" 1 "0
" "" "abcde" ":" "bc"
run_case "regex capture miss" 1 "
" "" "abcde" ":" "zzz\\(..\\)"
run_case_match_stderr "regex syntax error" 2 "" '^expr: regular expression error: ' "abc" ":" "[["
run_case "strict negative needs --" 2 "" "usage: expr [-e] [--] expression
" "-1" "+" "2"
run_case "negative with --" 0 "1
" "" "--" "-1" "+" "2"
run_case "compat still needs -- for leading negative operand" 2 "" "usage: expr [-e] [--] expression
" "-e" "-1" "+" "2"
run_case "compat leading plus" 0 "1
" "" "-e" " +7" "=" "7"
run_case "compat empty string as zero" 0 "1
" "" "-e" "" "+" "1"
run_case "compat empty string compares as zero" 0 "1
" "" "-e" "" "=" "0"
run_case "syntax error" 2 "" "expr: syntax error
" "1" "+"
run_case "trailing token syntax error" 2 "" "expr: syntax error
" "1" "2"
run_case "missing closing parenthesis" 2 "" "expr: syntax error
" "(" "1" "+" "2"

stdout_file=$WORKDIR/write-fail.stdout
stderr_file=$WORKDIR/write-fail.stderr
if (
	exec 1>&-
	"$EXPR_BIN" "write-test"
) >"$stdout_file" 2>"$stderr_file"; then
	status=0
else
	status=$?
fi
assert_status "write failure status" 2 "$status"
assert_file_eq "write failure stdout" "" "$stdout_file"
case $(cat "$stderr_file") in
	"expr: write failed: "* ) ;;
	* ) fail "write failure stderr" ;;
esac

printf '%s\n' "PASS"