Rewrite the TrailingObjects template to provide two new features:
[oota-llvm.git] / unittests / Support / TrailingObjectsTest.cpp
index 41e2d41..2b8ab4c 100644 (file)
@@ -45,9 +45,10 @@ public:
   using TrailingObjects::getTrailingObjects;
 };
 
-// Here, there are two singular optional object types appended.
-// Note that it fails to compile without the alignment spec.
-class LLVM_ALIGNAS(8) Class2 final : protected TrailingObjects<Class2, double, short> {
+// Here, there are two singular optional object types appended.  Note
+// that the alignment of Class2 is automatically increased to account
+// for the alignment requirements of the trailing objects.
+class Class2 final : protected TrailingObjects<Class2, double, short> {
   friend TrailingObjects;
 
   bool HasShort, HasDouble;
@@ -117,7 +118,9 @@ TEST(TrailingObjects, TwoArg) {
   Class2 *C1 = Class2::create(4);
   Class2 *C2 = Class2::create(0, 4.2);
 
-  EXPECT_EQ(sizeof(Class2), 8u); // due to alignment
+  EXPECT_EQ(sizeof(Class2), llvm::RoundUpToAlignment(sizeof(bool) * 2,
+                                                     llvm::alignOf<double>()));
+  EXPECT_EQ(llvm::alignOf<Class2>(), llvm::alignOf<double>());
 
   EXPECT_EQ((Class2::additionalSizeToAlloc<double, short>(1, 0)),
             sizeof(double));
@@ -144,4 +147,31 @@ TEST(TrailingObjects, TwoArg) {
   delete C1;
   delete C2;
 }
+
+// This test class is not trying to be a usage demo, just asserting
+// that three args does actually work too (it's the same code as
+// handles the second arg, so it's basically covered by the above, but
+// just in case..)
+class Class3 final : public TrailingObjects<Class3, double, short, bool> {
+  friend TrailingObjects;
+
+  size_t numTrailingObjects(OverloadToken<double>) const { return 1; }
+  size_t numTrailingObjects(OverloadToken<short>) const { return 1; }
+};
+
+TEST(TrailingObjects, ThreeArg) {
+  EXPECT_EQ((Class3::additionalSizeToAlloc<double, short, bool>(1, 1, 3)),
+            sizeof(double) + sizeof(short) + 3 * sizeof(bool));
+  EXPECT_EQ(sizeof(Class3),
+            llvm::RoundUpToAlignment(1, llvm::alignOf<double>()));
+  Class3 *C = reinterpret_cast<Class3 *>(::operator new(1000));
+  EXPECT_EQ(C->getTrailingObjects<double>(), reinterpret_cast<double *>(C + 1));
+  EXPECT_EQ(C->getTrailingObjects<short>(),
+            reinterpret_cast<short *>(reinterpret_cast<double *>(C + 1) + 1));
+  EXPECT_EQ(
+      C->getTrailingObjects<bool>(),
+      reinterpret_cast<bool *>(
+          reinterpret_cast<short *>(reinterpret_cast<double *>(C + 1) + 1) +
+          1));
+}
 }