serial_port.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // serial_port.cpp
  3. // ~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. // Copyright (c) 2008 Rep Invariant Systems, Inc. (info@repinvariant.com)
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // Disable autolinking for unit tests.
  12. #if !defined(BOOST_ALL_NO_LIB)
  13. #define BOOST_ALL_NO_LIB 1
  14. #endif // !defined(BOOST_ALL_NO_LIB)
  15. // Test that header file is self-contained.
  16. #include <boost/asio/serial_port.hpp>
  17. #include "archetypes/async_result.hpp"
  18. #include <boost/asio/io_context.hpp>
  19. #include "unit_test.hpp"
  20. //------------------------------------------------------------------------------
  21. // serial_port_compile test
  22. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  23. // The following test checks that all public member functions on the class
  24. // serial_port compile and link correctly. Runtime failures are ignored.
  25. namespace serial_port_compile {
  26. struct write_some_handler
  27. {
  28. write_some_handler() {}
  29. void operator()(const boost::system::error_code&, std::size_t) {}
  30. #if defined(BOOST_ASIO_HAS_MOVE)
  31. write_some_handler(write_some_handler&&) {}
  32. private:
  33. write_some_handler(const write_some_handler&);
  34. #endif // defined(BOOST_ASIO_HAS_MOVE)
  35. };
  36. struct read_some_handler
  37. {
  38. read_some_handler() {}
  39. void operator()(const boost::system::error_code&, std::size_t) {}
  40. #if defined(BOOST_ASIO_HAS_MOVE)
  41. read_some_handler(read_some_handler&&) {}
  42. private:
  43. read_some_handler(const read_some_handler&);
  44. #endif // defined(BOOST_ASIO_HAS_MOVE)
  45. };
  46. void test()
  47. {
  48. #if defined(BOOST_ASIO_HAS_SERIAL_PORT)
  49. using namespace boost::asio;
  50. try
  51. {
  52. io_context ioc;
  53. const io_context::executor_type ioc_ex = ioc.get_executor();
  54. char mutable_char_buffer[128] = "";
  55. const char const_char_buffer[128] = "";
  56. serial_port::baud_rate serial_port_option;
  57. archetypes::lazy_handler lazy;
  58. boost::system::error_code ec;
  59. // basic_serial_port constructors.
  60. serial_port port1(ioc);
  61. serial_port port2(ioc, "null");
  62. serial_port::native_handle_type native_port1 = port1.native_handle();
  63. #if defined(BOOST_ASIO_MSVC) && (_MSC_VER < 1910)
  64. // Skip this on older MSVC due to mysterious ambiguous overload errors.
  65. #else
  66. serial_port port3(ioc, native_port1);
  67. #endif
  68. serial_port port4(ioc_ex);
  69. serial_port port5(ioc_ex, "null");
  70. serial_port::native_handle_type native_port2 = port1.native_handle();
  71. serial_port port6(ioc_ex, native_port2);
  72. #if defined(BOOST_ASIO_HAS_MOVE)
  73. serial_port port7(std::move(port6));
  74. #endif // defined(BOOST_ASIO_HAS_MOVE)
  75. // basic_serial_port operators.
  76. #if defined(BOOST_ASIO_HAS_MOVE)
  77. port1 = serial_port(ioc);
  78. port1 = std::move(port2);
  79. #endif // defined(BOOST_ASIO_HAS_MOVE)
  80. // basic_io_object functions.
  81. serial_port::executor_type ex = port1.get_executor();
  82. (void)ex;
  83. // basic_serial_port functions.
  84. serial_port::lowest_layer_type& lowest_layer = port1.lowest_layer();
  85. (void)lowest_layer;
  86. const serial_port& port8 = port1;
  87. const serial_port::lowest_layer_type& lowest_layer2 = port8.lowest_layer();
  88. (void)lowest_layer2;
  89. port1.open("null");
  90. port1.open("null", ec);
  91. serial_port::native_handle_type native_port3 = port1.native_handle();
  92. port1.assign(native_port3);
  93. serial_port::native_handle_type native_port4 = port1.native_handle();
  94. port1.assign(native_port4, ec);
  95. bool is_open = port1.is_open();
  96. (void)is_open;
  97. port1.close();
  98. port1.close(ec);
  99. serial_port::native_handle_type native_port5 = port1.native_handle();
  100. (void)native_port5;
  101. port1.cancel();
  102. port1.cancel(ec);
  103. port1.set_option(serial_port_option);
  104. port1.set_option(serial_port_option, ec);
  105. port1.get_option(serial_port_option);
  106. port1.get_option(serial_port_option, ec);
  107. port1.send_break();
  108. port1.send_break(ec);
  109. port1.write_some(buffer(mutable_char_buffer));
  110. port1.write_some(buffer(const_char_buffer));
  111. port1.write_some(buffer(mutable_char_buffer), ec);
  112. port1.write_some(buffer(const_char_buffer), ec);
  113. port1.async_write_some(buffer(mutable_char_buffer), write_some_handler());
  114. port1.async_write_some(buffer(const_char_buffer), write_some_handler());
  115. int i1 = port1.async_write_some(buffer(mutable_char_buffer), lazy);
  116. (void)i1;
  117. int i2 = port1.async_write_some(buffer(const_char_buffer), lazy);
  118. (void)i2;
  119. port1.read_some(buffer(mutable_char_buffer));
  120. port1.read_some(buffer(mutable_char_buffer), ec);
  121. port1.async_read_some(buffer(mutable_char_buffer), read_some_handler());
  122. int i3 = port1.async_read_some(buffer(mutable_char_buffer), lazy);
  123. (void)i3;
  124. }
  125. catch (std::exception&)
  126. {
  127. }
  128. #endif // defined(BOOST_ASIO_HAS_SERIAL_PORT)
  129. }
  130. } // namespace serial_port_compile
  131. //------------------------------------------------------------------------------
  132. BOOST_ASIO_TEST_SUITE
  133. (
  134. "serial_port",
  135. BOOST_ASIO_TEST_CASE(serial_port_compile::test)
  136. )