any.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. #include <algorithm>
  2. #include <string.h>
  3. #include <unordered_map>
  4. #include <vector>
  5. #include <gtest/gtest.h>
  6. #include <entt/core/any.hpp>
  7. struct empty {
  8. ~empty() { ++counter; }
  9. inline static int counter = 0;
  10. };
  11. struct fat {
  12. fat(double v1, double v2, double v3, double v4)
  13. : value{v1, v2, v3, v4}
  14. {}
  15. ~fat() { ++counter; }
  16. bool operator==(const fat &other) const {
  17. return std::equal(std::begin(value), std::end(value), std::begin(other.value), std::end(other.value));
  18. }
  19. inline static int counter{0};
  20. double value[4];
  21. };
  22. struct not_comparable {
  23. bool operator==(const not_comparable &) const = delete;
  24. };
  25. template<auto Sz>
  26. struct not_copyable {
  27. not_copyable(): payload{} {}
  28. not_copyable(const not_copyable &) = delete;
  29. not_copyable(not_copyable &&) = default;
  30. not_copyable & operator=(const not_copyable &) = delete;
  31. not_copyable & operator=(not_copyable &&) = default;
  32. double payload[Sz];
  33. };
  34. struct alignas(64u) over_aligned {};
  35. struct Any: ::testing::Test {
  36. void SetUp() override {
  37. fat::counter = 0;
  38. empty::counter = 0;
  39. }
  40. };
  41. TEST_F(Any, SBO) {
  42. entt::any any{'c'};
  43. ASSERT_TRUE(any);
  44. ASSERT_EQ(any.type(), entt::type_id<char>());
  45. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  46. ASSERT_EQ(entt::any_cast<char>(any), 'c');
  47. }
  48. TEST_F(Any, NoSBO) {
  49. fat instance{.1, .2, .3, .4};
  50. entt::any any{instance};
  51. ASSERT_TRUE(any);
  52. ASSERT_EQ(any.type(), entt::type_id<fat>());
  53. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  54. ASSERT_EQ(entt::any_cast<fat>(any), instance);
  55. }
  56. TEST_F(Any, Empty) {
  57. entt::any any{};
  58. ASSERT_FALSE(any);
  59. ASSERT_FALSE(any.type());
  60. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  61. ASSERT_EQ(any.data(), nullptr);
  62. }
  63. TEST_F(Any, SBOInPlaceTypeConstruction) {
  64. entt::any any{std::in_place_type<int>, 42};
  65. ASSERT_TRUE(any);
  66. ASSERT_EQ(any.type(), entt::type_id<int>());
  67. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  68. ASSERT_EQ(entt::any_cast<int>(any), 42);
  69. auto other = any.as_ref();
  70. ASSERT_TRUE(other);
  71. ASSERT_EQ(other.type(), entt::type_id<int>());
  72. ASSERT_EQ(entt::any_cast<int>(other), 42);
  73. ASSERT_EQ(other.data(), any.data());
  74. }
  75. TEST_F(Any, SBOAsRefConstruction) {
  76. int value = 42;
  77. entt::any any{std::ref(value)};
  78. ASSERT_TRUE(any);
  79. ASSERT_EQ(any.type(), entt::type_id<int>());
  80. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  81. ASSERT_EQ(entt::any_cast<const int>(&any), &value);
  82. ASSERT_EQ(entt::any_cast<int>(&any), &value);
  83. ASSERT_EQ(entt::any_cast<const int>(&std::as_const(any)), &value);
  84. ASSERT_EQ(entt::any_cast<int>(&std::as_const(any)), &value);
  85. ASSERT_EQ(entt::any_cast<const int &>(any), 42);
  86. ASSERT_EQ(entt::any_cast<int>(any), 42);
  87. ASSERT_EQ(any.data(), &value);
  88. ASSERT_EQ(std::as_const(any).data(), &value);
  89. any = std::ref(value);
  90. ASSERT_TRUE(any);
  91. ASSERT_EQ(any.type(), entt::type_id<int>());
  92. ASSERT_EQ(entt::any_cast<int>(&any), &value);
  93. auto other = any.as_ref();
  94. ASSERT_TRUE(other);
  95. ASSERT_EQ(other.type(), entt::type_id<int>());
  96. ASSERT_EQ(entt::any_cast<int>(other), 42);
  97. ASSERT_EQ(other.data(), any.data());
  98. }
  99. TEST_F(Any, SBOAsConstRefConstruction) {
  100. int value = 42;
  101. entt::any any{std::cref(value)};
  102. ASSERT_TRUE(any);
  103. ASSERT_EQ(any.type(), entt::type_id<int>());
  104. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  105. ASSERT_EQ(entt::any_cast<const int>(&any), &value);
  106. ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
  107. ASSERT_EQ(entt::any_cast<const int>(&std::as_const(any)), &value);
  108. ASSERT_EQ(entt::any_cast<int>(&std::as_const(any)), &value);
  109. ASSERT_EQ(entt::any_cast<const int &>(any), 42);
  110. ASSERT_EQ(entt::any_cast<int>(any), 42);
  111. ASSERT_EQ(any.data(), nullptr);
  112. ASSERT_EQ(std::as_const(any).data(), &value);
  113. any = std::cref(value);
  114. ASSERT_TRUE(any);
  115. ASSERT_EQ(any.type(), entt::type_id<int>());
  116. ASSERT_EQ(entt::any_cast<const int>(&any), &value);
  117. auto other = any.as_ref();
  118. ASSERT_TRUE(other);
  119. ASSERT_EQ(other.type(), entt::type_id<int>());
  120. ASSERT_EQ(entt::any_cast<int>(other), 42);
  121. ASSERT_EQ(other.data(), any.data());
  122. }
  123. TEST_F(Any, SBOCopyConstruction) {
  124. entt::any any{42};
  125. entt::any other{any};
  126. ASSERT_TRUE(any);
  127. ASSERT_TRUE(other);
  128. ASSERT_EQ(any.type(), entt::type_id<int>());
  129. ASSERT_EQ(other.type(), entt::type_id<int>());
  130. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  131. ASSERT_EQ(entt::any_cast<int>(other), 42);
  132. }
  133. TEST_F(Any, SBOCopyAssignment) {
  134. entt::any any{42};
  135. entt::any other{3};
  136. other = any;
  137. ASSERT_TRUE(any);
  138. ASSERT_TRUE(other);
  139. ASSERT_EQ(any.type(), entt::type_id<int>());
  140. ASSERT_EQ(other.type(), entt::type_id<int>());
  141. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  142. ASSERT_EQ(entt::any_cast<int>(other), 42);
  143. }
  144. TEST_F(Any, SBOMoveConstruction) {
  145. entt::any any{42};
  146. entt::any other{std::move(any)};
  147. ASSERT_TRUE(any);
  148. ASSERT_TRUE(other);
  149. ASSERT_NE(any.data(), nullptr);
  150. ASSERT_EQ(any.type(), entt::type_id<int>());
  151. ASSERT_EQ(other.type(), entt::type_id<int>());
  152. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  153. ASSERT_EQ(entt::any_cast<int>(other), 42);
  154. }
  155. TEST_F(Any, SBOMoveAssignment) {
  156. entt::any any{42};
  157. entt::any other{3};
  158. other = std::move(any);
  159. ASSERT_TRUE(any);
  160. ASSERT_TRUE(other);
  161. ASSERT_NE(any.data(), nullptr);
  162. ASSERT_EQ(any.type(), entt::type_id<int>());
  163. ASSERT_EQ(other.type(), entt::type_id<int>());
  164. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  165. ASSERT_EQ(entt::any_cast<int>(other), 42);
  166. }
  167. TEST_F(Any, SBODirectAssignment) {
  168. entt::any any{};
  169. any = 42;
  170. ASSERT_TRUE(any);
  171. ASSERT_EQ(any.type(), entt::type_id<int>());
  172. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  173. ASSERT_EQ(entt::any_cast<int>(any), 42);
  174. }
  175. TEST_F(Any, NoSBOInPlaceTypeConstruction) {
  176. fat instance{.1, .2, .3, .4};
  177. entt::any any{std::in_place_type<fat>, instance};
  178. ASSERT_TRUE(any);
  179. ASSERT_EQ(any.type(), entt::type_id<fat>());
  180. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  181. ASSERT_EQ(entt::any_cast<fat>(any), instance);
  182. auto other = any.as_ref();
  183. ASSERT_TRUE(other);
  184. ASSERT_EQ(other.type(), entt::type_id<fat>());
  185. ASSERT_EQ(entt::any_cast<fat>(other), (fat{.1, .2, .3, .4}));
  186. ASSERT_EQ(other.data(), any.data());
  187. }
  188. TEST_F(Any, NoSBOAsRefConstruction) {
  189. fat instance{.1, .2, .3, .4};
  190. entt::any any{std::ref(instance)};
  191. ASSERT_TRUE(any);
  192. ASSERT_EQ(any.type(), entt::type_id<fat>());
  193. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  194. ASSERT_EQ(entt::any_cast<const fat>(&any), &instance);
  195. ASSERT_EQ(entt::any_cast<fat>(&any), &instance);
  196. ASSERT_EQ(entt::any_cast<const fat>(&std::as_const(any)), &instance);
  197. ASSERT_EQ(entt::any_cast<fat>(&std::as_const(any)), &instance);
  198. ASSERT_EQ(entt::any_cast<const fat &>(any), instance);
  199. ASSERT_EQ(entt::any_cast<fat>(any), instance);
  200. ASSERT_EQ(any.data(), &instance);
  201. ASSERT_EQ(std::as_const(any).data(), &instance);
  202. any = std::ref(instance);
  203. ASSERT_TRUE(any);
  204. ASSERT_EQ(any.type(), entt::type_id<fat>());
  205. ASSERT_EQ(entt::any_cast<fat>(&any), &instance);
  206. auto other = any.as_ref();
  207. ASSERT_TRUE(other);
  208. ASSERT_EQ(other.type(), entt::type_id<fat>());
  209. ASSERT_EQ(entt::any_cast<fat>(other), (fat{.1, .2, .3, .4}));
  210. ASSERT_EQ(other.data(), any.data());
  211. }
  212. TEST_F(Any, NoSBOAsConstRefConstruction) {
  213. fat instance{.1, .2, .3, .4};
  214. entt::any any{std::cref(instance)};
  215. ASSERT_TRUE(any);
  216. ASSERT_EQ(any.type(), entt::type_id<fat>());
  217. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  218. ASSERT_EQ(entt::any_cast<const fat>(&any), &instance);
  219. ASSERT_EQ(entt::any_cast<fat>(&any), nullptr);
  220. ASSERT_EQ(entt::any_cast<const fat>(&std::as_const(any)), &instance);
  221. ASSERT_EQ(entt::any_cast<fat>(&std::as_const(any)), &instance);
  222. ASSERT_EQ(entt::any_cast<const fat &>(any), instance);
  223. ASSERT_EQ(entt::any_cast<fat>(any), instance);
  224. ASSERT_EQ(any.data(), nullptr);
  225. ASSERT_EQ(std::as_const(any).data(), &instance);
  226. any = std::cref(instance);
  227. ASSERT_TRUE(any);
  228. ASSERT_EQ(any.type(), entt::type_id<fat>());
  229. ASSERT_EQ(entt::any_cast<const fat>(&any), &instance);
  230. auto other = any.as_ref();
  231. ASSERT_TRUE(other);
  232. ASSERT_EQ(other.type(), entt::type_id<fat>());
  233. ASSERT_EQ(entt::any_cast<fat>(other), (fat{.1, .2, .3, .4}));
  234. ASSERT_EQ(other.data(), any.data());
  235. }
  236. TEST_F(Any, NoSBOCopyConstruction) {
  237. fat instance{.1, .2, .3, .4};
  238. entt::any any{instance};
  239. entt::any other{any};
  240. ASSERT_TRUE(any);
  241. ASSERT_TRUE(other);
  242. ASSERT_EQ(any.type(), entt::type_id<fat>());
  243. ASSERT_EQ(other.type(), entt::type_id<fat>());
  244. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  245. ASSERT_EQ(entt::any_cast<fat>(other), instance);
  246. }
  247. TEST_F(Any, NoSBOCopyAssignment) {
  248. fat instance{.1, .2, .3, .4};
  249. entt::any any{instance};
  250. entt::any other{3};
  251. other = any;
  252. ASSERT_TRUE(any);
  253. ASSERT_TRUE(other);
  254. ASSERT_EQ(any.type(), entt::type_id<fat>());
  255. ASSERT_EQ(other.type(), entt::type_id<fat>());
  256. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  257. ASSERT_EQ(entt::any_cast<fat>(other), instance);
  258. }
  259. TEST_F(Any, NoSBOMoveConstruction) {
  260. fat instance{.1, .2, .3, .4};
  261. entt::any any{instance};
  262. entt::any other{std::move(any)};
  263. ASSERT_FALSE(any);
  264. ASSERT_TRUE(other);
  265. ASSERT_EQ(any.data(), nullptr);
  266. ASSERT_EQ(any.type(), entt::type_id<fat>());
  267. ASSERT_EQ(other.type(), entt::type_id<fat>());
  268. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  269. ASSERT_EQ(entt::any_cast<fat>(other), instance);
  270. }
  271. TEST_F(Any, NoSBOMoveAssignment) {
  272. fat instance{.1, .2, .3, .4};
  273. entt::any any{instance};
  274. entt::any other{3};
  275. other = std::move(any);
  276. ASSERT_FALSE(any);
  277. ASSERT_TRUE(other);
  278. ASSERT_EQ(any.data(), nullptr);
  279. ASSERT_EQ(any.type(), entt::type_id<fat>());
  280. ASSERT_EQ(other.type(), entt::type_id<fat>());
  281. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  282. ASSERT_EQ(entt::any_cast<fat>(other), instance);
  283. }
  284. TEST_F(Any, NoSBODirectAssignment) {
  285. fat instance{.1, .2, .3, .4};
  286. entt::any any{};
  287. any = instance;
  288. ASSERT_TRUE(any);
  289. ASSERT_EQ(any.type(), entt::type_id<fat>());
  290. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  291. ASSERT_EQ(entt::any_cast<fat>(any), instance);
  292. }
  293. TEST_F(Any, VoidInPlaceTypeConstruction) {
  294. entt::any any{std::in_place_type<void>};
  295. ASSERT_FALSE(any);
  296. ASSERT_FALSE(any.type());
  297. ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
  298. }
  299. TEST_F(Any, VoidCopyConstruction) {
  300. entt::any any{std::in_place_type<void>};
  301. entt::any other{any};
  302. ASSERT_FALSE(any);
  303. ASSERT_FALSE(other);
  304. ASSERT_FALSE(any.type());
  305. ASSERT_FALSE(other.type());
  306. ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
  307. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  308. }
  309. TEST_F(Any, VoidCopyAssignment) {
  310. entt::any any{std::in_place_type<void>};
  311. entt::any other{42};
  312. other = any;
  313. ASSERT_FALSE(any);
  314. ASSERT_FALSE(other);
  315. ASSERT_FALSE(any.type());
  316. ASSERT_FALSE(other.type());
  317. ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
  318. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  319. }
  320. TEST_F(Any, VoidMoveConstruction) {
  321. entt::any any{std::in_place_type<void>};
  322. entt::any other{std::move(any)};
  323. ASSERT_FALSE(any);
  324. ASSERT_FALSE(other);
  325. ASSERT_FALSE(any.type());
  326. ASSERT_FALSE(other.type());
  327. ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
  328. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  329. }
  330. TEST_F(Any, VoidMoveAssignment) {
  331. entt::any any{std::in_place_type<void>};
  332. entt::any other{42};
  333. other = std::move(any);
  334. ASSERT_FALSE(any);
  335. ASSERT_FALSE(other);
  336. ASSERT_FALSE(any.type());
  337. ASSERT_FALSE(other.type());
  338. ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
  339. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  340. }
  341. TEST_F(Any, SBOMoveValidButUnspecifiedState) {
  342. entt::any any{42};
  343. entt::any other{std::move(any)};
  344. entt::any valid = std::move(other);
  345. ASSERT_TRUE(any);
  346. ASSERT_TRUE(other);
  347. ASSERT_TRUE(valid);
  348. }
  349. TEST_F(Any, NoSBOMoveValidButUnspecifiedState) {
  350. fat instance{.1, .2, .3, .4};
  351. entt::any any{instance};
  352. entt::any other{std::move(any)};
  353. entt::any valid = std::move(other);
  354. ASSERT_FALSE(any);
  355. ASSERT_FALSE(other);
  356. ASSERT_TRUE(valid);
  357. }
  358. TEST_F(Any, VoidMoveValidButUnspecifiedState) {
  359. entt::any any{std::in_place_type<void>};
  360. entt::any other{std::move(any)};
  361. entt::any valid = std::move(other);
  362. ASSERT_FALSE(any);
  363. ASSERT_FALSE(other);
  364. ASSERT_FALSE(valid);
  365. }
  366. TEST_F(Any, SBODestruction) {
  367. {
  368. entt::any any{std::in_place_type<empty>};
  369. any.emplace<empty>();
  370. any = empty{};
  371. entt::any other{std::move(any)};
  372. any = std::move(other);
  373. }
  374. ASSERT_EQ(empty::counter, 6);
  375. }
  376. TEST_F(Any, NoSBODestruction) {
  377. {
  378. entt::any any{std::in_place_type<fat>, 1., 2., 3., 4.};
  379. any.emplace<fat>(1., 2., 3., 4.);
  380. any = fat{1., 2., 3., 4.};
  381. entt::any other{std::move(any)};
  382. any = std::move(other);
  383. }
  384. ASSERT_EQ(fat::counter, 4);
  385. }
  386. TEST_F(Any, VoidDestruction) {
  387. // just let asan tell us if everything is ok here
  388. [[maybe_unused]] entt::any any{std::in_place_type<void>};
  389. }
  390. TEST_F(Any, Emplace) {
  391. entt::any any{};
  392. any.emplace<int>(42);
  393. ASSERT_TRUE(any);
  394. ASSERT_EQ(any.type(), entt::type_id<int>());
  395. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  396. ASSERT_EQ(entt::any_cast<int>(any), 42);
  397. }
  398. TEST_F(Any, EmplaceVoid) {
  399. entt::any any{};
  400. any.emplace<void>();
  401. ASSERT_FALSE(any);
  402. ASSERT_FALSE(any.type());
  403. }
  404. TEST_F(Any, Reset) {
  405. entt::any any{42};
  406. ASSERT_TRUE(any);
  407. ASSERT_EQ(any.type(), entt::type_id<int>());
  408. any.reset();
  409. ASSERT_FALSE(any);
  410. ASSERT_EQ(any.type(), entt::type_info{});
  411. }
  412. TEST_F(Any, SBOSwap) {
  413. entt::any lhs{'c'};
  414. entt::any rhs{42};
  415. std::swap(lhs, rhs);
  416. ASSERT_EQ(lhs.type(), entt::type_id<int>());
  417. ASSERT_EQ(rhs.type(), entt::type_id<char>());
  418. ASSERT_EQ(entt::any_cast<char>(&lhs), nullptr);
  419. ASSERT_EQ(entt::any_cast<int>(&rhs), nullptr);
  420. ASSERT_EQ(entt::any_cast<int>(lhs), 42);
  421. ASSERT_EQ(entt::any_cast<char>(rhs), 'c');
  422. }
  423. TEST_F(Any, NoSBOSwap) {
  424. entt::any lhs{fat{.1, .2, .3, .4}};
  425. entt::any rhs{fat{.4, .3, .2, .1}};
  426. std::swap(lhs, rhs);
  427. ASSERT_EQ(entt::any_cast<fat>(lhs), (fat{.4, .3, .2, .1}));
  428. ASSERT_EQ(entt::any_cast<fat>(rhs), (fat{.1, .2, .3, .4}));
  429. }
  430. TEST_F(Any, VoidSwap) {
  431. entt::any lhs{std::in_place_type<void>};
  432. entt::any rhs{std::in_place_type<void>};
  433. const auto *pre = lhs.data();
  434. std::swap(lhs, rhs);
  435. ASSERT_EQ(pre, lhs.data());
  436. }
  437. TEST_F(Any, SBOWithNoSBOSwap) {
  438. entt::any lhs{fat{.1, .2, .3, .4}};
  439. entt::any rhs{'c'};
  440. std::swap(lhs, rhs);
  441. ASSERT_EQ(lhs.type(), entt::type_id<char>());
  442. ASSERT_EQ(rhs.type(), entt::type_id<fat>());
  443. ASSERT_EQ(entt::any_cast<fat>(&lhs), nullptr);
  444. ASSERT_EQ(entt::any_cast<char>(&rhs), nullptr);
  445. ASSERT_EQ(entt::any_cast<char>(lhs), 'c');
  446. ASSERT_EQ(entt::any_cast<fat>(rhs), (fat{.1, .2, .3, .4}));
  447. }
  448. TEST_F(Any, SBOWithRefSwap) {
  449. int value = 3;
  450. entt::any lhs{std::ref(value)};
  451. entt::any rhs{'c'};
  452. std::swap(lhs, rhs);
  453. ASSERT_EQ(lhs.type(), entt::type_id<char>());
  454. ASSERT_EQ(rhs.type(), entt::type_id<int>());
  455. ASSERT_EQ(entt::any_cast<int>(&lhs), nullptr);
  456. ASSERT_EQ(entt::any_cast<char>(&rhs), nullptr);
  457. ASSERT_EQ(entt::any_cast<char>(lhs), 'c');
  458. ASSERT_EQ(entt::any_cast<int>(rhs), 3);
  459. ASSERT_EQ(rhs.data(), &value);
  460. }
  461. TEST_F(Any, SBOWithConstRefSwap) {
  462. int value = 3;
  463. entt::any lhs{std::cref(value)};
  464. entt::any rhs{'c'};
  465. std::swap(lhs, rhs);
  466. ASSERT_EQ(lhs.type(), entt::type_id<char>());
  467. ASSERT_EQ(rhs.type(), entt::type_id<int>());
  468. ASSERT_EQ(entt::any_cast<int>(&lhs), nullptr);
  469. ASSERT_EQ(entt::any_cast<char>(&rhs), nullptr);
  470. ASSERT_EQ(entt::any_cast<char>(lhs), 'c');
  471. ASSERT_EQ(entt::any_cast<int>(rhs), 3);
  472. ASSERT_EQ(rhs.data(), nullptr);
  473. ASSERT_EQ(std::as_const(rhs).data(), &value);
  474. }
  475. TEST_F(Any, SBOWithEmptySwap) {
  476. entt::any lhs{'c'};
  477. entt::any rhs{};
  478. std::swap(lhs, rhs);
  479. ASSERT_FALSE(lhs);
  480. ASSERT_EQ(rhs.type(), entt::type_id<char>());
  481. ASSERT_EQ(entt::any_cast<char>(&lhs), nullptr);
  482. ASSERT_EQ(entt::any_cast<double>(&rhs), nullptr);
  483. ASSERT_EQ(entt::any_cast<char>(rhs), 'c');
  484. std::swap(lhs, rhs);
  485. ASSERT_FALSE(rhs);
  486. ASSERT_EQ(lhs.type(), entt::type_id<char>());
  487. ASSERT_EQ(entt::any_cast<double>(&lhs), nullptr);
  488. ASSERT_EQ(entt::any_cast<char>(&rhs), nullptr);
  489. ASSERT_EQ(entt::any_cast<char>(lhs), 'c');
  490. }
  491. TEST_F(Any, SBOWithVoidSwap) {
  492. entt::any lhs{'c'};
  493. entt::any rhs{std::in_place_type<void>};
  494. std::swap(lhs, rhs);
  495. ASSERT_FALSE(lhs);
  496. ASSERT_EQ(rhs.type(), entt::type_id<char>());
  497. ASSERT_EQ(entt::any_cast<char>(&lhs), nullptr);
  498. ASSERT_EQ(entt::any_cast<double>(&rhs), nullptr);
  499. ASSERT_EQ(entt::any_cast<char>(rhs), 'c');
  500. std::swap(lhs, rhs);
  501. ASSERT_FALSE(rhs);
  502. ASSERT_EQ(lhs.type(), entt::type_id<char>());
  503. ASSERT_EQ(entt::any_cast<double>(&lhs), nullptr);
  504. ASSERT_EQ(entt::any_cast<char>(&rhs), nullptr);
  505. ASSERT_EQ(entt::any_cast<char>(lhs), 'c');
  506. }
  507. TEST_F(Any, NoSBOWithRefSwap) {
  508. int value = 3;
  509. entt::any lhs{std::ref(value)};
  510. entt::any rhs{fat{.1, .2, .3, .4}};
  511. std::swap(lhs, rhs);
  512. ASSERT_EQ(lhs.type(), entt::type_id<fat>());
  513. ASSERT_EQ(rhs.type(), entt::type_id<int>());
  514. ASSERT_EQ(entt::any_cast<int>(&lhs), nullptr);
  515. ASSERT_EQ(entt::any_cast<fat>(&rhs), nullptr);
  516. ASSERT_EQ(entt::any_cast<fat>(lhs), (fat{.1, .2, .3, .4}));
  517. ASSERT_EQ(entt::any_cast<int>(rhs), 3);
  518. ASSERT_EQ(rhs.data(), &value);
  519. }
  520. TEST_F(Any, NoSBOWithConstRefSwap) {
  521. int value = 3;
  522. entt::any lhs{std::cref(value)};
  523. entt::any rhs{fat{.1, .2, .3, .4}};
  524. std::swap(lhs, rhs);
  525. ASSERT_EQ(lhs.type(), entt::type_id<fat>());
  526. ASSERT_EQ(rhs.type(), entt::type_id<int>());
  527. ASSERT_EQ(entt::any_cast<int>(&lhs), nullptr);
  528. ASSERT_EQ(entt::any_cast<fat>(&rhs), nullptr);
  529. ASSERT_EQ(entt::any_cast<fat>(lhs), (fat{.1, .2, .3, .4}));
  530. ASSERT_EQ(entt::any_cast<int>(rhs), 3);
  531. ASSERT_EQ(rhs.data(), nullptr);
  532. ASSERT_EQ(std::as_const(rhs).data(), &value);
  533. }
  534. TEST_F(Any, NoSBOWithEmptySwap) {
  535. entt::any lhs{fat{.1, .2, .3, .4}};
  536. entt::any rhs{};
  537. std::swap(lhs, rhs);
  538. ASSERT_FALSE(lhs);
  539. ASSERT_EQ(rhs.type(), entt::type_id<fat>());
  540. ASSERT_EQ(entt::any_cast<fat>(&lhs), nullptr);
  541. ASSERT_EQ(entt::any_cast<double>(&rhs), nullptr);
  542. ASSERT_EQ(entt::any_cast<fat>(rhs), (fat{.1, .2, .3, .4}));
  543. std::swap(lhs, rhs);
  544. ASSERT_FALSE(rhs);
  545. ASSERT_EQ(lhs.type(), entt::type_id<fat>());
  546. ASSERT_EQ(entt::any_cast<double>(&lhs), nullptr);
  547. ASSERT_EQ(entt::any_cast<fat>(&rhs), nullptr);
  548. ASSERT_EQ(entt::any_cast<fat>(lhs), (fat{.1, .2, .3, .4}));
  549. }
  550. TEST_F(Any, NoSBOWithVoidSwap) {
  551. entt::any lhs{fat{.1, .2, .3, .4}};
  552. entt::any rhs{std::in_place_type<void>};
  553. std::swap(lhs, rhs);
  554. ASSERT_FALSE(lhs);
  555. ASSERT_EQ(rhs.type(), entt::type_id<fat>());
  556. ASSERT_EQ(entt::any_cast<fat>(&lhs), nullptr);
  557. ASSERT_EQ(entt::any_cast<double>(&rhs), nullptr);
  558. ASSERT_EQ(entt::any_cast<fat>(rhs), (fat{.1, .2, .3, .4}));
  559. std::swap(lhs, rhs);
  560. ASSERT_FALSE(rhs);
  561. ASSERT_EQ(lhs.type(), entt::type_id<fat>());
  562. ASSERT_EQ(entt::any_cast<double>(&lhs), nullptr);
  563. ASSERT_EQ(entt::any_cast<fat>(&rhs), nullptr);
  564. ASSERT_EQ(entt::any_cast<fat>(lhs), (fat{.1, .2, .3, .4}));
  565. }
  566. TEST_F(Any, AsRef) {
  567. entt::any any{42};
  568. auto ref = any.as_ref();
  569. auto cref = std::as_const(any).as_ref();
  570. ASSERT_EQ(entt::any_cast<int>(&any), any.data());
  571. ASSERT_EQ(entt::any_cast<int>(&ref), any.data());
  572. ASSERT_EQ(entt::any_cast<int>(&cref), nullptr);
  573. ASSERT_EQ(entt::any_cast<const int>(&any), any.data());
  574. ASSERT_EQ(entt::any_cast<const int>(&ref), any.data());
  575. ASSERT_EQ(entt::any_cast<const int>(&cref), any.data());
  576. ASSERT_EQ(entt::any_cast<int>(any), 42);
  577. ASSERT_EQ(entt::any_cast<int>(ref), 42);
  578. ASSERT_EQ(entt::any_cast<int>(cref), 42);
  579. ASSERT_EQ(entt::any_cast<const int>(any), 42);
  580. ASSERT_EQ(entt::any_cast<const int>(ref), 42);
  581. ASSERT_EQ(entt::any_cast<const int>(cref), 42);
  582. ASSERT_EQ(entt::any_cast<int &>(any), 42);
  583. ASSERT_EQ(entt::any_cast<const int &>(any), 42);
  584. ASSERT_EQ(entt::any_cast<int &>(ref), 42);
  585. ASSERT_EQ(entt::any_cast<const int &>(ref), 42);
  586. ASSERT_EQ(entt::any_cast<int>(&cref), nullptr);
  587. ASSERT_EQ(entt::any_cast<const int &>(cref), 42);
  588. entt::any_cast<int &>(any) = 3;
  589. ASSERT_EQ(entt::any_cast<int>(any), 3);
  590. ASSERT_EQ(entt::any_cast<int>(ref), 3);
  591. ASSERT_EQ(entt::any_cast<int>(cref), 3);
  592. std::swap(ref, cref);
  593. ASSERT_EQ(entt::any_cast<int>(&ref), nullptr);
  594. ASSERT_EQ(entt::any_cast<int>(&cref), any.data());
  595. ref = ref.as_ref();
  596. cref = std::as_const(cref).as_ref();
  597. ASSERT_EQ(entt::any_cast<int>(&ref), nullptr);
  598. ASSERT_EQ(entt::any_cast<int>(&cref), nullptr);
  599. ASSERT_EQ(entt::any_cast<const int>(&ref), any.data());
  600. ASSERT_EQ(entt::any_cast<const int>(&cref), any.data());
  601. ASSERT_EQ(entt::any_cast<int>(&ref), nullptr);
  602. ASSERT_EQ(entt::any_cast<int>(&cref), nullptr);
  603. ASSERT_EQ(entt::any_cast<const int &>(ref), 3);
  604. ASSERT_EQ(entt::any_cast<const int &>(cref), 3);
  605. ref = 42;
  606. cref = 42;
  607. ASSERT_NE(entt::any_cast<int>(&ref), nullptr);
  608. ASSERT_NE(entt::any_cast<int>(&cref), nullptr);
  609. ASSERT_EQ(entt::any_cast<int &>(ref), 42);
  610. ASSERT_EQ(entt::any_cast<int &>(cref), 42);
  611. ASSERT_EQ(entt::any_cast<const int &>(ref), 42);
  612. ASSERT_EQ(entt::any_cast<const int &>(cref), 42);
  613. ASSERT_NE(entt::any_cast<int>(&ref), any.data());
  614. ASSERT_NE(entt::any_cast<int>(&cref), any.data());
  615. }
  616. TEST_F(Any, Comparable) {
  617. auto test = [](entt::any any, entt::any other) {
  618. ASSERT_EQ(any, any);
  619. ASSERT_NE(other, any);
  620. ASSERT_NE(any, entt::any{});
  621. ASSERT_TRUE(any == any);
  622. ASSERT_FALSE(other == any);
  623. ASSERT_TRUE(any != other);
  624. ASSERT_TRUE(entt::any{} != any);
  625. };
  626. int value = 42;
  627. test('c', 'a');
  628. test(fat{.1, .2, .3, .4}, fat{.0, .1, .2, .3});
  629. test(std::ref(value), 3);
  630. test(3, std::cref(value));
  631. }
  632. TEST_F(Any, NotComparable) {
  633. auto test = [](const auto &instance) {
  634. entt::any any{std::cref(instance)};
  635. ASSERT_EQ(any, any);
  636. ASSERT_NE(any, entt::any{instance});
  637. ASSERT_NE(entt::any{}, any);
  638. ASSERT_TRUE(any == any);
  639. ASSERT_FALSE(any == entt::any{instance});
  640. ASSERT_TRUE(entt::any{} != any);
  641. };
  642. test(not_comparable{});
  643. test(std::unordered_map<int, not_comparable>{});
  644. test(std::vector<not_comparable>{});
  645. }
  646. TEST_F(Any, CompareVoid) {
  647. entt::any any{std::in_place_type<void>};
  648. ASSERT_EQ(any, any);
  649. ASSERT_EQ(any, entt::any{std::in_place_type<void>});
  650. ASSERT_NE(entt::any{'a'}, any);
  651. ASSERT_EQ(any, entt::any{});
  652. ASSERT_TRUE(any == any);
  653. ASSERT_TRUE(any == entt::any{std::in_place_type<void>});
  654. ASSERT_FALSE(entt::any{'a'} == any);
  655. ASSERT_TRUE(any != entt::any{'a'});
  656. ASSERT_FALSE(entt::any{} != any);
  657. }
  658. TEST_F(Any, AnyCast) {
  659. entt::any any{42};
  660. const auto &cany = any;
  661. ASSERT_EQ(entt::any_cast<char>(&any), nullptr);
  662. ASSERT_EQ(entt::any_cast<char>(&cany), nullptr);
  663. ASSERT_EQ(*entt::any_cast<int>(&any), 42);
  664. ASSERT_EQ(*entt::any_cast<int>(&cany), 42);
  665. ASSERT_EQ(entt::any_cast<int &>(any), 42);
  666. ASSERT_DEATH(entt::any_cast<double &>(any), "");
  667. ASSERT_EQ(entt::any_cast<const int &>(cany), 42);
  668. ASSERT_DEATH(entt::any_cast<const double &>(cany), "");
  669. ASSERT_EQ(entt::any_cast<int>(entt::any{42}), 42);
  670. ASSERT_DEATH(entt::any_cast<double>(entt::any{42}), "");
  671. }
  672. TEST_F(Any, NotCopyableType) {
  673. auto test = [](entt::any any) {
  674. entt::any copy{any};
  675. ASSERT_TRUE(any);
  676. ASSERT_FALSE(copy);
  677. copy = any;
  678. ASSERT_TRUE(any);
  679. ASSERT_FALSE(copy);
  680. };
  681. test(entt::any{std::in_place_type<not_copyable<1>>});
  682. test(entt::any{std::in_place_type<not_copyable<4>>});
  683. }
  684. TEST_F(Any, Array) {
  685. entt::any any{std::in_place_type<int[1]>};
  686. entt::any copy{any};
  687. ASSERT_TRUE(any);
  688. ASSERT_FALSE(copy);
  689. ASSERT_EQ(any.type(), entt::type_id<int[1]>());
  690. ASSERT_NE(entt::any_cast<int[1]>(&any), nullptr);
  691. ASSERT_EQ(entt::any_cast<int[2]>(&any), nullptr);
  692. ASSERT_EQ(entt::any_cast<int *>(&any), nullptr);
  693. entt::any_cast<int(&)[1]>(any)[0] = 42;
  694. ASSERT_EQ(entt::any_cast<const int(&)[1]>(std::as_const(any))[0], 42);
  695. }
  696. TEST_F(Any, CopyMoveReference) {
  697. int value{};
  698. auto test = [&](auto ref) {
  699. value = 3;
  700. entt::any any{ref};
  701. entt::any move = std::move(any);
  702. entt::any copy = move;
  703. ASSERT_TRUE(any);
  704. ASSERT_TRUE(move);
  705. ASSERT_TRUE(copy);
  706. ASSERT_EQ(move.type(), entt::type_id<int>());
  707. ASSERT_EQ(copy.type(), entt::type_id<int>());
  708. ASSERT_EQ(std::as_const(move).data(), &value);
  709. ASSERT_NE(std::as_const(copy).data(), &value);
  710. ASSERT_EQ(entt::any_cast<int>(move), 3);
  711. ASSERT_EQ(entt::any_cast<int>(copy), 3);
  712. value = 42;
  713. ASSERT_EQ(entt::any_cast<const int &>(move), 42);
  714. ASSERT_EQ(entt::any_cast<const int &>(copy), 3);
  715. };
  716. test(std::ref(value));
  717. test(std::cref(value));
  718. }
  719. TEST_F(Any, SBOVsZeroedSBOSize) {
  720. entt::any sbo{42};
  721. const auto *broken = sbo.data();
  722. entt::any other = std::move(sbo);
  723. ASSERT_NE(broken, other.data());
  724. entt::basic_any<0u> dyn{42};
  725. const auto *valid = dyn.data();
  726. entt::basic_any<0u> same = std::move(dyn);
  727. ASSERT_EQ(valid, same.data());
  728. }
  729. TEST_F(Any, Alignment) {
  730. static constexpr auto alignment = alignof(over_aligned);
  731. auto test = [](auto *target, auto cb) {
  732. const auto *data = target[0].data();
  733. ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(target[0u].data()) % alignment) == 0u);
  734. ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(target[1u].data()) % alignment) == 0u);
  735. std::swap(target[0], target[1]);
  736. ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(target[0u].data()) % alignment) == 0u);
  737. ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(target[1u].data()) % alignment) == 0u);
  738. cb(data, target[1].data());
  739. };
  740. entt::basic_any<alignment> nosbo[2] = { over_aligned{}, over_aligned{} };
  741. test(nosbo, [](auto *pre, auto *post) { ASSERT_EQ(pre, post); });
  742. entt::basic_any<alignment, alignment> sbo[2] = { over_aligned{}, over_aligned{} };
  743. test(sbo, [](auto *pre, auto *post) { ASSERT_NE(pre, post); });
  744. }
  745. TEST_F(Any, AggregatesMustWork) {
  746. struct aggregate_type { int value; };
  747. // the goal of this test is to enforce the requirements for aggregate types
  748. entt::any{std::in_place_type<aggregate_type>, 42}.emplace<aggregate_type>(42);
  749. }
  750. TEST_F(Any, DeducedArrayType) {
  751. entt::any any{"array of char"};
  752. ASSERT_TRUE(any);
  753. ASSERT_EQ(any.type(), entt::type_id<const char *>());
  754. ASSERT_EQ((std::strcmp("array of char", entt::any_cast<const char *>(any))), 0);
  755. any = "another array of char";
  756. ASSERT_TRUE(any);
  757. ASSERT_EQ(any.type(), entt::type_id<const char *>());
  758. ASSERT_EQ((std::strcmp("another array of char", entt::any_cast<const char *>(any))), 0);
  759. }