any.cpp 45 KB

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