xref: /minix3/tests/include/sys/t_cdefs.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /* $NetBSD: t_cdefs.c,v 1.3 2013/09/05 09:01:27 gsutre Exp $ */
211be35a1SLionel Sambuc 
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc  * Copyright (c) 2012 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc  * All rights reserved.
611be35a1SLionel Sambuc  *
711be35a1SLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
811be35a1SLionel Sambuc  * by Christos Zoulas.
911be35a1SLionel Sambuc  *
1011be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
1111be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
1211be35a1SLionel Sambuc  * are met:
1311be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
1411be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
1511be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
1611be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
1711be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
1811be35a1SLionel Sambuc  *
1911be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2011be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2111be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2211be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2311be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2411be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2511be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2611be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2711be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2811be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2911be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
3011be35a1SLionel Sambuc  */
3111be35a1SLionel Sambuc 
3211be35a1SLionel Sambuc #include <sys/cdefs.h>
3311be35a1SLionel Sambuc __COPYRIGHT("@(#) Copyright (c) 2008\
3411be35a1SLionel Sambuc  The NetBSD Foundation, inc. All rights reserved.");
35*84d9c625SLionel Sambuc __RCSID("$NetBSD: t_cdefs.c,v 1.3 2013/09/05 09:01:27 gsutre Exp $");
3611be35a1SLionel Sambuc 
3711be35a1SLionel Sambuc #include <atf-c.h>
3811be35a1SLionel Sambuc #include <sys/types.h>
3911be35a1SLionel Sambuc #include <limits.h>
4011be35a1SLionel Sambuc #include <stdint.h>
4111be35a1SLionel Sambuc 
4211be35a1SLionel Sambuc static const struct {
4311be35a1SLionel Sambuc 	const char *name;
4411be35a1SLionel Sambuc 	intmax_t min;
4511be35a1SLionel Sambuc 	intmax_t max;
4611be35a1SLionel Sambuc } s[] = {
4711be35a1SLionel Sambuc 	{ "signed char", SCHAR_MIN, SCHAR_MAX },
4811be35a1SLionel Sambuc 	{ "signed short", SHRT_MIN, SHRT_MAX },
4911be35a1SLionel Sambuc 	{ "signed int", INT_MIN, INT_MAX },
5011be35a1SLionel Sambuc 	{ "signed long", LONG_MIN, LONG_MAX },
5111be35a1SLionel Sambuc 	{ "signed long long", LLONG_MIN, LLONG_MAX },
5211be35a1SLionel Sambuc };
5311be35a1SLionel Sambuc 
5411be35a1SLionel Sambuc static const struct {
5511be35a1SLionel Sambuc 	const char *name;
5611be35a1SLionel Sambuc 	uintmax_t min;
5711be35a1SLionel Sambuc 	uintmax_t max;
5811be35a1SLionel Sambuc } u[] = {
5911be35a1SLionel Sambuc 	{ "unsigned char", 0, UCHAR_MAX },
6011be35a1SLionel Sambuc 	{ "unsigned short", 0, USHRT_MAX },
6111be35a1SLionel Sambuc 	{ "unsigned int", 0, UINT_MAX },
6211be35a1SLionel Sambuc 	{ "unsigned long", 0, ULONG_MAX },
6311be35a1SLionel Sambuc 	{ "unsigned long long", 0, ULLONG_MAX },
6411be35a1SLionel Sambuc };
6511be35a1SLionel Sambuc 
6611be35a1SLionel Sambuc ATF_TC(stypeminmax);
ATF_TC_HEAD(stypeminmax,tc)6711be35a1SLionel Sambuc ATF_TC_HEAD(stypeminmax, tc)
6811be35a1SLionel Sambuc {
6911be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks signed type min/max macros");
7011be35a1SLionel Sambuc }
7111be35a1SLionel Sambuc 
7211be35a1SLionel Sambuc 
ATF_TC_BODY(stypeminmax,tc)7311be35a1SLionel Sambuc ATF_TC_BODY(stypeminmax, tc)
7411be35a1SLionel Sambuc {
7511be35a1SLionel Sambuc #define CHECK(a, b) ATF_REQUIRE(__type_min(a) == s[b].min); \
7611be35a1SLionel Sambuc     ATF_REQUIRE(__type_max(a) == s[b].max)
7711be35a1SLionel Sambuc 
7811be35a1SLionel Sambuc 	CHECK(signed char, 0);
7911be35a1SLionel Sambuc 	CHECK(signed short, 1);
8011be35a1SLionel Sambuc 	CHECK(signed int, 2);
8111be35a1SLionel Sambuc 	CHECK(signed long, 3);
8211be35a1SLionel Sambuc 	CHECK(signed long long, 4);
8311be35a1SLionel Sambuc #undef CHECK
8411be35a1SLionel Sambuc }
8511be35a1SLionel Sambuc 
8611be35a1SLionel Sambuc ATF_TC(utypeminmax);
ATF_TC_HEAD(utypeminmax,tc)8711be35a1SLionel Sambuc ATF_TC_HEAD(utypeminmax, tc)
8811be35a1SLionel Sambuc {
8911be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks unsigned type min/max macros");
9011be35a1SLionel Sambuc }
9111be35a1SLionel Sambuc 
ATF_TC_BODY(utypeminmax,tc)9211be35a1SLionel Sambuc ATF_TC_BODY(utypeminmax, tc)
9311be35a1SLionel Sambuc {
9411be35a1SLionel Sambuc #define CHECK(a, b) ATF_REQUIRE(__type_min(a) == u[b].min); \
9511be35a1SLionel Sambuc     ATF_REQUIRE(__type_max(a) == u[b].max)
9611be35a1SLionel Sambuc 
9711be35a1SLionel Sambuc 	CHECK(unsigned char, 0);
9811be35a1SLionel Sambuc 	CHECK(unsigned short, 1);
9911be35a1SLionel Sambuc 	CHECK(unsigned int, 2);
10011be35a1SLionel Sambuc 	CHECK(unsigned long, 3);
10111be35a1SLionel Sambuc 	CHECK(unsigned long long, 4);
10211be35a1SLionel Sambuc #undef CHECK
10311be35a1SLionel Sambuc }
10411be35a1SLionel Sambuc 
10511be35a1SLionel Sambuc ATF_TC(sissigned);
ATF_TC_HEAD(sissigned,tc)10611be35a1SLionel Sambuc ATF_TC_HEAD(sissigned, tc)
10711be35a1SLionel Sambuc {
10811be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks issigned macro for signed");
10911be35a1SLionel Sambuc }
11011be35a1SLionel Sambuc 
ATF_TC_BODY(sissigned,tc)11111be35a1SLionel Sambuc ATF_TC_BODY(sissigned, tc)
11211be35a1SLionel Sambuc {
11311be35a1SLionel Sambuc #define CHECK(a) ATF_REQUIRE(__type_is_signed(a) == 1)
11411be35a1SLionel Sambuc 
11511be35a1SLionel Sambuc 	CHECK(signed char);
11611be35a1SLionel Sambuc 	CHECK(signed short);
11711be35a1SLionel Sambuc 	CHECK(signed int);
11811be35a1SLionel Sambuc 	CHECK(signed long);
11911be35a1SLionel Sambuc 	CHECK(signed long long);
12011be35a1SLionel Sambuc #undef CHECK
12111be35a1SLionel Sambuc }
12211be35a1SLionel Sambuc 
12311be35a1SLionel Sambuc ATF_TC(uissigned);
ATF_TC_HEAD(uissigned,tc)12411be35a1SLionel Sambuc ATF_TC_HEAD(uissigned, tc)
12511be35a1SLionel Sambuc {
12611be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks issigned macro for unsigned");
12711be35a1SLionel Sambuc }
12811be35a1SLionel Sambuc 
ATF_TC_BODY(uissigned,tc)12911be35a1SLionel Sambuc ATF_TC_BODY(uissigned, tc)
13011be35a1SLionel Sambuc {
13111be35a1SLionel Sambuc #define CHECK(a) ATF_REQUIRE(__type_is_signed(a) == 0)
13211be35a1SLionel Sambuc 
13311be35a1SLionel Sambuc 	CHECK(unsigned char);
13411be35a1SLionel Sambuc 	CHECK(unsigned short);
13511be35a1SLionel Sambuc 	CHECK(unsigned int);
13611be35a1SLionel Sambuc 	CHECK(unsigned long);
13711be35a1SLionel Sambuc 	CHECK(unsigned long long);
13811be35a1SLionel Sambuc #undef CHECK
13911be35a1SLionel Sambuc }
14011be35a1SLionel Sambuc 
14111be35a1SLionel Sambuc ATF_TC(utypemask);
ATF_TC_HEAD(utypemask,tc)14211be35a1SLionel Sambuc ATF_TC_HEAD(utypemask, tc)
14311be35a1SLionel Sambuc {
14411be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks type mask macro for unsigned");
14511be35a1SLionel Sambuc }
14611be35a1SLionel Sambuc 
ATF_TC_BODY(utypemask,tc)14711be35a1SLionel Sambuc ATF_TC_BODY(utypemask, tc)
14811be35a1SLionel Sambuc {
14911be35a1SLionel Sambuc #define CHECK(a, b) ATF_REQUIRE(__type_mask(a) == b)
15011be35a1SLionel Sambuc 
15111be35a1SLionel Sambuc 	CHECK(unsigned char,      0xffffffffffffff00ULL);
15211be35a1SLionel Sambuc 	CHECK(unsigned short,     0xffffffffffff0000ULL);
15311be35a1SLionel Sambuc 	CHECK(unsigned int,       0xffffffff00000000ULL);
15411be35a1SLionel Sambuc 	CHECK(unsigned long long, 0x0000000000000000ULL);
15511be35a1SLionel Sambuc #undef CHECK
15611be35a1SLionel Sambuc }
15711be35a1SLionel Sambuc 
15811be35a1SLionel Sambuc ATF_TC(stypemask);
ATF_TC_HEAD(stypemask,tc)15911be35a1SLionel Sambuc ATF_TC_HEAD(stypemask, tc)
16011be35a1SLionel Sambuc {
16111be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks type mask macro for signed");
16211be35a1SLionel Sambuc }
16311be35a1SLionel Sambuc 
ATF_TC_BODY(stypemask,tc)16411be35a1SLionel Sambuc ATF_TC_BODY(stypemask, tc)
16511be35a1SLionel Sambuc {
16611be35a1SLionel Sambuc #define CHECK(a, b) ATF_REQUIRE(__type_mask(a) == b)
16711be35a1SLionel Sambuc 
16811be35a1SLionel Sambuc 	CHECK(signed char,      0xffffffffffffff00LL);
16911be35a1SLionel Sambuc 	CHECK(signed short,     0xffffffffffff0000LL);
17011be35a1SLionel Sambuc 	CHECK(signed int,       0xffffffff00000000LL);
17111be35a1SLionel Sambuc 	CHECK(signed long long, 0x0000000000000000LL);
17211be35a1SLionel Sambuc #undef CHECK
17311be35a1SLionel Sambuc }
17411be35a1SLionel Sambuc 
17511be35a1SLionel Sambuc ATF_TC(stypefit);
ATF_TC_HEAD(stypefit,tc)17611be35a1SLionel Sambuc ATF_TC_HEAD(stypefit, tc)
17711be35a1SLionel Sambuc {
17811be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks typefit macro for signed");
17911be35a1SLionel Sambuc }
18011be35a1SLionel Sambuc 
ATF_TC_BODY(stypefit,tc)18111be35a1SLionel Sambuc ATF_TC_BODY(stypefit, tc)
18211be35a1SLionel Sambuc {
18311be35a1SLionel Sambuc #define CHECK(a, b, c) ATF_REQUIRE(!__type_fit(a, b) == c)
18411be35a1SLionel Sambuc 
18511be35a1SLionel Sambuc 	CHECK(signed char, -1, 0);
18611be35a1SLionel Sambuc 	CHECK(signed char, 1, 0);
18711be35a1SLionel Sambuc 	CHECK(signed char, 0x7f, 0);
18811be35a1SLionel Sambuc 	CHECK(signed char, 0x80, 1);
18911be35a1SLionel Sambuc 	CHECK(signed char, 0xff, 1);
19011be35a1SLionel Sambuc 	CHECK(signed char, 0x1ff, 1);
19111be35a1SLionel Sambuc 
19211be35a1SLionel Sambuc 	CHECK(signed short, -1, 0);
19311be35a1SLionel Sambuc 	CHECK(signed short, 1, 0);
19411be35a1SLionel Sambuc 	CHECK(signed short, 0x7fff, 0);
19511be35a1SLionel Sambuc 	CHECK(signed short, 0x8000, 1);
19611be35a1SLionel Sambuc 	CHECK(signed short, 0xffff, 1);
19711be35a1SLionel Sambuc 	CHECK(signed short, 0x1ffff, 1);
19811be35a1SLionel Sambuc 
19911be35a1SLionel Sambuc 	CHECK(signed int, -1, 0);
20011be35a1SLionel Sambuc 	CHECK(signed int, 1, 0);
20111be35a1SLionel Sambuc 	CHECK(signed int, 0x7fffffff, 0);
20211be35a1SLionel Sambuc 	CHECK(signed int, 0x80000000, 1);
20311be35a1SLionel Sambuc 	CHECK(signed int, 0xffffffff, 1);
20411be35a1SLionel Sambuc 	CHECK(signed int, 0x1ffffffffLL, 1);
20511be35a1SLionel Sambuc 
20611be35a1SLionel Sambuc 	CHECK(signed long long, -1, 0);
20711be35a1SLionel Sambuc 	CHECK(signed long long, 1, 0);
20811be35a1SLionel Sambuc 	CHECK(signed long long, 0x7fffffffffffffffLL, 0);
209*84d9c625SLionel Sambuc 	CHECK(signed long long, 0x8000000000000000LL, 1);
210*84d9c625SLionel Sambuc 	CHECK(signed long long, 0xffffffffffffffffLL, 1);
21111be35a1SLionel Sambuc 
21211be35a1SLionel Sambuc #undef CHECK
21311be35a1SLionel Sambuc }
21411be35a1SLionel Sambuc 
21511be35a1SLionel Sambuc ATF_TC(utypefit);
ATF_TC_HEAD(utypefit,tc)21611be35a1SLionel Sambuc ATF_TC_HEAD(utypefit, tc)
21711be35a1SLionel Sambuc {
21811be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks typefit macro for unsigned");
21911be35a1SLionel Sambuc }
22011be35a1SLionel Sambuc 
ATF_TC_BODY(utypefit,tc)22111be35a1SLionel Sambuc ATF_TC_BODY(utypefit, tc)
22211be35a1SLionel Sambuc {
22311be35a1SLionel Sambuc #define CHECK(a, b, c) ATF_REQUIRE(!__type_fit(a, b) == c)
22411be35a1SLionel Sambuc 
22511be35a1SLionel Sambuc 	CHECK(unsigned char, -1, 1);
22611be35a1SLionel Sambuc 	CHECK(unsigned char, 1, 0);
22711be35a1SLionel Sambuc 	CHECK(unsigned char, 0x7f, 0);
22811be35a1SLionel Sambuc 	CHECK(unsigned char, 0x80, 0);
22911be35a1SLionel Sambuc 	CHECK(unsigned char, 0xff, 0);
23011be35a1SLionel Sambuc 	CHECK(unsigned char, 0x1ff, 1);
23111be35a1SLionel Sambuc 
23211be35a1SLionel Sambuc 	CHECK(unsigned short, -1, 1);
23311be35a1SLionel Sambuc 	CHECK(unsigned short, 1, 0);
23411be35a1SLionel Sambuc 	CHECK(unsigned short, 0x7fff, 0);
23511be35a1SLionel Sambuc 	CHECK(unsigned short, 0x8000, 0);
23611be35a1SLionel Sambuc 	CHECK(unsigned short, 0xffff, 0);
23711be35a1SLionel Sambuc 	CHECK(unsigned short, 0x1ffff, 1);
23811be35a1SLionel Sambuc 
23911be35a1SLionel Sambuc 	CHECK(unsigned int, -1, 1);
24011be35a1SLionel Sambuc 	CHECK(unsigned int, 1, 0);
24111be35a1SLionel Sambuc 	CHECK(unsigned int, 0x7fffffff, 0);
24211be35a1SLionel Sambuc 	CHECK(unsigned int, 0x80000000, 0);
24311be35a1SLionel Sambuc 	CHECK(unsigned int, 0xffffffff, 0);
24411be35a1SLionel Sambuc 	CHECK(unsigned int, 0x1ffffffffLL, 1);
24511be35a1SLionel Sambuc 
24611be35a1SLionel Sambuc 	CHECK(unsigned long long, -1, 1);
24711be35a1SLionel Sambuc 	CHECK(unsigned long long, 1, 0);
24811be35a1SLionel Sambuc 	CHECK(unsigned long long, 0x7fffffffffffffffULL, 0);
24911be35a1SLionel Sambuc 	CHECK(unsigned long long, 0x8000000000000000ULL, 0);
25011be35a1SLionel Sambuc 	CHECK(unsigned long long, 0xffffffffffffffffULL, 0);
25111be35a1SLionel Sambuc 
25211be35a1SLionel Sambuc #undef CHECK
25311be35a1SLionel Sambuc }
25411be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)25511be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
25611be35a1SLionel Sambuc {
25711be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, stypeminmax);
25811be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, utypeminmax);
25911be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, sissigned);
26011be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, uissigned);
26111be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, stypemask);
26211be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, utypemask);
26311be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, stypefit);
26411be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, utypefit);
26511be35a1SLionel Sambuc 
26611be35a1SLionel Sambuc 	return atf_no_error();
26711be35a1SLionel Sambuc }
268