t_cdefs.c revision 1.1 1 1.1 christos /* $NetBSD: t_cdefs.c,v 1.1 2012/03/18 15:31:00 christos Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.1 christos * by Christos Zoulas.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos *
19 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.1 christos */
31 1.1 christos
32 1.1 christos #include <sys/cdefs.h>
33 1.1 christos __COPYRIGHT("@(#) Copyright (c) 2008\
34 1.1 christos The NetBSD Foundation, inc. All rights reserved.");
35 1.1 christos __RCSID("$NetBSD: t_cdefs.c,v 1.1 2012/03/18 15:31:00 christos Exp $");
36 1.1 christos
37 1.1 christos #include <atf-c.h>
38 1.1 christos #include <sys/types.h>
39 1.1 christos #include <limits.h>
40 1.1 christos #include <stdint.h>
41 1.1 christos
42 1.1 christos static const struct {
43 1.1 christos const char *name;
44 1.1 christos intmax_t min;
45 1.1 christos intmax_t max;
46 1.1 christos } s[] = {
47 1.1 christos { "signed char", CHAR_MIN, CHAR_MAX },
48 1.1 christos { "signed short", SHRT_MIN, SHRT_MAX },
49 1.1 christos { "signed int", INT_MIN, INT_MAX },
50 1.1 christos { "signed long", LONG_MIN, LONG_MAX },
51 1.1 christos { "signed long long", LLONG_MIN, LLONG_MAX },
52 1.1 christos };
53 1.1 christos
54 1.1 christos static const struct {
55 1.1 christos const char *name;
56 1.1 christos uintmax_t min;
57 1.1 christos uintmax_t max;
58 1.1 christos } u[] = {
59 1.1 christos { "unsigned char", 0, UCHAR_MAX },
60 1.1 christos { "unsigned short", 0, USHRT_MAX },
61 1.1 christos { "unsigned int", 0, UINT_MAX },
62 1.1 christos { "unsigned long", 0, ULONG_MAX },
63 1.1 christos { "unsigned long long", 0, ULLONG_MAX },
64 1.1 christos };
65 1.1 christos
66 1.1 christos ATF_TC(stypeminmax);
67 1.1 christos ATF_TC_HEAD(stypeminmax, tc)
68 1.1 christos {
69 1.1 christos atf_tc_set_md_var(tc, "descr", "Checks signed type min/max macros");
70 1.1 christos }
71 1.1 christos
72 1.1 christos
73 1.1 christos ATF_TC_BODY(stypeminmax, tc)
74 1.1 christos {
75 1.1 christos #define CHECK(a, b) ATF_REQUIRE(__type_min(a) == s[b].min); \
76 1.1 christos ATF_REQUIRE(__type_max(a) == s[b].max)
77 1.1 christos
78 1.1 christos CHECK(signed char, 0);
79 1.1 christos CHECK(signed short, 1);
80 1.1 christos CHECK(signed int, 2);
81 1.1 christos CHECK(signed long, 3);
82 1.1 christos CHECK(signed long long, 4);
83 1.1 christos #undef CHECK
84 1.1 christos }
85 1.1 christos
86 1.1 christos ATF_TC(utypeminmax);
87 1.1 christos ATF_TC_HEAD(utypeminmax, tc)
88 1.1 christos {
89 1.1 christos atf_tc_set_md_var(tc, "descr", "Checks unsigned type min/max macros");
90 1.1 christos }
91 1.1 christos
92 1.1 christos ATF_TC_BODY(utypeminmax, tc)
93 1.1 christos {
94 1.1 christos #define CHECK(a, b) ATF_REQUIRE(__type_min(a) == u[b].min); \
95 1.1 christos ATF_REQUIRE(__type_max(a) == u[b].max)
96 1.1 christos
97 1.1 christos CHECK(unsigned char, 0);
98 1.1 christos CHECK(unsigned short, 1);
99 1.1 christos CHECK(unsigned int, 2);
100 1.1 christos CHECK(unsigned long, 3);
101 1.1 christos CHECK(unsigned long long, 4);
102 1.1 christos #undef CHECK
103 1.1 christos }
104 1.1 christos
105 1.1 christos ATF_TC(sissigned);
106 1.1 christos ATF_TC_HEAD(sissigned, tc)
107 1.1 christos {
108 1.1 christos atf_tc_set_md_var(tc, "descr", "Checks issigned macro for signed");
109 1.1 christos }
110 1.1 christos
111 1.1 christos ATF_TC_BODY(sissigned, tc)
112 1.1 christos {
113 1.1 christos #define CHECK(a) ATF_REQUIRE(__type_is_signed(a) == 1)
114 1.1 christos
115 1.1 christos CHECK(signed char);
116 1.1 christos CHECK(signed short);
117 1.1 christos CHECK(signed int);
118 1.1 christos CHECK(signed long);
119 1.1 christos CHECK(signed long long);
120 1.1 christos #undef CHECK
121 1.1 christos }
122 1.1 christos
123 1.1 christos ATF_TC(uissigned);
124 1.1 christos ATF_TC_HEAD(uissigned, tc)
125 1.1 christos {
126 1.1 christos atf_tc_set_md_var(tc, "descr", "Checks issigned macro for unsigned");
127 1.1 christos }
128 1.1 christos
129 1.1 christos ATF_TC_BODY(uissigned, tc)
130 1.1 christos {
131 1.1 christos #define CHECK(a) ATF_REQUIRE(__type_is_signed(a) == 0)
132 1.1 christos
133 1.1 christos CHECK(unsigned char);
134 1.1 christos CHECK(unsigned short);
135 1.1 christos CHECK(unsigned int);
136 1.1 christos CHECK(unsigned long);
137 1.1 christos CHECK(unsigned long long);
138 1.1 christos #undef CHECK
139 1.1 christos }
140 1.1 christos
141 1.1 christos ATF_TC(utypemask);
142 1.1 christos ATF_TC_HEAD(utypemask, tc)
143 1.1 christos {
144 1.1 christos atf_tc_set_md_var(tc, "descr", "Checks type mask macro for unsigned");
145 1.1 christos }
146 1.1 christos
147 1.1 christos ATF_TC_BODY(utypemask, tc)
148 1.1 christos {
149 1.1 christos #define CHECK(a, b) ATF_REQUIRE(__type_mask(a) == b)
150 1.1 christos
151 1.1 christos CHECK(unsigned char, 0xffffffffffffff00ULL);
152 1.1 christos CHECK(unsigned short, 0xffffffffffff0000ULL);
153 1.1 christos CHECK(unsigned int, 0xffffffff00000000ULL);
154 1.1 christos CHECK(unsigned long long, 0x0000000000000000ULL);
155 1.1 christos #undef CHECK
156 1.1 christos }
157 1.1 christos
158 1.1 christos ATF_TC(stypemask);
159 1.1 christos ATF_TC_HEAD(stypemask, tc)
160 1.1 christos {
161 1.1 christos atf_tc_set_md_var(tc, "descr", "Checks type mask macro for signed");
162 1.1 christos }
163 1.1 christos
164 1.1 christos ATF_TC_BODY(stypemask, tc)
165 1.1 christos {
166 1.1 christos #define CHECK(a, b) ATF_REQUIRE(__type_mask(a) == b)
167 1.1 christos
168 1.1 christos CHECK(signed char, 0xffffffffffffff00LL);
169 1.1 christos CHECK(signed short, 0xffffffffffff0000LL);
170 1.1 christos CHECK(signed int, 0xffffffff00000000LL);
171 1.1 christos CHECK(signed long long, 0x0000000000000000LL);
172 1.1 christos #undef CHECK
173 1.1 christos }
174 1.1 christos
175 1.1 christos ATF_TC(stypefit);
176 1.1 christos ATF_TC_HEAD(stypefit, tc)
177 1.1 christos {
178 1.1 christos atf_tc_set_md_var(tc, "descr", "Checks typefit macro for signed");
179 1.1 christos }
180 1.1 christos
181 1.1 christos ATF_TC_BODY(stypefit, tc)
182 1.1 christos {
183 1.1 christos #define CHECK(a, b, c) ATF_REQUIRE(!__type_fit(a, b) == c)
184 1.1 christos
185 1.1 christos CHECK(signed char, -1, 0);
186 1.1 christos CHECK(signed char, 1, 0);
187 1.1 christos CHECK(signed char, 0x7f, 0);
188 1.1 christos CHECK(signed char, 0x80, 1);
189 1.1 christos CHECK(signed char, 0xff, 1);
190 1.1 christos CHECK(signed char, 0x1ff, 1);
191 1.1 christos
192 1.1 christos CHECK(signed short, -1, 0);
193 1.1 christos CHECK(signed short, 1, 0);
194 1.1 christos CHECK(signed short, 0x7fff, 0);
195 1.1 christos CHECK(signed short, 0x8000, 1);
196 1.1 christos CHECK(signed short, 0xffff, 1);
197 1.1 christos CHECK(signed short, 0x1ffff, 1);
198 1.1 christos
199 1.1 christos CHECK(signed int, -1, 0);
200 1.1 christos CHECK(signed int, 1, 0);
201 1.1 christos CHECK(signed int, 0x7fffffff, 0);
202 1.1 christos CHECK(signed int, 0x80000000, 1);
203 1.1 christos CHECK(signed int, 0xffffffff, 1);
204 1.1 christos CHECK(signed int, 0x1ffffffffLL, 1);
205 1.1 christos
206 1.1 christos CHECK(signed long long, -1, 0);
207 1.1 christos CHECK(signed long long, 1, 0);
208 1.1 christos CHECK(signed long long, 0x7fffffffffffffffLL, 0);
209 1.1 christos CHECK(signed long long, 0x8000000000000000LL, 0);
210 1.1 christos CHECK(signed long long, 0xffffffffffffffffLL, 0);
211 1.1 christos
212 1.1 christos #undef CHECK
213 1.1 christos }
214 1.1 christos
215 1.1 christos ATF_TC(utypefit);
216 1.1 christos ATF_TC_HEAD(utypefit, tc)
217 1.1 christos {
218 1.1 christos atf_tc_set_md_var(tc, "descr", "Checks typefit macro for unsigned");
219 1.1 christos }
220 1.1 christos
221 1.1 christos ATF_TC_BODY(utypefit, tc)
222 1.1 christos {
223 1.1 christos #define CHECK(a, b, c) ATF_REQUIRE(!__type_fit(a, b) == c)
224 1.1 christos
225 1.1 christos CHECK(unsigned char, -1, 1);
226 1.1 christos CHECK(unsigned char, 1, 0);
227 1.1 christos CHECK(unsigned char, 0x7f, 0);
228 1.1 christos CHECK(unsigned char, 0x80, 0);
229 1.1 christos CHECK(unsigned char, 0xff, 0);
230 1.1 christos CHECK(unsigned char, 0x1ff, 1);
231 1.1 christos
232 1.1 christos CHECK(unsigned short, -1, 1);
233 1.1 christos CHECK(unsigned short, 1, 0);
234 1.1 christos CHECK(unsigned short, 0x7fff, 0);
235 1.1 christos CHECK(unsigned short, 0x8000, 0);
236 1.1 christos CHECK(unsigned short, 0xffff, 0);
237 1.1 christos CHECK(unsigned short, 0x1ffff, 1);
238 1.1 christos
239 1.1 christos CHECK(unsigned int, -1, 1);
240 1.1 christos CHECK(unsigned int, 1, 0);
241 1.1 christos CHECK(unsigned int, 0x7fffffff, 0);
242 1.1 christos CHECK(unsigned int, 0x80000000, 0);
243 1.1 christos CHECK(unsigned int, 0xffffffff, 0);
244 1.1 christos CHECK(unsigned int, 0x1ffffffffLL, 1);
245 1.1 christos
246 1.1 christos CHECK(unsigned long long, -1, 1);
247 1.1 christos CHECK(unsigned long long, 1, 0);
248 1.1 christos CHECK(unsigned long long, 0x7fffffffffffffffULL, 0);
249 1.1 christos CHECK(unsigned long long, 0x8000000000000000ULL, 0);
250 1.1 christos CHECK(unsigned long long, 0xffffffffffffffffULL, 0);
251 1.1 christos
252 1.1 christos #undef CHECK
253 1.1 christos }
254 1.1 christos
255 1.1 christos ATF_TP_ADD_TCS(tp)
256 1.1 christos {
257 1.1 christos ATF_TP_ADD_TC(tp, stypeminmax);
258 1.1 christos ATF_TP_ADD_TC(tp, utypeminmax);
259 1.1 christos ATF_TP_ADD_TC(tp, sissigned);
260 1.1 christos ATF_TP_ADD_TC(tp, uissigned);
261 1.1 christos ATF_TP_ADD_TC(tp, stypemask);
262 1.1 christos ATF_TP_ADD_TC(tp, utypemask);
263 1.1 christos ATF_TP_ADD_TC(tp, stypefit);
264 1.1 christos ATF_TP_ADD_TC(tp, utypefit);
265 1.1 christos
266 1.1 christos return atf_no_error();
267 1.1 christos }
268