mirror of
https://github.com/boostorg/test.git
synced 2026-02-19 14:52:09 +00:00
176 lines
15 KiB
HTML
176 lines
15 KiB
HTML
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
|
|
<title>Example</title>
|
|
<link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css">
|
|
<meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
|
|
<link rel="home" href="../../index.html" title="Boost.Test">
|
|
<link rel="up" href="../bt_minimal_testing.html" title="Minimal testing facility">
|
|
<link rel="prev" href="bt_minimal.html" title="Testing tools">
|
|
<link rel="next" href="../runtime_configuration_or_what_ar.html" title="Runtime configuration ... or what are the strings I can pull?">
|
|
</head>
|
|
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
|
<table cellpadding="2" width="100%"><tr>
|
|
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
|
|
<td align="center"><a href="../../../../../../../index.html">Home</a></td>
|
|
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
|
|
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
|
|
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
|
|
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
|
|
</tr></table>
|
|
<hr>
|
|
<div class="spirit-nav">
|
|
<a accesskey="p" href="bt_minimal.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../bt_minimal_testing.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="../runtime_configuration_or_what_ar.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h3 class="title">
|
|
<a name="boost_test.bt_minimal_testing.example"></a><a class="link" href="example.html" title="Example">Example</a>
|
|
</h3></div></div></div>
|
|
<p>
|
|
The following example illustrates different approaches you can employ to
|
|
detect and report errors using different testing tools
|
|
</p>
|
|
<div class="table">
|
|
<a name="boost_test.bt_minimal_testing.example.id_example27"></a><p class="title"><b>Table 57. Minimal testing facility application</b></p>
|
|
<div class="table-contents"><table class="table" summary="Minimal testing facility application">
|
|
<colgroup>
|
|
<col>
|
|
<col>
|
|
</colgroup>
|
|
<thead><tr>
|
|
<th>
|
|
<p>
|
|
Code
|
|
</p>
|
|
</th>
|
|
<th>
|
|
<p>
|
|
Output
|
|
</p>
|
|
</th>
|
|
</tr></thead>
|
|
<tbody><tr>
|
|
<td>
|
|
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">test</span><span class="special">/</span><span class="identifier">minimal</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
|
|
|
<span class="comment">//____________________________________________________________________________//</span>
|
|
|
|
<span class="keyword">int</span> <span class="identifier">add</span><span class="special">(</span> <span class="keyword">int</span> <span class="identifier">i</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">j</span> <span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">i</span><span class="special">+</span><span class="identifier">j</span><span class="special">;</span> <span class="special">}</span>
|
|
|
|
<span class="comment">//____________________________________________________________________________//</span>
|
|
|
|
<span class="keyword">int</span> <span class="identifier">test_main</span><span class="special">(</span> <span class="keyword">int</span><span class="special">,</span> <span class="keyword">char</span> <span class="special">*[]</span> <span class="special">)</span> <span class="comment">// note the name!</span>
|
|
<span class="special">{</span>
|
|
<span class="comment">// six ways to detect and report the same error:</span>
|
|
|
|
<span class="comment">// continues on error</span>
|
|
<span class="identifier">BOOST_CHECK</span><span class="special">(</span> <span class="identifier">add</span><span class="special">(</span> <span class="number">2</span><span class="special">,</span><span class="number">2</span> <span class="special">)</span> <span class="special">==</span> <span class="number">4</span> <span class="special">);</span> <a class="co" name="boost_test.bt_minimal_testing.example.c0" href="example.html#boost_test.bt_minimal_testing.example.c1"><img src="../../../../../../../doc/src/images/callouts/1.png" alt="1" border="0"></a>
|
|
|
|
<span class="comment">// throws on error</span>
|
|
<span class="identifier">BOOST_REQUIRE</span><span class="special">(</span> <span class="identifier">add</span><span class="special">(</span> <span class="number">2</span><span class="special">,</span><span class="number">2</span> <span class="special">)</span> <span class="special">==</span> <span class="number">4</span> <span class="special">);</span> <a class="co" name="boost_test.bt_minimal_testing.example.c2" href="example.html#boost_test.bt_minimal_testing.example.c3"><img src="../../../../../../../doc/src/images/callouts/2.png" alt="2" border="0"></a>
|
|
|
|
<span class="comment">// continues on error</span>
|
|
<span class="keyword">if</span><span class="special">(</span> <span class="identifier">add</span><span class="special">(</span> <span class="number">2</span><span class="special">,</span><span class="number">2</span> <span class="special">)</span> <span class="special">!=</span> <span class="number">4</span> <span class="special">)</span>
|
|
<span class="identifier">BOOST_ERROR</span><span class="special">(</span> <span class="string">"Ouch..."</span> <span class="special">);</span> <a class="co" name="boost_test.bt_minimal_testing.example.c4" href="example.html#boost_test.bt_minimal_testing.example.c5"><img src="../../../../../../../doc/src/images/callouts/3.png" alt="3" border="0"></a>
|
|
|
|
<span class="comment">// throws on error</span>
|
|
<span class="keyword">if</span><span class="special">(</span> <span class="identifier">add</span><span class="special">(</span> <span class="number">2</span><span class="special">,</span><span class="number">2</span> <span class="special">)</span> <span class="special">!=</span> <span class="number">4</span> <span class="special">)</span>
|
|
<span class="identifier">BOOST_FAIL</span><span class="special">(</span> <span class="string">"Ouch..."</span> <span class="special">);</span> <a class="co" name="boost_test.bt_minimal_testing.example.c6" href="example.html#boost_test.bt_minimal_testing.example.c7"><img src="../../../../../../../doc/src/images/callouts/4.png" alt="4" border="0"></a>
|
|
|
|
<span class="comment">// throws on error</span>
|
|
<span class="keyword">if</span><span class="special">(</span> <span class="identifier">add</span><span class="special">(</span> <span class="number">2</span><span class="special">,</span><span class="number">2</span> <span class="special">)</span> <span class="special">!=</span> <span class="number">4</span> <span class="special">)</span>
|
|
<span class="keyword">throw</span> <span class="string">"Oops..."</span><span class="special">;</span> <a class="co" name="boost_test.bt_minimal_testing.example.c8" href="example.html#boost_test.bt_minimal_testing.example.c9"><img src="../../../../../../../doc/src/images/callouts/5.png" alt="5" border="0"></a>
|
|
|
|
<span class="comment">// returns error code</span>
|
|
<span class="keyword">return</span> <span class="identifier">add</span><span class="special">(</span> <span class="number">2</span><span class="special">,</span> <span class="number">2</span> <span class="special">)</span> <span class="special">==</span> <span class="number">4</span> <span class="special">?</span> <span class="number">0</span> <span class="special">:</span> <span class="number">1</span><span class="special">;</span> <a class="co" name="boost_test.bt_minimal_testing.example.c10" href="example.html#boost_test.bt_minimal_testing.example.c11"><img src="../../../../../../../doc/src/images/callouts/6.png" alt="6" border="0"></a>
|
|
<span class="special">}</span>
|
|
</pre>
|
|
<div class="calloutlist"><table border="0" summary="Callout list">
|
|
<tr>
|
|
<td width="5%" valign="top" align="left"><p><a name="boost_test.bt_minimal_testing.example.c1"></a><a href="#boost_test.bt_minimal_testing.example.c0"><img src="../../../../../../../doc/src/images/callouts/1.png" alt="1" border="0"></a> </p></td>
|
|
<td valign="top" align="left"><p>
|
|
This approach uses the <code class="computeroutput"><span class="identifier">BOOST_CHECK</span></code>
|
|
tool, which displays an error message on std::cout that includes
|
|
the expression that failed, the source file name, and the source
|
|
file line number. It also increments the error count. At program
|
|
termination, the error count will be displayed automatically
|
|
by the <span class="emphasis"><em>Minimal Testing Facility</em></span>.
|
|
</p></td>
|
|
</tr>
|
|
<tr>
|
|
<td width="5%" valign="top" align="left"><p><a name="boost_test.bt_minimal_testing.example.c3"></a><a href="#boost_test.bt_minimal_testing.example.c2"><img src="../../../../../../../doc/src/images/callouts/2.png" alt="2" border="0"></a> </p></td>
|
|
<td valign="top" align="left"><p>
|
|
This approach using the <code class="computeroutput"><span class="identifier">BOOST_REQUIRE</span></code>
|
|
tool, is similar to #1, except that after displaying the error,
|
|
an exception is thrown, to be caught by the <span class="emphasis"><em>Minimal
|
|
Testing Facility</em></span>. This approach is suitable when
|
|
writing an explicit test program, and the error would be so
|
|
severe as to make further testing impractical. <code class="computeroutput"><span class="identifier">BOOST_REQUIRE</span></code> differs from
|
|
the C++ Standard Library's <code class="computeroutput"><span class="identifier">assert</span><span class="special">()</span></code> macro in that it is always
|
|
generated, and channels error detection into the uniform reporting
|
|
procedure.
|
|
</p></td>
|
|
</tr>
|
|
<tr>
|
|
<td width="5%" valign="top" align="left"><p><a name="boost_test.bt_minimal_testing.example.c5"></a><a href="#boost_test.bt_minimal_testing.example.c4"><img src="../../../../../../../doc/src/images/callouts/3.png" alt="3" border="0"></a> </p></td>
|
|
<td valign="top" align="left"><p>
|
|
This approach is similar to #1, except that the error detection
|
|
is coded separately. This is most useful when the specific
|
|
condition being tested is not indicative of the reason for
|
|
failure.
|
|
</p></td>
|
|
</tr>
|
|
<tr>
|
|
<td width="5%" valign="top" align="left"><p><a name="boost_test.bt_minimal_testing.example.c7"></a><a href="#boost_test.bt_minimal_testing.example.c6"><img src="../../../../../../../doc/src/images/callouts/4.png" alt="4" border="0"></a> </p></td>
|
|
<td valign="top" align="left"><p>
|
|
This approach is similar to #2, except that the error detection
|
|
is coded separately. This is most useful when the specific
|
|
condition being tested is not indicative of the reason for
|
|
failure.
|
|
</p></td>
|
|
</tr>
|
|
<tr>
|
|
<td width="5%" valign="top" align="left"><p><a name="boost_test.bt_minimal_testing.example.c9"></a><a href="#boost_test.bt_minimal_testing.example.c8"><img src="../../../../../../../doc/src/images/callouts/5.png" alt="5" border="0"></a> </p></td>
|
|
<td valign="top" align="left"><p>
|
|
This approach throws an exception, which will be caught and
|
|
reported by the <span class="emphasis"><em>Minimal Testing Facility</em></span>.
|
|
This approach is suitable for both production and test code,
|
|
in libraries or not. The error message displayed when the exception
|
|
is caught will be most meaningful if the exception is derived
|
|
from <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">exception</span></code>, or is a <code class="computeroutput"><span class="keyword">char</span><span class="special">*</span></code>
|
|
or <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span></code>.
|
|
</p></td>
|
|
</tr>
|
|
<tr>
|
|
<td width="5%" valign="top" align="left"><p><a name="boost_test.bt_minimal_testing.example.c11"></a><a href="#boost_test.bt_minimal_testing.example.c10"><img src="../../../../../../../doc/src/images/callouts/6.png" alt="6" border="0"></a> </p></td>
|
|
<td valign="top" align="left"><p>
|
|
An error code may be returned by the <span class="emphasis"><em>Minimal Testing
|
|
Facility</em></span>
|
|
</p></td>
|
|
</tr>
|
|
</table></div>
|
|
</td>
|
|
<td>
|
|
<pre class="programlisting"><span class="special">****</span> <span class="identifier">no</span> <span class="identifier">errors</span> <span class="identifier">detected</span>
|
|
</pre>
|
|
</td>
|
|
</tr></tbody>
|
|
</table></div>
|
|
</div>
|
|
<br class="table-break">
|
|
</div>
|
|
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
|
<td align="left"></td>
|
|
<td align="right"><div class="copyright-footer">Copyright © 2001-2013 Gennadiy Rozental<p>
|
|
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
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>)
|
|
</p>
|
|
</div></td>
|
|
</tr></table>
|
|
<hr>
|
|
<div class="spirit-nav">
|
|
<a accesskey="p" href="bt_minimal.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../bt_minimal_testing.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="../runtime_configuration_or_what_ar.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
|
|
</div>
|
|
</body>
|
|
</html>
|