]> git.ipfire.org Git - thirdparty/bash.git/blob - tests/posixpat.tests
bash-5.1 distribution sources and documentation
[thirdparty/bash.git] / tests / posixpat.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 # A test suite for the POSIX.2 (BRE) pattern matching code
15 LC_ALL=C
16 LANG=C
17
18 # First, test POSIX.2 character classes
19
20 case e in
21 [[:xdigit:]]) echo ok 1;;
22 esac
23
24 case a in
25 [[:alpha:]123]) echo ok 2;;
26 esac
27
28 case 1 in
29 [[:alpha:]123]) echo ok 3;;
30 esac
31
32 case 9 in
33 [![:alpha:]]) echo ok 4;;
34 esac
35
36 case a in
37 [:al:]) echo ok 5;;
38 esac
39
40 # invalid character class expressions are no longer just characters to be
41 # matched
42 case a in
43 [[:al:]) echo bad 6;;
44 *) echo ok 6;;
45 esac
46
47 case '!' in
48 [abc[:punct:][0-9]) echo ok 7;;
49 esac
50
51 # let's try to match the start of a valid sh identifier
52 case 'PATH' in
53 [_[:alpha:]]*) echo ok 8;;
54 esac
55
56 # let's try to match the first two characters of a valid sh identifier
57 case PATH in
58 [_[:alpha:]][_[:alnum:]]*) echo ok 9;;
59 esac
60
61 # is ^C a cntrl character?
62 case $'\003' in
63 [[:cntrl:]]) echo ok 10;;
64 esac
65
66 # how about A?
67 case A in
68 [[:cntrl:]]) echo oops -- cntrl ;;
69 *) echo ok 11;;
70 esac
71
72 case 9 in
73 [[:digit:]]) echo ok 12;;
74 esac
75
76 case X in
77 [[:digit:]]) echo oops -- digit;;
78 *) echo ok 13;;
79 esac
80
81 case $'\033' in
82 [[:graph:]]) echo oops -- graph;;
83 *) echo ok 14;;
84 esac
85
86 case $'\040' in
87 [[:graph:]]) echo oops -- graph 2;;
88 *) echo ok 15;;
89 esac
90
91 case ' ' in
92 [[:graph:]]) echo oops -- graph 3;;
93 *) echo ok 16;;
94 esac
95
96 case 'aB' in
97 [[:lower:]][[:upper:]]) echo ok 17;;
98 esac
99
100 case $'\040' in
101 [[:print:]]) echo ok 18;;
102 *) echo oops -- print;;
103 esac
104
105 case PS3 in
106 [_[:alpha:]][_[:alnum:]][_[:alnum:]]*) echo ok 19;;
107 esac
108
109 case a in
110 [[:alpha:][:digit:]]) echo ok 20;;
111 *) echo oops - skip brackpat ;;
112 esac
113
114 case a in
115 [[:alpha:]\]) echo oops -- dangling backslash in brackpat ;;
116 *) echo ok 21 ;;
117 esac
118
119 # what's a newline? is it a blank? a space?
120 case $'\n' in
121 [[:blank:]]) echo ok -- blank ;;
122 [[:space:]]) echo ok -- space ;;
123 *) echo oops newline ;;
124 esac
125
126 # OK, what's a tab? is it a blank? a space?
127 case $'\t' in
128 [[:blank:]]) echo ok -- blank ;;
129 [[:space:]]) echo ok -- space ;;
130 *) echo oops newline ;;
131 esac
132
133 # let's check out characters in the ASCII range
134 case $'\377' in
135 [[:ascii:]]) echo oops -- ascii\?;;
136 esac
137
138 case 9 in
139 [1[:alpha:]123]) echo oops 1;;
140 esac
141
142 # however, an unterminated brace expression containing a valid char class
143 # that matches had better fail
144 case a in
145 [[:alpha:]) echo oops 2;;
146 esac
147
148 case $'\b' in
149 [[:graph:]]) echo oops 3;;
150 esac
151
152 case $'\b' in
153 [[:print:]]) echo oops 4;;
154 esac
155
156 case $' ' in
157 [[:punct:]]) echo oops 5;;
158 esac
159
160 # Next, test POSIX.2 collating symbols
161
162 case 'a' in
163 [[.a.]]) echo ok 1;;
164 esac
165
166 case '-' in
167 [[.hyphen.]-9]) echo ok 2;;
168 esac
169
170 case 'p' in
171 [[.a.]-[.z.]]) echo ok 3;;
172 esac
173
174 case '-' in
175 [[.-.]]) echo ok 4;;
176 esac
177
178 case ' ' in
179 [[.space.]]) echo ok 5;;
180 esac
181
182 case ' ' in
183 [[.grave-accent.]]) echo oops - grave;;
184 *) echo ok 6;;
185 esac
186
187 case '4' in
188 [[.-.]-9]) echo ok 7;;
189 esac
190
191 # an invalid collating symbol cannot be the first part of a range
192 case 'c' in
193 [[.yyz.]-[.z.]]) echo oops - yyz;;
194 *) echo ok 8;;
195 esac
196
197 case 'c' in
198 [[.yyz.][.a.]-z]) echo ok 9;;
199 esac
200
201 # but when not part of a range is not an error
202 case 'c' in
203 [[.yyz.][.a.]-[.z.]]) echo ok 10 ;;
204 esac
205
206 case 'p' in
207 [[.a.]-[.Z.]]) echo oops -- bad range ;;
208 *) echo ok 11;;
209 esac
210
211 case p in
212 [[.a.]-[.zz.]p]) echo ok 12;;
213 *) echo oops -- bad range 2;;
214 esac
215
216 case p in
217 [[.aa.]-[.z.]p]) echo ok 13;;
218 *) echo oops -- bad range 3;;
219 esac
220
221 case c in
222 [[.yyz.]cde]) echo ok 14;;
223 esac
224
225 case abc in
226 [[.cb.]a-Za]*) echo ok 15;;
227 esac
228
229 case $'\t' in
230 [[.space.][.tab.][.newline.]]) echo ok 16;;
231 esac
232
233 # and finally, test POSIX.2 equivalence classes
234
235 case "abc" in
236 [[:alpha:]][[=b=]][[:ascii:]]) echo ok 1;;
237 esac
238
239 case "abc" in
240 [[:alpha:]][[=B=]][[:ascii:]]) echo oops -- =B=;;
241 *) echo ok 2 ;;
242 esac
243
244 case a in
245 [[=b=]) echo oops;; # an incomplete equiv class is just a string
246 *) echo ok 3;;
247 esac