]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/process/test/pipe.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / process / test / pipe.cpp
CommitLineData
b32b8144
FG
1// Copyright (c) 2016 Klemens D. Morgenstern
2//
3// Distributed under the Boost Software License, Version 1.0. (See accompanying
4// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6
7#define BOOST_TEST_MAIN
8
9#include <boost/test/included/unit_test.hpp>
10#include <iostream>
11#include <thread>
12
13#include <boost/process/pipe.hpp>
92f5a8d4 14#include <boost/process/environment.hpp>
b32b8144
FG
15
16using namespace std;
17namespace bp = boost::process;
18
92f5a8d4
TL
19BOOST_AUTO_TEST_SUITE( pipe_tests );
20
21
b32b8144
FG
22BOOST_AUTO_TEST_CASE(plain, *boost::unit_test::timeout(2))
23{
24 bp::pipe pipe;
25
26 std::string in = "test";
27 pipe.write(in.c_str(), in.size());
28
29 std::string out;
30 out.resize(4);
31 pipe.read(&out.front(), out.size());
32
33 BOOST_CHECK_EQUAL(out, in);
34}
35
36BOOST_AUTO_TEST_CASE(named, *boost::unit_test::timeout(2))
37{
38
39#if defined( BOOST_WINDOWS_API )
40 bp::pipe pipe("\\\\.\\pipe\\pipe_name");
41#elif defined( BOOST_POSIX_API )
92f5a8d4
TL
42 const auto home_path = boost::this_process::environment()["HOME"].to_string();
43 bp::pipe pipe(home_path + "/.boost_process_test_pipe");
b32b8144
FG
44#endif
45
46 std::string in = "xyz";
47 pipe.write(in.c_str(), in.size());
48
49
50 std::string out;
51 out.resize(3);
52 pipe.read(&out.front(), out.size());
53
54
55 BOOST_CHECK_EQUAL(out, in);
56}
57
58BOOST_AUTO_TEST_CASE(copy_pipe, *boost::unit_test::timeout(2))
59{
60 bp::pipe pipe;
61
62 std::string in = "test";
63 pipe.write(in.c_str(), in.size());
64
65 std::string out;
66 out.resize(4);
67 auto p2 = pipe;
68 p2.read(&out.front(), out.size());
69
70 BOOST_CHECK_EQUAL(out, in);
71}
72
73BOOST_AUTO_TEST_CASE(move_pipe, *boost::unit_test::timeout(2))
74{
75 bp::pipe pipe;
76
77 std::string in = "test";
78 pipe.write(in.c_str(), in.size());
79
80 std::string out;
81 out.resize(4);
82 auto p2 = std::move(pipe);
83 p2.read(&out.front(), out.size());
84
85 BOOST_CHECK_EQUAL(out, in);
86}
87
88BOOST_AUTO_TEST_CASE(stream, *boost::unit_test::timeout(2))
89{
90
91 bp::pipe pipe;
92
93 bp::pstream os(pipe);
94 bp::ipstream is(pipe);
95
96 int i = 42, j = 0;
97
98 os << i << std::endl;
99 os << std::endl;
100 is >> j;
101
102 BOOST_CHECK_EQUAL(i, j);
103}
104
105BOOST_AUTO_TEST_CASE(stream_line, *boost::unit_test::timeout(2))
106{
107
108 bp::pstream os;
109
110 std::string s = "My Test String";
111
112 std::string out;
113
114 os << s << std::endl;
115
116 std::getline(os, out);
117
118 auto size = (out.size() < s.size()) ? out.size() : s.size();
119
120
121 BOOST_CHECK_EQUAL_COLLECTIONS(
122 s.begin(), s. begin() + size,
123 out.begin(), out.begin() + size
124 );
125}
126
127
128BOOST_AUTO_TEST_CASE(large_data, *boost::unit_test::timeout(20))
129{
130 bp::pipe pipe;
131
132 bp::pipebuf is_buf(pipe);
133 bp::pipebuf os_buf(std::move(pipe));
134
135 std::istream is(&is_buf);
136 std::ostream os(&os_buf);
137
138 std::string in(1000000, '0');
139 std::string out;
140
141 int cnt = 0;
142 for (auto & c: in)
143 c = (cnt++ % 26) + 'A';
144
145 std::thread th([&]{os << in << std::endl;});
146
147 is >> out;
148 BOOST_REQUIRE_EQUAL_COLLECTIONS(out.begin(), out.end(), in.begin(), in.end());
149 th.join();
150}
151
152BOOST_AUTO_TEST_CASE(closed, *boost::unit_test::timeout(2))
153{
154 bp::opstream os;
155 bp::ipstream is;
156
157 os.pipe().close();
158 is.pipe().close();
159
160 int i;
161
162 BOOST_CHECK(!(os << 42 << endl));
163 BOOST_CHECK(!(is >> i));
164}
165
166
167BOOST_AUTO_TEST_CASE(coverage, *boost::unit_test::timeout(5))
168{
169 //more of a syntax check, since template.
170 {
171 bp::pipe p1;
172 bp::ipstream is1(p1);
173 bp::ipstream is2(std::move(p1));
174
175 is2.pipe(is1.pipe());
176
177 bp::pipe p2_;
178 bp::pipe p2 = p2_;
179 BOOST_REQUIRE_NO_THROW(p2_ == p2);
180 BOOST_CHECK(p2_ == p2);
181
182 bp::opstream os1(p2);
183 bp::opstream os2(std::move(p2));
184
185 os2.pipe(os1.pipe());
186
187 bp::pipe p3;
188 is1 = p3;
189 is2 = std::move(p3);
190
191 bp::pipe p4_;
192 bp::pipe p4 = std::move(p4_);
193
194 bp::pipe p5;
195 BOOST_REQUIRE_NO_THROW(p4_ != p4);
196 BOOST_CHECK(p4_ != p4);
197
198 BOOST_REQUIRE_NO_THROW(p5 != p4);
199 BOOST_CHECK(p4 != p5);
200
201 is1 = p4;
202 is2 = std::move(p4);
203 }
204 {
205 bp::wpipe p;
206 bp::wpstream ws1(p);
207 bp::wpstream ws2(std::move(p));
208
209 ws2.pipe(std::move(ws1.pipe()));
210
211 bp::wpipe p2;
212
213 ws1 = p2;
214 ws2 = std::move(p2);
215
216 const bp::wpstream & ws2c = ws2;
217 ws1.pipe(ws2c.pipe());
218 }
219
220 {
221 bp::wpipe p;
222 bp::wpipebuf ws1(p);
223 bp::wpipebuf ws2(std::move(p));
224
225 ws2.pipe(std::move(ws1.pipe()));
226
227 bp::wpipe p2;
228
229 ws1 = p2;
230 ws2 = std::move(p2);
231
232 const bp::wpipebuf & ws2c = ws2;
233 ws1.pipe(ws2c.pipe());
234
235 }
236}
237
92f5a8d4
TL
238
239BOOST_AUTO_TEST_CASE(stream_close, *boost::unit_test::timeout(5))
240{
241 bp::pipe p;
242 int i = 1234, j = 0;
243 bp::opstream op{p};
244 bp::ipstream ip{p};
245 p.close();
246
247 op << i << " ";
248 op.close();
249
250 ip >> j;
251
252 BOOST_CHECK_EQUAL(i, j);
253}
254
255BOOST_AUTO_TEST_CASE(stream_close_scope, *boost::unit_test::timeout(5))
256{
257 bp::pipe p;
258 int i = 1234, j = 0;
259 bp::ipstream ip;
260
261 {
262 bp::opstream op{ip.pipe()};
263 op << i << " ";
264 }
265 ip >> j;
266
267 BOOST_CHECK_EQUAL(i, j);
268}
269
270
271BOOST_AUTO_TEST_SUITE_END();