set_allocator_requirement_test_templates.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef SET_ALLOCATOR_REQUIREMENT_TEST_TEMPLATES_H
  9. #define SET_ALLOCATOR_REQUIREMENT_TEST_TEMPLATES_H
  10. // <set>
  11. // <unordered_set>
  12. // class set
  13. // class unordered_set
  14. // insert(...);
  15. // emplace(...);
  16. // emplace_hint(...);
  17. #include <cassert>
  18. #include "test_macros.h"
  19. #include "count_new.h"
  20. #include "container_test_types.h"
  21. #include "assert_checkpoint.h"
  22. template <class Container>
  23. void testSetInsert()
  24. {
  25. typedef typename Container::value_type ValueTp;
  26. ConstructController* cc = getConstructController();
  27. cc->reset();
  28. {
  29. CHECKPOINT("Testing C::insert(const value_type&)");
  30. Container c;
  31. const ValueTp v(42);
  32. cc->expect<const ValueTp&>();
  33. assert(c.insert(v).second);
  34. assert(!cc->unchecked());
  35. {
  36. DisableAllocationGuard g;
  37. const ValueTp v2(42);
  38. assert(c.insert(v2).second == false);
  39. }
  40. }
  41. {
  42. CHECKPOINT("Testing C::insert(value_type&)");
  43. Container c;
  44. ValueTp v(42);
  45. cc->expect<const ValueTp&>();
  46. assert(c.insert(v).second);
  47. assert(!cc->unchecked());
  48. {
  49. DisableAllocationGuard g;
  50. ValueTp v2(42);
  51. assert(c.insert(v2).second == false);
  52. }
  53. }
  54. {
  55. CHECKPOINT("Testing C::insert(value_type&&)");
  56. Container c;
  57. ValueTp v(42);
  58. cc->expect<ValueTp&&>();
  59. assert(c.insert(std::move(v)).second);
  60. assert(!cc->unchecked());
  61. {
  62. DisableAllocationGuard g;
  63. ValueTp v2(42);
  64. assert(c.insert(std::move(v2)).second == false);
  65. }
  66. }
  67. {
  68. CHECKPOINT("Testing C::insert(const value_type&&)");
  69. Container c;
  70. const ValueTp v(42);
  71. cc->expect<const ValueTp&>();
  72. assert(c.insert(std::move(v)).second);
  73. assert(!cc->unchecked());
  74. {
  75. DisableAllocationGuard g;
  76. const ValueTp v2(42);
  77. assert(c.insert(std::move(v2)).second == false);
  78. }
  79. }
  80. {
  81. CHECKPOINT("Testing C::insert(std::initializer_list<ValueTp>)");
  82. Container c;
  83. std::initializer_list<ValueTp> il = { ValueTp(1), ValueTp(2) };
  84. cc->expect<ValueTp const&>(2);
  85. c.insert(il);
  86. assert(!cc->unchecked());
  87. {
  88. DisableAllocationGuard g;
  89. c.insert(il);
  90. }
  91. }
  92. {
  93. CHECKPOINT("Testing C::insert(Iter, Iter) for *Iter = value_type const&");
  94. Container c;
  95. const ValueTp ValueList[] = { ValueTp(1), ValueTp(2), ValueTp(3) };
  96. cc->expect<ValueTp const&>(3);
  97. c.insert(std::begin(ValueList), std::end(ValueList));
  98. assert(!cc->unchecked());
  99. {
  100. DisableAllocationGuard g;
  101. c.insert(std::begin(ValueList), std::end(ValueList));
  102. }
  103. }
  104. {
  105. CHECKPOINT("Testing C::insert(Iter, Iter) for *Iter = value_type&&");
  106. Container c;
  107. ValueTp ValueList[] = { ValueTp(1), ValueTp(2) , ValueTp(3) };
  108. cc->expect<ValueTp&&>(3);
  109. c.insert(std::move_iterator<ValueTp*>(std::begin(ValueList)),
  110. std::move_iterator<ValueTp*>(std::end(ValueList)));
  111. assert(!cc->unchecked());
  112. {
  113. DisableAllocationGuard g;
  114. ValueTp ValueList2[] = { ValueTp(1), ValueTp(2) , ValueTp(3) };
  115. c.insert(std::move_iterator<ValueTp*>(std::begin(ValueList2)),
  116. std::move_iterator<ValueTp*>(std::end(ValueList2)));
  117. }
  118. }
  119. {
  120. CHECKPOINT("Testing C::insert(Iter, Iter) for *Iter = value_type&");
  121. Container c;
  122. ValueTp ValueList[] = { ValueTp(1), ValueTp(2) , ValueTp(3) };
  123. cc->expect<ValueTp const&>(3);
  124. c.insert(std::begin(ValueList), std::end(ValueList));
  125. assert(!cc->unchecked());
  126. {
  127. DisableAllocationGuard g;
  128. c.insert(std::begin(ValueList), std::end(ValueList));
  129. }
  130. }
  131. }
  132. template <class Container>
  133. void testSetEmplace()
  134. {
  135. typedef typename Container::value_type ValueTp;
  136. ConstructController* cc = getConstructController();
  137. cc->reset();
  138. {
  139. CHECKPOINT("Testing C::emplace(const value_type&)");
  140. Container c;
  141. const ValueTp v(42);
  142. cc->expect<const ValueTp&>();
  143. assert(c.emplace(v).second);
  144. assert(!cc->unchecked());
  145. {
  146. DisableAllocationGuard g;
  147. const ValueTp v2(42);
  148. assert(c.emplace(v2).second == false);
  149. }
  150. }
  151. {
  152. CHECKPOINT("Testing C::emplace(value_type&)");
  153. Container c;
  154. ValueTp v(42);
  155. cc->expect<ValueTp&>();
  156. assert(c.emplace(v).second);
  157. assert(!cc->unchecked());
  158. {
  159. DisableAllocationGuard g;
  160. ValueTp v2(42);
  161. assert(c.emplace(v2).second == false);
  162. }
  163. }
  164. {
  165. CHECKPOINT("Testing C::emplace(value_type&&)");
  166. Container c;
  167. ValueTp v(42);
  168. cc->expect<ValueTp&&>();
  169. assert(c.emplace(std::move(v)).second);
  170. assert(!cc->unchecked());
  171. {
  172. DisableAllocationGuard g;
  173. ValueTp v2(42);
  174. assert(c.emplace(std::move(v2)).second == false);
  175. }
  176. }
  177. {
  178. CHECKPOINT("Testing C::emplace(const value_type&&)");
  179. Container c;
  180. const ValueTp v(42);
  181. cc->expect<const ValueTp&&>();
  182. assert(c.emplace(std::move(v)).second);
  183. assert(!cc->unchecked());
  184. {
  185. DisableAllocationGuard g;
  186. const ValueTp v2(42);
  187. assert(c.emplace(std::move(v2)).second == false);
  188. }
  189. }
  190. }
  191. template <class Container>
  192. void testSetEmplaceHint()
  193. {
  194. typedef typename Container::value_type ValueTp;
  195. typedef Container C;
  196. typedef typename C::iterator It;
  197. ConstructController* cc = getConstructController();
  198. cc->reset();
  199. {
  200. CHECKPOINT("Testing C::emplace_hint(p, const value_type&)");
  201. Container c;
  202. const ValueTp v(42);
  203. cc->expect<const ValueTp&>();
  204. It ret = c.emplace_hint(c.end(), v);
  205. assert(ret != c.end());
  206. assert(c.size() == 1);
  207. assert(!cc->unchecked());
  208. {
  209. DisableAllocationGuard g;
  210. const ValueTp v2(42);
  211. It ret2 = c.emplace_hint(c.begin(), v2);
  212. assert(&(*ret2) == &(*ret));
  213. assert(c.size() == 1);
  214. }
  215. }
  216. {
  217. CHECKPOINT("Testing C::emplace_hint(p, value_type&)");
  218. Container c;
  219. ValueTp v(42);
  220. cc->expect<ValueTp&>();
  221. It ret = c.emplace_hint(c.end(), v);
  222. assert(ret != c.end());
  223. assert(c.size() == 1);
  224. assert(!cc->unchecked());
  225. {
  226. DisableAllocationGuard g;
  227. ValueTp v2(42);
  228. It ret2 = c.emplace_hint(c.begin(), v2);
  229. assert(&(*ret2) == &(*ret));
  230. assert(c.size() == 1);
  231. }
  232. }
  233. {
  234. CHECKPOINT("Testing C::emplace_hint(p, value_type&&)");
  235. Container c;
  236. ValueTp v(42);
  237. cc->expect<ValueTp&&>();
  238. It ret = c.emplace_hint(c.end(), std::move(v));
  239. assert(ret != c.end());
  240. assert(c.size() == 1);
  241. assert(!cc->unchecked());
  242. {
  243. DisableAllocationGuard g;
  244. ValueTp v2(42);
  245. It ret2 = c.emplace_hint(c.begin(), std::move(v2));
  246. assert(&(*ret2) == &(*ret));
  247. assert(c.size() == 1);
  248. }
  249. }
  250. {
  251. CHECKPOINT("Testing C::emplace_hint(p, const value_type&&)");
  252. Container c;
  253. const ValueTp v(42);
  254. cc->expect<const ValueTp&&>();
  255. It ret = c.emplace_hint(c.end(), std::move(v));
  256. assert(ret != c.end());
  257. assert(c.size() == 1);
  258. assert(!cc->unchecked());
  259. {
  260. DisableAllocationGuard g;
  261. const ValueTp v2(42);
  262. It ret2 = c.emplace_hint(c.begin(), std::move(v2));
  263. assert(&(*ret2) == &(*ret));
  264. assert(c.size() == 1);
  265. }
  266. }
  267. }
  268. template <class Container>
  269. void testMultisetInsert()
  270. {
  271. typedef typename Container::value_type ValueTp;
  272. ConstructController* cc = getConstructController();
  273. cc->reset();
  274. {
  275. CHECKPOINT("Testing C::insert(const value_type&)");
  276. Container c;
  277. const ValueTp v(42);
  278. cc->expect<const ValueTp&>();
  279. c.insert(v);
  280. assert(!cc->unchecked());
  281. }
  282. {
  283. CHECKPOINT("Testing C::insert(value_type&)");
  284. Container c;
  285. ValueTp v(42);
  286. cc->expect<const ValueTp&>();
  287. c.insert(v);
  288. assert(!cc->unchecked());
  289. }
  290. {
  291. CHECKPOINT("Testing C::insert(value_type&&)");
  292. Container c;
  293. ValueTp v(42);
  294. cc->expect<ValueTp&&>();
  295. c.insert(std::move(v));
  296. assert(!cc->unchecked());
  297. }
  298. {
  299. CHECKPOINT("Testing C::insert(std::initializer_list<ValueTp>)");
  300. Container c;
  301. std::initializer_list<ValueTp> il = { ValueTp(1), ValueTp(2) };
  302. cc->expect<ValueTp const&>(2);
  303. c.insert(il);
  304. assert(!cc->unchecked());
  305. }
  306. {
  307. CHECKPOINT("Testing C::insert(Iter, Iter) for *Iter = value_type const&");
  308. Container c;
  309. const ValueTp ValueList[] = { ValueTp(1), ValueTp(2), ValueTp(3) };
  310. cc->expect<ValueTp const&>(3);
  311. c.insert(std::begin(ValueList), std::end(ValueList));
  312. assert(!cc->unchecked());
  313. }
  314. {
  315. CHECKPOINT("Testing C::insert(Iter, Iter) for *Iter = value_type&&");
  316. Container c;
  317. ValueTp ValueList[] = { ValueTp(1), ValueTp(2) , ValueTp(3) };
  318. cc->expect<ValueTp&&>(3);
  319. c.insert(std::move_iterator<ValueTp*>(std::begin(ValueList)),
  320. std::move_iterator<ValueTp*>(std::end(ValueList)));
  321. assert(!cc->unchecked());
  322. }
  323. {
  324. CHECKPOINT("Testing C::insert(Iter, Iter) for *Iter = value_type&");
  325. Container c;
  326. ValueTp ValueList[] = { ValueTp(1), ValueTp(2) , ValueTp(1) };
  327. cc->expect<ValueTp&>(3);
  328. c.insert(std::begin(ValueList), std::end(ValueList));
  329. assert(!cc->unchecked());
  330. }
  331. }
  332. template <class Container>
  333. void testMultisetEmplace()
  334. {
  335. typedef typename Container::value_type ValueTp;
  336. ConstructController* cc = getConstructController();
  337. cc->reset();
  338. {
  339. CHECKPOINT("Testing C::emplace(const value_type&)");
  340. Container c;
  341. const ValueTp v(42);
  342. cc->expect<const ValueTp&>();
  343. c.emplace(v);
  344. assert(!cc->unchecked());
  345. }
  346. {
  347. CHECKPOINT("Testing C::emplace(value_type&)");
  348. Container c;
  349. ValueTp v(42);
  350. cc->expect<ValueTp&>();
  351. c.emplace(v);
  352. assert(!cc->unchecked());
  353. }
  354. {
  355. CHECKPOINT("Testing C::emplace(value_type&&)");
  356. Container c;
  357. ValueTp v(42);
  358. cc->expect<ValueTp&&>();
  359. c.emplace(std::move(v));
  360. assert(!cc->unchecked());
  361. }
  362. {
  363. CHECKPOINT("Testing C::emplace(const value_type&&)");
  364. Container c;
  365. const ValueTp v(42);
  366. cc->expect<const ValueTp&&>();
  367. c.emplace(std::move(v));
  368. assert(!cc->unchecked());
  369. }
  370. }
  371. #endif