expected.t.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839
  1. // This version targets C++11 and later.
  2. //
  3. // Copyright (c) 2016-2018 Martin Moene.
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // expected lite is based on:
  9. // A proposal to add a utility class to represent expected monad
  10. // by Vicente J. Botet Escriba and Pierre Talbot, http:://wg21.link/p0323
  11. #include "expected-main.t.hpp"
  12. #ifndef nsel_CONFIG_CONFIRMS_COMPILATION_ERRORS
  13. #define nsel_CONFIG_CONFIRMS_COMPILATION_ERRORS 0
  14. #endif
  15. // Suppress:
  16. // - unused parameter, for cases without assertions such as [.std...]
  17. #if defined(__clang__)
  18. # pragma clang diagnostic ignored "-Wunused-parameter"
  19. #elif defined __GNUC__
  20. # pragma GCC diagnostic ignored "-Wunused-parameter"
  21. #endif
  22. //namespace {
  23. using namespace nonstd;
  24. struct Implicit { int x; Implicit(int v) : x(v) {} };
  25. struct Explicit { int x; explicit Explicit(int v) : x(v) {} };
  26. struct MoveOnly {
  27. int x;
  28. explicit MoveOnly(int x) :x{x} {}
  29. MoveOnly(const MoveOnly&) = delete;
  30. MoveOnly(MoveOnly&& other) noexcept :x{other.x} {}
  31. MoveOnly& operator=(const MoveOnly&) = delete;
  32. MoveOnly& operator=(MoveOnly&& other) noexcept {
  33. if (&other == this) return *this;
  34. x = other.x;
  35. return *this;
  36. }
  37. };
  38. bool operator==( Implicit a, Implicit b ) { return a.x == b.x; }
  39. bool operator==( Explicit a, Explicit b ) { return a.x == b.x; }
  40. template< typename R >
  41. bool operator==( MoveOnly const & a, R const & r ) { return a.x == r; }
  42. std::ostream & operator<<( std::ostream & os, Implicit i ) { return os << "Implicit:" << i.x; }
  43. std::ostream & operator<<( std::ostream & os, Explicit e ) { return os << "Explicit:" << e.x; }
  44. std::ostream & operator<<( std::ostream & os, MoveOnly const & m ) { return os << "MoveOnly:" << m.x; }
  45. struct InitList
  46. {
  47. std::vector<int> vec;
  48. char c;
  49. InitList( std::initializer_list<int> il, char k ) noexcept
  50. : vec( il ), c( k ) {}
  51. // InitList( InitList const & ) noexcept = default;
  52. // InitList( InitList && ) noexcept = default;
  53. //
  54. // InitList & operator=( InitList const & ) noexcept = default;
  55. // InitList & operator=( InitList && ) noexcept = default;
  56. };
  57. enum State
  58. {
  59. sDefaultConstructed,
  60. sValueCopyConstructed,
  61. sValueMoveConstructed,
  62. sCopyConstructed,
  63. sMoveConstructed,
  64. sMoveAssigned,
  65. sCopyAssigned,
  66. sValueCopyAssigned,
  67. sValueMoveAssigned,
  68. sMovedFrom,
  69. sValueConstructed
  70. };
  71. struct OracleVal
  72. {
  73. State s;
  74. int i;
  75. OracleVal(int i_ = 0) : s(sValueConstructed), i(i_) {}
  76. bool operator==( OracleVal const & other ) const { return s==other.s && i==other.i; }
  77. };
  78. struct Oracle
  79. {
  80. State s;
  81. OracleVal val;
  82. Oracle() : s(sDefaultConstructed) {}
  83. Oracle(const OracleVal& v) : s(sValueCopyConstructed), val(v) {}
  84. Oracle(OracleVal&& v) : s(sValueMoveConstructed), val(std::move(v)) {v.s = sMovedFrom;}
  85. Oracle(const Oracle& o) : s(sCopyConstructed), val(o.val) {}
  86. Oracle(Oracle&& o) : s(sMoveConstructed), val(std::move(o.val)) {o.s = sMovedFrom;}
  87. Oracle& operator=(const OracleVal& v) { s = sValueCopyConstructed; val = v; return *this; }
  88. Oracle& operator=(OracleVal&& v) { s = sValueMoveConstructed; val = std::move(v); v.s = sMovedFrom; return *this; }
  89. Oracle& operator=(const Oracle& o) { s = sCopyConstructed; val = o.val; return *this; }
  90. Oracle& operator=(Oracle&& o) { s = sMoveConstructed; val = std::move(o.val); o.s = sMovedFrom; return *this; }
  91. bool operator==( Oracle const & other ) const { return s == other.s && val == other.val;}
  92. };
  93. std::ostream & operator<<( std::ostream & os, OracleVal const & o )
  94. {
  95. using lest::to_string;
  96. return os << "[oracle:" << to_string( o.i ) << "]";
  97. }
  98. //} // anonymous namespace
  99. namespace nonstd {
  100. template< typename T, typename E >
  101. std::ostream & operator<<( std::ostream & os, expected<T,E> const & e )
  102. {
  103. using lest::to_string;
  104. return os << ( e ? to_string( *e ) : "[error:" + to_string( e.error() ) + "]" );
  105. }
  106. template< typename E >
  107. std::ostream & operator<<( std::ostream & os, unexpected_type<E> const & u )
  108. {
  109. using lest::to_string;
  110. return os << "[unexp:" << to_string( u.value() ) << "]";
  111. }
  112. }
  113. using namespace nonstd;
  114. //
  115. // test specification:
  116. //
  117. CASE( "A C++11 union can contain non-POD types" "[.]" )
  118. {
  119. struct nonpod { nonpod(){} };
  120. union U
  121. {
  122. char c;
  123. nonpod np;
  124. };
  125. }
  126. // -----------------------------------------------------------------------
  127. // storage_t<>
  128. CASE( "[storage_t]" "[.implement]" )
  129. {
  130. }
  131. // -----------------------------------------------------------------------
  132. // unexpected_type, unexpected_type<std::exception_ptr>
  133. CASE( "unexpected_type: Disallows default construction" )
  134. {
  135. #if nsel_CONFIG_CONFIRMS_COMPILATION_ERRORS
  136. unexpected_type<int> u;
  137. #endif
  138. }
  139. CASE( "unexpected_type: Allows to copy-construct from unexpected_type, default" )
  140. {
  141. unexpected_type<int> a{ 7 };
  142. unexpected_type<int> b( a );
  143. EXPECT( a.value() == 7 );
  144. EXPECT( b.value() == 7 );
  145. }
  146. CASE( "unexpected_type: Allows to move-construct from unexpected_type, default" )
  147. {
  148. unexpected_type<int> a{ 7 };
  149. unexpected_type<int> b( std::move( a ) );
  150. EXPECT( a.value() == 7 );
  151. EXPECT( b.value() == 7 );
  152. }
  153. CASE( "unexpected_type: Allows to in-place-construct" )
  154. {
  155. unexpected_type<Explicit> ue( in_place, 5 );
  156. unexpected_type<Implicit> ui( in_place, 7 );
  157. EXPECT( ue.value() == Explicit{5} );
  158. EXPECT( ui.value() == Implicit{7} );
  159. }
  160. CASE( "unexpected_type: Allows to in-place-construct from initializer_list" )
  161. {
  162. unexpected_type<InitList> u( in_place, { 7, 8, 9 }, 'a' );
  163. EXPECT( u.value().vec[0] == 7 );
  164. EXPECT( u.value().vec[1] == 8 );
  165. EXPECT( u.value().vec[2] == 9 );
  166. EXPECT( u.value().c == 'a');
  167. }
  168. CASE( "unexpected_type: Allows to copy-construct from error_type" )
  169. {
  170. unexpected_type<int> u{ 7 };
  171. EXPECT( u.value() == 7 );
  172. }
  173. CASE( "unexpected_type: Allows to move-construct from error_type" )
  174. {
  175. unexpected_type<int> u{ std::move( 7 ) };
  176. EXPECT( u.value() == 7 );
  177. }
  178. CASE( "unexpected_type: Allows to copy-construct from unexpected_type, explicit converting" )
  179. {
  180. unexpected_type<int> a{ 7 };
  181. unexpected_type<Explicit> b{ a };
  182. EXPECT( b.value() == Explicit{7} );
  183. }
  184. CASE( "unexpected_type: Allows to copy-construct from unexpected_type, non-explicit converting" )
  185. {
  186. unexpected_type<int> a{ 7 };
  187. unexpected_type<Implicit> b( a );
  188. EXPECT( b.value() == Implicit{7} );
  189. }
  190. CASE( "unexpected_type: Allows to move-construct from unexpected_type, explicit converting" )
  191. {
  192. unexpected_type<int> a{ 7 };
  193. unexpected_type<Explicit> b{ std::move( a ) };
  194. EXPECT( b.value() == Explicit{7} );
  195. }
  196. CASE( "unexpected_type: Allows to move-construct from unexpected_type, non-explicit converting" )
  197. {
  198. unexpected_type<int> a{ 7 };
  199. unexpected_type<Implicit> b( std::move( a ) );
  200. EXPECT( b.value() == Implicit{7} );
  201. }
  202. CASE( "unexpected_type: Allows to copy-assign from unexpected_type, default" )
  203. {
  204. unexpected_type<int> a{ 7 };
  205. unexpected_type<int> b{ 0 };
  206. b = a;
  207. EXPECT( b.value() == 7 );
  208. }
  209. CASE( "unexpected_type: Allows to move-assign from unexpected_type, default" )
  210. {
  211. unexpected_type<int> a{ 7 };
  212. unexpected_type<int> b{ 0 };
  213. b = std::move( a );
  214. EXPECT( b.value() == 7 );
  215. }
  216. CASE( "unexpected_type: Allows to copy-assign from unexpected_type, converting" )
  217. {
  218. unexpected_type<int> u{ 7 };
  219. unexpected_type<Explicit> ue{ 0 };
  220. unexpected_type<Implicit> ui{ 0 };
  221. ue = u;
  222. ui = u;
  223. EXPECT( ue.value() == Explicit{7} );
  224. EXPECT( ui.value() == Implicit{7} );
  225. }
  226. CASE( "unexpected_type: Allows to move-assign from unexpected, converting" )
  227. {
  228. unexpected_type<int> u{ 7 };
  229. unexpected_type<int> v{ 7 };
  230. unexpected_type<Explicit> ue{ 0 };
  231. unexpected_type<Implicit> ui{ 0 };
  232. ue = std::move( u );
  233. ui = std::move( v );
  234. EXPECT( ue.value() == Explicit{7} );
  235. EXPECT( ui.value() == Implicit{7} );
  236. }
  237. CASE( "unexpected_type: Allows to observe its value via a l-value reference" )
  238. {
  239. unexpected_type<int> u{ 7 };
  240. unexpected_type<int> uc{ 7 };
  241. EXPECT( u.value() == 7 );
  242. EXPECT( uc.value() == 7 );
  243. }
  244. CASE( "unexpected_type: Allows to observe its value via a r-value reference" )
  245. {
  246. unexpected_type<int> u{ 7 };
  247. unexpected_type<int> uc{ 7 };
  248. EXPECT( std::move( u).value() == 7 );
  249. EXPECT( std::move(uc).value() == 7 );
  250. }
  251. CASE( "unexpected_type: Allows to modify its value via a l-value reference" )
  252. {
  253. unexpected_type<int> u{ 5 };
  254. u.value() = 7;
  255. EXPECT( u.value() == 7 );
  256. }
  257. //CASE( "unexpected_type: Allows to modify its value via a r-value reference" )
  258. //{
  259. // const auto v = 9;
  260. // unexpected_type<int> u{ 7 };
  261. //
  262. // std::move( u.value() ) = v;
  263. //
  264. // EXPECT( u.value() == v );
  265. //}
  266. CASE( "unexpected_type: Allows to be swapped" )
  267. {
  268. unexpected_type<int> a{ 5 };
  269. unexpected_type<int> b{ 7 };
  270. a.swap( b );
  271. EXPECT( a.value() == 7 );
  272. EXPECT( b.value() == 5 );
  273. }
  274. //CASE( "unexpected_type: Allows reset via = {}" )
  275. //{
  276. // unexpected_type<int> u( 3 );
  277. //
  278. // u = {};
  279. //}
  280. // -----------------------------------------------------------------------
  281. // unexpected_type<std::exception_ptr>
  282. // TODO: unexpected_type: Should expected be specialized for particular E types such as exception_ptr and how?
  283. // See p0323r7 2.1. Ergonomics, http://wg21.link/p0323
  284. namespace {
  285. std::exception_ptr make_ep()
  286. {
  287. try
  288. {
  289. // this generates an std::out_of_range:
  290. (void) std::string().at(1);
  291. }
  292. catch(...)
  293. {
  294. return std::current_exception();
  295. }
  296. return nullptr;
  297. }
  298. } // anonymous namespace
  299. CASE( "unexpected_type<std::exception_ptr>: Disallows default construction" )
  300. {
  301. #if nsel_CONFIG_CONFIRMS_COMPILATION_ERRORS
  302. unexpected_type<std::exception_ptr> u;
  303. #endif
  304. }
  305. CASE( "unexpected_type<std::exception_ptr>: Allows to copy-construct from error_type" )
  306. {
  307. auto ep = make_ep();
  308. unexpected_type<std::exception_ptr> u{ ep };
  309. EXPECT( u.value() == ep );
  310. }
  311. CASE( "unexpected_type<std::exception_ptr>: Allows to move-construct from error_type" )
  312. {
  313. auto ep_move = make_ep();
  314. const auto ep_copy = ep_move;
  315. unexpected_type<std::exception_ptr> u{ std::move( ep_move ) };
  316. EXPECT( u.value() == ep_copy );
  317. }
  318. CASE( "unexpected_type<std::exception_ptr>: Allows to copy-construct from an exception" )
  319. {
  320. std::string text = "hello, world";
  321. unexpected_type<std::exception_ptr> u{ std::make_exception_ptr( std::logic_error( text.c_str() ) ) };
  322. try
  323. {
  324. std::rethrow_exception( u.value() );
  325. }
  326. catch( std::exception const & e )
  327. {
  328. EXPECT( e.what() == text );
  329. }
  330. }
  331. CASE( "unexpected_type<std::exception_ptr>: Allows to observe its value" )
  332. {
  333. const auto ep = make_ep();
  334. unexpected_type<std::exception_ptr> u{ ep };
  335. EXPECT( u.value() == ep );
  336. }
  337. CASE( "unexpected_type<std::exception_ptr>: Allows to modify its value" )
  338. {
  339. const auto ep1 = make_ep();
  340. const auto ep2 = make_ep();
  341. unexpected_type<std::exception_ptr> u{ ep1 };
  342. u.value() = ep2;
  343. EXPECT( u.value() == ep2 );
  344. }
  345. //CASE( "unexpected_type: Allows reset via = {}, std::exception_ptr specialization" )
  346. //{
  347. // unexpected_type<int> u( 3 );
  348. //
  349. // u = {};
  350. //}
  351. // -----------------------------------------------------------------------
  352. // unexpected_type relational operators
  353. CASE( "unexpected_type: Provides relational operators" )
  354. {
  355. SETUP( "" ) {
  356. unexpected_type<int> u1( 6 );
  357. unexpected_type<int> u2( 7 );
  358. // compare engaged expected with engaged expected
  359. SECTION( "==" ) { EXPECT( u1 == u1 ); }
  360. SECTION( "!=" ) { EXPECT( u1 != u2 ); }
  361. #if nsel_P0323R <= 2
  362. SECTION( "< " ) { EXPECT( u1 < u2 ); }
  363. SECTION( "> " ) { EXPECT( u2 > u1 ); }
  364. SECTION( "<=" ) { EXPECT( u1 <= u1 ); }
  365. SECTION( "<=" ) { EXPECT( u1 <= u2 ); }
  366. SECTION( ">=" ) { EXPECT( u1 >= u1 ); }
  367. SECTION( ">=" ) { EXPECT( u2 >= u1 ); }
  368. #endif
  369. }
  370. }
  371. CASE( "unexpected_type: Provides relational operators, std::exception_ptr specialization" )
  372. {
  373. SETUP( "" ) {
  374. unexpected_type<std::exception_ptr> u( make_ep() );
  375. unexpected_type<std::exception_ptr> u2( make_ep() );
  376. // compare engaged expected with engaged expected
  377. SECTION( "==" ) { EXPECT ( u == u ); }
  378. SECTION( "!=" ) { EXPECT ( u != u2); }
  379. #if nsel_P0323R <= 2
  380. SECTION( "< " ) { EXPECT_NOT( u < u ); }
  381. SECTION( "> " ) { EXPECT_NOT( u > u ); }
  382. SECTION( "< " ) { EXPECT_NOT( u < u2); }
  383. SECTION( "> " ) { EXPECT_NOT( u > u2); }
  384. SECTION( "<=" ) { EXPECT ( u <= u ); }
  385. SECTION( ">=" ) { EXPECT ( u >= u ); }
  386. #endif
  387. }
  388. }
  389. // unexpected: traits
  390. CASE( "is_unexpected<X>: Is true for unexpected_type" "[.deprecated]" )
  391. {
  392. #if nsel_P0323R <= 3
  393. EXPECT( is_unexpected<unexpected_type<std::exception_ptr>>::value );
  394. #else
  395. EXPECT( !!"is_unexpected<> is not available (nsel_P0323R > 3)" );
  396. #endif
  397. }
  398. CASE( "is_unexpected<X>: Is false for non-unexpected_type (int)" "[.deprecated]" )
  399. {
  400. #if nsel_P0323R <= 3
  401. EXPECT_NOT( is_unexpected<int>::value );
  402. #else
  403. EXPECT( !!"is_unexpected<> is not available (nsel_P0323R > 3)" );
  404. #endif
  405. }
  406. // unexpected: factory
  407. CASE( "make_unexpected(): Allows to create an unexpected_type<E> from an E" )
  408. {
  409. const auto error = 7;
  410. auto u = make_unexpected( error );
  411. EXPECT( u.value() == error );
  412. }
  413. CASE( "make_unexpected_from_current_exception(): Allows to create an unexpected_type<std::exception_ptr> from the current exception" "[.deprecated]" )
  414. {
  415. #if nsel_P0323R <= 2
  416. std::string text = "hello, world";
  417. try
  418. {
  419. throw std::logic_error( text.c_str() );
  420. }
  421. catch( std::exception const & )
  422. {
  423. auto u = make_unexpected_from_current_exception() ;
  424. try
  425. {
  426. std::rethrow_exception( u.value() );
  427. }
  428. catch( std::exception const & e )
  429. {
  430. EXPECT( e.what() == text );
  431. }
  432. }
  433. #else
  434. EXPECT( !!"make_unexpected_from_current_exception() is not available (nsel_P0323R > 2)" );
  435. #endif
  436. }
  437. CASE( "unexpected: C++17 and later provide unexpected_type as unexpected" )
  438. {
  439. #if nsel_CPP17_OR_GREATER || nsel_COMPILER_MSVC_VERSION > 141
  440. nonstd::unexpected<int> u{7};
  441. #else
  442. EXPECT( !!"unexpected is not available (no C++17)." );
  443. #endif
  444. }
  445. // -----------------------------------------------------------------------
  446. // x.x.6 bad_expected_access<>
  447. CASE( "bad_expected_access: Disallows default construction" )
  448. {
  449. #if nsel_CONFIG_CONFIRMS_COMPILATION_ERRORS
  450. bad_expected_access<int> bad;
  451. #endif
  452. }
  453. CASE( "bad_expected_access: Allows construction from error_type" )
  454. {
  455. bad_expected_access<int> bea( 123 );
  456. }
  457. CASE( "bad_expected_access: Allows to observe its error" )
  458. {
  459. const int error = 7;
  460. bad_expected_access<int> bea( error );
  461. bad_expected_access<int> const beac( error );
  462. EXPECT( bea.error() == error );
  463. EXPECT( beac.error() == error );
  464. EXPECT( std::move( bea.error() ) == error );
  465. EXPECT( std::move( beac.error() ) == error );
  466. }
  467. CASE( "bad_expected_access: Allows to change its error" )
  468. {
  469. const int old_error = 0;
  470. const int new_error = 7;
  471. bad_expected_access<int> bea( old_error );
  472. bea.error() = new_error;
  473. EXPECT( bea.error() == new_error );
  474. }
  475. CASE( "bad_expected_access: Provides non-empty what()" )
  476. {
  477. bad_expected_access<int> bea( 123 );
  478. EXPECT( ! std::string( bea.what() ).empty() );
  479. }
  480. // -----------------------------------------------------------------------
  481. // expected<>
  482. // x.x.4.1 expected<> constructors (value)
  483. CASE( "expected: Allows to default construct" )
  484. {
  485. expected<int, char> e;
  486. EXPECT( e.has_value() );
  487. }
  488. CASE( "expected: Allows to copy-construct from expected: value" )
  489. {
  490. expected<int, char> a = 7;
  491. expected<int, char> b{ a };
  492. EXPECT( b );
  493. EXPECT( b.value() == 7 );
  494. }
  495. CASE( "expected: Allows to copy-construct from expected: error" )
  496. {
  497. expected<char, int> a{ unexpect, 7 };
  498. expected<char, int> b{ a };
  499. EXPECT( !b );
  500. EXPECT( b.error() == 7 );
  501. }
  502. CASE( "expected: Allows to move-construct from expected: value" )
  503. {
  504. expected<int, char> a = 7;
  505. expected<int, char> b{ std::move( a ) };
  506. EXPECT( b );
  507. EXPECT( b.value() == 7 );
  508. }
  509. CASE( "expected: Allows to move-construct from expected: error" )
  510. {
  511. expected<char, int> a{ unexpect, 7 };
  512. expected<char, int> b{ std::move( a ) };
  513. EXPECT( !b );
  514. EXPECT( b.error() == 7 );
  515. }
  516. CASE( "expected: Allows to copy-construct from expected; value, explicit converting" )
  517. {
  518. expected<int, char> a = 7;
  519. expected<Explicit, char> b{ a };
  520. EXPECT( b );
  521. EXPECT( b.value() == Explicit{7} );
  522. }
  523. CASE( "expected: Allows to copy-construct from expected; error, explicit converting" )
  524. {
  525. expected<char, int> a{ unexpect, 7 };
  526. expected<char, Explicit> b{ a };
  527. EXPECT( !b );
  528. EXPECT( b.error() == Explicit{7} );
  529. }
  530. CASE( "expected: Allows to copy-construct from expected; value, non-explicit converting" )
  531. {
  532. expected<int, char> a = 7;
  533. expected<Implicit, char> b( a );
  534. EXPECT( b );
  535. EXPECT( b.value() == Implicit{7} );
  536. }
  537. CASE( "expected: Allows to copy-construct from expected; error, non-explicit converting" )
  538. {
  539. expected<char, int> a{ unexpect, 7 };
  540. expected<char, Implicit> b( a );
  541. EXPECT( !b );
  542. EXPECT( b.error() == Implicit{7} );
  543. }
  544. CASE( "expected: Allows to move-construct from expected; value, explicit converting" )
  545. {
  546. expected<int, char> a = 7;
  547. expected<Explicit, char> b{ std::move( a ) };
  548. EXPECT( b );
  549. EXPECT( b.value() == Explicit{7} );
  550. }
  551. CASE( "expected: Allows to move-construct from expected; error, explicit converting" )
  552. {
  553. expected<char, int> a{ unexpect, 7 };
  554. expected<char, Explicit> b{ std::move( a ) };
  555. EXPECT( !b );
  556. EXPECT( b.error() == Explicit{7} );
  557. }
  558. CASE( "expected: Allows to move-construct from expected; value, non-explicit converting" )
  559. {
  560. expected<int, char> a = 7;
  561. expected<Implicit, char> b( std::move( a ) );
  562. EXPECT( b );
  563. EXPECT( b.value() == Implicit{7} );
  564. }
  565. CASE( "expected: Allows to move-construct from expected; error, non-explicit converting" )
  566. {
  567. expected<char, int> a{ unexpect, 7 };
  568. expected<char, Implicit> b( std::move( a ) );
  569. EXPECT( !b );
  570. EXPECT( b.error() == Implicit{7} );
  571. }
  572. CASE( "expected: Allows to forward-construct from value, explicit converting" )
  573. {
  574. auto v = 7;
  575. expected<Explicit, char> b{ std::move( v ) };
  576. EXPECT( b );
  577. EXPECT( b.value() == Explicit{7} );
  578. }
  579. CASE( "expected: Allows to forward-construct from value, non-explicit converting" )
  580. {
  581. auto v = 7;
  582. expected<Implicit, char> b{ std::move( v ) };
  583. EXPECT( b );
  584. EXPECT( b.value() == Implicit{7} );
  585. }
  586. CASE( "expected: Allows to in-place-construct value" )
  587. {
  588. auto v = 7;
  589. expected<Implicit, char> b{ in_place, v };
  590. EXPECT( b );
  591. EXPECT( b.value() == Implicit{7} );
  592. }
  593. CASE( "expected: Allows to in-place-construct value from initializer_list" )
  594. {
  595. expected<InitList, char> b{ in_place, { 7, 8, 9 }, 'a' };
  596. EXPECT( b );
  597. EXPECT( b->vec[0] == 7 );
  598. EXPECT( b->vec[1] == 8 );
  599. EXPECT( b->vec[2] == 9 );
  600. EXPECT( b->c == 'a');
  601. }
  602. // x.x.4.1 expected<> constructors (error)
  603. CASE( "expected: Allows to copy-construct from unexpected, explicit converting" )
  604. {
  605. unexpected_type<int> u{ 7 };
  606. expected<int, Explicit> e{ u };
  607. EXPECT( e.error() == Explicit{7} );
  608. }
  609. CASE( "expected: Allows to copy-construct from unexpected, non-explicit converting" )
  610. {
  611. unexpected_type<int> u{ 7 };
  612. expected<int, Implicit> e{ u };
  613. EXPECT( e.error() == Implicit{7} );
  614. }
  615. CASE( "expected: Allows to move-construct from unexpected, explicit converting" )
  616. {
  617. unexpected_type<int> u{ 7 };
  618. expected<int, Explicit> e{ std::move( u ) };
  619. EXPECT( e.error() == Explicit{7} );
  620. }
  621. CASE( "expected: Allows to move-construct from unexpected, non-explicit converting" )
  622. {
  623. unexpected_type<int> u{ 7 };
  624. expected<int, Implicit> e{ std::move( u ) };
  625. EXPECT( e.error() == Implicit{7} );
  626. }
  627. CASE( "expected: Allows to in-place-construct error" )
  628. {
  629. expected<int, Explicit> e{ unexpect, 7 };
  630. expected<int, Implicit> i{ unexpect, 7 };
  631. EXPECT( e.error() == Explicit{7} );
  632. EXPECT( i.error() == Implicit{7} );
  633. }
  634. CASE( "expected: Allows to in-place-construct error from initializer_list" )
  635. {
  636. expected<char, InitList> e{ unexpect, { 7, 8, 9 }, 'a' };
  637. EXPECT( !e );
  638. EXPECT( e.error().vec[0] == 7 );
  639. EXPECT( e.error().vec[1] == 8 );
  640. EXPECT( e.error().vec[2] == 9 );
  641. EXPECT( e.error().c == 'a');
  642. }
  643. // x.x.4.3 expected<> assignment
  644. CASE( "expected: Allows to copy-assign from expected, value" )
  645. {
  646. expected<int, char> a{ 7 };
  647. expected<int, char> b;
  648. b = a;
  649. EXPECT( b );
  650. EXPECT( b.value() == 7 );
  651. }
  652. CASE( "expected: Allows to copy-assign from expected, error" )
  653. {
  654. expected<char, int> a{ unexpect, 7 };
  655. expected<char, int> b;
  656. b = a;
  657. EXPECT( !b );
  658. EXPECT( b.error() == 7 );
  659. }
  660. CASE( "expected: Allows to move-assign from expected, value" )
  661. {
  662. expected<int, char> a{ 7 };
  663. expected<int, char> b;
  664. b = std::move( a );
  665. EXPECT( b );
  666. EXPECT( b.value() == 7 );
  667. }
  668. CASE( "expected: Allows to move-assign from expected, error" )
  669. {
  670. expected<char, int> a{ unexpect, 7 };
  671. expected<char, int> b;
  672. b = std::move( a );
  673. EXPECT( !b );
  674. EXPECT( b.error() == 7 );
  675. }
  676. CASE( "expected: Allows to forward-assign from value" )
  677. {
  678. expected<int, char> a;
  679. expected<int, char> b;
  680. a = '7';
  681. b = 7;
  682. EXPECT( a );
  683. EXPECT( b );
  684. EXPECT( a.value() == '7' );
  685. EXPECT( b.value() == 7 );
  686. }
  687. CASE( "expected: Allows to copy-assign from unexpected" )
  688. {
  689. expected<char, int> e;
  690. unexpected_type<int> u{ 7 } ;
  691. e = u;
  692. EXPECT( e.error() == 7 );
  693. }
  694. CASE( "expected: Allows to move-assign from unexpected" )
  695. {
  696. expected<char, int> e;
  697. unexpected_type<int> u{ 7 } ;
  698. e = std::move( u );
  699. EXPECT( e.error() == 7 );
  700. }
  701. CASE( "expected: Allows to move-assign from move-only unexpected" )
  702. {
  703. expected<char, MoveOnly> e;
  704. unexpected_type<MoveOnly> u{7};
  705. e = std::move( u );
  706. EXPECT( e.error() == 7 );
  707. }
  708. CASE( "expected: Allows to emplace value" )
  709. {
  710. expected<int, char> a;
  711. expected<int, char> b;
  712. auto va = a.emplace( '7' );
  713. auto vb = b.emplace( 7 );
  714. EXPECT( va == '7' );
  715. EXPECT( vb == 7 );
  716. EXPECT( a.value() == '7' );
  717. EXPECT( b.value() == 7 );
  718. }
  719. CASE( "expected: Allows to emplace value from initializer_list" )
  720. {
  721. expected<InitList, char> e{ {}, 'x'};
  722. auto ve = e.emplace( { 7, 8, 9 }, 'a' );
  723. EXPECT( e );
  724. EXPECT( ve.vec[0] == 7 );
  725. EXPECT( ve.vec[1] == 8 );
  726. EXPECT( ve.vec[2] == 9 );
  727. EXPECT( ve.c == 'a');
  728. EXPECT( e.value().vec[0] == 7 );
  729. EXPECT( e.value().vec[1] == 8 );
  730. EXPECT( e.value().vec[2] == 9 );
  731. EXPECT( e.value().c == 'a');
  732. }
  733. // x.x.4.4 expected<> swap
  734. CASE( "expected: Allows to be swapped" )
  735. {
  736. using std::swap;
  737. SETUP("") {
  738. expected<int, char> e1{ 1 };
  739. expected<int, char> e2{ 2 };
  740. expected<int, char> u1{ unexpect, '1' };
  741. expected<int, char> u2{ unexpect, '2' };
  742. SECTION("value-value, member swap")
  743. {
  744. e1.swap( e2 );
  745. EXPECT( e1.value() == 2 );
  746. EXPECT( e2.value() == 1 );
  747. }
  748. SECTION("error-error, member swap")
  749. {
  750. u1.swap( u2 );
  751. EXPECT( u1.error() == '2' );
  752. EXPECT( u2.error() == '1' );
  753. }
  754. SECTION("value-error, member swap")
  755. {
  756. e1.swap( u1 );
  757. EXPECT( e1.error() == '1' );
  758. EXPECT( u1.value() == 1 );
  759. }
  760. SECTION("error-value, member swap")
  761. {
  762. u1.swap( e1 );
  763. EXPECT( u1.value() == 1 );
  764. EXPECT( e1.error() == '1' );
  765. }
  766. }
  767. }
  768. // x.x.4.5 expected<> observers
  769. CASE( "expected: Allows to observe its value via a pointer" )
  770. {
  771. const auto v = 7;
  772. expected<Implicit, char> e{ v };
  773. EXPECT( e->x == v );
  774. }
  775. CASE( "expected: Allows to observe its value via a pointer to constant" )
  776. {
  777. const auto v = 7;
  778. const expected<Implicit, char> e{ v };
  779. EXPECT( e->x == v );
  780. }
  781. CASE( "expected: Allows to modify its value via a pointer" )
  782. {
  783. const auto v1 = 7;
  784. const auto v2 = 42;
  785. expected<Implicit, char> e{ v1 };
  786. e->x = v2;
  787. EXPECT( e->x == v2 );
  788. }
  789. CASE( "expected: Allows to observe its value via a l-value reference" )
  790. {
  791. const auto v = 7;
  792. expected<Implicit, char> e{ v };
  793. expected<Implicit, char> const ec{ v };
  794. EXPECT( *e == Implicit{v} );
  795. EXPECT( *ec == Implicit{v} );
  796. }
  797. CASE( "expected: Allows to observe its value via a r-value reference" )
  798. {
  799. const auto v = 7;
  800. expected<Implicit, char> e{ v };
  801. expected<Implicit, char> const ec{ v };
  802. EXPECT( *std::move( e) == Implicit{v} );
  803. EXPECT( *std::move(ec) == Implicit{v} );
  804. }
  805. CASE( "expected: Allows to modify its value via a l-value reference" )
  806. {
  807. const auto v1 = 7;
  808. const auto v2 = 42;
  809. expected<Implicit, char> e{ v1 };
  810. *e = Implicit{v2};
  811. EXPECT( *e == Implicit{v2} );
  812. }
  813. CASE( "expected: Allows to modify its value via a r-value reference" )
  814. {
  815. const auto v1 = 7;
  816. const auto v2 = 42;
  817. expected<Implicit, char> e{ v1 };
  818. #if !nsel_COMPILER_GNUC_VERSION || nsel_COMPILER_GNUC_VERSION >= 490
  819. *std::move(e) = Implicit{v2};
  820. #else
  821. *e = Implicit{v2}; // non-r-value
  822. #endif
  823. EXPECT( *e == Implicit{v2} );
  824. }
  825. CASE( "expected: Allows to observe if it contains a value (or error)" )
  826. {
  827. expected<int, int> e;
  828. expected<int, int> u{ unexpect, 3 };
  829. EXPECT( e );
  830. EXPECT( !u );
  831. }
  832. CASE( "expected: Allows to observe its value" )
  833. {
  834. const auto v = 7;
  835. expected<Implicit, char> e{ v };
  836. expected<Implicit, char> const ec{ v };
  837. EXPECT( e.value() == Implicit{v} );
  838. EXPECT( ec.value() == Implicit{v} );
  839. }
  840. CASE( "expected: Allows to modify its value" )
  841. {
  842. const auto v1 = 7;
  843. const auto v2 = 42;
  844. expected<Implicit, char> e{ v1 };
  845. e.value() = Implicit{v2};
  846. EXPECT( e.value() == Implicit{v2} );
  847. }
  848. CASE( "expected: Allows to move its value" )
  849. {
  850. const auto v = 7;
  851. expected<Implicit, char> m{ v };
  852. expected<Implicit, char> const mc{ v };
  853. expected<Implicit, char> e{ std::move(m ).value() };
  854. expected<Implicit, char> ec{ std::move(mc).value() };
  855. EXPECT( e.value() == Implicit{v} );
  856. EXPECT( ec.value() == Implicit{v} );
  857. }
  858. CASE( "expected: Allows to observe its error" )
  859. {
  860. const auto v = 7;
  861. expected<char, Implicit> e{ unexpect, v };
  862. expected<char, Implicit> const ec{ unexpect, v };
  863. EXPECT( e.error() == Implicit{v} );
  864. EXPECT( ec.error() == Implicit{v} );
  865. }
  866. CASE( "expected: Allows to modify its error" )
  867. {
  868. const auto v1 = 7;
  869. const auto v2 = 42;
  870. expected<char, Implicit> e{ unexpect, v1 };
  871. e.error() = Implicit{v2};
  872. EXPECT( e.error() == Implicit{v2} );
  873. }
  874. CASE( "expected: Allows to move its error" )
  875. {
  876. const auto v = 7;
  877. expected<char, Implicit> m{ unexpect, v };
  878. expected<char, Implicit> const mc{ unexpect, v };
  879. expected<char, Implicit> e{ unexpect, std::move(m ).error() };
  880. expected<char, Implicit> ec{ unexpect, std::move(mc).error() };
  881. EXPECT( e.error() == Implicit{v} );
  882. EXPECT( ec.error() == Implicit{v} );
  883. }
  884. CASE( "expected: Allows to observe its error as unexpected" )
  885. {
  886. const auto v = 7;
  887. expected<char, Implicit> e{ unexpect, v };
  888. EXPECT( e.get_unexpected().value() == v );
  889. }
  890. CASE( "expected: Allows to query if it contains an exception of a specific base type" )
  891. {
  892. expected<int, std::out_of_range> e{ unexpect, std::out_of_range( "oor" ) };
  893. EXPECT( e.has_exception< std::logic_error >() );
  894. EXPECT( !e.has_exception< std::runtime_error >() );
  895. }
  896. CASE( "expected: Allows to observe its value if available, or obtain a specified value otherwise" )
  897. {
  898. const auto ve = 3;
  899. const auto vu = 7;
  900. expected<int, int> e{ ve };
  901. expected<int, int> u{ unexpect, 0 };
  902. EXPECT( e.value_or( vu ) == ve );
  903. EXPECT( u.value_or( vu ) == vu );
  904. }
  905. CASE( "expected: Allows to move its value if available, or obtain a specified value otherwise" )
  906. {
  907. const auto ve = 3;
  908. const auto vu = 7;
  909. expected<int, int> mv{ ve };
  910. expected<int, int> mu{ unexpect, 0 };
  911. EXPECT( std::move( mv ).value_or( vu ) == ve );
  912. EXPECT( std::move( mu ).value_or( vu ) == vu );
  913. }
  914. CASE( "expected: Throws bad_expected_access on value access when disengaged" )
  915. {
  916. expected<char, int > e{ unexpect, 7 };
  917. expected<char, int > ec{ unexpect, 7 };
  918. EXPECT_THROWS( e.value() );
  919. EXPECT_THROWS_AS( e.value(), bad_expected_access<int> );
  920. EXPECT_THROWS( ec.value() );
  921. EXPECT_THROWS_AS( ec.value(), bad_expected_access<int> );
  922. EXPECT_THROWS( std::move( e).value() );
  923. EXPECT_THROWS_AS( std::move( e).value(), bad_expected_access<int> );
  924. EXPECT_THROWS( std::move(ec).value() );
  925. EXPECT_THROWS_AS( std::move(ec).value(), bad_expected_access<int> );
  926. }
  927. // -----------------------------------------------------------------------
  928. // expected<void> specialization
  929. // x.x.4.1 expected<void> constructors
  930. CASE( "expected<void>: Allows to default-construct" )
  931. {
  932. expected<void, char> e;
  933. EXPECT( e.has_value() );
  934. }
  935. CASE( "expected<void>: Allows to copy-construct from expected<void>: value" )
  936. {
  937. expected<void, int> a;
  938. expected<void, int> b{ a };
  939. EXPECT( b );
  940. }
  941. CASE( "expected<void>: Allows to copy-construct from expected<void>: error" )
  942. {
  943. expected<void, int > a{ unexpect, 7 };
  944. expected<void, int> b{ a };
  945. EXPECT( !b );
  946. EXPECT( b.error() == 7 );
  947. }
  948. CASE( "expected<void>: Allows to move-construct from expected<void>: value" )
  949. {
  950. expected<void, int> a;
  951. expected<void, int> b{ std::move( a ) };
  952. EXPECT( b );
  953. }
  954. CASE( "expected<void>: Allows to move-construct from expected<void>: error" )
  955. {
  956. expected<void, int > a{ unexpect, 7 };
  957. expected<void, int> b{ std::move( a ) };
  958. EXPECT( !b );
  959. EXPECT( b.error() == 7 );
  960. }
  961. CASE( "expected<void>: Allows to in-place-construct" )
  962. {
  963. expected<void, int > e{ in_place };
  964. EXPECT( e );
  965. }
  966. CASE( "expected<void>: Allows to copy-construct from unexpected, explicit converting" )
  967. {
  968. unexpected_type<int> u{ 7 };
  969. expected<void, Explicit> e{ u };
  970. EXPECT( e.error() == Explicit{7} );
  971. }
  972. CASE( "expected<void>: Allows to copy-construct from unexpected, non-explicit converting" )
  973. {
  974. unexpected_type<int> u{ 7 };
  975. expected<void, Implicit> e{ u };
  976. EXPECT( e.error() == Implicit{7} );
  977. }
  978. CASE( "expected<void>: Allows to move-construct from unexpected, explicit converting" )
  979. {
  980. unexpected_type<int> u{ 7 };
  981. expected<void, Explicit> e{ std::move( u ) };
  982. EXPECT( e.error() == Explicit{7} );
  983. }
  984. CASE( "expected<void>: Allows to move-construct from unexpected, non-explicit converting" )
  985. {
  986. unexpected_type<int> u{ 7 };
  987. expected<void, Implicit> e{ std::move( u ) };
  988. EXPECT( e.error() == Implicit{7} );
  989. }
  990. CASE( "expected<void>: Allows to in-place-construct unexpected_type" )
  991. {
  992. expected<void, Explicit> e{ unexpect, 7 };
  993. expected<void, Implicit> i{ unexpect, 7 };
  994. EXPECT( e.error() == Explicit{7} );
  995. EXPECT( i.error() == Implicit{7} );
  996. }
  997. CASE( "expected<void>: Allows to in-place-construct error from initializer_list" )
  998. {
  999. expected<void, InitList> e{ unexpect, { 7, 8, 9 }, 'a' };
  1000. EXPECT( !e );
  1001. EXPECT( e.error().vec[0] == 7 );
  1002. EXPECT( e.error().vec[1] == 8 );
  1003. EXPECT( e.error().vec[2] == 9 );
  1004. EXPECT( e.error().c == 'a');
  1005. }
  1006. // x.x.4.3 expected<void> assignment
  1007. CASE( "expected<void>: Allows to copy-assign from expected, value" )
  1008. {
  1009. expected<void, int> a;
  1010. expected<void, int> b;
  1011. b = a;
  1012. EXPECT( b );
  1013. }
  1014. CASE( "expected<void>: Allows to copy-assign from expected, error" )
  1015. {
  1016. expected<void, int> a{ unexpect, 7 };
  1017. expected<void, int> b;
  1018. b = a;
  1019. EXPECT( !b );
  1020. EXPECT( b.error() == 7 );
  1021. }
  1022. CASE( "expected<void>: Allows to move-assign from expected, value" )
  1023. {
  1024. expected<void, int> a;
  1025. expected<void, int> b;
  1026. b = std::move( a );
  1027. EXPECT( b );
  1028. }
  1029. CASE( "expected<void>: Allows to move-assign from expected, error" )
  1030. {
  1031. expected<void, int> a{ unexpect, 7 };
  1032. expected<void, int> b;
  1033. b = std::move( a );
  1034. EXPECT( !b );
  1035. EXPECT( b.error() == 7 );
  1036. }
  1037. CASE( "expected<void>: Allows to emplace value" )
  1038. {
  1039. expected<void, int> a{ unexpect, 7 };
  1040. a.emplace();
  1041. EXPECT( a );
  1042. }
  1043. // x.x.4.4 expected<void> swap
  1044. CASE( "expected<void>: Allows to be swapped" )
  1045. {
  1046. using std::swap;
  1047. SETUP("") {
  1048. expected<void, char> e1;
  1049. expected<void, char> e2;
  1050. expected<void, char> u1{ unexpect, '1' };
  1051. expected<void, char> u2{ unexpect, '2' };
  1052. SECTION("value-value, member swap")
  1053. {
  1054. e1.swap( e2 );
  1055. EXPECT( e1 );
  1056. EXPECT( e2 );
  1057. }
  1058. SECTION("error-error, member swap")
  1059. {
  1060. u1.swap( u2 );
  1061. EXPECT( u1.error() == '2' );
  1062. EXPECT( u2.error() == '1' );
  1063. }
  1064. SECTION("value-error, member swap")
  1065. {
  1066. e1.swap( u1 );
  1067. EXPECT( e1.error() == '1' );
  1068. EXPECT( u1 );
  1069. }
  1070. SECTION("error-value, member swap")
  1071. {
  1072. u1.swap( e1 );
  1073. EXPECT( u1 );
  1074. EXPECT( e1.error() == '1' );
  1075. }
  1076. }
  1077. }
  1078. // x.x.4.5 expected<void> observers
  1079. CASE( "expected<void>: Allows to observe if it contains a value (or error)" )
  1080. {
  1081. expected<void, int> e;
  1082. expected<void, int> u{ unexpect, 3 };
  1083. EXPECT( e );
  1084. EXPECT( !u );
  1085. }
  1086. CASE( "expected<void>: Allows to observe its value" )
  1087. {
  1088. expected<void, int> e;
  1089. expected<void, int> const ec;
  1090. EXPECT( ( e.value(), true ) );
  1091. EXPECT( ( ec.value(), true ) );
  1092. }
  1093. CASE( "expected<void>: Allows to observe its error" )
  1094. {
  1095. const auto v = 7;
  1096. expected<void, Implicit> e{ unexpect, v };
  1097. expected<void, Implicit> const ec{ unexpect, v };
  1098. EXPECT( e.error() == Implicit{v} );
  1099. EXPECT( ec.error() == Implicit{v} );
  1100. }
  1101. CASE( "expected<void>: Allows to modify its error" )
  1102. {
  1103. const auto v1 = 7;
  1104. const auto v2 = 42;
  1105. expected<void, Implicit> e{ unexpect, v1 };
  1106. e.error() = Implicit{v2};
  1107. EXPECT( e.error() == Implicit{v2} );
  1108. }
  1109. CASE( "expected<void>: Allows to move its error" )
  1110. {
  1111. const auto v = 7;
  1112. expected<void, Implicit> m{ unexpect, v };
  1113. expected<void, Implicit> const mc{ unexpect, v };
  1114. expected<void, Implicit> e{ unexpect, std::move(m ).error() };
  1115. expected<void, Implicit> ec{ unexpect, std::move(mc).error() };
  1116. EXPECT( e.error() == Implicit{v} );
  1117. EXPECT( ec.error() == Implicit{v} );
  1118. }
  1119. CASE( "expected<void>: Allows to observe its error as unexpected" )
  1120. {
  1121. const auto value = 7;
  1122. expected<void, int> e{ unexpect, value };
  1123. EXPECT( e.get_unexpected().value() == value );
  1124. }
  1125. CASE( "expected<void>: Allows to query if it contains an exception of a specific base type" )
  1126. {
  1127. expected<void, std::out_of_range> e{ unexpect, std::out_of_range( "oor" ) };
  1128. EXPECT( e.has_exception< std::logic_error >() );
  1129. EXPECT( !e.has_exception< std::runtime_error >() );
  1130. }
  1131. CASE( "expected<void>: Throws bad_expected_access on value access when disengaged" )
  1132. {
  1133. expected<void, int > e{ unexpect, 7 };
  1134. expected<void, int > ec{ unexpect, 7 };
  1135. EXPECT_THROWS( e.value() );
  1136. EXPECT_THROWS_AS( e.value(), bad_expected_access<int> );
  1137. EXPECT_THROWS( ec.value() );
  1138. EXPECT_THROWS_AS( ec.value(), bad_expected_access<int> );
  1139. EXPECT_THROWS( std::move( e).value() );
  1140. EXPECT_THROWS_AS( std::move( e).value(), bad_expected_access<int> );
  1141. EXPECT_THROWS( std::move(ec).value() );
  1142. EXPECT_THROWS_AS( std::move(ec).value(), bad_expected_access<int> );
  1143. }
  1144. // [expected<> unwrap()]
  1145. // [expected<> factories]
  1146. // x.x.4.7 expected<>: relational operators
  1147. CASE( "operators: Provides expected relational operators" )
  1148. {
  1149. SETUP( "" ) {
  1150. auto v1 = 6;
  1151. auto v2 = 7;
  1152. expected<int, char> e1( v1 );
  1153. expected<int, char> e2( v2 );
  1154. unexpected_type<char> u( 'u' );
  1155. expected<int, char> d( u );
  1156. // compare engaged expected with engaged expected
  1157. SECTION( "engaged == engaged" ) { EXPECT( e1 == e1 ); }
  1158. SECTION( "engaged != engaged" ) { EXPECT( e1 != e2 ); }
  1159. #if nsel_P0323R <= 2
  1160. SECTION( "engaged < engaged" ) { EXPECT( e1 < e2 ); }
  1161. SECTION( "engaged > engaged" ) { EXPECT( e2 > e1 ); }
  1162. SECTION( "engaged <= engaged" ) { EXPECT( e1 <= e1 ); }
  1163. SECTION( "engaged <= engaged" ) { EXPECT( e1 <= e2 ); }
  1164. SECTION( "engaged >= engaged" ) { EXPECT( e1 >= e1 ); }
  1165. SECTION( "engaged >= engaged" ) { EXPECT( e2 >= e1 ); }
  1166. #endif
  1167. // compare engaged expected with value
  1168. SECTION( "engaged == value" ) { EXPECT( e1 == v1 ); }
  1169. SECTION( "value == engaged" ) { EXPECT( v1 == e1 ); }
  1170. SECTION( "engaged != value" ) { EXPECT( e1 != v2 ); }
  1171. SECTION( "value != engaged" ) { EXPECT( v1 != e2 ); }
  1172. #if nsel_P0323R <= 2
  1173. SECTION( "engaged < value" ) { EXPECT( e1 < v2 ); }
  1174. SECTION( "value < engaged" ) { EXPECT( v1 < e2 ); }
  1175. SECTION( "engaged > value" ) { EXPECT( e2 > v1 ); }
  1176. SECTION( "value > engaged" ) { EXPECT( v2 > e1 ); }
  1177. SECTION( "engaged <= value" ) { EXPECT( e1 <= v2 ); }
  1178. SECTION( "value <= engaged" ) { EXPECT( v1 <= e2 ); }
  1179. SECTION( "engaged >= value" ) { EXPECT( e2 >= v1 ); }
  1180. SECTION( "value >= engaged" ) { EXPECT( v2 >= e1 ); }
  1181. #endif
  1182. // compare engaged expected with disengaged expected
  1183. SECTION( "engaged == disengaged" ) { EXPECT_NOT( e1 == d ); }
  1184. SECTION( "disengaged == engaged" ) { EXPECT_NOT( d == e1 ); }
  1185. SECTION( "engaged != disengaged" ) { EXPECT ( e1 != d ); }
  1186. SECTION( "disengaged != engaged" ) { EXPECT ( d != e2 ); }
  1187. #if nsel_P0323R <= 2
  1188. SECTION( "engaged < disengaged" ) { EXPECT_NOT( e1 < d ); }
  1189. SECTION( "disengaged < engaged" ) { EXPECT ( d < e2 ); }
  1190. SECTION( "engaged > disengaged" ) { EXPECT ( e2 > d ); }
  1191. SECTION( "disengaged > engaged" ) { EXPECT_NOT( d > e1 ); }
  1192. SECTION( "engaged <= disengaged" ) { EXPECT_NOT( e1 <= d ); }
  1193. SECTION( "disengaged <= engaged" ) { EXPECT ( d <= e2 ); }
  1194. SECTION( "engaged >= disengaged" ) { EXPECT ( e2 >= d ); }
  1195. SECTION( "disengaged >= engaged" ) { EXPECT_NOT( d >= e1 ); }
  1196. #endif
  1197. // compare engaged expected with unexpected
  1198. SECTION( "disengaged == unexpected" ) { EXPECT ( d == u ); }
  1199. SECTION( "unexpected == disengaged" ) { EXPECT ( u == d ); }
  1200. SECTION( "engaged != unexpected" ) { EXPECT ( e1 != u ); }
  1201. SECTION( "unexpected != engaged" ) { EXPECT ( u != e1 ); }
  1202. #if nsel_P0323R <= 2
  1203. SECTION( "disengaged < unexpected" ) { EXPECT_NOT( d < u ); }
  1204. SECTION( "unexpected < disengaged" ) { EXPECT_NOT( u < d ); }
  1205. SECTION( "disengaged <= unexpected" ) { EXPECT ( d <= u ); }
  1206. SECTION( "unexpected <= disengaged" ) { EXPECT ( u <= d ); }
  1207. SECTION( "disengaged > unexpected" ) { EXPECT_NOT( d > u ); }
  1208. SECTION( "unexpected > disengaged" ) { EXPECT_NOT( u > d ); }
  1209. SECTION( "disengaged >= unexpected" ) { EXPECT ( d >= u ); }
  1210. SECTION( "unexpected >= disengaged" ) { EXPECT ( u >= d ); }
  1211. #endif
  1212. }
  1213. }
  1214. // -----------------------------------------------------------------------
  1215. // expected: specialized algorithms
  1216. // -----------------------------------------------------------------------
  1217. // Other
  1218. CASE( "swap: Allows expected to be swapped" )
  1219. {
  1220. using std::swap;
  1221. SETUP("") {
  1222. expected<int, char> e1{ 1 };
  1223. expected<int, char> e2{ 2 };
  1224. expected<int, char> u1{ unexpect, '1' };
  1225. expected<int, char> u2{ unexpect, '2' };
  1226. SECTION("value-value, std::swap")
  1227. {
  1228. swap( e1, e2 );
  1229. EXPECT( e1.value() == 2 );
  1230. EXPECT( e2.value() == 1 );
  1231. }
  1232. SECTION("error-error, std::swap")
  1233. {
  1234. swap( u1, u2 );
  1235. EXPECT( u1.error() == '2' );
  1236. EXPECT( u2.error() == '1' );
  1237. }
  1238. SECTION("value-error, std::swap")
  1239. {
  1240. swap( e1, u1 );
  1241. EXPECT( e1.error() == '1' );
  1242. EXPECT( u1.value() == 1 );
  1243. }
  1244. SECTION("error-value, std::swap")
  1245. {
  1246. swap( u1, e1 );
  1247. EXPECT( u1.value() == 1 );
  1248. EXPECT( e1.error() == '1' );
  1249. }
  1250. }
  1251. }
  1252. CASE( "std::hash: Allows to compute hash value for expected" )
  1253. {
  1254. expected<int, char> a{ 7 };
  1255. expected<int, char> b{ 7 };
  1256. EXPECT( (std::hash< expected<int, char> >{}( a )) == (std::hash< expected<int, char> >{}( b )) );
  1257. }
  1258. #if nsel_P0323R <= 3
  1259. #include <memory>
  1260. void vfoo() {}
  1261. expected<int> foo()
  1262. {
  1263. return make_expected( 7 );
  1264. }
  1265. expected<std::unique_ptr<int>> bar()
  1266. {
  1267. return make_expected( std::unique_ptr<int>( new int(7) ) );
  1268. }
  1269. #endif // nsel_P0323R
  1270. CASE( "make_expected(): create expected from given value" "[.deprecated]" )
  1271. {
  1272. #if nsel_P0323R <= 3
  1273. auto e = make_expected( 7 );
  1274. EXPECT( e );
  1275. EXPECT( *e == 7 );
  1276. #else
  1277. EXPECT( !!"make_expected() is not available (nsel_P0323R > 3)" );
  1278. #endif
  1279. }
  1280. CASE( "make_expected(): create expected<void>" "[.deprecated]" )
  1281. {
  1282. #if nsel_P0323R <= 3
  1283. auto e = make_expected();
  1284. EXPECT( e );
  1285. #else
  1286. EXPECT( !!"make_expected() is not available (nsel_P0323R > 3)" );
  1287. #endif
  1288. }
  1289. CASE( "make_expected_from_current_exception(): create expected from current exception" "[.deprecated]" )
  1290. {
  1291. #if nsel_P0323R <= 3
  1292. EXPECT( !!"Implement" );
  1293. #else
  1294. EXPECT( !!"make_expected() is not available (nsel_P0323R > 3)" );
  1295. #endif
  1296. }
  1297. CASE( "make_expected_from_exception(): create expected from given exception" "[.deprecated]" )
  1298. {
  1299. #if nsel_P0323R <= 3
  1300. EXPECT( !!"Implement" );
  1301. #else
  1302. EXPECT( !!"make_expected() is not available (nsel_P0323R > 3)" );
  1303. #endif
  1304. }
  1305. CASE( "make_expected_from_call(): non-void return type" "[.deprecated]" )
  1306. {
  1307. #if nsel_P0323R <= 3
  1308. expected<int> ei = foo();
  1309. expected<std::unique_ptr<int>> eup = bar();
  1310. auto e2 = make_expected_from_call( foo );
  1311. auto eup2 = make_expected_from_call( bar );
  1312. EXPECT( e2 );
  1313. EXPECT( eup2 );
  1314. #else
  1315. EXPECT( !!"make_expected_from_call() is not available (nsel_P0323R > 3)" );
  1316. #endif
  1317. }
  1318. CASE( "make_expected_from_call(): void return type" "[.deprecated]" )
  1319. {
  1320. #if nsel_P0323R <= 3
  1321. auto ev = make_expected_from_call( vfoo );
  1322. EXPECT( ev );
  1323. #else
  1324. EXPECT( !!"make_expected_from_call() is not available (nsel_P0323R > 3)" );
  1325. #endif
  1326. }
  1327. CASE( "tweak header: reads tweak header if supported " "[tweak]" )
  1328. {
  1329. #if expected_HAVE_TWEAK_HEADER
  1330. EXPECT( EXPECTED_TWEAK_VALUE == 42 );
  1331. #else
  1332. EXPECT( !!"Tweak header is not available (expected_HAVE_TWEAK_HEADER: 0)." );
  1333. #endif
  1334. }
  1335. // -----------------------------------------------------------------------
  1336. // expected: issues
  1337. // issue #15, https://github.com/martinmoene/expected-dark/issues/15
  1338. CASE( "issue-15d" )
  1339. {
  1340. nonstd::expected< int, std::error_code > e = 12;
  1341. (void)e.value();
  1342. }
  1343. // issue #15, https://github.com/martinmoene/expected-lite/issues/15
  1344. CASE( "issue-15" )
  1345. {
  1346. (void) nonstd::expected< int, int >( 12).value();
  1347. }
  1348. // issue #29, https://github.com/martinmoene/expected-lite/issues/29
  1349. // issue #32, https://github.com/martinmoene/expected-lite/issues/32
  1350. namespace issue_32 {
  1351. enum class Error
  1352. {
  1353. Bad
  1354. };
  1355. class MyNonMoveableObject
  1356. {
  1357. public:
  1358. MyNonMoveableObject() = default;
  1359. MyNonMoveableObject( MyNonMoveableObject const & ) = default;
  1360. MyNonMoveableObject( MyNonMoveableObject && ) = delete;
  1361. MyNonMoveableObject& operator=( MyNonMoveableObject const &) = default;
  1362. MyNonMoveableObject& operator=( MyNonMoveableObject &&) = delete;
  1363. ~MyNonMoveableObject() = default;
  1364. };
  1365. nonstd::expected< MyNonMoveableObject, Error > create_copyable()
  1366. {
  1367. return MyNonMoveableObject{};
  1368. }
  1369. class MyNonCopyableObject
  1370. {
  1371. public:
  1372. MyNonCopyableObject() = default;
  1373. MyNonCopyableObject( MyNonCopyableObject const & ) = delete;
  1374. MyNonCopyableObject( MyNonCopyableObject && ) = default;
  1375. MyNonCopyableObject& operator=( MyNonCopyableObject const & ) = delete;
  1376. MyNonCopyableObject& operator=( MyNonCopyableObject && ) = default;
  1377. ~MyNonCopyableObject() = default;
  1378. };
  1379. nonstd::expected< MyNonCopyableObject, Error > create_moveable()
  1380. {
  1381. return MyNonCopyableObject{};
  1382. }
  1383. } // namespace issue_32
  1384. CASE( "pr-41" )
  1385. {
  1386. expected<int, int> a{7};
  1387. const expected<int, int> ca{7};
  1388. expected<int, int> b{unexpect, 7};
  1389. const expected<int, int> cb{unexpect, 7};
  1390. expected<void, int> c{unexpect, 7};
  1391. const expected<void, int> cc{unexpect, 7};
  1392. EXPECT( *std::move(a) == 7 );
  1393. EXPECT( *std::move(ca) == 7 );
  1394. EXPECT( std::move(b).error() == 7 );
  1395. EXPECT( std::move(cb).error() == 7 );
  1396. EXPECT( std::move(c).error() == 7 );
  1397. EXPECT( std::move(cc).error() == 7 );
  1398. }
  1399. // -----------------------------------------------------------------------
  1400. // using as optional
  1401. #if 1
  1402. /// disengaged optional state tag
  1403. struct nullopt_t{};
  1404. const nullopt_t nullopt{};
  1405. /// optional expressed in expected
  1406. template< typename T >
  1407. using optional = expected<T, nullopt_t>;
  1408. #endif