1 # $NetBSD: t_awk.sh,v 1.7 2020/06/26 07:50:11 jruoho Exp $ 2 # 3 # Copyright (c) 2012 The NetBSD Foundation, Inc. 4 # All rights reserved. 5 # 6 # This code is derived from software contributed to The NetBSD Foundation 7 # by Christos Zoulas 8 # 9 # Redistribution and use in source and binary forms, with or without 10 # modification, are permitted provided that the following conditions 11 # are met: 12 # 1. Redistributions of source code must retain the above copyright 13 # notice, this list of conditions and the following disclaimer. 14 # 2. Redistributions in binary form must reproduce the above copyright 15 # notice, this list of conditions and the following disclaimer in the 16 # documentation and/or other materials provided with the distribution. 17 # 18 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 # POSSIBILITY OF SUCH DAMAGE. 29 # 30 31 awk=awk 32 33 h_check() 34 { 35 local fname=d_$1 36 for sfx in in out awk; do 37 cp -r $(atf_get_srcdir)/$fname.$sfx . 38 done 39 shift 1 40 atf_check -o file:$fname.out -x "awk $@ -f $fname.awk < $fname.in" 41 } 42 43 atf_test_case big_regexp 44 45 big_regexp_head() { 46 atf_set "descr" "Checks matching long regular expressions (PR/33392)" 47 } 48 49 big_regexp_body() { 50 h_check big_regexp 51 } 52 53 atf_test_case end 54 55 end_head() { 56 atf_set "descr" "Checks that the last line of the input" \ 57 "is available under END pattern (PR/29659)" 58 } 59 60 end_body() { 61 h_check end1 62 h_check end2 63 } 64 65 atf_test_case string1 66 67 string1_head() { 68 atf_set "descr" "Checks escaping newlines in string literals" 69 } 70 71 string1_body() { 72 for sfx in out awk; do 73 cp -r $(atf_get_srcdir)/d_string1.$sfx . 74 done 75 atf_check -o file:d_string1.out awk -f d_string1.awk 76 } 77 78 atf_test_case multibyte 79 80 multibyte_head() { 81 atf_set "descr" "Checks multibyte charsets support" \ 82 "in tolower and toupper (PR/36394)" 83 } 84 85 multibyte_body() { 86 export LANG=en_US.UTF-8 87 88 h_check tolower 89 h_check toupper 90 } 91 92 atf_test_case period 93 94 period_head() { 95 atf_set "descr" "Checks that the period character is recognised" \ 96 "in awk program regardless of locale (PR bin/42320)" 97 } 98 99 period_body() { 100 export LANG=ru_RU.KOI8-R 101 102 h_check period -v x=0.5 103 } 104 105 atf_test_case assign_NF 106 107 assign_NF_head() { 108 atf_set "descr" 'Checks that assign to NF changes $0 and $n (PR/44063)' 109 } 110 111 assign_NF_body() { 112 h_check assign_NF 113 } 114 115 atf_test_case single_char_rs 116 117 single_char_rs_head() { 118 atf_set "descr" "Test awk(1) with single character RS" 119 } 120 121 single_char_rs_body() { 122 atf_check \ 123 -o "inline:1\n2\n\n3\n\n\n4\n\n" \ 124 -x "echo 1a2aa3aaa4 | $awk 1 RS=a" 125 } 126 127 atf_test_case two_char_rs 128 129 two_char_rs_head() { 130 atf_set "descr" "Test awk(1) with two characters RS" 131 } 132 133 two_char_rs_body() { 134 atf_check \ 135 -o "inline:1\n2\n3\n4\n\n" \ 136 -x "echo 1ab2ab3ab4 | $awk 1 RS=ab" 137 } 138 139 atf_test_case single_char_regex_group_rs 140 141 single_char_regex_group_rs_head() { 142 atf_set "descr" "Test awk(1) with single character regex group RS" 143 } 144 145 single_char_regex_group_rs_body() { 146 atf_check \ 147 -o "inline:1\n2\n\n3\n\n\n4\n\n" \ 148 -x "echo 1a2aa3aaa4 | $awk 1 RS='[a]'" 149 } 150 151 atf_test_case two_char_regex_group_rs 152 153 two_char_regex_group_rs_head() { 154 atf_set "descr" "Test awk(1) with two characters regex group RS" 155 } 156 157 two_char_regex_group_rs_body() { 158 atf_check \ 159 -o "inline:1\n2\n\n3\n\n\n4\n\n" \ 160 -x "echo 1a2ab3aba4 | $awk 1 RS='[ab]'" 161 } 162 163 atf_test_case single_char_regex_star_rs 164 165 single_char_regex_star_rs_head() { 166 atf_set "descr" "Test awk(1) with single character regex star RS" 167 } 168 169 single_char_regex_star_rs_body() { 170 atf_check \ 171 -o "inline:1\n2\n3\n4\n\n" \ 172 -x "echo 1a2aa3aaa4 | $awk 1 RS='a*'" 173 } 174 175 atf_test_case two_char_regex_star_rs 176 177 two_char_regex_star_rs_head() { 178 atf_set "descr" "Test awk(1) with two characters regex star RS" 179 } 180 181 two_char_regex_star_rs_body() { 182 atf_check \ 183 -o "inline:1\n2\n3\n4\n\n" \ 184 -x "echo 1a2aa3aaa4 | $awk 1 RS='aa*'" 185 } 186 187 atf_test_case regex_two_star_rs 188 189 regex_two_star_rs_head() { 190 atf_set "descr" "Test awk(1) with regex two star RS" 191 } 192 193 regex_two_star_rs_body() { 194 atf_check \ 195 -o "inline:1\n2\n3\n4\n\n" \ 196 -x "echo 1a2ab3aab4 | $awk 1 RS='aa*b*'" 197 } 198 199 atf_test_case regex_or_1_rs 200 201 regex_or_1_rs_head() { 202 atf_set "descr" "Test awk(1) with regex | case 1 RS" 203 } 204 205 regex_or_1_rs_body() { 206 atf_check \ 207 -o "inline:1a\nc\n\n" \ 208 -x "echo 1abc | $awk 1 RS='abcde|b'" 209 } 210 211 atf_test_case regex_or_2_rs 212 213 regex_or_2_rs_head() { 214 atf_set "descr" "Test awk(1) with regex | case 2 RS" 215 } 216 217 regex_or_2_rs_body() { 218 atf_check \ 219 -o "inline:1a\ncdf2\n\n" \ 220 -x "echo 1abcdf2 | $awk 1 RS='abcde|b'" 221 } 222 223 atf_test_case regex_or_3_rs 224 225 regex_or_3_rs_head() { 226 atf_set "descr" "Test awk(1) with regex | case 3 RS" 227 } 228 229 regex_or_3_rs_body() { 230 atf_check \ 231 -o "inline:1\n\nf2\n\n" \ 232 -x "echo 1abcdebf2 | $awk 1 RS='abcde|b'" 233 } 234 235 atf_test_case regex_or_4_rs 236 237 regex_or_4_rs_head() { 238 atf_set "descr" "Test awk(1) with regex | case 4 RS" 239 } 240 241 regex_or_4_rs_body() { 242 atf_check \ 243 -o "inline:1\nbcdf2\n\n" \ 244 -x "echo 1abcdf2 | $awk 1 RS='abcde|a'" 245 246 } 247 248 atf_test_case regex_caret_1_rs 249 250 regex_caret_1_rs_head() { 251 atf_set "descr" "Test awk(1) with regex ^ case 1 RS" 252 } 253 254 regex_caret_1_rs_body() { 255 atf_check \ 256 -o "inline:\n1a2a3a\n\n" \ 257 -x "echo a1a2a3a | $awk 1 RS='^a'" 258 259 } 260 261 atf_test_case regex_caret_2_rs 262 263 regex_caret_2_rs_head() { 264 atf_set "descr" "Test awk(1) with regex ^ case 2 RS" 265 } 266 267 regex_caret_2_rs_body() { 268 atf_check \ 269 -o "inline:\naa1a2a\n\n" \ 270 -x "echo aaa1a2a | $awk 1 RS='^a'" 271 272 } 273 274 atf_test_case regex_dollar_1_rs 275 276 regex_dollar_1_rs_head() { 277 atf_set "descr" "Test awk(1) with regex $ case 1 RS" 278 } 279 280 regex_dollar_1_rs_body() { 281 atf_check \ 282 -o "inline:a1a2a3a\n\n" \ 283 -x "echo a1a2a3a | $awk 1 RS='a$'" 284 285 } 286 287 atf_test_case regex_dollar_2_rs 288 289 regex_dollar_2_rs_head() { 290 atf_set "descr" "Test awk(1) with regex $ case 2 RS" 291 } 292 293 regex_dollar_2_rs_body() { 294 atf_check \ 295 -o "inline:a1a2aaa\n\n" \ 296 -x "echo a1a2aaa | $awk 1 RS='a$'" 297 298 } 299 300 atf_test_case regex_reallocation_rs 301 302 regex_reallocation_rs_head() { 303 atf_set "descr" "Test awk(1) with regex reallocation RS" 304 } 305 306 regex_reallocation_rs_body() { 307 atf_check \ 308 -o "inline:a\na\na\na\na\na\na\na\na\na10000\n\n" \ 309 -x "jot -s a 10000 | $awk 'NR>1' RS='999[0-9]'" 310 311 } 312 313 atf_test_case empty_rs 314 315 empty_rs_head() { 316 atf_set "descr" "Test awk(1) with empty RS" 317 } 318 319 empty_rs_body() { 320 atf_check \ 321 -o "inline:foo\n" \ 322 -x "echo foo | $awk 1 RS=''" 323 324 } 325 326 atf_test_case newline_rs 327 328 newline_rs_head() { 329 atf_set "descr" "Test awk(1) with newline RS" 330 } 331 332 newline_rs_body() { 333 atf_check \ 334 -o "inline:r1f1:r1f2\nr2f1:r2f2\n" \ 335 -x "printf '\n\n\nr1f1\nr1f2\n\nr2f1\nr2f2\n\n\n' | $awk '{\$1=\$1}1' RS= OFS=:" 336 } 337 338 atf_test_case regex_range 339 340 regex_range_head() { 341 atf_set "descr" "Test awk(1) with regex range" 342 } 343 344 regex_range_body() { 345 atf_check \ 346 -o "inline:matched\n" \ 347 -x "echo '1 a' | $awk '/[[:digit:]][[:space:]][[:alpha:]]/ { print \"matched\"; }'" 348 } 349 350 atf_test_case regex_repeat 351 352 regex_repeat_head() { 353 atf_set "descr" "Test awk(1) with regex repeat" 354 } 355 356 regex_repeat_body() { 357 atf_check \ 358 -o "inline:matched\n" \ 359 -x "echo 'aaabbbbcc' | $awk '/a{3}b{4}c{2}/ { print \"matched\"; }'" 360 } 361 362 atf_test_case modify_subsep 363 364 modify_subsep_head() { 365 atf_set "descr" "Test awk(1) SUPSEP modification (PR/47306)" 366 } 367 368 modify_subsep_body() { 369 atf_check \ 370 -o "inline:1\n1\n1\n" \ 371 -x "printf '1\n1 2\n' | \ 372 $awk '1{ arr[\$1 SUBSEP \$2 SUBSEP ++cnt[\$1]]=1} {for (f in arr) print arr[f];}'" 373 } 374 375 atf_init_test_cases() { 376 377 atf_add_test_case big_regexp 378 atf_add_test_case end 379 atf_add_test_case string1 380 atf_add_test_case multibyte 381 atf_add_test_case period 382 atf_add_test_case assign_NF 383 384 atf_add_test_case single_char_rs 385 atf_add_test_case two_char_rs 386 atf_add_test_case single_char_regex_group_rs 387 atf_add_test_case two_char_regex_group_rs 388 atf_add_test_case two_char_regex_star_rs 389 atf_add_test_case single_char_regex_star_rs 390 atf_add_test_case regex_two_star_rs 391 atf_add_test_case regex_or_1_rs 392 atf_add_test_case regex_or_2_rs 393 atf_add_test_case regex_or_3_rs 394 atf_add_test_case regex_caret_1_rs 395 atf_add_test_case regex_caret_2_rs 396 atf_add_test_case regex_dollar_1_rs 397 atf_add_test_case regex_dollar_2_rs 398 atf_add_test_case regex_reallocation_rs 399 atf_add_test_case empty_rs 400 atf_add_test_case newline_rs 401 atf_add_test_case regex_range 402 atf_add_test_case regex_repeat 403 atf_add_test_case modify_subsep 404 } 405