stream_handle.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // stream_handle.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/windows/stream_handle.hpp>
  16. #include <boost/asio/io_context.hpp>
  17. #include "../archetypes/async_result.hpp"
  18. #include "../unit_test.hpp"
  19. //------------------------------------------------------------------------------
  20. // windows_stream_handle_compile test
  21. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  22. // The following test checks that all public member functions on the class
  23. // windows::stream_handle compile and link correctly. Runtime failures are
  24. // ignored.
  25. namespace windows_stream_handle_compile {
  26. void write_some_handler(const boost::system::error_code&, std::size_t)
  27. {
  28. }
  29. void read_some_handler(const boost::system::error_code&, std::size_t)
  30. {
  31. }
  32. void test()
  33. {
  34. #if defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE)
  35. using namespace boost::asio;
  36. namespace win = boost::asio::windows;
  37. try
  38. {
  39. io_context ioc;
  40. const io_context::executor_type ioc_ex = ioc.get_executor();
  41. char mutable_char_buffer[128] = "";
  42. const char const_char_buffer[128] = "";
  43. archetypes::lazy_handler lazy;
  44. boost::system::error_code ec;
  45. // basic_stream_handle constructors.
  46. win::stream_handle handle1(ioc);
  47. HANDLE native_handle1 = INVALID_HANDLE_VALUE;
  48. #if defined(BOOST_ASIO_MSVC) && (_MSC_VER < 1910)
  49. // Skip this on older MSVC due to mysterious ambiguous overload errors.
  50. #else
  51. win::stream_handle handle2(ioc, native_handle1);
  52. #endif
  53. win::stream_handle handle3(ioc_ex);
  54. HANDLE native_handle2 = INVALID_HANDLE_VALUE;
  55. win::stream_handle handle4(ioc_ex, native_handle2);
  56. #if defined(BOOST_ASIO_HAS_MOVE)
  57. win::stream_handle handle5(std::move(handle4));
  58. #endif // defined(BOOST_ASIO_HAS_MOVE)
  59. // basic_stream_handle operators.
  60. #if defined(BOOST_ASIO_HAS_MOVE)
  61. handle1 = win::stream_handle(ioc);
  62. handle1 = std::move(handle4);
  63. #endif // defined(BOOST_ASIO_HAS_MOVE)
  64. // basic_io_object functions.
  65. windows::stream_handle::executor_type ex = handle1.get_executor();
  66. (void)ex;
  67. // basic_overlapped_handle functions.
  68. win::stream_handle::lowest_layer_type& lowest_layer
  69. = handle1.lowest_layer();
  70. (void)lowest_layer;
  71. const win::stream_handle& handle6 = handle1;
  72. const win::stream_handle::lowest_layer_type& lowest_layer2
  73. = handle6.lowest_layer();
  74. (void)lowest_layer2;
  75. HANDLE native_handle3 = INVALID_HANDLE_VALUE;
  76. handle1.assign(native_handle3);
  77. bool is_open = handle1.is_open();
  78. (void)is_open;
  79. handle1.close();
  80. handle1.close(ec);
  81. win::stream_handle::native_handle_type native_handle4
  82. = handle1.native_handle();
  83. (void)native_handle4;
  84. handle1.cancel();
  85. handle1.cancel(ec);
  86. // basic_stream_handle functions.
  87. handle1.write_some(buffer(mutable_char_buffer));
  88. handle1.write_some(buffer(const_char_buffer));
  89. handle1.write_some(buffer(mutable_char_buffer), ec);
  90. handle1.write_some(buffer(const_char_buffer), ec);
  91. handle1.async_write_some(buffer(mutable_char_buffer), &write_some_handler);
  92. handle1.async_write_some(buffer(const_char_buffer), &write_some_handler);
  93. int i1 = handle1.async_write_some(buffer(mutable_char_buffer), lazy);
  94. (void)i1;
  95. int i2 = handle1.async_write_some(buffer(const_char_buffer), lazy);
  96. (void)i2;
  97. handle1.read_some(buffer(mutable_char_buffer));
  98. handle1.read_some(buffer(mutable_char_buffer), ec);
  99. handle1.async_read_some(buffer(mutable_char_buffer), &read_some_handler);
  100. int i3 = handle1.async_read_some(buffer(mutable_char_buffer), lazy);
  101. (void)i3;
  102. }
  103. catch (std::exception&)
  104. {
  105. }
  106. #endif // defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE)
  107. }
  108. } // namespace windows_stream_handle_compile
  109. //------------------------------------------------------------------------------
  110. BOOST_ASIO_TEST_SUITE
  111. (
  112. "windows/stream_handle",
  113. BOOST_ASIO_TEST_CASE(windows_stream_handle_compile::test)
  114. )