xref: /illumos-gate/usr/src/test/util-tests/tests/sed/sed_addr.ksh (revision e0721d5ae1542c80097f6fcd487736fdfe601233)
1#!/bin/ksh -p
2#
3# This file and its contents are supplied under the terms of the
4# Common Development and Distribution License ("CDDL"), version 1.0.
5# You may only use this file in accordance with the terms of version
6# 1.0 of the CDDL.
7#
8# A full copy of the text of the CDDL should have accompanied this
9# source. A copy of the CDDL is also available via the Internet at
10# http://www.illumos.org/license/CDDL.
11
12# Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
13
14function fatal {
15	echo "[FATAL] $*" > /dev/stderr
16	exit 1
17}
18
19function runtest {
20	typeset script="$1"
21	typeset expect="$2"
22
23	typeset ef=`mktemp`
24	[[ -n "$expect" ]] && printf "%s\n" $expect > $ef
25
26	sed -n "$script" < $input > $output
27	if [[ $? -eq 0 ]] && cmp -s $output $ef; then
28		echo "[PASS] sed $script"
29	else
30		echo "[FAIL] sed $script"
31		diff -u $ef $output
32		err=1
33	fi
34	rm -f $ef
35}
36
37input=`mktemp`
38output=`mktemp`
39[[ -n "$input" && -f "$input" ]] || fatal "Could not create temp input"
40[[ -n "$output" && -f "$output" ]] || fatal "Could not create temp output"
41
42typeset err=0
43printf "%s\n" a b c d e f g h a j > $input
44[[ $? -eq 0 && -s "$input" ]] || fatal "Could not populate input file"
45
46# Simple
47runtest "3p" "c"
48runtest "\$p" "j"
49runtest "7,\$p" "g h a j"
50runtest "/d/p" "d"
51runtest "/a/p" "a a"
52
53# Ranges
54runtest "5,7p" "e f g"
55runtest "5,4p" "e"
56runtest "/a/,4p" "a b c d a"
57runtest "0,/b/p" ""
58runtest "4,/a/p" "d e f g h a"
59runtest "/d/,/g/p" "d e f g"
60
61# Relative ranges
62runtest "3,+0p" "c"
63runtest "3,+1p" "c d"
64runtest "5,+3p" "e f g h"
65runtest "6,+3p" "f g h a"
66runtest "7,+3p" "g h a j"
67runtest "8,+3p" "h a j"
68runtest "/a/,+1p" "a b a j"
69runtest "/a/,+8p" "a b c d e f g h a"
70runtest "/a/,+9p" "a b c d e f g h a j"
71
72# Negative
73runtest "4,7!p" "a b c h a j"
74runtest "6,+3!p" "a b c d e j"
75runtest "7,+3!p" "a b c d e f"
76runtest "8,+3!p" "a b c d e f g"
77
78# Branch
79runtest "4,7 { /e/b
80		p
81	}" "d f g"
82runtest "4,+3 { /e/b
83		p
84	}" "d f g"
85
86rm -f $input $output
87
88exit $err
89