variant_tests.cpp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  1. #include <array>
  2. #include <cstdint>
  3. #include <functional>
  4. #include <memory>
  5. #include <string>
  6. #include <type_traits>
  7. #include <gtest/gtest.h>
  8. #include <pdx/rpc/variant.h>
  9. using namespace android::pdx;
  10. using namespace android::pdx::rpc;
  11. namespace {
  12. struct BaseType {
  13. // NOLINTNEXTLINE(google-explicit-constructor)
  14. BaseType(int value) : value(value) {}
  15. int value;
  16. };
  17. struct DerivedType : BaseType {
  18. // NOLINTNEXTLINE(google-explicit-constructor)
  19. DerivedType(int value) : BaseType{value} {};
  20. };
  21. template <typename T>
  22. class TestType {
  23. public:
  24. // NOLINTNEXTLINE(google-explicit-constructor)
  25. TestType(const T& value) : value_(value) {}
  26. // NOLINTNEXTLINE(google-explicit-constructor)
  27. TestType(T&& value) : value_(std::move(value)) {}
  28. TestType(const TestType&) = default;
  29. TestType(TestType&&) = default;
  30. TestType& operator=(const TestType&) = default;
  31. TestType& operator=(TestType&&) = default;
  32. const T& get() const { return value_; }
  33. T&& take() { return std::move(value_); }
  34. private:
  35. T value_;
  36. };
  37. template <typename T>
  38. class InstrumentType {
  39. public:
  40. // NOLINTNEXTLINE(google-explicit-constructor)
  41. InstrumentType(const T& value) : value_(value) { constructor_count_++; }
  42. // NOLINTNEXTLINE(google-explicit-constructor)
  43. InstrumentType(T&& value) : value_(std::move(value)) { constructor_count_++; }
  44. InstrumentType(const InstrumentType& other) : value_(other.value_) {
  45. constructor_count_++;
  46. }
  47. InstrumentType(InstrumentType&& other) : value_(std::move(other.value_)) {
  48. constructor_count_++;
  49. }
  50. // NOLINTNEXTLINE(google-explicit-constructor)
  51. InstrumentType(const TestType<T>& other) : value_(other.get()) {
  52. constructor_count_++;
  53. }
  54. // NOLINTNEXTLINE(google-explicit-constructor)
  55. InstrumentType(TestType<T>&& other) : value_(other.take()) {
  56. constructor_count_++;
  57. }
  58. ~InstrumentType() { destructor_count_++; }
  59. InstrumentType& operator=(const InstrumentType& other) {
  60. copy_assignment_count_++;
  61. value_ = other.value_;
  62. return *this;
  63. }
  64. InstrumentType& operator=(InstrumentType&& other) {
  65. move_assignment_count_++;
  66. value_ = std::move(other.value_);
  67. return *this;
  68. }
  69. InstrumentType& operator=(const TestType<T>& other) {
  70. copy_assignment_count_++;
  71. value_ = other.get();
  72. return *this;
  73. }
  74. InstrumentType& operator=(TestType<T>&& other) {
  75. move_assignment_count_++;
  76. value_ = other.take();
  77. return *this;
  78. }
  79. static std::size_t constructor_count() { return constructor_count_; }
  80. static std::size_t destructor_count() { return destructor_count_; }
  81. static std::size_t move_assignment_count() { return move_assignment_count_; }
  82. static std::size_t copy_assignment_count() { return copy_assignment_count_; }
  83. const T& get() const { return value_; }
  84. T&& take() { return std::move(value_); }
  85. static void clear() {
  86. constructor_count_ = 0;
  87. destructor_count_ = 0;
  88. move_assignment_count_ = 0;
  89. copy_assignment_count_ = 0;
  90. }
  91. private:
  92. T value_;
  93. static std::size_t constructor_count_;
  94. static std::size_t destructor_count_;
  95. static std::size_t move_assignment_count_;
  96. static std::size_t copy_assignment_count_;
  97. };
  98. template <typename T>
  99. std::size_t InstrumentType<T>::constructor_count_ = 0;
  100. template <typename T>
  101. std::size_t InstrumentType<T>::destructor_count_ = 0;
  102. template <typename T>
  103. std::size_t InstrumentType<T>::move_assignment_count_ = 0;
  104. template <typename T>
  105. std::size_t InstrumentType<T>::copy_assignment_count_ = 0;
  106. } // anonymous namespace
  107. TEST(Variant, Assignment) {
  108. // Assert basic type properties.
  109. {
  110. Variant<int, bool, float> v;
  111. ASSERT_EQ(-1, v.index());
  112. ASSERT_FALSE(v.is<int>());
  113. ASSERT_FALSE(v.is<bool>());
  114. ASSERT_FALSE(v.is<float>());
  115. }
  116. {
  117. Variant<int, bool, float> v;
  118. v = 10;
  119. ASSERT_EQ(0, v.index());
  120. ASSERT_TRUE(v.is<int>());
  121. ASSERT_FALSE(v.is<bool>());
  122. ASSERT_FALSE(v.is<float>());
  123. EXPECT_EQ(10, std::get<int>(v));
  124. }
  125. {
  126. Variant<int, bool, float> v;
  127. v = false;
  128. ASSERT_EQ(1, v.index());
  129. ASSERT_FALSE(v.is<int>());
  130. ASSERT_TRUE(v.is<bool>());
  131. ASSERT_FALSE(v.is<float>());
  132. EXPECT_EQ(false, std::get<bool>(v));
  133. }
  134. {
  135. Variant<int, bool, float> v;
  136. v = 1.0f;
  137. ASSERT_EQ(2, v.index());
  138. ASSERT_FALSE(v.is<int>());
  139. ASSERT_FALSE(v.is<bool>());
  140. ASSERT_TRUE(v.is<float>());
  141. EXPECT_FLOAT_EQ(1.0f, std::get<float>(v));
  142. }
  143. {
  144. Variant<int, bool, float> v;
  145. // ERROR: More than one type is implicitly convertible from double.
  146. // v = 1.0;
  147. v = static_cast<float>(1.0);
  148. }
  149. {
  150. Variant<int, bool, float> v;
  151. double x = 1.1;
  152. v = static_cast<float>(x);
  153. ASSERT_EQ(2, v.index());
  154. ASSERT_FALSE(v.is<int>());
  155. ASSERT_FALSE(v.is<bool>());
  156. ASSERT_TRUE(v.is<float>());
  157. EXPECT_FLOAT_EQ(1.1, std::get<float>(v));
  158. }
  159. {
  160. Variant<int, std::string> v;
  161. ASSERT_EQ(-1, v.index());
  162. ASSERT_FALSE(v.is<int>());
  163. ASSERT_FALSE(v.is<std::string>());
  164. }
  165. {
  166. Variant<int, std::string> v;
  167. v = 20;
  168. ASSERT_EQ(0, v.index());
  169. ASSERT_TRUE(v.is<int>());
  170. ASSERT_FALSE(v.is<std::string>());
  171. EXPECT_EQ(20, std::get<int>(v));
  172. }
  173. {
  174. Variant<int, std::string> v;
  175. v = std::string("test");
  176. ASSERT_EQ(1, v.index());
  177. ASSERT_FALSE(v.is<int>());
  178. ASSERT_TRUE(v.is<std::string>());
  179. EXPECT_EQ("test", std::get<std::string>(v));
  180. }
  181. {
  182. Variant<int, std::string> v;
  183. v = "test";
  184. ASSERT_EQ(1, v.index());
  185. ASSERT_FALSE(v.is<int>());
  186. ASSERT_TRUE(v.is<std::string>());
  187. EXPECT_EQ("test", std::get<std::string>(v));
  188. }
  189. {
  190. Variant<const char*> v1;
  191. Variant<std::string> v2;
  192. v1 = "test";
  193. ASSERT_TRUE(v1.is<const char*>());
  194. v2 = v1;
  195. ASSERT_TRUE(v2.is<std::string>());
  196. EXPECT_EQ("test", std::get<std::string>(v2));
  197. }
  198. {
  199. Variant<int> a(1);
  200. Variant<int> b;
  201. ASSERT_TRUE(!a.empty());
  202. ASSERT_TRUE(b.empty());
  203. a = b;
  204. ASSERT_TRUE(a.empty());
  205. ASSERT_TRUE(b.empty());
  206. }
  207. {
  208. Variant<int*, char*> v;
  209. // ERROR: More than one type is implicitly convertible from nullptr.
  210. // v = nullptr;
  211. v = static_cast<int*>(nullptr);
  212. EXPECT_TRUE(v.is<int*>());
  213. v = static_cast<char*>(nullptr);
  214. EXPECT_TRUE(v.is<char*>());
  215. }
  216. {
  217. Variant<int*, char*> v;
  218. int a = 10;
  219. char b = 20;
  220. v = &b;
  221. ASSERT_TRUE(v.is<char*>());
  222. EXPECT_EQ(&b, std::get<char*>(v));
  223. EXPECT_EQ(b, *std::get<char*>(v));
  224. v = &a;
  225. ASSERT_TRUE(v.is<int*>());
  226. EXPECT_EQ(&a, std::get<int*>(v));
  227. EXPECT_EQ(a, *std::get<int*>(v));
  228. }
  229. {
  230. using IntRef = std::reference_wrapper<int>;
  231. Variant<IntRef> v;
  232. int a = 10;
  233. v = a;
  234. ASSERT_TRUE(v.is<IntRef>());
  235. EXPECT_EQ(a, std::get<IntRef>(v));
  236. a = 20;
  237. EXPECT_EQ(a, std::get<IntRef>(v));
  238. }
  239. }
  240. TEST(Variant, MoveAssignment) {
  241. {
  242. Variant<std::string> v;
  243. std::string s = "test";
  244. v = std::move(s);
  245. EXPECT_TRUE(s.empty());
  246. ASSERT_TRUE(v.is<std::string>());
  247. EXPECT_EQ("test", std::get<std::string>(v));
  248. }
  249. {
  250. Variant<std::string> v("test");
  251. std::string s = "fizz";
  252. s = std::move(std::get<std::string>(v));
  253. ASSERT_TRUE(v.is<std::string>());
  254. EXPECT_TRUE(std::get<std::string>(v).empty());
  255. EXPECT_EQ("test", s);
  256. }
  257. {
  258. Variant<std::string> a("test");
  259. Variant<std::string> b;
  260. b = std::move(a);
  261. ASSERT_TRUE(a.is<std::string>());
  262. ASSERT_TRUE(b.is<std::string>());
  263. EXPECT_TRUE(std::get<std::string>(a).empty());
  264. EXPECT_EQ("test", std::get<std::string>(b));
  265. }
  266. {
  267. Variant<std::string> a("test");
  268. Variant<std::string> b("fizz");
  269. b = std::move(a);
  270. ASSERT_TRUE(a.is<std::string>());
  271. ASSERT_TRUE(b.is<std::string>());
  272. EXPECT_TRUE(std::get<std::string>(a).empty());
  273. EXPECT_EQ("test", std::get<std::string>(b));
  274. }
  275. {
  276. Variant<int, std::string> a("test");
  277. Variant<int, std::string> b(10);
  278. b = std::move(a);
  279. ASSERT_TRUE(a.is<std::string>());
  280. ASSERT_TRUE(b.is<std::string>());
  281. EXPECT_TRUE(std::get<std::string>(a).empty());
  282. EXPECT_EQ("test", std::get<std::string>(b));
  283. }
  284. {
  285. Variant<int, std::string> a(10);
  286. Variant<int, std::string> b("test");
  287. b = std::move(a);
  288. ASSERT_TRUE(a.is<int>());
  289. ASSERT_TRUE(b.is<int>());
  290. EXPECT_EQ(10, std::get<int>(a));
  291. EXPECT_EQ(10, std::get<int>(b));
  292. }
  293. }
  294. TEST(Variant, Constructor) {
  295. {
  296. Variant<int, bool, float> v(true);
  297. EXPECT_TRUE(v.is<bool>());
  298. }
  299. {
  300. Variant<int, bool, float> v(10);
  301. EXPECT_TRUE(v.is<int>());
  302. }
  303. {
  304. Variant<int, bool, float> v(10.1f);
  305. EXPECT_TRUE(v.is<float>());
  306. }
  307. {
  308. Variant<float, std::string> v(10.);
  309. EXPECT_TRUE(v.is<float>());
  310. }
  311. {
  312. TestType<int> i(1);
  313. Variant<int, bool, float> v(i.take());
  314. ASSERT_TRUE(v.is<int>());
  315. EXPECT_EQ(1, std::get<int>(v));
  316. }
  317. {
  318. TestType<int> i(1);
  319. Variant<int, bool, float> v(i.get());
  320. ASSERT_TRUE(v.is<int>());
  321. EXPECT_EQ(1, std::get<int>(v));
  322. }
  323. {
  324. TestType<bool> b(true);
  325. Variant<int, bool, float> v(b.take());
  326. ASSERT_TRUE(v.is<bool>());
  327. EXPECT_EQ(true, std::get<bool>(v));
  328. }
  329. {
  330. TestType<bool> b(true);
  331. Variant<int, bool, float> v(b.get());
  332. ASSERT_TRUE(v.is<bool>());
  333. EXPECT_EQ(true, std::get<bool>(v));
  334. }
  335. {
  336. Variant<const char*> c("test");
  337. Variant<std::string> s(c);
  338. ASSERT_TRUE(s.is<std::string>());
  339. EXPECT_EQ("test", std::get<std::string>(s));
  340. }
  341. {
  342. Variant<int, bool, float> a(true);
  343. Variant<int, bool, float> b(a);
  344. ASSERT_TRUE(b.is<bool>());
  345. }
  346. {
  347. using IntRef = std::reference_wrapper<int>;
  348. int a = 10;
  349. Variant<IntRef> v(a);
  350. TestType<IntRef> t(a);
  351. ASSERT_TRUE(v.is<IntRef>());
  352. EXPECT_EQ(a, std::get<IntRef>(v));
  353. EXPECT_EQ(a, t.get());
  354. a = 20;
  355. EXPECT_EQ(a, std::get<IntRef>(v));
  356. EXPECT_EQ(a, t.get());
  357. }
  358. }
  359. // Verify correct ctor/dtor and assignment behavior used an instrumented type.
  360. TEST(Variant, CopyMoveConstructAssign) {
  361. {
  362. InstrumentType<int>::clear();
  363. // Default construct to empty, no InstrumentType activity.
  364. Variant<int, InstrumentType<int>> v;
  365. ASSERT_EQ(0u, InstrumentType<int>::constructor_count());
  366. ASSERT_EQ(0u, InstrumentType<int>::destructor_count());
  367. ASSERT_EQ(0u, InstrumentType<int>::move_assignment_count());
  368. ASSERT_EQ(0u, InstrumentType<int>::copy_assignment_count());
  369. }
  370. {
  371. InstrumentType<int>::clear();
  372. // Construct from int type, no InstrumentType activity.
  373. Variant<int, InstrumentType<int>> v;
  374. v = 10;
  375. EXPECT_EQ(0u, InstrumentType<int>::constructor_count());
  376. EXPECT_EQ(0u, InstrumentType<int>::destructor_count());
  377. EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
  378. EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
  379. }
  380. {
  381. InstrumentType<int>::clear();
  382. // Construct from int type, no InstrumentType activity.
  383. Variant<int, InstrumentType<int>> v(10);
  384. EXPECT_EQ(0u, InstrumentType<int>::constructor_count());
  385. EXPECT_EQ(0u, InstrumentType<int>::destructor_count());
  386. EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
  387. EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
  388. }
  389. {
  390. InstrumentType<int>::clear();
  391. // Construct from temporary, temporary ctor/dtor.
  392. Variant<int, InstrumentType<int>> v;
  393. v = InstrumentType<int>(25);
  394. EXPECT_EQ(2u, InstrumentType<int>::constructor_count());
  395. EXPECT_EQ(1u, InstrumentType<int>::destructor_count());
  396. EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
  397. EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
  398. }
  399. {
  400. InstrumentType<int>::clear();
  401. // Construct from temporary, temporary ctor/dtor.
  402. Variant<int, InstrumentType<int>> v(InstrumentType<int>(25));
  403. EXPECT_EQ(2u, InstrumentType<int>::constructor_count());
  404. EXPECT_EQ(1u, InstrumentType<int>::destructor_count());
  405. EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
  406. EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
  407. }
  408. {
  409. InstrumentType<int>::clear();
  410. // Construct from temporary, temporary ctor/dtor.
  411. Variant<int, InstrumentType<int>> v(InstrumentType<int>(25));
  412. // Assign from temporary, temporary ctor/dtor.
  413. v = InstrumentType<int>(35);
  414. EXPECT_EQ(3u, InstrumentType<int>::constructor_count());
  415. EXPECT_EQ(2u, InstrumentType<int>::destructor_count());
  416. EXPECT_EQ(1u, InstrumentType<int>::move_assignment_count());
  417. EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
  418. }
  419. {
  420. InstrumentType<int>::clear();
  421. // Construct from temporary, temporary ctor/dtor.
  422. Variant<int, InstrumentType<int>> v(InstrumentType<int>(25));
  423. // dtor.
  424. v = 10;
  425. EXPECT_EQ(2u, InstrumentType<int>::constructor_count());
  426. EXPECT_EQ(2u, InstrumentType<int>::destructor_count());
  427. EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
  428. EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
  429. }
  430. {
  431. InstrumentType<int>::clear();
  432. // Construct from temporary, temporary ctor/dtor.
  433. Variant<int, InstrumentType<int>> v(InstrumentType<int>(25));
  434. EXPECT_EQ(2u, InstrumentType<int>::constructor_count());
  435. EXPECT_EQ(1u, InstrumentType<int>::destructor_count());
  436. EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
  437. EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
  438. }
  439. EXPECT_EQ(2u, InstrumentType<int>::constructor_count());
  440. EXPECT_EQ(2u, InstrumentType<int>::destructor_count());
  441. EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
  442. EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
  443. {
  444. InstrumentType<int>::clear();
  445. // Construct from other temporary.
  446. Variant<int, InstrumentType<int>> v(TestType<int>(10));
  447. EXPECT_EQ(1u, InstrumentType<int>::constructor_count());
  448. EXPECT_EQ(0u, InstrumentType<int>::destructor_count());
  449. EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
  450. EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
  451. }
  452. {
  453. InstrumentType<int>::clear();
  454. // Construct from other temporary.
  455. Variant<int, InstrumentType<int>> v(TestType<int>(10));
  456. // Assign from other temporary.
  457. v = TestType<int>(11);
  458. EXPECT_EQ(1u, InstrumentType<int>::constructor_count());
  459. EXPECT_EQ(0u, InstrumentType<int>::destructor_count());
  460. EXPECT_EQ(1u, InstrumentType<int>::move_assignment_count());
  461. EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
  462. }
  463. {
  464. InstrumentType<int>::clear();
  465. // Construct from other temporary.
  466. Variant<int, InstrumentType<int>> v(TestType<int>(10));
  467. // Assign from empty Variant.
  468. v = Variant<int, InstrumentType<int>>();
  469. EXPECT_EQ(1u, InstrumentType<int>::constructor_count());
  470. EXPECT_EQ(1u, InstrumentType<int>::destructor_count());
  471. EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
  472. EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
  473. }
  474. {
  475. InstrumentType<int>::clear();
  476. TestType<int> other(10);
  477. // Construct from other.
  478. Variant<int, InstrumentType<int>> v(other);
  479. EXPECT_EQ(1u, InstrumentType<int>::constructor_count());
  480. EXPECT_EQ(0u, InstrumentType<int>::destructor_count());
  481. EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
  482. EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
  483. }
  484. {
  485. InstrumentType<int>::clear();
  486. // Construct from other temporary.
  487. Variant<int, InstrumentType<int>> v(TestType<int>(0));
  488. TestType<int> other(10);
  489. // Assign from other.
  490. v = other;
  491. EXPECT_EQ(1u, InstrumentType<int>::constructor_count());
  492. EXPECT_EQ(0u, InstrumentType<int>::destructor_count());
  493. EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
  494. EXPECT_EQ(1u, InstrumentType<int>::copy_assignment_count());
  495. }
  496. {
  497. InstrumentType<int>::clear();
  498. // Construct from temporary, temporary ctor/dtor.
  499. Variant<int, InstrumentType<int>> v(InstrumentType<int>(25));
  500. // Assign EmptyVariant.
  501. v = EmptyVariant{};
  502. EXPECT_EQ(2u, InstrumentType<int>::constructor_count());
  503. EXPECT_EQ(2u, InstrumentType<int>::destructor_count());
  504. EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
  505. EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
  506. }
  507. EXPECT_EQ(2u, InstrumentType<int>::constructor_count());
  508. EXPECT_EQ(2u, InstrumentType<int>::destructor_count());
  509. EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
  510. EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
  511. }
  512. TEST(Variant, MoveConstructor) {
  513. {
  514. std::unique_ptr<int> pointer = std::make_unique<int>(10);
  515. Variant<std::unique_ptr<int>> v(std::move(pointer));
  516. ASSERT_TRUE(v.is<std::unique_ptr<int>>());
  517. EXPECT_TRUE(std::get<std::unique_ptr<int>>(v) != nullptr);
  518. EXPECT_TRUE(pointer == nullptr);
  519. }
  520. {
  521. Variant<std::unique_ptr<int>> a(std::make_unique<int>(10));
  522. Variant<std::unique_ptr<int>> b(std::move(a));
  523. ASSERT_TRUE(a.is<std::unique_ptr<int>>());
  524. ASSERT_TRUE(b.is<std::unique_ptr<int>>());
  525. EXPECT_TRUE(std::get<std::unique_ptr<int>>(a) == nullptr);
  526. EXPECT_TRUE(std::get<std::unique_ptr<int>>(b) != nullptr);
  527. }
  528. }
  529. TEST(Variant, IndexOf) {
  530. Variant<int, bool, float> v1;
  531. EXPECT_EQ(0, v1.index_of<int>());
  532. EXPECT_EQ(1, v1.index_of<bool>());
  533. EXPECT_EQ(2, v1.index_of<float>());
  534. Variant<int, bool, float, int> v2;
  535. EXPECT_EQ(0, v2.index_of<int>());
  536. EXPECT_EQ(1, v2.index_of<bool>());
  537. EXPECT_EQ(2, v2.index_of<float>());
  538. }
  539. struct Visitor {
  540. int int_value = 0;
  541. bool bool_value = false;
  542. float float_value = 0.0;
  543. bool empty_value = false;
  544. void Visit(int value) { int_value = value; }
  545. void Visit(bool value) { bool_value = value; }
  546. void Visit(float value) { float_value = value; }
  547. void Visit(EmptyVariant) { empty_value = true; }
  548. };
  549. TEST(Variant, Visit) {
  550. {
  551. Variant<int, bool, float> v(10);
  552. EXPECT_TRUE(v.is<int>());
  553. Visitor visitor;
  554. v.Visit([&visitor](const auto& value) { visitor.Visit(value); });
  555. EXPECT_EQ(10, visitor.int_value);
  556. visitor = {};
  557. v = true;
  558. v.Visit([&visitor](const auto& value) { visitor.Visit(value); });
  559. EXPECT_EQ(true, visitor.bool_value);
  560. }
  561. {
  562. Variant<int, bool, float> v;
  563. EXPECT_EQ(-1, v.index());
  564. Visitor visitor;
  565. v.Visit([&visitor](const auto& value) { visitor.Visit(value); });
  566. EXPECT_TRUE(visitor.empty_value);
  567. }
  568. {
  569. Variant<std::string> v("test");
  570. ASSERT_TRUE(v.is<std::string>());
  571. EXPECT_FALSE(std::get<std::string>(v).empty());
  572. v.Visit([](auto&& value) {
  573. std::remove_reference_t<decltype(value)> empty;
  574. std::swap(empty, value);
  575. });
  576. ASSERT_TRUE(v.is<std::string>());
  577. EXPECT_TRUE(std::get<std::string>(v).empty());
  578. }
  579. }
  580. TEST(Variant, Become) {
  581. {
  582. Variant<int, bool, float> v;
  583. v.Become(0);
  584. EXPECT_TRUE(v.is<int>());
  585. v.Become(1);
  586. EXPECT_TRUE(v.is<bool>());
  587. v.Become(2);
  588. EXPECT_TRUE(v.is<float>());
  589. v.Become(3);
  590. EXPECT_TRUE(v.empty());
  591. v.Become(-1);
  592. EXPECT_TRUE(v.empty());
  593. v.Become(-2);
  594. EXPECT_TRUE(v.empty());
  595. }
  596. {
  597. Variant<int, bool, float> v;
  598. v.Become(0, 10);
  599. ASSERT_TRUE(v.is<int>());
  600. EXPECT_EQ(10, std::get<int>(v));
  601. v.Become(1, true);
  602. ASSERT_TRUE(v.is<bool>());
  603. EXPECT_EQ(true, std::get<bool>(v));
  604. v.Become(2, 2.0f);
  605. ASSERT_TRUE(v.is<float>());
  606. EXPECT_FLOAT_EQ(2.0f, std::get<float>(v));
  607. v.Become(3, 10);
  608. EXPECT_TRUE(v.empty());
  609. v.Become(-1, 10);
  610. EXPECT_TRUE(v.empty());
  611. v.Become(-2, 20);
  612. EXPECT_TRUE(v.empty());
  613. }
  614. {
  615. Variant<std::string> v;
  616. v.Become(0);
  617. ASSERT_TRUE(v.is<std::string>());
  618. EXPECT_TRUE(std::get<std::string>(v).empty());
  619. }
  620. {
  621. Variant<std::string> v;
  622. v.Become(0, "test");
  623. ASSERT_TRUE(v.is<std::string>());
  624. EXPECT_EQ("test", std::get<std::string>(v));
  625. }
  626. {
  627. Variant<std::string> v("foo");
  628. v.Become(0, "bar");
  629. ASSERT_TRUE(v.is<std::string>());
  630. EXPECT_EQ("foo", std::get<std::string>(v));
  631. }
  632. }
  633. TEST(Variant, Swap) {
  634. {
  635. Variant<std::string> a;
  636. Variant<std::string> b;
  637. std::swap(a, b);
  638. EXPECT_TRUE(a.empty());
  639. EXPECT_TRUE(b.empty());
  640. }
  641. {
  642. Variant<std::string> a("1");
  643. Variant<std::string> b;
  644. std::swap(a, b);
  645. EXPECT_TRUE(a.empty());
  646. EXPECT_TRUE(!b.empty());
  647. ASSERT_TRUE(b.is<std::string>());
  648. EXPECT_EQ("1", std::get<std::string>(b));
  649. }
  650. {
  651. Variant<std::string> a;
  652. Variant<std::string> b("1");
  653. std::swap(a, b);
  654. EXPECT_TRUE(!a.empty());
  655. EXPECT_TRUE(b.empty());
  656. ASSERT_TRUE(a.is<std::string>());
  657. EXPECT_EQ("1", std::get<std::string>(a));
  658. }
  659. {
  660. Variant<std::string> a("1");
  661. Variant<std::string> b("2");
  662. std::swap(a, b);
  663. ASSERT_TRUE(a.is<std::string>());
  664. ASSERT_TRUE(b.is<std::string>());
  665. EXPECT_EQ("2", std::get<std::string>(a));
  666. EXPECT_EQ("1", std::get<std::string>(b));
  667. }
  668. {
  669. Variant<int, std::string> a(10);
  670. Variant<int, std::string> b("1");
  671. std::swap(a, b);
  672. ASSERT_TRUE(a.is<std::string>());
  673. ASSERT_TRUE(b.is<int>());
  674. EXPECT_EQ("1", std::get<std::string>(a));
  675. EXPECT_EQ(10, std::get<int>(b));
  676. }
  677. {
  678. Variant<int, std::string> a("1");
  679. Variant<int, std::string> b(10);
  680. std::swap(a, b);
  681. ASSERT_TRUE(a.is<int>());
  682. ASSERT_TRUE(b.is<std::string>());
  683. EXPECT_EQ(10, std::get<int>(a));
  684. EXPECT_EQ("1", std::get<std::string>(b));
  685. }
  686. }
  687. TEST(Variant, Get) {
  688. {
  689. Variant<int, bool, float, int> v;
  690. EXPECT_EQ(nullptr, &std::get<int>(v));
  691. EXPECT_EQ(nullptr, &std::get<bool>(v));
  692. EXPECT_EQ(nullptr, &std::get<float>(v));
  693. EXPECT_EQ(nullptr, &std::get<0>(v));
  694. EXPECT_EQ(nullptr, &std::get<1>(v));
  695. EXPECT_EQ(nullptr, &std::get<2>(v));
  696. EXPECT_EQ(nullptr, &std::get<3>(v));
  697. }
  698. {
  699. Variant<int, bool, float, int> v;
  700. v = 9;
  701. ASSERT_TRUE(v.is<int>())
  702. << "Expected type " << v.index_of<int>() << " got type " << v.index();
  703. EXPECT_EQ(9, std::get<int>(v));
  704. EXPECT_EQ(9, std::get<0>(v));
  705. std::get<int>(v) = 10;
  706. EXPECT_EQ(10, std::get<int>(v));
  707. EXPECT_EQ(10, std::get<0>(v));
  708. std::get<0>(v) = 11;
  709. EXPECT_EQ(11, std::get<int>(v));
  710. EXPECT_EQ(11, std::get<0>(v));
  711. std::get<3>(v) = 12;
  712. EXPECT_EQ(12, std::get<int>(v));
  713. EXPECT_EQ(12, std::get<3>(v));
  714. }
  715. {
  716. Variant<int, bool, float, int> v;
  717. v = false;
  718. ASSERT_TRUE(v.is<bool>())
  719. << "Expected type " << v.index_of<bool>() << " got type " << v.index();
  720. EXPECT_EQ(false, std::get<bool>(v));
  721. EXPECT_EQ(false, std::get<1>(v));
  722. std::get<bool>(v) = true;
  723. EXPECT_EQ(true, std::get<bool>(v));
  724. EXPECT_EQ(true, std::get<1>(v));
  725. std::get<bool>(v) = false;
  726. EXPECT_EQ(false, std::get<bool>(v));
  727. EXPECT_EQ(false, std::get<1>(v));
  728. std::get<1>(v) = true;
  729. EXPECT_EQ(true, std::get<bool>(v));
  730. EXPECT_EQ(true, std::get<1>(v));
  731. std::get<1>(v) = false;
  732. EXPECT_EQ(false, std::get<bool>(v));
  733. EXPECT_EQ(false, std::get<1>(v));
  734. }
  735. {
  736. Variant<int, bool, float, int> v;
  737. v = 1.0f;
  738. ASSERT_TRUE(v.is<float>())
  739. << "Expected type " << v.index_of<float>() << " got type " << v.index();
  740. EXPECT_EQ(2, v.index());
  741. EXPECT_FLOAT_EQ(1.0, std::get<float>(v));
  742. EXPECT_FLOAT_EQ(1.0, std::get<2>(v));
  743. std::get<float>(v) = 1.1;
  744. EXPECT_FLOAT_EQ(1.1, std::get<float>(v));
  745. EXPECT_FLOAT_EQ(1.1, std::get<2>(v));
  746. std::get<float>(v) = -3.0;
  747. EXPECT_FLOAT_EQ(-3.0, std::get<float>(v));
  748. EXPECT_FLOAT_EQ(-3.0, std::get<2>(v));
  749. std::get<2>(v) = 1.1;
  750. EXPECT_FLOAT_EQ(1.1, std::get<float>(v));
  751. EXPECT_FLOAT_EQ(1.1, std::get<2>(v));
  752. std::get<2>(v) = -3.0;
  753. EXPECT_FLOAT_EQ(-3.0, std::get<float>(v));
  754. EXPECT_FLOAT_EQ(-3.0, std::get<2>(v));
  755. }
  756. {
  757. Variant<std::unique_ptr<int>> v(std::make_unique<int>(10));
  758. std::unique_ptr<int> pointer = std::move(std::get<std::unique_ptr<int>>(v));
  759. ASSERT_FALSE(v.empty());
  760. EXPECT_TRUE(pointer != nullptr);
  761. EXPECT_TRUE(std::get<std::unique_ptr<int>>(v) == nullptr);
  762. }
  763. {
  764. Variant<std::string> v("test");
  765. std::string s = std::get<std::string>(std::move(v));
  766. EXPECT_EQ("test", s);
  767. }
  768. }
  769. TEST(Variant, IfAnyOf) {
  770. {
  771. Variant<int, float> v(10);
  772. ASSERT_TRUE(v.is<int>());
  773. bool b = false;
  774. EXPECT_TRUE(IfAnyOf<int>::Get(&v, &b));
  775. EXPECT_TRUE(b);
  776. float f = 0.0f;
  777. EXPECT_TRUE((IfAnyOf<int, float>::Get(&v, &f)));
  778. EXPECT_FLOAT_EQ(10.f, f);
  779. }
  780. {
  781. const Variant<int, float> v(10);
  782. ASSERT_TRUE(v.is<int>());
  783. bool b = false;
  784. EXPECT_TRUE(IfAnyOf<int>::Get(&v, &b));
  785. EXPECT_TRUE(b);
  786. float f = 0.0f;
  787. EXPECT_TRUE((IfAnyOf<int, float>::Get(&v, &f)));
  788. EXPECT_FLOAT_EQ(10.f, f);
  789. }
  790. {
  791. Variant<int, float> v(10);
  792. ASSERT_TRUE(v.is<int>());
  793. bool b = false;
  794. EXPECT_TRUE(IfAnyOf<int>::Call(&v, [&b](const auto& value) { b = value; }));
  795. EXPECT_TRUE(b);
  796. float f = 0.0f;
  797. EXPECT_TRUE((
  798. IfAnyOf<int, float>::Call(&v, [&f](const auto& value) { f = value; })));
  799. EXPECT_FLOAT_EQ(10.f, f);
  800. }
  801. {
  802. Variant<std::unique_ptr<int>, int> v(std::make_unique<int>(10));
  803. ASSERT_TRUE(v.is<std::unique_ptr<int>>());
  804. const int* original_v = std::get<std::unique_ptr<int>>(v).get();
  805. std::unique_ptr<int> u(std::make_unique<int>(20));
  806. EXPECT_TRUE(IfAnyOf<std::unique_ptr<int>>::Take(&v, &u));
  807. ASSERT_TRUE(v.is<std::unique_ptr<int>>());
  808. EXPECT_TRUE(std::get<std::unique_ptr<int>>(v) == nullptr);
  809. EXPECT_EQ(u.get(), original_v);
  810. }
  811. {
  812. Variant<std::unique_ptr<DerivedType>, int> v(
  813. std::make_unique<DerivedType>(10));
  814. ASSERT_TRUE(v.is<std::unique_ptr<DerivedType>>());
  815. const DerivedType* original_v =
  816. std::get<std::unique_ptr<DerivedType>>(v).get();
  817. std::unique_ptr<BaseType> u(std::make_unique<BaseType>(20));
  818. EXPECT_TRUE(IfAnyOf<std::unique_ptr<DerivedType>>::Take(&v, &u));
  819. ASSERT_TRUE(v.is<std::unique_ptr<DerivedType>>());
  820. EXPECT_TRUE(std::get<std::unique_ptr<DerivedType>>(v) == nullptr);
  821. EXPECT_EQ(u.get(), original_v);
  822. }
  823. {
  824. Variant<std::unique_ptr<int>, int> v(std::make_unique<int>(10));
  825. ASSERT_TRUE(v.is<std::unique_ptr<int>>());
  826. const int* original_v = std::get<std::unique_ptr<int>>(v).get();
  827. std::unique_ptr<int> u(std::make_unique<int>(20));
  828. EXPECT_TRUE(IfAnyOf<std::unique_ptr<int>>::Call(
  829. &v, [&u](auto&& value) { u = std::move(value); }));
  830. ASSERT_TRUE(v.is<std::unique_ptr<int>>());
  831. EXPECT_TRUE(std::get<std::unique_ptr<int>>(v) == nullptr);
  832. EXPECT_EQ(u.get(), original_v);
  833. }
  834. {
  835. Variant<int, bool, float> v(true);
  836. ASSERT_TRUE(v.is<bool>());
  837. float f = 0.f;
  838. EXPECT_FALSE((IfAnyOf<int, float>::Get(&v, &f)));
  839. EXPECT_FLOAT_EQ(0.f, f);
  840. }
  841. {
  842. Variant<std::string, int> v("foo");
  843. ASSERT_TRUE(v.is<std::string>());
  844. std::string s = "bar";
  845. EXPECT_TRUE(IfAnyOf<std::string>::Swap(&v, &s));
  846. ASSERT_TRUE(v.is<std::string>());
  847. EXPECT_EQ("bar", std::get<std::string>(v));
  848. EXPECT_EQ("foo", s);
  849. }
  850. {
  851. Variant<std::string, const char*> v(static_cast<const char*>("foo"));
  852. ASSERT_TRUE(v.is<const char*>());
  853. std::string s = "bar";
  854. EXPECT_TRUE((IfAnyOf<std::string, const char*>::Take(&v, &s)));
  855. ASSERT_TRUE(v.is<const char*>());
  856. EXPECT_EQ("foo", std::get<const char*>(v));
  857. EXPECT_EQ("foo", s);
  858. v = std::string("bar");
  859. ASSERT_TRUE(v.is<std::string>());
  860. EXPECT_TRUE((IfAnyOf<std::string, const char*>::Take(&v, &s)));
  861. ASSERT_TRUE(v.is<std::string>());
  862. EXPECT_EQ("bar", s);
  863. }
  864. {
  865. Variant<std::string, const char*> v;
  866. ASSERT_TRUE(v.empty());
  867. std::string s = "bar";
  868. EXPECT_FALSE((IfAnyOf<std::string, const char*>::Take(&v, &s)));
  869. EXPECT_EQ("bar", s);
  870. }
  871. {
  872. Variant<std::string, const char*> v(static_cast<const char*>("test"));
  873. ASSERT_TRUE(v.is<const char*>());
  874. std::string s;
  875. EXPECT_FALSE(IfAnyOf<>::Take(&v, &s));
  876. EXPECT_TRUE(s.empty());
  877. }
  878. }
  879. TEST(Variant, ConstVolatile) {
  880. {
  881. Variant<const int> v(10);
  882. ASSERT_TRUE(v.is<const int>());
  883. EXPECT_EQ(10, std::get<const int>(v));
  884. }
  885. {
  886. Variant<const std::string> v("test");
  887. ASSERT_TRUE(v.is<const std::string>());
  888. EXPECT_EQ("test", std::get<const std::string>(v));
  889. }
  890. {
  891. Variant<volatile int, std::string> v(10);
  892. ASSERT_TRUE(v.is<volatile int>());
  893. EXPECT_EQ(10, std::get<volatile int>(v));
  894. }
  895. }
  896. TEST(Variant, HasType) {
  897. EXPECT_TRUE((detail::HasType<int, int, float, bool>::value));
  898. EXPECT_FALSE((detail::HasType<char, int, float, bool>::value));
  899. EXPECT_FALSE(detail::HasType<>::value);
  900. EXPECT_TRUE((detail::HasType<int&, int, float, bool>::value));
  901. EXPECT_FALSE((detail::HasType<char&, int, float, bool>::value));
  902. }
  903. TEST(Variant, IsConstructible) {
  904. using ArrayType = const float[3];
  905. struct ImplicitBool {
  906. // NOLINTNEXTLINE(google-explicit-constructor)
  907. operator bool() const { return true; }
  908. };
  909. struct ExplicitBool {
  910. explicit operator bool() const { return true; }
  911. };
  912. struct NonBool {};
  913. struct TwoArgs {
  914. TwoArgs(int, bool) {}
  915. };
  916. EXPECT_FALSE((detail::IsConstructible<bool, ArrayType>::value));
  917. EXPECT_TRUE((detail::IsConstructible<bool, int>::value));
  918. EXPECT_TRUE((detail::IsConstructible<bool, ImplicitBool>::value));
  919. EXPECT_TRUE((detail::IsConstructible<bool, ExplicitBool>::value));
  920. EXPECT_FALSE((detail::IsConstructible<bool, NonBool>::value));
  921. EXPECT_TRUE((detail::IsConstructible<TwoArgs, int, bool>::value));
  922. EXPECT_FALSE((detail::IsConstructible<TwoArgs, int, std::string>::value));
  923. EXPECT_FALSE((detail::IsConstructible<TwoArgs, int>::value));
  924. }
  925. TEST(Variant, Set) {
  926. EXPECT_TRUE((detail::Set<int, bool, float>::template IsSubset<int, bool,
  927. float>::value));
  928. EXPECT_TRUE(
  929. (detail::Set<int, bool, float>::template IsSubset<bool, float>::value));
  930. EXPECT_TRUE((detail::Set<int, bool, float>::template IsSubset<float>::value));
  931. EXPECT_TRUE((detail::Set<int, bool, float>::template IsSubset<>::value));
  932. EXPECT_FALSE(
  933. (detail::Set<int, bool, float>::template IsSubset<int, bool, float,
  934. char>::value));
  935. EXPECT_FALSE((detail::Set<int, bool, float>::template IsSubset<bool, float,
  936. char>::value));
  937. EXPECT_FALSE(
  938. (detail::Set<int, bool, float>::template IsSubset<float, char>::value));
  939. EXPECT_FALSE((detail::Set<int, bool, float>::template IsSubset<char>::value));
  940. EXPECT_TRUE(detail::Set<>::template IsSubset<>::value);
  941. EXPECT_FALSE(detail::Set<>::template IsSubset<int>::value);
  942. EXPECT_FALSE((detail::Set<>::template IsSubset<int, float>::value));
  943. }