xref: /freebsd/contrib/netbsd-tests/include/sys/t_cdefs.c (revision 57718be8fa0bd5edc11ab9a72e68cc71982939a6)
1 /* $NetBSD: t_cdefs.c,v 1.3 2013/09/05 09:01:27 gsutre Exp $ */
2 
3 /*-
4  * Copyright (c) 2012 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
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 
32 #include <sys/cdefs.h>
33 __COPYRIGHT("@(#) Copyright (c) 2008\
34  The NetBSD Foundation, inc. All rights reserved.");
35 __RCSID("$NetBSD: t_cdefs.c,v 1.3 2013/09/05 09:01:27 gsutre Exp $");
36 
37 #include <atf-c.h>
38 #include <sys/types.h>
39 #include <limits.h>
40 #include <stdint.h>
41 
42 static const struct {
43 	const char *name;
44 	intmax_t min;
45 	intmax_t max;
46 } s[] = {
47 	{ "signed char", SCHAR_MIN, SCHAR_MAX },
48 	{ "signed short", SHRT_MIN, SHRT_MAX },
49 	{ "signed int", INT_MIN, INT_MAX },
50 	{ "signed long", LONG_MIN, LONG_MAX },
51 	{ "signed long long", LLONG_MIN, LLONG_MAX },
52 };
53 
54 static const struct {
55 	const char *name;
56 	uintmax_t min;
57 	uintmax_t max;
58 } u[] = {
59 	{ "unsigned char", 0, UCHAR_MAX },
60 	{ "unsigned short", 0, USHRT_MAX },
61 	{ "unsigned int", 0, UINT_MAX },
62 	{ "unsigned long", 0, ULONG_MAX },
63 	{ "unsigned long long", 0, ULLONG_MAX },
64 };
65 
66 ATF_TC(stypeminmax);
67 ATF_TC_HEAD(stypeminmax, tc)
68 {
69 	atf_tc_set_md_var(tc, "descr", "Checks signed type min/max macros");
70 }
71 
72 
73 ATF_TC_BODY(stypeminmax, tc)
74 {
75 #define CHECK(a, b) ATF_REQUIRE(__type_min(a) == s[b].min); \
76     ATF_REQUIRE(__type_max(a) == s[b].max)
77 
78 	CHECK(signed char, 0);
79 	CHECK(signed short, 1);
80 	CHECK(signed int, 2);
81 	CHECK(signed long, 3);
82 	CHECK(signed long long, 4);
83 #undef CHECK
84 }
85 
86 ATF_TC(utypeminmax);
87 ATF_TC_HEAD(utypeminmax, tc)
88 {
89 	atf_tc_set_md_var(tc, "descr", "Checks unsigned type min/max macros");
90 }
91 
92 ATF_TC_BODY(utypeminmax, tc)
93 {
94 #define CHECK(a, b) ATF_REQUIRE(__type_min(a) == u[b].min); \
95     ATF_REQUIRE(__type_max(a) == u[b].max)
96 
97 	CHECK(unsigned char, 0);
98 	CHECK(unsigned short, 1);
99 	CHECK(unsigned int, 2);
100 	CHECK(unsigned long, 3);
101 	CHECK(unsigned long long, 4);
102 #undef CHECK
103 }
104 
105 ATF_TC(sissigned);
106 ATF_TC_HEAD(sissigned, tc)
107 {
108 	atf_tc_set_md_var(tc, "descr", "Checks issigned macro for signed");
109 }
110 
111 ATF_TC_BODY(sissigned, tc)
112 {
113 #define CHECK(a) ATF_REQUIRE(__type_is_signed(a) == 1)
114 
115 	CHECK(signed char);
116 	CHECK(signed short);
117 	CHECK(signed int);
118 	CHECK(signed long);
119 	CHECK(signed long long);
120 #undef CHECK
121 }
122 
123 ATF_TC(uissigned);
124 ATF_TC_HEAD(uissigned, tc)
125 {
126 	atf_tc_set_md_var(tc, "descr", "Checks issigned macro for unsigned");
127 }
128 
129 ATF_TC_BODY(uissigned, tc)
130 {
131 #define CHECK(a) ATF_REQUIRE(__type_is_signed(a) == 0)
132 
133 	CHECK(unsigned char);
134 	CHECK(unsigned short);
135 	CHECK(unsigned int);
136 	CHECK(unsigned long);
137 	CHECK(unsigned long long);
138 #undef CHECK
139 }
140 
141 ATF_TC(utypemask);
142 ATF_TC_HEAD(utypemask, tc)
143 {
144 	atf_tc_set_md_var(tc, "descr", "Checks type mask macro for unsigned");
145 }
146 
147 ATF_TC_BODY(utypemask, tc)
148 {
149 #define CHECK(a, b) ATF_REQUIRE(__type_mask(a) == b)
150 
151 	CHECK(unsigned char,      0xffffffffffffff00ULL);
152 	CHECK(unsigned short,     0xffffffffffff0000ULL);
153 	CHECK(unsigned int,       0xffffffff00000000ULL);
154 	CHECK(unsigned long long, 0x0000000000000000ULL);
155 #undef CHECK
156 }
157 
158 ATF_TC(stypemask);
159 ATF_TC_HEAD(stypemask, tc)
160 {
161 	atf_tc_set_md_var(tc, "descr", "Checks type mask macro for signed");
162 }
163 
164 ATF_TC_BODY(stypemask, tc)
165 {
166 #define CHECK(a, b) ATF_REQUIRE(__type_mask(a) == b)
167 
168 	CHECK(signed char,      0xffffffffffffff00LL);
169 	CHECK(signed short,     0xffffffffffff0000LL);
170 	CHECK(signed int,       0xffffffff00000000LL);
171 	CHECK(signed long long, 0x0000000000000000LL);
172 #undef CHECK
173 }
174 
175 ATF_TC(stypefit);
176 ATF_TC_HEAD(stypefit, tc)
177 {
178 	atf_tc_set_md_var(tc, "descr", "Checks typefit macro for signed");
179 }
180 
181 ATF_TC_BODY(stypefit, tc)
182 {
183 #define CHECK(a, b, c) ATF_REQUIRE(!__type_fit(a, b) == c)
184 
185 	CHECK(signed char, -1, 0);
186 	CHECK(signed char, 1, 0);
187 	CHECK(signed char, 0x7f, 0);
188 	CHECK(signed char, 0x80, 1);
189 	CHECK(signed char, 0xff, 1);
190 	CHECK(signed char, 0x1ff, 1);
191 
192 	CHECK(signed short, -1, 0);
193 	CHECK(signed short, 1, 0);
194 	CHECK(signed short, 0x7fff, 0);
195 	CHECK(signed short, 0x8000, 1);
196 	CHECK(signed short, 0xffff, 1);
197 	CHECK(signed short, 0x1ffff, 1);
198 
199 	CHECK(signed int, -1, 0);
200 	CHECK(signed int, 1, 0);
201 	CHECK(signed int, 0x7fffffff, 0);
202 	CHECK(signed int, 0x80000000, 1);
203 	CHECK(signed int, 0xffffffff, 1);
204 	CHECK(signed int, 0x1ffffffffLL, 1);
205 
206 	CHECK(signed long long, -1, 0);
207 	CHECK(signed long long, 1, 0);
208 	CHECK(signed long long, 0x7fffffffffffffffLL, 0);
209 	CHECK(signed long long, 0x8000000000000000LL, 1);
210 	CHECK(signed long long, 0xffffffffffffffffLL, 1);
211 
212 #undef CHECK
213 }
214 
215 ATF_TC(utypefit);
216 ATF_TC_HEAD(utypefit, tc)
217 {
218 	atf_tc_set_md_var(tc, "descr", "Checks typefit macro for unsigned");
219 }
220 
221 ATF_TC_BODY(utypefit, tc)
222 {
223 #define CHECK(a, b, c) ATF_REQUIRE(!__type_fit(a, b) == c)
224 
225 	CHECK(unsigned char, -1, 1);
226 	CHECK(unsigned char, 1, 0);
227 	CHECK(unsigned char, 0x7f, 0);
228 	CHECK(unsigned char, 0x80, 0);
229 	CHECK(unsigned char, 0xff, 0);
230 	CHECK(unsigned char, 0x1ff, 1);
231 
232 	CHECK(unsigned short, -1, 1);
233 	CHECK(unsigned short, 1, 0);
234 	CHECK(unsigned short, 0x7fff, 0);
235 	CHECK(unsigned short, 0x8000, 0);
236 	CHECK(unsigned short, 0xffff, 0);
237 	CHECK(unsigned short, 0x1ffff, 1);
238 
239 	CHECK(unsigned int, -1, 1);
240 	CHECK(unsigned int, 1, 0);
241 	CHECK(unsigned int, 0x7fffffff, 0);
242 	CHECK(unsigned int, 0x80000000, 0);
243 	CHECK(unsigned int, 0xffffffff, 0);
244 	CHECK(unsigned int, 0x1ffffffffLL, 1);
245 
246 	CHECK(unsigned long long, -1, 1);
247 	CHECK(unsigned long long, 1, 0);
248 	CHECK(unsigned long long, 0x7fffffffffffffffULL, 0);
249 	CHECK(unsigned long long, 0x8000000000000000ULL, 0);
250 	CHECK(unsigned long long, 0xffffffffffffffffULL, 0);
251 
252 #undef CHECK
253 }
254 
255 ATF_TP_ADD_TCS(tp)
256 {
257 	ATF_TP_ADD_TC(tp, stypeminmax);
258 	ATF_TP_ADD_TC(tp, utypeminmax);
259 	ATF_TP_ADD_TC(tp, sissigned);
260 	ATF_TP_ADD_TC(tp, uissigned);
261 	ATF_TP_ADD_TC(tp, stypemask);
262 	ATF_TP_ADD_TC(tp, utypemask);
263 	ATF_TP_ADD_TC(tp, stypefit);
264 	ATF_TP_ADD_TC(tp, utypefit);
265 
266 	return atf_no_error();
267 }
268