xref: /netbsd/tests/usr.bin/awk/t_awk.sh (revision e8114514)
1*e8114514Sjruoho# $NetBSD: t_awk.sh,v 1.7 2020/06/26 07:50:11 jruoho Exp $
217886a9cSchristos#
317886a9cSchristos# Copyright (c) 2012 The NetBSD Foundation, Inc.
417886a9cSchristos# All rights reserved.
517886a9cSchristos#
617886a9cSchristos# This code is derived from software contributed to The NetBSD Foundation
717886a9cSchristos# by Christos Zoulas
817886a9cSchristos#
917886a9cSchristos# Redistribution and use in source and binary forms, with or without
1017886a9cSchristos# modification, are permitted provided that the following conditions
1117886a9cSchristos# are met:
1217886a9cSchristos# 1. Redistributions of source code must retain the above copyright
1317886a9cSchristos#    notice, this list of conditions and the following disclaimer.
1417886a9cSchristos# 2. Redistributions in binary form must reproduce the above copyright
1517886a9cSchristos#    notice, this list of conditions and the following disclaimer in the
1617886a9cSchristos#    documentation and/or other materials provided with the distribution.
1717886a9cSchristos#
1817886a9cSchristos# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1917886a9cSchristos# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2017886a9cSchristos# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2117886a9cSchristos# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2217886a9cSchristos# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2317886a9cSchristos# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2417886a9cSchristos# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2517886a9cSchristos# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2617886a9cSchristos# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2717886a9cSchristos# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2817886a9cSchristos# POSSIBILITY OF SUCH DAMAGE.
2917886a9cSchristos#
3017886a9cSchristos
3117886a9cSchristosawk=awk
3217886a9cSchristos
335102c6e4Sjruohoh_check()
345102c6e4Sjruoho{
355102c6e4Sjruoho	local fname=d_$1
365102c6e4Sjruoho	for sfx in in out awk; do
375102c6e4Sjruoho		cp -r $(atf_get_srcdir)/$fname.$sfx .
385102c6e4Sjruoho	done
395102c6e4Sjruoho	shift 1
405102c6e4Sjruoho	atf_check -o file:$fname.out -x "awk $@ -f $fname.awk < $fname.in"
415102c6e4Sjruoho}
425102c6e4Sjruoho
435102c6e4Sjruohoatf_test_case big_regexp
445102c6e4Sjruoho
455102c6e4Sjruohobig_regexp_head() {
465102c6e4Sjruoho	atf_set "descr" "Checks matching long regular expressions (PR/33392)"
475102c6e4Sjruoho}
485102c6e4Sjruoho
495102c6e4Sjruohobig_regexp_body() {
505102c6e4Sjruoho	h_check big_regexp
515102c6e4Sjruoho}
525102c6e4Sjruoho
535102c6e4Sjruohoatf_test_case end
545102c6e4Sjruoho
555102c6e4Sjruohoend_head() {
565102c6e4Sjruoho	atf_set "descr" "Checks that the last line of the input" \
575102c6e4Sjruoho	                "is available under END pattern (PR/29659)"
585102c6e4Sjruoho}
595102c6e4Sjruoho
605102c6e4Sjruohoend_body() {
615102c6e4Sjruoho	h_check end1
625102c6e4Sjruoho	h_check end2
635102c6e4Sjruoho}
645102c6e4Sjruoho
655102c6e4Sjruohoatf_test_case string1
665102c6e4Sjruoho
675102c6e4Sjruohostring1_head() {
685102c6e4Sjruoho	atf_set "descr" "Checks escaping newlines in string literals"
695102c6e4Sjruoho}
705102c6e4Sjruoho
715102c6e4Sjruohostring1_body() {
725102c6e4Sjruoho	for sfx in out awk; do
735102c6e4Sjruoho		cp -r $(atf_get_srcdir)/d_string1.$sfx .
745102c6e4Sjruoho	done
755102c6e4Sjruoho	atf_check -o file:d_string1.out awk -f d_string1.awk
765102c6e4Sjruoho}
775102c6e4Sjruoho
785102c6e4Sjruohoatf_test_case multibyte
795102c6e4Sjruoho
805102c6e4Sjruohomultibyte_head() {
815102c6e4Sjruoho	atf_set "descr" "Checks multibyte charsets support" \
825102c6e4Sjruoho	                "in tolower and toupper (PR/36394)"
835102c6e4Sjruoho}
845102c6e4Sjruoho
855102c6e4Sjruohomultibyte_body() {
865102c6e4Sjruoho	export LANG=en_US.UTF-8
875102c6e4Sjruoho
885102c6e4Sjruoho	h_check tolower
895102c6e4Sjruoho	h_check toupper
905102c6e4Sjruoho}
915102c6e4Sjruoho
925102c6e4Sjruohoatf_test_case period
935102c6e4Sjruoho
945102c6e4Sjruohoperiod_head() {
955102c6e4Sjruoho	atf_set "descr" "Checks that the period character is recognised" \
96*e8114514Sjruoho	                "in awk program regardless of locale (PR bin/42320)"
975102c6e4Sjruoho}
985102c6e4Sjruoho
995102c6e4Sjruohoperiod_body() {
1005102c6e4Sjruoho	export LANG=ru_RU.KOI8-R
1015102c6e4Sjruoho
1025102c6e4Sjruoho	h_check period -v x=0.5
1035102c6e4Sjruoho}
1045102c6e4Sjruoho
1055102c6e4Sjruohoatf_test_case assign_NF
1065102c6e4Sjruoho
1075102c6e4Sjruohoassign_NF_head() {
1085102c6e4Sjruoho	atf_set "descr" 'Checks that assign to NF changes $0 and $n (PR/44063)'
1095102c6e4Sjruoho}
1105102c6e4Sjruoho
1115102c6e4Sjruohoassign_NF_body() {
1125102c6e4Sjruoho	h_check assign_NF
1135102c6e4Sjruoho}
1145102c6e4Sjruoho
11517886a9cSchristosatf_test_case single_char_rs
11617886a9cSchristos
11717886a9cSchristossingle_char_rs_head() {
11817886a9cSchristos	atf_set "descr" "Test awk(1) with single character RS"
11917886a9cSchristos}
12017886a9cSchristos
12117886a9cSchristossingle_char_rs_body() {
12217886a9cSchristos	atf_check \
12317886a9cSchristos		-o "inline:1\n2\n\n3\n\n\n4\n\n" \
12417886a9cSchristos		-x "echo 1a2aa3aaa4 | $awk 1 RS=a"
12517886a9cSchristos}
12617886a9cSchristos
12717886a9cSchristosatf_test_case two_char_rs
12817886a9cSchristos
12917886a9cSchristostwo_char_rs_head() {
13017886a9cSchristos	atf_set "descr" "Test awk(1) with two characters RS"
13117886a9cSchristos}
13217886a9cSchristos
13317886a9cSchristostwo_char_rs_body() {
13417886a9cSchristos	atf_check \
13517886a9cSchristos		-o "inline:1\n2\n3\n4\n\n" \
13617886a9cSchristos		-x "echo 1ab2ab3ab4 | $awk 1 RS=ab"
13717886a9cSchristos}
13817886a9cSchristos
13917886a9cSchristosatf_test_case single_char_regex_group_rs
14017886a9cSchristos
14117886a9cSchristossingle_char_regex_group_rs_head() {
14217886a9cSchristos	atf_set "descr" "Test awk(1) with single character regex group RS"
14317886a9cSchristos}
14417886a9cSchristos
14517886a9cSchristossingle_char_regex_group_rs_body() {
14617886a9cSchristos	atf_check \
14717886a9cSchristos		-o "inline:1\n2\n\n3\n\n\n4\n\n" \
14817886a9cSchristos		-x "echo 1a2aa3aaa4 | $awk 1 RS='[a]'"
14917886a9cSchristos}
15017886a9cSchristos
15117886a9cSchristosatf_test_case two_char_regex_group_rs
15217886a9cSchristos
15317886a9cSchristostwo_char_regex_group_rs_head() {
15417886a9cSchristos	atf_set "descr" "Test awk(1) with two characters regex group RS"
15517886a9cSchristos}
15617886a9cSchristos
15717886a9cSchristostwo_char_regex_group_rs_body() {
15817886a9cSchristos	atf_check \
15917886a9cSchristos		-o "inline:1\n2\n\n3\n\n\n4\n\n" \
16017886a9cSchristos		-x "echo 1a2ab3aba4 | $awk 1 RS='[ab]'"
16117886a9cSchristos}
16217886a9cSchristos
16317886a9cSchristosatf_test_case single_char_regex_star_rs
16417886a9cSchristos
16517886a9cSchristossingle_char_regex_star_rs_head() {
16617886a9cSchristos	atf_set "descr" "Test awk(1) with single character regex star RS"
16717886a9cSchristos}
16817886a9cSchristos
16917886a9cSchristossingle_char_regex_star_rs_body() {
17017886a9cSchristos	atf_check \
17117886a9cSchristos		-o "inline:1\n2\n3\n4\n\n" \
17217886a9cSchristos		-x "echo 1a2aa3aaa4 | $awk 1 RS='a*'"
17317886a9cSchristos}
17417886a9cSchristos
17517886a9cSchristosatf_test_case two_char_regex_star_rs
17617886a9cSchristos
17717886a9cSchristostwo_char_regex_star_rs_head() {
17817886a9cSchristos	atf_set "descr" "Test awk(1) with two characters regex star RS"
17917886a9cSchristos}
18017886a9cSchristos
18117886a9cSchristostwo_char_regex_star_rs_body() {
18217886a9cSchristos	atf_check \
18317886a9cSchristos		-o "inline:1\n2\n3\n4\n\n" \
18417886a9cSchristos		-x "echo 1a2aa3aaa4 | $awk 1 RS='aa*'"
18517886a9cSchristos}
18617886a9cSchristos
18717886a9cSchristosatf_test_case regex_two_star_rs
18817886a9cSchristos
18917886a9cSchristosregex_two_star_rs_head() {
19017886a9cSchristos	atf_set "descr" "Test awk(1) with regex two star RS"
19117886a9cSchristos}
19217886a9cSchristos
19317886a9cSchristosregex_two_star_rs_body() {
19417886a9cSchristos	atf_check \
19517886a9cSchristos		-o "inline:1\n2\n3\n4\n\n" \
19617886a9cSchristos		-x "echo 1a2ab3aab4 | $awk 1 RS='aa*b*'"
19717886a9cSchristos}
19817886a9cSchristos
19917886a9cSchristosatf_test_case regex_or_1_rs
20017886a9cSchristos
20117886a9cSchristosregex_or_1_rs_head() {
20217886a9cSchristos	atf_set "descr" "Test awk(1) with regex | case 1 RS"
20317886a9cSchristos}
20417886a9cSchristos
20517886a9cSchristosregex_or_1_rs_body() {
20617886a9cSchristos	atf_check \
20717886a9cSchristos		-o "inline:1a\nc\n\n" \
20817886a9cSchristos		-x "echo 1abc | $awk 1 RS='abcde|b'"
20917886a9cSchristos}
21017886a9cSchristos
21117886a9cSchristosatf_test_case regex_or_2_rs
21217886a9cSchristos
21317886a9cSchristosregex_or_2_rs_head() {
21417886a9cSchristos	atf_set "descr" "Test awk(1) with regex | case 2 RS"
21517886a9cSchristos}
21617886a9cSchristos
21717886a9cSchristosregex_or_2_rs_body() {
21817886a9cSchristos	atf_check \
21917886a9cSchristos		-o "inline:1a\ncdf2\n\n" \
22017886a9cSchristos		-x "echo 1abcdf2 | $awk 1 RS='abcde|b'"
22117886a9cSchristos}
22217886a9cSchristos
22317886a9cSchristosatf_test_case regex_or_3_rs
22417886a9cSchristos
22517886a9cSchristosregex_or_3_rs_head() {
22617886a9cSchristos	atf_set "descr" "Test awk(1) with regex | case 3 RS"
22717886a9cSchristos}
22817886a9cSchristos
22917886a9cSchristosregex_or_3_rs_body() {
23017886a9cSchristos	atf_check \
23117886a9cSchristos		-o "inline:1\n\nf2\n\n" \
23217886a9cSchristos		-x "echo 1abcdebf2 | $awk 1 RS='abcde|b'"
23317886a9cSchristos}
23417886a9cSchristos
23517886a9cSchristosatf_test_case regex_or_4_rs
23617886a9cSchristos
23717886a9cSchristosregex_or_4_rs_head() {
23817886a9cSchristos	atf_set "descr" "Test awk(1) with regex | case 4 RS"
23917886a9cSchristos}
24017886a9cSchristos
24117886a9cSchristosregex_or_4_rs_body() {
24217886a9cSchristos	atf_check \
24317886a9cSchristos		-o "inline:1\nbcdf2\n\n" \
24417886a9cSchristos		-x "echo 1abcdf2 | $awk 1 RS='abcde|a'"
24517886a9cSchristos
24617886a9cSchristos}
24717886a9cSchristos
24817886a9cSchristosatf_test_case regex_caret_1_rs
24917886a9cSchristos
25017886a9cSchristosregex_caret_1_rs_head() {
25117886a9cSchristos	atf_set "descr" "Test awk(1) with regex ^ case 1 RS"
25217886a9cSchristos}
25317886a9cSchristos
25417886a9cSchristosregex_caret_1_rs_body() {
25517886a9cSchristos	atf_check \
25617886a9cSchristos		-o "inline:\n1a2a3a\n\n" \
25717886a9cSchristos		-x "echo a1a2a3a | $awk 1 RS='^a'"
25817886a9cSchristos
25917886a9cSchristos}
26017886a9cSchristos
26117886a9cSchristosatf_test_case regex_caret_2_rs
26217886a9cSchristos
26317886a9cSchristosregex_caret_2_rs_head() {
26417886a9cSchristos	atf_set "descr" "Test awk(1) with regex ^ case 2 RS"
26517886a9cSchristos}
26617886a9cSchristos
26717886a9cSchristosregex_caret_2_rs_body() {
26817886a9cSchristos	atf_check \
2692a028dc9Schristos		-o "inline:\naa1a2a\n\n" \
27017886a9cSchristos		-x "echo aaa1a2a | $awk 1 RS='^a'"
27117886a9cSchristos
27217886a9cSchristos}
27317886a9cSchristos
27417886a9cSchristosatf_test_case regex_dollar_1_rs
27517886a9cSchristos
27617886a9cSchristosregex_dollar_1_rs_head() {
27717886a9cSchristos	atf_set "descr" "Test awk(1) with regex $ case 1 RS"
27817886a9cSchristos}
27917886a9cSchristos
28017886a9cSchristosregex_dollar_1_rs_body() {
28117886a9cSchristos	atf_check \
28217886a9cSchristos		-o "inline:a1a2a3a\n\n" \
28317886a9cSchristos		-x "echo a1a2a3a | $awk 1 RS='a$'"
28417886a9cSchristos
28517886a9cSchristos}
28617886a9cSchristos
28717886a9cSchristosatf_test_case regex_dollar_2_rs
28817886a9cSchristos
28917886a9cSchristosregex_dollar_2_rs_head() {
29017886a9cSchristos	atf_set "descr" "Test awk(1) with regex $ case 2 RS"
29117886a9cSchristos}
29217886a9cSchristos
29317886a9cSchristosregex_dollar_2_rs_body() {
29417886a9cSchristos	atf_check \
2952a028dc9Schristos		-o "inline:a1a2aaa\n\n" \
29617886a9cSchristos		-x "echo a1a2aaa | $awk 1 RS='a$'"
29717886a9cSchristos
29817886a9cSchristos}
29917886a9cSchristos
30017886a9cSchristosatf_test_case regex_reallocation_rs
30117886a9cSchristos
30217886a9cSchristosregex_reallocation_rs_head() {
30317886a9cSchristos	atf_set "descr" "Test awk(1) with regex reallocation RS"
30417886a9cSchristos}
30517886a9cSchristos
30617886a9cSchristosregex_reallocation_rs_body() {
30717886a9cSchristos	atf_check \
30817886a9cSchristos		-o "inline:a\na\na\na\na\na\na\na\na\na10000\n\n" \
30917886a9cSchristos		-x "jot -s a 10000 | $awk 'NR>1' RS='999[0-9]'"
31017886a9cSchristos
31117886a9cSchristos}
31217886a9cSchristos
31317886a9cSchristosatf_test_case empty_rs
31417886a9cSchristos
31517886a9cSchristosempty_rs_head() {
31617886a9cSchristos	atf_set "descr" "Test awk(1) with empty RS"
31717886a9cSchristos}
31817886a9cSchristos
31917886a9cSchristosempty_rs_body() {
32017886a9cSchristos	atf_check \
32117886a9cSchristos		-o "inline:foo\n" \
3222a028dc9Schristos		-x "echo foo | $awk 1 RS=''"
32317886a9cSchristos
32417886a9cSchristos}
32517886a9cSchristos
32617886a9cSchristosatf_test_case newline_rs
32717886a9cSchristos
32817886a9cSchristosnewline_rs_head() {
32917886a9cSchristos	atf_set "descr" "Test awk(1) with newline RS"
33017886a9cSchristos}
33117886a9cSchristos
33217886a9cSchristosnewline_rs_body() {
33317886a9cSchristos	atf_check \
33417886a9cSchristos		-o "inline:r1f1:r1f2\nr2f1:r2f2\n" \
3352a028dc9Schristos		-x "printf '\n\n\nr1f1\nr1f2\n\nr2f1\nr2f2\n\n\n' | $awk '{\$1=\$1}1' RS= OFS=:"
33617886a9cSchristos}
33717886a9cSchristos
3380f68210cSchristosatf_test_case regex_range
3390f68210cSchristos
3400f68210cSchristosregex_range_head() {
3410f68210cSchristos	atf_set "descr" "Test awk(1) with regex range"
3420f68210cSchristos}
3430f68210cSchristos
3440f68210cSchristosregex_range_body() {
3450f68210cSchristos	atf_check \
3460f68210cSchristos		-o "inline:matched\n" \
3470f68210cSchristos		-x "echo '1 a' | $awk '/[[:digit:]][[:space:]][[:alpha:]]/ { print \"matched\"; }'"
3480f68210cSchristos}
3490f68210cSchristos
3500f68210cSchristosatf_test_case regex_repeat
3510f68210cSchristos
3520f68210cSchristosregex_repeat_head() {
3530f68210cSchristos	atf_set "descr" "Test awk(1) with regex repeat"
3540f68210cSchristos}
3550f68210cSchristos
3560f68210cSchristosregex_repeat_body() {
3570f68210cSchristos	atf_check \
3580f68210cSchristos		-o "inline:matched\n" \
3590f68210cSchristos		-x "echo 'aaabbbbcc' | $awk '/a{3}b{4}c{2}/ { print \"matched\"; }'"
3600f68210cSchristos}
3610f68210cSchristos
3629dbddf28Schristosatf_test_case modify_subsep
3639dbddf28Schristos
3649dbddf28Schristosmodify_subsep_head() {
3659dbddf28Schristos	atf_set "descr" "Test awk(1) SUPSEP modification (PR/47306)"
3669dbddf28Schristos}
3679dbddf28Schristos
3689dbddf28Schristosmodify_subsep_body() {
3699dbddf28Schristos	atf_check \
3709dbddf28Schristos		-o "inline:1\n1\n1\n" \
3719dbddf28Schristos		-x "printf '1\n1 2\n' | \
3729dbddf28Schristos		$awk '1{ arr[\$1 SUBSEP \$2 SUBSEP ++cnt[\$1]]=1} {for (f in arr) print arr[f];}'"
3739dbddf28Schristos}
3749dbddf28Schristos
37517886a9cSchristosatf_init_test_cases() {
3765102c6e4Sjruoho
3775102c6e4Sjruoho	atf_add_test_case big_regexp
3785102c6e4Sjruoho	atf_add_test_case end
3795102c6e4Sjruoho	atf_add_test_case string1
3805102c6e4Sjruoho	atf_add_test_case multibyte
3815102c6e4Sjruoho	atf_add_test_case period
3825102c6e4Sjruoho	atf_add_test_case assign_NF
3835102c6e4Sjruoho
38417886a9cSchristos	atf_add_test_case single_char_rs
38517886a9cSchristos	atf_add_test_case two_char_rs
38617886a9cSchristos	atf_add_test_case single_char_regex_group_rs
38717886a9cSchristos	atf_add_test_case two_char_regex_group_rs
38817886a9cSchristos	atf_add_test_case two_char_regex_star_rs
38917886a9cSchristos	atf_add_test_case single_char_regex_star_rs
39017886a9cSchristos	atf_add_test_case regex_two_star_rs
39117886a9cSchristos	atf_add_test_case regex_or_1_rs
39217886a9cSchristos	atf_add_test_case regex_or_2_rs
39317886a9cSchristos	atf_add_test_case regex_or_3_rs
39417886a9cSchristos	atf_add_test_case regex_caret_1_rs
39517886a9cSchristos	atf_add_test_case regex_caret_2_rs
39617886a9cSchristos	atf_add_test_case regex_dollar_1_rs
39717886a9cSchristos	atf_add_test_case regex_dollar_2_rs
39817886a9cSchristos	atf_add_test_case regex_reallocation_rs
39917886a9cSchristos	atf_add_test_case empty_rs
40017886a9cSchristos	atf_add_test_case newline_rs
4010f68210cSchristos	atf_add_test_case regex_range
4020f68210cSchristos	atf_add_test_case regex_repeat
4039dbddf28Schristos	atf_add_test_case modify_subsep
40417886a9cSchristos}
405