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