TinyPtrVectorTest.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. //===- llvm/unittest/ADT/TinyPtrVectorTest.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. //
  10. // TinyPtrVector unit tests.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/ADT/TinyPtrVector.h"
  14. #include "llvm/ADT/ArrayRef.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/Support/type_traits.h"
  18. #include "gtest/gtest.h"
  19. #include <algorithm>
  20. #include <list>
  21. #include <vector>
  22. using namespace llvm;
  23. namespace {
  24. // The world's worst RNG, but it is deterministic and makes it easy to get
  25. // *some* shuffling of elements.
  26. static ptrdiff_t test_shuffle_rng(ptrdiff_t i) {
  27. return (i + i * 33) % i;
  28. }
  29. static ptrdiff_t (*test_shuffle_rng_p)(ptrdiff_t) = &test_shuffle_rng;
  30. template <typename VectorT>
  31. class TinyPtrVectorTest : public testing::Test {
  32. protected:
  33. typedef typename VectorT::value_type PtrT;
  34. typedef typename remove_pointer<PtrT>::type ValueT;
  35. VectorT V;
  36. VectorT V2;
  37. ValueT TestValues[1024];
  38. std::vector<PtrT> TestPtrs;
  39. TinyPtrVectorTest() {
  40. for (size_t i = 0, e = array_lengthof(TestValues); i != e; ++i)
  41. TestPtrs.push_back(&TestValues[i]);
  42. std::random_shuffle(TestPtrs.begin(), TestPtrs.end(), test_shuffle_rng_p);
  43. }
  44. ArrayRef<PtrT> testArray(size_t N) {
  45. return makeArrayRef(&TestPtrs[0], N);
  46. }
  47. void appendValues(VectorT &V, ArrayRef<PtrT> Values) {
  48. for (size_t i = 0, e = Values.size(); i != e; ++i)
  49. V.push_back(Values[i]);
  50. }
  51. void setVectors(ArrayRef<PtrT> Values1, ArrayRef<PtrT> Values2) {
  52. V.clear();
  53. appendValues(V, Values1);
  54. V2.clear();
  55. appendValues(V2, Values2);
  56. }
  57. void expectValues(const VectorT &V, ArrayRef<PtrT> Values) {
  58. EXPECT_EQ(Values.empty(), V.empty());
  59. EXPECT_EQ(Values.size(), V.size());
  60. for (size_t i = 0, e = Values.size(); i != e; ++i) {
  61. EXPECT_EQ(Values[i], V[i]);
  62. EXPECT_EQ(Values[i], *llvm::next(V.begin(), i));
  63. }
  64. EXPECT_EQ(V.end(), llvm::next(V.begin(), Values.size()));
  65. }
  66. };
  67. typedef ::testing::Types<TinyPtrVector<int*>,
  68. TinyPtrVector<double*>
  69. > TinyPtrVectorTestTypes;
  70. TYPED_TEST_CASE(TinyPtrVectorTest, TinyPtrVectorTestTypes);
  71. TYPED_TEST(TinyPtrVectorTest, EmptyTest) {
  72. this->expectValues(this->V, this->testArray(0));
  73. }
  74. TYPED_TEST(TinyPtrVectorTest, PushPopBack) {
  75. this->V.push_back(this->TestPtrs[0]);
  76. this->expectValues(this->V, this->testArray(1));
  77. this->V.push_back(this->TestPtrs[1]);
  78. this->expectValues(this->V, this->testArray(2));
  79. this->V.push_back(this->TestPtrs[2]);
  80. this->expectValues(this->V, this->testArray(3));
  81. this->V.push_back(this->TestPtrs[3]);
  82. this->expectValues(this->V, this->testArray(4));
  83. this->V.push_back(this->TestPtrs[4]);
  84. this->expectValues(this->V, this->testArray(5));
  85. // Pop and clobber a few values to keep things interesting.
  86. this->V.pop_back();
  87. this->expectValues(this->V, this->testArray(4));
  88. this->V.pop_back();
  89. this->expectValues(this->V, this->testArray(3));
  90. this->TestPtrs[3] = &this->TestValues[42];
  91. this->TestPtrs[4] = &this->TestValues[43];
  92. this->V.push_back(this->TestPtrs[3]);
  93. this->expectValues(this->V, this->testArray(4));
  94. this->V.push_back(this->TestPtrs[4]);
  95. this->expectValues(this->V, this->testArray(5));
  96. this->V.pop_back();
  97. this->expectValues(this->V, this->testArray(4));
  98. this->V.pop_back();
  99. this->expectValues(this->V, this->testArray(3));
  100. this->V.pop_back();
  101. this->expectValues(this->V, this->testArray(2));
  102. this->V.pop_back();
  103. this->expectValues(this->V, this->testArray(1));
  104. this->V.pop_back();
  105. this->expectValues(this->V, this->testArray(0));
  106. this->appendValues(this->V, this->testArray(42));
  107. this->expectValues(this->V, this->testArray(42));
  108. }
  109. TYPED_TEST(TinyPtrVectorTest, ClearTest) {
  110. this->expectValues(this->V, this->testArray(0));
  111. this->V.clear();
  112. this->expectValues(this->V, this->testArray(0));
  113. this->appendValues(this->V, this->testArray(1));
  114. this->expectValues(this->V, this->testArray(1));
  115. this->V.clear();
  116. this->expectValues(this->V, this->testArray(0));
  117. this->appendValues(this->V, this->testArray(42));
  118. this->expectValues(this->V, this->testArray(42));
  119. this->V.clear();
  120. this->expectValues(this->V, this->testArray(0));
  121. }
  122. TYPED_TEST(TinyPtrVectorTest, CopyAndMoveCtorTest) {
  123. this->appendValues(this->V, this->testArray(42));
  124. TypeParam Copy(this->V);
  125. this->expectValues(Copy, this->testArray(42));
  126. // This is a separate copy, and so it shouldn't destroy the original.
  127. Copy.clear();
  128. this->expectValues(Copy, this->testArray(0));
  129. this->expectValues(this->V, this->testArray(42));
  130. TypeParam Copy2(this->V2);
  131. this->appendValues(Copy2, this->testArray(42));
  132. this->expectValues(Copy2, this->testArray(42));
  133. this->expectValues(this->V2, this->testArray(0));
  134. #if LLVM_HAS_RVALUE_REFERENCES
  135. TypeParam Move(std::move(Copy2));
  136. this->expectValues(Move, this->testArray(42));
  137. this->expectValues(Copy2, this->testArray(0));
  138. #endif
  139. }
  140. TYPED_TEST(TinyPtrVectorTest, CopyAndMoveTest) {
  141. this->V = this->V2;
  142. this->expectValues(this->V, this->testArray(0));
  143. this->expectValues(this->V2, this->testArray(0));
  144. #if LLVM_HAS_RVALUE_REFERENCES
  145. this->V = std::move(this->V2);
  146. this->expectValues(this->V, this->testArray(0));
  147. #endif
  148. this->setVectors(this->testArray(1), this->testArray(0));
  149. this->V = this->V2;
  150. this->expectValues(this->V, this->testArray(0));
  151. this->expectValues(this->V2, this->testArray(0));
  152. #if LLVM_HAS_RVALUE_REFERENCES
  153. this->setVectors(this->testArray(1), this->testArray(0));
  154. this->V = std::move(this->V2);
  155. this->expectValues(this->V, this->testArray(0));
  156. #endif
  157. this->setVectors(this->testArray(2), this->testArray(0));
  158. this->V = this->V2;
  159. this->expectValues(this->V, this->testArray(0));
  160. this->expectValues(this->V2, this->testArray(0));
  161. #if LLVM_HAS_RVALUE_REFERENCES
  162. this->setVectors(this->testArray(2), this->testArray(0));
  163. this->V = std::move(this->V2);
  164. this->expectValues(this->V, this->testArray(0));
  165. #endif
  166. this->setVectors(this->testArray(42), this->testArray(0));
  167. this->V = this->V2;
  168. this->expectValues(this->V, this->testArray(0));
  169. this->expectValues(this->V2, this->testArray(0));
  170. #if LLVM_HAS_RVALUE_REFERENCES
  171. this->setVectors(this->testArray(42), this->testArray(0));
  172. this->V = std::move(this->V2);
  173. this->expectValues(this->V, this->testArray(0));
  174. #endif
  175. this->setVectors(this->testArray(0), this->testArray(1));
  176. this->V = this->V2;
  177. this->expectValues(this->V, this->testArray(1));
  178. this->expectValues(this->V2, this->testArray(1));
  179. #if LLVM_HAS_RVALUE_REFERENCES
  180. this->setVectors(this->testArray(0), this->testArray(1));
  181. this->V = std::move(this->V2);
  182. this->expectValues(this->V, this->testArray(1));
  183. #endif
  184. this->setVectors(this->testArray(0), this->testArray(2));
  185. this->V = this->V2;
  186. this->expectValues(this->V, this->testArray(2));
  187. this->expectValues(this->V2, this->testArray(2));
  188. #if LLVM_HAS_RVALUE_REFERENCES
  189. this->setVectors(this->testArray(0), this->testArray(2));
  190. this->V = std::move(this->V2);
  191. this->expectValues(this->V, this->testArray(2));
  192. #endif
  193. this->setVectors(this->testArray(0), this->testArray(42));
  194. this->V = this->V2;
  195. this->expectValues(this->V, this->testArray(42));
  196. this->expectValues(this->V2, this->testArray(42));
  197. #if LLVM_HAS_RVALUE_REFERENCES
  198. this->setVectors(this->testArray(0), this->testArray(42));
  199. this->V = std::move(this->V2);
  200. this->expectValues(this->V, this->testArray(42));
  201. #endif
  202. this->setVectors(this->testArray(1), this->testArray(1));
  203. this->V = this->V2;
  204. this->expectValues(this->V, this->testArray(1));
  205. this->expectValues(this->V2, this->testArray(1));
  206. #if LLVM_HAS_RVALUE_REFERENCES
  207. this->V = std::move(this->V2);
  208. this->expectValues(this->V, this->testArray(1));
  209. #endif
  210. this->setVectors(this->testArray(1), this->testArray(2));
  211. this->V = this->V2;
  212. this->expectValues(this->V, this->testArray(2));
  213. this->expectValues(this->V2, this->testArray(2));
  214. #if LLVM_HAS_RVALUE_REFERENCES
  215. this->setVectors(this->testArray(1), this->testArray(2));
  216. this->V = std::move(this->V2);
  217. this->expectValues(this->V, this->testArray(2));
  218. #endif
  219. this->setVectors(this->testArray(1), this->testArray(42));
  220. this->V = this->V2;
  221. this->expectValues(this->V, this->testArray(42));
  222. this->expectValues(this->V2, this->testArray(42));
  223. #if LLVM_HAS_RVALUE_REFERENCES
  224. this->setVectors(this->testArray(1), this->testArray(42));
  225. this->V = std::move(this->V2);
  226. this->expectValues(this->V, this->testArray(42));
  227. #endif
  228. this->setVectors(this->testArray(2), this->testArray(1));
  229. this->V = this->V2;
  230. this->expectValues(this->V, this->testArray(1));
  231. this->expectValues(this->V2, this->testArray(1));
  232. #if LLVM_HAS_RVALUE_REFERENCES
  233. this->setVectors(this->testArray(2), this->testArray(1));
  234. this->V = std::move(this->V2);
  235. this->expectValues(this->V, this->testArray(1));
  236. #endif
  237. this->setVectors(this->testArray(2), this->testArray(2));
  238. this->V = this->V2;
  239. this->expectValues(this->V, this->testArray(2));
  240. this->expectValues(this->V2, this->testArray(2));
  241. #if LLVM_HAS_RVALUE_REFERENCES
  242. this->setVectors(this->testArray(2), this->testArray(2));
  243. this->V = std::move(this->V2);
  244. this->expectValues(this->V, this->testArray(2));
  245. #endif
  246. this->setVectors(this->testArray(2), this->testArray(42));
  247. this->V = this->V2;
  248. this->expectValues(this->V, this->testArray(42));
  249. this->expectValues(this->V2, this->testArray(42));
  250. #if LLVM_HAS_RVALUE_REFERENCES
  251. this->setVectors(this->testArray(2), this->testArray(42));
  252. this->V = std::move(this->V2);
  253. this->expectValues(this->V, this->testArray(42));
  254. #endif
  255. this->setVectors(this->testArray(42), this->testArray(1));
  256. this->V = this->V2;
  257. this->expectValues(this->V, this->testArray(1));
  258. this->expectValues(this->V2, this->testArray(1));
  259. #if LLVM_HAS_RVALUE_REFERENCES
  260. this->setVectors(this->testArray(42), this->testArray(1));
  261. this->V = std::move(this->V2);
  262. this->expectValues(this->V, this->testArray(1));
  263. #endif
  264. this->setVectors(this->testArray(42), this->testArray(2));
  265. this->V = this->V2;
  266. this->expectValues(this->V, this->testArray(2));
  267. this->expectValues(this->V2, this->testArray(2));
  268. #if LLVM_HAS_RVALUE_REFERENCES
  269. this->setVectors(this->testArray(42), this->testArray(2));
  270. this->V = std::move(this->V2);
  271. this->expectValues(this->V, this->testArray(2));
  272. #endif
  273. this->setVectors(this->testArray(42), this->testArray(42));
  274. this->V = this->V2;
  275. this->expectValues(this->V, this->testArray(42));
  276. this->expectValues(this->V2, this->testArray(42));
  277. #if LLVM_HAS_RVALUE_REFERENCES
  278. this->setVectors(this->testArray(42), this->testArray(42));
  279. this->V = std::move(this->V2);
  280. this->expectValues(this->V, this->testArray(42));
  281. #endif
  282. }
  283. TYPED_TEST(TinyPtrVectorTest, EraseTest) {
  284. this->appendValues(this->V, this->testArray(1));
  285. this->expectValues(this->V, this->testArray(1));
  286. this->V.erase(this->V.begin());
  287. this->expectValues(this->V, this->testArray(0));
  288. this->appendValues(this->V, this->testArray(42));
  289. this->expectValues(this->V, this->testArray(42));
  290. this->V.erase(this->V.begin());
  291. this->TestPtrs.erase(this->TestPtrs.begin());
  292. this->expectValues(this->V, this->testArray(41));
  293. this->V.erase(llvm::next(this->V.begin(), 1));
  294. this->TestPtrs.erase(llvm::next(this->TestPtrs.begin(), 1));
  295. this->expectValues(this->V, this->testArray(40));
  296. this->V.erase(llvm::next(this->V.begin(), 2));
  297. this->TestPtrs.erase(llvm::next(this->TestPtrs.begin(), 2));
  298. this->expectValues(this->V, this->testArray(39));
  299. this->V.erase(llvm::next(this->V.begin(), 5));
  300. this->TestPtrs.erase(llvm::next(this->TestPtrs.begin(), 5));
  301. this->expectValues(this->V, this->testArray(38));
  302. this->V.erase(llvm::next(this->V.begin(), 13));
  303. this->TestPtrs.erase(llvm::next(this->TestPtrs.begin(), 13));
  304. this->expectValues(this->V, this->testArray(37));
  305. typename TypeParam::iterator I = this->V.begin();
  306. do {
  307. I = this->V.erase(I);
  308. } while (I != this->V.end());
  309. this->expectValues(this->V, this->testArray(0));
  310. }
  311. TYPED_TEST(TinyPtrVectorTest, EraseRangeTest) {
  312. this->appendValues(this->V, this->testArray(1));
  313. this->expectValues(this->V, this->testArray(1));
  314. this->V.erase(this->V.begin(), this->V.begin());
  315. this->expectValues(this->V, this->testArray(1));
  316. this->V.erase(this->V.end(), this->V.end());
  317. this->expectValues(this->V, this->testArray(1));
  318. this->V.erase(this->V.begin(), this->V.end());
  319. this->expectValues(this->V, this->testArray(0));
  320. this->appendValues(this->V, this->testArray(42));
  321. this->expectValues(this->V, this->testArray(42));
  322. this->V.erase(this->V.begin(), llvm::next(this->V.begin(), 1));
  323. this->TestPtrs.erase(this->TestPtrs.begin(),
  324. llvm::next(this->TestPtrs.begin(), 1));
  325. this->expectValues(this->V, this->testArray(41));
  326. this->V.erase(llvm::next(this->V.begin(), 1), llvm::next(this->V.begin(), 2));
  327. this->TestPtrs.erase(llvm::next(this->TestPtrs.begin(), 1),
  328. llvm::next(this->TestPtrs.begin(), 2));
  329. this->expectValues(this->V, this->testArray(40));
  330. this->V.erase(llvm::next(this->V.begin(), 2), llvm::next(this->V.begin(), 4));
  331. this->TestPtrs.erase(llvm::next(this->TestPtrs.begin(), 2),
  332. llvm::next(this->TestPtrs.begin(), 4));
  333. this->expectValues(this->V, this->testArray(38));
  334. this->V.erase(llvm::next(this->V.begin(), 5), llvm::next(this->V.begin(), 10));
  335. this->TestPtrs.erase(llvm::next(this->TestPtrs.begin(), 5),
  336. llvm::next(this->TestPtrs.begin(), 10));
  337. this->expectValues(this->V, this->testArray(33));
  338. this->V.erase(llvm::next(this->V.begin(), 13), llvm::next(this->V.begin(), 26));
  339. this->TestPtrs.erase(llvm::next(this->TestPtrs.begin(), 13),
  340. llvm::next(this->TestPtrs.begin(), 26));
  341. this->expectValues(this->V, this->testArray(20));
  342. this->V.erase(llvm::next(this->V.begin(), 7), this->V.end());
  343. this->expectValues(this->V, this->testArray(7));
  344. this->V.erase(this->V.begin(), this->V.end());
  345. this->expectValues(this->V, this->testArray(0));
  346. }
  347. TYPED_TEST(TinyPtrVectorTest, Insert) {
  348. this->V.insert(this->V.end(), this->TestPtrs[0]);
  349. this->expectValues(this->V, this->testArray(1));
  350. this->V.clear();
  351. this->appendValues(this->V, this->testArray(4));
  352. this->expectValues(this->V, this->testArray(4));
  353. this->V.insert(this->V.end(), this->TestPtrs[4]);
  354. this->expectValues(this->V, this->testArray(5));
  355. this->V.insert(this->V.begin(), this->TestPtrs[42]);
  356. this->TestPtrs.insert(this->TestPtrs.begin(), this->TestPtrs[42]);
  357. this->expectValues(this->V, this->testArray(6));
  358. this->V.insert(llvm::next(this->V.begin(), 3), this->TestPtrs[43]);
  359. this->TestPtrs.insert(llvm::next(this->TestPtrs.begin(), 3),
  360. this->TestPtrs[43]);
  361. this->expectValues(this->V, this->testArray(7));
  362. }
  363. TYPED_TEST(TinyPtrVectorTest, InsertRange) {
  364. this->V.insert(this->V.end(), this->TestPtrs.begin(), this->TestPtrs.begin());
  365. this->expectValues(this->V, this->testArray(0));
  366. this->V.insert(this->V.begin(), this->TestPtrs.begin(),
  367. this->TestPtrs.begin());
  368. this->expectValues(this->V, this->testArray(0));
  369. this->V.insert(this->V.end(), this->TestPtrs.end(), this->TestPtrs.end());
  370. this->expectValues(this->V, this->testArray(0));
  371. this->V.insert(this->V.end(), this->TestPtrs.begin(),
  372. llvm::next(this->TestPtrs.begin()));
  373. this->expectValues(this->V, this->testArray(1));
  374. this->V.clear();
  375. this->V.insert(this->V.end(), this->TestPtrs.begin(),
  376. llvm::next(this->TestPtrs.begin(), 2));
  377. this->expectValues(this->V, this->testArray(2));
  378. this->V.clear();
  379. this->V.insert(this->V.end(), this->TestPtrs.begin(),
  380. llvm::next(this->TestPtrs.begin(), 42));
  381. this->expectValues(this->V, this->testArray(42));
  382. this->V.clear();
  383. this->V.insert(this->V.end(),
  384. llvm::next(this->TestPtrs.begin(), 5),
  385. llvm::next(this->TestPtrs.begin(), 13));
  386. this->V.insert(this->V.begin(),
  387. llvm::next(this->TestPtrs.begin(), 0),
  388. llvm::next(this->TestPtrs.begin(), 3));
  389. this->V.insert(llvm::next(this->V.begin(), 2),
  390. llvm::next(this->TestPtrs.begin(), 2),
  391. llvm::next(this->TestPtrs.begin(), 4));
  392. this->V.erase(llvm::next(this->V.begin(), 4));
  393. this->V.insert(llvm::next(this->V.begin(), 4),
  394. llvm::next(this->TestPtrs.begin(), 4),
  395. llvm::next(this->TestPtrs.begin(), 5));
  396. this->expectValues(this->V, this->testArray(13));
  397. }
  398. }