test_iostream.py
Go to the documentation of this file.
1 from contextlib import redirect_stderr, redirect_stdout
2 from io import StringIO
3 
4 from pybind11_tests import iostream as m
5 
6 
7 def test_captured(capsys):
8  msg = "I've been redirected to Python, I hope!"
9  m.captured_output(msg)
10  stdout, stderr = capsys.readouterr()
11  assert stdout == msg
12  assert not stderr
13 
14  m.captured_output_default(msg)
15  stdout, stderr = capsys.readouterr()
16  assert stdout == msg
17  assert not stderr
18 
19  m.captured_err(msg)
20  stdout, stderr = capsys.readouterr()
21  assert not stdout
22  assert stderr == msg
23 
24 
26  # Make this bigger than the buffer used on the C++ side: 1024 chars
27  msg = "I've been redirected to Python, I hope!"
28  msg = msg * (1024 // len(msg) + 1)
29 
30  m.captured_output_default(msg)
31  stdout, stderr = capsys.readouterr()
32  assert stdout == msg
33  assert not stderr
34 
35 
37  msg = "\u07FF"
38  msg = "" + msg * (1024 // len(msg) + 1)
39 
40  m.captured_output_default(msg)
41  stdout, stderr = capsys.readouterr()
42  assert stdout == msg
43  assert not stderr
44 
45 
47  msg = "\u07FF"
48  msg = "1" + msg * (1024 // len(msg) + 1)
49 
50  m.captured_output_default(msg)
51  stdout, stderr = capsys.readouterr()
52  assert stdout == msg
53  assert not stderr
54 
55 
57  msg = "\uFFFF"
58  msg = "" + msg * (1024 // len(msg) + 1)
59 
60  m.captured_output_default(msg)
61  stdout, stderr = capsys.readouterr()
62  assert stdout == msg
63  assert not stderr
64 
65 
67  msg = "\uFFFF"
68  msg = "1" + msg * (1024 // len(msg) + 1)
69 
70  m.captured_output_default(msg)
71  stdout, stderr = capsys.readouterr()
72  assert stdout == msg
73  assert not stderr
74 
75 
77  msg = "\uFFFF"
78  msg = "12" + msg * (1024 // len(msg) + 1)
79 
80  m.captured_output_default(msg)
81  stdout, stderr = capsys.readouterr()
82  assert stdout == msg
83  assert not stderr
84 
85 
87  msg = "\U0010FFFF"
88  msg = "" + msg * (1024 // len(msg) + 1)
89 
90  m.captured_output_default(msg)
91  stdout, stderr = capsys.readouterr()
92  assert stdout == msg
93  assert not stderr
94 
95 
97  msg = "\U0010FFFF"
98  msg = "1" + msg * (1024 // len(msg) + 1)
99 
100  m.captured_output_default(msg)
101  stdout, stderr = capsys.readouterr()
102  assert stdout == msg
103  assert not stderr
104 
105 
107  msg = "\U0010FFFF"
108  msg = "12" + msg * (1024 // len(msg) + 1)
109 
110  m.captured_output_default(msg)
111  stdout, stderr = capsys.readouterr()
112  assert stdout == msg
113  assert not stderr
114 
115 
117  msg = "\U0010FFFF"
118  msg = "123" + msg * (1024 // len(msg) + 1)
119 
120  m.captured_output_default(msg)
121  stdout, stderr = capsys.readouterr()
122  assert stdout == msg
123  assert not stderr
124 
125 
126 def test_guard_capture(capsys):
127  msg = "I've been redirected to Python, I hope!"
128  m.guard_output(msg)
129  stdout, stderr = capsys.readouterr()
130  assert stdout == msg
131  assert not stderr
132 
133 
134 def test_series_captured(capture):
135  with capture:
136  m.captured_output("a")
137  m.captured_output("b")
138  assert capture == "ab"
139 
140 
141 def test_flush(capfd):
142  msg = "(not flushed)"
143  msg2 = "(flushed)"
144 
145  with m.ostream_redirect():
146  m.noisy_function(msg, flush=False)
147  stdout, stderr = capfd.readouterr()
148  assert not stdout
149 
150  m.noisy_function(msg2, flush=True)
151  stdout, stderr = capfd.readouterr()
152  assert stdout == msg + msg2
153 
154  m.noisy_function(msg, flush=False)
155 
156  stdout, stderr = capfd.readouterr()
157  assert stdout == msg
158 
159 
160 def test_not_captured(capfd):
161  msg = "Something that should not show up in log"
162  stream = StringIO()
163  with redirect_stdout(stream):
164  m.raw_output(msg)
165  stdout, stderr = capfd.readouterr()
166  assert stdout == msg
167  assert not stderr
168  assert not stream.getvalue()
169 
170  stream = StringIO()
171  with redirect_stdout(stream):
172  m.captured_output(msg)
173  stdout, stderr = capfd.readouterr()
174  assert not stdout
175  assert not stderr
176  assert stream.getvalue() == msg
177 
178 
179 def test_err(capfd):
180  msg = "Something that should not show up in log"
181  stream = StringIO()
182  with redirect_stderr(stream):
183  m.raw_err(msg)
184  stdout, stderr = capfd.readouterr()
185  assert not stdout
186  assert stderr == msg
187  assert not stream.getvalue()
188 
189  stream = StringIO()
190  with redirect_stderr(stream):
191  m.captured_err(msg)
192  stdout, stderr = capfd.readouterr()
193  assert not stdout
194  assert not stderr
195  assert stream.getvalue() == msg
196 
197 
199  stream = StringIO()
200  with redirect_stdout(stream):
201  m.captured_output("a")
202  m.raw_output("b")
203  m.captured_output("c")
204  m.raw_output("d")
205  stdout, stderr = capfd.readouterr()
206  assert stdout == "bd"
207  assert stream.getvalue() == "ac"
208 
209 
210 def test_dual(capsys):
211  m.captured_dual("a", "b")
212  stdout, stderr = capsys.readouterr()
213  assert stdout == "a"
214  assert stderr == "b"
215 
216 
217 def test_redirect(capfd):
218  msg = "Should not be in log!"
219  stream = StringIO()
220  with redirect_stdout(stream):
221  m.raw_output(msg)
222  stdout, stderr = capfd.readouterr()
223  assert stdout == msg
224  assert not stream.getvalue()
225 
226  stream = StringIO()
227  with redirect_stdout(stream), m.ostream_redirect():
228  m.raw_output(msg)
229  stdout, stderr = capfd.readouterr()
230  assert not stdout
231  assert stream.getvalue() == msg
232 
233  stream = StringIO()
234  with redirect_stdout(stream):
235  m.raw_output(msg)
236  stdout, stderr = capfd.readouterr()
237  assert stdout == msg
238  assert not stream.getvalue()
239 
240 
241 def test_redirect_err(capfd):
242  msg = "StdOut"
243  msg2 = "StdErr"
244 
245  stream = StringIO()
246  with redirect_stderr(stream), m.ostream_redirect(stdout=False):
247  m.raw_output(msg)
248  m.raw_err(msg2)
249  stdout, stderr = capfd.readouterr()
250  assert stdout == msg
251  assert not stderr
252  assert stream.getvalue() == msg2
253 
254 
256  msg = "StdOut"
257  msg2 = "StdErr"
258 
259  stream = StringIO()
260  stream2 = StringIO()
261  with redirect_stdout(stream), redirect_stderr(stream2), m.ostream_redirect():
262  m.raw_output(msg)
263  m.raw_err(msg2)
264  stdout, stderr = capfd.readouterr()
265  assert not stdout
266  assert not stderr
267  assert stream.getvalue() == msg
268  assert stream2.getvalue() == msg2
269 
270 
272  with m.ostream_redirect(stdout=True, stderr=False):
273  # start some threads
274  threads = []
275 
276  # start some threads
277  for _j in range(20):
278  threads.append(m.TestThread())
279 
280  # give the threads some time to fail
281  threads[0].sleep()
282 
283  # stop all the threads
284  for t in threads:
285  t.stop()
286 
287  for t in threads:
288  t.join()
289 
290  # if a thread segfaults, we don't get here
291  assert True
test_iostream.test_captured_utf8_4byte_offset3
def test_captured_utf8_4byte_offset3(capsys)
Definition: test_iostream.py:116
test_iostream.test_captured_utf8_2byte_offset1
def test_captured_utf8_2byte_offset1(capsys)
Definition: test_iostream.py:46
test_iostream.test_captured_utf8_3byte_offset0
def test_captured_utf8_3byte_offset0(capsys)
Definition: test_iostream.py:56
test_iostream.test_captured_utf8_4byte_offset2
def test_captured_utf8_4byte_offset2(capsys)
Definition: test_iostream.py:106
test_iostream.test_guard_capture
def test_guard_capture(capsys)
Definition: test_iostream.py:126
test_iostream.test_captured_utf8_4byte_offset0
def test_captured_utf8_4byte_offset0(capsys)
Definition: test_iostream.py:86
test_iostream.test_err
def test_err(capfd)
Definition: test_iostream.py:179
gtsam::range
Double_ range(const Point2_ &p, const Point2_ &q)
Definition: slam/expressions.h:30
test_iostream.test_redirect_err
def test_redirect_err(capfd)
Definition: test_iostream.py:241
test_iostream.test_redirect_both
def test_redirect_both(capfd)
Definition: test_iostream.py:255
test_iostream.test_captured
def test_captured(capsys)
Definition: test_iostream.py:7
test_iostream.test_dual
def test_dual(capsys)
Definition: test_iostream.py:210
test_iostream.test_captured_large_string
def test_captured_large_string(capsys)
Definition: test_iostream.py:25
test_iostream.test_flush
def test_flush(capfd)
Definition: test_iostream.py:141
test_iostream.test_multi_captured
def test_multi_captured(capfd)
Definition: test_iostream.py:198
test_iostream.test_captured_utf8_3byte_offset1
def test_captured_utf8_3byte_offset1(capsys)
Definition: test_iostream.py:66
test_iostream.test_series_captured
def test_series_captured(capture)
Definition: test_iostream.py:134
test_iostream.test_redirect
def test_redirect(capfd)
Definition: test_iostream.py:217
test_iostream.test_captured_utf8_4byte_offset1
def test_captured_utf8_4byte_offset1(capsys)
Definition: test_iostream.py:96
test_iostream.test_captured_utf8_2byte_offset0
def test_captured_utf8_2byte_offset0(capsys)
Definition: test_iostream.py:36
len
size_t len(handle h)
Get the length of a Python object.
Definition: pytypes.h:2399
test_iostream.test_threading
def test_threading()
Definition: test_iostream.py:271
test_iostream.test_not_captured
def test_not_captured(capfd)
Definition: test_iostream.py:160
test_iostream.test_captured_utf8_3byte_offset2
def test_captured_utf8_3byte_offset2(capsys)
Definition: test_iostream.py:76


gtsam
Author(s):
autogenerated on Tue Jun 25 2024 03:05:28