]> git.ipfire.org Git - thirdparty/bash.git/blob - tests/posix2.tests
bash-5.1 distribution sources and documentation
[thirdparty/bash.git] / tests / posix2.tests
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
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.
45 TESTSHELL=${THIS_SH:-$PWD/../bash}
46
47 # these tests create temp files with names $TMPDIR/conf*
48 : ${TMPDIR:=/tmp}
49
50 exitval=0
51 numtests=0
52
53 echo "Testing for POSIX.2 conformance"
54
55 newtest()
56 {
57 numtests=$(($numtests + 1))
58 }
59
60 testfail()
61 {
62 echo "$1 test failed"
63 exitval=$(($exitval + 1))
64 }
65
66 newtest
67 empty=""
68 test "${empty:-ok}" = ok || testfail "empty var colon"
69 newtest
70 test "${empty-bad}" = "" || testfail "got \"${empty-bad}\": empty var nocolon"
71 newtest
72 test "${unsetvar-ok}" = ok || testfail "unset var"
73 newtest
74 unset empty
75 test "${empty-ok}" = ok || testfail "unset"
76
77 newtest
78 set -- -Z
79 test "x$1" = x-Z || testfail '\"set -- arg\"'
80 # this should empty the argument list
81 newtest
82 set --
83 test $# = 0 || testfail "still $# args: \"set --\""
84
85 # IFS parsing:
86 newtest
87 names=one/good/three
88 saved_ifs="$IFS"
89 IFS=/
90 set $names lose
91 test "$2" = good || testfail "got \"$2\": IFS parsing"
92 IFS="$saved_ifs"
93
94 # "$@" with 0 arguments should expand to 0 arguments
95 newtest
96 cat > $TMPDIR/conftest1 << EOF
97 $TMPDIR/conftest2 "\$@"
98 EOF
99 cat > $TMPDIR/conftest2 << "EOF"
100 #! /bin/sh
101 echo $#
102 EOF
103 chmod +x $TMPDIR/conftest1 $TMPDIR/conftest2
104 numargs=$($TESTSHELL $TMPDIR/conftest1)
105 if [ "$?" != 0 ]; then
106 testfail 'running $@'
107 else
108 test "$numargs" = 0 || testfail '"$@" got '"$numargs args: expansion w 0 args"
109 fi
110 rm -f $TMPDIR/conftest1 $TMPDIR/conftest2
111
112 newtest
113 val=$("$TESTSHELL" -c 'echo $1' csh good)
114 test "$val" = good || testfail "got \"$val\": sh -c"
115
116 newtest
117 # do these tests in a sub-shell because failure will exit
118 val=$("$TESTSHELL" -c 'echo ${10}' 0 1 2 3 4 5 6 7 8 9 ten 11 2> /dev/null)
119 test "$val" = ten || testfail "accessing more than 9 positional params"
120
121 a=abc_def_ghi
122 export a
123 newtest; val=`"$TESTSHELL" -c 'echo "${a%_*}"' 2> /dev/null`
124 test "$val" = abc_def || testfail "parameter % op"
125 newtest; val=`"$TESTSHELL" -c 'echo "${a%%_*}"' 2> /dev/null`
126 test "$val" = abc || testfail "parameter %% op"
127 newtest; val=`"$TESTSHELL" -c 'echo "${a#*_}"' 2> /dev/null`
128 test "$val" = def_ghi || testfail "parameter # op"
129 newtest; val=`"$TESTSHELL" -c 'echo "${a##*_}"' 2> /dev/null`
130 test "$val" = ghi || testfail "parameter ## op"
131
132 newtest
133 "$TESTSHELL" -c 'export a=value' 2> /dev/null || testfail "export with value"
134
135 newtest
136 a=5; test "$(( ($a+1)/2 ))" = 3 || testfail "arithmetic expansion"
137
138 # does "test" support the -x switch?
139 newtest
140 touch $TMPDIR/conftest
141 chmod -x $TMPDIR/conftest
142 test -x $TMPDIR/conftest && testfail "negative test -x"
143 chmod +x $TMPDIR/conftest
144 test -x $TMPDIR/conftest || testfail "positive test -x"
145 rm -f $TMPDIR/conftest
146
147 newtest
148 test "$OPTIND" = 1 || testfail "OPTIND initial value"
149
150 newtest
151 getopts a: store -a aoptval
152 if [ "$OPTIND" != 3 ] || [ "$store" != a ] || [ "$OPTARG" != aoptval ]; then
153 testfail "getopts"
154 fi
155
156 # if I change the default quoting style for variable values, these
157 # next four must change
158
159 newtest
160 SQUOTE="'"
161 val1=$(set | sed -n 's:^SQUOTE=::p')
162 if [ "$val1" != "\'" ]; then
163 testfail "variable quoting 1"
164 fi
165
166 newtest
167 VTILDE='~'
168 val1=$(set | sed -n 's:^VTILDE=::p')
169 if [ "$val1" != "'~'" ]; then
170 testfail "variable quoting 2"
171 fi
172
173 newtest
174 VHASH=ab#cd
175 val1=$(set | sed -n 's:^VHASH=::p')
176 if [ "$val1" != "ab#cd" ]; then
177 testfail "variable quoting 3"
178 fi
179
180 newtest
181 VHASH2=#abcd
182 val1=$(set | sed -n 's:^VHASH2=::p')
183 if [ "$val1" != "'#abcd'" ]; then
184 testfail "variable quoting 4"
185 fi
186
187 # these are Posix.2 shell grammar rule 4, problems through bash-4.3
188 newtest
189 case esac in (foo|esac) ;; *) testfail "case esac test 1" ;; esac
190 newtest
191 case esac in foo|esac) ;; *) testfail "case esac test 2" ;; esac
192
193 # these are supposed to be syntax errors
194 newtest
195 eval 'case esac in (esac) ;; *) echo "case esac test 3" ;; esac'
196 newtest
197 eval 'case esac in esac) ;; *) echo "case esac test 4";; esac'
198
199 if [ $exitval = 0 ]; then
200 echo "All tests passed"
201 else
202 echo "$exitval of $numtests tests failed"
203 fi
204 exit $exitval