deprecated_file_descriptor_test.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // (C) Copyright 2010 Daniel James
  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. #include <boost/iostreams/device/file_descriptor.hpp>
  5. #include <boost/iostreams/stream.hpp>
  6. #include <boost/test/unit_test.hpp>
  7. #include "detail/temp_file.hpp"
  8. #include "detail/verification.hpp"
  9. #include "detail/file_handle.hpp"
  10. // Test file_descriptor with the depreacted constructors.
  11. namespace boost_ios = boost::iostreams;
  12. namespace ios_test = boost::iostreams::test;
  13. template <class FileDescriptor>
  14. void file_handle_test_impl(FileDescriptor*)
  15. {
  16. ios_test::test_file test1;
  17. ios_test::test_file test2;
  18. //--------------Deprecated file descriptor constructor--------------------//
  19. {
  20. boost_ios::detail::file_handle handle = ios_test::open_file_handle(test1.name());
  21. {
  22. FileDescriptor device1(handle);
  23. BOOST_CHECK(device1.handle() == handle);
  24. }
  25. BOOST_CHECK_HANDLE_OPEN(handle);
  26. ios_test::close_file_handle(handle);
  27. }
  28. {
  29. boost_ios::detail::file_handle handle = ios_test::open_file_handle(test1.name());
  30. {
  31. FileDescriptor device1(handle, false);
  32. BOOST_CHECK(device1.handle() == handle);
  33. }
  34. BOOST_CHECK_HANDLE_OPEN(handle);
  35. ios_test::close_file_handle(handle);
  36. }
  37. {
  38. boost_ios::detail::file_handle handle = ios_test::open_file_handle(test1.name());
  39. {
  40. FileDescriptor device1(handle, true);
  41. BOOST_CHECK(device1.handle() == handle);
  42. }
  43. BOOST_CHECK_HANDLE_CLOSED(handle);
  44. }
  45. {
  46. boost_ios::detail::file_handle handle = ios_test::open_file_handle(test1.name());
  47. FileDescriptor device1(handle);
  48. BOOST_CHECK(device1.handle() == handle);
  49. device1.close();
  50. BOOST_CHECK(!device1.is_open());
  51. BOOST_CHECK_HANDLE_CLOSED(handle);
  52. }
  53. {
  54. boost_ios::detail::file_handle handle = ios_test::open_file_handle(test1.name());
  55. FileDescriptor device1(handle, false);
  56. BOOST_CHECK(device1.handle() == handle);
  57. device1.close();
  58. BOOST_CHECK(!device1.is_open());
  59. BOOST_CHECK_HANDLE_CLOSED(handle);
  60. }
  61. {
  62. boost_ios::detail::file_handle handle = ios_test::open_file_handle(test1.name());
  63. FileDescriptor device1(handle, true);
  64. BOOST_CHECK(device1.handle() == handle);
  65. device1.close();
  66. BOOST_CHECK(!device1.is_open());
  67. BOOST_CHECK_HANDLE_CLOSED(handle);
  68. }
  69. //--------------Deprecated file descriptor open---------------------------//
  70. {
  71. boost_ios::detail::file_handle handle = ios_test::open_file_handle(test1.name());
  72. {
  73. FileDescriptor device1;
  74. device1.open(handle);
  75. BOOST_CHECK(device1.handle() == handle);
  76. }
  77. BOOST_CHECK_HANDLE_OPEN(handle);
  78. ios_test::close_file_handle(handle);
  79. }
  80. {
  81. boost_ios::detail::file_handle handle = ios_test::open_file_handle(test1.name());
  82. {
  83. FileDescriptor device1;
  84. device1.open(handle, false);
  85. BOOST_CHECK(device1.handle() == handle);
  86. }
  87. BOOST_CHECK_HANDLE_OPEN(handle);
  88. ios_test::close_file_handle(handle);
  89. }
  90. {
  91. boost_ios::detail::file_handle handle = ios_test::open_file_handle(test1.name());
  92. {
  93. FileDescriptor device1;
  94. device1.open(handle, true);
  95. BOOST_CHECK(device1.handle() == handle);
  96. }
  97. BOOST_CHECK_HANDLE_CLOSED(handle);
  98. }
  99. {
  100. boost_ios::detail::file_handle handle = ios_test::open_file_handle(test1.name());
  101. FileDescriptor device1;
  102. device1.open(handle);
  103. BOOST_CHECK(device1.handle() == handle);
  104. device1.close();
  105. BOOST_CHECK(!device1.is_open());
  106. BOOST_CHECK_HANDLE_CLOSED(handle);
  107. }
  108. {
  109. boost_ios::detail::file_handle handle = ios_test::open_file_handle(test1.name());
  110. FileDescriptor device1;
  111. device1.open(handle, false);
  112. BOOST_CHECK(device1.handle() == handle);
  113. device1.close();
  114. BOOST_CHECK(!device1.is_open());
  115. BOOST_CHECK_HANDLE_CLOSED(handle);
  116. }
  117. {
  118. boost_ios::detail::file_handle handle = ios_test::open_file_handle(test1.name());
  119. FileDescriptor device1;
  120. device1.open(handle, true);
  121. BOOST_CHECK(device1.handle() == handle);
  122. device1.close();
  123. BOOST_CHECK(!device1.is_open());
  124. BOOST_CHECK_HANDLE_CLOSED(handle);
  125. }
  126. //--------------Deprecated open with existing descriptor------------------//
  127. {
  128. boost_ios::detail::file_handle handle1 = ios_test::open_file_handle(test1.name());
  129. boost_ios::detail::file_handle handle2 = ios_test::open_file_handle(test1.name());
  130. {
  131. FileDescriptor device1(handle1);
  132. BOOST_CHECK(device1.handle() == handle1);
  133. BOOST_CHECK_HANDLE_OPEN(handle1);
  134. BOOST_CHECK_HANDLE_OPEN(handle2);
  135. device1.open(handle2);
  136. BOOST_CHECK(device1.handle() == handle2);
  137. BOOST_CHECK_HANDLE_OPEN(handle1);
  138. BOOST_CHECK_HANDLE_OPEN(handle2);
  139. device1.close();
  140. BOOST_CHECK_HANDLE_OPEN(handle1);
  141. BOOST_CHECK_HANDLE_CLOSED(handle2);
  142. }
  143. BOOST_CHECK_HANDLE_OPEN(handle1);
  144. BOOST_CHECK_HANDLE_CLOSED(handle2);
  145. ios_test::close_file_handle(handle1);
  146. }
  147. {
  148. boost_ios::detail::file_handle handle1 = ios_test::open_file_handle(test1.name());
  149. boost_ios::detail::file_handle handle2 = ios_test::open_file_handle(test1.name());
  150. {
  151. FileDescriptor device1(handle1, true);
  152. BOOST_CHECK(device1.handle() == handle1);
  153. BOOST_CHECK_HANDLE_OPEN(handle1);
  154. BOOST_CHECK_HANDLE_OPEN(handle2);
  155. device1.open(handle2);
  156. BOOST_CHECK(device1.handle() == handle2);
  157. BOOST_CHECK_HANDLE_CLOSED(handle1);
  158. BOOST_CHECK_HANDLE_OPEN(handle2);
  159. device1.close();
  160. BOOST_CHECK_HANDLE_CLOSED(handle1);
  161. BOOST_CHECK_HANDLE_CLOSED(handle2);
  162. }
  163. BOOST_CHECK_HANDLE_CLOSED(handle1);
  164. BOOST_CHECK_HANDLE_CLOSED(handle2);
  165. }
  166. {
  167. boost_ios::detail::file_handle handle1 = ios_test::open_file_handle(test1.name());
  168. boost_ios::detail::file_handle handle2 = ios_test::open_file_handle(test1.name());
  169. {
  170. FileDescriptor device1(handle1, false);
  171. BOOST_CHECK(device1.handle() == handle1);
  172. BOOST_CHECK_HANDLE_OPEN(handle1);
  173. BOOST_CHECK_HANDLE_OPEN(handle2);
  174. device1.open(handle2, false);
  175. BOOST_CHECK(device1.handle() == handle2);
  176. BOOST_CHECK_HANDLE_OPEN(handle1);
  177. BOOST_CHECK_HANDLE_OPEN(handle2);
  178. }
  179. BOOST_CHECK_HANDLE_OPEN(handle1);
  180. BOOST_CHECK_HANDLE_OPEN(handle2);
  181. ios_test::close_file_handle(handle1);
  182. ios_test::close_file_handle(handle2);
  183. }
  184. {
  185. boost_ios::detail::file_handle handle1 = ios_test::open_file_handle(test1.name());
  186. boost_ios::detail::file_handle handle2 = ios_test::open_file_handle(test1.name());
  187. {
  188. FileDescriptor device1(handle1, true);
  189. BOOST_CHECK(device1.handle() == handle1);
  190. BOOST_CHECK_HANDLE_OPEN(handle1);
  191. BOOST_CHECK_HANDLE_OPEN(handle2);
  192. device1.open(handle2, true);
  193. BOOST_CHECK(device1.handle() == handle2);
  194. BOOST_CHECK_HANDLE_CLOSED(handle1);
  195. BOOST_CHECK_HANDLE_OPEN(handle2);
  196. }
  197. BOOST_CHECK_HANDLE_CLOSED(handle1);
  198. BOOST_CHECK_HANDLE_CLOSED(handle2);
  199. }
  200. }
  201. void deprecated_file_descriptor_test()
  202. {
  203. file_handle_test_impl((boost_ios::file_descriptor*) 0);
  204. file_handle_test_impl((boost_ios::file_descriptor_sink*) 0);
  205. file_handle_test_impl((boost_ios::file_descriptor_source*) 0);
  206. }
  207. boost::unit_test::test_suite* init_unit_test_suite(int, char* [])
  208. {
  209. boost::unit_test::test_suite* test = BOOST_TEST_SUITE("deprecated file_descriptor test");
  210. test->add(BOOST_TEST_CASE(&deprecated_file_descriptor_test));
  211. return test;
  212. }