xref: /netbsd-src/tests/lib/libc/stdlib/t_getopt.sh (revision 360a298dc1ba9b1542aad62ec08af0c3ff9c95cf)
1*360a298dSgutteridge# $NetBSD: t_getopt.sh,v 1.3 2023/05/10 23:44:15 gutteridge Exp $
29a073313Spgoyette#
39a073313Spgoyette# Copyright (c) 2008 The NetBSD Foundation, Inc.
49a073313Spgoyette# All rights reserved.
59a073313Spgoyette#
69a073313Spgoyette# Redistribution and use in source and binary forms, with or without
79a073313Spgoyette# modification, are permitted provided that the following conditions
89a073313Spgoyette# are met:
99a073313Spgoyette# 1. Redistributions of source code must retain the above copyright
109a073313Spgoyette#    notice, this list of conditions and the following disclaimer.
119a073313Spgoyette# 2. Redistributions in binary form must reproduce the above copyright
129a073313Spgoyette#    notice, this list of conditions and the following disclaimer in the
139a073313Spgoyette#    documentation and/or other materials provided with the distribution.
149a073313Spgoyette#
159a073313Spgoyette# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
169a073313Spgoyette# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
179a073313Spgoyette# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
189a073313Spgoyette# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
199a073313Spgoyette# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
209a073313Spgoyette# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
219a073313Spgoyette# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
229a073313Spgoyette# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
239a073313Spgoyette# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
249a073313Spgoyette# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
259a073313Spgoyette# POSSIBILITY OF SUCH DAMAGE.
269a073313Spgoyette#
279a073313Spgoyette
289a073313Spgoyetteh_getopt()
299a073313Spgoyette{
309a073313Spgoyette	atf_check -e save:stderr -x "$(atf_get_srcdir)/h_getopt" <<EOF
319a073313Spgoyetteload:	$1
329a073313Spgoyetteargs:	$2
339a073313Spgoyetteresult:	$3
349a073313SpgoyetteEOF
359a073313Spgoyette	cat stderr
369a073313Spgoyette	rm -f stderr
379a073313Spgoyette}
389a073313Spgoyette
399a073313Spgoyetteh_getopt_long()
409a073313Spgoyette{
419a073313Spgoyette	atf_check -e save:stderr -x "$(atf_get_srcdir)/h_getopt_long" <<EOF
429a073313Spgoyette$1
439a073313Spgoyetteargs:	$2
449a073313Spgoyetteresult:	$3
459a073313SpgoyetteEOF
469a073313Spgoyette	cat stderr
479a073313Spgoyette	rm -f stderr
489a073313Spgoyette}
499a073313Spgoyette
509a073313Spgoyetteatf_test_case getopt
519a073313Spgoyettegetopt_head()
529a073313Spgoyette{
539a073313Spgoyette	atf_set "descr" "Checks getopt(3)"
549a073313Spgoyette}
559a073313Spgoyettegetopt_body()
569a073313Spgoyette{
579a073313Spgoyette	load="c:d"
589a073313Spgoyette
599a073313Spgoyette	h_getopt "${load}" "foo -c 1 -d foo" "c=1,d|1"
609a073313Spgoyette	h_getopt "${load}" "foo -d foo bar" "d|2"
619a073313Spgoyette	h_getopt "${load}" "foo -c 2 foo bar" "c=2|2"
629a073313Spgoyette	h_getopt "${load}" "foo -e 1 foo bar" "!?|3"
639a073313Spgoyette	h_getopt "${load}" "foo -d -- -c 1" "d|2"
649a073313Spgoyette	h_getopt "${load}" "foo -c- 1" "c=-|1"
659a073313Spgoyette	h_getopt "${load}" "foo -d - 1" "d|2"
669a073313Spgoyette}
679a073313Spgoyette
6836c8de92Schristosatf_test_case getopt_optval
69*360a298dSgutteridgegetopt_optval_head()
7036c8de92Schristos{
7136c8de92Schristos	atf_set "descr" "Checks getopt(3) with optional value"
7236c8de92Schristos}
7336c8de92Schristosgetopt_optval_body()
7436c8de92Schristos{
7536c8de92Schristos	h_getopt "o::" "foo -o" "o=(null)|0"
7636c8de92Schristos	h_getopt "o::" "foo -o1 2" "o=1|1"
7736c8de92Schristos	h_getopt "o::" "foo -o 1 2" "o=(null)|2"
7836c8de92Schristos}
7936c8de92Schristos
809a073313Spgoyetteatf_test_case getopt_long
819a073313Spgoyettegetopt_long_head()
829a073313Spgoyette{
839a073313Spgoyette	atf_set "descr" "Checks getopt_long(3)"
849a073313Spgoyette}
859a073313Spgoyettegetopt_long_body()
869a073313Spgoyette{
879a073313Spgoyette	# GNU libc tests with these
889a073313Spgoyette	load="optstring:	abc:
899a073313Spgoyettelongopts:	5
909a073313Spgoyettelongopt:	required, required_argument, , 'r'
919a073313Spgoyettelongopt:	optional, optional_argument, , 'o'
929a073313Spgoyettelongopt:	none, no_argument, , 'n'
939a073313Spgoyettelongopt:	color, no_argument, , 'C'
949a073313Spgoyettelongopt:	colour, no_argument, , 'C'"
959a073313Spgoyette
969a073313Spgoyette	h_getopt_long "${load}" "foo --req foobar" "-required=foobar|0"
979a073313Spgoyette	h_getopt_long "${load}" "foo --opt=bazbug" "-optional=bazbug|0"
989a073313Spgoyette
999a073313Spgoyette	# This is problematic
1009a073313Spgoyette	#
1019a073313Spgoyette	# GNU libc 2.1.3 this fails with ambiguous result
1029a073313Spgoyette	# h_getopt_long "${load}" "foo --col" "!?|0"
1039a073313Spgoyette	#
1049a073313Spgoyette	# GNU libc 2.2 >= this works
1059a073313Spgoyette	h_getopt_long "${load}" "foo --col" "-color|0"
1069a073313Spgoyette
1079a073313Spgoyette	h_getopt_long "${load}" "foo --colour" "-colour|0"
1089a073313Spgoyette
1099a073313Spgoyette	# This is the real GNU libc test!
1109a073313Spgoyette	args="foo -a -b -cfoobar --required foobar --optional=bazbug --none random --col --color --colour"
1119a073313Spgoyette	# GNU libc 2.1.3 this fails with ambiguous
1129a073313Spgoyette	#result="a,b,c=foobar,-required=foobar,-optional=bazbug,-none,!?,-color,-colour|1"
1139a073313Spgoyette	#
1149a073313Spgoyette	# GNU libc 2.2 >= this works
1159a073313Spgoyette	result="a,b,c=foobar,-required=foobar,-optional=bazbug,-none,-color,-color,-colour|1"
1169a073313Spgoyette	h_getopt_long "${load}" "${args}" "${result}"
1179a073313Spgoyette
1189a073313Spgoyette	#
1199a073313Spgoyette	# The order of long options in the long option array should not matter.
1209a073313Spgoyette	# An exact match should never be treated as ambiguous.
1219a073313Spgoyette	#
1229a073313Spgoyette	load="optstring:	fgl
1239a073313Spgoyettelongopts:	3
1249a073313Spgoyettelongopt:	list-foobar, no_argument, lopt, 'f'
1259a073313Spgoyettelongopt:	list-goobar, no_argument, lopt, 'g'
1269a073313Spgoyettelongopt:	list, no_argument, lopt, 'l'"
1279a073313Spgoyette	h_getopt_long "${load}" "foo --list" "-list|0"
1289a073313Spgoyette}
1299a073313Spgoyette
1309a073313Spgoyette
1319a073313Spgoyetteatf_init_test_cases()
1329a073313Spgoyette{
1339a073313Spgoyette	atf_add_test_case getopt
13436c8de92Schristos	atf_add_test_case getopt_optval
1359a073313Spgoyette	atf_add_test_case getopt_long
1369a073313Spgoyette}
137