TinyPtrVectorTest.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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 std::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], *std::next(V.begin(), i));
  63. }
  64. EXPECT_EQ(V.end(), std::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. TypeParam Move(std::move(Copy2));
  135. this->expectValues(Move, this->testArray(42));
  136. this->expectValues(Copy2, this->testArray(0));
  137. }
  138. TYPED_TEST(TinyPtrVectorTest, CopyAndMoveTest) {
  139. this->V = this->V2;
  140. this->expectValues(this->V, this->testArray(0));
  141. this->expectValues(this->V2, this->testArray(0));
  142. this->V = std::move(this->V2);
  143. this->expectValues(this->V, this->testArray(0));
  144. this->setVectors(this->testArray(1), this->testArray(0));
  145. this->V = this->V2;
  146. this->expectValues(this->V, this->testArray(0));
  147. this->expectValues(this->V2, this->testArray(0));
  148. this->setVectors(this->testArray(1), this->testArray(0));
  149. this->V = std::move(this->V2);
  150. this->expectValues(this->V, this->testArray(0));
  151. this->setVectors(this->testArray(2), this->testArray(0));
  152. this->V = this->V2;
  153. this->expectValues(this->V, this->testArray(0));
  154. this->expectValues(this->V2, this->testArray(0));
  155. this->setVectors(this->testArray(2), this->testArray(0));
  156. this->V = std::move(this->V2);
  157. this->expectValues(this->V, this->testArray(0));
  158. this->setVectors(this->testArray(42), this->testArray(0));
  159. this->V = this->V2;
  160. this->expectValues(this->V, this->testArray(0));
  161. this->expectValues(this->V2, this->testArray(0));
  162. this->setVectors(this->testArray(42), this->testArray(0));
  163. this->V = std::move(this->V2);
  164. this->expectValues(this->V, this->testArray(0));
  165. this->setVectors(this->testArray(0), this->testArray(1));
  166. this->V = this->V2;
  167. this->expectValues(this->V, this->testArray(1));
  168. this->expectValues(this->V2, this->testArray(1));
  169. this->setVectors(this->testArray(0), this->testArray(1));
  170. this->V = std::move(this->V2);
  171. this->expectValues(this->V, this->testArray(1));
  172. this->setVectors(this->testArray(0), this->testArray(2));
  173. this->V = this->V2;
  174. this->expectValues(this->V, this->testArray(2));
  175. this->expectValues(this->V2, this->testArray(2));
  176. this->setVectors(this->testArray(0), this->testArray(2));
  177. this->V = std::move(this->V2);
  178. this->expectValues(this->V, this->testArray(2));
  179. this->setVectors(this->testArray(0), this->testArray(42));
  180. this->V = this->V2;
  181. this->expectValues(this->V, this->testArray(42));
  182. this->expectValues(this->V2, this->testArray(42));
  183. this->setVectors(this->testArray(0), this->testArray(42));
  184. this->V = std::move(this->V2);
  185. this->expectValues(this->V, this->testArray(42));
  186. this->setVectors(this->testArray(1), this->testArray(1));
  187. this->V = this->V2;
  188. this->expectValues(this->V, this->testArray(1));
  189. this->expectValues(this->V2, this->testArray(1));
  190. this->V = std::move(this->V2);
  191. this->expectValues(this->V, this->testArray(1));
  192. this->setVectors(this->testArray(1), this->testArray(2));
  193. this->V = this->V2;
  194. this->expectValues(this->V, this->testArray(2));
  195. this->expectValues(this->V2, this->testArray(2));
  196. this->setVectors(this->testArray(1), this->testArray(2));
  197. this->V = std::move(this->V2);
  198. this->expectValues(this->V, this->testArray(2));
  199. this->setVectors(this->testArray(1), this->testArray(42));
  200. this->V = this->V2;
  201. this->expectValues(this->V, this->testArray(42));
  202. this->expectValues(this->V2, this->testArray(42));
  203. this->setVectors(this->testArray(1), this->testArray(42));
  204. this->V = std::move(this->V2);
  205. this->expectValues(this->V, this->testArray(42));
  206. this->setVectors(this->testArray(2), this->testArray(1));
  207. this->V = this->V2;
  208. this->expectValues(this->V, this->testArray(1));
  209. this->expectValues(this->V2, this->testArray(1));
  210. this->setVectors(this->testArray(2), this->testArray(1));
  211. this->V = std::move(this->V2);
  212. this->expectValues(this->V, this->testArray(1));
  213. this->setVectors(this->testArray(2), this->testArray(2));
  214. this->V = this->V2;
  215. this->expectValues(this->V, this->testArray(2));
  216. this->expectValues(this->V2, this->testArray(2));
  217. this->setVectors(this->testArray(2), this->testArray(2));
  218. this->V = std::move(this->V2);
  219. this->expectValues(this->V, this->testArray(2));
  220. this->setVectors(this->testArray(2), this->testArray(42));
  221. this->V = this->V2;
  222. this->expectValues(this->V, this->testArray(42));
  223. this->expectValues(this->V2, this->testArray(42));
  224. this->setVectors(this->testArray(2), this->testArray(42));
  225. this->V = std::move(this->V2);
  226. this->expectValues(this->V, this->testArray(42));
  227. this->setVectors(this->testArray(42), this->testArray(1));
  228. this->V = this->V2;
  229. this->expectValues(this->V, this->testArray(1));
  230. this->expectValues(this->V2, this->testArray(1));
  231. this->setVectors(this->testArray(42), this->testArray(1));
  232. this->V = std::move(this->V2);
  233. this->expectValues(this->V, this->testArray(1));
  234. this->setVectors(this->testArray(42), this->testArray(2));
  235. this->V = this->V2;
  236. this->expectValues(this->V, this->testArray(2));
  237. this->expectValues(this->V2, this->testArray(2));
  238. this->setVectors(this->testArray(42), this->testArray(2));
  239. this->V = std::move(this->V2);
  240. this->expectValues(this->V, this->testArray(2));
  241. this->setVectors(this->testArray(42), this->testArray(42));
  242. this->V = this->V2;
  243. this->expectValues(this->V, this->testArray(42));
  244. this->expectValues(this->V2, this->testArray(42));
  245. this->setVectors(this->testArray(42), this->testArray(42));
  246. this->V = std::move(this->V2);
  247. this->expectValues(this->V, this->testArray(42));
  248. }
  249. TYPED_TEST(TinyPtrVectorTest, EraseTest) {
  250. this->appendValues(this->V, this->testArray(1));
  251. this->expectValues(this->V, this->testArray(1));
  252. this->V.erase(this->V.begin());
  253. this->expectValues(this->V, this->testArray(0));
  254. this->appendValues(this->V, this->testArray(42));
  255. this->expectValues(this->V, this->testArray(42));
  256. this->V.erase(this->V.begin());
  257. this->TestPtrs.erase(this->TestPtrs.begin());
  258. this->expectValues(this->V, this->testArray(41));
  259. this->V.erase(std::next(this->V.begin(), 1));
  260. this->TestPtrs.erase(std::next(this->TestPtrs.begin(), 1));
  261. this->expectValues(this->V, this->testArray(40));
  262. this->V.erase(std::next(this->V.begin(), 2));
  263. this->TestPtrs.erase(std::next(this->TestPtrs.begin(), 2));
  264. this->expectValues(this->V, this->testArray(39));
  265. this->V.erase(std::next(this->V.begin(), 5));
  266. this->TestPtrs.erase(std::next(this->TestPtrs.begin(), 5));
  267. this->expectValues(this->V, this->testArray(38));
  268. this->V.erase(std::next(this->V.begin(), 13));
  269. this->TestPtrs.erase(std::next(this->TestPtrs.begin(), 13));
  270. this->expectValues(this->V, this->testArray(37));
  271. typename TypeParam::iterator I = this->V.begin();
  272. do {
  273. I = this->V.erase(I);
  274. } while (I != this->V.end());
  275. this->expectValues(this->V, this->testArray(0));
  276. }
  277. TYPED_TEST(TinyPtrVectorTest, EraseRangeTest) {
  278. this->appendValues(this->V, this->testArray(1));
  279. this->expectValues(this->V, this->testArray(1));
  280. this->V.erase(this->V.begin(), this->V.begin());
  281. this->expectValues(this->V, this->testArray(1));
  282. this->V.erase(this->V.end(), this->V.end());
  283. this->expectValues(this->V, this->testArray(1));
  284. this->V.erase(this->V.begin(), this->V.end());
  285. this->expectValues(this->V, this->testArray(0));
  286. this->appendValues(this->V, this->testArray(42));
  287. this->expectValues(this->V, this->testArray(42));
  288. this->V.erase(this->V.begin(), std::next(this->V.begin(), 1));
  289. this->TestPtrs.erase(this->TestPtrs.begin(),
  290. std::next(this->TestPtrs.begin(), 1));
  291. this->expectValues(this->V, this->testArray(41));
  292. this->V.erase(std::next(this->V.begin(), 1), std::next(this->V.begin(), 2));
  293. this->TestPtrs.erase(std::next(this->TestPtrs.begin(), 1),
  294. std::next(this->TestPtrs.begin(), 2));
  295. this->expectValues(this->V, this->testArray(40));
  296. this->V.erase(std::next(this->V.begin(), 2), std::next(this->V.begin(), 4));
  297. this->TestPtrs.erase(std::next(this->TestPtrs.begin(), 2),
  298. std::next(this->TestPtrs.begin(), 4));
  299. this->expectValues(this->V, this->testArray(38));
  300. this->V.erase(std::next(this->V.begin(), 5), std::next(this->V.begin(), 10));
  301. this->TestPtrs.erase(std::next(this->TestPtrs.begin(), 5),
  302. std::next(this->TestPtrs.begin(), 10));
  303. this->expectValues(this->V, this->testArray(33));
  304. this->V.erase(std::next(this->V.begin(), 13), std::next(this->V.begin(), 26));
  305. this->TestPtrs.erase(std::next(this->TestPtrs.begin(), 13),
  306. std::next(this->TestPtrs.begin(), 26));
  307. this->expectValues(this->V, this->testArray(20));
  308. this->V.erase(std::next(this->V.begin(), 7), this->V.end());
  309. this->expectValues(this->V, this->testArray(7));
  310. this->V.erase(this->V.begin(), this->V.end());
  311. this->expectValues(this->V, this->testArray(0));
  312. }
  313. TYPED_TEST(TinyPtrVectorTest, Insert) {
  314. this->V.insert(this->V.end(), this->TestPtrs[0]);
  315. this->expectValues(this->V, this->testArray(1));
  316. this->V.clear();
  317. this->appendValues(this->V, this->testArray(4));
  318. this->expectValues(this->V, this->testArray(4));
  319. this->V.insert(this->V.end(), this->TestPtrs[4]);
  320. this->expectValues(this->V, this->testArray(5));
  321. this->V.insert(this->V.begin(), this->TestPtrs[42]);
  322. this->TestPtrs.insert(this->TestPtrs.begin(), this->TestPtrs[42]);
  323. this->expectValues(this->V, this->testArray(6));
  324. this->V.insert(std::next(this->V.begin(), 3), this->TestPtrs[43]);
  325. this->TestPtrs.insert(std::next(this->TestPtrs.begin(), 3),
  326. this->TestPtrs[43]);
  327. this->expectValues(this->V, this->testArray(7));
  328. }
  329. TYPED_TEST(TinyPtrVectorTest, InsertRange) {
  330. this->V.insert(this->V.end(), this->TestPtrs.begin(), this->TestPtrs.begin());
  331. this->expectValues(this->V, this->testArray(0));
  332. this->V.insert(this->V.begin(), this->TestPtrs.begin(),
  333. this->TestPtrs.begin());
  334. this->expectValues(this->V, this->testArray(0));
  335. this->V.insert(this->V.end(), this->TestPtrs.end(), this->TestPtrs.end());
  336. this->expectValues(this->V, this->testArray(0));
  337. this->V.insert(this->V.end(), this->TestPtrs.begin(),
  338. std::next(this->TestPtrs.begin()));
  339. this->expectValues(this->V, this->testArray(1));
  340. this->V.clear();
  341. this->V.insert(this->V.end(), this->TestPtrs.begin(),
  342. std::next(this->TestPtrs.begin(), 2));
  343. this->expectValues(this->V, this->testArray(2));
  344. this->V.clear();
  345. this->V.insert(this->V.end(), this->TestPtrs.begin(),
  346. std::next(this->TestPtrs.begin(), 42));
  347. this->expectValues(this->V, this->testArray(42));
  348. this->V.clear();
  349. this->V.insert(this->V.end(),
  350. std::next(this->TestPtrs.begin(), 5),
  351. std::next(this->TestPtrs.begin(), 13));
  352. this->V.insert(this->V.begin(),
  353. std::next(this->TestPtrs.begin(), 0),
  354. std::next(this->TestPtrs.begin(), 3));
  355. this->V.insert(std::next(this->V.begin(), 2),
  356. std::next(this->TestPtrs.begin(), 2),
  357. std::next(this->TestPtrs.begin(), 4));
  358. this->V.erase(std::next(this->V.begin(), 4));
  359. this->V.insert(std::next(this->V.begin(), 4),
  360. std::next(this->TestPtrs.begin(), 4),
  361. std::next(this->TestPtrs.begin(), 5));
  362. this->expectValues(this->V, this->testArray(13));
  363. }
  364. }
  365. TEST(TinyPtrVectorTest, SingleEltCtorTest) {
  366. int v = 55;
  367. TinyPtrVector<int *> V(&v);
  368. EXPECT_TRUE(V.size() == 1);
  369. EXPECT_FALSE(V.empty());
  370. EXPECT_TRUE(V.front() == &v);
  371. }
  372. TEST(TinyPtrVectorTest, ArrayRefCtorTest) {
  373. int data_array[128];
  374. std::vector<int *> data;
  375. for (unsigned i = 0, e = 128; i != e; ++i) {
  376. data_array[i] = 324 - int(i);
  377. data.push_back(&data_array[i]);
  378. }
  379. TinyPtrVector<int *> V(data);
  380. EXPECT_TRUE(V.size() == 128);
  381. EXPECT_FALSE(V.empty());
  382. for (unsigned i = 0, e = 128; i != e; ++i) {
  383. EXPECT_TRUE(V[i] == data[i]);
  384. }
  385. }
  386. TEST(TinyPtrVectorTest, MutableArrayRefTest) {
  387. int data_array[128];
  388. std::vector<int *> data;
  389. for (unsigned i = 0, e = 128; i != e; ++i) {
  390. data_array[i] = 324 - int(i);
  391. data.push_back(&data_array[i]);
  392. }
  393. TinyPtrVector<int *> V(data);
  394. EXPECT_TRUE(V.size() == 128);
  395. EXPECT_FALSE(V.empty());
  396. MutableArrayRef<int *> mut_array = V;
  397. for (unsigned i = 0, e = 128; i != e; ++i) {
  398. EXPECT_TRUE(mut_array[i] == data[i]);
  399. mut_array[i] = 324 + mut_array[i];
  400. EXPECT_TRUE(mut_array[i] == (324 + data[i]));
  401. }
  402. }