1272343Sngie# $NetBSD: t_sed.sh,v 1.5 2013/03/14 06:03:44 jmmv 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 Jukka Ruohonen and David A. Holland.
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
31272343Sngieatf_test_case c2048
32272343Sngiec2048_head() {
33272343Sngie	atf_set "descr" "Test that sed(1) does not fail when the " \
34272343Sngie			"2048'th character is a backslash (PR bin/25899)"
35272343Sngie}
36272343Sngie
37272343Sngiec2048_body() {
38272343Sngie
39272343Sngie	atf_check -s exit:0 -o inline:'foo\n' -e empty \
40272343Sngie		-x "echo foo | sed -f $(atf_get_srcdir)/d_c2048.in"
41272343Sngie}
42272343Sngie
43272343Sngieatf_test_case emptybackref
44272343Sngieemptybackref_head() {
45272343Sngie	atf_set "descr" "Test that sed(1) handles " \
46272343Sngie			"empty back references (PR bin/28126)"
47272343Sngie}
48272343Sngie
49272343Sngieemptybackref_body() {
50272343Sngie
51272343Sngie	atf_check -o inline:"foo1bar1\n" \
52272343Sngie		-x "echo foo1bar1 | sed -ne '/foo\(.*\)bar\1/p'"
53272343Sngie
54272343Sngie	atf_expect_fail "PR bin/28126"
55272343Sngie
56272343Sngie	atf_check -o inline:"foobar\n" \
57272343Sngie		-x "echo foobar | sed -ne '/foo\(.*\)bar\1/p'"
58272343Sngie}
59272343Sngie
60272343Sngieatf_test_case longlines
61272343Sngielonglines_head() {
62272343Sngie	atf_set "descr" "Test that sed(1) handles " \
63272343Sngie			"long lines correctly (PR bin/42261)"
64272343Sngie}
65272343Sngie
66272343Sngielonglines_body() {
67272343Sngie
68272343Sngie	str=$(awk 'BEGIN {while(x<2043){printf "x";x++}}')
69272343Sngie	echo $str > input
70272343Sngie
71272343Sngie	atf_check -o save:output -x "echo x | sed s,x,${str},g"
72272343Sngie	atf_check -s exit:0 -o empty -e empty -x "diff input output"
73272343Sngie}
74272343Sngie
75272343Sngieatf_test_case rangeselection
76272343Sngierangeselection_head() {
77272343Sngie	atf_set "descr" "Test that sed(1) handles " \
78272343Sngie			"range selection correctly"
79272343Sngie}
80272343Sngie
81272343Sngierangeselection_body() {
82272343Sngie	# basic cases
83272343Sngie	atf_check -o inline:"D\n" \
84272343Sngie		-x "printf 'A\nB\nC\nD\n' | sed '1,3d'"
85272343Sngie	atf_check -o inline:"A\n" \
86272343Sngie		-x "printf 'A\nB\nC\nD\n' | sed '2,4d'"
87272343Sngie	# two nonoverlapping ranges
88272343Sngie	atf_check -o inline:"C\n" \
89272343Sngie		-x "printf 'A\nB\nC\nD\nE\n' | sed '1,2d;4,5d'"
90272343Sngie	# overlapping ranges; the first prevents the second from being entered
91272343Sngie	atf_check -o inline:"D\nE\n" \
92272343Sngie		-x "printf 'A\nB\nC\nD\nE\n' | sed '1,3d;3,5d'"
93272343Sngie	# the 'n' command can also prevent ranges from being entered
94272343Sngie	atf_check -o inline:"B\nB\nC\nD\n" \
95272343Sngie		-x "printf 'A\nB\nC\nD\n' | sed '1,3s/A/B/;1,3n;1,3s/B/C/'"
96272343Sngie	atf_check -o inline:"B\nC\nC\nD\n" \
97272343Sngie		-x "printf 'A\nB\nC\nD\n' | sed '1,3s/A/B/;1,3n;2,3s/B/C/'"
98272343Sngie
99272343Sngie	# basic cases using regexps
100272343Sngie	atf_check -o inline:"D\n" \
101272343Sngie		-x "printf 'A\nB\nC\nD\n' | sed '/A/,/C/d'"
102272343Sngie	atf_check -o inline:"A\n" \
103272343Sngie		-x "printf 'A\nB\nC\nD\n' | sed '/B/,/D/d'"
104272343Sngie	# two nonoverlapping ranges
105272343Sngie	atf_check -o inline:"C\n" \
106272343Sngie		-x "printf 'A\nB\nC\nD\nE\n' | sed '/A/,/B/d;/D/,/E/d'"
107272343Sngie	# two overlapping ranges; the first blocks the second as above
108272343Sngie	atf_check -o inline:"D\nE\n" \
109272343Sngie		-x "printf 'A\nB\nC\nD\nE\n' | sed '/A/,/C/d;/C/,/E/d'"
110272343Sngie	# the 'n' command makes some lines invisible to downstreap regexps
111272343Sngie	atf_check -o inline:"B\nC\nC\nD\n" \
112272343Sngie		-x "printf 'A\nB\nC\nD\n' | sed '/A/,/C/s/A/B/;1,3n;/B/,/C/s/B/C/'"
113272343Sngie
114272343Sngie	# a range ends at the *first* matching end line
115272343Sngie	atf_check -o inline:"D\nC\n" \
116272343Sngie		-x "printf 'A\nB\nC\nD\nC\n' | sed '/A/,/C/d'"
117272343Sngie	# another matching start line within the range has no effect
118272343Sngie	atf_check -o inline:"D\nC\n" \
119272343Sngie		-x "printf 'A\nB\nA\nC\nD\nC\n' | sed '/A/,/C/d'"
120272343Sngie}
121272343Sngie
122272343Sngieatf_init_test_cases() {
123272343Sngie	atf_add_test_case c2048
124272343Sngie	atf_add_test_case emptybackref
125272343Sngie	atf_add_test_case longlines
126272343Sngie	atf_add_test_case rangeselection
127272343Sngie}
128