1 //=== - llvm/unittest/Support/TrailingObjectsTest.cpp ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/TrailingObjects.h"
11 #include "gtest/gtest.h"
16 // This class, beyond being used by the test case, a nice
17 // demonstration of the intended usage of TrailingObjects, with a
18 // single trailing array.
19 class Class1 final : protected TrailingObjects<Class1, short> {
20 friend TrailingObjects;
25 size_t numTrailingObjects(OverloadToken<short>) const { return NumShorts; }
27 Class1(int *ShortArray, unsigned NumShorts) : NumShorts(NumShorts) {
28 std::uninitialized_copy(ShortArray, ShortArray + NumShorts,
29 getTrailingObjects<short>());
33 static Class1 *create(int *ShortArray, unsigned NumShorts) {
34 void *Mem = ::operator new(totalSizeToAlloc<short>(NumShorts));
35 return new (Mem) Class1(ShortArray, NumShorts);
38 short get(unsigned Num) const { return getTrailingObjects<short>()[Num]; }
40 unsigned numShorts() const { return NumShorts; }
42 // Pull some protected members in as public, for testability.
43 using TrailingObjects::totalSizeToAlloc;
44 using TrailingObjects::additionalSizeToAlloc;
45 using TrailingObjects::getTrailingObjects;
48 // Here, there are two singular optional object types appended.
49 // Note that it fails to compile without the alignment spec.
50 class LLVM_ALIGNAS(8) Class2 final : protected TrailingObjects<Class2, double, short> {
51 friend TrailingObjects;
53 bool HasShort, HasDouble;
56 size_t numTrailingObjects(OverloadToken<short>) const {
57 return HasShort ? 1 : 0;
59 size_t numTrailingObjects(OverloadToken<double>) const {
60 return HasDouble ? 1 : 0;
63 Class2(bool HasShort, bool HasDouble)
64 : HasShort(HasShort), HasDouble(HasDouble) {}
67 static Class2 *create(short S = 0, double D = 0.0) {
68 bool HasShort = S != 0;
69 bool HasDouble = D != 0.0;
72 ::operator new(totalSizeToAlloc<double, short>(HasDouble, HasShort));
73 Class2 *C = new (Mem) Class2(HasShort, HasDouble);
75 *C->getTrailingObjects<short>() = S;
77 *C->getTrailingObjects<double>() = D;
81 short getShort() const {
84 return *getTrailingObjects<short>();
87 double getDouble() const {
90 return *getTrailingObjects<double>();
93 // Pull some protected members in as public, for testability.
94 using TrailingObjects::totalSizeToAlloc;
95 using TrailingObjects::additionalSizeToAlloc;
96 using TrailingObjects::getTrailingObjects;
99 TEST(TrailingObjects, OneArg) {
100 int arr[] = {1, 2, 3};
101 Class1 *C = Class1::create(arr, 3);
102 EXPECT_EQ(sizeof(Class1), sizeof(unsigned));
103 EXPECT_EQ(Class1::additionalSizeToAlloc<short>(1), sizeof(short));
104 EXPECT_EQ(Class1::additionalSizeToAlloc<short>(3), sizeof(short) * 3);
106 EXPECT_EQ(Class1::totalSizeToAlloc<short>(1), sizeof(Class1) + sizeof(short));
107 EXPECT_EQ(Class1::totalSizeToAlloc<short>(3),
108 sizeof(Class1) + sizeof(short) * 3);
110 EXPECT_EQ(C->getTrailingObjects<short>(), reinterpret_cast<short *>(C + 1));
111 EXPECT_EQ(C->get(0), 1);
112 EXPECT_EQ(C->get(2), 3);
116 TEST(TrailingObjects, TwoArg) {
117 Class2 *C1 = Class2::create(4);
118 Class2 *C2 = Class2::create(0, 4.2);
120 EXPECT_EQ(sizeof(Class2), 8u); // due to alignment
122 EXPECT_EQ((Class2::additionalSizeToAlloc<double, short>(1, 0)),
124 EXPECT_EQ((Class2::additionalSizeToAlloc<double, short>(0, 1)),
126 EXPECT_EQ((Class2::additionalSizeToAlloc<double, short>(3, 1)),
127 sizeof(double) * 3 + sizeof(short));
129 EXPECT_EQ((Class2::totalSizeToAlloc<double, short>(1, 1)),
130 sizeof(Class2) + sizeof(double) + sizeof(short));
132 EXPECT_EQ(C1->getDouble(), 0);
133 EXPECT_EQ(C1->getShort(), 4);
134 EXPECT_EQ(C1->getTrailingObjects<double>(),
135 reinterpret_cast<double *>(C1 + 1));
136 EXPECT_EQ(C1->getTrailingObjects<short>(), reinterpret_cast<short *>(C1 + 1));
138 EXPECT_EQ(C2->getDouble(), 4.2);
139 EXPECT_EQ(C2->getShort(), 0);
140 EXPECT_EQ(C2->getTrailingObjects<double>(),
141 reinterpret_cast<double *>(C2 + 1));
142 EXPECT_EQ(C2->getTrailingObjects<short>(),
143 reinterpret_cast<short *>(reinterpret_cast<double *>(C2 + 1) + 1));