]> git.ipfire.org Git - thirdparty/bash.git/blame - tests/posix2.tests
bash-5.1 distribution sources and documentation
[thirdparty/bash.git] / tests / posix2.tests
CommitLineData
8868edaf
CR
1# This program is free software: you can redistribute it and/or modify
2# it under the terms of the GNU General Public License as published by
3# the Free Software Foundation, either version 3 of the License, or
4# (at your option) any later version.
5#
6# This program is distributed in the hope that it will be useful,
7# but WITHOUT ANY WARRANTY; without even the implied warranty of
8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9# GNU General Public License for more details.
10#
11# You should have received a copy of the GNU General Public License
12# along with this program. If not, see <http://www.gnu.org/licenses/>.
13#
14
ccc6cda3
JA
15# posix-2.sh - Simple identification tests for POSIX.2 features
16# commonly missing or incorrectly implemented.
17# Time-stamp: <96/04/10 16:43:48 gildea>
18# By Stephen Gildea <gildea@x.org> March 1995
19#
20# Copyright (c) 1995 Stephen Gildea
21# Permission is hereby granted to deal in this Software without restriction.
22# THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
23#
24# MODIFIED BY chet@po.cwru.edu to make part of the bash test suite.
25# last change: Wed Jun 19 12:24:24 EDT 1996
26#
27# some of the tests:
28#
29# shell functions (do we care?)
30# var=${var:-val}
31# unset
32# set --
33# IFS parsing
34## not exiting with -e and failed "if", the way Ultrix does (Ultrix 4.2?)
35# "$@" expands to zero arguments if passed zero arguments
36# $SHELL -c 'echo $1' bad good
37# test -x
38# positional parameters greater than 9
39# arithmetic expansion $(( ... ))
40# getopts
41
42# For some tests we must run a sub-shell; $TESTSHELL says what to use.
43# If set, TESTSHELL must be an absolute pathname.
44# For example, on HP-UX 9, /bin/posix/sh is the supposedly-compliant shell.
45TESTSHELL=${THIS_SH:-$PWD/../bash}
46
47# these tests create temp files with names $TMPDIR/conf*
48: ${TMPDIR:=/tmp}
49
50exitval=0
51numtests=0
52
53echo "Testing for POSIX.2 conformance"
54
55newtest()
56{
57 numtests=$(($numtests + 1))
58}
59
60testfail()
61{
62 echo "$1 test failed"
63 exitval=$(($exitval + 1))
64}
65
66newtest
67empty=""
68test "${empty:-ok}" = ok || testfail "empty var colon"
69newtest
70test "${empty-bad}" = "" || testfail "got \"${empty-bad}\": empty var nocolon"
71newtest
72test "${unsetvar-ok}" = ok || testfail "unset var"
73newtest
74unset empty
75test "${empty-ok}" = ok || testfail "unset"
76
77newtest
78set -- -Z
79test "x$1" = x-Z || testfail '\"set -- arg\"'
80# this should empty the argument list
81newtest
82set --
83test $# = 0 || testfail "still $# args: \"set --\""
84
85# IFS parsing:
86newtest
87names=one/good/three
88saved_ifs="$IFS"
89IFS=/
90set $names lose
91test "$2" = good || testfail "got \"$2\": IFS parsing"
92IFS="$saved_ifs"
93
94# "$@" with 0 arguments should expand to 0 arguments
95newtest
96cat > $TMPDIR/conftest1 << EOF
97$TMPDIR/conftest2 "\$@"
98EOF
99cat > $TMPDIR/conftest2 << "EOF"
100#! /bin/sh
101echo $#
102EOF
103chmod +x $TMPDIR/conftest1 $TMPDIR/conftest2
104numargs=$($TESTSHELL $TMPDIR/conftest1)
105if [ "$?" != 0 ]; then
106 testfail 'running $@'
107else
108 test "$numargs" = 0 || testfail '"$@" got '"$numargs args: expansion w 0 args"
109fi
110rm -f $TMPDIR/conftest1 $TMPDIR/conftest2
111
112newtest
113val=$("$TESTSHELL" -c 'echo $1' csh good)
114test "$val" = good || testfail "got \"$val\": sh -c"
115
116newtest
117# do these tests in a sub-shell because failure will exit
118val=$("$TESTSHELL" -c 'echo ${10}' 0 1 2 3 4 5 6 7 8 9 ten 11 2> /dev/null)
119test "$val" = ten || testfail "accessing more than 9 positional params"
120
121a=abc_def_ghi
122export a
123newtest; val=`"$TESTSHELL" -c 'echo "${a%_*}"' 2> /dev/null`
124test "$val" = abc_def || testfail "parameter % op"
125newtest; val=`"$TESTSHELL" -c 'echo "${a%%_*}"' 2> /dev/null`
126test "$val" = abc || testfail "parameter %% op"
127newtest; val=`"$TESTSHELL" -c 'echo "${a#*_}"' 2> /dev/null`
128test "$val" = def_ghi || testfail "parameter # op"
129newtest; val=`"$TESTSHELL" -c 'echo "${a##*_}"' 2> /dev/null`
130test "$val" = ghi || testfail "parameter ## op"
131
132newtest
133"$TESTSHELL" -c 'export a=value' 2> /dev/null || testfail "export with value"
134
135newtest
136a=5; test "$(( ($a+1)/2 ))" = 3 || testfail "arithmetic expansion"
137
138# does "test" support the -x switch?
139newtest
140touch $TMPDIR/conftest
141chmod -x $TMPDIR/conftest
142test -x $TMPDIR/conftest && testfail "negative test -x"
143chmod +x $TMPDIR/conftest
144test -x $TMPDIR/conftest || testfail "positive test -x"
145rm -f $TMPDIR/conftest
146
147newtest
148test "$OPTIND" = 1 || testfail "OPTIND initial value"
149
150newtest
151getopts a: store -a aoptval
152if [ "$OPTIND" != 3 ] || [ "$store" != a ] || [ "$OPTARG" != aoptval ]; then
153 testfail "getopts"
154fi
155
28ef6c31
JA
156# if I change the default quoting style for variable values, these
157# next four must change
158
cce855bc
JA
159newtest
160SQUOTE="'"
161val1=$(set | sed -n 's:^SQUOTE=::p')
ac50fbac 162if [ "$val1" != "\'" ]; then
cce855bc
JA
163 testfail "variable quoting 1"
164fi
165
166newtest
167VTILDE='~'
168val1=$(set | sed -n 's:^VTILDE=::p')
f73dda09 169if [ "$val1" != "'~'" ]; then
cce855bc
JA
170 testfail "variable quoting 2"
171fi
172
173newtest
174VHASH=ab#cd
175val1=$(set | sed -n 's:^VHASH=::p')
176if [ "$val1" != "ab#cd" ]; then
177 testfail "variable quoting 3"
178fi
179
180newtest
181VHASH2=#abcd
182val1=$(set | sed -n 's:^VHASH2=::p')
f73dda09 183if [ "$val1" != "'#abcd'" ]; then
cce855bc
JA
184 testfail "variable quoting 4"
185fi
186
a0c0a00f
CR
187# these are Posix.2 shell grammar rule 4, problems through bash-4.3
188newtest
189case esac in (foo|esac) ;; *) testfail "case esac test 1" ;; esac
190newtest
191case esac in foo|esac) ;; *) testfail "case esac test 2" ;; esac
192
193# these are supposed to be syntax errors
194newtest
195eval 'case esac in (esac) ;; *) echo "case esac test 3" ;; esac'
196newtest
197eval 'case esac in esac) ;; *) echo "case esac test 4";; esac'
198
ccc6cda3
JA
199if [ $exitval = 0 ]; then
200 echo "All tests passed"
201else
202 echo "$exitval of $numtests tests failed"
203fi
204exit $exitval