xref: /minix3/tests/lib/libutil/t_snprintb.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_snprintb.c,v 1.4 2014/06/06 06:59:21 shm Exp $ */
211be35a1SLionel Sambuc 
311be35a1SLionel Sambuc /*
411be35a1SLionel Sambuc  * Copyright (c) 2002, 2004, 2008, 2010 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc  * All rights reserved.
611be35a1SLionel Sambuc  *
711be35a1SLionel Sambuc  * This code was contributed to The NetBSD Foundation by Christos Zoulas.
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 <sys/cdefs.h>
3211be35a1SLionel Sambuc __COPYRIGHT("@(#) Copyright (c) 2008, 2010\
3311be35a1SLionel Sambuc  The NetBSD Foundation, inc. All rights reserved.");
34*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: t_snprintb.c,v 1.4 2014/06/06 06:59:21 shm Exp $");
3511be35a1SLionel Sambuc 
3611be35a1SLionel Sambuc #include <string.h>
3711be35a1SLionel Sambuc #include <util.h>
3811be35a1SLionel Sambuc 
3911be35a1SLionel Sambuc #include <atf-c.h>
4011be35a1SLionel Sambuc 
4111be35a1SLionel Sambuc static void
h_snprintb(const char * fmt,uint64_t val,const char * res)4211be35a1SLionel Sambuc h_snprintb(const char *fmt, uint64_t val, const char *res)
4311be35a1SLionel Sambuc {
4411be35a1SLionel Sambuc 	char buf[1024];
4511be35a1SLionel Sambuc 	int len, slen;
4611be35a1SLionel Sambuc 
4711be35a1SLionel Sambuc 	len = snprintb(buf, sizeof(buf), fmt, val);
4811be35a1SLionel Sambuc 	slen = (int) strlen(res);
4911be35a1SLionel Sambuc 
5011be35a1SLionel Sambuc 	ATF_REQUIRE_STREQ(res, buf);
5111be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(len, slen);
5211be35a1SLionel Sambuc }
5311be35a1SLionel Sambuc 
5411be35a1SLionel Sambuc ATF_TC(snprintb);
ATF_TC_HEAD(snprintb,tc)5511be35a1SLionel Sambuc ATF_TC_HEAD(snprintb, tc)
5611be35a1SLionel Sambuc {
5711be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks snprintb(3)");
5811be35a1SLionel Sambuc }
ATF_TC_BODY(snprintb,tc)5911be35a1SLionel Sambuc ATF_TC_BODY(snprintb, tc)
6011be35a1SLionel Sambuc {
6111be35a1SLionel Sambuc 	h_snprintb("\10\2BITTWO\1BITONE", 3, "03<BITTWO,BITONE>");
62*0a6a1f1dSLionel Sambuc 	h_snprintb("\177\20b\0A\0\0", 0, "0x0");
6311be35a1SLionel Sambuc 
6411be35a1SLionel Sambuc 	h_snprintb("\177\20b\05NOTBOOT\0b\06FPP\0b\013SDVMA\0b\015VIDEO\0"
6511be35a1SLionel Sambuc 		"b\020LORES\0b\021FPA\0b\022DIAG\0b\016CACHE\0"
6611be35a1SLionel Sambuc 		"b\017IOCACHE\0b\022LOOPBACK\0b\04DBGCACHE\0",
6711be35a1SLionel Sambuc 		0xe860, "0xe860<NOTBOOT,FPP,SDVMA,VIDEO,CACHE,IOCACHE>");
6811be35a1SLionel Sambuc }
6911be35a1SLionel Sambuc 
7084d9c625SLionel Sambuc static void
h_snprintb_m(const char * fmt,uint64_t val,int line_max,const char * res,int res_len)7184d9c625SLionel Sambuc h_snprintb_m(const char *fmt, uint64_t val, int line_max, const char *res,
7284d9c625SLionel Sambuc 	     int res_len)
7384d9c625SLionel Sambuc {
7484d9c625SLionel Sambuc 	char buf[1024];
7584d9c625SLionel Sambuc 	int len;
7684d9c625SLionel Sambuc 
7784d9c625SLionel Sambuc 	len = snprintb_m(buf, sizeof(buf), fmt, val, line_max);
7884d9c625SLionel Sambuc 
7984d9c625SLionel Sambuc 	ATF_REQUIRE_EQ(len, res_len);
8084d9c625SLionel Sambuc 	ATF_REQUIRE_EQ(0, memcmp(res, buf, res_len + 1));
8184d9c625SLionel Sambuc }
8284d9c625SLionel Sambuc 
8384d9c625SLionel Sambuc ATF_TC(snprintb_m);
ATF_TC_HEAD(snprintb_m,tc)8484d9c625SLionel Sambuc ATF_TC_HEAD(snprintb_m, tc)
8584d9c625SLionel Sambuc {
8684d9c625SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks snprintb_m(3)");
8784d9c625SLionel Sambuc }
ATF_TC_BODY(snprintb_m,tc)8884d9c625SLionel Sambuc ATF_TC_BODY(snprintb_m, tc)
8984d9c625SLionel Sambuc {
9084d9c625SLionel Sambuc 	h_snprintb_m("\177\020b\0LSB\0b\1_BITONE\0f\4\4NIBBLE2\0"
9184d9c625SLionel Sambuc 			"f\x10\4BURST\0=\4FOUR\0=\xfSIXTEEN\0"
9284d9c625SLionel Sambuc 			"b\x1fMSB\0\0",
9384d9c625SLionel Sambuc                      0x800f0701,
9484d9c625SLionel Sambuc 		     33,
9584d9c625SLionel Sambuc 		     "0x800f0701<LSB,NIBBLE2=0x0>\0"
9684d9c625SLionel Sambuc 			"0x800f0701<BURST=0xf=SIXTEEN,MSB>\0\0",
9784d9c625SLionel Sambuc 		     62);
9884d9c625SLionel Sambuc 
9984d9c625SLionel Sambuc 	h_snprintb_m("\177\020b\0LSB\0b\1_BITONE\0f\4\4NIBBLE2\0"
10084d9c625SLionel Sambuc 			"f\x10\4BURST\0=\4FOUR\0=\xfSIXTEEN\0"
10184d9c625SLionel Sambuc 			"b\x1fMSB\0\0",
10284d9c625SLionel Sambuc                      0x800f0701,
10384d9c625SLionel Sambuc 		     32,
10484d9c625SLionel Sambuc 		     "0x800f0701<LSB,NIBBLE2=0x0>\0"
10584d9c625SLionel Sambuc 			"0x800f0701<BURST=0xf=SIXTEEN>\0"
10684d9c625SLionel Sambuc 			"0x800f0701<MSB>\0\0",
10784d9c625SLionel Sambuc 		     74);
10884d9c625SLionel Sambuc }
10984d9c625SLionel Sambuc 
ATF_TP_ADD_TCS(tp)11011be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
11111be35a1SLionel Sambuc {
11211be35a1SLionel Sambuc 
11311be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, snprintb);
11484d9c625SLionel Sambuc 	ATF_TP_ADD_TC(tp, snprintb_m);
11511be35a1SLionel Sambuc 
11611be35a1SLionel Sambuc 	return atf_no_error();
11711be35a1SLionel Sambuc }
118