random_access_handle.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // random_access_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/random_access_handle.hpp>
  16. #include <boost/asio/io_context.hpp>
  17. #include "../archetypes/async_result.hpp"
  18. #include "../unit_test.hpp"
  19. //------------------------------------------------------------------------------
  20. // windows_random_access_handle_compile test
  21. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  22. // The following test checks that all public member functions on the class
  23. // windows::random_access_handle compile and link correctly. Runtime failures
  24. // are ignored.
  25. namespace windows_random_access_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_RANDOM_ACCESS_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. boost::asio::uint64_t offset = 0;
  44. archetypes::lazy_handler lazy;
  45. boost::system::error_code ec;
  46. // basic_random_access_handle constructors.
  47. win::random_access_handle handle1(ioc);
  48. HANDLE native_handle1 = INVALID_HANDLE_VALUE;
  49. #if defined(BOOST_ASIO_MSVC) && (_MSC_VER < 1910)
  50. // Skip this on older MSVC due to mysterious ambiguous overload errors.
  51. #else
  52. win::random_access_handle handle2(ioc, native_handle1);
  53. #endif
  54. win::random_access_handle handle3(ioc_ex);
  55. HANDLE native_handle2 = INVALID_HANDLE_VALUE;
  56. win::random_access_handle handle4(ioc_ex, native_handle2);
  57. #if defined(BOOST_ASIO_HAS_MOVE)
  58. win::random_access_handle handle5(std::move(handle4));
  59. #endif // defined(BOOST_ASIO_HAS_MOVE)
  60. // basic_random_access_handle operators.
  61. #if defined(BOOST_ASIO_HAS_MOVE)
  62. handle1 = win::random_access_handle(ioc);
  63. handle1 = std::move(handle4);
  64. #endif // defined(BOOST_ASIO_HAS_MOVE)
  65. // basic_io_object functions.
  66. windows::random_access_handle::executor_type ex = handle1.get_executor();
  67. (void)ex;
  68. // basic_overlapped_handle functions.
  69. win::random_access_handle::lowest_layer_type& lowest_layer
  70. = handle1.lowest_layer();
  71. (void)lowest_layer;
  72. const win::random_access_handle& handle6 = handle1;
  73. const win::random_access_handle::lowest_layer_type& lowest_layer2
  74. = handle6.lowest_layer();
  75. (void)lowest_layer2;
  76. HANDLE native_handle3 = INVALID_HANDLE_VALUE;
  77. handle1.assign(native_handle3);
  78. bool is_open = handle1.is_open();
  79. (void)is_open;
  80. handle1.close();
  81. handle1.close(ec);
  82. win::random_access_handle::native_handle_type native_handle4
  83. = handle1.native_handle();
  84. (void)native_handle4;
  85. handle1.cancel();
  86. handle1.cancel(ec);
  87. // basic_random_access_handle functions.
  88. handle1.write_some_at(offset, buffer(mutable_char_buffer));
  89. handle1.write_some_at(offset, buffer(const_char_buffer));
  90. handle1.write_some_at(offset, buffer(mutable_char_buffer), ec);
  91. handle1.write_some_at(offset, buffer(const_char_buffer), ec);
  92. handle1.async_write_some_at(offset,
  93. buffer(mutable_char_buffer), &write_some_handler);
  94. handle1.async_write_some_at(offset,
  95. buffer(const_char_buffer), &write_some_handler);
  96. int i1 = handle1.async_write_some_at(offset,
  97. buffer(mutable_char_buffer), lazy);
  98. (void)i1;
  99. int i2 = handle1.async_write_some_at(offset,
  100. buffer(const_char_buffer), lazy);
  101. (void)i2;
  102. handle1.read_some_at(offset, buffer(mutable_char_buffer));
  103. handle1.read_some_at(offset, buffer(mutable_char_buffer), ec);
  104. handle1.async_read_some_at(offset,
  105. buffer(mutable_char_buffer), &read_some_handler);
  106. int i3 = handle1.async_read_some_at(offset,
  107. buffer(mutable_char_buffer), lazy);
  108. (void)i3;
  109. }
  110. catch (std::exception&)
  111. {
  112. }
  113. #endif // defined(BOOST_ASIO_HAS_WINDOWS_RANDOM_ACCESS_HANDLE)
  114. }
  115. } // namespace windows_random_access_handle_compile
  116. //------------------------------------------------------------------------------
  117. BOOST_ASIO_TEST_SUITE
  118. (
  119. "windows/random_access_handle",
  120. BOOST_ASIO_TEST_CASE(windows_random_access_handle_compile::test)
  121. )