• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // serial_port.cpp
3 // ~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2021 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 {
write_some_handlerserial_port_compile::write_some_handler35   write_some_handler() {}
operator ()serial_port_compile::write_some_handler36   void operator()(const boost::system::error_code&, std::size_t) {}
37 #if defined(BOOST_ASIO_HAS_MOVE)
write_some_handlerserial_port_compile::write_some_handler38   write_some_handler(write_some_handler&&) {}
39 private:
40   write_some_handler(const write_some_handler&);
41 #endif // defined(BOOST_ASIO_HAS_MOVE)
42 };
43 
44 struct read_some_handler
45 {
read_some_handlerserial_port_compile::read_some_handler46   read_some_handler() {}
operator ()serial_port_compile::read_some_handler47   void operator()(const boost::system::error_code&, std::size_t) {}
48 #if defined(BOOST_ASIO_HAS_MOVE)
read_some_handlerserial_port_compile::read_some_handler49   read_some_handler(read_some_handler&&) {}
50 private:
51   read_some_handler(const read_some_handler&);
52 #endif // defined(BOOST_ASIO_HAS_MOVE)
53 };
54 
test()55 void test()
56 {
57 #if defined(BOOST_ASIO_HAS_SERIAL_PORT)
58   using namespace boost::asio;
59 
60   try
61   {
62     io_context ioc;
63     const io_context::executor_type ioc_ex = ioc.get_executor();
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 
72     serial_port port1(ioc);
73     serial_port port2(ioc, "null");
74     serial_port::native_handle_type native_port1 = port1.native_handle();
75 #if defined(BOOST_ASIO_MSVC) && (_MSC_VER < 1910)
76     // Skip this on older MSVC due to mysterious ambiguous overload errors.
77 #else
78     serial_port port3(ioc, native_port1);
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);
85 
86 #if defined(BOOST_ASIO_HAS_MOVE)
87     serial_port port7(std::move(port6));
88 #endif // defined(BOOST_ASIO_HAS_MOVE)
89 
90     // basic_serial_port operators.
91 
92 #if defined(BOOST_ASIO_HAS_MOVE)
93     port1 = serial_port(ioc);
94     port1 = std::move(port2);
95 #endif // defined(BOOST_ASIO_HAS_MOVE)
96 
97     // basic_io_object functions.
98 
99     serial_port::executor_type ex = port1.get_executor();
100     (void)ex;
101 
102     // basic_serial_port functions.
103 
104     serial_port::lowest_layer_type& lowest_layer = port1.lowest_layer();
105     (void)lowest_layer;
106 
107     const serial_port& port8 = port1;
108     const serial_port::lowest_layer_type& lowest_layer2 = port8.lowest_layer();
109     (void)lowest_layer2;
110 
111     port1.open("null");
112     port1.open("null", ec);
113 
114     serial_port::native_handle_type native_port3 = port1.native_handle();
115     port1.assign(native_port3);
116     serial_port::native_handle_type native_port4 = port1.native_handle();
117     port1.assign(native_port4, ec);
118 
119     bool is_open = port1.is_open();
120     (void)is_open;
121 
122     port1.close();
123     port1.close(ec);
124 
125     serial_port::native_handle_type native_port5 = port1.native_handle();
126     (void)native_port5;
127 
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 
145     port1.async_write_some(buffer(mutable_char_buffer), write_some_handler());
146     port1.async_write_some(buffer(const_char_buffer), write_some_handler());
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 
155     port1.async_read_some(buffer(mutable_char_buffer), read_some_handler());
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 )
174