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