]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/process/test/pipe.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / process / test / pipe.cpp
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>
14
15 using namespace std;
16 namespace bp = boost::process;
17
18 BOOST_AUTO_TEST_CASE(plain, *boost::unit_test::timeout(2))
19 {
20 bp::pipe pipe;
21
22 std::string in = "test";
23 pipe.write(in.c_str(), in.size());
24
25 std::string out;
26 out.resize(4);
27 pipe.read(&out.front(), out.size());
28
29 BOOST_CHECK_EQUAL(out, in);
30 }
31
32 BOOST_AUTO_TEST_CASE(named, *boost::unit_test::timeout(2))
33 {
34
35 #if defined( BOOST_WINDOWS_API )
36 bp::pipe pipe("\\\\.\\pipe\\pipe_name");
37 #elif defined( BOOST_POSIX_API )
38 bp::pipe pipe("./test_pipe");
39 #endif
40
41 std::string in = "xyz";
42 pipe.write(in.c_str(), in.size());
43
44
45 std::string out;
46 out.resize(3);
47 pipe.read(&out.front(), out.size());
48
49
50 BOOST_CHECK_EQUAL(out, in);
51 }
52
53 BOOST_AUTO_TEST_CASE(copy_pipe, *boost::unit_test::timeout(2))
54 {
55 bp::pipe pipe;
56
57 std::string in = "test";
58 pipe.write(in.c_str(), in.size());
59
60 std::string out;
61 out.resize(4);
62 auto p2 = pipe;
63 p2.read(&out.front(), out.size());
64
65 BOOST_CHECK_EQUAL(out, in);
66 }
67
68 BOOST_AUTO_TEST_CASE(move_pipe, *boost::unit_test::timeout(2))
69 {
70 bp::pipe pipe;
71
72 std::string in = "test";
73 pipe.write(in.c_str(), in.size());
74
75 std::string out;
76 out.resize(4);
77 auto p2 = std::move(pipe);
78 p2.read(&out.front(), out.size());
79
80 BOOST_CHECK_EQUAL(out, in);
81 }
82
83 BOOST_AUTO_TEST_CASE(stream, *boost::unit_test::timeout(2))
84 {
85
86 bp::pipe pipe;
87
88 bp::pstream os(pipe);
89 bp::ipstream is(pipe);
90
91 int i = 42, j = 0;
92
93 os << i << std::endl;
94 os << std::endl;
95 is >> j;
96
97 BOOST_CHECK_EQUAL(i, j);
98 }
99
100 BOOST_AUTO_TEST_CASE(stream_line, *boost::unit_test::timeout(2))
101 {
102
103 bp::pstream os;
104
105 std::string s = "My Test String";
106
107 std::string out;
108
109 os << s << std::endl;
110
111 std::getline(os, out);
112
113 auto size = (out.size() < s.size()) ? out.size() : s.size();
114
115
116 BOOST_CHECK_EQUAL_COLLECTIONS(
117 s.begin(), s. begin() + size,
118 out.begin(), out.begin() + size
119 );
120 }
121
122
123 BOOST_AUTO_TEST_CASE(large_data, *boost::unit_test::timeout(20))
124 {
125 bp::pipe pipe;
126
127 bp::pipebuf is_buf(pipe);
128 bp::pipebuf os_buf(std::move(pipe));
129
130 std::istream is(&is_buf);
131 std::ostream os(&os_buf);
132
133 std::string in(1000000, '0');
134 std::string out;
135
136 int cnt = 0;
137 for (auto & c: in)
138 c = (cnt++ % 26) + 'A';
139
140 std::thread th([&]{os << in << std::endl;});
141
142 is >> out;
143 BOOST_REQUIRE_EQUAL_COLLECTIONS(out.begin(), out.end(), in.begin(), in.end());
144 th.join();
145 }
146
147 BOOST_AUTO_TEST_CASE(closed, *boost::unit_test::timeout(2))
148 {
149 bp::opstream os;
150 bp::ipstream is;
151
152 os.pipe().close();
153 is.pipe().close();
154
155 int i;
156
157 BOOST_CHECK(!(os << 42 << endl));
158 BOOST_CHECK(!(is >> i));
159 }
160
161
162 BOOST_AUTO_TEST_CASE(coverage, *boost::unit_test::timeout(5))
163 {
164 //more of a syntax check, since template.
165 {
166 bp::pipe p1;
167 bp::ipstream is1(p1);
168 bp::ipstream is2(std::move(p1));
169
170 is2.pipe(is1.pipe());
171
172 bp::pipe p2_;
173 bp::pipe p2 = p2_;
174 BOOST_REQUIRE_NO_THROW(p2_ == p2);
175 BOOST_CHECK(p2_ == p2);
176
177 bp::opstream os1(p2);
178 bp::opstream os2(std::move(p2));
179
180 os2.pipe(os1.pipe());
181
182 bp::pipe p3;
183 is1 = p3;
184 is2 = std::move(p3);
185
186 bp::pipe p4_;
187 bp::pipe p4 = std::move(p4_);
188
189 bp::pipe p5;
190 BOOST_REQUIRE_NO_THROW(p4_ != p4);
191 BOOST_CHECK(p4_ != p4);
192
193 BOOST_REQUIRE_NO_THROW(p5 != p4);
194 BOOST_CHECK(p4 != p5);
195
196 is1 = p4;
197 is2 = std::move(p4);
198 }
199 {
200 bp::wpipe p;
201 bp::wpstream ws1(p);
202 bp::wpstream ws2(std::move(p));
203
204 ws2.pipe(std::move(ws1.pipe()));
205
206 bp::wpipe p2;
207
208 ws1 = p2;
209 ws2 = std::move(p2);
210
211 const bp::wpstream & ws2c = ws2;
212 ws1.pipe(ws2c.pipe());
213 }
214
215 {
216 bp::wpipe p;
217 bp::wpipebuf ws1(p);
218 bp::wpipebuf ws2(std::move(p));
219
220 ws2.pipe(std::move(ws1.pipe()));
221
222 bp::wpipe p2;
223
224 ws1 = p2;
225 ws2 = std::move(p2);
226
227 const bp::wpipebuf & ws2c = ws2;
228 ws1.pipe(ws2c.pipe());
229
230 }
231 }
232