TrailingObjectsTest.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //=== - llvm/unittest/Support/TrailingObjectsTest.cpp ---------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/Support/TrailingObjects.h"
  10. #include "gtest/gtest.h"
  11. using namespace llvm;
  12. namespace {
  13. // This class, beyond being used by the test case, a nice
  14. // demonstration of the intended usage of TrailingObjects, with a
  15. // single trailing array.
  16. class Class1 final : protected TrailingObjects<Class1, short> {
  17. friend TrailingObjects;
  18. unsigned NumShorts;
  19. protected:
  20. size_t numTrailingObjects(OverloadToken<short>) const { return NumShorts; }
  21. Class1(int *ShortArray, unsigned NumShorts) : NumShorts(NumShorts) {
  22. std::uninitialized_copy(ShortArray, ShortArray + NumShorts,
  23. getTrailingObjects<short>());
  24. }
  25. public:
  26. static Class1 *create(int *ShortArray, unsigned NumShorts) {
  27. void *Mem = ::operator new(totalSizeToAlloc<short>(NumShorts));
  28. return new (Mem) Class1(ShortArray, NumShorts);
  29. }
  30. using TrailingObjects::operator delete;
  31. short get(unsigned Num) const { return getTrailingObjects<short>()[Num]; }
  32. unsigned numShorts() const { return NumShorts; }
  33. // Pull some protected members in as public, for testability.
  34. using TrailingObjects::totalSizeToAlloc;
  35. using TrailingObjects::additionalSizeToAlloc;
  36. using TrailingObjects::getTrailingObjects;
  37. };
  38. // Here, there are two singular optional object types appended. Note
  39. // that the alignment of Class2 is automatically increased to account
  40. // for the alignment requirements of the trailing objects.
  41. class Class2 final : protected TrailingObjects<Class2, double, short> {
  42. friend TrailingObjects;
  43. bool HasShort, HasDouble;
  44. protected:
  45. size_t numTrailingObjects(OverloadToken<short>) const {
  46. return HasShort ? 1 : 0;
  47. }
  48. size_t numTrailingObjects(OverloadToken<double>) const {
  49. return HasDouble ? 1 : 0;
  50. }
  51. Class2(bool HasShort, bool HasDouble)
  52. : HasShort(HasShort), HasDouble(HasDouble) {}
  53. public:
  54. static Class2 *create(short S = 0, double D = 0.0) {
  55. bool HasShort = S != 0;
  56. bool HasDouble = D != 0.0;
  57. void *Mem =
  58. ::operator new(totalSizeToAlloc<double, short>(HasDouble, HasShort));
  59. Class2 *C = new (Mem) Class2(HasShort, HasDouble);
  60. if (HasShort)
  61. *C->getTrailingObjects<short>() = S;
  62. if (HasDouble)
  63. *C->getTrailingObjects<double>() = D;
  64. return C;
  65. }
  66. using TrailingObjects::operator delete;
  67. short getShort() const {
  68. if (!HasShort)
  69. return 0;
  70. return *getTrailingObjects<short>();
  71. }
  72. double getDouble() const {
  73. if (!HasDouble)
  74. return 0.0;
  75. return *getTrailingObjects<double>();
  76. }
  77. // Pull some protected members in as public, for testability.
  78. using TrailingObjects::totalSizeToAlloc;
  79. using TrailingObjects::additionalSizeToAlloc;
  80. using TrailingObjects::getTrailingObjects;
  81. };
  82. TEST(TrailingObjects, OneArg) {
  83. int arr[] = {1, 2, 3};
  84. Class1 *C = Class1::create(arr, 3);
  85. EXPECT_EQ(sizeof(Class1), sizeof(unsigned));
  86. EXPECT_EQ(Class1::additionalSizeToAlloc<short>(1), sizeof(short));
  87. EXPECT_EQ(Class1::additionalSizeToAlloc<short>(3), sizeof(short) * 3);
  88. EXPECT_EQ(Class1::totalSizeToAlloc<short>(1), sizeof(Class1) + sizeof(short));
  89. EXPECT_EQ(Class1::totalSizeToAlloc<short>(3),
  90. sizeof(Class1) + sizeof(short) * 3);
  91. EXPECT_EQ(C->getTrailingObjects<short>(), reinterpret_cast<short *>(C + 1));
  92. EXPECT_EQ(C->get(0), 1);
  93. EXPECT_EQ(C->get(2), 3);
  94. delete C;
  95. }
  96. TEST(TrailingObjects, TwoArg) {
  97. Class2 *C1 = Class2::create(4);
  98. Class2 *C2 = Class2::create(0, 4.2);
  99. EXPECT_EQ(sizeof(Class2),
  100. llvm::alignTo(sizeof(bool) * 2, llvm::alignOf<double>()));
  101. EXPECT_EQ(llvm::alignOf<Class2>(), llvm::alignOf<double>());
  102. EXPECT_EQ((Class2::additionalSizeToAlloc<double, short>(1, 0)),
  103. sizeof(double));
  104. EXPECT_EQ((Class2::additionalSizeToAlloc<double, short>(0, 1)),
  105. sizeof(short));
  106. EXPECT_EQ((Class2::additionalSizeToAlloc<double, short>(3, 1)),
  107. sizeof(double) * 3 + sizeof(short));
  108. EXPECT_EQ((Class2::totalSizeToAlloc<double, short>(1, 1)),
  109. sizeof(Class2) + sizeof(double) + sizeof(short));
  110. EXPECT_EQ(C1->getDouble(), 0);
  111. EXPECT_EQ(C1->getShort(), 4);
  112. EXPECT_EQ(C1->getTrailingObjects<double>(),
  113. reinterpret_cast<double *>(C1 + 1));
  114. EXPECT_EQ(C1->getTrailingObjects<short>(), reinterpret_cast<short *>(C1 + 1));
  115. EXPECT_EQ(C2->getDouble(), 4.2);
  116. EXPECT_EQ(C2->getShort(), 0);
  117. EXPECT_EQ(C2->getTrailingObjects<double>(),
  118. reinterpret_cast<double *>(C2 + 1));
  119. EXPECT_EQ(C2->getTrailingObjects<short>(),
  120. reinterpret_cast<short *>(reinterpret_cast<double *>(C2 + 1) + 1));
  121. delete C1;
  122. delete C2;
  123. }
  124. // This test class is not trying to be a usage demo, just asserting
  125. // that three args does actually work too (it's the same code as
  126. // handles the second arg, so it's basically covered by the above, but
  127. // just in case..)
  128. class Class3 final : public TrailingObjects<Class3, double, short, bool> {
  129. friend TrailingObjects;
  130. size_t numTrailingObjects(OverloadToken<double>) const { return 1; }
  131. size_t numTrailingObjects(OverloadToken<short>) const { return 1; }
  132. };
  133. TEST(TrailingObjects, ThreeArg) {
  134. EXPECT_EQ((Class3::additionalSizeToAlloc<double, short, bool>(1, 1, 3)),
  135. sizeof(double) + sizeof(short) + 3 * sizeof(bool));
  136. EXPECT_EQ(sizeof(Class3), llvm::alignTo(1, llvm::alignOf<double>()));
  137. std::unique_ptr<char[]> P(new char[1000]);
  138. Class3 *C = reinterpret_cast<Class3 *>(P.get());
  139. EXPECT_EQ(C->getTrailingObjects<double>(), reinterpret_cast<double *>(C + 1));
  140. EXPECT_EQ(C->getTrailingObjects<short>(),
  141. reinterpret_cast<short *>(reinterpret_cast<double *>(C + 1) + 1));
  142. EXPECT_EQ(
  143. C->getTrailingObjects<bool>(),
  144. reinterpret_cast<bool *>(
  145. reinterpret_cast<short *>(reinterpret_cast<double *>(C + 1) + 1) +
  146. 1));
  147. }
  148. class Class4 final : public TrailingObjects<Class4, char, long> {
  149. friend TrailingObjects;
  150. size_t numTrailingObjects(OverloadToken<char>) const { return 1; }
  151. };
  152. TEST(TrailingObjects, Realignment) {
  153. EXPECT_EQ((Class4::additionalSizeToAlloc<char, long>(1, 1)),
  154. llvm::alignTo(sizeof(long) + 1, llvm::alignOf<long>()));
  155. EXPECT_EQ(sizeof(Class4), llvm::alignTo(1, llvm::alignOf<long>()));
  156. std::unique_ptr<char[]> P(new char[1000]);
  157. Class4 *C = reinterpret_cast<Class4 *>(P.get());
  158. EXPECT_EQ(C->getTrailingObjects<char>(), reinterpret_cast<char *>(C + 1));
  159. EXPECT_EQ(C->getTrailingObjects<long>(),
  160. reinterpret_cast<long *>(llvm::alignAddr(
  161. reinterpret_cast<char *>(C + 1) + 1, llvm::alignOf<long>())));
  162. }
  163. }