| 1 |
struct Zero |
| 2 |
{ |
| 3 |
int x; |
| 4 |
} |
| 5 |
|
| 6 |
void testZero() |
| 7 |
{ |
| 8 |
auto zeroInit = __traits(initSymbol, Zero); |
| 9 |
static assert(is(typeof(zeroInit) == const(void[]))); |
| 10 |
|
| 11 |
assert(zeroInit.ptr is null); |
| 12 |
assert(zeroInit.length == Zero.sizeof); |
| 13 |
} |
| 14 |
|
| 15 |
struct NonZero |
| 16 |
{ |
| 17 |
long x = 1; |
| 18 |
} |
| 19 |
|
| 20 |
void testNonZero() |
| 21 |
{ |
| 22 |
auto nonZeroInit = __traits(initSymbol, NonZero); |
| 23 |
static assert(is(typeof(nonZeroInit) == const(void[]))); |
| 24 |
|
| 25 |
assert(nonZeroInit.ptr); |
| 26 |
assert(nonZeroInit.length == NonZero.sizeof); |
| 27 |
assert(cast(const(long[])) nonZeroInit == [1L]); |
| 28 |
} |
| 29 |
|
| 30 |
class C |
| 31 |
{ |
| 32 |
short x = 123; |
| 33 |
} |
| 34 |
|
| 35 |
void testClass() |
| 36 |
{ |
| 37 |
auto cInit = __traits(initSymbol, C); |
| 38 |
static assert(is(typeof(cInit) == const(void[]))); |
| 39 |
|
| 40 |
assert(cInit.ptr); |
| 41 |
assert(cInit.length == __traits(classInstanceSize, C)); |
| 42 |
|
| 43 |
scope c = new C; |
| 44 |
assert((cast(void*) c)[0 .. cInit.length] == cInit); |
| 45 |
} |
| 46 |
|
| 47 |
struct AlignedStruct |
| 48 |
{ |
| 49 |
short s = 5; |
| 50 |
// 2 byte padding |
| 51 |
align(4) char c = 'c'; |
| 52 |
// 3 byte padding |
| 53 |
int i = 4; |
| 54 |
// reduced alignment |
| 55 |
align(1) long l = 0xDEADBEEF; |
| 56 |
} |
| 57 |
|
| 58 |
void testAlignedStruct() |
| 59 |
{ |
| 60 |
auto init = __traits(initSymbol, AlignedStruct); |
| 61 |
|
| 62 |
assert(init.ptr); |
| 63 |
assert(init.length == AlignedStruct.sizeof); |
| 64 |
|
| 65 |
version (GNU) |
| 66 |
AlignedStruct exp = AlignedStruct(); |
| 67 |
else |
| 68 |
AlignedStruct exp; |
| 69 |
assert(init == (cast(void*) &exp)[0 .. AlignedStruct.sizeof]); |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
class AlignedClass : C |
| 74 |
{ |
| 75 |
short s = 5; |
| 76 |
// 2 byte padding |
| 77 |
align(4) char c = 'c'; |
| 78 |
// 3 byte padding |
| 79 |
int i = 4; |
| 80 |
// reduced alignment |
| 81 |
align(1) long l = 0xDEADBEEF; |
| 82 |
} |
| 83 |
|
| 84 |
void testAlignedClass() |
| 85 |
{ |
| 86 |
auto init = __traits(initSymbol, AlignedClass); |
| 87 |
|
| 88 |
assert(init.ptr); |
| 89 |
assert(init.length == __traits(classInstanceSize, AlignedClass)); |
| 90 |
|
| 91 |
scope ac = new AlignedClass(); |
| 92 |
assert(init == (cast(void*) ac)[0 .. init.length]); |
| 93 |
} |
| 94 |
|
| 95 |
extern (C++) class ExternCppClass |
| 96 |
{ |
| 97 |
int i = 4; |
| 98 |
} |
| 99 |
|
| 100 |
void testExternCppClass() |
| 101 |
{ |
| 102 |
auto init = __traits(initSymbol, ExternCppClass); |
| 103 |
|
| 104 |
assert(init.ptr); |
| 105 |
assert(init.length == __traits(classInstanceSize, ExternCppClass)); |
| 106 |
|
| 107 |
scope ac = new ExternCppClass(); |
| 108 |
assert(init == (cast(void*) ac)[0 .. init.length]); |
| 109 |
} |
| 110 |
|
| 111 |
void main() |
| 112 |
{ |
| 113 |
testZero(); |
| 114 |
testNonZero(); |
| 115 |
testClass(); |
| 116 |
testAlignedStruct(); |
| 117 |
testAlignedClass(); |
| 118 |
testExternCppClass(); |
| 119 |
} |