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