any.cpp 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812
  1. #include <array>
  2. #include <cstddef>
  3. #include <cstdint>
  4. #include <cstring>
  5. #include <memory>
  6. #include <unordered_map>
  7. #include <utility>
  8. #include <vector>
  9. #include <gtest/gtest.h>
  10. #include <entt/core/any.hpp>
  11. #include <entt/core/type_info.hpp>
  12. #include "../../common/aggregate.h"
  13. #include "../../common/config.h"
  14. #include "../../common/linter.hpp"
  15. #include "../../common/new_delete.h"
  16. #include "../../common/non_comparable.h"
  17. #include "../../common/non_movable.h"
  18. template<std::size_t Len>
  19. struct tracker {
  20. tracker(int &cnt)
  21. : counter{&cnt} {}
  22. tracker(const tracker &) = default;
  23. tracker &operator=(const tracker &) = delete;
  24. ~tracker() {
  25. ++(*counter);
  26. }
  27. private:
  28. std::array<std::byte, Len> buffer{};
  29. int *counter{};
  30. };
  31. struct fat {
  32. fat(double v1, double v2, double v3, double v4)
  33. : value{v1, v2, v3, v4} {}
  34. [[nodiscard]] bool operator==(const fat &other) const {
  35. return (value == other.value);
  36. }
  37. std::array<double, 4u> value{};
  38. };
  39. struct alignas(64u) over_aligned {};
  40. TEST(Any, Empty) {
  41. entt::any any{};
  42. ASSERT_FALSE(any);
  43. ASSERT_FALSE(any.owner());
  44. ASSERT_EQ(any.policy(), entt::any_policy::empty);
  45. ASSERT_EQ(any.type(), entt::type_id<void>());
  46. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  47. ASSERT_EQ(any.data(), nullptr);
  48. }
  49. TEST(Any, SBO) {
  50. entt::any any{'c'};
  51. ASSERT_TRUE(any);
  52. ASSERT_TRUE(any.owner());
  53. ASSERT_EQ(any.policy(), entt::any_policy::embedded);
  54. ASSERT_EQ(any.type(), entt::type_id<char>());
  55. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  56. ASSERT_EQ(entt::any_cast<char>(any), 'c');
  57. }
  58. TEST(Any, NoSBO) {
  59. const fat instance{.1, .2, .3, .4};
  60. entt::any any{instance};
  61. ASSERT_TRUE(any);
  62. ASSERT_TRUE(any.owner());
  63. ASSERT_EQ(any.policy(), entt::any_policy::dynamic);
  64. ASSERT_EQ(any.type(), entt::type_id<fat>());
  65. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  66. ASSERT_EQ(entt::any_cast<fat>(any), instance);
  67. }
  68. TEST(Any, SBOInPlaceConstruction) {
  69. std::unique_ptr<int> elem = std::make_unique<int>(2);
  70. // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
  71. entt::any any{std::in_place, elem.release()};
  72. ASSERT_TRUE(any);
  73. ASSERT_TRUE(any.owner());
  74. ASSERT_EQ(any.policy(), entt::any_policy::dynamic);
  75. ASSERT_EQ(any.info(), entt::type_id<int>());
  76. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  77. ASSERT_EQ(entt::any_cast<int>(any), 2);
  78. auto other = any.as_ref();
  79. ASSERT_TRUE(other);
  80. ASSERT_FALSE(other.owner());
  81. ASSERT_EQ(other.policy(), entt::any_policy::ref);
  82. ASSERT_EQ(other.info(), entt::type_id<int>());
  83. ASSERT_EQ(entt::any_cast<int>(other), 2);
  84. ASSERT_EQ(other.data(), any.data());
  85. }
  86. TEST(Any, SBOInPlaceNullptrConstruction) {
  87. int *instance = nullptr;
  88. const entt::any any{std::in_place, instance};
  89. ASSERT_FALSE(any);
  90. ASSERT_FALSE(any.owner());
  91. ASSERT_EQ(any.policy(), entt::any_policy::empty);
  92. ASSERT_EQ(any.info(), entt::type_id<void>());
  93. }
  94. TEST(Any, SBOInPlaceTypeConstruction) {
  95. entt::any any{std::in_place_type<int>, 2};
  96. ASSERT_TRUE(any);
  97. ASSERT_TRUE(any.owner());
  98. ASSERT_EQ(any.policy(), entt::any_policy::embedded);
  99. ASSERT_EQ(any.info(), entt::type_id<int>());
  100. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  101. ASSERT_EQ(entt::any_cast<int>(any), 2);
  102. auto other = any.as_ref();
  103. ASSERT_TRUE(other);
  104. ASSERT_FALSE(other.owner());
  105. ASSERT_EQ(other.policy(), entt::any_policy::ref);
  106. ASSERT_EQ(other.info(), entt::type_id<int>());
  107. ASSERT_EQ(entt::any_cast<int>(other), 2);
  108. ASSERT_EQ(other.data(), any.data());
  109. }
  110. TEST(Any, SBOAsRefConstruction) {
  111. int value = 2;
  112. entt::any any{entt::forward_as_any(value)};
  113. ASSERT_TRUE(any);
  114. ASSERT_FALSE(any.owner());
  115. ASSERT_EQ(any.policy(), entt::any_policy::ref);
  116. ASSERT_EQ(any.info(), entt::type_id<int>());
  117. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  118. ASSERT_EQ(entt::any_cast<const int>(&any), &value);
  119. ASSERT_EQ(entt::any_cast<int>(&any), &value);
  120. ASSERT_EQ(entt::any_cast<const int>(&std::as_const(any)), &value);
  121. ASSERT_EQ(entt::any_cast<int>(&std::as_const(any)), &value);
  122. ASSERT_EQ(entt::any_cast<const int &>(any), 2);
  123. ASSERT_EQ(entt::any_cast<int>(any), 2);
  124. ASSERT_EQ(any.data(), &value);
  125. ASSERT_EQ(std::as_const(any).data(), &value);
  126. any.emplace<int &>(value);
  127. ASSERT_TRUE(any);
  128. ASSERT_FALSE(any.owner());
  129. ASSERT_EQ(any.policy(), entt::any_policy::ref);
  130. ASSERT_EQ(any.info(), entt::type_id<int>());
  131. ASSERT_EQ(entt::any_cast<int>(&any), &value);
  132. auto other = any.as_ref();
  133. ASSERT_TRUE(other);
  134. ASSERT_FALSE(other.owner());
  135. ASSERT_EQ(other.policy(), entt::any_policy::ref);
  136. ASSERT_EQ(other.info(), entt::type_id<int>());
  137. ASSERT_EQ(entt::any_cast<int>(other), 2);
  138. ASSERT_EQ(other.data(), any.data());
  139. }
  140. TEST(Any, SBOAsConstRefConstruction) {
  141. const int value = 2;
  142. entt::any any{entt::forward_as_any(value)};
  143. ASSERT_TRUE(any);
  144. ASSERT_FALSE(any.owner());
  145. ASSERT_EQ(any.policy(), entt::any_policy::cref);
  146. ASSERT_EQ(any.info(), entt::type_id<int>());
  147. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  148. ASSERT_EQ(entt::any_cast<const int>(&any), &value);
  149. ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
  150. ASSERT_EQ(entt::any_cast<const int>(&std::as_const(any)), &value);
  151. ASSERT_EQ(entt::any_cast<int>(&std::as_const(any)), &value);
  152. ASSERT_EQ(entt::any_cast<const int &>(any), 2);
  153. ASSERT_EQ(entt::any_cast<int>(any), 2);
  154. ASSERT_EQ(any.data(), nullptr);
  155. ASSERT_EQ(std::as_const(any).data(), &value);
  156. any.emplace<const int &>(value);
  157. ASSERT_TRUE(any);
  158. ASSERT_FALSE(any.owner());
  159. ASSERT_EQ(any.policy(), entt::any_policy::cref);
  160. ASSERT_EQ(any.info(), entt::type_id<int>());
  161. ASSERT_EQ(entt::any_cast<const int>(&any), &value);
  162. auto other = any.as_ref();
  163. ASSERT_TRUE(other);
  164. ASSERT_FALSE(other.owner());
  165. ASSERT_EQ(other.policy(), entt::any_policy::cref);
  166. ASSERT_EQ(other.info(), entt::type_id<int>());
  167. ASSERT_EQ(entt::any_cast<int>(other), 2);
  168. ASSERT_EQ(other.data(), any.data());
  169. }
  170. TEST(Any, SBOCopyConstruction) {
  171. const entt::any any{2};
  172. entt::any other{any};
  173. ASSERT_TRUE(any);
  174. ASSERT_TRUE(other);
  175. ASSERT_TRUE(other.owner());
  176. ASSERT_EQ(other.policy(), entt::any_policy::embedded);
  177. ASSERT_EQ(other.info(), entt::type_id<int>());
  178. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  179. ASSERT_EQ(entt::any_cast<int>(other), 2);
  180. }
  181. TEST(Any, SBOCopyAssignment) {
  182. const entt::any any{2};
  183. entt::any other{3};
  184. other = any;
  185. ASSERT_TRUE(any);
  186. ASSERT_TRUE(other);
  187. ASSERT_TRUE(other.owner());
  188. ASSERT_EQ(other.policy(), entt::any_policy::embedded);
  189. ASSERT_EQ(other.info(), entt::type_id<int>());
  190. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  191. ASSERT_EQ(entt::any_cast<int>(other), 2);
  192. }
  193. TEST(Any, SBOSelfCopyAssignment) {
  194. entt::any any{2};
  195. // avoid warnings due to self-assignment
  196. any = *&any;
  197. ASSERT_TRUE(any);
  198. ASSERT_TRUE(any.owner());
  199. ASSERT_EQ(any.policy(), entt::any_policy::embedded);
  200. ASSERT_EQ(any.info(), entt::type_id<int>());
  201. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  202. ASSERT_EQ(entt::any_cast<int>(any), 2);
  203. }
  204. TEST(Any, SBOMoveConstruction) {
  205. entt::any any{2};
  206. entt::any other{std::move(any)};
  207. test::is_initialized(any);
  208. ASSERT_TRUE(any);
  209. ASSERT_TRUE(other);
  210. ASSERT_TRUE(other.owner());
  211. ASSERT_EQ(other.policy(), entt::any_policy::embedded);
  212. ASSERT_EQ(other.info(), entt::type_id<int>());
  213. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  214. ASSERT_EQ(entt::any_cast<int>(other), 2);
  215. }
  216. TEST(Any, SBOMoveAssignment) {
  217. entt::any any{2};
  218. entt::any other{3};
  219. other = std::move(any);
  220. test::is_initialized(any);
  221. ASSERT_TRUE(any);
  222. ASSERT_TRUE(other);
  223. ASSERT_TRUE(other.owner());
  224. ASSERT_EQ(other.policy(), entt::any_policy::embedded);
  225. ASSERT_EQ(other.info(), entt::type_id<int>());
  226. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  227. ASSERT_EQ(entt::any_cast<int>(other), 2);
  228. }
  229. TEST(AnyDeathTest, SBOSelfMoveAssignment) {
  230. entt::any any{2};
  231. // avoid warnings due to self-assignment
  232. any = std::move(*&any);
  233. ASSERT_FALSE(any);
  234. ASSERT_FALSE(any.owner());
  235. ASSERT_EQ(any.policy(), entt::any_policy::empty);
  236. ASSERT_EQ(any.info(), entt::type_id<void>());
  237. ASSERT_EQ(any.data(), nullptr);
  238. }
  239. TEST(Any, SBODirectAssignment) {
  240. entt::any any{};
  241. any = 2;
  242. ASSERT_TRUE(any);
  243. ASSERT_TRUE(any.owner());
  244. ASSERT_EQ(any.policy(), entt::any_policy::embedded);
  245. ASSERT_EQ(any.info(), entt::type_id<int>());
  246. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  247. ASSERT_EQ(entt::any_cast<int>(any), 2);
  248. }
  249. TEST(Any, SBOAssignValue) {
  250. entt::any any{2};
  251. const entt::any other{3};
  252. const entt::any invalid{'c'};
  253. ASSERT_TRUE(any);
  254. ASSERT_EQ(entt::any_cast<int>(any), 2);
  255. ASSERT_TRUE(any.assign(other));
  256. ASSERT_FALSE(any.assign(invalid));
  257. ASSERT_EQ(entt::any_cast<int>(any), 3);
  258. }
  259. TEST(Any, SBOAsRefAssignValue) {
  260. int value = 2;
  261. entt::any any{entt::forward_as_any(value)};
  262. const entt::any other{3};
  263. const entt::any invalid{'c'};
  264. ASSERT_TRUE(any);
  265. ASSERT_EQ(entt::any_cast<int>(any), 2);
  266. ASSERT_TRUE(any.assign(other));
  267. ASSERT_FALSE(any.assign(invalid));
  268. ASSERT_EQ(entt::any_cast<int>(any), 3);
  269. ASSERT_EQ(value, 3);
  270. }
  271. TEST(Any, SBOAsConstRefAssignValue) {
  272. const int value = 2;
  273. entt::any any{entt::forward_as_any(value)};
  274. const entt::any other{3};
  275. const entt::any invalid{'c'};
  276. ASSERT_TRUE(any);
  277. ASSERT_EQ(entt::any_cast<int>(any), 2);
  278. ASSERT_FALSE(any.assign(other));
  279. ASSERT_FALSE(any.assign(invalid));
  280. ASSERT_EQ(entt::any_cast<int>(any), 2);
  281. ASSERT_EQ(value, 2);
  282. }
  283. TEST(Any, SBOTransferValue) {
  284. entt::any any{2};
  285. ASSERT_TRUE(any);
  286. ASSERT_EQ(entt::any_cast<int>(any), 2);
  287. ASSERT_TRUE(any.assign(3));
  288. ASSERT_FALSE(any.assign('c'));
  289. ASSERT_EQ(entt::any_cast<int>(any), 3);
  290. }
  291. TEST(Any, SBOTransferConstValue) {
  292. const int value = 3;
  293. entt::any any{2};
  294. ASSERT_TRUE(any);
  295. ASSERT_EQ(entt::any_cast<int>(any), 2);
  296. ASSERT_TRUE(any.assign(entt::forward_as_any(value)));
  297. ASSERT_EQ(entt::any_cast<int>(any), 3);
  298. }
  299. TEST(Any, SBOAsRefTransferValue) {
  300. int value = 2;
  301. entt::any any{entt::forward_as_any(value)};
  302. ASSERT_TRUE(any);
  303. ASSERT_EQ(entt::any_cast<int>(any), 2);
  304. ASSERT_TRUE(any.assign(3));
  305. ASSERT_FALSE(any.assign('c'));
  306. ASSERT_EQ(entt::any_cast<int>(any), 3);
  307. ASSERT_EQ(value, 3);
  308. }
  309. TEST(Any, SBOAsConstRefTransferValue) {
  310. const int value = 2;
  311. entt::any any{entt::forward_as_any(value)};
  312. ASSERT_TRUE(any);
  313. ASSERT_EQ(entt::any_cast<int>(any), 2);
  314. ASSERT_FALSE(any.assign(3));
  315. ASSERT_FALSE(any.assign('c'));
  316. ASSERT_EQ(entt::any_cast<int>(any), 2);
  317. ASSERT_EQ(value, 2);
  318. }
  319. TEST(Any, NoSBOInPlaceConstruction) {
  320. std::unique_ptr<fat> elem = std::make_unique<fat>(.1, .2, .3, .4);
  321. // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
  322. entt::any any{std::in_place, elem.release()};
  323. ASSERT_TRUE(any);
  324. ASSERT_TRUE(any.owner());
  325. ASSERT_EQ(any.policy(), entt::any_policy::dynamic);
  326. ASSERT_EQ(any.info(), entt::type_id<fat>());
  327. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  328. ASSERT_EQ(entt::any_cast<fat>(any), (fat{.1, .2, .3, .4}));
  329. auto other = any.as_ref();
  330. ASSERT_TRUE(other);
  331. ASSERT_FALSE(other.owner());
  332. ASSERT_EQ(other.policy(), entt::any_policy::ref);
  333. ASSERT_EQ(other.info(), entt::type_id<fat>());
  334. ASSERT_EQ(entt::any_cast<fat>(other), (fat{.1, .2, .3, .4}));
  335. ASSERT_EQ(other.data(), any.data());
  336. }
  337. TEST(Any, NoSBOInPlaceNullptrConstruction) {
  338. fat *instance = nullptr;
  339. const entt::any any{std::in_place, instance};
  340. ASSERT_FALSE(any);
  341. ASSERT_FALSE(any.owner());
  342. ASSERT_EQ(any.policy(), entt::any_policy::empty);
  343. ASSERT_EQ(any.info(), entt::type_id<void>());
  344. }
  345. TEST(Any, NoSBOInPlaceTypeConstruction) {
  346. const fat instance{.1, .2, .3, .4};
  347. entt::any any{std::in_place_type<fat>, instance};
  348. ASSERT_TRUE(any);
  349. ASSERT_TRUE(any.owner());
  350. ASSERT_EQ(any.policy(), entt::any_policy::dynamic);
  351. ASSERT_EQ(any.info(), entt::type_id<fat>());
  352. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  353. ASSERT_EQ(entt::any_cast<fat>(any), instance);
  354. auto other = any.as_ref();
  355. ASSERT_TRUE(other);
  356. ASSERT_FALSE(other.owner());
  357. ASSERT_EQ(other.policy(), entt::any_policy::ref);
  358. ASSERT_EQ(other.info(), entt::type_id<fat>());
  359. ASSERT_EQ(entt::any_cast<fat>(other), (fat{.1, .2, .3, .4}));
  360. ASSERT_EQ(other.data(), any.data());
  361. }
  362. TEST(Any, NoSBOAsRefConstruction) {
  363. fat instance{.1, .2, .3, .4};
  364. entt::any any{entt::forward_as_any(instance)};
  365. ASSERT_TRUE(any);
  366. ASSERT_FALSE(any.owner());
  367. ASSERT_EQ(any.policy(), entt::any_policy::ref);
  368. ASSERT_EQ(any.info(), entt::type_id<fat>());
  369. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  370. ASSERT_EQ(entt::any_cast<const fat>(&any), &instance);
  371. ASSERT_EQ(entt::any_cast<fat>(&any), &instance);
  372. ASSERT_EQ(entt::any_cast<const fat>(&std::as_const(any)), &instance);
  373. ASSERT_EQ(entt::any_cast<fat>(&std::as_const(any)), &instance);
  374. ASSERT_EQ(entt::any_cast<const fat &>(any), instance);
  375. ASSERT_EQ(entt::any_cast<fat>(any), instance);
  376. ASSERT_EQ(any.data(), &instance);
  377. ASSERT_EQ(std::as_const(any).data(), &instance);
  378. any.emplace<fat &>(instance);
  379. ASSERT_TRUE(any);
  380. ASSERT_FALSE(any.owner());
  381. ASSERT_EQ(any.policy(), entt::any_policy::ref);
  382. ASSERT_EQ(any.info(), entt::type_id<fat>());
  383. ASSERT_EQ(entt::any_cast<fat>(&any), &instance);
  384. auto other = any.as_ref();
  385. ASSERT_TRUE(other);
  386. ASSERT_FALSE(other.owner());
  387. ASSERT_EQ(other.policy(), entt::any_policy::ref);
  388. ASSERT_EQ(other.info(), entt::type_id<fat>());
  389. ASSERT_EQ(entt::any_cast<fat>(other), (fat{.1, .2, .3, .4}));
  390. ASSERT_EQ(other.data(), any.data());
  391. }
  392. TEST(Any, NoSBOAsConstRefConstruction) {
  393. const fat instance{.1, .2, .3, .4};
  394. entt::any any{entt::forward_as_any(instance)};
  395. ASSERT_TRUE(any);
  396. ASSERT_FALSE(any.owner());
  397. ASSERT_EQ(any.policy(), entt::any_policy::cref);
  398. ASSERT_EQ(any.info(), entt::type_id<fat>());
  399. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  400. ASSERT_EQ(entt::any_cast<const fat>(&any), &instance);
  401. ASSERT_EQ(entt::any_cast<fat>(&any), nullptr);
  402. ASSERT_EQ(entt::any_cast<const fat>(&std::as_const(any)), &instance);
  403. ASSERT_EQ(entt::any_cast<fat>(&std::as_const(any)), &instance);
  404. ASSERT_EQ(entt::any_cast<const fat &>(any), instance);
  405. ASSERT_EQ(entt::any_cast<fat>(any), instance);
  406. ASSERT_EQ(any.data(), nullptr);
  407. ASSERT_EQ(std::as_const(any).data(), &instance);
  408. any.emplace<const fat &>(instance);
  409. ASSERT_TRUE(any);
  410. ASSERT_FALSE(any.owner());
  411. ASSERT_EQ(any.policy(), entt::any_policy::cref);
  412. ASSERT_EQ(any.info(), entt::type_id<fat>());
  413. ASSERT_EQ(entt::any_cast<const fat>(&any), &instance);
  414. auto other = any.as_ref();
  415. ASSERT_TRUE(other);
  416. ASSERT_FALSE(other.owner());
  417. ASSERT_EQ(other.policy(), entt::any_policy::cref);
  418. ASSERT_EQ(other.info(), entt::type_id<fat>());
  419. ASSERT_EQ(entt::any_cast<fat>(other), (fat{.1, .2, .3, .4}));
  420. ASSERT_EQ(other.data(), any.data());
  421. }
  422. TEST(Any, NoSBOCopyConstruction) {
  423. const fat instance{.1, .2, .3, .4};
  424. const entt::any any{instance};
  425. entt::any other{any};
  426. ASSERT_TRUE(any);
  427. ASSERT_TRUE(other);
  428. ASSERT_TRUE(other.owner());
  429. ASSERT_EQ(other.policy(), entt::any_policy::dynamic);
  430. ASSERT_EQ(other.info(), entt::type_id<fat>());
  431. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  432. ASSERT_EQ(entt::any_cast<fat>(other), instance);
  433. }
  434. TEST(Any, NoSBOCopyAssignment) {
  435. const fat instance{.1, .2, .3, .4};
  436. const entt::any any{instance};
  437. entt::any other{3};
  438. other = any;
  439. ASSERT_TRUE(any);
  440. ASSERT_TRUE(other);
  441. ASSERT_TRUE(other.owner());
  442. ASSERT_EQ(other.policy(), entt::any_policy::dynamic);
  443. ASSERT_EQ(other.info(), entt::type_id<fat>());
  444. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  445. ASSERT_EQ(entt::any_cast<fat>(other), instance);
  446. }
  447. TEST(Any, NoSBOSelfCopyAssignment) {
  448. const fat instance{.1, .2, .3, .4};
  449. entt::any any{instance};
  450. // avoid warnings due to self-assignment
  451. any = *&any;
  452. ASSERT_TRUE(any);
  453. ASSERT_TRUE(any.owner());
  454. ASSERT_EQ(any.policy(), entt::any_policy::dynamic);
  455. ASSERT_EQ(any.info(), entt::type_id<fat>());
  456. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  457. ASSERT_EQ(entt::any_cast<fat>(any), instance);
  458. }
  459. TEST(Any, NoSBOMoveConstruction) {
  460. const fat instance{.1, .2, .3, .4};
  461. entt::any any{instance};
  462. entt::any other{std::move(any)};
  463. test::is_initialized(any);
  464. ASSERT_TRUE(any);
  465. ASSERT_TRUE(other);
  466. ASSERT_TRUE(other.owner());
  467. ASSERT_EQ(other.policy(), entt::any_policy::dynamic);
  468. ASSERT_EQ(other.info(), entt::type_id<fat>());
  469. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  470. ASSERT_EQ(entt::any_cast<fat>(other), instance);
  471. }
  472. TEST(Any, NoSBOMoveAssignment) {
  473. const fat instance{.1, .2, .3, .4};
  474. entt::any any{instance};
  475. entt::any other{3};
  476. other = std::move(any);
  477. test::is_initialized(any);
  478. ASSERT_TRUE(any);
  479. ASSERT_TRUE(other);
  480. ASSERT_TRUE(other.owner());
  481. ASSERT_EQ(other.policy(), entt::any_policy::dynamic);
  482. ASSERT_EQ(other.info(), entt::type_id<fat>());
  483. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  484. ASSERT_EQ(entt::any_cast<fat>(other), instance);
  485. }
  486. TEST(AnyDeathTest, NoSBOSelfMoveAssignment) {
  487. const fat instance{.1, .2, .3, .4};
  488. entt::any any{instance};
  489. // avoid warnings due to self-assignment
  490. any = std::move(*&any);
  491. ASSERT_FALSE(any);
  492. ASSERT_FALSE(any.owner());
  493. ASSERT_EQ(any.policy(), entt::any_policy::empty);
  494. ASSERT_EQ(any.info(), entt::type_id<void>());
  495. ASSERT_EQ(any.data(), nullptr);
  496. }
  497. TEST(Any, NoSBODirectAssignment) {
  498. const fat instance{.1, .2, .3, .4};
  499. entt::any any{};
  500. any = instance;
  501. ASSERT_TRUE(any);
  502. ASSERT_TRUE(any.owner());
  503. ASSERT_EQ(any.policy(), entt::any_policy::dynamic);
  504. ASSERT_EQ(any.info(), entt::type_id<fat>());
  505. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  506. ASSERT_EQ(entt::any_cast<fat>(any), instance);
  507. }
  508. TEST(Any, NoSBOAssignValue) {
  509. entt::any any{fat{.1, .2, .3, .4}};
  510. const entt::any other{fat{.0, .1, .2, .3}};
  511. const entt::any invalid{'c'};
  512. const void *addr = std::as_const(any).data();
  513. ASSERT_TRUE(any);
  514. ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.1, .2, .3, .4}));
  515. ASSERT_TRUE(any.assign(other));
  516. ASSERT_FALSE(any.assign(invalid));
  517. ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.0, .1, .2, .3}));
  518. ASSERT_EQ(addr, std::as_const(any).data());
  519. }
  520. TEST(Any, NoSBOAsRefAssignValue) {
  521. fat instance{.1, .2, .3, .4};
  522. entt::any any{entt::forward_as_any(instance)};
  523. const entt::any other{fat{.0, .1, .2, .3}};
  524. const entt::any invalid{'c'};
  525. ASSERT_TRUE(any);
  526. ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.1, .2, .3, .4}));
  527. ASSERT_TRUE(any.assign(other));
  528. ASSERT_FALSE(any.assign(invalid));
  529. ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.0, .1, .2, .3}));
  530. ASSERT_EQ(instance, (fat{.0, .1, .2, .3}));
  531. }
  532. TEST(Any, NoSBOAsConstRefAssignValue) {
  533. const fat instance{.1, .2, .3, .4};
  534. entt::any any{entt::forward_as_any(instance)};
  535. const entt::any other{fat{.0, .1, .2, .3}};
  536. const entt::any invalid{'c'};
  537. ASSERT_TRUE(any);
  538. ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.1, .2, .3, .4}));
  539. ASSERT_FALSE(any.assign(other));
  540. ASSERT_FALSE(any.assign(invalid));
  541. ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.1, .2, .3, .4}));
  542. ASSERT_EQ(instance, (fat{.1, .2, .3, .4}));
  543. }
  544. TEST(Any, NoSBOTransferValue) {
  545. entt::any any{fat{.1, .2, .3, .4}};
  546. const void *addr = std::as_const(any).data();
  547. ASSERT_TRUE(any);
  548. ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.1, .2, .3, .4}));
  549. ASSERT_TRUE(any.assign(fat{.0, .1, .2, .3}));
  550. ASSERT_FALSE(any.assign('c'));
  551. ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.0, .1, .2, .3}));
  552. ASSERT_EQ(addr, std::as_const(any).data());
  553. }
  554. TEST(Any, NoSBOTransferConstValue) {
  555. const fat instance{.0, .1, .2, .3};
  556. entt::any any{fat{.1, .2, .3, .4}};
  557. const void *addr = std::as_const(any).data();
  558. ASSERT_TRUE(any);
  559. ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.1, .2, .3, .4}));
  560. ASSERT_TRUE(any.assign(entt::forward_as_any(instance)));
  561. ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.0, .1, .2, .3}));
  562. ASSERT_EQ(addr, std::as_const(any).data());
  563. }
  564. TEST(Any, NoSBOAsRefTransferValue) {
  565. fat instance{.1, .2, .3, .4};
  566. entt::any any{entt::forward_as_any(instance)};
  567. const void *addr = std::as_const(any).data();
  568. ASSERT_TRUE(any);
  569. ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.1, .2, .3, .4}));
  570. ASSERT_TRUE(any.assign(fat{.0, .1, .2, .3}));
  571. ASSERT_FALSE(any.assign('c'));
  572. ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.0, .1, .2, .3}));
  573. ASSERT_EQ(instance, (fat{.0, .1, .2, .3}));
  574. ASSERT_EQ(addr, std::as_const(any).data());
  575. }
  576. TEST(Any, NoSBOAsConstRefTransferValue) {
  577. const fat instance{.1, .2, .3, .4};
  578. entt::any any{entt::forward_as_any(instance)};
  579. const void *addr = std::as_const(any).data();
  580. ASSERT_TRUE(any);
  581. ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.1, .2, .3, .4}));
  582. ASSERT_FALSE(any.assign(fat{.0, .1, .2, .3}));
  583. ASSERT_FALSE(any.assign('c'));
  584. ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.1, .2, .3, .4}));
  585. ASSERT_EQ(instance, (fat{.1, .2, .3, .4}));
  586. ASSERT_EQ(addr, std::as_const(any).data());
  587. }
  588. TEST(Any, VoidInPlaceTypeConstruction) {
  589. entt::any any{std::in_place_type<void>};
  590. ASSERT_FALSE(any);
  591. ASSERT_FALSE(any.owner());
  592. ASSERT_EQ(any.policy(), entt::any_policy::empty);
  593. ASSERT_EQ(any.info(), entt::type_id<void>());
  594. ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
  595. }
  596. TEST(Any, VoidCopyConstruction) {
  597. entt::any any{std::in_place_type<void>};
  598. entt::any other{any};
  599. ASSERT_FALSE(any);
  600. ASSERT_FALSE(other);
  601. ASSERT_FALSE(other.owner());
  602. ASSERT_EQ(other.policy(), entt::any_policy::empty);
  603. ASSERT_EQ(other.info(), entt::type_id<void>());
  604. ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
  605. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  606. }
  607. TEST(Any, VoidCopyAssignment) {
  608. const entt::any any{std::in_place_type<void>};
  609. entt::any other{2};
  610. other = any;
  611. ASSERT_FALSE(any);
  612. ASSERT_FALSE(other);
  613. ASSERT_FALSE(other.owner());
  614. ASSERT_EQ(other.policy(), entt::any_policy::empty);
  615. ASSERT_EQ(other.info(), entt::type_id<void>());
  616. ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
  617. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  618. }
  619. TEST(Any, VoidSelfCopyAssignment) {
  620. entt::any any{std::in_place_type<void>};
  621. // avoid warnings due to self-assignment
  622. any = *&any;
  623. ASSERT_FALSE(any);
  624. ASSERT_FALSE(any.owner());
  625. ASSERT_EQ(any.policy(), entt::any_policy::empty);
  626. ASSERT_EQ(any.info(), entt::type_id<void>());
  627. ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
  628. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  629. }
  630. TEST(Any, VoidMoveConstruction) {
  631. entt::any any{std::in_place_type<void>};
  632. entt::any other{std::move(any)};
  633. test::is_initialized(any);
  634. ASSERT_FALSE(any);
  635. ASSERT_FALSE(other);
  636. ASSERT_FALSE(other.owner());
  637. ASSERT_EQ(other.policy(), entt::any_policy::empty);
  638. ASSERT_EQ(other.info(), entt::type_id<void>());
  639. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  640. }
  641. TEST(Any, VoidMoveAssignment) {
  642. entt::any any{std::in_place_type<void>};
  643. entt::any other{2};
  644. other = std::move(any);
  645. test::is_initialized(any);
  646. ASSERT_FALSE(any);
  647. ASSERT_FALSE(other);
  648. ASSERT_FALSE(other.owner());
  649. ASSERT_EQ(other.policy(), entt::any_policy::empty);
  650. ASSERT_EQ(other.info(), entt::type_id<void>());
  651. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  652. }
  653. TEST(AnyDeathTest, VoidSelfMoveAssignment) {
  654. entt::any any{std::in_place_type<void>};
  655. // avoid warnings due to self-assignment
  656. any = std::move(*&any);
  657. ASSERT_FALSE(any);
  658. ASSERT_FALSE(any.owner());
  659. ASSERT_EQ(any.policy(), entt::any_policy::empty);
  660. ASSERT_EQ(any.info(), entt::type_id<void>());
  661. ASSERT_EQ(any.data(), nullptr);
  662. }
  663. TEST(Any, SBOMoveValidButUnspecifiedState) {
  664. entt::any any{2};
  665. entt::any other{std::move(any)};
  666. const entt::any valid = std::move(other);
  667. test::is_initialized(any);
  668. test::is_initialized(other);
  669. ASSERT_TRUE(any);
  670. ASSERT_TRUE(other);
  671. ASSERT_TRUE(valid);
  672. }
  673. TEST(Any, NoSBOMoveValidButUnspecifiedState) {
  674. const fat instance{.1, .2, .3, .4};
  675. entt::any any{instance};
  676. entt::any other{std::move(any)};
  677. const entt::any valid = std::move(other);
  678. test::is_initialized(any);
  679. test::is_initialized(other);
  680. ASSERT_TRUE(any);
  681. ASSERT_TRUE(other);
  682. ASSERT_TRUE(valid);
  683. }
  684. TEST(Any, VoidMoveValidButUnspecifiedState) {
  685. entt::any any{std::in_place_type<void>};
  686. entt::any other{std::move(any)};
  687. const entt::any valid = std::move(other);
  688. test::is_initialized(any);
  689. test::is_initialized(other);
  690. ASSERT_FALSE(any);
  691. ASSERT_FALSE(other);
  692. ASSERT_FALSE(valid);
  693. }
  694. TEST(Any, SBODestruction) {
  695. using tracker_type = tracker<0u>;
  696. int counter{};
  697. {
  698. entt::any any{std::in_place_type<tracker_type>, counter};
  699. any.emplace<tracker_type>(counter);
  700. any = tracker_type{counter};
  701. entt::any other{std::move(any)};
  702. any = std::move(other);
  703. }
  704. ASSERT_EQ(counter, 6);
  705. }
  706. TEST(Any, NoSBODestruction) {
  707. using tracker_type = tracker<entt::any::length>;
  708. int counter{};
  709. {
  710. entt::any any{std::in_place_type<tracker_type>, counter};
  711. any.emplace<tracker_type>(counter);
  712. any = tracker_type{counter};
  713. entt::any other{std::move(any)};
  714. any = std::move(other);
  715. }
  716. ASSERT_EQ(counter, 4);
  717. }
  718. TEST(Any, VoidDestruction) {
  719. // just let asan tell us if everything is ok here
  720. [[maybe_unused]] const entt::any any{std::in_place_type<void>};
  721. }
  722. TEST(Any, Emplace) {
  723. entt::any any{};
  724. any.emplace<int>(2);
  725. ASSERT_TRUE(any);
  726. ASSERT_TRUE(any.owner());
  727. ASSERT_EQ(any.policy(), entt::any_policy::embedded);
  728. ASSERT_EQ(any.info(), entt::type_id<int>());
  729. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  730. ASSERT_EQ(entt::any_cast<int>(any), 2);
  731. }
  732. TEST(Any, EmplaceVoid) {
  733. entt::any any{};
  734. any.emplace<void>();
  735. ASSERT_FALSE(any);
  736. ASSERT_FALSE(any.owner());
  737. ASSERT_EQ(any.policy(), entt::any_policy::empty);
  738. ASSERT_EQ(any.info(), entt::type_id<void>());
  739. }
  740. TEST(Any, Reset) {
  741. entt::any any{2};
  742. ASSERT_TRUE(any);
  743. ASSERT_TRUE(any.owner());
  744. ASSERT_EQ(any.policy(), entt::any_policy::embedded);
  745. ASSERT_EQ(any.info(), entt::type_id<int>());
  746. any.reset();
  747. ASSERT_FALSE(any);
  748. ASSERT_FALSE(any.owner());
  749. ASSERT_EQ(any.policy(), entt::any_policy::empty);
  750. ASSERT_EQ(any.info(), entt::type_id<void>());
  751. int value = 2;
  752. any.emplace<int &>(value);
  753. ASSERT_TRUE(any);
  754. ASSERT_FALSE(any.owner());
  755. ASSERT_EQ(any.policy(), entt::any_policy::ref);
  756. ASSERT_EQ(any.info(), entt::type_id<int>());
  757. any.reset();
  758. ASSERT_FALSE(any);
  759. ASSERT_FALSE(any.owner());
  760. ASSERT_EQ(any.policy(), entt::any_policy::empty);
  761. ASSERT_EQ(any.info(), entt::type_id<void>());
  762. }
  763. TEST(Any, SBOSwap) {
  764. entt::any lhs{'c'};
  765. entt::any rhs{2};
  766. std::swap(lhs, rhs);
  767. ASSERT_TRUE(lhs.owner());
  768. ASSERT_TRUE(rhs.owner());
  769. ASSERT_EQ(lhs.policy(), entt::any_policy::embedded);
  770. ASSERT_EQ(rhs.policy(), entt::any_policy::embedded);
  771. ASSERT_EQ(lhs.info(), entt::type_id<int>());
  772. ASSERT_EQ(rhs.info(), entt::type_id<char>());
  773. ASSERT_EQ(entt::any_cast<char>(&lhs), nullptr);
  774. ASSERT_EQ(entt::any_cast<int>(&rhs), nullptr);
  775. ASSERT_EQ(entt::any_cast<int>(lhs), 2);
  776. ASSERT_EQ(entt::any_cast<char>(rhs), 'c');
  777. }
  778. TEST(Any, NoSBOSwap) {
  779. entt::any lhs{fat{.1, .2, .3, .4}};
  780. entt::any rhs{fat{.4, .3, .2, .1}};
  781. std::swap(lhs, rhs);
  782. ASSERT_TRUE(lhs.owner());
  783. ASSERT_TRUE(rhs.owner());
  784. ASSERT_EQ(lhs.policy(), entt::any_policy::dynamic);
  785. ASSERT_EQ(rhs.policy(), entt::any_policy::dynamic);
  786. ASSERT_EQ(entt::any_cast<fat>(lhs), (fat{.4, .3, .2, .1}));
  787. ASSERT_EQ(entt::any_cast<fat>(rhs), (fat{.1, .2, .3, .4}));
  788. }
  789. TEST(Any, VoidSwap) {
  790. entt::any lhs{std::in_place_type<void>};
  791. entt::any rhs{std::in_place_type<void>};
  792. const auto *pre = lhs.data();
  793. std::swap(lhs, rhs);
  794. ASSERT_FALSE(lhs.owner());
  795. ASSERT_FALSE(rhs.owner());
  796. ASSERT_EQ(lhs.policy(), entt::any_policy::empty);
  797. ASSERT_EQ(rhs.policy(), entt::any_policy::empty);
  798. ASSERT_EQ(pre, lhs.data());
  799. }
  800. TEST(Any, SBOWithNoSBOSwap) {
  801. entt::any lhs{fat{.1, .2, .3, .4}};
  802. entt::any rhs{'c'};
  803. std::swap(lhs, rhs);
  804. ASSERT_TRUE(lhs.owner());
  805. ASSERT_TRUE(rhs.owner());
  806. ASSERT_EQ(lhs.policy(), entt::any_policy::embedded);
  807. ASSERT_EQ(rhs.policy(), entt::any_policy::dynamic);
  808. ASSERT_EQ(lhs.info(), entt::type_id<char>());
  809. ASSERT_EQ(rhs.info(), entt::type_id<fat>());
  810. ASSERT_EQ(entt::any_cast<fat>(&lhs), nullptr);
  811. ASSERT_EQ(entt::any_cast<char>(&rhs), nullptr);
  812. ASSERT_EQ(entt::any_cast<char>(lhs), 'c');
  813. ASSERT_EQ(entt::any_cast<fat>(rhs), (fat{.1, .2, .3, .4}));
  814. }
  815. TEST(Any, SBOWithRefSwap) {
  816. int value = 3;
  817. entt::any lhs{entt::forward_as_any(value)};
  818. entt::any rhs{'c'};
  819. std::swap(lhs, rhs);
  820. ASSERT_TRUE(lhs.owner());
  821. ASSERT_FALSE(rhs.owner());
  822. ASSERT_EQ(lhs.policy(), entt::any_policy::embedded);
  823. ASSERT_EQ(rhs.policy(), entt::any_policy::ref);
  824. ASSERT_EQ(lhs.info(), entt::type_id<char>());
  825. ASSERT_EQ(rhs.info(), entt::type_id<int>());
  826. ASSERT_EQ(entt::any_cast<int>(&lhs), nullptr);
  827. ASSERT_EQ(entt::any_cast<char>(&rhs), nullptr);
  828. ASSERT_EQ(entt::any_cast<char>(lhs), 'c');
  829. ASSERT_EQ(entt::any_cast<int>(rhs), 3);
  830. ASSERT_EQ(rhs.data(), &value);
  831. }
  832. TEST(Any, SBOWithConstRefSwap) {
  833. const int value = 3;
  834. entt::any lhs{entt::forward_as_any(value)};
  835. entt::any rhs{'c'};
  836. std::swap(lhs, rhs);
  837. ASSERT_TRUE(lhs.owner());
  838. ASSERT_FALSE(rhs.owner());
  839. ASSERT_EQ(lhs.policy(), entt::any_policy::embedded);
  840. ASSERT_EQ(rhs.policy(), entt::any_policy::cref);
  841. ASSERT_EQ(lhs.info(), entt::type_id<char>());
  842. ASSERT_EQ(rhs.info(), entt::type_id<int>());
  843. ASSERT_EQ(entt::any_cast<int>(&lhs), nullptr);
  844. ASSERT_EQ(entt::any_cast<char>(&rhs), nullptr);
  845. ASSERT_EQ(entt::any_cast<char>(lhs), 'c');
  846. ASSERT_EQ(entt::any_cast<int>(rhs), 3);
  847. ASSERT_EQ(rhs.data(), nullptr);
  848. ASSERT_EQ(std::as_const(rhs).data(), &value);
  849. }
  850. TEST(Any, SBOWithEmptySwap) {
  851. entt::any lhs{'c'};
  852. entt::any rhs{};
  853. std::swap(lhs, rhs);
  854. ASSERT_FALSE(lhs);
  855. ASSERT_FALSE(lhs.owner());
  856. ASSERT_EQ(lhs.policy(), entt::any_policy::empty);
  857. ASSERT_EQ(rhs.info(), entt::type_id<char>());
  858. ASSERT_EQ(entt::any_cast<char>(&lhs), nullptr);
  859. ASSERT_EQ(entt::any_cast<double>(&rhs), nullptr);
  860. ASSERT_EQ(entt::any_cast<char>(rhs), 'c');
  861. std::swap(lhs, rhs);
  862. ASSERT_FALSE(rhs);
  863. ASSERT_TRUE(lhs.owner());
  864. ASSERT_EQ(lhs.policy(), entt::any_policy::embedded);
  865. ASSERT_EQ(lhs.info(), entt::type_id<char>());
  866. ASSERT_EQ(entt::any_cast<double>(&lhs), nullptr);
  867. ASSERT_EQ(entt::any_cast<char>(&rhs), nullptr);
  868. ASSERT_EQ(entt::any_cast<char>(lhs), 'c');
  869. }
  870. TEST(Any, SBOWithVoidSwap) {
  871. entt::any lhs{'c'};
  872. entt::any rhs{std::in_place_type<void>};
  873. std::swap(lhs, rhs);
  874. ASSERT_FALSE(lhs);
  875. ASSERT_FALSE(lhs.owner());
  876. ASSERT_EQ(lhs.policy(), entt::any_policy::empty);
  877. ASSERT_EQ(rhs.info(), entt::type_id<char>());
  878. ASSERT_EQ(entt::any_cast<char>(&lhs), nullptr);
  879. ASSERT_EQ(entt::any_cast<double>(&rhs), nullptr);
  880. ASSERT_EQ(entt::any_cast<char>(rhs), 'c');
  881. std::swap(lhs, rhs);
  882. ASSERT_FALSE(rhs);
  883. ASSERT_TRUE(lhs.owner());
  884. ASSERT_EQ(lhs.policy(), entt::any_policy::embedded);
  885. ASSERT_EQ(lhs.info(), entt::type_id<char>());
  886. ASSERT_EQ(entt::any_cast<double>(&lhs), nullptr);
  887. ASSERT_EQ(entt::any_cast<char>(&rhs), nullptr);
  888. ASSERT_EQ(entt::any_cast<char>(lhs), 'c');
  889. }
  890. TEST(Any, NoSBOWithRefSwap) {
  891. int value = 3;
  892. entt::any lhs{entt::forward_as_any(value)};
  893. entt::any rhs{fat{.1, .2, .3, .4}};
  894. std::swap(lhs, rhs);
  895. ASSERT_TRUE(lhs.owner());
  896. ASSERT_FALSE(rhs.owner());
  897. ASSERT_EQ(lhs.policy(), entt::any_policy::dynamic);
  898. ASSERT_EQ(rhs.policy(), entt::any_policy::ref);
  899. ASSERT_EQ(lhs.info(), entt::type_id<fat>());
  900. ASSERT_EQ(rhs.info(), entt::type_id<int>());
  901. ASSERT_EQ(entt::any_cast<int>(&lhs), nullptr);
  902. ASSERT_EQ(entt::any_cast<fat>(&rhs), nullptr);
  903. ASSERT_EQ(entt::any_cast<fat>(lhs), (fat{.1, .2, .3, .4}));
  904. ASSERT_EQ(entt::any_cast<int>(rhs), 3);
  905. ASSERT_EQ(rhs.data(), &value);
  906. }
  907. TEST(Any, NoSBOWithConstRefSwap) {
  908. const int value = 3;
  909. entt::any lhs{entt::forward_as_any(value)};
  910. entt::any rhs{fat{.1, .2, .3, .4}};
  911. std::swap(lhs, rhs);
  912. ASSERT_TRUE(lhs.owner());
  913. ASSERT_FALSE(rhs.owner());
  914. ASSERT_EQ(lhs.policy(), entt::any_policy::dynamic);
  915. ASSERT_EQ(rhs.policy(), entt::any_policy::cref);
  916. ASSERT_EQ(lhs.info(), entt::type_id<fat>());
  917. ASSERT_EQ(rhs.info(), entt::type_id<int>());
  918. ASSERT_EQ(entt::any_cast<int>(&lhs), nullptr);
  919. ASSERT_EQ(entt::any_cast<fat>(&rhs), nullptr);
  920. ASSERT_EQ(entt::any_cast<fat>(lhs), (fat{.1, .2, .3, .4}));
  921. ASSERT_EQ(entt::any_cast<int>(rhs), 3);
  922. ASSERT_EQ(rhs.data(), nullptr);
  923. ASSERT_EQ(std::as_const(rhs).data(), &value);
  924. }
  925. TEST(Any, NoSBOWithEmptySwap) {
  926. entt::any lhs{fat{.1, .2, .3, .4}};
  927. entt::any rhs{};
  928. std::swap(lhs, rhs);
  929. ASSERT_FALSE(lhs);
  930. ASSERT_FALSE(lhs.owner());
  931. ASSERT_EQ(lhs.policy(), entt::any_policy::empty);
  932. ASSERT_EQ(rhs.info(), entt::type_id<fat>());
  933. ASSERT_EQ(entt::any_cast<fat>(&lhs), nullptr);
  934. ASSERT_EQ(entt::any_cast<double>(&rhs), nullptr);
  935. ASSERT_EQ(entt::any_cast<fat>(rhs), (fat{.1, .2, .3, .4}));
  936. std::swap(lhs, rhs);
  937. ASSERT_FALSE(rhs);
  938. ASSERT_TRUE(lhs.owner());
  939. ASSERT_EQ(lhs.policy(), entt::any_policy::dynamic);
  940. ASSERT_EQ(lhs.info(), entt::type_id<fat>());
  941. ASSERT_EQ(entt::any_cast<double>(&lhs), nullptr);
  942. ASSERT_EQ(entt::any_cast<fat>(&rhs), nullptr);
  943. ASSERT_EQ(entt::any_cast<fat>(lhs), (fat{.1, .2, .3, .4}));
  944. }
  945. TEST(Any, NoSBOWithVoidSwap) {
  946. entt::any lhs{fat{.1, .2, .3, .4}};
  947. entt::any rhs{std::in_place_type<void>};
  948. std::swap(lhs, rhs);
  949. ASSERT_FALSE(lhs);
  950. ASSERT_FALSE(lhs.owner());
  951. ASSERT_EQ(lhs.policy(), entt::any_policy::empty);
  952. ASSERT_EQ(rhs.info(), entt::type_id<fat>());
  953. ASSERT_EQ(entt::any_cast<fat>(&lhs), nullptr);
  954. ASSERT_EQ(entt::any_cast<double>(&rhs), nullptr);
  955. ASSERT_EQ(entt::any_cast<fat>(rhs), (fat{.1, .2, .3, .4}));
  956. std::swap(lhs, rhs);
  957. ASSERT_FALSE(rhs);
  958. ASSERT_TRUE(lhs.owner());
  959. ASSERT_EQ(lhs.policy(), entt::any_policy::dynamic);
  960. ASSERT_EQ(lhs.info(), entt::type_id<fat>());
  961. ASSERT_EQ(entt::any_cast<double>(&lhs), nullptr);
  962. ASSERT_EQ(entt::any_cast<fat>(&rhs), nullptr);
  963. ASSERT_EQ(entt::any_cast<fat>(lhs), (fat{.1, .2, .3, .4}));
  964. }
  965. TEST(Any, AsRef) {
  966. entt::any any{2};
  967. auto ref = any.as_ref();
  968. auto cref = std::as_const(any).as_ref();
  969. ASSERT_FALSE(ref.owner());
  970. ASSERT_FALSE(cref.owner());
  971. ASSERT_EQ(ref.policy(), entt::any_policy::ref);
  972. ASSERT_EQ(cref.policy(), entt::any_policy::cref);
  973. ASSERT_EQ(entt::any_cast<int>(&any), any.data());
  974. ASSERT_EQ(entt::any_cast<int>(&ref), any.data());
  975. ASSERT_EQ(entt::any_cast<int>(&cref), nullptr);
  976. ASSERT_EQ(entt::any_cast<const int>(&any), any.data());
  977. ASSERT_EQ(entt::any_cast<const int>(&ref), any.data());
  978. ASSERT_EQ(entt::any_cast<const int>(&cref), any.data());
  979. ASSERT_EQ(entt::any_cast<int>(any), 2);
  980. ASSERT_EQ(entt::any_cast<int>(ref), 2);
  981. ASSERT_EQ(entt::any_cast<int>(cref), 2);
  982. ASSERT_EQ(entt::any_cast<const int>(any), 2);
  983. ASSERT_EQ(entt::any_cast<const int>(ref), 2);
  984. ASSERT_EQ(entt::any_cast<const int>(cref), 2);
  985. ASSERT_EQ(entt::any_cast<int &>(any), 2);
  986. ASSERT_EQ(entt::any_cast<const int &>(any), 2);
  987. ASSERT_EQ(entt::any_cast<int &>(ref), 2);
  988. ASSERT_EQ(entt::any_cast<const int &>(ref), 2);
  989. ASSERT_EQ(entt::any_cast<int>(&cref), nullptr);
  990. ASSERT_EQ(entt::any_cast<const int &>(cref), 2);
  991. entt::any_cast<int &>(any) = 3;
  992. ASSERT_EQ(entt::any_cast<int>(any), 3);
  993. ASSERT_EQ(entt::any_cast<int>(ref), 3);
  994. ASSERT_EQ(entt::any_cast<int>(cref), 3);
  995. std::swap(ref, cref);
  996. ASSERT_FALSE(ref.owner());
  997. ASSERT_FALSE(cref.owner());
  998. ASSERT_EQ(ref.policy(), entt::any_policy::cref);
  999. ASSERT_EQ(cref.policy(), entt::any_policy::ref);
  1000. ASSERT_EQ(entt::any_cast<int>(&ref), nullptr);
  1001. ASSERT_EQ(entt::any_cast<int>(&cref), any.data());
  1002. ref = ref.as_ref();
  1003. cref = std::as_const(cref).as_ref();
  1004. ASSERT_FALSE(ref.owner());
  1005. ASSERT_FALSE(cref.owner());
  1006. ASSERT_EQ(ref.policy(), entt::any_policy::cref);
  1007. ASSERT_EQ(cref.policy(), entt::any_policy::cref);
  1008. ASSERT_EQ(entt::any_cast<int>(&ref), nullptr);
  1009. ASSERT_EQ(entt::any_cast<int>(&cref), nullptr);
  1010. ASSERT_EQ(entt::any_cast<const int>(&ref), any.data());
  1011. ASSERT_EQ(entt::any_cast<const int>(&cref), any.data());
  1012. ASSERT_EQ(entt::any_cast<int>(&ref), nullptr);
  1013. ASSERT_EQ(entt::any_cast<int>(&cref), nullptr);
  1014. ASSERT_EQ(entt::any_cast<const int &>(ref), 3);
  1015. ASSERT_EQ(entt::any_cast<const int &>(cref), 3);
  1016. ref = 2;
  1017. cref = 2;
  1018. ASSERT_TRUE(ref.owner());
  1019. ASSERT_TRUE(cref.owner());
  1020. ASSERT_EQ(ref.policy(), entt::any_policy::embedded);
  1021. ASSERT_EQ(cref.policy(), entt::any_policy::embedded);
  1022. ASSERT_NE(entt::any_cast<int>(&ref), nullptr);
  1023. ASSERT_NE(entt::any_cast<int>(&cref), nullptr);
  1024. ASSERT_EQ(entt::any_cast<int &>(ref), 2);
  1025. ASSERT_EQ(entt::any_cast<int &>(cref), 2);
  1026. ASSERT_EQ(entt::any_cast<const int &>(ref), 2);
  1027. ASSERT_EQ(entt::any_cast<const int &>(cref), 2);
  1028. ASSERT_NE(entt::any_cast<int>(&ref), any.data());
  1029. ASSERT_NE(entt::any_cast<int>(&cref), any.data());
  1030. }
  1031. TEST(Any, Comparable) {
  1032. const entt::any any{'c'};
  1033. const entt::any other{'a'};
  1034. ASSERT_EQ(any, any);
  1035. ASSERT_NE(other, any);
  1036. ASSERT_NE(any, entt::any{});
  1037. ASSERT_TRUE(any == any);
  1038. ASSERT_FALSE(other == any);
  1039. ASSERT_TRUE(any != other);
  1040. ASSERT_TRUE(entt::any{} != any);
  1041. }
  1042. TEST(Any, NoSBOComparable) {
  1043. const entt::any any{fat{.1, .2, .3, .4}};
  1044. const entt::any other{fat{.0, .1, .2, .3}};
  1045. ASSERT_EQ(any, any);
  1046. ASSERT_NE(other, any);
  1047. ASSERT_NE(any, entt::any{});
  1048. ASSERT_TRUE(any == any);
  1049. ASSERT_FALSE(other == any);
  1050. ASSERT_TRUE(any != other);
  1051. ASSERT_TRUE(entt::any{} != any);
  1052. }
  1053. TEST(Any, RefComparable) {
  1054. int value = 2;
  1055. const entt::any any{entt::forward_as_any(value)};
  1056. const entt::any other{3};
  1057. ASSERT_EQ(any, any);
  1058. ASSERT_NE(other, any);
  1059. ASSERT_NE(any, entt::any{});
  1060. ASSERT_TRUE(any == any);
  1061. ASSERT_FALSE(other == any);
  1062. ASSERT_TRUE(any != other);
  1063. ASSERT_TRUE(entt::any{} != any);
  1064. }
  1065. TEST(Any, ConstRefComparable) {
  1066. int value = 2;
  1067. const entt::any any{3};
  1068. const entt::any other{entt::make_any<const int &>(value)};
  1069. ASSERT_EQ(any, any);
  1070. ASSERT_NE(other, any);
  1071. ASSERT_NE(any, entt::any{});
  1072. ASSERT_TRUE(any == any);
  1073. ASSERT_FALSE(other == any);
  1074. ASSERT_TRUE(any != other);
  1075. ASSERT_TRUE(entt::any{} != any);
  1076. }
  1077. TEST(Any, UnrelatedComparable) {
  1078. const entt::any any{'c'};
  1079. const entt::any other{2};
  1080. ASSERT_EQ(any, any);
  1081. ASSERT_NE(other, any);
  1082. ASSERT_NE(any, entt::any{});
  1083. ASSERT_TRUE(any == any);
  1084. ASSERT_FALSE(other == any);
  1085. ASSERT_TRUE(any != other);
  1086. ASSERT_TRUE(entt::any{} != any);
  1087. }
  1088. TEST(Any, NonComparable) {
  1089. const test::non_comparable instance{};
  1090. auto any = entt::forward_as_any(instance);
  1091. ASSERT_EQ(any, any);
  1092. ASSERT_NE(any, entt::any{instance});
  1093. ASSERT_NE(entt::any{}, any);
  1094. ASSERT_TRUE(any == any);
  1095. ASSERT_FALSE(any == entt::any{instance});
  1096. ASSERT_TRUE(entt::any{} != any);
  1097. }
  1098. TEST(Any, AssociativeContainerOfNonComparable) {
  1099. const std::unordered_map<int, test::non_comparable> instance{};
  1100. auto any = entt::forward_as_any(instance);
  1101. ASSERT_EQ(any, any);
  1102. ASSERT_NE(any, entt::any{instance});
  1103. ASSERT_NE(entt::any{}, any);
  1104. ASSERT_TRUE(any == any);
  1105. ASSERT_FALSE(any == entt::any{instance});
  1106. ASSERT_TRUE(entt::any{} != any);
  1107. }
  1108. TEST(Any, SequenceContainerOfNonComparable) {
  1109. const std::vector<test::non_comparable> instance{};
  1110. auto any = entt::forward_as_any(instance);
  1111. ASSERT_EQ(any, any);
  1112. ASSERT_NE(any, entt::any{instance});
  1113. ASSERT_NE(entt::any{}, any);
  1114. ASSERT_TRUE(any == any);
  1115. ASSERT_FALSE(any == entt::any{instance});
  1116. ASSERT_TRUE(entt::any{} != any);
  1117. }
  1118. TEST(Any, CompareVoid) {
  1119. const entt::any any{std::in_place_type<void>};
  1120. ASSERT_EQ(any, any);
  1121. ASSERT_EQ(any, entt::any{std::in_place_type<void>});
  1122. ASSERT_NE(entt::any{'a'}, any);
  1123. ASSERT_EQ(any, entt::any{});
  1124. ASSERT_TRUE(any == any);
  1125. ASSERT_TRUE(any == entt::any{std::in_place_type<void>});
  1126. ASSERT_FALSE(entt::any{'a'} == any);
  1127. ASSERT_TRUE(any != entt::any{'a'});
  1128. ASSERT_FALSE(entt::any{} != any);
  1129. }
  1130. TEST(Any, AnyCast) {
  1131. entt::any any{2};
  1132. const auto &cany = any;
  1133. ASSERT_EQ(entt::any_cast<char>(&any), nullptr);
  1134. ASSERT_EQ(entt::any_cast<char>(&cany), nullptr);
  1135. ASSERT_EQ(*entt::any_cast<int>(&any), 2);
  1136. ASSERT_EQ(*entt::any_cast<int>(&cany), 2);
  1137. ASSERT_EQ(entt::any_cast<int &>(any), 2);
  1138. ASSERT_EQ(entt::any_cast<const int &>(cany), 2);
  1139. auto instance = std::make_unique<double>(2.);
  1140. entt::any ref{entt::forward_as_any(instance)};
  1141. entt::any cref{entt::forward_as_any(std::as_const(*instance))};
  1142. ASSERT_EQ(entt::any_cast<double>(std::move(cref)), 2.);
  1143. ASSERT_EQ(*entt::any_cast<std::unique_ptr<double>>(std::move(ref)), 2.);
  1144. ASSERT_EQ(entt::any_cast<int>(entt::any{2}), 2);
  1145. }
  1146. ENTT_DEBUG_TEST(AnyDeathTest, AnyCast) {
  1147. entt::any any{2};
  1148. const auto &cany = any;
  1149. ASSERT_DEATH([[maybe_unused]] auto &elem = entt::any_cast<double &>(any), "");
  1150. ASSERT_DEATH([[maybe_unused]] const auto &elem = entt::any_cast<const double &>(cany), "");
  1151. auto instance = std::make_unique<double>(2.);
  1152. entt::any ref{entt::forward_as_any(instance)};
  1153. const entt::any cref{entt::forward_as_any(std::as_const(*instance))};
  1154. ASSERT_DEATH([[maybe_unused]] auto elem = entt::any_cast<std::unique_ptr<double>>(std::as_const(ref).as_ref()), "");
  1155. ASSERT_DEATH([[maybe_unused]] auto elem = entt::any_cast<double>(entt::any{2}), "");
  1156. }
  1157. TEST(Any, MakeAny) {
  1158. int value = 2;
  1159. auto any = entt::make_any<int>(value);
  1160. auto ext = entt::make_any<int, sizeof(int), alignof(int)>(value);
  1161. auto ref = entt::make_any<int &>(value);
  1162. ASSERT_TRUE(any);
  1163. ASSERT_TRUE(ext);
  1164. ASSERT_TRUE(ref);
  1165. ASSERT_TRUE(any.owner());
  1166. ASSERT_TRUE(ext.owner());
  1167. ASSERT_FALSE(ref.owner());
  1168. ASSERT_EQ(any.policy(), entt::any_policy::embedded);
  1169. ASSERT_EQ(ext.policy(), entt::any_policy::embedded);
  1170. ASSERT_EQ(ref.policy(), entt::any_policy::ref);
  1171. ASSERT_EQ(entt::any_cast<const int &>(any), 2);
  1172. ASSERT_EQ(entt::any_cast<const int &>(ext), 2);
  1173. ASSERT_EQ(entt::any_cast<const int &>(ref), 2);
  1174. ASSERT_EQ(decltype(any)::length, entt::any::length);
  1175. ASSERT_NE(decltype(ext)::length, entt::any::length);
  1176. ASSERT_EQ(decltype(ref)::length, entt::any::length);
  1177. ASSERT_NE(any.data(), &value);
  1178. ASSERT_NE(ext.data(), &value);
  1179. ASSERT_EQ(ref.data(), &value);
  1180. }
  1181. TEST(Any, ForwardAsAny) {
  1182. int value = 2;
  1183. auto ref = entt::forward_as_any(value);
  1184. auto cref = entt::forward_as_any(std::as_const(value));
  1185. auto any = entt::forward_as_any(static_cast<int &&>(value));
  1186. ASSERT_TRUE(any);
  1187. ASSERT_TRUE(ref);
  1188. ASSERT_TRUE(cref);
  1189. ASSERT_TRUE(any.owner());
  1190. ASSERT_FALSE(ref.owner());
  1191. ASSERT_FALSE(cref.owner());
  1192. ASSERT_EQ(any.policy(), entt::any_policy::embedded);
  1193. ASSERT_EQ(ref.policy(), entt::any_policy::ref);
  1194. ASSERT_EQ(cref.policy(), entt::any_policy::cref);
  1195. ASSERT_NE(entt::any_cast<int>(&any), nullptr);
  1196. ASSERT_NE(entt::any_cast<int>(&ref), nullptr);
  1197. ASSERT_EQ(entt::any_cast<int>(&cref), nullptr);
  1198. ASSERT_EQ(entt::any_cast<const int &>(any), 2);
  1199. ASSERT_EQ(entt::any_cast<const int &>(ref), 2);
  1200. ASSERT_EQ(entt::any_cast<const int &>(cref), 2);
  1201. ASSERT_NE(any.data(), &value);
  1202. ASSERT_EQ(ref.data(), &value);
  1203. }
  1204. TEST(Any, NonCopyableType) {
  1205. const std::unique_ptr<int> value{};
  1206. entt::any any{std::in_place_type<std::unique_ptr<int>>};
  1207. entt::any other = entt::forward_as_any(value);
  1208. ASSERT_TRUE(any);
  1209. ASSERT_TRUE(other);
  1210. ASSERT_TRUE(any.owner());
  1211. ASSERT_FALSE(other.owner());
  1212. ASSERT_EQ(any.policy(), entt::any_policy::embedded);
  1213. ASSERT_EQ(other.policy(), entt::any_policy::cref);
  1214. ASSERT_EQ(any.info(), other.info());
  1215. ASSERT_FALSE(any.assign(other));
  1216. ASSERT_FALSE(any.assign(std::move(other)));
  1217. entt::any copy{any};
  1218. ASSERT_TRUE(any);
  1219. ASSERT_FALSE(copy);
  1220. ASSERT_TRUE(any.owner());
  1221. ASSERT_FALSE(copy.owner());
  1222. ASSERT_EQ(any.policy(), entt::any_policy::embedded);
  1223. ASSERT_EQ(copy.policy(), entt::any_policy::empty);
  1224. copy = any;
  1225. ASSERT_TRUE(any);
  1226. ASSERT_FALSE(copy);
  1227. ASSERT_TRUE(any.owner());
  1228. ASSERT_FALSE(copy.owner());
  1229. ASSERT_EQ(any.policy(), entt::any_policy::embedded);
  1230. ASSERT_EQ(copy.policy(), entt::any_policy::empty);
  1231. }
  1232. TEST(Any, NonCopyableValueType) {
  1233. std::vector<entt::any> vec{};
  1234. vec.emplace_back(std::in_place_type<std::unique_ptr<int>>);
  1235. vec.shrink_to_fit();
  1236. ASSERT_EQ(vec.size(), 1u);
  1237. ASSERT_EQ(vec.capacity(), 1u);
  1238. ASSERT_TRUE(vec[0u]);
  1239. // strong exception guarantee due to noexcept move ctor
  1240. vec.emplace_back(std::in_place_type<std::unique_ptr<int>>);
  1241. ASSERT_EQ(vec.size(), 2u);
  1242. ASSERT_TRUE(vec[0u]);
  1243. ASSERT_TRUE(vec[1u]);
  1244. }
  1245. TEST(Any, NonMovableType) {
  1246. entt::any any{std::in_place_type<test::non_movable>};
  1247. entt::any other{std::in_place_type<test::non_movable>};
  1248. ASSERT_TRUE(any);
  1249. ASSERT_TRUE(other);
  1250. ASSERT_TRUE(any.owner());
  1251. ASSERT_TRUE(other.owner());
  1252. ASSERT_EQ(any.policy(), entt::any_policy::dynamic);
  1253. ASSERT_EQ(other.policy(), entt::any_policy::dynamic);
  1254. ASSERT_EQ(any.info(), other.info());
  1255. ASSERT_TRUE(any.assign(other));
  1256. ASSERT_TRUE(any.assign(std::move(other)));
  1257. entt::any copy{any};
  1258. ASSERT_TRUE(any);
  1259. ASSERT_TRUE(copy);
  1260. ASSERT_TRUE(any.owner());
  1261. ASSERT_TRUE(copy.owner());
  1262. ASSERT_EQ(any.policy(), entt::any_policy::dynamic);
  1263. ASSERT_EQ(copy.policy(), entt::any_policy::dynamic);
  1264. copy = any;
  1265. ASSERT_TRUE(any);
  1266. ASSERT_TRUE(copy);
  1267. ASSERT_TRUE(any.owner());
  1268. ASSERT_TRUE(copy.owner());
  1269. ASSERT_EQ(any.policy(), entt::any_policy::dynamic);
  1270. ASSERT_EQ(copy.policy(), entt::any_policy::dynamic);
  1271. }
  1272. TEST(Any, Array) {
  1273. // NOLINTNEXTLINE(*-avoid-c-arrays)
  1274. entt::any any{std::in_place_type<int[1]>};
  1275. const entt::any copy{any};
  1276. ASSERT_TRUE(any);
  1277. ASSERT_FALSE(copy);
  1278. // NOLINTBEGIN(*-avoid-c-arrays)
  1279. ASSERT_EQ(any.info(), entt::type_id<int[1]>());
  1280. ASSERT_NE(entt::any_cast<int[1]>(&any), nullptr);
  1281. ASSERT_EQ(entt::any_cast<int[2]>(&any), nullptr);
  1282. // NOLINTEND(*-avoid-c-arrays)
  1283. ASSERT_EQ(entt::any_cast<int *>(&any), nullptr);
  1284. // NOLINTNEXTLINE(*-avoid-c-arrays)
  1285. entt::any_cast<int(&)[1]>(any)[0] = 2;
  1286. // NOLINTNEXTLINE(*-avoid-c-arrays)
  1287. ASSERT_EQ(entt::any_cast<const int(&)[1]>(std::as_const(any))[0], 2);
  1288. }
  1289. TEST(Any, CopyMoveReference) {
  1290. int value = 3;
  1291. auto any = entt::forward_as_any(value);
  1292. entt::any move = std::move(any);
  1293. entt::any copy = move;
  1294. test::is_initialized(any);
  1295. ASSERT_TRUE(any);
  1296. ASSERT_TRUE(move);
  1297. ASSERT_TRUE(copy);
  1298. ASSERT_FALSE(move.owner());
  1299. ASSERT_TRUE(copy.owner());
  1300. ASSERT_EQ(move.policy(), entt::any_policy::ref);
  1301. ASSERT_EQ(copy.policy(), entt::any_policy::embedded);
  1302. ASSERT_EQ(move.info(), entt::type_id<int>());
  1303. ASSERT_EQ(copy.info(), entt::type_id<int>());
  1304. ASSERT_EQ(std::as_const(move).data(), &value);
  1305. ASSERT_NE(std::as_const(copy).data(), &value);
  1306. ASSERT_EQ(entt::any_cast<int>(move), 3);
  1307. ASSERT_EQ(entt::any_cast<int>(copy), 3);
  1308. value = 2;
  1309. ASSERT_EQ(entt::any_cast<int &>(move), 2);
  1310. ASSERT_EQ(entt::any_cast<int &>(copy), 3);
  1311. }
  1312. TEST(Any, CopyMoveConstReference) {
  1313. int value = 3;
  1314. auto any = entt::forward_as_any(std::as_const(value));
  1315. entt::any move = std::move(any);
  1316. entt::any copy = move;
  1317. test::is_initialized(any);
  1318. ASSERT_TRUE(any);
  1319. ASSERT_TRUE(move);
  1320. ASSERT_TRUE(copy);
  1321. ASSERT_FALSE(move.owner());
  1322. ASSERT_TRUE(copy.owner());
  1323. ASSERT_EQ(move.policy(), entt::any_policy::cref);
  1324. ASSERT_EQ(copy.policy(), entt::any_policy::embedded);
  1325. ASSERT_EQ(move.info(), entt::type_id<int>());
  1326. ASSERT_EQ(copy.info(), entt::type_id<int>());
  1327. ASSERT_EQ(std::as_const(move).data(), &value);
  1328. ASSERT_NE(std::as_const(copy).data(), &value);
  1329. ASSERT_EQ(entt::any_cast<int>(move), 3);
  1330. ASSERT_EQ(entt::any_cast<int>(copy), 3);
  1331. value = 2;
  1332. ASSERT_EQ(entt::any_cast<const int &>(move), 2);
  1333. ASSERT_EQ(entt::any_cast<const int &>(copy), 3);
  1334. }
  1335. TEST(Any, SBOVsZeroedSBOSize) {
  1336. entt::any sbo{2};
  1337. const auto *broken = sbo.data();
  1338. entt::any other = std::move(sbo);
  1339. ASSERT_NE(broken, other.data());
  1340. entt::basic_any<0u> dyn{2};
  1341. const auto *valid = dyn.data();
  1342. entt::basic_any<0u> same = std::move(dyn);
  1343. ASSERT_EQ(valid, same.data());
  1344. }
  1345. TEST(Any, SboAlignment) {
  1346. constexpr auto alignment = alignof(over_aligned);
  1347. using any_type = entt::basic_any<alignment, alignment>;
  1348. std::array<any_type, 2u> sbo = {over_aligned{}, over_aligned{}};
  1349. const auto *data = sbo[0].data();
  1350. // NOLINTBEGIN(*-reinterpret-cast)
  1351. ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(sbo[0u].data()) % alignment) == 0u);
  1352. ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(sbo[1u].data()) % alignment) == 0u);
  1353. // NOLINTEND(*-reinterpret-cast)
  1354. std::swap(sbo[0], sbo[1]);
  1355. // NOLINTBEGIN(*-reinterpret-cast)
  1356. ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(sbo[0u].data()) % alignment) == 0u);
  1357. ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(sbo[1u].data()) % alignment) == 0u);
  1358. // NOLINTEND(*-reinterpret-cast)
  1359. ASSERT_NE(data, sbo[1].data());
  1360. }
  1361. TEST(Any, NoSboAlignment) {
  1362. constexpr auto alignment = alignof(over_aligned);
  1363. using any_type = entt::basic_any<alignment>;
  1364. std::array<any_type, 2u> nosbo = {over_aligned{}, over_aligned{}};
  1365. const auto *data = nosbo[0].data();
  1366. // NOLINTBEGIN(*-reinterpret-cast)
  1367. ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(nosbo[0u].data()) % alignment) == 0u);
  1368. ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(nosbo[1u].data()) % alignment) == 0u);
  1369. // NOLINTEND(*-reinterpret-cast)
  1370. std::swap(nosbo[0], nosbo[1]);
  1371. // NOLINTBEGIN(*-reinterpret-cast)
  1372. ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(nosbo[0u].data()) % alignment) == 0u);
  1373. ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(nosbo[1u].data()) % alignment) == 0u);
  1374. // NOLINTEND(*-reinterpret-cast)
  1375. ASSERT_EQ(data, nosbo[1].data());
  1376. }
  1377. TEST(Any, AggregatesMustWork) {
  1378. // the goal of this test is to enforce the requirements for aggregate types
  1379. entt::any{std::in_place_type<test::aggregate>, 2}.emplace<test::aggregate>(2);
  1380. }
  1381. TEST(Any, DeducedArrayType) {
  1382. entt::any any{"array of char"};
  1383. ASSERT_TRUE(any);
  1384. ASSERT_EQ(any.info(), entt::type_id<const char *>());
  1385. ASSERT_EQ((std::strcmp("array of char", entt::any_cast<const char *>(any))), 0);
  1386. any = "another array of char";
  1387. ASSERT_TRUE(any);
  1388. ASSERT_EQ(any.info(), entt::type_id<const char *>());
  1389. ASSERT_EQ((std::strcmp("another array of char", entt::any_cast<const char *>(any))), 0);
  1390. }
  1391. TEST(Any, ClassLevelNewDelete) {
  1392. // yeah, that's for code coverage purposes only :)
  1393. entt::any any{std::in_place_type<test::new_delete>, *std::make_unique<test::new_delete>(test::new_delete{3})};
  1394. ASSERT_TRUE(any);
  1395. ASSERT_EQ(any.info(), entt::type_id<test::new_delete>());
  1396. ASSERT_EQ(entt::any_cast<const test::new_delete &>(any).value, 3);
  1397. }