t_expr.sh revision 1.5 1 1.5 kamil # $NetBSD: t_expr.sh,v 1.5 2018/06/27 17:14:47 kamil Exp $
2 1.1 jruoho #
3 1.1 jruoho # Copyright (c) 2007 The NetBSD Foundation, Inc.
4 1.1 jruoho # All rights reserved.
5 1.1 jruoho #
6 1.1 jruoho # Redistribution and use in source and binary forms, with or without
7 1.1 jruoho # modification, are permitted provided that the following conditions
8 1.1 jruoho # are met:
9 1.1 jruoho # 1. Redistributions of source code must retain the above copyright
10 1.1 jruoho # notice, this list of conditions and the following disclaimer.
11 1.1 jruoho # 2. Redistributions in binary form must reproduce the above copyright
12 1.1 jruoho # notice, this list of conditions and the following disclaimer in the
13 1.1 jruoho # documentation and/or other materials provided with the distribution.
14 1.1 jruoho #
15 1.1 jruoho # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 1.1 jruoho # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 1.1 jruoho # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 1.1 jruoho # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 1.1 jruoho # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 1.1 jruoho # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 1.1 jruoho # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 1.1 jruoho # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 1.1 jruoho # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 1.1 jruoho # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1 jruoho # POSSIBILITY OF SUCH DAMAGE.
26 1.1 jruoho #
27 1.1 jruoho
28 1.1 jruoho # The first arg will get eval'd so escape any meta characters
29 1.1 jruoho # The 2nd arg is an expected string/response from expr for that op.
30 1.1 jruoho test_expr() {
31 1.1 jruoho echo "Expression '${1}', expecting '${2}'"
32 1.1 jruoho res=`eval expr $1 2>&1`
33 1.1 jruoho if [ "$res" != "$2" ]; then
34 1.1 jruoho atf_fail "Expected $2, got $res from expression: " \
35 1.1 jruoho "`eval echo $1`"
36 1.1 jruoho fi
37 1.1 jruoho }
38 1.1 jruoho
39 1.3 jruoho atf_test_case lang
40 1.3 jruoho lang_ops_head() {
41 1.3 jruoho atf_set "descr" "Test that expr(1) works with non-C LANG (PR bin/2486)"
42 1.3 jruoho }
43 1.3 jruoho lang_body() {
44 1.3 jruoho
45 1.3 jruoho export LANG=nonexistent
46 1.3 jruoho atf_check -s exit:0 -o inline:"21\n" -e empty -x "expr 10 + 11"
47 1.3 jruoho
48 1.3 jruoho export LANG=ru_RU.KOI8-R
49 1.3 jruoho atf_check -s exit:0 -o inline:"21\n" -e empty -x "expr 10 + 11"
50 1.3 jruoho }
51 1.3 jruoho
52 1.1 jruoho atf_test_case overflow
53 1.1 jruoho overflow_head() {
54 1.1 jruoho atf_set "descr" "Test overflow cases"
55 1.1 jruoho }
56 1.1 jruoho overflow_body() {
57 1.1 jruoho test_expr '4611686018427387904 + 4611686018427387903' \
58 1.1 jruoho '9223372036854775807'
59 1.1 jruoho test_expr '4611686018427387904 + 4611686018427387904' \
60 1.1 jruoho "expr: integer overflow or underflow occurred for operation '4611686018427387904 + 4611686018427387904'"
61 1.1 jruoho test_expr '4611686018427387904 - -4611686018427387904' \
62 1.1 jruoho "expr: integer overflow or underflow occurred for operation '4611686018427387904 - -4611686018427387904'"
63 1.1 jruoho test_expr '-4611686018427387904 - 4611686018427387903' \
64 1.1 jruoho '-9223372036854775807'
65 1.1 jruoho test_expr '-4611686018427387904 - 4611686018427387905' \
66 1.1 jruoho "expr: integer overflow or underflow occurred for operation '-4611686018427387904 - 4611686018427387905'"
67 1.1 jruoho test_expr '-4611686018427387904 \* 1' '-4611686018427387904'
68 1.1 jruoho test_expr '-4611686018427387904 \* -1' '4611686018427387904'
69 1.1 jruoho test_expr '-4611686018427387904 \* 2' '-9223372036854775808'
70 1.1 jruoho test_expr '-4611686018427387904 \* 3' \
71 1.1 jruoho "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * 3'"
72 1.1 jruoho test_expr '-4611686018427387904 \* -2' \
73 1.1 jruoho "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * -2'"
74 1.1 jruoho test_expr '4611686018427387904 \* 1' '4611686018427387904'
75 1.1 jruoho test_expr '4611686018427387904 \* 2' \
76 1.1 jruoho "expr: integer overflow or underflow occurred for operation '4611686018427387904 * 2'"
77 1.1 jruoho test_expr '4611686018427387904 \* 3' \
78 1.1 jruoho "expr: integer overflow or underflow occurred for operation '4611686018427387904 * 3'"
79 1.4 kamil test_expr '-9223372036854775808 % -1' \
80 1.4 kamil "expr: integer overflow or underflow occurred for operation '-9223372036854775808 % -1'"
81 1.4 kamil test_expr '-9223372036854775808 / -1' \
82 1.4 kamil "expr: integer overflow or underflow occurred for operation '-9223372036854775808 / -1'"
83 1.5 kamil test_expr '0 + -9223372036854775808' '-9223372036854775808'
84 1.5 kamil test_expr '0 + -1' '-1'
85 1.5 kamil test_expr '0 + 0' '0'
86 1.5 kamil test_expr '0 + 1' '1'
87 1.5 kamil test_expr '0 + 9223372036854775807' '9223372036854775807'
88 1.5 kamil test_expr '-9223372036854775808 + 0' '-9223372036854775808'
89 1.5 kamil test_expr '9223372036854775807 + 0' '9223372036854775807'
90 1.5 kamil test_expr '4611686018427387904 \* -1' '-4611686018427387904'
91 1.5 kamil test_expr '4611686018427387904 \* -2' '-9223372036854775808'
92 1.5 kamil test_expr '4611686018427387904 \* -3' \
93 1.5 kamil "expr: integer overflow or underflow occurred for operation '4611686018427387904 * -3'"
94 1.5 kamil test_expr '-4611686018427387904 \* -1' '4611686018427387904'
95 1.5 kamil test_expr '-4611686018427387904 \* -2' \
96 1.5 kamil "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * -2'"
97 1.5 kamil test_expr '-4611686018427387904 \* -3' \
98 1.5 kamil "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * -3'"
99 1.1 jruoho }
100 1.1 jruoho
101 1.1 jruoho atf_test_case gtkmm
102 1.1 jruoho gtkmm_head() {
103 1.1 jruoho atf_set "descr" "Test from gtk-- configure that cause problems on old expr"
104 1.1 jruoho }
105 1.1 jruoho gtkmm_body() {
106 1.1 jruoho test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 5' '1'
107 1.1 jruoho test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 6' '0'
108 1.1 jruoho test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 3 \& 5 \>= 5' '0'
109 1.1 jruoho test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 2 \& 4 = 4 \& 5 \>= 5' '0'
110 1.1 jruoho test_expr '3 \> 2 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 6' '1'
111 1.1 jruoho test_expr '3 \> 3 \| 3 = 3 \& 4 \> 3 \| 3 = 3 \& 4 = 4 \& 5 \>= 5' '1'
112 1.1 jruoho }
113 1.1 jruoho
114 1.1 jruoho atf_test_case colon_vs_math
115 1.1 jruoho colon_vs_math_head() {
116 1.1 jruoho atf_set "descr" "Basic precendence test with the : operator vs. math"
117 1.1 jruoho }
118 1.1 jruoho colon_vs_math_body() {
119 1.1 jruoho test_expr '2 : 4 / 2' '0'
120 1.1 jruoho test_expr '4 : 4 % 3' '1'
121 1.1 jruoho }
122 1.1 jruoho
123 1.1 jruoho atf_test_case arithmetic_ops
124 1.1 jruoho arithmetic_ops_head() {
125 1.1 jruoho atf_set "descr" "Dangling arithemtic operator"
126 1.1 jruoho }
127 1.1 jruoho arithmetic_ops_body() {
128 1.1 jruoho test_expr '.java_wrapper : /' '0'
129 1.1 jruoho test_expr '4 : \*' '0'
130 1.1 jruoho test_expr '4 : +' '0'
131 1.1 jruoho test_expr '4 : -' '0'
132 1.1 jruoho test_expr '4 : /' '0'
133 1.1 jruoho test_expr '4 : %' '0'
134 1.1 jruoho }
135 1.1 jruoho
136 1.1 jruoho atf_test_case basic_math
137 1.1 jruoho basic_math_head() {
138 1.1 jruoho atf_set "descr" "Basic math test"
139 1.1 jruoho }
140 1.1 jruoho basic_math_body() {
141 1.1 jruoho test_expr '2 + 4 \* 5' '22'
142 1.1 jruoho }
143 1.1 jruoho
144 1.1 jruoho atf_test_case basic_functional
145 1.1 jruoho basic_functional_head() {
146 1.1 jruoho atf_set "descr" "Basic functional tests"
147 1.1 jruoho }
148 1.1 jruoho basic_functional_body() {
149 1.1 jruoho test_expr '2' '2'
150 1.1 jruoho test_expr '-4' '-4'
151 1.1 jruoho test_expr 'hello' 'hello'
152 1.1 jruoho }
153 1.1 jruoho
154 1.1 jruoho atf_test_case compare_ops_precedence
155 1.1 jruoho compare_ops_precedence_head() {
156 1.1 jruoho atf_set "descr" "Compare operator precendence test"
157 1.1 jruoho }
158 1.1 jruoho compare_ops_precedence_body() {
159 1.1 jruoho test_expr '2 \> 1 \* 17' '0'
160 1.1 jruoho }
161 1.1 jruoho
162 1.1 jruoho atf_test_case compare_ops
163 1.1 jruoho compare_ops_head() {
164 1.1 jruoho atf_set "descr" "Compare operator tests"
165 1.1 jruoho }
166 1.1 jruoho compare_ops_body() {
167 1.1 jruoho test_expr '2 \!= 5' '1'
168 1.1 jruoho test_expr '2 \!= 2' '0'
169 1.1 jruoho test_expr '2 \<= 3' '1'
170 1.1 jruoho test_expr '2 \<= 2' '1'
171 1.1 jruoho test_expr '2 \<= 1' '0'
172 1.1 jruoho test_expr '2 \< 3' '1'
173 1.1 jruoho test_expr '2 \< 2' '0'
174 1.1 jruoho test_expr '2 = 2' '1'
175 1.1 jruoho test_expr '2 = 4' '0'
176 1.1 jruoho test_expr '2 \>= 1' '1'
177 1.1 jruoho test_expr '2 \>= 2' '1'
178 1.1 jruoho test_expr '2 \>= 3' '0'
179 1.1 jruoho test_expr '2 \> 1' '1'
180 1.1 jruoho test_expr '2 \> 2' '0'
181 1.1 jruoho }
182 1.1 jruoho
183 1.1 jruoho atf_test_case multiply
184 1.1 jruoho multiply_head() {
185 1.2 jruoho atf_set "descr" "Test the multiply operator (PR bin/12838)"
186 1.1 jruoho }
187 1.1 jruoho multiply_body() {
188 1.1 jruoho test_expr '1 \* -1' '-1'
189 1.2 jruoho test_expr '2 \> 1 \* 17' '0'
190 1.1 jruoho }
191 1.1 jruoho
192 1.1 jruoho atf_test_case negative
193 1.1 jruoho negative_head() {
194 1.1 jruoho atf_set "descr" "Test the additive inverse"
195 1.1 jruoho }
196 1.1 jruoho negative_body() {
197 1.1 jruoho test_expr '-1 + 5' '4'
198 1.1 jruoho test_expr '- 1 + 5' 'expr: syntax error'
199 1.1 jruoho
200 1.1 jruoho test_expr '5 + -1' '4'
201 1.1 jruoho test_expr '5 + - 1' 'expr: syntax error'
202 1.1 jruoho
203 1.1 jruoho test_expr '1 - -5' '6'
204 1.1 jruoho }
205 1.1 jruoho
206 1.1 jruoho atf_test_case math_precedence
207 1.1 jruoho math_precedence_head() {
208 1.1 jruoho atf_set "descr" "More complex math test for precedence"
209 1.1 jruoho }
210 1.1 jruoho math_precedence_body() {
211 1.1 jruoho test_expr '-3 + -1 \* 4 + 3 / -6' '-7'
212 1.1 jruoho }
213 1.1 jruoho
214 1.1 jruoho atf_test_case precedence
215 1.1 jruoho precedence_head() {
216 1.1 jruoho atf_set "descr" "Test precedence"
217 1.1 jruoho }
218 1.1 jruoho precedence_body() {
219 1.1 jruoho # This is messy but the shell escapes cause that
220 1.1 jruoho test_expr 'X1/2/3 : X\\\(.\*[^/]\\\)//\*[^/][^/]\*/\*$ \| . : \\\(.\\\)' '1/2'
221 1.1 jruoho }
222 1.1 jruoho
223 1.1 jruoho atf_test_case regex
224 1.1 jruoho regex_head() {
225 1.1 jruoho atf_set "descr" "Test proper () returning \1 from a regex"
226 1.1 jruoho }
227 1.1 jruoho regex_body() {
228 1.1 jruoho # This is messy but the shell escapes cause that
229 1.1 jruoho test_expr '1/2 : .\*/\\\(.\*\\\)' '2'
230 1.1 jruoho }
231 1.1 jruoho
232 1.1 jruoho atf_init_test_cases()
233 1.1 jruoho {
234 1.3 jruoho atf_add_test_case lang
235 1.1 jruoho atf_add_test_case overflow
236 1.1 jruoho atf_add_test_case gtkmm
237 1.1 jruoho atf_add_test_case colon_vs_math
238 1.1 jruoho atf_add_test_case arithmetic_ops
239 1.1 jruoho atf_add_test_case basic_math
240 1.1 jruoho atf_add_test_case basic_functional
241 1.1 jruoho atf_add_test_case compare_ops_precedence
242 1.1 jruoho atf_add_test_case compare_ops
243 1.1 jruoho atf_add_test_case multiply
244 1.1 jruoho atf_add_test_case negative
245 1.1 jruoho atf_add_test_case math_precedence
246 1.1 jruoho atf_add_test_case precedence
247 1.1 jruoho atf_add_test_case regex
248 1.1 jruoho }
249