Home | History | Annotate | Line # | Download | only in include
      1 /*	$NetBSD: t_stddef.c,v 1.1 2025/04/01 00:33:55 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2025 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  * Include <stddef.h> first to verify it declares everything we need.
     31  */
     32 #include <stddef.h>
     33 
     34 #if __STDC_VERSION__ - 0 >= 202311L
     35 #if __STDC_VERSION_STDDEF_H__ - 0 < 202311L
     36 #error __STDC_VERSION_STDDEF_H__ not defined appropriately
     37 #endif
     38 #endif
     39 
     40 typedef ptrdiff_t nbtest_ptrdiff_t;
     41 typedef size_t nbtest_size_t;
     42 #if __STDC_VERSION__ - 0 >= 201112L
     43 typedef max_align_t nbtest_max_align_t;
     44 #endif
     45 typedef wchar_t nbtest_wchar_t;
     46 #if __STDC_VERSION__ - 0 >= 202311L
     47 typedef nullptr_t nbtest_nullptr_t;
     48 #endif
     49 
     50 #include <sys/cdefs.h>
     51 __RCSID("$NetBSD: t_stddef.c,v 1.1 2025/04/01 00:33:55 riastradh Exp $");
     52 
     53 #include <atf-c.h>
     54 #include <stdalign.h>
     55 
     56 ATF_TC(macros);
     57 ATF_TC_HEAD(macros, tc)
     58 {
     59 	atf_tc_set_md_var(tc, "descr", "Test <stddef.h> macros work");
     60 }
     61 ATF_TC_BODY(macros, tc)
     62 {
     63 	void *volatile pNULL = NULL;
     64 #if __STDC_VERSION__ - 0 >= 202311L
     65 	void *volatile pnullptr = nullptr;
     66 #endif
     67 	struct s { char x[3], y; };
     68 	size_t o;
     69 
     70 	ATF_CHECK(!pNULL);
     71 #if __STDC_VERSION__ - 0 >= 202311L
     72 	ATF_CHECK(!pnullptr);
     73 #endif
     74 
     75 #if __STDC_VERSION__ - 0 >= 202311L
     76 	volatile enum { A, B } x = A;
     77 	switch (x) {
     78 	case A:
     79 		break;
     80 	case B:
     81 	default:
     82 		unreachable();
     83 	}
     84 #endif
     85 
     86 	ATF_CHECK_MSG((o = offsetof(struct s, y)) == 3,
     87 	    "o=%zu", o);
     88 }
     89 
     90 ATF_TC(types);
     91 ATF_TC_HEAD(types, tc)
     92 {
     93 	atf_tc_set_md_var(tc, "descr", "Test <stddef.h> types are reasonable");
     94 }
     95 ATF_TC_BODY(types, tc)
     96 {
     97 
     98 #ifdef __GNUC__
     99 	char *p, *q;
    100 	ATF_CHECK(__builtin_types_compatible_p(ptrdiff_t, typeof(p - q)));
    101 	ATF_CHECK(__builtin_types_compatible_p(size_t, typeof(sizeof(p))));
    102 #if __STDC_VERSION__ - 0 >= 202311L
    103 	ATF_CHECK(__builtin_types_compatible_p(nullptr_t, typeof(nullptr)));
    104 #endif
    105 #endif
    106 
    107 #if __STDC_VERSION__ - 0 >= 201112L
    108 	size_t a;
    109 	ATF_CHECK_MSG((a = alignof(max_align_t)) >= alignof(long long),
    110 	    "a=%zu", a);
    111 	ATF_CHECK_MSG((a = alignof(max_align_t)) >= alignof(long double),
    112 	    "a=%zu", a);
    113 	ATF_CHECK_MSG((a = alignof(max_align_t)) >= alignof(void *),
    114 	    "a=%zu", a);
    115 	ATF_CHECK_MSG((a = alignof(max_align_t)) >= alignof(int (*)(void)),
    116 	    "a=%zu", a);
    117 #endif
    118 }
    119 
    120 ATF_TP_ADD_TCS(tp)
    121 {
    122 
    123 	ATF_TP_ADD_TC(tp, macros);
    124 	ATF_TP_ADD_TC(tp, types);
    125 
    126 	return atf_no_error();
    127 }
    128