1 | /* $Id: tstRTAssertCompile.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - AssertCompile* - A Compile Time Testcase.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/assert.h>
|
---|
42 |
|
---|
43 |
|
---|
44 | int main(int argc, char **argv)
|
---|
45 | {
|
---|
46 | /* Only positive tests here. */
|
---|
47 | NOREF(argc); NOREF(argv);
|
---|
48 |
|
---|
49 | AssertCompile(true);
|
---|
50 | AssertCompile(1);
|
---|
51 | AssertCompile(2);
|
---|
52 | AssertCompile(99);
|
---|
53 |
|
---|
54 | uint8_t u8; NOREF( u8);
|
---|
55 | uint16_t u16; NOREF(u16);
|
---|
56 | uint32_t u32; NOREF(u32);
|
---|
57 | uint64_t u64; NOREF(u64);
|
---|
58 | AssertCompileSize( u8, 1);
|
---|
59 | AssertCompileSize(u16, 2);
|
---|
60 | AssertCompileSize(u32, 4);
|
---|
61 | AssertCompileSize(u64, 8);
|
---|
62 |
|
---|
63 | AssertCompileSizeAlignment( u8, 1);
|
---|
64 | AssertCompileSizeAlignment(u16, 1);
|
---|
65 | AssertCompileSizeAlignment(u16, 2);
|
---|
66 | AssertCompileSizeAlignment(u32, 1);
|
---|
67 | AssertCompileSizeAlignment(u32, 2);
|
---|
68 | AssertCompileSizeAlignment(u32, 4);
|
---|
69 | AssertCompileSizeAlignment(u64, 1);
|
---|
70 | AssertCompileSizeAlignment(u64, 2);
|
---|
71 | AssertCompileSizeAlignment(u64, 4);
|
---|
72 | AssertCompileSizeAlignment(u64, 8);
|
---|
73 |
|
---|
74 | typedef struct STRUCT12S
|
---|
75 | {
|
---|
76 | uint8_t u8;
|
---|
77 | uint8_t au8[8];
|
---|
78 | uint64_t u64;
|
---|
79 | uint8_t u8UnalignmentFiller1;
|
---|
80 | uint32_t u32;
|
---|
81 | uint8_t u8UnalignmentFiller2;
|
---|
82 | uint16_t u16;
|
---|
83 | const char *psz;
|
---|
84 | uint32_t u32A;
|
---|
85 | uint32_t u32B;
|
---|
86 | } STRUCT1, STRUCT2;
|
---|
87 |
|
---|
88 | AssertCompileMemberSize(STRUCT1, u8, 1);
|
---|
89 | AssertCompileMemberSize(STRUCT1, u16, 2);
|
---|
90 | AssertCompileMemberSize(STRUCT1, u32, 4);
|
---|
91 | AssertCompileMemberSize(STRUCT1, u64, 8);
|
---|
92 |
|
---|
93 | AssertCompileMemberSizeAlignment(STRUCT1, u8, 1);
|
---|
94 | AssertCompileMemberSizeAlignment(STRUCT1, u16, 1);
|
---|
95 | AssertCompileMemberSizeAlignment(STRUCT1, u16, 2);
|
---|
96 | AssertCompileMemberSizeAlignment(STRUCT1, u32, 1);
|
---|
97 | AssertCompileMemberSizeAlignment(STRUCT1, u32, 2);
|
---|
98 | AssertCompileMemberSizeAlignment(STRUCT1, u32, 4);
|
---|
99 | AssertCompileMemberSizeAlignment(STRUCT1, u64, 1);
|
---|
100 | AssertCompileMemberSizeAlignment(STRUCT1, u64, 2);
|
---|
101 | AssertCompileMemberSizeAlignment(STRUCT1, u64, 4);
|
---|
102 | AssertCompileMemberSizeAlignment(STRUCT1, u64, 8);
|
---|
103 | AssertCompileMemberSizeAlignment(STRUCT1, psz, sizeof(void *));
|
---|
104 |
|
---|
105 | AssertCompileMemberAlignment(STRUCT1, u8, 1);
|
---|
106 | AssertCompileMemberAlignment(STRUCT1, u16, 1);
|
---|
107 | AssertCompileMemberAlignment(STRUCT1, u16, 2);
|
---|
108 | AssertCompileMemberAlignment(STRUCT1, u32, 1);
|
---|
109 | AssertCompileMemberAlignment(STRUCT1, u32, 2);
|
---|
110 | AssertCompileMemberAlignment(STRUCT1, u32, 4);
|
---|
111 | AssertCompileMemberAlignment(STRUCT1, u64, 1);
|
---|
112 | AssertCompileMemberAlignment(STRUCT1, u64, 2);
|
---|
113 | AssertCompileMemberAlignment(STRUCT1, u64, 4);
|
---|
114 | #if defined(__GNUC__) && ARCH_BITS >= 64
|
---|
115 | AssertCompileMemberAlignment(STRUCT1, u64, 8);
|
---|
116 | #endif
|
---|
117 | AssertCompileMemberAlignment(STRUCT1, psz, sizeof(void *));
|
---|
118 |
|
---|
119 | AssertCompileMemberOffset(STRUCT1, u8, 0);
|
---|
120 | AssertCompileMemberOffset(STRUCT1, au8, 1);
|
---|
121 | #ifndef _MSC_VER /** @todo figure out why MSC has trouble with these expressions */
|
---|
122 | AssertCompileMemberOffset(STRUCT1, au8[0], 1);
|
---|
123 | AssertCompileMemberOffset(STRUCT1, au8[8], 9);
|
---|
124 | #endif
|
---|
125 |
|
---|
126 | typedef union UNION1U
|
---|
127 | {
|
---|
128 | STRUCT1 s1;
|
---|
129 | STRUCT2 s2;
|
---|
130 | } UNION1;
|
---|
131 |
|
---|
132 | AssertCompile2MemberOffsets(UNION1, s1.u8, s2.u8);
|
---|
133 | AssertCompile2MemberOffsets(UNION1, s1.u16, s2.u16);
|
---|
134 | AssertCompile2MemberOffsets(UNION1, s1.u32, s2.u32);
|
---|
135 | AssertCompile2MemberOffsets(UNION1, s1.u64, s2.u64);
|
---|
136 | AssertCompile2MemberOffsets(UNION1, s1.psz, s2.psz);
|
---|
137 |
|
---|
138 | AssertCompileAdjacentMembers(STRUCT1, u32A, u32B);
|
---|
139 | AssertCompileAdjacentMembers(STRUCT1, u8, au8);
|
---|
140 | #ifndef _MSC_VER /** @todo figure out why MSC has trouble with these expressions */
|
---|
141 | AssertCompileAdjacentMembers(STRUCT1, u8, au8[0]);
|
---|
142 | AssertCompileAdjacentMembers(STRUCT1, au8[0], au8[1]);
|
---|
143 | #endif
|
---|
144 |
|
---|
145 | AssertCompileMembersAtSameOffset(STRUCT1, u8, STRUCT2, u8);
|
---|
146 | AssertCompileMembersAtSameOffset(STRUCT1, au8, STRUCT2, au8);
|
---|
147 | AssertCompileMembersAtSameOffset(STRUCT1, u16, STRUCT2, u16);
|
---|
148 | AssertCompileMembersAtSameOffset(STRUCT1, u32, STRUCT2, u32);
|
---|
149 | AssertCompileMembersAtSameOffset(STRUCT1, u64, STRUCT2, u64);
|
---|
150 |
|
---|
151 | AssertCompileMembersSameSize(STRUCT1, u8, STRUCT2, u8);
|
---|
152 | AssertCompileMembersSameSize(STRUCT1, au8, STRUCT2, au8);
|
---|
153 | AssertCompileMembersSameSize(STRUCT1, u16, STRUCT2, u16);
|
---|
154 | AssertCompileMembersSameSize(STRUCT1, u32, STRUCT2, u32);
|
---|
155 | AssertCompileMembersSameSize(STRUCT1, u64, STRUCT2, u64);
|
---|
156 |
|
---|
157 | AssertCompileMembersSameSizeAndOffset(STRUCT1, u8, STRUCT2, u8);
|
---|
158 | AssertCompileMembersSameSizeAndOffset(STRUCT1, au8, STRUCT2, au8);
|
---|
159 | AssertCompileMembersSameSizeAndOffset(STRUCT1, u16, STRUCT2, u16);
|
---|
160 | AssertCompileMembersSameSizeAndOffset(STRUCT1, u32, STRUCT2, u32);
|
---|
161 | AssertCompileMembersSameSizeAndOffset(STRUCT1, u64, STRUCT2, u64);
|
---|
162 |
|
---|
163 | /*
|
---|
164 | * Check some cdefs.h macros while where here, we'll be using
|
---|
165 | * AssertCompile so it's kind of related.
|
---|
166 | */
|
---|
167 | #ifdef RT_COMPILER_SUPPORTS_VA_ARGS
|
---|
168 | # if 0
|
---|
169 | AssertCompile(RT_COUNT_VA_ARGS() == 0);
|
---|
170 | AssertCompile(RT_COUNT_VA_ARGS(RT_NOTHING) == 1);
|
---|
171 | # endif
|
---|
172 | AssertCompile(RT_COUNT_VA_ARGS(asdf) == 1);
|
---|
173 | AssertCompile(RT_COUNT_VA_ARGS(yyyy) == 1);
|
---|
174 | AssertCompile(RT_COUNT_VA_ARGS(_) == 1);
|
---|
175 | AssertCompile(RT_COUNT_VA_ARGS(1, 2, 3, 4, 5, 6, 7, 8, 9, 0) == 10);
|
---|
176 | #endif
|
---|
177 | return 0;
|
---|
178 | }
|
---|
179 |
|
---|