TrailingObjectsTest.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. short get(unsigned Num) const { return getTrailingObjects<short>()[Num]; }
  31. unsigned numShorts() const { return NumShorts; }
  32. // Pull some protected members in as public, for testability.
  33. using TrailingObjects::totalSizeToAlloc;
  34. using TrailingObjects::additionalSizeToAlloc;
  35. using TrailingObjects::getTrailingObjects;
  36. };
  37. // Here, there are two singular optional object types appended. Note
  38. // that the alignment of Class2 is automatically increased to account
  39. // for the alignment requirements of the trailing objects.
  40. class Class2 final : protected TrailingObjects<Class2, double, short> {
  41. friend TrailingObjects;
  42. bool HasShort, HasDouble;
  43. protected:
  44. size_t numTrailingObjects(OverloadToken<short>) const {
  45. return HasShort ? 1 : 0;
  46. }
  47. size_t numTrailingObjects(OverloadToken<double>) const {
  48. return HasDouble ? 1 : 0;
  49. }
  50. Class2(bool HasShort, bool HasDouble)
  51. : HasShort(HasShort), HasDouble(HasDouble) {}
  52. public:
  53. static Class2 *create(short S = 0, double D = 0.0) {
  54. bool HasShort = S != 0;
  55. bool HasDouble = D != 0.0;
  56. void *Mem =
  57. ::operator new(totalSizeToAlloc<double, short>(HasDouble, HasShort));
  58. Class2 *C = new (Mem) Class2(HasShort, HasDouble);
  59. if (HasShort)
  60. *C->getTrailingObjects<short>() = S;
  61. if (HasDouble)
  62. *C->getTrailingObjects<double>() = D;
  63. return C;
  64. }
  65. short getShort() const {
  66. if (!HasShort)
  67. return 0;
  68. return *getTrailingObjects<short>();
  69. }
  70. double getDouble() const {
  71. if (!HasDouble)
  72. return 0.0;
  73. return *getTrailingObjects<double>();
  74. }
  75. // Pull some protected members in as public, for testability.
  76. using TrailingObjects::totalSizeToAlloc;
  77. using TrailingObjects::additionalSizeToAlloc;
  78. using TrailingObjects::getTrailingObjects;
  79. };
  80. TEST(TrailingObjects, OneArg) {
  81. int arr[] = {1, 2, 3};
  82. Class1 *C = Class1::create(arr, 3);
  83. EXPECT_EQ(sizeof(Class1), sizeof(unsigned));
  84. EXPECT_EQ(Class1::additionalSizeToAlloc<short>(1), sizeof(short));
  85. EXPECT_EQ(Class1::additionalSizeToAlloc<short>(3), sizeof(short) * 3);
  86. EXPECT_EQ(Class1::totalSizeToAlloc<short>(1), sizeof(Class1) + sizeof(short));
  87. EXPECT_EQ(Class1::totalSizeToAlloc<short>(3),
  88. sizeof(Class1) + sizeof(short) * 3);
  89. EXPECT_EQ(C->getTrailingObjects<short>(), reinterpret_cast<short *>(C + 1));
  90. EXPECT_EQ(C->get(0), 1);
  91. EXPECT_EQ(C->get(2), 3);
  92. delete C;
  93. }
  94. TEST(TrailingObjects, TwoArg) {
  95. Class2 *C1 = Class2::create(4);
  96. Class2 *C2 = Class2::create(0, 4.2);
  97. EXPECT_EQ(sizeof(Class2), llvm::RoundUpToAlignment(sizeof(bool) * 2,
  98. llvm::alignOf<double>()));
  99. EXPECT_EQ(llvm::alignOf<Class2>(), llvm::alignOf<double>());
  100. EXPECT_EQ((Class2::additionalSizeToAlloc<double, short>(1, 0)),
  101. sizeof(double));
  102. EXPECT_EQ((Class2::additionalSizeToAlloc<double, short>(0, 1)),
  103. sizeof(short));
  104. EXPECT_EQ((Class2::additionalSizeToAlloc<double, short>(3, 1)),
  105. sizeof(double) * 3 + sizeof(short));
  106. EXPECT_EQ((Class2::totalSizeToAlloc<double, short>(1, 1)),
  107. sizeof(Class2) + sizeof(double) + sizeof(short));
  108. EXPECT_EQ(C1->getDouble(), 0);
  109. EXPECT_EQ(C1->getShort(), 4);
  110. EXPECT_EQ(C1->getTrailingObjects<double>(),
  111. reinterpret_cast<double *>(C1 + 1));
  112. EXPECT_EQ(C1->getTrailingObjects<short>(), reinterpret_cast<short *>(C1 + 1));
  113. EXPECT_EQ(C2->getDouble(), 4.2);
  114. EXPECT_EQ(C2->getShort(), 0);
  115. EXPECT_EQ(C2->getTrailingObjects<double>(),
  116. reinterpret_cast<double *>(C2 + 1));
  117. EXPECT_EQ(C2->getTrailingObjects<short>(),
  118. reinterpret_cast<short *>(reinterpret_cast<double *>(C2 + 1) + 1));
  119. delete C1;
  120. delete C2;
  121. }
  122. // This test class is not trying to be a usage demo, just asserting
  123. // that three args does actually work too (it's the same code as
  124. // handles the second arg, so it's basically covered by the above, but
  125. // just in case..)
  126. class Class3 final : public TrailingObjects<Class3, double, short, bool> {
  127. friend TrailingObjects;
  128. size_t numTrailingObjects(OverloadToken<double>) const { return 1; }
  129. size_t numTrailingObjects(OverloadToken<short>) const { return 1; }
  130. };
  131. TEST(TrailingObjects, ThreeArg) {
  132. EXPECT_EQ((Class3::additionalSizeToAlloc<double, short, bool>(1, 1, 3)),
  133. sizeof(double) + sizeof(short) + 3 * sizeof(bool));
  134. EXPECT_EQ(sizeof(Class3),
  135. llvm::RoundUpToAlignment(1, llvm::alignOf<double>()));
  136. Class3 *C = reinterpret_cast<Class3 *>(::operator new(1000));
  137. EXPECT_EQ(C->getTrailingObjects<double>(), reinterpret_cast<double *>(C + 1));
  138. EXPECT_EQ(C->getTrailingObjects<short>(),
  139. reinterpret_cast<short *>(reinterpret_cast<double *>(C + 1) + 1));
  140. EXPECT_EQ(
  141. C->getTrailingObjects<bool>(),
  142. reinterpret_cast<bool *>(
  143. reinterpret_cast<short *>(reinterpret_cast<double *>(C + 1) + 1) +
  144. 1));
  145. }
  146. }