xref: /minix3/tests/lib/libc/stdlib/t_getopt.sh (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc# $NetBSD: t_getopt.sh,v 1.1 2011/01/01 23:56:49 pgoyette Exp $
2*11be35a1SLionel Sambuc#
3*11be35a1SLionel Sambuc# Copyright (c) 2008 The NetBSD Foundation, Inc.
4*11be35a1SLionel Sambuc# All rights reserved.
5*11be35a1SLionel Sambuc#
6*11be35a1SLionel Sambuc# Redistribution and use in source and binary forms, with or without
7*11be35a1SLionel Sambuc# modification, are permitted provided that the following conditions
8*11be35a1SLionel Sambuc# are met:
9*11be35a1SLionel Sambuc# 1. Redistributions of source code must retain the above copyright
10*11be35a1SLionel Sambuc#    notice, this list of conditions and the following disclaimer.
11*11be35a1SLionel Sambuc# 2. Redistributions in binary form must reproduce the above copyright
12*11be35a1SLionel Sambuc#    notice, this list of conditions and the following disclaimer in the
13*11be35a1SLionel Sambuc#    documentation and/or other materials provided with the distribution.
14*11be35a1SLionel Sambuc#
15*11be35a1SLionel Sambuc# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16*11be35a1SLionel Sambuc# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17*11be35a1SLionel Sambuc# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18*11be35a1SLionel Sambuc# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19*11be35a1SLionel Sambuc# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20*11be35a1SLionel Sambuc# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21*11be35a1SLionel Sambuc# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22*11be35a1SLionel Sambuc# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23*11be35a1SLionel Sambuc# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24*11be35a1SLionel Sambuc# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25*11be35a1SLionel Sambuc# POSSIBILITY OF SUCH DAMAGE.
26*11be35a1SLionel Sambuc#
27*11be35a1SLionel Sambuc
28*11be35a1SLionel Sambuch_getopt()
29*11be35a1SLionel Sambuc{
30*11be35a1SLionel Sambuc	atf_check -e save:stderr -x "$(atf_get_srcdir)/h_getopt" <<EOF
31*11be35a1SLionel Sambucload:	$1
32*11be35a1SLionel Sambucargs:	$2
33*11be35a1SLionel Sambucresult:	$3
34*11be35a1SLionel SambucEOF
35*11be35a1SLionel Sambuc	cat stderr
36*11be35a1SLionel Sambuc	rm -f stderr
37*11be35a1SLionel Sambuc}
38*11be35a1SLionel Sambuc
39*11be35a1SLionel Sambuch_getopt_long()
40*11be35a1SLionel Sambuc{
41*11be35a1SLionel Sambuc	atf_check -e save:stderr -x "$(atf_get_srcdir)/h_getopt_long" <<EOF
42*11be35a1SLionel Sambuc$1
43*11be35a1SLionel Sambucargs:	$2
44*11be35a1SLionel Sambucresult:	$3
45*11be35a1SLionel SambucEOF
46*11be35a1SLionel Sambuc	cat stderr
47*11be35a1SLionel Sambuc	rm -f stderr
48*11be35a1SLionel Sambuc}
49*11be35a1SLionel Sambuc
50*11be35a1SLionel Sambucatf_test_case getopt
51*11be35a1SLionel Sambucgetopt_head()
52*11be35a1SLionel Sambuc{
53*11be35a1SLionel Sambuc	atf_set "descr" "Checks getopt(3)"
54*11be35a1SLionel Sambuc}
55*11be35a1SLionel Sambucgetopt_body()
56*11be35a1SLionel Sambuc{
57*11be35a1SLionel Sambuc	load="c:d"
58*11be35a1SLionel Sambuc
59*11be35a1SLionel Sambuc	h_getopt "${load}" "foo -c 1 -d foo" "c=1,d|1"
60*11be35a1SLionel Sambuc	h_getopt "${load}" "foo -d foo bar" "d|2"
61*11be35a1SLionel Sambuc	h_getopt "${load}" "foo -c 2 foo bar" "c=2|2"
62*11be35a1SLionel Sambuc	h_getopt "${load}" "foo -e 1 foo bar" "!?|3"
63*11be35a1SLionel Sambuc	h_getopt "${load}" "foo -d -- -c 1" "d|2"
64*11be35a1SLionel Sambuc	h_getopt "${load}" "foo -c- 1" "c=-|1"
65*11be35a1SLionel Sambuc	h_getopt "${load}" "foo -d - 1" "d|2"
66*11be35a1SLionel Sambuc}
67*11be35a1SLionel Sambuc
68*11be35a1SLionel Sambucatf_test_case getopt_long
69*11be35a1SLionel Sambucgetopt_long_head()
70*11be35a1SLionel Sambuc{
71*11be35a1SLionel Sambuc	atf_set "descr" "Checks getopt_long(3)"
72*11be35a1SLionel Sambuc}
73*11be35a1SLionel Sambucgetopt_long_body()
74*11be35a1SLionel Sambuc{
75*11be35a1SLionel Sambuc	# GNU libc tests with these
76*11be35a1SLionel Sambuc	load="optstring:	abc:
77*11be35a1SLionel Sambuclongopts:	5
78*11be35a1SLionel Sambuclongopt:	required, required_argument, , 'r'
79*11be35a1SLionel Sambuclongopt:	optional, optional_argument, , 'o'
80*11be35a1SLionel Sambuclongopt:	none, no_argument, , 'n'
81*11be35a1SLionel Sambuclongopt:	color, no_argument, , 'C'
82*11be35a1SLionel Sambuclongopt:	colour, no_argument, , 'C'"
83*11be35a1SLionel Sambuc
84*11be35a1SLionel Sambuc	h_getopt_long "${load}" "foo --req foobar" "-required=foobar|0"
85*11be35a1SLionel Sambuc	h_getopt_long "${load}" "foo --opt=bazbug" "-optional=bazbug|0"
86*11be35a1SLionel Sambuc
87*11be35a1SLionel Sambuc	# This is problematic
88*11be35a1SLionel Sambuc	#
89*11be35a1SLionel Sambuc	# GNU libc 2.1.3 this fails with ambiguous result
90*11be35a1SLionel Sambuc	# h_getopt_long "${load}" "foo --col" "!?|0"
91*11be35a1SLionel Sambuc	#
92*11be35a1SLionel Sambuc	# GNU libc 2.2 >= this works
93*11be35a1SLionel Sambuc	h_getopt_long "${load}" "foo --col" "-color|0"
94*11be35a1SLionel Sambuc
95*11be35a1SLionel Sambuc	h_getopt_long "${load}" "foo --colour" "-colour|0"
96*11be35a1SLionel Sambuc
97*11be35a1SLionel Sambuc	# This is the real GNU libc test!
98*11be35a1SLionel Sambuc	args="foo -a -b -cfoobar --required foobar --optional=bazbug --none random --col --color --colour"
99*11be35a1SLionel Sambuc	# GNU libc 2.1.3 this fails with ambiguous
100*11be35a1SLionel Sambuc	#result="a,b,c=foobar,-required=foobar,-optional=bazbug,-none,!?,-color,-colour|1"
101*11be35a1SLionel Sambuc	#
102*11be35a1SLionel Sambuc	# GNU libc 2.2 >= this works
103*11be35a1SLionel Sambuc	result="a,b,c=foobar,-required=foobar,-optional=bazbug,-none,-color,-color,-colour|1"
104*11be35a1SLionel Sambuc	h_getopt_long "${load}" "${args}" "${result}"
105*11be35a1SLionel Sambuc
106*11be35a1SLionel Sambuc	#
107*11be35a1SLionel Sambuc	# The order of long options in the long option array should not matter.
108*11be35a1SLionel Sambuc	# An exact match should never be treated as ambiguous.
109*11be35a1SLionel Sambuc	#
110*11be35a1SLionel Sambuc	load="optstring:	fgl
111*11be35a1SLionel Sambuclongopts:	3
112*11be35a1SLionel Sambuclongopt:	list-foobar, no_argument, lopt, 'f'
113*11be35a1SLionel Sambuclongopt:	list-goobar, no_argument, lopt, 'g'
114*11be35a1SLionel Sambuclongopt:	list, no_argument, lopt, 'l'"
115*11be35a1SLionel Sambuc	h_getopt_long "${load}" "foo --list" "-list|0"
116*11be35a1SLionel Sambuc}
117*11be35a1SLionel Sambuc
118*11be35a1SLionel Sambuc
119*11be35a1SLionel Sambucatf_init_test_cases()
120*11be35a1SLionel Sambuc{
121*11be35a1SLionel Sambuc	atf_add_test_case getopt
122*11be35a1SLionel Sambuc	atf_add_test_case getopt_long
123*11be35a1SLionel Sambuc}
124