xref: /minix/tests/lib/libc/gen/t_fmtcheck.c (revision ebfedea0)
1 /*	$NetBSD: t_fmtcheck.c,v 1.2 2011/07/07 09:49:59 jruoho Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code was contributed to The NetBSD Foundation by Allen Briggs.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <atf-c.h>
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 
36 const char *fmtcheck(const char *f1, const char *f2)
37                 __attribute__((__format_arg__(2)));
38 
39 #include <err.h>
40 
41 struct test_fmt {
42 	const char	*fmt1;
43 	const char	*fmt2;
44 	int		correct;
45 } test_fmts[] = {
46 	{ "%d", "%d", 1 },
47 	{ "%2d", "%2.2d", 1 },
48 	{ "%x", "%d", 1 },
49 	{ "%u", "%d", 1 },
50 	{ "%03d", "%d", 1 },
51 	{ "%-2d", "%d", 1 },
52 	{ "%d", "%-12.1d", 1 },
53 	{ "%d", "%-01.3d", 1 },
54 	{ "%X", "%-01.3d", 1 },
55 	{ "%D", "%ld", 1 },
56 	{ "%s", "%s", 1 },
57 	{ "%s", "This is a %s test", 1 },
58 	{ "Hi, there.  This is a %s test", "%s", 1 },
59 	{ "%d", "%s", 2 },
60 	{ "%e", "%s", 2 },
61 	{ "%r", "%d", 2 },
62 	{ "%*.2d", "%*d", 1 },
63 	{ "%2.*d", "%*d", 2 },
64 	{ "%*d", "%*d", 1 },
65 	{ "%-3", "%d", 2 },
66 	{ "%d %s", "%d", 2 },
67 	{ "%*.*.*d", "%*.*.*d", 2 },
68 	{ "%d", "%d %s", 1 },
69 	{ "%40s", "%20s", 1 },
70 	{ "%x %x %x", "%o %u %d", 1 },
71 	{ "%o %u %d", "%x %x %X", 1 },
72 	{ "%#o %u %#-d", "%x %#x %X", 1 },
73 	{ "%qd", "%llx", 1 },
74 	{ "%%", "%llx", 1 },
75 	{ "%p %30s %#llx %-10.*e", "This number %lu%% and string %s has %qd numbers and %.*g floats", 1 },
76 };
77 
78 ATF_TC(fmtcheck_basic);
79 ATF_TC_HEAD(fmtcheck_basic, tc)
80 {
81 
82 	atf_tc_set_md_var(tc, "descr", "Test fmtcheck(3)");
83 }
84 
85 ATF_TC_BODY(fmtcheck_basic, tc)
86 {
87 	unsigned int	i, r;
88 	const char	*f, *cf, *f1, *f2;
89 
90 	r = 0;
91 	for (i = 0 ; i < __arraycount(test_fmts); i++) {
92 		f1 = test_fmts[i].fmt1;
93 		f2 = test_fmts[i].fmt2;
94 		f = fmtcheck(f1, f2);
95 		if (test_fmts[i].correct == 1) {
96 			cf = f1;
97 		} else {
98 			cf = f2;
99 		}
100 		if (f != cf) {
101 			r++;
102 			atf_tc_fail_nonfatal("Test %d: (%s) vs. (%s) failed "
103 			    "(should have returned %s)", i, f1, f2,
104 			    (test_fmts[i].correct == 1) ? "1st" : "2nd");
105 		}
106 	}
107 }
108 
109 ATF_TP_ADD_TCS(tp)
110 {
111 
112 	ATF_TP_ADD_TC(tp, fmtcheck_basic);
113 
114 	return atf_no_error();
115 }
116