xref: /titanic_50/usr/src/test/util-tests/tests/xargs/xargs_test.ksh (revision 538aa54d819fa7751ca82bcc30d4ed8c57ec2ef2)
1#! /usr/bin/ksh
2#
3#
4# This file and its contents are supplied under the terms of the
5# Common Development and Distribution License ("CDDL"), version 1.0.
6# You may only use this file in accordance with the terms of version
7# 1.0 of the CDDL.
8#
9# A full copy of the text of the CDDL should have accompanied this
10# source.  A copy of the CDDL is also available via the Internet at
11# http://www.illumos.org/license/CDDL.
12#
13
14#
15# Copyright 2014 Garrett D'Amore <garrett@damore.org>
16#
17
18XARGS=${XARGS:=/usr/bin/xargs}
19
20test_start() {
21	print "TEST STARTING ${1}: ${2}"
22}
23
24test_pass() {
25	print "TEST PASS: ${1}"
26}
27
28test_fail() {
29	print "TEST FAIL: ${1}: ${2}"
30	exit -1
31}
32
33checkrv() {
34	if [[ $? -ne 0 ]]; then
35		test_fail $1 "exit failure"
36	fi
37}
38
39compare() {
40	if [[ "$2" != "$3" ]]; then
41		test_fail $1 "compare mismatch, got [$2] expected [$3]"
42	fi
43}
44
45test1() {
46	t=test1
47	test_start $t "-I handling"
48	comp=$(echo foo bar baz other | $XARGS -I THING echo '** THING **')
49	checkrv $t
50	good='** foo bar baz other **'
51	compare $t "$comp" "$good"
52	test_pass $t
53}
54
55test2() {
56	t=test2
57	test_start $t "-n 1 handling"
58	comp=$(echo foo bar baz other | $XARGS -n 1 echo '***')
59	checkrv $t
60	good='*** foo
61*** bar
62*** baz
63*** other'
64	compare $t "$comp" "$good"
65	test_pass $t
66}
67
68test3() {
69	t=test3
70	test_start $t "-I before -n 1"
71	comp=$(echo foo bar baz other | $XARGS -I THING -n1 echo '** THING **')
72	checkrv $t
73	good='** THING ** foo
74** THING ** bar
75** THING ** baz
76** THING ** other'
77	compare $t "$comp" "$good"
78	test_pass $t
79}
80
81test4() {
82	t=test4
83	test_start $t "-n 1  before -I"
84	comp=$(echo foo bar baz other | $XARGS -n 1 -I THING echo '** THING **')
85	checkrv $t
86	good='** foo bar baz other **'
87	compare $t "$comp" "$good"
88	test_pass $t
89}
90
91test5() {
92	t=test5
93	test_start $t "-i multiple lines handling"
94	comp=$(printf "abc def\nxyz\n123" | $XARGS -n1 -i echo '[{}]')
95	checkrv $t
96	good='[abc def]
97[xyz]
98[123]'
99	compare $t "$comp" "$good"
100	test_pass $t
101}
102
103test6() {
104	t=test6
105	test_start $t "-E handling"
106	comp=$(printf "abc def xyx\n_\n123\nnope" | $XARGS -edef echo)
107	checkrv $t
108	good='abc'
109	compare $t "$comp" "$good"
110	test_pass $t
111}
112
113test7() {
114	t=test7
115	test_start $t "newlines in arguments"
116	comp=$(printf "abc def\nxyz\n\n123 456\n789" | $XARGS echo)
117	checkrv $t
118	good='abc def xyz 123 456 789'
119	compare $t "$comp" "$good"
120	test_pass $t
121}
122
123test8() {
124	t=test8
125	test_start $t "limited counts via -n3"
126	comp=$(printf "abc def ghi jkl mno 123 456 789" | $XARGS -n 3 echo '**' )
127	checkrv $t
128	good='** abc def ghi
129** jkl mno 123
130** 456 789'
131	compare $t "$comp" "$good"
132	test_pass $t
133}
134
135test9() {
136	t=test9
137	test_start $t "multiple lines via -L2"
138	comp=$(printf "abc def\n123 456\npeterpiper" | $XARGS -L2 echo '**')
139	checkrv $t
140	good='** abc def 123 456
141** peterpiper'
142	compare $t "$comp" "$good"
143	test_pass $t
144}
145
146test10() {
147	t=test10
148	test_start $t "argument sizes"
149	comp=$(printf "abc def 123 456 peter alpha\n" | $XARGS -s15 echo)
150	checkrv $t
151	good='abc def
152123 456
153peter
154alpha'
155	compare $t "$comp" "$good"
156	test_pass $t
157}
158
159test11() {
160	t=test11
161	test_start $t "bare -e"
162	comp=$(printf "abc def _ end of string" | $XARGS -e echo '**')
163	checkrv $t
164	good='** abc def _ end of string'
165	compare $t "$comp" "$good"
166	test_pass $t
167}
168
169test12() {
170	t=test12
171	test_start $t "-E ''"
172	comp=$(printf "abc def _ end of string" | $XARGS -E '' echo '**')
173	checkrv $t
174	good='** abc def _ end of string'
175	compare $t "$comp" "$good"
176	test_pass $t
177}
178
179test13() {
180	t=test13
181	test_start $t "end of string (no -E or -e)"
182	comp=$(printf "abc def _ end of string" | $XARGS echo '**')
183	checkrv $t
184	good='** abc def'
185	compare $t "$comp" "$good"
186	test_pass $t
187}
188
189test14() {
190	t=test14
191	test_start $t "trailing blank with -L"
192	comp=$(printf "abc def \n123 456\npeter\nbogus" | $XARGS -L2 echo '**')
193	checkrv $t
194	good='** abc def 123 456 peter
195** bogus'
196	compare $t "$comp" "$good"
197	test_pass $t
198}
199
200test15() {
201	t=test15
202	test_start $t "leading and embedded blanks with -i"
203	comp=$(printf "abc def\n  xyz  bogus\nnext" | $XARGS -i echo '** {}')
204	checkrv $t
205	good='** abc def
206** xyz  bogus
207** next'
208	compare $t "$comp" "$good"
209	test_pass $t
210}
211
212test16() {
213	t=test16
214	test_start $t "single character replstring"
215	comp=$(echo foo bar baz other | $XARGS -I X echo '** X **')
216	checkrv $t
217	good='** foo bar baz other **'
218	compare $t "$comp" "$good"
219	test_pass $t
220}
221
222test17() {
223	t=test17
224	test_start $t "null byte separators"
225	comp=$(print 'foo bar baz\000more data' | $XARGS -n1 -0 echo '**')
226	checkrv $t
227	good='** foo bar baz
228** more data'
229	compare $t "$comp" "$good"
230	test_pass $t
231}
232
233test18() {
234	t=test18
235	test_start $t "escape characters"
236	comp=$(printf 'foo\\ bar second" "arg third' | $XARGS -n1 echo '**')
237	checkrv $t
238	good='** foo bar
239** second arg
240** third'
241	compare $t "$comp" "$good"
242	test_pass $t
243}
244
245test1
246test2
247test3
248test4
249test5
250test6
251test7
252test8
253test9
254test10
255test11
256test12
257test13
258test14
259test15
260test16
261test17
262test18
263