xref: /minix3/tests/lib/libc/gen/t_fmtcheck.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: t_fmtcheck.c,v 1.3 2014/06/14 08:19:02 apb Exp $	*/
211be35a1SLionel Sambuc 
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc  * Copyright (c) 2000 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc  * All rights reserved.
611be35a1SLionel Sambuc  *
711be35a1SLionel Sambuc  * This code was contributed to The NetBSD Foundation by Allen Briggs.
811be35a1SLionel Sambuc  *
911be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
1011be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
1111be35a1SLionel Sambuc  * are met:
1211be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
1311be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
1411be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
1511be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
1611be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
1711be35a1SLionel Sambuc  *
1811be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1911be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2011be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2111be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2211be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2311be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2411be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2511be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2611be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2711be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2811be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
2911be35a1SLionel Sambuc  */
3011be35a1SLionel Sambuc 
3111be35a1SLionel Sambuc #include <atf-c.h>
3211be35a1SLionel Sambuc 
3311be35a1SLionel Sambuc #include <stdio.h>
3411be35a1SLionel Sambuc #include <stdlib.h>
3511be35a1SLionel Sambuc 
3611be35a1SLionel Sambuc const char *fmtcheck(const char *f1, const char *f2)
3711be35a1SLionel Sambuc                 __attribute__((__format_arg__(2)));
3811be35a1SLionel Sambuc 
3911be35a1SLionel Sambuc #include <err.h>
4011be35a1SLionel Sambuc 
4111be35a1SLionel Sambuc struct test_fmt {
4211be35a1SLionel Sambuc 	const char	*fmt1;
4311be35a1SLionel Sambuc 	const char	*fmt2;
4411be35a1SLionel Sambuc 	int		correct;
4511be35a1SLionel Sambuc } test_fmts[] = {
4611be35a1SLionel Sambuc 	{ "%d", "%d", 1 },
4711be35a1SLionel Sambuc 	{ "%2d", "%2.2d", 1 },
4811be35a1SLionel Sambuc 	{ "%x", "%d", 1 },
4911be35a1SLionel Sambuc 	{ "%u", "%d", 1 },
5011be35a1SLionel Sambuc 	{ "%03d", "%d", 1 },
5111be35a1SLionel Sambuc 	{ "%-2d", "%d", 1 },
5211be35a1SLionel Sambuc 	{ "%d", "%-12.1d", 1 },
5311be35a1SLionel Sambuc 	{ "%d", "%-01.3d", 1 },
5411be35a1SLionel Sambuc 	{ "%X", "%-01.3d", 1 },
5511be35a1SLionel Sambuc 	{ "%D", "%ld", 1 },
5611be35a1SLionel Sambuc 	{ "%s", "%s", 1 },
5711be35a1SLionel Sambuc 	{ "%s", "This is a %s test", 1 },
5811be35a1SLionel Sambuc 	{ "Hi, there.  This is a %s test", "%s", 1 },
5911be35a1SLionel Sambuc 	{ "%d", "%s", 2 },
6011be35a1SLionel Sambuc 	{ "%e", "%s", 2 },
6111be35a1SLionel Sambuc 	{ "%r", "%d", 2 },
6211be35a1SLionel Sambuc 	{ "%*.2d", "%*d", 1 },
6311be35a1SLionel Sambuc 	{ "%2.*d", "%*d", 2 },
6411be35a1SLionel Sambuc 	{ "%*d", "%*d", 1 },
6511be35a1SLionel Sambuc 	{ "%-3", "%d", 2 },
6611be35a1SLionel Sambuc 	{ "%d %s", "%d", 2 },
6711be35a1SLionel Sambuc 	{ "%*.*.*d", "%*.*.*d", 2 },
6811be35a1SLionel Sambuc 	{ "%d", "%d %s", 1 },
6911be35a1SLionel Sambuc 	{ "%40s", "%20s", 1 },
7011be35a1SLionel Sambuc 	{ "%x %x %x", "%o %u %d", 1 },
7111be35a1SLionel Sambuc 	{ "%o %u %d", "%x %x %X", 1 },
7211be35a1SLionel Sambuc 	{ "%#o %u %#-d", "%x %#x %X", 1 },
7311be35a1SLionel Sambuc 	{ "%qd", "%llx", 1 },
7411be35a1SLionel Sambuc 	{ "%%", "%llx", 1 },
75*0a6a1f1dSLionel Sambuc 	{ "%ld %30s %#llx %-10.*e", "This number %lu%% and string %s has %qd numbers and %.*g floats", 1 },
76*0a6a1f1dSLionel Sambuc 	{ "%o", "%lx", 2 },
77*0a6a1f1dSLionel Sambuc 	{ "%p", "%lu", 2 },
7811be35a1SLionel Sambuc };
7911be35a1SLionel Sambuc 
8011be35a1SLionel Sambuc ATF_TC(fmtcheck_basic);
ATF_TC_HEAD(fmtcheck_basic,tc)8111be35a1SLionel Sambuc ATF_TC_HEAD(fmtcheck_basic, tc)
8211be35a1SLionel Sambuc {
8311be35a1SLionel Sambuc 
8411be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test fmtcheck(3)");
8511be35a1SLionel Sambuc }
8611be35a1SLionel Sambuc 
ATF_TC_BODY(fmtcheck_basic,tc)8711be35a1SLionel Sambuc ATF_TC_BODY(fmtcheck_basic, tc)
8811be35a1SLionel Sambuc {
8911be35a1SLionel Sambuc 	unsigned int	i, r;
9011be35a1SLionel Sambuc 	const char	*f, *cf, *f1, *f2;
9111be35a1SLionel Sambuc 
9211be35a1SLionel Sambuc 	r = 0;
9311be35a1SLionel Sambuc 	for (i = 0 ; i < __arraycount(test_fmts); i++) {
9411be35a1SLionel Sambuc 		f1 = test_fmts[i].fmt1;
9511be35a1SLionel Sambuc 		f2 = test_fmts[i].fmt2;
9611be35a1SLionel Sambuc 		f = fmtcheck(f1, f2);
9711be35a1SLionel Sambuc 		if (test_fmts[i].correct == 1) {
9811be35a1SLionel Sambuc 			cf = f1;
9911be35a1SLionel Sambuc 		} else {
10011be35a1SLionel Sambuc 			cf = f2;
10111be35a1SLionel Sambuc 		}
10211be35a1SLionel Sambuc 		if (f != cf) {
10311be35a1SLionel Sambuc 			r++;
10411be35a1SLionel Sambuc 			atf_tc_fail_nonfatal("Test %d: (%s) vs. (%s) failed "
10511be35a1SLionel Sambuc 			    "(should have returned %s)", i, f1, f2,
10611be35a1SLionel Sambuc 			    (test_fmts[i].correct == 1) ? "1st" : "2nd");
10711be35a1SLionel Sambuc 		}
10811be35a1SLionel Sambuc 	}
10911be35a1SLionel Sambuc }
11011be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)11111be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
11211be35a1SLionel Sambuc {
11311be35a1SLionel Sambuc 
11411be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, fmtcheck_basic);
11511be35a1SLionel Sambuc 
11611be35a1SLionel Sambuc 	return atf_no_error();
11711be35a1SLionel Sambuc }
118