stream_descriptor.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // stream_descriptor.cpp
  3. // ~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // Disable autolinking for unit tests.
  11. #if !defined(BOOST_ALL_NO_LIB)
  12. #define BOOST_ALL_NO_LIB 1
  13. #endif // !defined(BOOST_ALL_NO_LIB)
  14. // Test that header file is self-contained.
  15. #include <boost/asio/posix/stream_descriptor.hpp>
  16. #include <boost/asio/io_context.hpp>
  17. #include "../archetypes/async_result.hpp"
  18. #include "../unit_test.hpp"
  19. //------------------------------------------------------------------------------
  20. // posix_stream_descriptor_compile test
  21. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  22. // The following test checks that all public member functions on the class
  23. // posix::stream_descriptor compile and link correctly. Runtime failures are
  24. // ignored.
  25. namespace posix_stream_descriptor_compile {
  26. void wait_handler(const boost::system::error_code&)
  27. {
  28. }
  29. void write_some_handler(const boost::system::error_code&, std::size_t)
  30. {
  31. }
  32. void read_some_handler(const boost::system::error_code&, std::size_t)
  33. {
  34. }
  35. void test()
  36. {
  37. #if defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
  38. using namespace boost::asio;
  39. namespace posix = boost::asio::posix;
  40. try
  41. {
  42. io_context ioc;
  43. const io_context::executor_type ioc_ex = ioc.get_executor();
  44. char mutable_char_buffer[128] = "";
  45. const char const_char_buffer[128] = "";
  46. posix::descriptor_base::bytes_readable io_control_command;
  47. archetypes::lazy_handler lazy;
  48. boost::system::error_code ec;
  49. // basic_stream_descriptor constructors.
  50. posix::stream_descriptor descriptor1(ioc);
  51. posix::stream_descriptor descriptor2(ioc_ex);
  52. int native_descriptor1 = -1;
  53. posix::stream_descriptor descriptor3(ioc, native_descriptor1);
  54. posix::stream_descriptor descriptor4(ioc_ex, native_descriptor1);
  55. #if defined(BOOST_ASIO_HAS_MOVE)
  56. posix::stream_descriptor descriptor5(std::move(descriptor2));
  57. #endif // defined(BOOST_ASIO_HAS_MOVE)
  58. // basic_stream_descriptor operators.
  59. #if defined(BOOST_ASIO_HAS_MOVE)
  60. descriptor1 = posix::stream_descriptor(ioc);
  61. descriptor1 = std::move(descriptor2);
  62. #endif // defined(BOOST_ASIO_HAS_MOVE)
  63. // basic_io_object functions.
  64. posix::stream_descriptor::executor_type ex = descriptor1.get_executor();
  65. (void)ex;
  66. // basic_descriptor functions.
  67. posix::stream_descriptor::lowest_layer_type& lowest_layer
  68. = descriptor1.lowest_layer();
  69. (void)lowest_layer;
  70. const posix::stream_descriptor& descriptor6 = descriptor1;
  71. const posix::stream_descriptor::lowest_layer_type& lowest_layer2
  72. = descriptor6.lowest_layer();
  73. (void)lowest_layer2;
  74. int native_descriptor2 = -1;
  75. descriptor1.assign(native_descriptor2);
  76. bool is_open = descriptor1.is_open();
  77. (void)is_open;
  78. descriptor1.close();
  79. descriptor1.close(ec);
  80. posix::stream_descriptor::native_handle_type native_descriptor3
  81. = descriptor1.native_handle();
  82. (void)native_descriptor3;
  83. posix::stream_descriptor::native_handle_type native_descriptor4
  84. = descriptor1.release();
  85. (void)native_descriptor4;
  86. descriptor1.cancel();
  87. descriptor1.cancel(ec);
  88. descriptor1.io_control(io_control_command);
  89. descriptor1.io_control(io_control_command, ec);
  90. bool non_blocking1 = descriptor1.non_blocking();
  91. (void)non_blocking1;
  92. descriptor1.non_blocking(true);
  93. descriptor1.non_blocking(false, ec);
  94. bool non_blocking2 = descriptor1.native_non_blocking();
  95. (void)non_blocking2;
  96. descriptor1.native_non_blocking(true);
  97. descriptor1.native_non_blocking(false, ec);
  98. descriptor1.wait(posix::descriptor_base::wait_read);
  99. descriptor1.wait(posix::descriptor_base::wait_write, ec);
  100. descriptor1.async_wait(posix::descriptor_base::wait_read, &wait_handler);
  101. int i1 = descriptor1.async_wait(posix::descriptor_base::wait_write, lazy);
  102. (void)i1;
  103. // basic_stream_descriptor functions.
  104. descriptor1.write_some(buffer(mutable_char_buffer));
  105. descriptor1.write_some(buffer(const_char_buffer));
  106. descriptor1.write_some(null_buffers());
  107. descriptor1.write_some(buffer(mutable_char_buffer), ec);
  108. descriptor1.write_some(buffer(const_char_buffer), ec);
  109. descriptor1.write_some(null_buffers(), ec);
  110. descriptor1.async_write_some(buffer(mutable_char_buffer),
  111. write_some_handler);
  112. descriptor1.async_write_some(buffer(const_char_buffer),
  113. write_some_handler);
  114. descriptor1.async_write_some(null_buffers(),
  115. write_some_handler);
  116. int i2 = descriptor1.async_write_some(buffer(mutable_char_buffer), lazy);
  117. (void)i2;
  118. int i3 = descriptor1.async_write_some(buffer(const_char_buffer), lazy);
  119. (void)i3;
  120. int i4 = descriptor1.async_write_some(null_buffers(), lazy);
  121. (void)i4;
  122. descriptor1.read_some(buffer(mutable_char_buffer));
  123. descriptor1.read_some(buffer(mutable_char_buffer), ec);
  124. descriptor1.read_some(null_buffers(), ec);
  125. descriptor1.async_read_some(buffer(mutable_char_buffer), read_some_handler);
  126. descriptor1.async_read_some(null_buffers(), read_some_handler);
  127. int i5 = descriptor1.async_read_some(buffer(mutable_char_buffer), lazy);
  128. (void)i5;
  129. int i6 = descriptor1.async_read_some(null_buffers(), lazy);
  130. (void)i6;
  131. }
  132. catch (std::exception&)
  133. {
  134. }
  135. #endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
  136. }
  137. } // namespace posix_stream_descriptor_compile
  138. //------------------------------------------------------------------------------
  139. BOOST_ASIO_TEST_SUITE
  140. (
  141. "posix/stream_descriptor",
  142. BOOST_ASIO_TEST_CASE(posix_stream_descriptor_compile::test)
  143. )