xref: /netbsd-src/tests/lib/libc/stdlib/t_a64l.c (revision 55abcd082fb95790d4d0f731f30a61a3b8a70dee)
1 /* $NetBSD: t_a64l.c,v 1.1 2020/07/01 07:16:37 jruoho Exp $ */
2 
3 /*-
4  * Copyright (c) 2020 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jukka Ruohonen.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_a64l.c,v 1.1 2020/07/01 07:16:37 jruoho Exp $");
33 
34 #include <atf-c.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 
38 struct test {
39 	const char	*str;
40 	size_t		 len;
41 	long		 val;
42 } t[] = {
43 	{ "",	     0, 0L	  },
44 	{ "///",     3, 4161L	  }, /* 1*64^0 + 1*64^1 + 1*64^2	    */
45 	{ "123",     3, 20739L	  }, /* 3*64^0 + 4*64^1 + 5*64^2	    */
46 	{ "zzz",     3, 262143L	  }, /* 63*64^0 + 63*64^1 + 63*64^2	    */
47 	{ "a64l",    4, 12870182L }, /* 38*64^0 + 8*64^1 + 6*64^2 + 49*64^3 */
48 	{ "a64l...", 4, 12870182L }, /* 38*64^0 + 8*64^1 + 6*64^2 + 49*64^3 */
49 };
50 
51 #define MAXLEN 8
52 
53 ATF_TC(a64l_basic);
ATF_TC_HEAD(a64l_basic,tc)54 ATF_TC_HEAD(a64l_basic, tc)
55 {
56 	atf_tc_set_md_var(tc, "descr", "A basic test of a64l(3)");
57 }
58 
ATF_TC_BODY(a64l_basic,tc)59 ATF_TC_BODY(a64l_basic, tc)
60 {
61 	for (size_t i = 0; i < __arraycount(t); i++) {
62 		long val = a64l(t[i].str);
63 		ATF_REQUIRE(val == t[i].val);
64 	}
65 
66 }
67 
68 ATF_TC(l64a_basic);
ATF_TC_HEAD(l64a_basic,tc)69 ATF_TC_HEAD(l64a_basic, tc)
70 {
71 	atf_tc_set_md_var(tc, "descr", "A basic test of l64a(3)");
72 }
73 
ATF_TC_BODY(l64a_basic,tc)74 ATF_TC_BODY(l64a_basic, tc)
75 {
76 	for (size_t i = 0; i < __arraycount(t); i++) {
77 		char *str = l64a(t[i].val);
78 		ATF_REQUIRE(strlen(str) == t[i].len);
79 		ATF_REQUIRE(strncmp(str, t[i].str, t[i].len) == 0);
80 	}
81 }
82 
83 ATF_TC(l64a_r_basic);
ATF_TC_HEAD(l64a_r_basic,tc)84 ATF_TC_HEAD(l64a_r_basic, tc)
85 {
86 	atf_tc_set_md_var(tc, "descr", "A basic test of l64a_r(3)");
87 }
88 
ATF_TC_BODY(l64a_r_basic,tc)89 ATF_TC_BODY(l64a_r_basic, tc)
90 {
91 	char buf[MAXLEN];
92 
93 	for (size_t i = 0; i < __arraycount(t); i++) {
94 
95 		(void)memset(buf, '\0', sizeof(buf));
96 
97 		ATF_REQUIRE(l64a_r(t[i].val, buf, t[i].len + 1) == 0);
98 		ATF_REQUIRE(strlen(buf) == t[i].len);
99 		ATF_REQUIRE(strncmp(buf, t[i].str, t[i].len) == 0);
100 	}
101 }
102 
ATF_TP_ADD_TCS(tp)103 ATF_TP_ADD_TCS(tp)
104 {
105 
106 	ATF_TP_ADD_TC(tp, a64l_basic);
107 	ATF_TP_ADD_TC(tp, l64a_basic);
108 	ATF_TP_ADD_TC(tp, l64a_r_basic);
109 
110 	return atf_no_error();
111 }
112