| 1 | // |
| 2 | // serial_port.cpp |
| 3 | // ~~~~~~~~~~~~~~~ |
| 4 | // |
| 5 | // Copyright (c) 2003-2024 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 | |
| 12 | // Disable autolinking for unit tests. |
| 13 | #if !defined(BOOST_ALL_NO_LIB) |
| 14 | #define BOOST_ALL_NO_LIB 1 |
| 15 | #endif // !defined(BOOST_ALL_NO_LIB) |
| 16 | |
| 17 | // Test that header file is self-contained. |
| 18 | #include <boost/asio/serial_port.hpp> |
| 19 | |
| 20 | #include "archetypes/async_result.hpp" |
| 21 | #include <boost/asio/io_context.hpp> |
| 22 | #include "unit_test.hpp" |
| 23 | |
| 24 | //------------------------------------------------------------------------------ |
| 25 | |
| 26 | // serial_port_compile test |
| 27 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 28 | // The following test checks that all public member functions on the class |
| 29 | // serial_port compile and link correctly. Runtime failures are ignored. |
| 30 | |
| 31 | namespace serial_port_compile { |
| 32 | |
| 33 | struct write_some_handler |
| 34 | { |
| 35 | write_some_handler() {} |
| 36 | void operator()(const boost::system::error_code&, std::size_t) {} |
| 37 | write_some_handler(write_some_handler&&) {} |
| 38 | private: |
| 39 | write_some_handler(const write_some_handler&); |
| 40 | }; |
| 41 | |
| 42 | struct read_some_handler |
| 43 | { |
| 44 | read_some_handler() {} |
| 45 | void operator()(const boost::system::error_code&, std::size_t) {} |
| 46 | read_some_handler(read_some_handler&&) {} |
| 47 | private: |
| 48 | read_some_handler(const read_some_handler&); |
| 49 | }; |
| 50 | |
| 51 | void test() |
| 52 | { |
| 53 | #if defined(BOOST_ASIO_HAS_SERIAL_PORT) |
| 54 | using namespace boost::asio; |
| 55 | |
| 56 | try |
| 57 | { |
| 58 | io_context ioc; |
| 59 | const io_context::executor_type ioc_ex = ioc.get_executor(); |
| 60 | char mutable_char_buffer[128] = "" ; |
| 61 | const char const_char_buffer[128] = "" ; |
| 62 | serial_port::baud_rate serial_port_option; |
| 63 | archetypes::lazy_handler lazy; |
| 64 | boost::system::error_code ec; |
| 65 | |
| 66 | // basic_serial_port constructors. |
| 67 | |
| 68 | serial_port port1(ioc); |
| 69 | serial_port port2(ioc, "null" ); |
| 70 | serial_port::native_handle_type native_port1 = port1.native_handle(); |
| 71 | #if defined(BOOST_ASIO_MSVC) && (_MSC_VER < 1910) |
| 72 | // Skip this on older MSVC due to mysterious ambiguous overload errors. |
| 73 | #else |
| 74 | serial_port port3(ioc, native_port1); |
| 75 | #endif |
| 76 | |
| 77 | serial_port port4(ioc_ex); |
| 78 | serial_port port5(ioc_ex, "null" ); |
| 79 | serial_port::native_handle_type native_port2 = port1.native_handle(); |
| 80 | serial_port port6(ioc_ex, native_port2); |
| 81 | |
| 82 | serial_port port7(std::move(port6)); |
| 83 | |
| 84 | basic_serial_port<io_context::executor_type> port8(ioc); |
| 85 | serial_port port9(std::move(port8)); |
| 86 | |
| 87 | // basic_serial_port operators. |
| 88 | |
| 89 | port1 = serial_port(ioc); |
| 90 | port1 = std::move(port2); |
| 91 | port1 = std::move(port8); |
| 92 | |
| 93 | // basic_io_object functions. |
| 94 | |
| 95 | serial_port::executor_type ex = port1.get_executor(); |
| 96 | (void)ex; |
| 97 | |
| 98 | // basic_serial_port functions. |
| 99 | |
| 100 | serial_port::lowest_layer_type& lowest_layer = port1.lowest_layer(); |
| 101 | (void)lowest_layer; |
| 102 | |
| 103 | const serial_port& port10 = port1; |
| 104 | const serial_port::lowest_layer_type& lowest_layer2 = port10.lowest_layer(); |
| 105 | (void)lowest_layer2; |
| 106 | |
| 107 | port1.open(device: "null" ); |
| 108 | port1.open(device: "null" , ec); |
| 109 | |
| 110 | serial_port::native_handle_type native_port3 = port1.native_handle(); |
| 111 | port1.assign(native_serial_port: native_port3); |
| 112 | serial_port::native_handle_type native_port4 = port1.native_handle(); |
| 113 | port1.assign(native_serial_port: native_port4, ec); |
| 114 | |
| 115 | bool is_open = port1.is_open(); |
| 116 | (void)is_open; |
| 117 | |
| 118 | port1.close(); |
| 119 | port1.close(ec); |
| 120 | |
| 121 | serial_port::native_handle_type native_port5 = port1.native_handle(); |
| 122 | (void)native_port5; |
| 123 | |
| 124 | port1.cancel(); |
| 125 | port1.cancel(ec); |
| 126 | |
| 127 | port1.set_option(serial_port_option); |
| 128 | port1.set_option(option: serial_port_option, ec); |
| 129 | |
| 130 | port1.get_option(option&: serial_port_option); |
| 131 | port1.get_option(option&: serial_port_option, ec); |
| 132 | |
| 133 | port1.send_break(); |
| 134 | port1.send_break(ec); |
| 135 | |
| 136 | port1.write_some(buffers: buffer(data&: mutable_char_buffer)); |
| 137 | port1.write_some(buffers: buffer(data: const_char_buffer)); |
| 138 | port1.write_some(buffers: buffer(data&: mutable_char_buffer), ec); |
| 139 | port1.write_some(buffers: buffer(data: const_char_buffer), ec); |
| 140 | |
| 141 | port1.async_write_some(buffers: buffer(data&: mutable_char_buffer), token: write_some_handler()); |
| 142 | port1.async_write_some(buffers: buffer(data: const_char_buffer), token: write_some_handler()); |
| 143 | int i1 = port1.async_write_some(buffers: buffer(data&: mutable_char_buffer), token&: lazy); |
| 144 | (void)i1; |
| 145 | int i2 = port1.async_write_some(buffers: buffer(data: const_char_buffer), token&: lazy); |
| 146 | (void)i2; |
| 147 | |
| 148 | port1.read_some(buffers: buffer(data&: mutable_char_buffer)); |
| 149 | port1.read_some(buffers: buffer(data&: mutable_char_buffer), ec); |
| 150 | |
| 151 | port1.async_read_some(buffers: buffer(data&: mutable_char_buffer), token: read_some_handler()); |
| 152 | int i3 = port1.async_read_some(buffers: buffer(data&: mutable_char_buffer), token&: lazy); |
| 153 | (void)i3; |
| 154 | } |
| 155 | catch (std::exception&) |
| 156 | { |
| 157 | } |
| 158 | #endif // defined(BOOST_ASIO_HAS_SERIAL_PORT) |
| 159 | } |
| 160 | |
| 161 | } // namespace serial_port_compile |
| 162 | |
| 163 | //------------------------------------------------------------------------------ |
| 164 | |
| 165 | BOOST_ASIO_TEST_SUITE |
| 166 | ( |
| 167 | "serial_port" , |
| 168 | BOOST_ASIO_COMPILE_TEST_CASE(serial_port_compile::test) |
| 169 | ) |
| 170 | |