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