1# $NetBSD: t_awk.sh,v 1.5 2012/12/10 20:30:06 christos 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
31awk=awk
32
33h_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
43atf_test_case big_regexp
44
45big_regexp_head() {
46	atf_set "descr" "Checks matching long regular expressions (PR/33392)"
47}
48
49big_regexp_body() {
50	h_check big_regexp
51}
52
53atf_test_case end
54
55end_head() {
56	atf_set "descr" "Checks that the last line of the input" \
57	                "is available under END pattern (PR/29659)"
58}
59
60end_body() {
61	h_check end1
62	h_check end2
63}
64
65atf_test_case string1
66
67string1_head() {
68	atf_set "descr" "Checks escaping newlines in string literals"
69}
70
71string1_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
78atf_test_case multibyte
79
80multibyte_head() {
81	atf_set "descr" "Checks multibyte charsets support" \
82	                "in tolower and toupper (PR/36394)"
83}
84
85multibyte_body() {
86	export LANG=en_US.UTF-8
87
88	h_check tolower
89	h_check toupper
90}
91
92atf_test_case period
93
94period_head() {
95	atf_set "descr" "Checks that the period character is recognised" \
96	                "in awk program regardless of locale (bin/42320)"
97}
98
99period_body() {
100	export LANG=ru_RU.KOI8-R
101
102	h_check period -v x=0.5
103}
104
105atf_test_case assign_NF
106
107assign_NF_head() {
108	atf_set "descr" 'Checks that assign to NF changes $0 and $n (PR/44063)'
109}
110
111assign_NF_body() {
112	h_check assign_NF
113}
114
115atf_test_case single_char_rs
116
117single_char_rs_head() {
118	atf_set "descr" "Test awk(1) with single character RS"
119}
120
121single_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
127atf_test_case two_char_rs
128
129two_char_rs_head() {
130	atf_set "descr" "Test awk(1) with two characters RS"
131}
132
133two_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
139atf_test_case single_char_regex_group_rs
140
141single_char_regex_group_rs_head() {
142	atf_set "descr" "Test awk(1) with single character regex group RS"
143}
144
145single_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
151atf_test_case two_char_regex_group_rs
152
153two_char_regex_group_rs_head() {
154	atf_set "descr" "Test awk(1) with two characters regex group RS"
155}
156
157two_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
163atf_test_case single_char_regex_star_rs
164
165single_char_regex_star_rs_head() {
166	atf_set "descr" "Test awk(1) with single character regex star RS"
167}
168
169single_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
175atf_test_case two_char_regex_star_rs
176
177two_char_regex_star_rs_head() {
178	atf_set "descr" "Test awk(1) with two characters regex star RS"
179}
180
181two_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
187atf_test_case regex_two_star_rs
188
189regex_two_star_rs_head() {
190	atf_set "descr" "Test awk(1) with regex two star RS"
191}
192
193regex_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
199atf_test_case regex_or_1_rs
200
201regex_or_1_rs_head() {
202	atf_set "descr" "Test awk(1) with regex | case 1 RS"
203}
204
205regex_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
211atf_test_case regex_or_2_rs
212
213regex_or_2_rs_head() {
214	atf_set "descr" "Test awk(1) with regex | case 2 RS"
215}
216
217regex_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
223atf_test_case regex_or_3_rs
224
225regex_or_3_rs_head() {
226	atf_set "descr" "Test awk(1) with regex | case 3 RS"
227}
228
229regex_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
235atf_test_case regex_or_4_rs
236
237regex_or_4_rs_head() {
238	atf_set "descr" "Test awk(1) with regex | case 4 RS"
239}
240
241regex_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
248atf_test_case regex_caret_1_rs
249
250regex_caret_1_rs_head() {
251	atf_set "descr" "Test awk(1) with regex ^ case 1 RS"
252}
253
254regex_caret_1_rs_body() {
255	atf_check \
256		-o "inline:\n1a2a3a\n\n" \
257		-x "echo a1a2a3a | $awk 1 RS='^a'"
258
259}
260
261atf_test_case regex_caret_2_rs
262
263regex_caret_2_rs_head() {
264	atf_set "descr" "Test awk(1) with regex ^ case 2 RS"
265}
266
267regex_caret_2_rs_body() {
268	atf_check \
269		-o "inline:\naa1a2a\n\n" \
270		-x "echo aaa1a2a | $awk 1 RS='^a'"
271
272}
273
274atf_test_case regex_dollar_1_rs
275
276regex_dollar_1_rs_head() {
277	atf_set "descr" "Test awk(1) with regex $ case 1 RS"
278}
279
280regex_dollar_1_rs_body() {
281	atf_check \
282		-o "inline:a1a2a3a\n\n" \
283		-x "echo a1a2a3a | $awk 1 RS='a$'"
284
285}
286
287atf_test_case regex_dollar_2_rs
288
289regex_dollar_2_rs_head() {
290	atf_set "descr" "Test awk(1) with regex $ case 2 RS"
291}
292
293regex_dollar_2_rs_body() {
294	atf_check \
295		-o "inline:a1a2aaa\n\n" \
296		-x "echo a1a2aaa | $awk 1 RS='a$'"
297
298}
299
300atf_test_case regex_reallocation_rs
301
302regex_reallocation_rs_head() {
303	atf_set "descr" "Test awk(1) with regex reallocation RS"
304}
305
306regex_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
313atf_test_case empty_rs
314
315empty_rs_head() {
316	atf_set "descr" "Test awk(1) with empty RS"
317}
318
319empty_rs_body() {
320	atf_check \
321		-o "inline:foo\n" \
322		-x "echo foo | $awk 1 RS=''"
323
324}
325
326atf_test_case newline_rs
327
328newline_rs_head() {
329	atf_set "descr" "Test awk(1) with newline RS"
330}
331
332newline_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
338atf_test_case modify_subsep
339
340modify_subsep_head() {
341	atf_set "descr" "Test awk(1) SUPSEP modification (PR/47306)"
342}
343
344modify_subsep_body() {
345	atf_check \
346		-o "inline:1\n1\n1\n" \
347		-x "printf '1\n1 2\n' | \
348		$awk '1{ arr[\$1 SUBSEP \$2 SUBSEP ++cnt[\$1]]=1} {for (f in arr) print arr[f];}'"
349}
350
351atf_init_test_cases() {
352
353	atf_add_test_case big_regexp
354	atf_add_test_case end
355	atf_add_test_case string1
356	atf_add_test_case multibyte
357	atf_add_test_case period
358	atf_add_test_case assign_NF
359
360	atf_add_test_case single_char_rs
361	atf_add_test_case two_char_rs
362	atf_add_test_case single_char_regex_group_rs
363	atf_add_test_case two_char_regex_group_rs
364	atf_add_test_case two_char_regex_star_rs
365	atf_add_test_case single_char_regex_star_rs
366	atf_add_test_case regex_two_star_rs
367	atf_add_test_case regex_or_1_rs
368	atf_add_test_case regex_or_2_rs
369	atf_add_test_case regex_or_3_rs
370	atf_add_test_case regex_caret_1_rs
371	atf_add_test_case regex_caret_2_rs
372	atf_add_test_case regex_dollar_1_rs
373	atf_add_test_case regex_dollar_2_rs
374	atf_add_test_case regex_reallocation_rs
375	atf_add_test_case empty_rs
376	atf_add_test_case newline_rs
377	atf_add_test_case modify_subsep
378}
379