summaryrefslogtreecommitdiff
path: root/usr/src/test/util-tests/tests/sed/sed_addr.ksh
blob: e5327fcd27866a16fabe16c5887dc86389e7cc26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/bin/ksh -p
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source. A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.

# Copyright 2020 OmniOS Community Edition (OmniOSce) Association.

SED=${SED:=/usr/bin/sed}

function fatal {
	echo "[FATAL] $*" > /dev/stderr
	exit 1
}

function runtest {
	typeset script="$1"
	typeset expect="$2"
	typeset files="${3:-$input1}"

	typeset ef=`mktemp`
	[[ -n "$expect" ]] && printf "%s\n" $expect > $ef

	$SED -n "$script" $files > $output
	if [[ $? -eq 0 ]] && cmp -s $output $ef; then
		echo "[PASS] sed $script $files"
	else
		echo "[FAIL] sed $script $files"
		diff -u $ef $output
		err=1
	fi
	rm -f $ef
}

for f in input1 input2 output; do
	tf=`mktemp`
	[[ -n "$tf" && -f "$tf" ]] || fatal "Could not create temp file $f"
	eval $f=$tf
done

typeset err=0

printf "%s\n" a b c d e f g h a j > $input1
[[ $? -eq 0 && -s "$input1" ]] || fatal "Could not populate input1 file"
printf "%s\n" m n o p q z > $input2
[[ $? -eq 0 && -s "$input2" ]] || fatal "Could not populate input2 file"

# Simple
runtest "3p" "c"
runtest "\$p" "j"
runtest "7,\$p" "g h a j"
runtest "/d/p" "d"
runtest "/a/p" "a a"

# Ranges
runtest "5,7p" "e f g"
runtest "5,4p" "e"
runtest "/a/,4p" "a b c d a"
runtest "0,/b/p" ""
runtest "4,/a/p" "d e f g h a"
runtest "/d/,/g/p" "d e f g"

# Relative ranges
runtest "3,+0p" "c"
runtest "3,+1p" "c d"
runtest "5,+3p" "e f g h"
runtest "6,+3p" "f g h a"
runtest "7,+3p" "g h a j"
runtest "8,+3p" "h a j"
runtest "/a/,+1p" "a b a j"
runtest "/a/,+8p" "a b c d e f g h a"
runtest "/a/,+9p" "a b c d e f g h a j"

# Negative
runtest "4,7!p" "a b c h a j"
runtest "6,+3!p" "a b c d e j"
runtest "7,+3!p" "a b c d e f"
runtest "8,+3!p" "a b c d e f g"

# Branch
runtest "4,7 { /e/b
		p
	}" "d f g"
runtest "4,+3 { /e/b
		p
	}" "d f g"

# stdin
cat $input2 $input2 | runtest "\$p" "z" " "

# Multi-file
for fileset in \
	"$input1 $input2" \
	"$input1 /dev/null $input2" \
	"/dev/null $input1 $input2" \
	"$input1 $input2 /dev/null" \
	"$input1 /dev/null $input2 /dev/null" \
	"/dev/null $input1 /dev/null $input2" \
	"$input1 $input2 /dev/null /dev/null" \
	"$input1 /dev/null /dev/null $input2 /dev/null" \
	"/dev/null $input1 /dev/null /dev/null $input2" \
; do
	runtest "\$p" "z" "$fileset"
	runtest "3p" "c" "$fileset"
	runtest "13p" "o" "$fileset"
done

rm -f $input1 $input2 $output

[[ $err -eq 0 ]] && echo "--- All sed_addr tests passed"

exit $err