1*3890e545Smrg /* $NetBSD: t_cdefs.c,v 1.4 2016/03/16 07:21:36 mrg Exp $ */
227215b2bSchristos
327215b2bSchristos /*-
427215b2bSchristos * Copyright (c) 2012 The NetBSD Foundation, Inc.
527215b2bSchristos * All rights reserved.
627215b2bSchristos *
727215b2bSchristos * This code is derived from software contributed to The NetBSD Foundation
827215b2bSchristos * by Christos Zoulas.
927215b2bSchristos *
1027215b2bSchristos * Redistribution and use in source and binary forms, with or without
1127215b2bSchristos * modification, are permitted provided that the following conditions
1227215b2bSchristos * are met:
1327215b2bSchristos * 1. Redistributions of source code must retain the above copyright
1427215b2bSchristos * notice, this list of conditions and the following disclaimer.
1527215b2bSchristos * 2. Redistributions in binary form must reproduce the above copyright
1627215b2bSchristos * notice, this list of conditions and the following disclaimer in the
1727215b2bSchristos * documentation and/or other materials provided with the distribution.
1827215b2bSchristos *
1927215b2bSchristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2027215b2bSchristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2127215b2bSchristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2227215b2bSchristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2327215b2bSchristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2427215b2bSchristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2527215b2bSchristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2627215b2bSchristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2727215b2bSchristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2827215b2bSchristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2927215b2bSchristos * POSSIBILITY OF SUCH DAMAGE.
3027215b2bSchristos */
3127215b2bSchristos
3227215b2bSchristos #include <sys/cdefs.h>
3327215b2bSchristos __COPYRIGHT("@(#) Copyright (c) 2008\
3427215b2bSchristos The NetBSD Foundation, inc. All rights reserved.");
35*3890e545Smrg __RCSID("$NetBSD: t_cdefs.c,v 1.4 2016/03/16 07:21:36 mrg Exp $");
3627215b2bSchristos
3727215b2bSchristos #include <atf-c.h>
3827215b2bSchristos #include <sys/types.h>
3927215b2bSchristos #include <limits.h>
4027215b2bSchristos #include <stdint.h>
4127215b2bSchristos
4227215b2bSchristos static const struct {
4327215b2bSchristos const char *name;
4427215b2bSchristos intmax_t min;
4527215b2bSchristos intmax_t max;
4627215b2bSchristos } s[] = {
47c76c9d1fSmatt { "signed char", SCHAR_MIN, SCHAR_MAX },
4827215b2bSchristos { "signed short", SHRT_MIN, SHRT_MAX },
4927215b2bSchristos { "signed int", INT_MIN, INT_MAX },
5027215b2bSchristos { "signed long", LONG_MIN, LONG_MAX },
5127215b2bSchristos { "signed long long", LLONG_MIN, LLONG_MAX },
5227215b2bSchristos };
5327215b2bSchristos
5427215b2bSchristos static const struct {
5527215b2bSchristos const char *name;
5627215b2bSchristos uintmax_t min;
5727215b2bSchristos uintmax_t max;
5827215b2bSchristos } u[] = {
5927215b2bSchristos { "unsigned char", 0, UCHAR_MAX },
6027215b2bSchristos { "unsigned short", 0, USHRT_MAX },
6127215b2bSchristos { "unsigned int", 0, UINT_MAX },
6227215b2bSchristos { "unsigned long", 0, ULONG_MAX },
6327215b2bSchristos { "unsigned long long", 0, ULLONG_MAX },
6427215b2bSchristos };
6527215b2bSchristos
6627215b2bSchristos ATF_TC(stypeminmax);
ATF_TC_HEAD(stypeminmax,tc)6727215b2bSchristos ATF_TC_HEAD(stypeminmax, tc)
6827215b2bSchristos {
6927215b2bSchristos atf_tc_set_md_var(tc, "descr", "Checks signed type min/max macros");
7027215b2bSchristos }
7127215b2bSchristos
7227215b2bSchristos
ATF_TC_BODY(stypeminmax,tc)7327215b2bSchristos ATF_TC_BODY(stypeminmax, tc)
7427215b2bSchristos {
7527215b2bSchristos #define CHECK(a, b) ATF_REQUIRE(__type_min(a) == s[b].min); \
7627215b2bSchristos ATF_REQUIRE(__type_max(a) == s[b].max)
7727215b2bSchristos
7827215b2bSchristos CHECK(signed char, 0);
7927215b2bSchristos CHECK(signed short, 1);
8027215b2bSchristos CHECK(signed int, 2);
8127215b2bSchristos CHECK(signed long, 3);
8227215b2bSchristos CHECK(signed long long, 4);
8327215b2bSchristos #undef CHECK
8427215b2bSchristos }
8527215b2bSchristos
8627215b2bSchristos ATF_TC(utypeminmax);
ATF_TC_HEAD(utypeminmax,tc)8727215b2bSchristos ATF_TC_HEAD(utypeminmax, tc)
8827215b2bSchristos {
8927215b2bSchristos atf_tc_set_md_var(tc, "descr", "Checks unsigned type min/max macros");
9027215b2bSchristos }
9127215b2bSchristos
ATF_TC_BODY(utypeminmax,tc)9227215b2bSchristos ATF_TC_BODY(utypeminmax, tc)
9327215b2bSchristos {
9427215b2bSchristos #define CHECK(a, b) ATF_REQUIRE(__type_min(a) == u[b].min); \
9527215b2bSchristos ATF_REQUIRE(__type_max(a) == u[b].max)
9627215b2bSchristos
9727215b2bSchristos CHECK(unsigned char, 0);
9827215b2bSchristos CHECK(unsigned short, 1);
9927215b2bSchristos CHECK(unsigned int, 2);
10027215b2bSchristos CHECK(unsigned long, 3);
10127215b2bSchristos CHECK(unsigned long long, 4);
10227215b2bSchristos #undef CHECK
10327215b2bSchristos }
10427215b2bSchristos
10527215b2bSchristos ATF_TC(sissigned);
ATF_TC_HEAD(sissigned,tc)10627215b2bSchristos ATF_TC_HEAD(sissigned, tc)
10727215b2bSchristos {
10827215b2bSchristos atf_tc_set_md_var(tc, "descr", "Checks issigned macro for signed");
10927215b2bSchristos }
11027215b2bSchristos
ATF_TC_BODY(sissigned,tc)11127215b2bSchristos ATF_TC_BODY(sissigned, tc)
11227215b2bSchristos {
11327215b2bSchristos #define CHECK(a) ATF_REQUIRE(__type_is_signed(a) == 1)
11427215b2bSchristos
11527215b2bSchristos CHECK(signed char);
11627215b2bSchristos CHECK(signed short);
11727215b2bSchristos CHECK(signed int);
11827215b2bSchristos CHECK(signed long);
11927215b2bSchristos CHECK(signed long long);
12027215b2bSchristos #undef CHECK
12127215b2bSchristos }
12227215b2bSchristos
12327215b2bSchristos ATF_TC(uissigned);
ATF_TC_HEAD(uissigned,tc)12427215b2bSchristos ATF_TC_HEAD(uissigned, tc)
12527215b2bSchristos {
12627215b2bSchristos atf_tc_set_md_var(tc, "descr", "Checks issigned macro for unsigned");
12727215b2bSchristos }
12827215b2bSchristos
ATF_TC_BODY(uissigned,tc)12927215b2bSchristos ATF_TC_BODY(uissigned, tc)
13027215b2bSchristos {
13127215b2bSchristos #define CHECK(a) ATF_REQUIRE(__type_is_signed(a) == 0)
13227215b2bSchristos
13327215b2bSchristos CHECK(unsigned char);
13427215b2bSchristos CHECK(unsigned short);
13527215b2bSchristos CHECK(unsigned int);
13627215b2bSchristos CHECK(unsigned long);
13727215b2bSchristos CHECK(unsigned long long);
13827215b2bSchristos #undef CHECK
13927215b2bSchristos }
14027215b2bSchristos
14127215b2bSchristos ATF_TC(utypemask);
ATF_TC_HEAD(utypemask,tc)14227215b2bSchristos ATF_TC_HEAD(utypemask, tc)
14327215b2bSchristos {
14427215b2bSchristos atf_tc_set_md_var(tc, "descr", "Checks type mask macro for unsigned");
14527215b2bSchristos }
14627215b2bSchristos
ATF_TC_BODY(utypemask,tc)14727215b2bSchristos ATF_TC_BODY(utypemask, tc)
14827215b2bSchristos {
14927215b2bSchristos #define CHECK(a, b) ATF_REQUIRE(__type_mask(a) == b)
15027215b2bSchristos
15127215b2bSchristos CHECK(unsigned char, 0xffffffffffffff00ULL);
15227215b2bSchristos CHECK(unsigned short, 0xffffffffffff0000ULL);
15327215b2bSchristos CHECK(unsigned int, 0xffffffff00000000ULL);
15427215b2bSchristos CHECK(unsigned long long, 0x0000000000000000ULL);
15527215b2bSchristos #undef CHECK
15627215b2bSchristos }
15727215b2bSchristos
15827215b2bSchristos ATF_TC(stypemask);
ATF_TC_HEAD(stypemask,tc)15927215b2bSchristos ATF_TC_HEAD(stypemask, tc)
16027215b2bSchristos {
16127215b2bSchristos atf_tc_set_md_var(tc, "descr", "Checks type mask macro for signed");
16227215b2bSchristos }
16327215b2bSchristos
ATF_TC_BODY(stypemask,tc)16427215b2bSchristos ATF_TC_BODY(stypemask, tc)
16527215b2bSchristos {
16627215b2bSchristos #define CHECK(a, b) ATF_REQUIRE(__type_mask(a) == b)
16727215b2bSchristos
16827215b2bSchristos CHECK(signed char, 0xffffffffffffff00LL);
16927215b2bSchristos CHECK(signed short, 0xffffffffffff0000LL);
17027215b2bSchristos CHECK(signed int, 0xffffffff00000000LL);
17127215b2bSchristos CHECK(signed long long, 0x0000000000000000LL);
17227215b2bSchristos #undef CHECK
17327215b2bSchristos }
17427215b2bSchristos
17527215b2bSchristos ATF_TC(stypefit);
ATF_TC_HEAD(stypefit,tc)17627215b2bSchristos ATF_TC_HEAD(stypefit, tc)
17727215b2bSchristos {
17827215b2bSchristos atf_tc_set_md_var(tc, "descr", "Checks typefit macro for signed");
17927215b2bSchristos }
18027215b2bSchristos
ATF_TC_BODY(stypefit,tc)18127215b2bSchristos ATF_TC_BODY(stypefit, tc)
18227215b2bSchristos {
183*3890e545Smrg #define CHECK(a, b, c) ATF_REQUIRE(__type_fit(a, b) == c)
18427215b2bSchristos
185*3890e545Smrg CHECK(signed char, -1, 1);
186*3890e545Smrg CHECK(signed char, 1, 1);
187*3890e545Smrg CHECK(signed char, 0x7f, 1);
188*3890e545Smrg CHECK(signed char, 0x80, 0);
189*3890e545Smrg CHECK(signed char, 0xff, 0);
190*3890e545Smrg CHECK(signed char, 0x1ff, 0);
19127215b2bSchristos
192*3890e545Smrg CHECK(signed short, -1, 1);
193*3890e545Smrg CHECK(signed short, 1, 1);
194*3890e545Smrg CHECK(signed short, 0x7fff, 1);
195*3890e545Smrg CHECK(signed short, 0x8000, 0);
196*3890e545Smrg CHECK(signed short, 0xffff, 0);
197*3890e545Smrg CHECK(signed short, 0x1ffff, 0);
19827215b2bSchristos
199*3890e545Smrg CHECK(signed int, -1, 1);
200*3890e545Smrg CHECK(signed int, 1, 1);
201*3890e545Smrg CHECK(signed int, 0x7fffffff, 1);
202*3890e545Smrg CHECK(signed int, 0x80000000, 0);
203*3890e545Smrg CHECK(signed int, 0xffffffff, 0);
204*3890e545Smrg CHECK(signed int, 0x1ffffffffLL, 0);
20527215b2bSchristos
206*3890e545Smrg CHECK(signed long long, -1, 1);
207*3890e545Smrg CHECK(signed long long, 1, 1);
208*3890e545Smrg CHECK(signed long long, 0x7fffffffffffffffLL, 1);
209*3890e545Smrg CHECK(signed long long, 0x8000000000000000LL, 0);
210*3890e545Smrg CHECK(signed long long, 0xffffffffffffffffLL, 0);
21127215b2bSchristos
21227215b2bSchristos #undef CHECK
21327215b2bSchristos }
21427215b2bSchristos
21527215b2bSchristos ATF_TC(utypefit);
ATF_TC_HEAD(utypefit,tc)21627215b2bSchristos ATF_TC_HEAD(utypefit, tc)
21727215b2bSchristos {
21827215b2bSchristos atf_tc_set_md_var(tc, "descr", "Checks typefit macro for unsigned");
21927215b2bSchristos }
22027215b2bSchristos
ATF_TC_BODY(utypefit,tc)22127215b2bSchristos ATF_TC_BODY(utypefit, tc)
22227215b2bSchristos {
223*3890e545Smrg #define CHECK(a, b, c) ATF_REQUIRE(__type_fit(a, b) == c)
22427215b2bSchristos
225*3890e545Smrg CHECK(unsigned char, -1, 0);
226*3890e545Smrg CHECK(unsigned char, 1, 1);
227*3890e545Smrg CHECK(unsigned char, 0x7f, 1);
228*3890e545Smrg CHECK(unsigned char, 0x80, 1);
229*3890e545Smrg CHECK(unsigned char, 0xff, 1);
230*3890e545Smrg CHECK(unsigned char, 0x1ff, 0);
23127215b2bSchristos
232*3890e545Smrg CHECK(unsigned short, -1, 0);
233*3890e545Smrg CHECK(unsigned short, 1, 1);
234*3890e545Smrg CHECK(unsigned short, 0x7fff, 1);
235*3890e545Smrg CHECK(unsigned short, 0x8000, 1);
236*3890e545Smrg CHECK(unsigned short, 0xffff, 1);
237*3890e545Smrg CHECK(unsigned short, 0x1ffff, 0);
23827215b2bSchristos
239*3890e545Smrg CHECK(unsigned int, -1, 0);
240*3890e545Smrg CHECK(unsigned int, 1, 1);
241*3890e545Smrg CHECK(unsigned int, 0x7fffffff, 1);
242*3890e545Smrg CHECK(unsigned int, 0x80000000, 1);
243*3890e545Smrg CHECK(unsigned int, 0xffffffff, 1);
244*3890e545Smrg CHECK(unsigned int, 0x1ffffffffLL, 0);
24527215b2bSchristos
246*3890e545Smrg CHECK(unsigned long long, -1, 0);
247*3890e545Smrg CHECK(unsigned long long, 1, 1);
248*3890e545Smrg CHECK(unsigned long long, 0x7fffffffffffffffULL, 1);
249*3890e545Smrg CHECK(unsigned long long, 0x8000000000000000ULL, 1);
250*3890e545Smrg CHECK(unsigned long long, 0xffffffffffffffffULL, 1);
25127215b2bSchristos
25227215b2bSchristos #undef CHECK
25327215b2bSchristos }
25427215b2bSchristos
ATF_TP_ADD_TCS(tp)25527215b2bSchristos ATF_TP_ADD_TCS(tp)
25627215b2bSchristos {
25727215b2bSchristos ATF_TP_ADD_TC(tp, stypeminmax);
25827215b2bSchristos ATF_TP_ADD_TC(tp, utypeminmax);
25927215b2bSchristos ATF_TP_ADD_TC(tp, sissigned);
26027215b2bSchristos ATF_TP_ADD_TC(tp, uissigned);
26127215b2bSchristos ATF_TP_ADD_TC(tp, stypemask);
26227215b2bSchristos ATF_TP_ADD_TC(tp, utypemask);
26327215b2bSchristos ATF_TP_ADD_TC(tp, stypefit);
26427215b2bSchristos ATF_TP_ADD_TC(tp, utypefit);
26527215b2bSchristos
26627215b2bSchristos return atf_no_error();
26727215b2bSchristos }
268