ArrayRefTest.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. //===- llvm/unittest/ADT/ArrayRefTest.cpp - ArrayRef unit tests -----------===//
  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/ADT/ArrayRef.h"
  10. #include "llvm/Support/Allocator.h"
  11. #include "llvm/Support/raw_ostream.h"
  12. #include "gtest/gtest.h"
  13. #include <limits>
  14. #include <vector>
  15. using namespace llvm;
  16. // Check that the ArrayRef-of-pointer converting constructor only allows adding
  17. // cv qualifiers (not removing them, or otherwise changing the type)
  18. static_assert(
  19. std::is_convertible<ArrayRef<int *>, ArrayRef<const int *>>::value,
  20. "Adding const");
  21. static_assert(
  22. std::is_convertible<ArrayRef<int *>, ArrayRef<volatile int *>>::value,
  23. "Adding volatile");
  24. static_assert(!std::is_convertible<ArrayRef<int *>, ArrayRef<float *>>::value,
  25. "Changing pointer of one type to a pointer of another");
  26. static_assert(
  27. !std::is_convertible<ArrayRef<const int *>, ArrayRef<int *>>::value,
  28. "Removing const");
  29. static_assert(
  30. !std::is_convertible<ArrayRef<volatile int *>, ArrayRef<int *>>::value,
  31. "Removing volatile");
  32. // Check that we can't accidentally assign a temporary location to an ArrayRef.
  33. // (Unfortunately we can't make use of the same thing with constructors.)
  34. //
  35. // Disable this check under MSVC; even MSVC 2015 isn't inconsistent between
  36. // std::is_assignable and actually writing such an assignment.
  37. #if !defined(_MSC_VER)
  38. static_assert(
  39. !std::is_assignable<ArrayRef<int *>&, int *>::value,
  40. "Assigning from single prvalue element");
  41. static_assert(
  42. !std::is_assignable<ArrayRef<int *>&, int * &&>::value,
  43. "Assigning from single xvalue element");
  44. static_assert(
  45. std::is_assignable<ArrayRef<int *>&, int * &>::value,
  46. "Assigning from single lvalue element");
  47. static_assert(
  48. !std::is_assignable<ArrayRef<int *>&, std::initializer_list<int *>>::value,
  49. "Assigning from an initializer list");
  50. #endif
  51. namespace {
  52. TEST(ArrayRefTest, AllocatorCopy) {
  53. BumpPtrAllocator Alloc;
  54. static const uint16_t Words1[] = { 1, 4, 200, 37 };
  55. ArrayRef<uint16_t> Array1 = makeArrayRef(Words1, 4);
  56. static const uint16_t Words2[] = { 11, 4003, 67, 64000, 13 };
  57. ArrayRef<uint16_t> Array2 = makeArrayRef(Words2, 5);
  58. ArrayRef<uint16_t> Array1c = Array1.copy(Alloc);
  59. ArrayRef<uint16_t> Array2c = Array2.copy(Alloc);
  60. EXPECT_TRUE(Array1.equals(Array1c));
  61. EXPECT_NE(Array1.data(), Array1c.data());
  62. EXPECT_TRUE(Array2.equals(Array2c));
  63. EXPECT_NE(Array2.data(), Array2c.data());
  64. // Check that copy can cope with uninitialized memory.
  65. struct NonAssignable {
  66. const char *Ptr;
  67. NonAssignable(const char *Ptr) : Ptr(Ptr) {}
  68. NonAssignable(const NonAssignable &RHS) = default;
  69. void operator=(const NonAssignable &RHS) { assert(RHS.Ptr != nullptr); }
  70. bool operator==(const NonAssignable &RHS) const { return Ptr == RHS.Ptr; }
  71. } Array3Src[] = {"hello", "world"};
  72. ArrayRef<NonAssignable> Array3Copy = makeArrayRef(Array3Src).copy(Alloc);
  73. EXPECT_EQ(makeArrayRef(Array3Src), Array3Copy);
  74. EXPECT_NE(makeArrayRef(Array3Src).data(), Array3Copy.data());
  75. }
  76. TEST(ArrayRefTest, SizeTSizedOperations) {
  77. ArrayRef<char> AR(nullptr, std::numeric_limits<ptrdiff_t>::max());
  78. // Check that drop_back accepts size_t-sized numbers.
  79. EXPECT_EQ(1U, AR.drop_back(AR.size() - 1).size());
  80. // Check that drop_front accepts size_t-sized numbers.
  81. EXPECT_EQ(1U, AR.drop_front(AR.size() - 1).size());
  82. // Check that slice accepts size_t-sized numbers.
  83. EXPECT_EQ(1U, AR.slice(AR.size() - 1).size());
  84. EXPECT_EQ(AR.size() - 1, AR.slice(1, AR.size() - 1).size());
  85. }
  86. TEST(ArrayRefTest, DropBack) {
  87. static const int TheNumbers[] = {4, 8, 15, 16, 23, 42};
  88. ArrayRef<int> AR1(TheNumbers);
  89. ArrayRef<int> AR2(TheNumbers, AR1.size() - 1);
  90. EXPECT_TRUE(AR1.drop_back().equals(AR2));
  91. }
  92. TEST(ArrayRefTest, DropFront) {
  93. static const int TheNumbers[] = {4, 8, 15, 16, 23, 42};
  94. ArrayRef<int> AR1(TheNumbers);
  95. ArrayRef<int> AR2(&TheNumbers[2], AR1.size() - 2);
  96. EXPECT_TRUE(AR1.drop_front(2).equals(AR2));
  97. }
  98. TEST(ArrayRefTest, DropWhile) {
  99. static const int TheNumbers[] = {1, 3, 5, 8, 10, 11};
  100. ArrayRef<int> AR1(TheNumbers);
  101. ArrayRef<int> Expected = AR1.drop_front(3);
  102. EXPECT_EQ(Expected, AR1.drop_while([](const int &N) { return N % 2 == 1; }));
  103. EXPECT_EQ(AR1, AR1.drop_while([](const int &N) { return N < 0; }));
  104. EXPECT_EQ(ArrayRef<int>(),
  105. AR1.drop_while([](const int &N) { return N > 0; }));
  106. }
  107. TEST(ArrayRefTest, DropUntil) {
  108. static const int TheNumbers[] = {1, 3, 5, 8, 10, 11};
  109. ArrayRef<int> AR1(TheNumbers);
  110. ArrayRef<int> Expected = AR1.drop_front(3);
  111. EXPECT_EQ(Expected, AR1.drop_until([](const int &N) { return N % 2 == 0; }));
  112. EXPECT_EQ(ArrayRef<int>(),
  113. AR1.drop_until([](const int &N) { return N < 0; }));
  114. EXPECT_EQ(AR1, AR1.drop_until([](const int &N) { return N > 0; }));
  115. }
  116. TEST(ArrayRefTest, TakeBack) {
  117. static const int TheNumbers[] = {4, 8, 15, 16, 23, 42};
  118. ArrayRef<int> AR1(TheNumbers);
  119. ArrayRef<int> AR2(AR1.end() - 1, 1);
  120. EXPECT_TRUE(AR1.take_back().equals(AR2));
  121. }
  122. TEST(ArrayRefTest, TakeFront) {
  123. static const int TheNumbers[] = {4, 8, 15, 16, 23, 42};
  124. ArrayRef<int> AR1(TheNumbers);
  125. ArrayRef<int> AR2(AR1.data(), 2);
  126. EXPECT_TRUE(AR1.take_front(2).equals(AR2));
  127. }
  128. TEST(ArrayRefTest, TakeWhile) {
  129. static const int TheNumbers[] = {1, 3, 5, 8, 10, 11};
  130. ArrayRef<int> AR1(TheNumbers);
  131. ArrayRef<int> Expected = AR1.take_front(3);
  132. EXPECT_EQ(Expected, AR1.take_while([](const int &N) { return N % 2 == 1; }));
  133. EXPECT_EQ(ArrayRef<int>(),
  134. AR1.take_while([](const int &N) { return N < 0; }));
  135. EXPECT_EQ(AR1, AR1.take_while([](const int &N) { return N > 0; }));
  136. }
  137. TEST(ArrayRefTest, TakeUntil) {
  138. static const int TheNumbers[] = {1, 3, 5, 8, 10, 11};
  139. ArrayRef<int> AR1(TheNumbers);
  140. ArrayRef<int> Expected = AR1.take_front(3);
  141. EXPECT_EQ(Expected, AR1.take_until([](const int &N) { return N % 2 == 0; }));
  142. EXPECT_EQ(AR1, AR1.take_until([](const int &N) { return N < 0; }));
  143. EXPECT_EQ(ArrayRef<int>(),
  144. AR1.take_until([](const int &N) { return N > 0; }));
  145. }
  146. TEST(ArrayRefTest, Equals) {
  147. static const int A1[] = {1, 2, 3, 4, 5, 6, 7, 8};
  148. ArrayRef<int> AR1(A1);
  149. EXPECT_TRUE(AR1.equals({1, 2, 3, 4, 5, 6, 7, 8}));
  150. EXPECT_FALSE(AR1.equals({8, 1, 2, 4, 5, 6, 6, 7}));
  151. EXPECT_FALSE(AR1.equals({2, 4, 5, 6, 6, 7, 8, 1}));
  152. EXPECT_FALSE(AR1.equals({0, 1, 2, 4, 5, 6, 6, 7}));
  153. EXPECT_FALSE(AR1.equals({1, 2, 42, 4, 5, 6, 7, 8}));
  154. EXPECT_FALSE(AR1.equals({42, 2, 3, 4, 5, 6, 7, 8}));
  155. EXPECT_FALSE(AR1.equals({1, 2, 3, 4, 5, 6, 7, 42}));
  156. EXPECT_FALSE(AR1.equals({1, 2, 3, 4, 5, 6, 7}));
  157. EXPECT_FALSE(AR1.equals({1, 2, 3, 4, 5, 6, 7, 8, 9}));
  158. ArrayRef<int> AR1a = AR1.drop_back();
  159. EXPECT_TRUE(AR1a.equals({1, 2, 3, 4, 5, 6, 7}));
  160. EXPECT_FALSE(AR1a.equals({1, 2, 3, 4, 5, 6, 7, 8}));
  161. ArrayRef<int> AR1b = AR1a.slice(2, 4);
  162. EXPECT_TRUE(AR1b.equals({3, 4, 5, 6}));
  163. EXPECT_FALSE(AR1b.equals({2, 3, 4, 5, 6}));
  164. EXPECT_FALSE(AR1b.equals({3, 4, 5, 6, 7}));
  165. SmallVector<int, 8> V1{1, 2, 3, 4, 5, 6, 7, 8};
  166. EXPECT_EQ(AR1, V1);
  167. EXPECT_EQ(V1, AR1);
  168. }
  169. TEST(ArrayRefTest, EmptyEquals) {
  170. EXPECT_TRUE(ArrayRef<unsigned>() == ArrayRef<unsigned>());
  171. }
  172. TEST(ArrayRefTest, ConstConvert) {
  173. int buf[4];
  174. for (int i = 0; i < 4; ++i)
  175. buf[i] = i;
  176. static int *A[] = {&buf[0], &buf[1], &buf[2], &buf[3]};
  177. ArrayRef<const int *> a((ArrayRef<int *>(A)));
  178. a = ArrayRef<int *>(A);
  179. }
  180. static std::vector<int> ReturnTest12() { return {1, 2}; }
  181. static void ArgTest12(ArrayRef<int> A) {
  182. EXPECT_EQ(2U, A.size());
  183. EXPECT_EQ(1, A[0]);
  184. EXPECT_EQ(2, A[1]);
  185. }
  186. TEST(ArrayRefTest, InitializerList) {
  187. std::initializer_list<int> init_list = { 0, 1, 2, 3, 4 };
  188. ArrayRef<int> A = init_list;
  189. for (int i = 0; i < 5; ++i)
  190. EXPECT_EQ(i, A[i]);
  191. std::vector<int> B = ReturnTest12();
  192. A = B;
  193. EXPECT_EQ(1, A[0]);
  194. EXPECT_EQ(2, A[1]);
  195. ArgTest12({1, 2});
  196. }
  197. TEST(ArrayRefTest, EmptyInitializerList) {
  198. ArrayRef<int> A = {};
  199. EXPECT_TRUE(A.empty());
  200. A = {};
  201. EXPECT_TRUE(A.empty());
  202. }
  203. // Test that makeArrayRef works on ArrayRef (no-op)
  204. TEST(ArrayRefTest, makeArrayRef) {
  205. static const int A1[] = {1, 2, 3, 4, 5, 6, 7, 8};
  206. // No copy expected for non-const ArrayRef (true no-op)
  207. ArrayRef<int> AR1(A1);
  208. ArrayRef<int> &AR1Ref = makeArrayRef(AR1);
  209. EXPECT_EQ(&AR1, &AR1Ref);
  210. // A copy is expected for non-const ArrayRef (thin copy)
  211. const ArrayRef<int> AR2(A1);
  212. const ArrayRef<int> &AR2Ref = makeArrayRef(AR2);
  213. EXPECT_NE(&AR2Ref, &AR2);
  214. EXPECT_TRUE(AR2.equals(AR2Ref));
  215. }
  216. } // end anonymous namespace