t_expr.sh revision 1.2 1 1.2 jruoho # $NetBSD: t_expr.sh,v 1.2 2012/03/20 06:30:02 jruoho 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.1 jruoho atf_test_case overflow
40 1.1 jruoho overflow_head() {
41 1.1 jruoho atf_set "descr" "Test overflow cases"
42 1.1 jruoho }
43 1.1 jruoho overflow_body() {
44 1.1 jruoho test_expr '4611686018427387904 + 4611686018427387903' \
45 1.1 jruoho '9223372036854775807'
46 1.1 jruoho test_expr '4611686018427387904 + 4611686018427387904' \
47 1.1 jruoho "expr: integer overflow or underflow occurred for operation '4611686018427387904 + 4611686018427387904'"
48 1.1 jruoho test_expr '4611686018427387904 - -4611686018427387904' \
49 1.1 jruoho "expr: integer overflow or underflow occurred for operation '4611686018427387904 - -4611686018427387904'"
50 1.1 jruoho test_expr '-4611686018427387904 - 4611686018427387903' \
51 1.1 jruoho '-9223372036854775807'
52 1.1 jruoho test_expr '-4611686018427387904 - 4611686018427387905' \
53 1.1 jruoho "expr: integer overflow or underflow occurred for operation '-4611686018427387904 - 4611686018427387905'"
54 1.1 jruoho test_expr '-4611686018427387904 \* 1' '-4611686018427387904'
55 1.1 jruoho test_expr '-4611686018427387904 \* -1' '4611686018427387904'
56 1.1 jruoho test_expr '-4611686018427387904 \* 2' '-9223372036854775808'
57 1.1 jruoho test_expr '-4611686018427387904 \* 3' \
58 1.1 jruoho "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * 3'"
59 1.1 jruoho test_expr '-4611686018427387904 \* -2' \
60 1.1 jruoho "expr: integer overflow or underflow occurred for operation '-4611686018427387904 * -2'"
61 1.1 jruoho test_expr '4611686018427387904 \* 1' '4611686018427387904'
62 1.1 jruoho test_expr '4611686018427387904 \* 2' \
63 1.1 jruoho "expr: integer overflow or underflow occurred for operation '4611686018427387904 * 2'"
64 1.1 jruoho test_expr '4611686018427387904 \* 3' \
65 1.1 jruoho "expr: integer overflow or underflow occurred for operation '4611686018427387904 * 3'"
66 1.1 jruoho }
67 1.1 jruoho
68 1.1 jruoho atf_test_case gtkmm
69 1.1 jruoho gtkmm_head() {
70 1.1 jruoho atf_set "descr" "Test from gtk-- configure that cause problems on old expr"
71 1.1 jruoho }
72 1.1 jruoho gtkmm_body() {
73 1.1 jruoho test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 5' '1'
74 1.1 jruoho test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 6' '0'
75 1.1 jruoho test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 3 \& 5 \>= 5' '0'
76 1.1 jruoho test_expr '3 \> 3 \| 3 = 3 \& 4 \> 4 \| 3 = 2 \& 4 = 4 \& 5 \>= 5' '0'
77 1.1 jruoho test_expr '3 \> 2 \| 3 = 3 \& 4 \> 4 \| 3 = 3 \& 4 = 4 \& 5 \>= 6' '1'
78 1.1 jruoho test_expr '3 \> 3 \| 3 = 3 \& 4 \> 3 \| 3 = 3 \& 4 = 4 \& 5 \>= 5' '1'
79 1.1 jruoho }
80 1.1 jruoho
81 1.1 jruoho atf_test_case colon_vs_math
82 1.1 jruoho colon_vs_math_head() {
83 1.1 jruoho atf_set "descr" "Basic precendence test with the : operator vs. math"
84 1.1 jruoho }
85 1.1 jruoho colon_vs_math_body() {
86 1.1 jruoho test_expr '2 : 4 / 2' '0'
87 1.1 jruoho test_expr '4 : 4 % 3' '1'
88 1.1 jruoho }
89 1.1 jruoho
90 1.1 jruoho atf_test_case arithmetic_ops
91 1.1 jruoho arithmetic_ops_head() {
92 1.1 jruoho atf_set "descr" "Dangling arithemtic operator"
93 1.1 jruoho }
94 1.1 jruoho arithmetic_ops_body() {
95 1.1 jruoho test_expr '.java_wrapper : /' '0'
96 1.1 jruoho test_expr '4 : \*' '0'
97 1.1 jruoho test_expr '4 : +' '0'
98 1.1 jruoho test_expr '4 : -' '0'
99 1.1 jruoho test_expr '4 : /' '0'
100 1.1 jruoho test_expr '4 : %' '0'
101 1.1 jruoho }
102 1.1 jruoho
103 1.1 jruoho atf_test_case basic_math
104 1.1 jruoho basic_math_head() {
105 1.1 jruoho atf_set "descr" "Basic math test"
106 1.1 jruoho }
107 1.1 jruoho basic_math_body() {
108 1.1 jruoho test_expr '2 + 4 \* 5' '22'
109 1.1 jruoho }
110 1.1 jruoho
111 1.1 jruoho atf_test_case basic_functional
112 1.1 jruoho basic_functional_head() {
113 1.1 jruoho atf_set "descr" "Basic functional tests"
114 1.1 jruoho }
115 1.1 jruoho basic_functional_body() {
116 1.1 jruoho test_expr '2' '2'
117 1.1 jruoho test_expr '-4' '-4'
118 1.1 jruoho test_expr 'hello' 'hello'
119 1.1 jruoho }
120 1.1 jruoho
121 1.1 jruoho atf_test_case compare_ops_precedence
122 1.1 jruoho compare_ops_precedence_head() {
123 1.1 jruoho atf_set "descr" "Compare operator precendence test"
124 1.1 jruoho }
125 1.1 jruoho compare_ops_precedence_body() {
126 1.1 jruoho test_expr '2 \> 1 \* 17' '0'
127 1.1 jruoho }
128 1.1 jruoho
129 1.1 jruoho atf_test_case compare_ops
130 1.1 jruoho compare_ops_head() {
131 1.1 jruoho atf_set "descr" "Compare operator tests"
132 1.1 jruoho }
133 1.1 jruoho compare_ops_body() {
134 1.1 jruoho test_expr '2 \!= 5' '1'
135 1.1 jruoho test_expr '2 \!= 2' '0'
136 1.1 jruoho test_expr '2 \<= 3' '1'
137 1.1 jruoho test_expr '2 \<= 2' '1'
138 1.1 jruoho test_expr '2 \<= 1' '0'
139 1.1 jruoho test_expr '2 \< 3' '1'
140 1.1 jruoho test_expr '2 \< 2' '0'
141 1.1 jruoho test_expr '2 = 2' '1'
142 1.1 jruoho test_expr '2 = 4' '0'
143 1.1 jruoho test_expr '2 \>= 1' '1'
144 1.1 jruoho test_expr '2 \>= 2' '1'
145 1.1 jruoho test_expr '2 \>= 3' '0'
146 1.1 jruoho test_expr '2 \> 1' '1'
147 1.1 jruoho test_expr '2 \> 2' '0'
148 1.1 jruoho }
149 1.1 jruoho
150 1.1 jruoho atf_test_case multiply
151 1.1 jruoho multiply_head() {
152 1.2 jruoho atf_set "descr" "Test the multiply operator (PR bin/12838)"
153 1.1 jruoho }
154 1.1 jruoho multiply_body() {
155 1.1 jruoho test_expr '1 \* -1' '-1'
156 1.2 jruoho test_expr '2 \> 1 \* 17' '0'
157 1.1 jruoho }
158 1.1 jruoho
159 1.1 jruoho atf_test_case negative
160 1.1 jruoho negative_head() {
161 1.1 jruoho atf_set "descr" "Test the additive inverse"
162 1.1 jruoho }
163 1.1 jruoho negative_body() {
164 1.1 jruoho test_expr '-1 + 5' '4'
165 1.1 jruoho test_expr '- 1 + 5' 'expr: syntax error'
166 1.1 jruoho
167 1.1 jruoho test_expr '5 + -1' '4'
168 1.1 jruoho test_expr '5 + - 1' 'expr: syntax error'
169 1.1 jruoho
170 1.1 jruoho test_expr '1 - -5' '6'
171 1.1 jruoho }
172 1.1 jruoho
173 1.1 jruoho atf_test_case math_precedence
174 1.1 jruoho math_precedence_head() {
175 1.1 jruoho atf_set "descr" "More complex math test for precedence"
176 1.1 jruoho }
177 1.1 jruoho math_precedence_body() {
178 1.1 jruoho test_expr '-3 + -1 \* 4 + 3 / -6' '-7'
179 1.1 jruoho }
180 1.1 jruoho
181 1.1 jruoho atf_test_case precedence
182 1.1 jruoho precedence_head() {
183 1.1 jruoho atf_set "descr" "Test precedence"
184 1.1 jruoho }
185 1.1 jruoho precedence_body() {
186 1.1 jruoho # This is messy but the shell escapes cause that
187 1.1 jruoho test_expr 'X1/2/3 : X\\\(.\*[^/]\\\)//\*[^/][^/]\*/\*$ \| . : \\\(.\\\)' '1/2'
188 1.1 jruoho }
189 1.1 jruoho
190 1.1 jruoho atf_test_case regex
191 1.1 jruoho regex_head() {
192 1.1 jruoho atf_set "descr" "Test proper () returning \1 from a regex"
193 1.1 jruoho }
194 1.1 jruoho regex_body() {
195 1.1 jruoho # This is messy but the shell escapes cause that
196 1.1 jruoho test_expr '1/2 : .\*/\\\(.\*\\\)' '2'
197 1.1 jruoho }
198 1.1 jruoho
199 1.1 jruoho atf_init_test_cases()
200 1.1 jruoho {
201 1.1 jruoho atf_add_test_case overflow
202 1.1 jruoho atf_add_test_case gtkmm
203 1.1 jruoho atf_add_test_case colon_vs_math
204 1.1 jruoho atf_add_test_case arithmetic_ops
205 1.1 jruoho atf_add_test_case basic_math
206 1.1 jruoho atf_add_test_case basic_functional
207 1.1 jruoho atf_add_test_case compare_ops_precedence
208 1.1 jruoho atf_add_test_case compare_ops
209 1.1 jruoho atf_add_test_case multiply
210 1.1 jruoho atf_add_test_case negative
211 1.1 jruoho atf_add_test_case math_precedence
212 1.1 jruoho atf_add_test_case precedence
213 1.1 jruoho atf_add_test_case regex
214 1.1 jruoho }
215