Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/ksh93/tests/math.sh
1810 views
1
########################################################################
2
# #
3
# This software is part of the ast package #
4
# Copyright (c) 1982-2011 AT&T Intellectual Property #
5
# and is licensed under the #
6
# Eclipse Public License, Version 1.0 #
7
# by AT&T Intellectual Property #
8
# #
9
# A copy of the License is available at #
10
# http://www.eclipse.org/org/documents/epl-v10.html #
11
# (with md5 checksum b35adb5213ca9657e911e9befb180842) #
12
# #
13
# Information and Software Systems Research #
14
# AT&T Research #
15
# Florham Park NJ #
16
# #
17
# David Korn <[email protected]> #
18
# #
19
########################################################################
20
function err_exit
21
{
22
print -u2 -n "\t"
23
print -u2 -r ${Command}[$1]: "${@:2}"
24
(( Errors < 127 && Errors++ ))
25
}
26
alias err_exit='err_exit $LINENO'
27
28
set -o nounset
29
Command=${0##*/}
30
integer Errors=0
31
32
set -o nounset
33
34
typeset tmp
35
36
# create temporary test directory
37
tmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
38
trap "cd /; rm -rf $tmp" EXIT
39
cd $tmp || exit
40
41
function test_arithmetric_expression_accesss_array_element_through_nameref
42
{
43
compound out=( typeset stdout stderr ; integer res )
44
compound -r -a tests=(
45
(
46
cmd='@@TYPE@@ -a @@VAR@@ ; @@VAR@@[1]=90 ; function x { nameref nz=$1 ; print " $(( round(nz) ))==$(( round($nz) ))" ; } ; x @@VAR@@[1]' ; stdoutpattern=' 90==90'
47
)
48
(
49
cmd='@@TYPE@@ -a @@VAR@@=( [1]=90 ) ; function x { nameref nz=$1 ; print " $(( round(nz) ))==$(( round($nz) ))" ; } ; x @@VAR@@[1]' ; stdoutpattern=' 90==90'
50
)
51
(
52
cmd='@@TYPE@@ -a @@VAR@@ ; @@VAR@@[1][3]=90 ; function x { nameref nz=$1 ; print " $(( round(nz) ))==$(( round($nz) ))" ; } ; x @@VAR@@[1][3]' ; stdoutpattern=' 90==90'
53
)
54
(
55
cmd='@@TYPE@@ -a @@VAR@@=( [1][3]=90 ) ; function x { nameref nz=$1 ; print " $(( round(nz) ))==$(( round($nz) ))" ; } ; x @@VAR@@[1][3]' ; stdoutpattern=' 90==90'
56
)
57
(
58
cmd='@@TYPE@@ -a @@VAR@@ ; @@VAR@@[1][3][5]=90 ; function x { nameref nz=$1 ; print " $(( round(nz) ))==$(( round($nz) ))" ; } ; x @@VAR@@[1][3][5]' ; stdoutpattern=' 90==90'
59
)
60
(
61
cmd='@@TYPE@@ -a @@VAR@@=( [1][3][5]=90 ) ; function x { nameref nz=$1 ; print " $(( round(nz) ))==$(( round($nz) ))" ; } ; x @@VAR@@[1][3][5]' ; stdoutpattern=' 90==90'
62
)
63
(
64
cmd='@@TYPE@@ -a @@VAR@@ ; @@VAR@@[1][3][5]=90 ; function x { nameref nz=${1}[$2][$3][$4] ; print " $(( round(nz) ))==$(( round($nz) ))" ; } ; x @@VAR@@ 1 3 5' ; stdoutpattern=' 90==90'
65
)
66
(
67
cmd='@@TYPE@@ -A @@VAR@@ ; @@VAR@@[1]=90 ; function x { nameref nz=$1 ; print " $(( round(nz) ))==$(( round($nz) ))" ; } ; x @@VAR@@[1]' ; stdoutpattern=' 90==90'
68
)
69
(
70
cmd='@@TYPE@@ -A @@VAR@@=( [1]=90 ) ; function x { nameref nz=$1 ; print " $(( round(nz) ))==$(( round($nz) ))" ; } ; x @@VAR@@[1]' ; stdoutpattern=' 90==90'
71
)
72
)
73
74
typeset testname
75
integer i
76
typeset mode
77
typeset cmd
78
79
for (( i=0 ; i < ${#tests[@]} ; i++ )) ; do
80
# fixme: This list should include "typeset -lX" and "typeset -X" but ast-ksh.2010-03-09 fails like this:
81
# 'typeset -X -a z ; z[1][3]=90 ; function x { nameref nz=$1 ; print " $(( nz ))==$(( $nz ))" ; } ; x z[1][3]'
82
# + typeset -X -a z
83
# + z[1][3]=90
84
# + x 'z[1][3]'
85
# /home/test001/bin/ksh[1]: x: line 1: x1.68000000000000000000000000000000p: no parent
86
for ty in \
87
'typeset' \
88
'integer' \
89
'float' \
90
'typeset -i' \
91
'typeset -si' \
92
'typeset -li' \
93
'typeset -E' \
94
'typeset -F' \
95
'typeset -X' \
96
'typeset -lE' \
97
'typeset -lX' \
98
'typeset -lF' ; do
99
for mode in \
100
'plain' \
101
'in_compound' \
102
'in_indexed_compound_array' \
103
'in_2d_indexed_compound_array' \
104
'in_4d_indexed_compound_array' \
105
'in_associative_compound_array' \
106
'in_compound_nameref' \
107
'in_indexed_compound_array_nameref' \
108
'in_2d_indexed_compound_array_nameref' \
109
'in_4d_indexed_compound_array_nameref' \
110
'in_associative_compound_array_nameref' \
111
; do
112
nameref tst=tests[i]
113
114
cmd="${tst.cmd//@@TYPE@@/${ty}}"
115
116
case "${mode}" in
117
'plain')
118
cmd="${cmd//@@VAR@@/z}"
119
;;
120
121
'in_compound')
122
cmd="compound c ; ${cmd//@@VAR@@/c.z}"
123
;;
124
'in_indexed_compound_array')
125
cmd="compound -a c ; ${cmd//@@VAR@@/c[11].z}"
126
;;
127
'in_2d_indexed_compound_array')
128
cmd="compound -a c ; ${cmd//@@VAR@@/c[17][19].z}"
129
;;
130
'in_4d_indexed_compound_array')
131
cmd="compound -a c ; ${cmd//@@VAR@@/c[17][19][23][27].z}"
132
;;
133
'in_associative_compound_array')
134
cmd="compound -A c ; ${cmd//@@VAR@@/c[info].z}"
135
;;
136
137
'in_compound_nameref')
138
cmd="compound c ; nameref ncr=c.z ; ${cmd//@@VAR@@/ncr}"
139
;;
140
'in_indexed_compound_array_nameref')
141
cmd="compound -a c ; nameref ncr=c[11].z ; ${cmd//@@VAR@@/ncr}"
142
;;
143
'in_2d_indexed_compound_array_nameref')
144
cmd="compound -a c ; nameref ncr=c[17][19].z ; ${cmd//@@VAR@@/ncr}"
145
;;
146
'in_4d_indexed_compound_array_nameref')
147
cmd="compound -a c ; nameref ncr=c[17][19][23][27].z ; ${cmd//@@VAR@@/ncr}"
148
;;
149
'in_associative_compound_array_nameref')
150
cmd="compound -A c ; nameref ncr=c[info].z ; ${cmd//@@VAR@@/ncr}"
151
;;
152
*)
153
err_exit "Unexpected mode ${mode}"
154
;;
155
esac
156
157
testname="${0}/${cmd}"
158
#set -x
159
out.stderr="${ { out.stdout="${ ${SHELL} -o nounset -o errexit -c "${cmd}" ; (( out.res=$? )) ; }" ; } 2>&1 ; }"
160
#set +x
161
162
[[ "${out.stdout}" == ${tst.stdoutpattern} ]] || err_exit "${testname}: Expected stdout to match $(printf '%q\n' "${tst.stdoutpattern}"), got $(printf '%q\n' "${out.stdout}")"
163
[[ "${out.stderr}" == '' ]] || err_exit "${testname}: Expected empty stderr, got $(printf '%q\n' "${out.stderr}")"
164
(( out.res == 0 )) || err_exit "${testname}: Unexpected exit code ${out.res}"
165
done
166
done
167
done
168
169
return 0
170
}
171
172
function test_has_iszero
173
{
174
typeset str
175
integer i
176
177
typeset -r -a tests=(
178
'(( iszero(0) )) && print "OK"'
179
'(( iszero(0.) )) && print "OK"'
180
'(( iszero(-0) )) && print "OK"'
181
'(( iszero(-0.) )) && print "OK"'
182
'float n=0. ; (( iszero(n) )) && print "OK"'
183
'float n=+0. ; (( iszero(n) )) && print "OK"'
184
'float n=-0. ; (( iszero(n) )) && print "OK"'
185
'float n=1. ; (( iszero(n) )) || print "OK"'
186
'float n=1. ; (( iszero(n-1.) )) && print "OK"'
187
'float n=-1. ; (( iszero(n+1.) )) && print "OK"'
188
)
189
190
for (( i=0 ; i < ${#tests[@]} ; i++ )) ; do
191
str="$( ${SHELL} -o errexit -c "${tests[i]}" 2>&1 )" || err_exit "test $i: returned non-zero exit code $?"
192
[[ "${str}" == 'OK' ]] || err_exit "test $i: expected 'OK', got '${str}'"
193
done
194
195
return 0
196
}
197
198
# run tests
199
test_arithmetric_expression_accesss_array_element_through_nameref
200
test_has_iszero
201
202
exit $((Errors<125?Errors:125))
203
204