• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Contexts</title>
5<link rel="stylesheet" href="../../../boostbook.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../../../index.html" title="Boost.Test">
8<link rel="up" href="../test_tools_support_for_logging.html" title="Tools supports for logging">
9<link rel="prev" href="checkpoints.html" title="Checkpoints for accurate failure location">
10<link rel="next" href="log_floating_points.html" title="Logging floating point type numbers">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr>
14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
15<td align="center"><a href="../../../../../../../index.html">Home</a></td>
16<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
20</tr></table>
21<hr>
22<div class="spirit-nav">
23<a accesskey="p" href="checkpoints.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../test_tools_support_for_logging.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="log_floating_points.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h4 class="title">
27<a name="boost_test.test_output.test_tools_support_for_logging.contexts"></a><a class="link" href="contexts.html" title="Contexts">Contexts</a>
28</h4></div></div></div>
29<p>
30          Contexts are a facility provided by the <span class="emphasis"><em>Unit Test Framework</em></span>
31          in order to be able to trace the location of assertions better. To grasp
32          the idea, consider the following example:
33        </p>
34<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">test_operations</span><span class="special">(</span><span class="identifier">Processor</span><span class="special">&amp;</span> <span class="identifier">processor</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">limit</span><span class="special">)</span>
35<span class="special">{</span>
36  <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">limit</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">{</span>
37    <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">processor</span><span class="special">.</span><span class="identifier">op1</span><span class="special">(</span><span class="identifier">i</span><span class="special">));</span>
38    <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">j</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">j</span> <span class="special">&lt;</span> <span class="identifier">i</span><span class="special">;</span> <span class="special">++</span><span class="identifier">j</span><span class="special">)</span> <span class="special">{</span>
39      <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">processor</span><span class="special">.</span><span class="identifier">op2</span><span class="special">(</span><span class="identifier">i</span><span class="special">,</span> <span class="identifier">j</span><span class="special">));</span>
40    <span class="special">}</span>
41  <span class="special">}</span>
42<span class="special">}</span>
43</pre>
44<p>
45          In case of failure, in order to see in the logs at which point of the loops
46          the failure occurred, we need some extra information in the assertion,
47          which can be achieved for instance <a class="link" href="../../testing_tools/reports.html#boost_test.testing_tools.reports.custom_messages">the
48          following way</a>:
49        </p>
50<pre class="programlisting"><span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">processor</span><span class="special">.</span><span class="identifier">op1</span><span class="special">(</span><span class="identifier">i</span><span class="special">));</span>
51</pre>
52<p>
53          replaced by
54        </p>
55<pre class="programlisting"><span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">processor</span><span class="special">.</span><span class="identifier">op1</span><span class="special">(</span><span class="identifier">i</span><span class="special">),</span> <span class="string">"With parameter i = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span><span class="special">);</span>
56</pre>
57<p>
58          We see in this trivial example that a context, which is the variable <code class="computeroutput"><span class="identifier">i</span></code> in this case, should be acknowledged
59          by the assertion <code class="computeroutput"><span class="identifier">BOOST_CHECK</span></code>
60          in a particular way. In the approach above, this is done by adding a message
61          to the assertion itself.
62        </p>
63<p>
64          What if the context is more complex than that? In case the complexity of
65          the context increases, the fact that the assertion and the context is tightly
66          coupled as in the approach above is difficult to maintain:
67        </p>
68<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">test_operations</span><span class="special">(</span><span class="identifier">Processor</span><span class="special">&amp;</span> <span class="identifier">processor</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">limit</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">level</span><span class="special">)</span>
69<span class="special">{</span>
70  <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">limit</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">{</span>
71    <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">processor</span><span class="special">.</span><span class="identifier">op1</span><span class="special">(</span><span class="identifier">i</span><span class="special">),</span>
72               <span class="string">"With optimization level "</span> <span class="special">&lt;&lt;</span> <span class="identifier">level</span> <span class="special">&lt;&lt;</span> <span class="string">", With parameter i = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span><span class="special">);</span>
73    <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">j</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">j</span> <span class="special">&lt;</span> <span class="identifier">i</span><span class="special">;</span> <span class="special">++</span><span class="identifier">j</span><span class="special">)</span> <span class="special">{</span>
74      <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">processor</span><span class="special">.</span><span class="identifier">op2</span><span class="special">(</span><span class="identifier">i</span><span class="special">,</span> <span class="identifier">j</span><span class="special">),</span>
75                 <span class="string">"With optimization level "</span> <span class="special">&lt;&lt;</span> <span class="identifier">level</span> <span class="special">&lt;&lt;</span>
76                 <span class="string">", With parameter i = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span> <span class="special">&lt;&lt;</span> <span class="string">", With parameter j = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">j</span><span class="special">);</span>
77    <span class="special">}</span>
78  <span class="special">}</span>
79<span class="special">}</span>
80
81<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span><span class="identifier">test1</span><span class="special">)</span>
82<span class="special">{</span>
83  <span class="identifier">Processor</span> <span class="identifier">processor</span><span class="special">;</span>
84
85  <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">level</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">level</span> <span class="special">&lt;</span> <span class="number">3</span><span class="special">;</span> <span class="special">++</span><span class="identifier">level</span><span class="special">)</span> <span class="special">{</span>
86    <span class="identifier">processor</span><span class="special">.</span><span class="identifier">optimization_level</span><span class="special">(</span><span class="identifier">level</span><span class="special">);</span>
87    <span class="identifier">test_operations</span><span class="special">(</span><span class="identifier">processor</span><span class="special">,</span> <span class="number">2</span><span class="special">,</span> <span class="identifier">level</span><span class="special">);</span>
88  <span class="special">}</span>
89<span class="special">}</span>
90</pre>
91<p>
92          Note the length of the messages, the repetition, and the fact, that we
93          pass argument <code class="computeroutput"><span class="identifier">level</span></code> to
94          function <code class="computeroutput"><span class="identifier">test_operations</span></code>
95          only for the sake of generating an error message in case of a failure.
96        </p>
97<p>
98          Therefore, <span class="bold"><strong>loose</strong></span> coupling between the
99          context of an assertion and the assertion point is a property that is desirable.
100        </p>
101<a name="ref_BOOST_TEST_INFO"></a><h4>
102<a name="boost_test.test_output.test_tools_support_for_logging.contexts.h0"></a>
103          <span class="phrase"><a name="boost_test.test_output.test_tools_support_for_logging.contexts.assertion_bound_context"></a></span><a class="link" href="contexts.html#boost_test.test_output.test_tools_support_for_logging.contexts.assertion_bound_context">Assertion-bound
104          context</a>
105        </h4>
106<p>
107          <code class="computeroutput"><span class="identifier">BOOST_TEST_INFO</span></code> can be
108          used to define an error message to be bound to the first following assertion.
109          If (and only if) the assertion fails, the bound message will be displayed
110          along:
111        </p>
112<h6>
113<a name="boost_test.test_output.test_tools_support_for_logging.contexts.h1"></a>
114          <span class="phrase"><a name="boost_test.test_output.test_tools_support_for_logging.contexts.example_descr"></a></span><a class="link" href="contexts.html#boost_test.test_output.test_tools_support_for_logging.contexts.example_descr">Example:
115          Assertion-bound context</a>
116        </h6>
117<div class="informaltable"><table class="table">
118<colgroup><col></colgroup>
119<thead><tr><th>
120                  <p>
121                    Code
122                  </p>
123                </th></tr></thead>
124<tbody><tr><td>
125<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_TEST_MODULE</span> <span class="identifier">example80</span>
126<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">test</span><span class="special">/</span><span class="identifier">included</span><span class="special">/</span><span class="identifier">unit_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
127
128<span class="keyword">void</span> <span class="identifier">test</span><span class="special">()</span>
129<span class="special">{</span>
130  <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="keyword">false</span><span class="special">);</span>
131<span class="special">}</span>
132
133<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span><span class="identifier">test_case1</span><span class="special">)</span>
134<span class="special">{</span>
135  <span class="identifier">BOOST_TEST_INFO</span><span class="special">(</span><span class="string">"Alpha"</span><span class="special">);</span>
136  <span class="identifier">BOOST_TEST_INFO</span><span class="special">(</span><span class="string">"Beta"</span><span class="special">);</span>
137  <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="keyword">true</span><span class="special">);</span>
138
139  <span class="identifier">BOOST_TEST_INFO</span><span class="special">(</span><span class="string">"Gamma"</span><span class="special">);</span>
140  <span class="keyword">char</span> <span class="identifier">a</span> <span class="special">=</span> <span class="char">'a'</span><span class="special">;</span>
141  <span class="identifier">BOOST_TEST_INFO</span><span class="special">(</span><span class="string">"Delt"</span> <span class="special">&lt;&lt;</span> <span class="identifier">a</span><span class="special">);</span>
142  <span class="identifier">test</span><span class="special">();</span>
143<span class="special">}</span>
144</pre>
145                </td></tr></tbody>
146</table></div>
147<div class="informaltable"><table class="table">
148<colgroup><col></colgroup>
149<thead><tr><th>
150                  <p>
151                    Output
152                  </p>
153                </th></tr></thead>
154<tbody><tr><td>
155<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="special">&gt;</span> <span class="identifier">example</span>
156<span class="identifier">Running</span> <span class="number">1</span> <span class="identifier">test</span> <span class="keyword">case</span><span class="special">...</span>
157<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">14</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test_case1"</span><span class="special">:</span> <span class="identifier">check</span> <span class="keyword">false</span> <span class="identifier">has</span> <span class="identifier">failed</span>
158<span class="identifier">Failure</span> <span class="identifier">occurred</span> <span class="identifier">in</span> <span class="identifier">a</span> <span class="identifier">following</span> <span class="identifier">context</span><span class="special">:</span>
159    <span class="identifier">Gamma</span>
160    <span class="identifier">Delta</span>
161
162<span class="special">***</span> <span class="number">1</span> <span class="identifier">failures</span> <span class="identifier">is</span> <span class="identifier">detected</span> <span class="identifier">in</span> <span class="identifier">test</span> <span class="identifier">module</span> <span class="string">"example80"</span>
163</pre>
164                </td></tr></tbody>
165</table></div>
166<p>
167          The information composed inside <code class="computeroutput"><span class="identifier">BOOST_TEST_INFO</span></code>
168          is bound only to the first assertion following the declaration. This information
169          is only displayed if the assertion fails; otherwise the message is discarded.
170          The <code class="computeroutput"><span class="identifier">BOOST_TEST_INFO</span></code> declaration
171          does not have to immediately precede the assertion, it is allowed to intertwine
172          them with other instructions, they can even be declared in different scopes.
173          It is also possible to bind more than one information to a given assertion.
174        </p>
175<p>
176          With <code class="computeroutput"><span class="identifier">BOOST_TEST_INFO</span></code>, we
177          can improve our initial example as follows:
178        </p>
179<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">test_operations</span><span class="special">(</span><span class="identifier">Processor</span><span class="special">&amp;</span> <span class="identifier">processor</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">limit</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">level</span><span class="special">)</span>
180<span class="special">{</span>
181  <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">limit</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">{</span>
182    <span class="identifier">BOOST_TEST_INFO</span><span class="special">(</span><span class="string">"With optimization level "</span> <span class="special">&lt;&lt;</span> <span class="identifier">level</span><span class="special">);</span>
183    <span class="identifier">BOOST_TEST_INFO</span><span class="special">(</span><span class="string">"With parameter i = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span><span class="special">);</span>
184    <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">processor</span><span class="special">.</span><span class="identifier">op1</span><span class="special">(</span><span class="identifier">i</span><span class="special">));</span>
185    <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">j</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">j</span> <span class="special">&lt;</span> <span class="identifier">i</span><span class="special">;</span> <span class="special">++</span><span class="identifier">j</span><span class="special">)</span> <span class="special">{</span>
186      <span class="identifier">BOOST_TEST_INFO</span><span class="special">(</span><span class="string">"With optimization level "</span> <span class="special">&lt;&lt;</span> <span class="identifier">level</span><span class="special">);</span>
187      <span class="identifier">BOOST_TEST_INFO</span><span class="special">(</span><span class="string">"With parameter i = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span><span class="special">);</span>
188      <span class="identifier">BOOST_TEST_INFO</span><span class="special">(</span><span class="string">"With parameter j = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">j</span><span class="special">);</span>
189      <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">processor</span><span class="special">.</span><span class="identifier">op2</span><span class="special">(</span><span class="identifier">i</span><span class="special">,</span> <span class="identifier">j</span><span class="special">));</span>
190    <span class="special">}</span>
191  <span class="special">}</span>
192<span class="special">}</span>
193
194<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span><span class="identifier">test1</span><span class="special">)</span>
195<span class="special">{</span>
196  <span class="identifier">Processor</span> <span class="identifier">processor</span><span class="special">;</span>
197
198  <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">level</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">level</span> <span class="special">&lt;</span> <span class="number">3</span><span class="special">;</span> <span class="special">++</span><span class="identifier">level</span><span class="special">)</span> <span class="special">{</span>
199    <span class="identifier">processor</span><span class="special">.</span><span class="identifier">optimization_level</span><span class="special">(</span><span class="identifier">level</span><span class="special">);</span>
200    <span class="identifier">test_operations</span><span class="special">(</span><span class="identifier">processor</span><span class="special">,</span> <span class="number">2</span><span class="special">,</span> <span class="identifier">level</span><span class="special">);</span>
201  <span class="special">}</span>
202<span class="special">}</span>
203</pre>
204<a name="ref_BOOST_TEST_CONTEXT"></a><h4>
205<a name="boost_test.test_output.test_tools_support_for_logging.contexts.h2"></a>
206          <span class="phrase"><a name="boost_test.test_output.test_tools_support_for_logging.contexts.scope_bound_context"></a></span><a class="link" href="contexts.html#boost_test.test_output.test_tools_support_for_logging.contexts.scope_bound_context">Scope-bound
207          context</a>
208        </h4>
209<p>
210          In the previous example, the information stored inside the calls to <code class="computeroutput"><span class="identifier">BOOST_TEST_INFO</span></code> were all consumed by
211          the next assertion. There are cases where we would like this information
212          be persistent for the current scope. <span class="emphasis"><em>Unit Test Framework</em></span>
213          provides two tools to achieve this:
214        </p>
215<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
216<li class="listitem">
217              <code class="computeroutput"><span class="identifier">BOOST_TEST_CONTEXT</span></code>
218              defines a diagnostic message and a scope. The message is bound to every
219              assertion in that scope, and is displayed along with every failed assertion.
220            </li>
221<li class="listitem">
222              <code class="computeroutput"><span class="identifier">BOOST_TEST_INFO_SCOPE</span></code>
223              acts the same as <code class="computeroutput"><span class="identifier">BOOST_TEST_INFO</span></code>,
224              but the stored context information is bound to all the assertions that
225              follow the call to <code class="computeroutput"><span class="identifier">BOOST_TEST_INFO_SCOPE</span></code>
226              within the current scope.
227            </li>
228</ul></div>
229<div class="tip"><table border="0" summary="Tip">
230<tr>
231<td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="../../../../../../../doc/src/images/tip.png"></td>
232<th align="left">Tip</th>
233</tr>
234<tr><td align="left" valign="top"><p>
235            Since Boost <a class="link" href="../../change_log.html#ref_CHANGE_LOG_3_10">Boost 1.70</a>, <code class="computeroutput"><span class="identifier">BOOST_TEST_CONTEXT</span></code> can accept multiple
236            arguments.
237          </p></td></tr>
238</table></div>
239<div class="tip"><table border="0" summary="Tip">
240<tr>
241<td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="../../../../../../../doc/src/images/tip.png"></td>
242<th align="left">Tip</th>
243</tr>
244<tr><td align="left" valign="top"><p>
245            <code class="computeroutput"><span class="identifier">BOOST_TEST_INFO_SCOPE</span></code>
246            has been introduced in <a class="link" href="../../change_log.html#ref_CHANGE_LOG_3_10">Boost 1.70</a>.
247          </p></td></tr>
248</table></div>
249<h6>
250<a name="boost_test.test_output.test_tools_support_for_logging.contexts.h3"></a>
251          <span class="phrase"><a name="boost_test.test_output.test_tools_support_for_logging.contexts.example_descr0"></a></span><a class="link" href="contexts.html#boost_test.test_output.test_tools_support_for_logging.contexts.example_descr0">Example:
252          Scope-bound context</a>
253        </h6>
254<div class="informaltable"><table class="table">
255<colgroup><col></colgroup>
256<thead><tr><th>
257                  <p>
258                    Code
259                  </p>
260                </th></tr></thead>
261<tbody><tr><td>
262<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_TEST_MODULE</span> <span class="identifier">example81</span>
263<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">test</span><span class="special">/</span><span class="identifier">included</span><span class="special">/</span><span class="identifier">unit_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
264
265<span class="keyword">void</span> <span class="identifier">test</span><span class="special">()</span>
266<span class="special">{</span>
267  <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="number">2</span> <span class="special">!=</span> <span class="number">2</span><span class="special">);</span>
268<span class="special">}</span>
269
270<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span><span class="identifier">test_case1</span><span class="special">)</span>
271<span class="special">{</span>
272  <span class="identifier">BOOST_TEST_CONTEXT</span><span class="special">(</span><span class="string">"Alpha"</span><span class="special">)</span> <span class="special">{</span>
273    <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="number">1</span> <span class="special">!=</span> <span class="number">1</span><span class="special">);</span>
274    <span class="identifier">test</span><span class="special">();</span>
275
276    <span class="identifier">BOOST_TEST_CONTEXT</span><span class="special">(</span><span class="string">"Be"</span> <span class="special">&lt;&lt;</span> <span class="string">"ta"</span><span class="special">)</span>
277      <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="number">3</span> <span class="special">!=</span> <span class="number">3</span><span class="special">);</span>
278
279    <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="number">4</span> <span class="special">==</span> <span class="number">4</span><span class="special">);</span>
280  <span class="special">}</span>
281
282  <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="number">5</span> <span class="special">!=</span> <span class="number">5</span><span class="special">);</span>
283<span class="special">}</span>
284</pre>
285                </td></tr></tbody>
286</table></div>
287<div class="informaltable"><table class="table">
288<colgroup><col></colgroup>
289<thead><tr><th>
290                  <p>
291                    Output
292                  </p>
293                </th></tr></thead>
294<tbody><tr><td>
295<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="special">&gt;</span> <span class="identifier">example</span>
296<span class="identifier">Running</span> <span class="number">1</span> <span class="identifier">test</span> <span class="keyword">case</span><span class="special">...</span>
297<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">20</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test_case1"</span><span class="special">:</span> <span class="identifier">check</span> <span class="number">1</span> <span class="special">!=</span> <span class="number">1</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[</span><span class="number">1</span> <span class="special">==</span> <span class="number">1</span><span class="special">]</span>
298<span class="identifier">Failure</span> <span class="identifier">occurred</span> <span class="identifier">in</span> <span class="identifier">a</span> <span class="identifier">following</span> <span class="identifier">context</span><span class="special">:</span>
299    <span class="identifier">Alpha</span>
300<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">14</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test_case1"</span><span class="special">:</span> <span class="identifier">check</span> <span class="number">2</span> <span class="special">!=</span> <span class="number">2</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[</span><span class="number">2</span> <span class="special">==</span> <span class="number">2</span><span class="special">]</span>
301<span class="identifier">Failure</span> <span class="identifier">occurred</span> <span class="identifier">in</span> <span class="identifier">a</span> <span class="identifier">following</span> <span class="identifier">context</span><span class="special">:</span>
302    <span class="identifier">Alpha</span>
303<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">24</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test_case1"</span><span class="special">:</span> <span class="identifier">check</span> <span class="number">3</span> <span class="special">!=</span> <span class="number">3</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[</span><span class="number">3</span> <span class="special">==</span> <span class="number">3</span><span class="special">]</span>
304<span class="identifier">Failure</span> <span class="identifier">occurred</span> <span class="identifier">in</span> <span class="identifier">a</span> <span class="identifier">following</span> <span class="identifier">context</span><span class="special">:</span>
305    <span class="identifier">Alpha</span>
306    <span class="identifier">Beta</span>
307<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">29</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test_case1"</span><span class="special">:</span> <span class="identifier">check</span> <span class="number">5</span> <span class="special">!=</span> <span class="number">5</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[</span><span class="number">5</span> <span class="special">==</span> <span class="number">5</span><span class="special">]</span>
308
309<span class="special">***</span> <span class="number">4</span> <span class="identifier">failures</span> <span class="identifier">are</span> <span class="identifier">detected</span> <span class="identifier">in</span> <span class="identifier">test</span> <span class="identifier">module</span> <span class="string">"example81"</span>
310</pre>
311                </td></tr></tbody>
312</table></div>
313<p>
314          In the previous example, there is an opening brace right after <code class="computeroutput"><span class="identifier">BOOST_TEST_CONTEXT</span></code>: this pair of braces
315          defines the scope in which the diagnostic message is in effect. If there
316          is no braces, the scope applies only to the following statement. <code class="computeroutput"><span class="identifier">BOOST_TEST_CONTEXT</span></code> declarations can nest.
317        </p>
318<p>
319          With <code class="computeroutput"><span class="identifier">BOOST_TEST_CONTEXT</span></code>,
320          we can further improve our initial example, by putting variable <code class="computeroutput"><span class="identifier">level</span></code> into a scope-level context and
321          not pass it as function parameter:
322        </p>
323<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">test_operations</span><span class="special">(</span><span class="identifier">Processor</span><span class="special">&amp;</span> <span class="identifier">processor</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">limit</span><span class="special">)</span>
324<span class="special">{</span>
325  <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">limit</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">{</span>
326    <span class="identifier">BOOST_TEST_INFO</span><span class="special">(</span><span class="string">"With parameter i = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span><span class="special">);</span>
327    <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">processor</span><span class="special">.</span><span class="identifier">op1</span><span class="special">(</span><span class="identifier">i</span><span class="special">));</span>
328    <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">j</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">j</span> <span class="special">&lt;</span> <span class="identifier">i</span><span class="special">;</span> <span class="special">++</span><span class="identifier">j</span><span class="special">)</span> <span class="special">{</span>
329      <span class="identifier">BOOST_TEST_INFO</span><span class="special">(</span><span class="string">"With parameter i = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span><span class="special">);</span>
330      <span class="identifier">BOOST_TEST_INFO</span><span class="special">(</span><span class="string">"With parameter j = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">j</span><span class="special">);</span>
331      <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">processor</span><span class="special">.</span><span class="identifier">op2</span><span class="special">(</span><span class="identifier">i</span><span class="special">,</span> <span class="identifier">j</span><span class="special">));</span>
332    <span class="special">}</span>
333  <span class="special">}</span>
334<span class="special">}</span>
335
336<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span><span class="identifier">test1</span><span class="special">)</span>
337<span class="special">{</span>
338  <span class="identifier">Processor</span> <span class="identifier">processor</span><span class="special">;</span>
339
340  <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">level</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">level</span> <span class="special">&lt;</span> <span class="number">3</span><span class="special">;</span> <span class="special">++</span><span class="identifier">level</span><span class="special">)</span> <span class="special">{</span>
341    <span class="identifier">BOOST_TEST_CONTEXT</span><span class="special">(</span><span class="string">"With optimization level "</span> <span class="special">&lt;&lt;</span> <span class="identifier">level</span><span class="special">)</span> <span class="special">{</span>
342      <span class="identifier">processor</span><span class="special">.</span><span class="identifier">optimization_level</span><span class="special">(</span><span class="identifier">level</span><span class="special">);</span>
343      <span class="identifier">test_operations</span><span class="special">(</span><span class="identifier">processor</span><span class="special">,</span> <span class="number">2</span><span class="special">);</span>
344    <span class="special">}</span>
345  <span class="special">}</span>
346<span class="special">}</span>
347</pre>
348<p>
349          If we observe that variable <code class="computeroutput"><span class="identifier">i</span></code>
350          also applies in a certain scope, we can improve our example further still.
351        </p>
352<h6>
353<a name="boost_test.test_output.test_tools_support_for_logging.contexts.h4"></a>
354          <span class="phrase"><a name="boost_test.test_output.test_tools_support_for_logging.contexts.example_descr1"></a></span><a class="link" href="contexts.html#boost_test.test_output.test_tools_support_for_logging.contexts.example_descr1">Example:
355          Using contexts</a>
356        </h6>
357<div class="informaltable"><table class="table">
358<colgroup><col></colgroup>
359<thead><tr><th>
360                  <p>
361                    Code
362                  </p>
363                </th></tr></thead>
364<tbody><tr><td>
365<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_TEST_MODULE</span> <span class="identifier">example82</span>
366<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">test</span><span class="special">/</span><span class="identifier">included</span><span class="special">/</span><span class="identifier">unit_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
367
368<span class="keyword">struct</span> <span class="identifier">Processor</span>
369<span class="special">{</span>
370  <span class="keyword">int</span> <span class="identifier">level</span><span class="special">;</span>
371  <span class="keyword">void</span> <span class="identifier">optimization_level</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">l</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">level</span> <span class="special">=</span> <span class="identifier">l</span><span class="special">;</span> <span class="special">}</span>
372  <span class="keyword">bool</span> <span class="identifier">op1</span><span class="special">(</span><span class="keyword">int</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">level</span> <span class="special">&lt;</span> <span class="number">2</span><span class="special">;</span> <span class="special">}</span>
373  <span class="keyword">bool</span> <span class="identifier">op2</span><span class="special">(</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">int</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">level</span> <span class="special">&lt;</span> <span class="number">1</span><span class="special">;</span> <span class="special">}</span>
374<span class="special">};</span>
375
376<span class="keyword">void</span> <span class="identifier">test_operations</span><span class="special">(</span><span class="identifier">Processor</span><span class="special">&amp;</span> <span class="identifier">processor</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">limit</span><span class="special">)</span>
377<span class="special">{</span>
378  <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">limit</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">{</span>
379    <span class="identifier">BOOST_TEST_CONTEXT</span><span class="special">(</span><span class="string">"With parameter i = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span><span class="special">)</span> <span class="special">{</span>
380      <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">processor</span><span class="special">.</span><span class="identifier">op1</span><span class="special">(</span><span class="identifier">i</span><span class="special">));</span>
381      <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">j</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">j</span> <span class="special">&lt;</span> <span class="identifier">i</span><span class="special">;</span> <span class="special">++</span><span class="identifier">j</span><span class="special">)</span> <span class="special">{</span>
382        <span class="identifier">BOOST_TEST_INFO</span><span class="special">(</span><span class="string">"With parameter j = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">j</span><span class="special">);</span>
383        <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">processor</span><span class="special">.</span><span class="identifier">op2</span><span class="special">(</span><span class="identifier">i</span><span class="special">,</span> <span class="identifier">j</span><span class="special">));</span>
384      <span class="special">}</span>
385    <span class="special">}</span>
386  <span class="special">}</span>
387<span class="special">}</span>
388
389<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span><span class="identifier">test1</span><span class="special">)</span>
390<span class="special">{</span>
391  <span class="identifier">Processor</span> <span class="identifier">processor</span><span class="special">;</span>
392
393  <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">level</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">level</span> <span class="special">&lt;</span> <span class="number">3</span><span class="special">;</span> <span class="special">++</span><span class="identifier">level</span><span class="special">)</span> <span class="special">{</span>
394    <span class="identifier">BOOST_TEST_CONTEXT</span><span class="special">(</span><span class="string">"With optimization level "</span> <span class="special">&lt;&lt;</span> <span class="identifier">level</span><span class="special">)</span> <span class="special">{</span>
395      <span class="identifier">processor</span><span class="special">.</span><span class="identifier">optimization_level</span><span class="special">(</span><span class="identifier">level</span><span class="special">);</span>
396      <span class="identifier">test_operations</span><span class="special">(</span><span class="identifier">processor</span><span class="special">,</span> <span class="number">2</span><span class="special">);</span>
397    <span class="special">}</span>
398  <span class="special">}</span>
399<span class="special">}</span>
400</pre>
401                </td></tr></tbody>
402</table></div>
403<div class="informaltable"><table class="table">
404<colgroup><col></colgroup>
405<thead><tr><th>
406                  <p>
407                    Output
408                  </p>
409                </th></tr></thead>
410<tbody><tr><td>
411<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="special">&gt;</span> <span class="identifier">example</span>
412<span class="identifier">Running</span> <span class="number">1</span> <span class="identifier">test</span> <span class="keyword">case</span><span class="special">...</span>
413<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">27</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test1"</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">processor</span><span class="special">.</span><span class="identifier">op2</span><span class="special">(</span><span class="identifier">i</span><span class="special">,</span> <span class="identifier">j</span><span class="special">)</span> <span class="identifier">has</span> <span class="identifier">failed</span>
414<span class="identifier">Failure</span> <span class="identifier">occurred</span> <span class="identifier">in</span> <span class="identifier">a</span> <span class="identifier">following</span> <span class="identifier">context</span><span class="special">:</span>
415    <span class="identifier">With</span> <span class="identifier">optimization</span> <span class="identifier">level</span> <span class="number">1</span>
416    <span class="identifier">With</span> <span class="identifier">parameter</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">1</span>
417    <span class="identifier">With</span> <span class="identifier">parameter</span> <span class="identifier">j</span> <span class="special">=</span> <span class="number">0</span>
418<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">24</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test1"</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">processor</span><span class="special">.</span><span class="identifier">op1</span><span class="special">(</span><span class="identifier">i</span><span class="special">)</span> <span class="identifier">has</span> <span class="identifier">failed</span>
419<span class="identifier">Failure</span> <span class="identifier">occurred</span> <span class="identifier">in</span> <span class="identifier">a</span> <span class="identifier">following</span> <span class="identifier">context</span><span class="special">:</span>
420    <span class="identifier">With</span> <span class="identifier">optimization</span> <span class="identifier">level</span> <span class="number">2</span>
421    <span class="identifier">With</span> <span class="identifier">parameter</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span>
422<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">24</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test1"</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">processor</span><span class="special">.</span><span class="identifier">op1</span><span class="special">(</span><span class="identifier">i</span><span class="special">)</span> <span class="identifier">has</span> <span class="identifier">failed</span>
423<span class="identifier">Failure</span> <span class="identifier">occurred</span> <span class="identifier">in</span> <span class="identifier">a</span> <span class="identifier">following</span> <span class="identifier">context</span><span class="special">:</span>
424    <span class="identifier">With</span> <span class="identifier">optimization</span> <span class="identifier">level</span> <span class="number">2</span>
425    <span class="identifier">With</span> <span class="identifier">parameter</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">1</span>
426<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">27</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test1"</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">processor</span><span class="special">.</span><span class="identifier">op2</span><span class="special">(</span><span class="identifier">i</span><span class="special">,</span> <span class="identifier">j</span><span class="special">)</span> <span class="identifier">has</span> <span class="identifier">failed</span>
427<span class="identifier">Failure</span> <span class="identifier">occurred</span> <span class="identifier">in</span> <span class="identifier">a</span> <span class="identifier">following</span> <span class="identifier">context</span><span class="special">:</span>
428    <span class="identifier">With</span> <span class="identifier">optimization</span> <span class="identifier">level</span> <span class="number">2</span>
429    <span class="identifier">With</span> <span class="identifier">parameter</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">1</span>
430    <span class="identifier">With</span> <span class="identifier">parameter</span> <span class="identifier">j</span> <span class="special">=</span> <span class="number">0</span>
431
432<span class="special">***</span> <span class="number">4</span> <span class="identifier">failures</span> <span class="identifier">are</span> <span class="identifier">detected</span> <span class="identifier">in</span> <span class="identifier">the</span> <span class="identifier">test</span> <span class="identifier">module</span> <span class="string">"example82"</span>
433</pre>
434                </td></tr></tbody>
435</table></div>
436<p>
437          Finally, it is possible to pass several arguments to <code class="computeroutput"><span class="identifier">BOOST_TEST_CONTEXT</span></code>,
438          which is more convenient than having several scopes:
439        </p>
440<h6>
441<a name="boost_test.test_output.test_tools_support_for_logging.contexts.h5"></a>
442          <span class="phrase"><a name="boost_test.test_output.test_tools_support_for_logging.contexts.example_descr2"></a></span><a class="link" href="contexts.html#boost_test.test_output.test_tools_support_for_logging.contexts.example_descr2">Example:
443          Multiple arguments to <code class="computeroutput"><span class="identifier">BOOST_TEST_CONTEXT</span></code></a>
444        </h6>
445<div class="informaltable"><table class="table">
446<colgroup><col></colgroup>
447<thead><tr><th>
448                  <p>
449                    Code
450                  </p>
451                </th></tr></thead>
452<tbody><tr><td>
453<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_TEST_MODULE</span> <span class="identifier">example83</span> <span class="identifier">multicontext</span>
454<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">test</span><span class="special">/</span><span class="identifier">included</span><span class="special">/</span><span class="identifier">unit_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
455<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cmath</span><span class="special">&gt;</span>
456
457<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span><span class="identifier">test_multi_context</span><span class="special">)</span>
458<span class="special">{</span>
459  <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">level</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">level</span> <span class="special">&lt;</span> <span class="number">10</span><span class="special">;</span> <span class="special">++</span><span class="identifier">level</span><span class="special">)</span> <span class="special">{</span>
460    <span class="keyword">int</span> <span class="identifier">rand_value</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">abs</span><span class="special">(</span><span class="identifier">rand</span><span class="special">())</span> <span class="special">%</span> <span class="number">50</span><span class="special">;</span>
461    <span class="identifier">BOOST_TEST_CONTEXT</span><span class="special">(</span><span class="string">"With level "</span> <span class="special">&lt;&lt;</span> <span class="identifier">level</span><span class="special">,</span> <span class="string">"Random value="</span> <span class="special">&lt;&lt;</span> <span class="identifier">rand_value</span><span class="special">){</span>
462      <span class="keyword">for</span><span class="special">(</span> <span class="keyword">int</span> <span class="identifier">j</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span> <span class="identifier">j</span> <span class="special">&lt;</span> <span class="identifier">rand_value</span><span class="special">;</span> <span class="identifier">j</span><span class="special">++)</span> <span class="special">{</span>
463        <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">level</span> <span class="special">&lt;</span> <span class="identifier">rand_value</span><span class="special">);</span>
464        <span class="identifier">rand_value</span> <span class="special">/=</span> <span class="number">2</span><span class="special">;</span>
465      <span class="special">}</span>
466    <span class="special">}</span>
467  <span class="special">}</span>
468<span class="special">}</span>
469</pre>
470                </td></tr></tbody>
471</table></div>
472<div class="informaltable"><table class="table">
473<colgroup><col></colgroup>
474<thead><tr><th>
475                  <p>
476                    Output
477                  </p>
478                </th></tr></thead>
479<tbody><tr><td>
480<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="special">&gt;</span> <span class="identifier">example</span>
481<span class="identifier">Running</span> <span class="number">1</span> <span class="identifier">test</span> <span class="keyword">case</span><span class="special">...</span>
482<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">19</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test_multi_context"</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">level</span> <span class="special">&lt;</span> <span class="identifier">rand_value</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[</span><span class="number">7</span> <span class="special">&gt;=</span> <span class="number">5</span><span class="special">]</span>
483<span class="identifier">Failure</span> <span class="identifier">occurred</span> <span class="identifier">in</span> <span class="identifier">a</span> <span class="identifier">following</span> <span class="identifier">context</span><span class="special">:</span>
484    <span class="identifier">With</span> <span class="identifier">level</span> <span class="number">7</span>
485    <span class="identifier">Random</span> <span class="identifier">value</span><span class="special">=</span><span class="number">42</span>
486<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">19</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test_multi_context"</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">level</span> <span class="special">&lt;</span> <span class="identifier">rand_value</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[</span><span class="number">8</span> <span class="special">&gt;=</span> <span class="number">6</span><span class="special">]</span>
487<span class="identifier">Failure</span> <span class="identifier">occurred</span> <span class="identifier">in</span> <span class="identifier">a</span> <span class="identifier">following</span> <span class="identifier">context</span><span class="special">:</span>
488    <span class="identifier">With</span> <span class="identifier">level</span> <span class="number">8</span>
489    <span class="identifier">Random</span> <span class="identifier">value</span><span class="special">=</span><span class="number">49</span>
490<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">19</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test_multi_context"</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">level</span> <span class="special">&lt;</span> <span class="identifier">rand_value</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[</span><span class="number">9</span> <span class="special">&gt;=</span> <span class="number">5</span><span class="special">]</span>
491<span class="identifier">Failure</span> <span class="identifier">occurred</span> <span class="identifier">in</span> <span class="identifier">a</span> <span class="identifier">following</span> <span class="identifier">context</span><span class="special">:</span>
492    <span class="identifier">With</span> <span class="identifier">level</span> <span class="number">9</span>
493    <span class="identifier">Random</span> <span class="identifier">value</span><span class="special">=</span><span class="number">21</span>
494
495<span class="special">***</span> <span class="number">3</span> <span class="identifier">failures</span> <span class="identifier">are</span> <span class="identifier">detected</span> <span class="identifier">in</span> <span class="identifier">the</span> <span class="identifier">test</span> <span class="identifier">module</span> <span class="string">"example83 multicontext"</span>
496</pre>
497                </td></tr></tbody>
498</table></div>
499<p>
500          <code class="computeroutput"><span class="identifier">BOOST_TEST_INFO_SCOPE</span></code> is
501          convenient when you augment the current scope information as new information
502          arrives. The following example calls several time a quadratic polynomial
503          estimation function with random polynomial. As the random values are drawn
504          in a loop, they are placed in the current scope with <code class="computeroutput"><span class="identifier">BOOST_TEST_INFO_SCOPE</span></code>,
505          which allows us to easily debug the function.
506        </p>
507<h6>
508<a name="boost_test.test_output.test_tools_support_for_logging.contexts.h6"></a>
509          <span class="phrase"><a name="boost_test.test_output.test_tools_support_for_logging.contexts.example_descr3"></a></span><a class="link" href="contexts.html#boost_test.test_output.test_tools_support_for_logging.contexts.example_descr3">Example:
510          Sticky context with <code class="computeroutput"><span class="identifier">BOOST_TEST_INFO_SCOPE</span></code></a>
511        </h6>
512<div class="informaltable"><table class="table">
513<colgroup><col></colgroup>
514<thead><tr><th>
515                  <p>
516                    Code
517                  </p>
518                </th></tr></thead>
519<tbody><tr><td>
520<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_TEST_MODULE</span> <span class="identifier">example84</span>
521<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">test</span><span class="special">/</span><span class="identifier">included</span><span class="special">/</span><span class="identifier">unit_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
522<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">random</span><span class="special">&gt;</span>
523<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cmath</span><span class="special">&gt;</span>
524
525<span class="comment">// this function does not compute properly the polynomial root estimation</span>
526<span class="comment">// in the case of a double root.</span>
527<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">random_generator_t</span><span class="special">&gt;</span>
528<span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">,</span> <span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">estimate_polynomial_roots</span><span class="special">(</span>
529  <span class="identifier">random_generator_t</span><span class="special">&amp;</span> <span class="identifier">gen</span><span class="special">,</span>
530  <span class="identifier">std</span><span class="special">::</span><span class="identifier">function</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">(</span><span class="keyword">double</span><span class="special">)&gt;</span> <span class="identifier">polynomial</span><span class="special">)</span> <span class="special">{</span>
531
532  <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">std</span><span class="special">;</span>
533
534  <span class="identifier">std</span><span class="special">::</span><span class="identifier">uniform_real_distribution</span><span class="special">&lt;&gt;</span> <span class="identifier">dis</span><span class="special">(-</span><span class="number">10</span><span class="special">,</span> <span class="number">10</span><span class="special">);</span>
535  <span class="keyword">double</span> <span class="identifier">x1</span> <span class="special">=</span> <span class="identifier">dis</span><span class="special">(</span><span class="identifier">gen</span><span class="special">);</span>
536  <span class="keyword">double</span> <span class="identifier">x2</span> <span class="special">=</span> <span class="identifier">dis</span><span class="special">(</span><span class="identifier">gen</span><span class="special">);</span>
537  <span class="keyword">double</span> <span class="identifier">fx1</span> <span class="special">=</span> <span class="identifier">polynomial</span><span class="special">(</span><span class="identifier">x1</span><span class="special">);</span>
538  <span class="keyword">double</span> <span class="identifier">fx2</span> <span class="special">=</span> <span class="identifier">polynomial</span><span class="special">(</span><span class="identifier">x2</span><span class="special">);</span>
539
540  <span class="identifier">BOOST_TEST_INFO_SCOPE</span><span class="special">(</span><span class="string">"sample1 = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">x1</span><span class="special">);</span>
541  <span class="identifier">BOOST_TEST_INFO_SCOPE</span><span class="special">(</span><span class="string">"sample2 = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">x2</span><span class="special">);</span>
542
543  <span class="comment">// from Vieta formula</span>
544  <span class="keyword">double</span> <span class="identifier">minus_b</span> <span class="special">=</span> <span class="identifier">x2</span> <span class="special">+</span> <span class="identifier">x1</span> <span class="special">-</span> <span class="special">(</span><span class="identifier">fx2</span> <span class="special">-</span> <span class="identifier">fx1</span><span class="special">)</span> <span class="special">/</span> <span class="special">(</span><span class="identifier">x2</span> <span class="special">-</span> <span class="identifier">x1</span><span class="special">);</span>
545  <span class="keyword">double</span> <span class="identifier">c</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">x1</span> <span class="special">*</span> <span class="identifier">fx2</span> <span class="special">-</span> <span class="identifier">x2</span> <span class="special">*</span> <span class="identifier">fx1</span> <span class="special">+</span> <span class="identifier">x2</span> <span class="special">*</span> <span class="identifier">x1</span> <span class="special">*</span> <span class="identifier">x1</span> <span class="special">-</span> <span class="identifier">x1</span> <span class="special">*</span> <span class="identifier">x2</span> <span class="special">*</span> <span class="identifier">x2</span><span class="special">)</span> <span class="special">/</span> <span class="special">(</span><span class="identifier">x1</span> <span class="special">-</span> <span class="identifier">x2</span><span class="special">);</span>
546
547  <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">minus_b</span> <span class="special">*</span> <span class="identifier">minus_b</span> <span class="special">&gt;=</span> <span class="number">4</span><span class="special">*</span><span class="identifier">c</span><span class="special">);</span>
548
549  <span class="keyword">return</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">make_pair</span><span class="special">(</span>
550    <span class="special">(</span><span class="identifier">minus_b</span> <span class="special">-</span> <span class="identifier">sqrt</span><span class="special">(</span><span class="identifier">minus_b</span> <span class="special">*</span> <span class="identifier">minus_b</span> <span class="special">-</span> <span class="number">4</span> <span class="special">*</span> <span class="identifier">c</span><span class="special">))</span> <span class="special">/</span> <span class="number">2</span><span class="special">,</span>
551    <span class="special">(</span><span class="identifier">minus_b</span> <span class="special">+</span> <span class="identifier">sqrt</span><span class="special">(</span><span class="identifier">minus_b</span> <span class="special">*</span> <span class="identifier">minus_b</span> <span class="special">-</span> <span class="number">4</span> <span class="special">*</span> <span class="identifier">c</span><span class="special">))</span> <span class="special">/</span> <span class="number">2</span><span class="special">);</span>
552<span class="special">}</span>
553
554<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span><span class="identifier">quadratic_estimation</span><span class="special">)</span>
555<span class="special">{</span>
556  <span class="identifier">std</span><span class="special">::</span><span class="identifier">random_device</span> <span class="identifier">rd</span><span class="special">;</span>
557  <span class="keyword">unsigned</span> <span class="keyword">int</span> <span class="identifier">seed</span> <span class="special">=</span> <span class="identifier">rd</span><span class="special">();</span>
558  <span class="identifier">std</span><span class="special">::</span><span class="identifier">mt19937</span> <span class="identifier">gen</span><span class="special">(</span><span class="identifier">seed</span><span class="special">);</span>
559  <span class="identifier">std</span><span class="special">::</span><span class="identifier">uniform_int_distribution</span><span class="special">&lt;&gt;</span> <span class="identifier">dis</span><span class="special">(-</span><span class="number">10</span><span class="special">,</span> <span class="number">10</span><span class="special">);</span>
560
561  <span class="identifier">BOOST_TEST_MESSAGE</span><span class="special">(</span><span class="string">"Seed = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">seed</span><span class="special">);</span>
562
563  <span class="keyword">for</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="number">50</span><span class="special">;</span> <span class="identifier">i</span><span class="special">++)</span> <span class="special">{</span>
564    <span class="identifier">BOOST_TEST_INFO_SCOPE</span><span class="special">(</span><span class="string">"trial "</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span><span class="special">+</span><span class="number">1</span><span class="special">);</span>
565    <span class="keyword">int</span> <span class="identifier">root1</span> <span class="special">=</span> <span class="identifier">dis</span><span class="special">(</span><span class="identifier">gen</span><span class="special">);</span>
566    <span class="keyword">int</span> <span class="identifier">root2</span> <span class="special">=</span> <span class="identifier">dis</span><span class="special">(</span><span class="identifier">gen</span><span class="special">);</span>
567    <span class="keyword">if</span><span class="special">(</span><span class="identifier">root1</span> <span class="special">&gt;</span> <span class="identifier">root2</span><span class="special">)</span> <span class="special">{</span>
568      <span class="identifier">std</span><span class="special">::</span><span class="identifier">swap</span><span class="special">(</span><span class="identifier">root1</span><span class="special">,</span> <span class="identifier">root2</span><span class="special">);</span>
569    <span class="special">}</span>
570    <span class="identifier">BOOST_TEST_INFO_SCOPE</span><span class="special">(</span><span class="string">"root1 = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">root1</span><span class="special">);</span>
571    <span class="identifier">BOOST_TEST_INFO_SCOPE</span><span class="special">(</span><span class="string">"root2 = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">root2</span><span class="special">);</span>
572
573    <span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">,</span> <span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">estimated</span> <span class="special">=</span> <span class="identifier">estimate_polynomial_roots</span><span class="special">(</span>
574      <span class="identifier">gen</span><span class="special">,</span>
575      <span class="special">[</span><span class="identifier">root1</span><span class="special">,</span> <span class="identifier">root2</span><span class="special">](</span><span class="keyword">double</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">-&gt;</span> <span class="keyword">double</span> <span class="special">{</span> <span class="keyword">return</span> <span class="special">(</span><span class="identifier">x</span> <span class="special">-</span> <span class="identifier">root1</span><span class="special">)</span> <span class="special">*</span> <span class="special">(</span><span class="identifier">x</span> <span class="special">-</span> <span class="identifier">root2</span><span class="special">);</span> <span class="special">});</span>
576
577    <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">estimated</span><span class="special">.</span><span class="identifier">first</span> <span class="special">==</span> <span class="keyword">double</span><span class="special">(</span><span class="identifier">root1</span><span class="special">),</span> <span class="number">10.</span> <span class="special">%</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">test_tools</span><span class="special">::</span><span class="identifier">tolerance</span><span class="special">());</span>
578    <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">estimated</span><span class="special">.</span><span class="identifier">second</span> <span class="special">==</span> <span class="keyword">double</span><span class="special">(</span><span class="identifier">root2</span><span class="special">),</span> <span class="number">10.</span> <span class="special">%</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">test_tools</span><span class="special">::</span><span class="identifier">tolerance</span><span class="special">());</span>
579  <span class="special">}</span>
580<span class="special">}</span>
581</pre>
582                </td></tr></tbody>
583</table></div>
584<div class="informaltable"><table class="table">
585<colgroup><col></colgroup>
586<thead><tr><th>
587                  <p>
588                    Output
589                  </p>
590                </th></tr></thead>
591<tbody><tr><td>
592<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="special">&gt;</span> <span class="identifier">example</span> <span class="special">--</span><span class="identifier">log_level</span><span class="special">=</span><span class="identifier">message</span>
593<span class="identifier">Seed</span> <span class="special">=</span> <span class="number">162981956</span>
594<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">34</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"quadratic_estimation"</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">minus_b</span> <span class="special">*</span> <span class="identifier">minus_b</span> <span class="special">&gt;=</span> <span class="number">4</span><span class="special">*</span><span class="identifier">c</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[-</span><span class="number">13.999999999999998</span> <span class="special">*</span> <span class="special">-</span><span class="number">13.999999999999998</span> <span class="special">&lt;</span> <span class="number">195.99999999999997</span><span class="special">]</span>
595<span class="identifier">Failure</span> <span class="identifier">occurred</span> <span class="identifier">in</span> <span class="identifier">a</span> <span class="identifier">following</span> <span class="identifier">context</span><span class="special">:</span>
596    <span class="identifier">trial</span> <span class="number">14</span>
597    <span class="identifier">root1</span> <span class="special">=</span> <span class="special">-</span><span class="number">7</span>
598    <span class="identifier">root2</span> <span class="special">=</span> <span class="special">-</span><span class="number">7</span>
599    <span class="identifier">sample1</span> <span class="special">=</span> <span class="number">4.66289</span>
600    <span class="identifier">sample2</span> <span class="special">=</span> <span class="number">1.70234</span>
601<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">64</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"quadratic_estimation"</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">estimated</span><span class="special">.</span><span class="identifier">first</span> <span class="special">==</span> <span class="keyword">double</span><span class="special">(</span><span class="identifier">root1</span><span class="special">)</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[-</span><span class="identifier">nan</span> <span class="special">!=</span> <span class="special">-</span><span class="number">7</span><span class="special">].</span> <span class="identifier">Relative</span> <span class="identifier">difference</span> <span class="identifier">exceeds</span> <span class="identifier">tolerance</span> <span class="special">[-</span><span class="identifier">nan</span> <span class="special">&gt;</span> <span class="number">0.1</span><span class="special">]</span>
602<span class="identifier">Failure</span> <span class="identifier">occurred</span> <span class="identifier">in</span> <span class="identifier">a</span> <span class="identifier">following</span> <span class="identifier">context</span><span class="special">:</span>
603    <span class="identifier">trial</span> <span class="number">14</span>
604    <span class="identifier">root1</span> <span class="special">=</span> <span class="special">-</span><span class="number">7</span>
605    <span class="identifier">root2</span> <span class="special">=</span> <span class="special">-</span><span class="number">7</span>
606<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">65</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"quadratic_estimation"</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">estimated</span><span class="special">.</span><span class="identifier">second</span> <span class="special">==</span> <span class="keyword">double</span><span class="special">(</span><span class="identifier">root2</span><span class="special">)</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[-</span><span class="identifier">nan</span> <span class="special">!=</span> <span class="special">-</span><span class="number">7</span><span class="special">].</span> <span class="identifier">Relative</span> <span class="identifier">difference</span> <span class="identifier">exceeds</span> <span class="identifier">tolerance</span> <span class="special">[-</span><span class="identifier">nan</span> <span class="special">&gt;</span> <span class="number">0.1</span><span class="special">]</span>
607<span class="identifier">Failure</span> <span class="identifier">occurred</span> <span class="identifier">in</span> <span class="identifier">a</span> <span class="identifier">following</span> <span class="identifier">context</span><span class="special">:</span>
608    <span class="identifier">trial</span> <span class="number">14</span>
609    <span class="identifier">root1</span> <span class="special">=</span> <span class="special">-</span><span class="number">7</span>
610    <span class="identifier">root2</span> <span class="special">=</span> <span class="special">-</span><span class="number">7</span>
611
612<span class="special">***</span> <span class="number">3</span> <span class="identifier">failures</span> <span class="identifier">are</span> <span class="identifier">detected</span> <span class="identifier">in</span> <span class="identifier">the</span> <span class="identifier">test</span> <span class="identifier">module</span> <span class="string">"example84"</span>
613</pre>
614                </td></tr></tbody>
615</table></div>
616</div>
617<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
618<td align="left"></td>
619<td align="right"><div class="copyright-footer">Copyright © 2001-2020 Boost.Test contributors<p>
620        Distributed under the Boost Software License, Version 1.0. (See accompanying
621        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
622      </p>
623</div></td>
624</tr></table>
625<hr>
626<div class="spirit-nav">
627<a accesskey="p" href="checkpoints.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../test_tools_support_for_logging.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="log_floating_points.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
628</div>
629</body>
630</html>
631