]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // serial_port.cpp | |
3 | // ~~~~~~~~~~~~~~~ | |
4 | // | |
1e59de90 | 5 | // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
7c673cae FG |
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" | |
b32b8144 | 21 | #include <boost/asio/io_context.hpp> |
7c673cae FG |
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 | ||
b32b8144 | 33 | struct write_some_handler |
7c673cae | 34 | { |
b32b8144 FG |
35 | write_some_handler() {} |
36 | void operator()(const boost::system::error_code&, std::size_t) {} | |
37 | #if defined(BOOST_ASIO_HAS_MOVE) | |
38 | write_some_handler(write_some_handler&&) {} | |
39 | private: | |
40 | write_some_handler(const write_some_handler&); | |
41 | #endif // defined(BOOST_ASIO_HAS_MOVE) | |
42 | }; | |
7c673cae | 43 | |
b32b8144 | 44 | struct read_some_handler |
7c673cae | 45 | { |
b32b8144 FG |
46 | read_some_handler() {} |
47 | void operator()(const boost::system::error_code&, std::size_t) {} | |
48 | #if defined(BOOST_ASIO_HAS_MOVE) | |
49 | read_some_handler(read_some_handler&&) {} | |
50 | private: | |
51 | read_some_handler(const read_some_handler&); | |
52 | #endif // defined(BOOST_ASIO_HAS_MOVE) | |
53 | }; | |
7c673cae FG |
54 | |
55 | void test() | |
56 | { | |
57 | #if defined(BOOST_ASIO_HAS_SERIAL_PORT) | |
58 | using namespace boost::asio; | |
59 | ||
60 | try | |
61 | { | |
b32b8144 | 62 | io_context ioc; |
92f5a8d4 | 63 | const io_context::executor_type ioc_ex = ioc.get_executor(); |
7c673cae FG |
64 | char mutable_char_buffer[128] = ""; |
65 | const char const_char_buffer[128] = ""; | |
66 | serial_port::baud_rate serial_port_option; | |
67 | archetypes::lazy_handler lazy; | |
68 | boost::system::error_code ec; | |
69 | ||
70 | // basic_serial_port constructors. | |
71 | ||
b32b8144 FG |
72 | serial_port port1(ioc); |
73 | serial_port port2(ioc, "null"); | |
7c673cae | 74 | serial_port::native_handle_type native_port1 = port1.native_handle(); |
92f5a8d4 TL |
75 | #if defined(BOOST_ASIO_MSVC) && (_MSC_VER < 1910) |
76 | // Skip this on older MSVC due to mysterious ambiguous overload errors. | |
77 | #else | |
b32b8144 | 78 | serial_port port3(ioc, native_port1); |
92f5a8d4 TL |
79 | #endif |
80 | ||
81 | serial_port port4(ioc_ex); | |
82 | serial_port port5(ioc_ex, "null"); | |
83 | serial_port::native_handle_type native_port2 = port1.native_handle(); | |
84 | serial_port port6(ioc_ex, native_port2); | |
7c673cae FG |
85 | |
86 | #if defined(BOOST_ASIO_HAS_MOVE) | |
92f5a8d4 | 87 | serial_port port7(std::move(port6)); |
7c673cae FG |
88 | #endif // defined(BOOST_ASIO_HAS_MOVE) |
89 | ||
90 | // basic_serial_port operators. | |
91 | ||
92 | #if defined(BOOST_ASIO_HAS_MOVE) | |
b32b8144 | 93 | port1 = serial_port(ioc); |
7c673cae FG |
94 | port1 = std::move(port2); |
95 | #endif // defined(BOOST_ASIO_HAS_MOVE) | |
96 | ||
97 | // basic_io_object functions. | |
98 | ||
b32b8144 FG |
99 | serial_port::executor_type ex = port1.get_executor(); |
100 | (void)ex; | |
101 | ||
7c673cae FG |
102 | // basic_serial_port functions. |
103 | ||
104 | serial_port::lowest_layer_type& lowest_layer = port1.lowest_layer(); | |
105 | (void)lowest_layer; | |
106 | ||
92f5a8d4 TL |
107 | const serial_port& port8 = port1; |
108 | const serial_port::lowest_layer_type& lowest_layer2 = port8.lowest_layer(); | |
7c673cae FG |
109 | (void)lowest_layer2; |
110 | ||
111 | port1.open("null"); | |
112 | port1.open("null", ec); | |
113 | ||
7c673cae | 114 | serial_port::native_handle_type native_port3 = port1.native_handle(); |
92f5a8d4 TL |
115 | port1.assign(native_port3); |
116 | serial_port::native_handle_type native_port4 = port1.native_handle(); | |
117 | port1.assign(native_port4, ec); | |
7c673cae FG |
118 | |
119 | bool is_open = port1.is_open(); | |
120 | (void)is_open; | |
121 | ||
122 | port1.close(); | |
123 | port1.close(ec); | |
124 | ||
92f5a8d4 TL |
125 | serial_port::native_handle_type native_port5 = port1.native_handle(); |
126 | (void)native_port5; | |
7c673cae | 127 | |
7c673cae FG |
128 | port1.cancel(); |
129 | port1.cancel(ec); | |
130 | ||
131 | port1.set_option(serial_port_option); | |
132 | port1.set_option(serial_port_option, ec); | |
133 | ||
134 | port1.get_option(serial_port_option); | |
135 | port1.get_option(serial_port_option, ec); | |
136 | ||
137 | port1.send_break(); | |
138 | port1.send_break(ec); | |
139 | ||
140 | port1.write_some(buffer(mutable_char_buffer)); | |
141 | port1.write_some(buffer(const_char_buffer)); | |
142 | port1.write_some(buffer(mutable_char_buffer), ec); | |
143 | port1.write_some(buffer(const_char_buffer), ec); | |
144 | ||
b32b8144 FG |
145 | port1.async_write_some(buffer(mutable_char_buffer), write_some_handler()); |
146 | port1.async_write_some(buffer(const_char_buffer), write_some_handler()); | |
7c673cae FG |
147 | int i1 = port1.async_write_some(buffer(mutable_char_buffer), lazy); |
148 | (void)i1; | |
149 | int i2 = port1.async_write_some(buffer(const_char_buffer), lazy); | |
150 | (void)i2; | |
151 | ||
152 | port1.read_some(buffer(mutable_char_buffer)); | |
153 | port1.read_some(buffer(mutable_char_buffer), ec); | |
154 | ||
b32b8144 | 155 | port1.async_read_some(buffer(mutable_char_buffer), read_some_handler()); |
7c673cae FG |
156 | int i3 = port1.async_read_some(buffer(mutable_char_buffer), lazy); |
157 | (void)i3; | |
158 | } | |
159 | catch (std::exception&) | |
160 | { | |
161 | } | |
162 | #endif // defined(BOOST_ASIO_HAS_SERIAL_PORT) | |
163 | } | |
164 | ||
165 | } // namespace serial_port_compile | |
166 | ||
167 | //------------------------------------------------------------------------------ | |
168 | ||
169 | BOOST_ASIO_TEST_SUITE | |
170 | ( | |
171 | "serial_port", | |
172 | BOOST_ASIO_TEST_CASE(serial_port_compile::test) | |
173 | ) |