1272343Sngie# $NetBSD: t_awk.sh,v 1.5 2012/12/10 20:30:06 christos Exp $ 2272343Sngie# 3272343Sngie# Copyright (c) 2012 The NetBSD Foundation, Inc. 4272343Sngie# All rights reserved. 5272343Sngie# 6272343Sngie# This code is derived from software contributed to The NetBSD Foundation 7272343Sngie# by Christos Zoulas 8272343Sngie# 9272343Sngie# Redistribution and use in source and binary forms, with or without 10272343Sngie# modification, are permitted provided that the following conditions 11272343Sngie# are met: 12272343Sngie# 1. Redistributions of source code must retain the above copyright 13272343Sngie# notice, this list of conditions and the following disclaimer. 14272343Sngie# 2. Redistributions in binary form must reproduce the above copyright 15272343Sngie# notice, this list of conditions and the following disclaimer in the 16272343Sngie# documentation and/or other materials provided with the distribution. 17272343Sngie# 18272343Sngie# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19272343Sngie# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20272343Sngie# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21272343Sngie# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22272343Sngie# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23272343Sngie# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24272343Sngie# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25272343Sngie# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26272343Sngie# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27272343Sngie# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28272343Sngie# POSSIBILITY OF SUCH DAMAGE. 29272343Sngie# 30272343Sngie 31272343Sngieawk=awk 32272343Sngie 33272343Sngieh_check() 34272343Sngie{ 35272343Sngie local fname=d_$1 36272343Sngie for sfx in in out awk; do 37272343Sngie cp -r $(atf_get_srcdir)/$fname.$sfx . 38272343Sngie done 39272343Sngie shift 1 40272343Sngie atf_check -o file:$fname.out -x "awk $@ -f $fname.awk < $fname.in" 41272343Sngie} 42272343Sngie 43272343Sngieatf_test_case big_regexp 44272343Sngie 45272343Sngiebig_regexp_head() { 46272343Sngie atf_set "descr" "Checks matching long regular expressions (PR/33392)" 47272343Sngie} 48272343Sngie 49272343Sngiebig_regexp_body() { 50272343Sngie h_check big_regexp 51272343Sngie} 52272343Sngie 53272343Sngieatf_test_case end 54272343Sngie 55272343Sngieend_head() { 56272343Sngie atf_set "descr" "Checks that the last line of the input" \ 57272343Sngie "is available under END pattern (PR/29659)" 58272343Sngie} 59272343Sngie 60272343Sngieend_body() { 61272343Sngie h_check end1 62272343Sngie h_check end2 63272343Sngie} 64272343Sngie 65272343Sngieatf_test_case string1 66272343Sngie 67272343Sngiestring1_head() { 68272343Sngie atf_set "descr" "Checks escaping newlines in string literals" 69272343Sngie} 70272343Sngie 71272343Sngiestring1_body() { 72272343Sngie for sfx in out awk; do 73272343Sngie cp -r $(atf_get_srcdir)/d_string1.$sfx . 74272343Sngie done 75272343Sngie atf_check -o file:d_string1.out awk -f d_string1.awk 76272343Sngie} 77272343Sngie 78272343Sngieatf_test_case multibyte 79272343Sngie 80272343Sngiemultibyte_head() { 81272343Sngie atf_set "descr" "Checks multibyte charsets support" \ 82272343Sngie "in tolower and toupper (PR/36394)" 83272343Sngie} 84272343Sngie 85272343Sngiemultibyte_body() { 86272343Sngie export LANG=en_US.UTF-8 87272343Sngie 88272343Sngie h_check tolower 89272343Sngie h_check toupper 90272343Sngie} 91272343Sngie 92272343Sngieatf_test_case period 93272343Sngie 94272343Sngieperiod_head() { 95272343Sngie atf_set "descr" "Checks that the period character is recognised" \ 96272343Sngie "in awk program regardless of locale (bin/42320)" 97272343Sngie} 98272343Sngie 99272343Sngieperiod_body() { 100272343Sngie export LANG=ru_RU.KOI8-R 101272343Sngie 102272343Sngie h_check period -v x=0.5 103272343Sngie} 104272343Sngie 105272343Sngieatf_test_case assign_NF 106272343Sngie 107272343Sngieassign_NF_head() { 108272343Sngie atf_set "descr" 'Checks that assign to NF changes $0 and $n (PR/44063)' 109272343Sngie} 110272343Sngie 111272343Sngieassign_NF_body() { 112272343Sngie h_check assign_NF 113272343Sngie} 114272343Sngie 115272343Sngieatf_test_case single_char_rs 116272343Sngie 117272343Sngiesingle_char_rs_head() { 118272343Sngie atf_set "descr" "Test awk(1) with single character RS" 119272343Sngie} 120272343Sngie 121272343Sngiesingle_char_rs_body() { 122272343Sngie atf_check \ 123272343Sngie -o "inline:1\n2\n\n3\n\n\n4\n\n" \ 124272343Sngie -x "echo 1a2aa3aaa4 | $awk 1 RS=a" 125272343Sngie} 126272343Sngie 127272343Sngieatf_test_case two_char_rs 128272343Sngie 129272343Sngietwo_char_rs_head() { 130272343Sngie atf_set "descr" "Test awk(1) with two characters RS" 131272343Sngie} 132272343Sngie 133272343Sngietwo_char_rs_body() { 134272343Sngie atf_check \ 135272343Sngie -o "inline:1\n2\n3\n4\n\n" \ 136272343Sngie -x "echo 1ab2ab3ab4 | $awk 1 RS=ab" 137272343Sngie} 138272343Sngie 139272343Sngieatf_test_case single_char_regex_group_rs 140272343Sngie 141272343Sngiesingle_char_regex_group_rs_head() { 142272343Sngie atf_set "descr" "Test awk(1) with single character regex group RS" 143272343Sngie} 144272343Sngie 145272343Sngiesingle_char_regex_group_rs_body() { 146272343Sngie atf_check \ 147272343Sngie -o "inline:1\n2\n\n3\n\n\n4\n\n" \ 148272343Sngie -x "echo 1a2aa3aaa4 | $awk 1 RS='[a]'" 149272343Sngie} 150272343Sngie 151272343Sngieatf_test_case two_char_regex_group_rs 152272343Sngie 153272343Sngietwo_char_regex_group_rs_head() { 154272343Sngie atf_set "descr" "Test awk(1) with two characters regex group RS" 155272343Sngie} 156272343Sngie 157272343Sngietwo_char_regex_group_rs_body() { 158272343Sngie atf_check \ 159272343Sngie -o "inline:1\n2\n\n3\n\n\n4\n\n" \ 160272343Sngie -x "echo 1a2ab3aba4 | $awk 1 RS='[ab]'" 161272343Sngie} 162272343Sngie 163272343Sngieatf_test_case single_char_regex_star_rs 164272343Sngie 165272343Sngiesingle_char_regex_star_rs_head() { 166272343Sngie atf_set "descr" "Test awk(1) with single character regex star RS" 167272343Sngie} 168272343Sngie 169272343Sngiesingle_char_regex_star_rs_body() { 170272343Sngie atf_check \ 171272343Sngie -o "inline:1\n2\n3\n4\n\n" \ 172272343Sngie -x "echo 1a2aa3aaa4 | $awk 1 RS='a*'" 173272343Sngie} 174272343Sngie 175272343Sngieatf_test_case two_char_regex_star_rs 176272343Sngie 177272343Sngietwo_char_regex_star_rs_head() { 178272343Sngie atf_set "descr" "Test awk(1) with two characters regex star RS" 179272343Sngie} 180272343Sngie 181272343Sngietwo_char_regex_star_rs_body() { 182272343Sngie atf_check \ 183272343Sngie -o "inline:1\n2\n3\n4\n\n" \ 184272343Sngie -x "echo 1a2aa3aaa4 | $awk 1 RS='aa*'" 185272343Sngie} 186272343Sngie 187272343Sngieatf_test_case regex_two_star_rs 188272343Sngie 189272343Sngieregex_two_star_rs_head() { 190272343Sngie atf_set "descr" "Test awk(1) with regex two star RS" 191272343Sngie} 192272343Sngie 193272343Sngieregex_two_star_rs_body() { 194272343Sngie atf_check \ 195272343Sngie -o "inline:1\n2\n3\n4\n\n" \ 196272343Sngie -x "echo 1a2ab3aab4 | $awk 1 RS='aa*b*'" 197272343Sngie} 198272343Sngie 199272343Sngieatf_test_case regex_or_1_rs 200272343Sngie 201272343Sngieregex_or_1_rs_head() { 202272343Sngie atf_set "descr" "Test awk(1) with regex | case 1 RS" 203272343Sngie} 204272343Sngie 205272343Sngieregex_or_1_rs_body() { 206272343Sngie atf_check \ 207272343Sngie -o "inline:1a\nc\n\n" \ 208272343Sngie -x "echo 1abc | $awk 1 RS='abcde|b'" 209272343Sngie} 210272343Sngie 211272343Sngieatf_test_case regex_or_2_rs 212272343Sngie 213272343Sngieregex_or_2_rs_head() { 214272343Sngie atf_set "descr" "Test awk(1) with regex | case 2 RS" 215272343Sngie} 216272343Sngie 217272343Sngieregex_or_2_rs_body() { 218272343Sngie atf_check \ 219272343Sngie -o "inline:1a\ncdf2\n\n" \ 220272343Sngie -x "echo 1abcdf2 | $awk 1 RS='abcde|b'" 221272343Sngie} 222272343Sngie 223272343Sngieatf_test_case regex_or_3_rs 224272343Sngie 225272343Sngieregex_or_3_rs_head() { 226272343Sngie atf_set "descr" "Test awk(1) with regex | case 3 RS" 227272343Sngie} 228272343Sngie 229272343Sngieregex_or_3_rs_body() { 230272343Sngie atf_check \ 231272343Sngie -o "inline:1\n\nf2\n\n" \ 232272343Sngie -x "echo 1abcdebf2 | $awk 1 RS='abcde|b'" 233272343Sngie} 234272343Sngie 235272343Sngieatf_test_case regex_or_4_rs 236272343Sngie 237272343Sngieregex_or_4_rs_head() { 238272343Sngie atf_set "descr" "Test awk(1) with regex | case 4 RS" 239272343Sngie} 240272343Sngie 241272343Sngieregex_or_4_rs_body() { 242272343Sngie atf_check \ 243272343Sngie -o "inline:1\nbcdf2\n\n" \ 244272343Sngie -x "echo 1abcdf2 | $awk 1 RS='abcde|a'" 245272343Sngie 246272343Sngie} 247272343Sngie 248272343Sngieatf_test_case regex_caret_1_rs 249272343Sngie 250272343Sngieregex_caret_1_rs_head() { 251272343Sngie atf_set "descr" "Test awk(1) with regex ^ case 1 RS" 252272343Sngie} 253272343Sngie 254272343Sngieregex_caret_1_rs_body() { 255272343Sngie atf_check \ 256272343Sngie -o "inline:\n1a2a3a\n\n" \ 257272343Sngie -x "echo a1a2a3a | $awk 1 RS='^a'" 258272343Sngie 259272343Sngie} 260272343Sngie 261272343Sngieatf_test_case regex_caret_2_rs 262272343Sngie 263272343Sngieregex_caret_2_rs_head() { 264272343Sngie atf_set "descr" "Test awk(1) with regex ^ case 2 RS" 265272343Sngie} 266272343Sngie 267272343Sngieregex_caret_2_rs_body() { 268272343Sngie atf_check \ 269272343Sngie -o "inline:\naa1a2a\n\n" \ 270272343Sngie -x "echo aaa1a2a | $awk 1 RS='^a'" 271272343Sngie 272272343Sngie} 273272343Sngie 274272343Sngieatf_test_case regex_dollar_1_rs 275272343Sngie 276272343Sngieregex_dollar_1_rs_head() { 277272343Sngie atf_set "descr" "Test awk(1) with regex $ case 1 RS" 278272343Sngie} 279272343Sngie 280272343Sngieregex_dollar_1_rs_body() { 281272343Sngie atf_check \ 282272343Sngie -o "inline:a1a2a3a\n\n" \ 283272343Sngie -x "echo a1a2a3a | $awk 1 RS='a$'" 284272343Sngie 285272343Sngie} 286272343Sngie 287272343Sngieatf_test_case regex_dollar_2_rs 288272343Sngie 289272343Sngieregex_dollar_2_rs_head() { 290272343Sngie atf_set "descr" "Test awk(1) with regex $ case 2 RS" 291272343Sngie} 292272343Sngie 293272343Sngieregex_dollar_2_rs_body() { 294272343Sngie atf_check \ 295272343Sngie -o "inline:a1a2aaa\n\n" \ 296272343Sngie -x "echo a1a2aaa | $awk 1 RS='a$'" 297272343Sngie 298272343Sngie} 299272343Sngie 300272343Sngieatf_test_case regex_reallocation_rs 301272343Sngie 302272343Sngieregex_reallocation_rs_head() { 303272343Sngie atf_set "descr" "Test awk(1) with regex reallocation RS" 304272343Sngie} 305272343Sngie 306272343Sngieregex_reallocation_rs_body() { 307272343Sngie atf_check \ 308272343Sngie -o "inline:a\na\na\na\na\na\na\na\na\na10000\n\n" \ 309272343Sngie -x "jot -s a 10000 | $awk 'NR>1' RS='999[0-9]'" 310272343Sngie 311272343Sngie} 312272343Sngie 313272343Sngieatf_test_case empty_rs 314272343Sngie 315272343Sngieempty_rs_head() { 316272343Sngie atf_set "descr" "Test awk(1) with empty RS" 317272343Sngie} 318272343Sngie 319272343Sngieempty_rs_body() { 320272343Sngie atf_check \ 321272343Sngie -o "inline:foo\n" \ 322272343Sngie -x "echo foo | $awk 1 RS=''" 323272343Sngie 324272343Sngie} 325272343Sngie 326272343Sngieatf_test_case newline_rs 327272343Sngie 328272343Sngienewline_rs_head() { 329272343Sngie atf_set "descr" "Test awk(1) with newline RS" 330272343Sngie} 331272343Sngie 332272343Sngienewline_rs_body() { 333272343Sngie atf_check \ 334272343Sngie -o "inline:r1f1:r1f2\nr2f1:r2f2\n" \ 335272343Sngie -x "printf '\n\n\nr1f1\nr1f2\n\nr2f1\nr2f2\n\n\n' | $awk '{\$1=\$1}1' RS= OFS=:" 336272343Sngie} 337272343Sngie 338272343Sngieatf_test_case modify_subsep 339272343Sngie 340272343Sngiemodify_subsep_head() { 341272343Sngie atf_set "descr" "Test awk(1) SUPSEP modification (PR/47306)" 342272343Sngie} 343272343Sngie 344272343Sngiemodify_subsep_body() { 345272343Sngie atf_check \ 346272343Sngie -o "inline:1\n1\n1\n" \ 347272343Sngie -x "printf '1\n1 2\n' | \ 348272343Sngie $awk '1{ arr[\$1 SUBSEP \$2 SUBSEP ++cnt[\$1]]=1} {for (f in arr) print arr[f];}'" 349272343Sngie} 350272343Sngie 351272343Sngieatf_init_test_cases() { 352272343Sngie 353272343Sngie atf_add_test_case big_regexp 354272343Sngie atf_add_test_case end 355272343Sngie atf_add_test_case string1 356272343Sngie atf_add_test_case multibyte 357272343Sngie atf_add_test_case period 358272343Sngie atf_add_test_case assign_NF 359272343Sngie 360272343Sngie atf_add_test_case single_char_rs 361272343Sngie atf_add_test_case two_char_rs 362272343Sngie atf_add_test_case single_char_regex_group_rs 363272343Sngie atf_add_test_case two_char_regex_group_rs 364272343Sngie atf_add_test_case two_char_regex_star_rs 365272343Sngie atf_add_test_case single_char_regex_star_rs 366272343Sngie atf_add_test_case regex_two_star_rs 367272343Sngie atf_add_test_case regex_or_1_rs 368272343Sngie atf_add_test_case regex_or_2_rs 369272343Sngie atf_add_test_case regex_or_3_rs 370272343Sngie atf_add_test_case regex_caret_1_rs 371272343Sngie atf_add_test_case regex_caret_2_rs 372272343Sngie atf_add_test_case regex_dollar_1_rs 373272343Sngie atf_add_test_case regex_dollar_2_rs 374272343Sngie atf_add_test_case regex_reallocation_rs 375272343Sngie atf_add_test_case empty_rs 376272343Sngie atf_add_test_case newline_rs 377272343Sngie atf_add_test_case modify_subsep 378272343Sngie} 379