xref: /freebsd/contrib/netbsd-tests/lib/libutil/t_snprintb.c (revision 57718be8fa0bd5edc11ab9a72e68cc71982939a6)
1*57718be8SEnji Cooper /* $NetBSD: t_snprintb.c,v 1.4 2014/06/06 06:59:21 shm Exp $ */
2*57718be8SEnji Cooper 
3*57718be8SEnji Cooper /*
4*57718be8SEnji Cooper  * Copyright (c) 2002, 2004, 2008, 2010 The NetBSD Foundation, Inc.
5*57718be8SEnji Cooper  * All rights reserved.
6*57718be8SEnji Cooper  *
7*57718be8SEnji Cooper  * This code was contributed to The NetBSD Foundation by Christos Zoulas.
8*57718be8SEnji Cooper  *
9*57718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
10*57718be8SEnji Cooper  * modification, are permitted provided that the following conditions
11*57718be8SEnji Cooper  * are met:
12*57718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
13*57718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
14*57718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
15*57718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
16*57718be8SEnji Cooper  *    documentation and/or other materials provided with the distribution.
17*57718be8SEnji Cooper  *
18*57718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19*57718be8SEnji Cooper  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20*57718be8SEnji Cooper  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21*57718be8SEnji Cooper  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22*57718be8SEnji Cooper  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*57718be8SEnji Cooper  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*57718be8SEnji Cooper  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*57718be8SEnji Cooper  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*57718be8SEnji Cooper  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*57718be8SEnji Cooper  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*57718be8SEnji Cooper  * POSSIBILITY OF SUCH DAMAGE.
29*57718be8SEnji Cooper  */
30*57718be8SEnji Cooper 
31*57718be8SEnji Cooper #include <sys/cdefs.h>
32*57718be8SEnji Cooper __COPYRIGHT("@(#) Copyright (c) 2008, 2010\
33*57718be8SEnji Cooper  The NetBSD Foundation, inc. All rights reserved.");
34*57718be8SEnji Cooper __RCSID("$NetBSD: t_snprintb.c,v 1.4 2014/06/06 06:59:21 shm Exp $");
35*57718be8SEnji Cooper 
36*57718be8SEnji Cooper #include <string.h>
37*57718be8SEnji Cooper #include <util.h>
38*57718be8SEnji Cooper 
39*57718be8SEnji Cooper #include <atf-c.h>
40*57718be8SEnji Cooper 
41*57718be8SEnji Cooper static void
42*57718be8SEnji Cooper h_snprintb(const char *fmt, uint64_t val, const char *res)
43*57718be8SEnji Cooper {
44*57718be8SEnji Cooper 	char buf[1024];
45*57718be8SEnji Cooper 	int len, slen;
46*57718be8SEnji Cooper 
47*57718be8SEnji Cooper 	len = snprintb(buf, sizeof(buf), fmt, val);
48*57718be8SEnji Cooper 	slen = (int) strlen(res);
49*57718be8SEnji Cooper 
50*57718be8SEnji Cooper 	ATF_REQUIRE_STREQ(res, buf);
51*57718be8SEnji Cooper 	ATF_REQUIRE_EQ(len, slen);
52*57718be8SEnji Cooper }
53*57718be8SEnji Cooper 
54*57718be8SEnji Cooper ATF_TC(snprintb);
55*57718be8SEnji Cooper ATF_TC_HEAD(snprintb, tc)
56*57718be8SEnji Cooper {
57*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Checks snprintb(3)");
58*57718be8SEnji Cooper }
59*57718be8SEnji Cooper ATF_TC_BODY(snprintb, tc)
60*57718be8SEnji Cooper {
61*57718be8SEnji Cooper 	h_snprintb("\10\2BITTWO\1BITONE", 3, "03<BITTWO,BITONE>");
62*57718be8SEnji Cooper 	h_snprintb("\177\20b\0A\0\0", 0, "0x0");
63*57718be8SEnji Cooper 
64*57718be8SEnji Cooper 	h_snprintb("\177\20b\05NOTBOOT\0b\06FPP\0b\013SDVMA\0b\015VIDEO\0"
65*57718be8SEnji Cooper 		"b\020LORES\0b\021FPA\0b\022DIAG\0b\016CACHE\0"
66*57718be8SEnji Cooper 		"b\017IOCACHE\0b\022LOOPBACK\0b\04DBGCACHE\0",
67*57718be8SEnji Cooper 		0xe860, "0xe860<NOTBOOT,FPP,SDVMA,VIDEO,CACHE,IOCACHE>");
68*57718be8SEnji Cooper }
69*57718be8SEnji Cooper 
70*57718be8SEnji Cooper static void
71*57718be8SEnji Cooper h_snprintb_m(const char *fmt, uint64_t val, int line_max, const char *res,
72*57718be8SEnji Cooper 	     int res_len)
73*57718be8SEnji Cooper {
74*57718be8SEnji Cooper 	char buf[1024];
75*57718be8SEnji Cooper 	int len;
76*57718be8SEnji Cooper 
77*57718be8SEnji Cooper 	len = snprintb_m(buf, sizeof(buf), fmt, val, line_max);
78*57718be8SEnji Cooper 
79*57718be8SEnji Cooper 	ATF_REQUIRE_EQ(len, res_len);
80*57718be8SEnji Cooper 	ATF_REQUIRE_EQ(0, memcmp(res, buf, res_len + 1));
81*57718be8SEnji Cooper }
82*57718be8SEnji Cooper 
83*57718be8SEnji Cooper ATF_TC(snprintb_m);
84*57718be8SEnji Cooper ATF_TC_HEAD(snprintb_m, tc)
85*57718be8SEnji Cooper {
86*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Checks snprintb_m(3)");
87*57718be8SEnji Cooper }
88*57718be8SEnji Cooper ATF_TC_BODY(snprintb_m, tc)
89*57718be8SEnji Cooper {
90*57718be8SEnji Cooper 	h_snprintb_m("\177\020b\0LSB\0b\1_BITONE\0f\4\4NIBBLE2\0"
91*57718be8SEnji Cooper 			"f\x10\4BURST\0=\4FOUR\0=\xfSIXTEEN\0"
92*57718be8SEnji Cooper 			"b\x1fMSB\0\0",
93*57718be8SEnji Cooper                      0x800f0701,
94*57718be8SEnji Cooper 		     33,
95*57718be8SEnji Cooper 		     "0x800f0701<LSB,NIBBLE2=0x0>\0"
96*57718be8SEnji Cooper 			"0x800f0701<BURST=0xf=SIXTEEN,MSB>\0\0",
97*57718be8SEnji Cooper 		     62);
98*57718be8SEnji Cooper 
99*57718be8SEnji Cooper 	h_snprintb_m("\177\020b\0LSB\0b\1_BITONE\0f\4\4NIBBLE2\0"
100*57718be8SEnji Cooper 			"f\x10\4BURST\0=\4FOUR\0=\xfSIXTEEN\0"
101*57718be8SEnji Cooper 			"b\x1fMSB\0\0",
102*57718be8SEnji Cooper                      0x800f0701,
103*57718be8SEnji Cooper 		     32,
104*57718be8SEnji Cooper 		     "0x800f0701<LSB,NIBBLE2=0x0>\0"
105*57718be8SEnji Cooper 			"0x800f0701<BURST=0xf=SIXTEEN>\0"
106*57718be8SEnji Cooper 			"0x800f0701<MSB>\0\0",
107*57718be8SEnji Cooper 		     74);
108*57718be8SEnji Cooper }
109*57718be8SEnji Cooper 
110*57718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
111*57718be8SEnji Cooper {
112*57718be8SEnji Cooper 
113*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, snprintb);
114*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, snprintb_m);
115*57718be8SEnji Cooper 
116*57718be8SEnji Cooper 	return atf_no_error();
117*57718be8SEnji Cooper }
118