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


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:07:05