variant_nonempty_check.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. //-----------------------------------------------------------------------------
  2. // boost-libs variant/test/variant_nonempty_check.cpp source file
  3. // See http://www.boost.org for updates, documentation, and revision history.
  4. //-----------------------------------------------------------------------------
  5. //
  6. // Copyright (c) 2014-2019 Antony Polukhin
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See
  9. // accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. // In this file we are making tests to ensure that variant guarantees nonemptiness.
  12. //
  13. // For that purpose we create a `throwing_class`, that throws exception at a specified
  14. // assignment attempt. If exception was thrown during move/assignemnt operation we make sure
  15. // that data in variant is same as before move/assignemnt operation or that a fallback type is
  16. // stored in variant.
  17. //
  18. // Different nonthrowing_class'es are used to tests different variant internal policies:
  19. // with/without fallback type + throw/nothrow copyable + throw/nothrow movable
  20. #include "boost/variant/variant.hpp"
  21. #include "boost/variant/get.hpp"
  22. #include "boost/core/lightweight_test.hpp"
  23. #include <stdexcept>
  24. struct exception_on_assignment : std::exception {};
  25. struct exception_on_move_assignment : exception_on_assignment {};
  26. void prevent_compiler_noexcept_detection() {
  27. char* p = new char;
  28. *p = '\0';
  29. delete p;
  30. }
  31. struct throwing_class {
  32. int trash;
  33. enum helper_enum {
  34. do_not_throw = 780,
  35. throw_after_5,
  36. throw_after_4,
  37. throw_after_3,
  38. throw_after_2,
  39. throw_after_1
  40. };
  41. bool is_throw() {
  42. if (trash < do_not_throw) {
  43. return true;
  44. }
  45. if (trash > do_not_throw && trash <= throw_after_1) {
  46. ++ trash;
  47. return false;
  48. }
  49. return trash != do_not_throw;
  50. }
  51. throwing_class(int value = 123) BOOST_NOEXCEPT_IF(false) : trash(value) {
  52. prevent_compiler_noexcept_detection();
  53. }
  54. throwing_class(const throwing_class& b) BOOST_NOEXCEPT_IF(false) : trash(b.trash) {
  55. if (is_throw()) {
  56. throw exception_on_assignment();
  57. }
  58. }
  59. const throwing_class& operator=(const throwing_class& b) BOOST_NOEXCEPT_IF(false) {
  60. trash = b.trash;
  61. if (is_throw()) {
  62. throw exception_on_assignment();
  63. }
  64. return *this;
  65. }
  66. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  67. throwing_class(throwing_class&& b) BOOST_NOEXCEPT_IF(false) : trash(b.trash) {
  68. if (is_throw()) {
  69. throw exception_on_move_assignment();
  70. }
  71. }
  72. const throwing_class& operator=(throwing_class&& b) BOOST_NOEXCEPT_IF(false) {
  73. trash = b.trash;
  74. if (is_throw()) {
  75. throw exception_on_move_assignment();
  76. }
  77. return *this;
  78. }
  79. #endif
  80. virtual ~throwing_class() {}
  81. };
  82. struct nonthrowing_class {
  83. int trash;
  84. nonthrowing_class() BOOST_NOEXCEPT_IF(false) : trash(123) {
  85. prevent_compiler_noexcept_detection();
  86. }
  87. nonthrowing_class(const nonthrowing_class&) BOOST_NOEXCEPT_IF(false) {
  88. prevent_compiler_noexcept_detection();
  89. }
  90. const nonthrowing_class& operator=(const nonthrowing_class&) BOOST_NOEXCEPT_IF(false) {
  91. prevent_compiler_noexcept_detection();
  92. return *this;
  93. }
  94. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  95. nonthrowing_class(nonthrowing_class&&) BOOST_NOEXCEPT_IF(false) {
  96. prevent_compiler_noexcept_detection();
  97. }
  98. const nonthrowing_class& operator=(nonthrowing_class&&) BOOST_NOEXCEPT_IF(false) {
  99. prevent_compiler_noexcept_detection();
  100. return *this;
  101. }
  102. #endif
  103. };
  104. struct nonthrowing_class2 {
  105. int trash;
  106. nonthrowing_class2() BOOST_NOEXCEPT_IF(false) : trash(123) {
  107. prevent_compiler_noexcept_detection();
  108. }
  109. };
  110. struct nonthrowing_class3 {
  111. int trash;
  112. nonthrowing_class3() BOOST_NOEXCEPT_IF(true) : trash(123) {}
  113. nonthrowing_class3(const nonthrowing_class3&) BOOST_NOEXCEPT_IF(false) {
  114. prevent_compiler_noexcept_detection();
  115. }
  116. const nonthrowing_class3& operator=(const nonthrowing_class3&) BOOST_NOEXCEPT_IF(false) {
  117. prevent_compiler_noexcept_detection();
  118. return *this;
  119. }
  120. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  121. nonthrowing_class3(nonthrowing_class3&&) BOOST_NOEXCEPT_IF(false) {
  122. prevent_compiler_noexcept_detection();
  123. }
  124. const nonthrowing_class3& operator=(nonthrowing_class3&&) BOOST_NOEXCEPT_IF(false) {
  125. prevent_compiler_noexcept_detection();
  126. return *this;
  127. }
  128. #endif
  129. };
  130. struct nonthrowing_class4 {
  131. int trash;
  132. nonthrowing_class4() BOOST_NOEXCEPT_IF(false) : trash(123) {
  133. prevent_compiler_noexcept_detection();
  134. }
  135. nonthrowing_class4(const nonthrowing_class4&) BOOST_NOEXCEPT_IF(false) {
  136. prevent_compiler_noexcept_detection();
  137. }
  138. const nonthrowing_class4& operator=(const nonthrowing_class4&) BOOST_NOEXCEPT_IF(false) {
  139. prevent_compiler_noexcept_detection();
  140. return *this;
  141. }
  142. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  143. nonthrowing_class4(nonthrowing_class4&&) BOOST_NOEXCEPT_IF(true) {
  144. }
  145. const nonthrowing_class4& operator=(nonthrowing_class4&&) BOOST_NOEXCEPT_IF(true) {
  146. return *this;
  147. }
  148. #endif
  149. };
  150. // Tests /////////////////////////////////////////////////////////////////////////////////////
  151. template <class Nonthrowing>
  152. inline void check_1_impl(int helper)
  153. {
  154. boost::variant<throwing_class, Nonthrowing> v;
  155. try {
  156. v = throwing_class(helper);
  157. BOOST_TEST(!v.which());
  158. BOOST_TEST(boost::get<throwing_class>(&v));
  159. } catch (const exception_on_assignment& /*e*/) {
  160. BOOST_TEST(!v.which());
  161. BOOST_TEST(boost::get<throwing_class>(&v));
  162. }
  163. try {
  164. throwing_class tc(helper);
  165. v = tc;
  166. BOOST_TEST(!v.which());
  167. BOOST_TEST(boost::get<throwing_class>(&v));
  168. } catch (const exception_on_assignment& /*e*/) {
  169. BOOST_TEST(!v.which());
  170. BOOST_TEST(boost::get<throwing_class>(&v));
  171. }
  172. }
  173. inline void check_1(int helper = 1)
  174. {
  175. check_1_impl<nonthrowing_class>(helper);
  176. check_1_impl<nonthrowing_class2>(helper);
  177. check_1_impl<nonthrowing_class3>(helper);
  178. check_1_impl<nonthrowing_class4>(helper);
  179. check_1_impl<boost::blank>(helper);
  180. }
  181. template <class Nonthrowing>
  182. inline void check_2_impl(int helper)
  183. {
  184. boost::variant<Nonthrowing, throwing_class> v;
  185. try {
  186. v = throwing_class(helper);
  187. BOOST_TEST(v.which() == 1);
  188. BOOST_TEST(boost::get<throwing_class>(&v));
  189. } catch (const exception_on_assignment& /*e*/) {
  190. BOOST_TEST(!v.which());
  191. BOOST_TEST(boost::get<Nonthrowing>(&v));
  192. }
  193. try {
  194. throwing_class cl(helper);
  195. v = cl;
  196. BOOST_TEST(v.which() == 1);
  197. BOOST_TEST(boost::get<throwing_class>(&v));
  198. } catch (const exception_on_assignment& /*e*/) {
  199. BOOST_TEST(!v.which());
  200. BOOST_TEST(boost::get<Nonthrowing>(&v));
  201. }
  202. }
  203. inline void check_2(int helper = 1)
  204. {
  205. check_2_impl<nonthrowing_class>(helper);
  206. check_2_impl<nonthrowing_class2>(helper);
  207. check_2_impl<nonthrowing_class3>(helper);
  208. check_2_impl<nonthrowing_class4>(helper);
  209. check_2_impl<boost::blank>(helper);
  210. }
  211. template <class Nonthrowing>
  212. inline void check_3_impl(int helper)
  213. {
  214. boost::variant<Nonthrowing, throwing_class> v1, v2;
  215. swap(v1, v2);
  216. try {
  217. v1 = throwing_class(helper);
  218. BOOST_TEST(v1.which() == 1);
  219. BOOST_TEST(boost::get<throwing_class>(&v1));
  220. } catch (const exception_on_assignment& /*e*/) {
  221. BOOST_TEST(!v1.which());
  222. BOOST_TEST(boost::get<Nonthrowing>(&v1));
  223. }
  224. try {
  225. v2 = throwing_class(helper);
  226. BOOST_TEST(v2.which() == 1);
  227. BOOST_TEST(boost::get<throwing_class>(&v2));
  228. } catch (const exception_on_assignment& /*e*/) {
  229. BOOST_TEST(!v2.which());
  230. BOOST_TEST(boost::get<Nonthrowing>(&v2));
  231. }
  232. if (!v1.which() && !v2.which()) {
  233. swap(v1, v2); // Make sure that two backup holders swap well
  234. BOOST_TEST(!v1.which());
  235. BOOST_TEST(boost::get<Nonthrowing>(&v1));
  236. BOOST_TEST(!v2.which());
  237. BOOST_TEST(boost::get<Nonthrowing>(&v2));
  238. v1 = v2;
  239. }
  240. }
  241. inline void check_3(int helper = 1)
  242. {
  243. check_3_impl<nonthrowing_class>(helper);
  244. check_3_impl<nonthrowing_class2>(helper);
  245. check_3_impl<nonthrowing_class3>(helper);
  246. check_3_impl<nonthrowing_class4>(helper);
  247. check_3_impl<boost::blank>(helper);
  248. }
  249. inline void check_4(int helper = 1)
  250. {
  251. // This one has a fallback
  252. boost::variant<int, throwing_class> v1, v2;
  253. swap(v1, v2);
  254. try {
  255. v1 = throwing_class(helper);
  256. BOOST_TEST(v1.which() == 1);
  257. BOOST_TEST(boost::get<throwing_class>(&v1));
  258. } catch (const exception_on_assignment& /*e*/) {
  259. BOOST_TEST(!v1.which());
  260. BOOST_TEST(boost::get<int>(&v1));
  261. }
  262. try {
  263. v2 = throwing_class(helper);
  264. BOOST_TEST(v2.which() == 1);
  265. BOOST_TEST(boost::get<throwing_class>(&v2));
  266. } catch (const exception_on_assignment& /*e*/) {
  267. BOOST_TEST(!v2.which());
  268. BOOST_TEST(boost::get<int>(&v2));
  269. }
  270. if (!v1.which() && !v2.which()) {
  271. swap(v1, v2);
  272. BOOST_TEST(!v1.which());
  273. BOOST_TEST(boost::get<int>(&v1));
  274. BOOST_TEST(!v2.which());
  275. BOOST_TEST(boost::get<int>(&v2));
  276. v1 = v2;
  277. }
  278. }
  279. template <class Nonthrowing>
  280. inline void check_5_impl(int helper)
  281. {
  282. boost::variant<Nonthrowing, throwing_class> v1, v2;
  283. throwing_class throw_not_now;
  284. throw_not_now.trash = throwing_class::do_not_throw;
  285. v1 = throw_not_now;
  286. v2 = throw_not_now;
  287. boost::get<throwing_class>(v1).trash = 1;
  288. boost::get<throwing_class>(v2).trash = 1;
  289. try {
  290. v1 = throwing_class(helper);
  291. BOOST_TEST(v1.which() == 1);
  292. BOOST_TEST(boost::get<throwing_class>(&v1));
  293. } catch (const exception_on_assignment& /*e*/) {
  294. BOOST_TEST(v1.which() == 1);
  295. BOOST_TEST(boost::get<throwing_class>(&v1));
  296. }
  297. boost::get<throwing_class>(v1).trash = throwing_class::do_not_throw;
  298. boost::get<throwing_class>(v2).trash = throwing_class::do_not_throw;
  299. v1 = Nonthrowing();
  300. v2 = Nonthrowing();
  301. try {
  302. v1 = throwing_class(helper);
  303. BOOST_TEST(v1.which() == 1);
  304. BOOST_TEST(boost::get<throwing_class>(&v1));
  305. } catch (const exception_on_assignment& /*e*/) {
  306. BOOST_TEST(v1.which() == 0);
  307. BOOST_TEST(boost::get<Nonthrowing>(&v1));
  308. }
  309. int v1_type = v1.which();
  310. int v2_type = v2.which();
  311. try {
  312. swap(v1, v2); // Make sure that backup holders swap well
  313. BOOST_TEST(v1.which() == v2_type);
  314. BOOST_TEST(v2.which() == v1_type);
  315. } catch (const exception_on_assignment& /*e*/) {
  316. BOOST_TEST(v1.which() == v1_type);
  317. BOOST_TEST(v2.which() == v2_type);
  318. }
  319. }
  320. inline void check_5(int helper = 1)
  321. {
  322. check_5_impl<nonthrowing_class>(helper);
  323. check_5_impl<nonthrowing_class2>(helper);
  324. check_5_impl<nonthrowing_class3>(helper);
  325. check_5_impl<nonthrowing_class4>(helper);
  326. check_5_impl<boost::blank>(helper);
  327. }
  328. template <class Nonthrowing>
  329. inline void check_6_impl(int helper)
  330. {
  331. boost::variant<Nonthrowing, throwing_class> v1, v2;
  332. throwing_class throw_not_now;
  333. throw_not_now.trash = throwing_class::do_not_throw;
  334. v1 = throw_not_now;
  335. v2 = throw_not_now;
  336. v1 = throw_not_now;
  337. v2 = throw_not_now;
  338. swap(v1, v2);
  339. boost::get<throwing_class>(v1).trash = 1;
  340. boost::get<throwing_class>(v2).trash = 1;
  341. v1 = throwing_class(throw_not_now);
  342. v2 = v1;
  343. v1 = Nonthrowing();
  344. try {
  345. throwing_class tc;
  346. tc.trash = helper;
  347. v1 = tc;
  348. BOOST_TEST(v1.which() == 1);
  349. BOOST_TEST(boost::get<throwing_class>(&v1));
  350. } catch (const exception_on_assignment& /*e*/) {
  351. BOOST_TEST(v1.which() == 0);
  352. }
  353. v2 = Nonthrowing();
  354. try {
  355. v2 = 2;
  356. BOOST_TEST(false);
  357. } catch (const exception_on_assignment& /*e*/) {
  358. BOOST_TEST(v2.which() == 0);
  359. }
  360. // Probably the most significant test:
  361. // unsuccessful swap must preserve old values of variant
  362. v1 = throw_not_now;
  363. boost::get<throwing_class>(v1).trash = helper;
  364. try {
  365. swap(v1, v2);
  366. } catch (const exception_on_assignment& /*e*/) {
  367. BOOST_TEST(v1.which() == 1);
  368. BOOST_TEST(v2.which() == 0);
  369. BOOST_TEST(boost::get<throwing_class>(v1).trash == helper);
  370. }
  371. }
  372. inline void check_6(int helper = 1)
  373. {
  374. check_6_impl<nonthrowing_class>(helper);
  375. check_6_impl<nonthrowing_class2>(helper);
  376. check_6_impl<nonthrowing_class3>(helper);
  377. check_6_impl<nonthrowing_class4>(helper);
  378. check_6_impl<boost::blank>(helper);
  379. }
  380. int main()
  381. {
  382. // throwing_class::throw_after_1 + 1 => throw on first assignment/construction
  383. // throwing_class::throw_after_1 => throw on second assignment/construction
  384. // throwing_class::throw_after_2 => throw on third assignment/construction
  385. // ...
  386. for (int i = throwing_class::throw_after_1 + 1; i != throwing_class::do_not_throw; --i) {
  387. check_1(i);
  388. check_2(i);
  389. check_3(i);
  390. check_4(i);
  391. check_5(i);
  392. check_6(i);
  393. }
  394. return boost::report_errors();
  395. }