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