Home | History | Annotate | Line # | Download | only in sys
      1  1.4       mrg /* $NetBSD: t_cdefs.c,v 1.4 2016/03/16 07:21:36 mrg 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.4       mrg __RCSID("$NetBSD: t_cdefs.c,v 1.4 2016/03/16 07:21:36 mrg 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.2      matt 	{ "signed char", SCHAR_MIN, SCHAR_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.4       mrg #define CHECK(a, b, c) ATF_REQUIRE(__type_fit(a, b) == c)
    184  1.1  christos 
    185  1.4       mrg 	CHECK(signed char, -1, 1);
    186  1.4       mrg 	CHECK(signed char, 1, 1);
    187  1.4       mrg 	CHECK(signed char, 0x7f, 1);
    188  1.4       mrg 	CHECK(signed char, 0x80, 0);
    189  1.4       mrg 	CHECK(signed char, 0xff, 0);
    190  1.4       mrg 	CHECK(signed char, 0x1ff, 0);
    191  1.4       mrg 
    192  1.4       mrg 	CHECK(signed short, -1, 1);
    193  1.4       mrg 	CHECK(signed short, 1, 1);
    194  1.4       mrg 	CHECK(signed short, 0x7fff, 1);
    195  1.4       mrg 	CHECK(signed short, 0x8000, 0);
    196  1.4       mrg 	CHECK(signed short, 0xffff, 0);
    197  1.4       mrg 	CHECK(signed short, 0x1ffff, 0);
    198  1.4       mrg 
    199  1.4       mrg 	CHECK(signed int, -1, 1);
    200  1.4       mrg 	CHECK(signed int, 1, 1);
    201  1.4       mrg 	CHECK(signed int, 0x7fffffff, 1);
    202  1.4       mrg 	CHECK(signed int, 0x80000000, 0);
    203  1.4       mrg 	CHECK(signed int, 0xffffffff, 0);
    204  1.4       mrg 	CHECK(signed int, 0x1ffffffffLL, 0);
    205  1.4       mrg 
    206  1.4       mrg 	CHECK(signed long long, -1, 1);
    207  1.4       mrg 	CHECK(signed long long, 1, 1);
    208  1.4       mrg 	CHECK(signed long long, 0x7fffffffffffffffLL, 1);
    209  1.4       mrg 	CHECK(signed long long, 0x8000000000000000LL, 0);
    210  1.4       mrg 	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.4       mrg #define CHECK(a, b, c) ATF_REQUIRE(__type_fit(a, b) == c)
    224  1.1  christos 
    225  1.4       mrg 	CHECK(unsigned char, -1, 0);
    226  1.4       mrg 	CHECK(unsigned char, 1, 1);
    227  1.4       mrg 	CHECK(unsigned char, 0x7f, 1);
    228  1.4       mrg 	CHECK(unsigned char, 0x80, 1);
    229  1.4       mrg 	CHECK(unsigned char, 0xff, 1);
    230  1.4       mrg 	CHECK(unsigned char, 0x1ff, 0);
    231  1.4       mrg 
    232  1.4       mrg 	CHECK(unsigned short, -1, 0);
    233  1.4       mrg 	CHECK(unsigned short, 1, 1);
    234  1.4       mrg 	CHECK(unsigned short, 0x7fff, 1);
    235  1.4       mrg 	CHECK(unsigned short, 0x8000, 1);
    236  1.4       mrg 	CHECK(unsigned short, 0xffff, 1);
    237  1.4       mrg 	CHECK(unsigned short, 0x1ffff, 0);
    238  1.4       mrg 
    239  1.4       mrg 	CHECK(unsigned int, -1, 0);
    240  1.4       mrg 	CHECK(unsigned int, 1, 1);
    241  1.4       mrg 	CHECK(unsigned int, 0x7fffffff, 1);
    242  1.4       mrg 	CHECK(unsigned int, 0x80000000, 1);
    243  1.4       mrg 	CHECK(unsigned int, 0xffffffff, 1);
    244  1.4       mrg 	CHECK(unsigned int, 0x1ffffffffLL, 0);
    245  1.4       mrg 
    246  1.4       mrg 	CHECK(unsigned long long, -1, 0);
    247  1.4       mrg 	CHECK(unsigned long long, 1, 1);
    248  1.4       mrg 	CHECK(unsigned long long, 0x7fffffffffffffffULL, 1);
    249  1.4       mrg 	CHECK(unsigned long long, 0x8000000000000000ULL, 1);
    250  1.4       mrg 	CHECK(unsigned long long, 0xffffffffffffffffULL, 1);
    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