execute_test.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /*
  2. * Distributed under the Boost Software License, Version 1.0.(See accompanying
  3. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
  4. *
  5. * See http://www.boost.org/libs/iostreams for documentation.
  6. *
  7. * Tests the function templates boost::iostreams::detail::execute_all and
  8. * boost::iostreams::detail::execute_foreach
  9. *
  10. * File: libs/iostreams/test/execute_test.cpp
  11. * Date: Thu Dec 06 13:21:54 MST 2007
  12. * Copyright: 2007-2008 CodeRage, LLC
  13. * Author: Jonathan Turkanis
  14. * Contact: turkanis at coderage dot com
  15. */
  16. #include <boost/iostreams/detail/execute.hpp>
  17. #include <boost/test/test_tools.hpp>
  18. #include <boost/test/unit_test.hpp>
  19. using namespace std;
  20. using namespace boost;
  21. using namespace boost::iostreams;
  22. using namespace boost::iostreams::detail;
  23. using boost::unit_test::test_suite;
  24. // Function object that sets a boolean flag and returns a value
  25. // specified at construction
  26. template<typename Result>
  27. class operation {
  28. public:
  29. typedef Result result_type;
  30. explicit operation(Result r, bool& executed)
  31. : r_(r), executed_(executed)
  32. { }
  33. Result operator()() const
  34. {
  35. executed_ = true;
  36. return r_;
  37. }
  38. private:
  39. operation& operator=(const operation&);
  40. Result r_;
  41. bool& executed_;
  42. };
  43. // Specialization for void return
  44. template<>
  45. class operation<void> {
  46. public:
  47. typedef void result_type;
  48. explicit operation(bool& executed) : executed_(executed) { }
  49. void operator()() const { executed_ = true; }
  50. private:
  51. operation& operator=(const operation&);
  52. bool& executed_;
  53. };
  54. // Simple exception class with error code built in to type
  55. template<int Code>
  56. struct error { };
  57. // Function object that sets a boolean flag and throws an exception
  58. template<int Code>
  59. class thrower {
  60. public:
  61. typedef void result_type;
  62. explicit thrower(bool& executed) : executed_(executed) { }
  63. void operator()() const
  64. {
  65. executed_ = true;
  66. throw error<Code>();
  67. }
  68. private:
  69. thrower& operator=(const thrower&);
  70. bool& executed_;
  71. };
  72. // Function object for use by foreach_test
  73. class foreach_func {
  74. public:
  75. typedef void result_type;
  76. explicit foreach_func(int& count) : count_(count) { }
  77. void operator()(int x) const
  78. {
  79. ++count_;
  80. switch (x) {
  81. case 0: throw error<0>();
  82. case 1: throw error<1>();
  83. case 2: throw error<2>();
  84. case 3: throw error<3>();
  85. case 4: throw error<4>();
  86. case 5: throw error<5>();
  87. case 6: throw error<6>();
  88. case 7: throw error<7>();
  89. case 8: throw error<8>();
  90. case 9: throw error<9>();
  91. default:
  92. break;
  93. }
  94. }
  95. private:
  96. foreach_func& operator=(const foreach_func&);
  97. int& count_; // Number of times operator() has been called
  98. };
  99. void success_test()
  100. {
  101. // Test returning an int
  102. {
  103. bool executed = false;
  104. BOOST_CHECK(execute_all(operation<int>(9, executed)) == 9);
  105. BOOST_CHECK(executed);
  106. }
  107. // Test returning void
  108. {
  109. bool executed = false;
  110. execute_all(operation<void>(executed));
  111. BOOST_CHECK(executed);
  112. }
  113. // Test returning an int with one cleanup operation
  114. {
  115. bool executed = false, cleaned_up = false;
  116. BOOST_CHECK(
  117. execute_all(
  118. operation<int>(9, executed),
  119. operation<void>(cleaned_up)
  120. ) == 9
  121. );
  122. BOOST_CHECK(executed && cleaned_up);
  123. }
  124. // Test returning void with one cleanup operation
  125. {
  126. bool executed = false, cleaned_up = false;
  127. execute_all(
  128. operation<void>(executed),
  129. operation<void>(cleaned_up)
  130. );
  131. BOOST_CHECK(executed && cleaned_up);
  132. }
  133. // Test returning an int with two cleanup operations
  134. {
  135. bool executed = false, cleaned_up1 = false, cleaned_up2 = false;
  136. BOOST_CHECK(
  137. execute_all(
  138. operation<int>(9, executed),
  139. operation<void>(cleaned_up1),
  140. operation<void>(cleaned_up2)
  141. ) == 9
  142. );
  143. BOOST_CHECK(executed && cleaned_up1 && cleaned_up2);
  144. }
  145. // Test returning void with two cleanup operations
  146. {
  147. bool executed = false, cleaned_up1 = false, cleaned_up2 = false;
  148. execute_all(
  149. operation<void>(executed),
  150. operation<void>(cleaned_up1),
  151. operation<void>(cleaned_up2)
  152. );
  153. BOOST_CHECK(executed && cleaned_up1 && cleaned_up2);
  154. }
  155. // Test returning an int with three cleanup operations
  156. {
  157. bool executed = false, cleaned_up1 = false,
  158. cleaned_up2 = false, cleaned_up3 = false;
  159. BOOST_CHECK(
  160. execute_all(
  161. operation<int>(9, executed),
  162. operation<void>(cleaned_up1),
  163. operation<void>(cleaned_up2),
  164. operation<void>(cleaned_up3)
  165. ) == 9
  166. );
  167. BOOST_CHECK(executed && cleaned_up1 && cleaned_up2 && cleaned_up3);
  168. }
  169. // Test returning void with three cleanup operations
  170. {
  171. bool executed = false, cleaned_up1 = false,
  172. cleaned_up2 = false, cleaned_up3 = false;
  173. execute_all(
  174. operation<void>(executed),
  175. operation<void>(cleaned_up1),
  176. operation<void>(cleaned_up2),
  177. operation<void>(cleaned_up3)
  178. );
  179. BOOST_CHECK(executed && cleaned_up1 && cleaned_up2 && cleaned_up3);
  180. }
  181. }
  182. void operation_throws_test()
  183. {
  184. // Test primary operation throwing with no cleanup operations
  185. {
  186. bool executed = false;
  187. BOOST_CHECK_THROW(
  188. execute_all(thrower<0>(executed)),
  189. error<0>
  190. );
  191. BOOST_CHECK(executed);
  192. }
  193. // Test primary operation throwing with one cleanup operation
  194. {
  195. bool executed = false, cleaned_up = false;
  196. BOOST_CHECK_THROW(
  197. execute_all(
  198. thrower<0>(executed),
  199. operation<void>(cleaned_up)
  200. ),
  201. error<0>
  202. );
  203. BOOST_CHECK(executed && cleaned_up);
  204. }
  205. // Test primary operation throwing with two cleanup operations
  206. {
  207. bool executed = false, cleaned_up1 = false, cleaned_up2 = false;
  208. BOOST_CHECK_THROW(
  209. execute_all(
  210. thrower<0>(executed),
  211. operation<void>(cleaned_up1),
  212. operation<void>(cleaned_up2)
  213. ),
  214. error<0>
  215. );
  216. BOOST_CHECK(executed && cleaned_up1 && cleaned_up2);
  217. }
  218. // Test primary operation throwing with three cleanup operations
  219. {
  220. bool executed = false, cleaned_up1 = false,
  221. cleaned_up2 = false, cleaned_up3 = false;
  222. BOOST_CHECK_THROW(
  223. execute_all(
  224. thrower<0>(executed),
  225. operation<void>(cleaned_up1),
  226. operation<void>(cleaned_up2),
  227. operation<void>(cleaned_up3)
  228. ),
  229. error<0>
  230. );
  231. BOOST_CHECK(executed && cleaned_up1 && cleaned_up2 && cleaned_up3);
  232. }
  233. }
  234. void cleanup_throws_test()
  235. {
  236. // Test single cleanup operation that throws
  237. {
  238. bool executed = false, cleaned_up = false;
  239. BOOST_CHECK_THROW(
  240. execute_all(
  241. operation<void>(executed),
  242. thrower<1>(cleaned_up)
  243. ),
  244. error<1>
  245. );
  246. BOOST_CHECK(executed && cleaned_up);
  247. }
  248. // Test fist of two cleanup operations throwing
  249. {
  250. bool executed = false, cleaned_up1 = false, cleaned_up2 = false;
  251. BOOST_CHECK_THROW(
  252. execute_all(
  253. operation<void>(executed),
  254. thrower<1>(cleaned_up1),
  255. operation<void>(cleaned_up2)
  256. ),
  257. error<1>
  258. );
  259. BOOST_CHECK(executed && cleaned_up1 && cleaned_up2);
  260. }
  261. // Test second of two cleanup operations throwing
  262. {
  263. bool executed = false, cleaned_up1 = false, cleaned_up2 = false;
  264. BOOST_CHECK_THROW(
  265. execute_all(
  266. operation<void>(executed),
  267. operation<void>(cleaned_up1),
  268. thrower<2>(cleaned_up2)
  269. ),
  270. error<2>
  271. );
  272. BOOST_CHECK(executed && cleaned_up1 && cleaned_up2);
  273. }
  274. // Test first of three cleanup operations throwing
  275. {
  276. bool executed = false, cleaned_up1 = false,
  277. cleaned_up2 = false, cleaned_up3 = false;
  278. BOOST_CHECK_THROW(
  279. execute_all(
  280. operation<void>(executed),
  281. thrower<1>(cleaned_up1),
  282. operation<void>(cleaned_up2),
  283. operation<void>(cleaned_up3)
  284. ),
  285. error<1>
  286. );
  287. BOOST_CHECK(executed && cleaned_up1 && cleaned_up2 && cleaned_up3);
  288. }
  289. // Test second of three cleanup operations throwing
  290. {
  291. bool executed = false, cleaned_up1 = false,
  292. cleaned_up2 = false, cleaned_up3 = false;
  293. BOOST_CHECK_THROW(
  294. execute_all(
  295. operation<void>(executed),
  296. operation<void>(cleaned_up1),
  297. thrower<2>(cleaned_up2),
  298. operation<void>(cleaned_up3)
  299. ),
  300. error<2>
  301. );
  302. BOOST_CHECK(executed && cleaned_up1 && cleaned_up2 && cleaned_up3);
  303. }
  304. // Test third of three cleanup operations throwing
  305. {
  306. bool executed = false, cleaned_up1 = false,
  307. cleaned_up2 = false, cleaned_up3 = false;
  308. BOOST_CHECK_THROW(
  309. execute_all(
  310. operation<void>(executed),
  311. operation<void>(cleaned_up1),
  312. operation<void>(cleaned_up2),
  313. thrower<3>(cleaned_up3)
  314. ),
  315. error<3>
  316. );
  317. BOOST_CHECK(executed && cleaned_up1 && cleaned_up2 && cleaned_up3);
  318. }
  319. }
  320. void multiple_exceptions_test()
  321. {
  322. // Test primary operation and cleanup operation throwing
  323. {
  324. bool executed = false, cleaned_up = false;
  325. BOOST_CHECK_THROW(
  326. execute_all(
  327. thrower<0>(executed),
  328. thrower<1>(cleaned_up)
  329. ),
  330. error<0>
  331. );
  332. BOOST_CHECK(executed && cleaned_up);
  333. }
  334. // Test primary operation and first of two cleanup operations throwing
  335. {
  336. bool executed = false, cleaned_up1 = false, cleaned_up2 = false;
  337. BOOST_CHECK_THROW(
  338. execute_all(
  339. thrower<0>(executed),
  340. thrower<1>(cleaned_up1),
  341. operation<void>(cleaned_up2)
  342. ),
  343. error<0>
  344. );
  345. BOOST_CHECK(executed && cleaned_up1 && cleaned_up2);
  346. }
  347. // Test primary operation and second of two cleanup operations throwing
  348. {
  349. bool executed = false, cleaned_up1 = false, cleaned_up2 = false;
  350. BOOST_CHECK_THROW(
  351. execute_all(
  352. thrower<0>(executed),
  353. operation<void>(cleaned_up1),
  354. thrower<2>(cleaned_up2)
  355. ),
  356. error<0>
  357. );
  358. BOOST_CHECK(executed && cleaned_up1 && cleaned_up2);
  359. }
  360. // Test two cleanup operations throwing
  361. {
  362. bool executed = false, cleaned_up1 = false, cleaned_up2 = false;
  363. BOOST_CHECK_THROW(
  364. execute_all(
  365. operation<void>(executed),
  366. thrower<1>(cleaned_up1),
  367. thrower<2>(cleaned_up2)
  368. ),
  369. error<1>
  370. );
  371. BOOST_CHECK(executed && cleaned_up1 && cleaned_up2);
  372. }
  373. // Test primary operation and first of three cleanup operations throwing
  374. {
  375. bool executed = false, cleaned_up1 = false,
  376. cleaned_up2 = false, cleaned_up3 = false;
  377. BOOST_CHECK_THROW(
  378. execute_all(
  379. thrower<0>(executed),
  380. thrower<1>(cleaned_up1),
  381. operation<void>(cleaned_up2),
  382. operation<void>(cleaned_up3)
  383. ),
  384. error<0>
  385. );
  386. BOOST_CHECK(executed && cleaned_up1 && cleaned_up2 && cleaned_up3);
  387. }
  388. // Test primary operation and second of three cleanup operations throwing
  389. {
  390. bool executed = false, cleaned_up1 = false,
  391. cleaned_up2 = false, cleaned_up3 = false;
  392. BOOST_CHECK_THROW(
  393. execute_all(
  394. thrower<0>(executed),
  395. operation<void>(cleaned_up1),
  396. thrower<2>(cleaned_up2),
  397. operation<void>(cleaned_up3)
  398. ),
  399. error<0>
  400. );
  401. BOOST_CHECK(executed && cleaned_up1 && cleaned_up2 && cleaned_up3);
  402. }
  403. // Test primary operation and third of three cleanup operations throwing
  404. {
  405. bool executed = false, cleaned_up1 = false,
  406. cleaned_up2 = false, cleaned_up3 = false;
  407. BOOST_CHECK_THROW(
  408. execute_all(
  409. thrower<0>(executed),
  410. operation<void>(cleaned_up1),
  411. operation<void>(cleaned_up2),
  412. thrower<3>(cleaned_up3)
  413. ),
  414. error<0>
  415. );
  416. BOOST_CHECK(executed && cleaned_up1 && cleaned_up2 && cleaned_up3);
  417. }
  418. // Test first and second of three cleanup operations throwing
  419. {
  420. bool executed = false, cleaned_up1 = false,
  421. cleaned_up2 = false, cleaned_up3 = false;
  422. BOOST_CHECK_THROW(
  423. execute_all(
  424. operation<void>(executed),
  425. thrower<1>(cleaned_up1),
  426. thrower<2>(cleaned_up2),
  427. operation<void>(cleaned_up3)
  428. ),
  429. error<1>
  430. );
  431. BOOST_CHECK(executed && cleaned_up1 && cleaned_up2 && cleaned_up3);
  432. }
  433. // Test first and third of three cleanup operations throwing
  434. {
  435. bool executed = false, cleaned_up1 = false,
  436. cleaned_up2 = false, cleaned_up3 = false;
  437. BOOST_CHECK_THROW(
  438. execute_all(
  439. operation<void>(executed),
  440. thrower<1>(cleaned_up1),
  441. operation<void>(cleaned_up2),
  442. thrower<3>(cleaned_up3)
  443. ),
  444. error<1>
  445. );
  446. BOOST_CHECK(executed && cleaned_up1 && cleaned_up2 && cleaned_up3);
  447. }
  448. // Test second and third of three cleanup operations throwing
  449. {
  450. bool executed = false, cleaned_up1 = false,
  451. cleaned_up2 = false, cleaned_up3 = false;
  452. BOOST_CHECK_THROW(
  453. execute_all(
  454. operation<void>(executed),
  455. operation<void>(cleaned_up1),
  456. thrower<2>(cleaned_up2),
  457. thrower<3>(cleaned_up3)
  458. ),
  459. error<2>
  460. );
  461. BOOST_CHECK(executed && cleaned_up1 && cleaned_up2 && cleaned_up3);
  462. }
  463. // Test three cleanup operations throwing
  464. {
  465. bool executed = false, cleaned_up1 = false,
  466. cleaned_up2 = false, cleaned_up3 = false;
  467. BOOST_CHECK_THROW(
  468. execute_all(
  469. operation<void>(executed),
  470. thrower<1>(cleaned_up1),
  471. thrower<2>(cleaned_up2),
  472. thrower<3>(cleaned_up3)
  473. ),
  474. error<1>
  475. );
  476. BOOST_CHECK(executed && cleaned_up1 && cleaned_up2 && cleaned_up3);
  477. }
  478. }
  479. #define ARRAY_SIZE(ar) (sizeof(ar) / sizeof(ar[0]))
  480. void foreach_test()
  481. {
  482. // Test case where neither of two operations throws
  483. {
  484. int count = 0;
  485. int seq[] = {-1, -1};
  486. BOOST_CHECK_NO_THROW(
  487. execute_foreach(seq, seq + ARRAY_SIZE(seq), foreach_func(count))
  488. );
  489. BOOST_CHECK(count == ARRAY_SIZE(seq));
  490. }
  491. // Test case where first of two operations throws
  492. {
  493. int count = 0;
  494. int seq[] = {0, -1};
  495. BOOST_CHECK_THROW(
  496. execute_foreach(seq, seq + ARRAY_SIZE(seq), foreach_func(count)),
  497. error<0>
  498. );
  499. BOOST_CHECK(count == ARRAY_SIZE(seq));
  500. }
  501. // Test case where second of two operations throws
  502. {
  503. int count = 0;
  504. int seq[] = {-1, 1};
  505. BOOST_CHECK_THROW(
  506. execute_foreach(seq, seq + ARRAY_SIZE(seq), foreach_func(count)),
  507. error<1>
  508. );
  509. BOOST_CHECK(count == ARRAY_SIZE(seq));
  510. }
  511. // Test case where both of two operations throw
  512. {
  513. int count = 0;
  514. int seq[] = {0, 1};
  515. BOOST_CHECK_THROW(
  516. execute_foreach(seq, seq + ARRAY_SIZE(seq), foreach_func(count)),
  517. error<0>
  518. );
  519. BOOST_CHECK(count == ARRAY_SIZE(seq));
  520. }
  521. // Test case where none of three operations throws
  522. {
  523. int count = 0;
  524. int seq[] = {-1, -1, -1};
  525. BOOST_CHECK_NO_THROW(
  526. execute_foreach(seq, seq + ARRAY_SIZE(seq), foreach_func(count))
  527. );
  528. BOOST_CHECK(count == ARRAY_SIZE(seq));
  529. }
  530. // Test case where first of three operations throw
  531. {
  532. int count = 0;
  533. int seq[] = {0, -1, -1};
  534. BOOST_CHECK_THROW(
  535. execute_foreach(seq, seq + ARRAY_SIZE(seq), foreach_func(count)),
  536. error<0>
  537. );
  538. BOOST_CHECK(count == ARRAY_SIZE(seq));
  539. }
  540. // Test case where second of three operations throw
  541. {
  542. int count = 0;
  543. int seq[] = {-1, 1, -1};
  544. BOOST_CHECK_THROW(
  545. execute_foreach(seq, seq + ARRAY_SIZE(seq), foreach_func(count)),
  546. error<1>
  547. );
  548. BOOST_CHECK(count == ARRAY_SIZE(seq));
  549. }
  550. // Test case where third of three operations throw
  551. {
  552. int count = 0;
  553. int seq[] = {-1, -1, 2};
  554. BOOST_CHECK_THROW(
  555. execute_foreach(seq, seq + ARRAY_SIZE(seq), foreach_func(count)),
  556. error<2>
  557. );
  558. BOOST_CHECK(count == ARRAY_SIZE(seq));
  559. }
  560. // Test case where first and second of three operations throw
  561. {
  562. int count = 0;
  563. int seq[] = {0, 1, -1};
  564. BOOST_CHECK_THROW(
  565. execute_foreach(seq, seq + ARRAY_SIZE(seq), foreach_func(count)),
  566. error<0>
  567. );
  568. BOOST_CHECK(count == ARRAY_SIZE(seq));
  569. }
  570. // Test case where first and third of three operations throw
  571. {
  572. int count = 0;
  573. int seq[] = {0, -1, 2};
  574. BOOST_CHECK_THROW(
  575. execute_foreach(seq, seq + ARRAY_SIZE(seq), foreach_func(count)),
  576. error<0>
  577. );
  578. BOOST_CHECK(count == ARRAY_SIZE(seq));
  579. }
  580. // Test case where second and third of three operations throw
  581. {
  582. int count = 0;
  583. int seq[] = {-1, 1, 2};
  584. BOOST_CHECK_THROW(
  585. execute_foreach(seq, seq + ARRAY_SIZE(seq), foreach_func(count)),
  586. error<1>
  587. );
  588. BOOST_CHECK(count == ARRAY_SIZE(seq));
  589. }
  590. // Test case where three of three operations throw
  591. {
  592. int count = 0;
  593. int seq[] = {0, 1, 2};
  594. BOOST_CHECK_THROW(
  595. execute_foreach(seq, seq + ARRAY_SIZE(seq), foreach_func(count)),
  596. error<0>
  597. );
  598. BOOST_CHECK(count == ARRAY_SIZE(seq));
  599. }
  600. }
  601. test_suite* init_unit_test_suite(int, char* [])
  602. {
  603. test_suite* test = BOOST_TEST_SUITE("execute test");
  604. test->add(BOOST_TEST_CASE(&success_test));
  605. test->add(BOOST_TEST_CASE(&operation_throws_test));
  606. test->add(BOOST_TEST_CASE(&cleanup_throws_test));
  607. test->add(BOOST_TEST_CASE(&multiple_exceptions_test));
  608. test->add(BOOST_TEST_CASE(&foreach_test));
  609. return test;
  610. }