Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r64438 - in sandbox/SOC/2010/bits_and_ints/libs/integer: doc doc/html doc/html/boost_integer example test
From: muriloufg_at_[hidden]
Date: 2010-07-29 09:56:40


Author: murilov
Date: 2010-07-29 09:56:38 EDT (Thu, 29 Jul 2010)
New Revision: 64438
URL: http://svn.boost.org/trac/boost/changeset/64438

Log:
Added an example for dealing with policies in ilog2 and added it on documentation
Added:
   sandbox/SOC/2010/bits_and_ints/libs/integer/example/ilog2_policy_test.cpp (contents, props changed)
Text files modified:
   sandbox/SOC/2010/bits_and_ints/libs/integer/doc/bits_and_ints.qbk | 65 +++++++++++++++++++++++++++++++
   sandbox/SOC/2010/bits_and_ints/libs/integer/doc/html/boost_integer/bits_and_ints.html | 82 +++++++++++++++++++++++++++++++++++++++
   sandbox/SOC/2010/bits_and_ints/libs/integer/doc/html/boost_integer/history.html | 4
   sandbox/SOC/2010/bits_and_ints/libs/integer/doc/html/index.html | 2
   sandbox/SOC/2010/bits_and_ints/libs/integer/test/Jamfile.v2 | 1
   5 files changed, 148 insertions(+), 6 deletions(-)

Modified: sandbox/SOC/2010/bits_and_ints/libs/integer/doc/bits_and_ints.qbk
==============================================================================
--- sandbox/SOC/2010/bits_and_ints/libs/integer/doc/bits_and_ints.qbk (original)
+++ sandbox/SOC/2010/bits_and_ints/libs/integer/doc/bits_and_ints.qbk 2010-07-29 09:56:38 EDT (Thu, 29 Jul 2010)
@@ -391,11 +391,74 @@
         template <typename T>
         inline T ilog2(T value);
         
+ template <typename T, typename Policy>
+ inline T ilog2(T value, const Policy& pol);
+
 [endsect]
 
 *[*Remarks: ] If `value` is 0, the result is [*undefined].
 *[*Requires: ] `T` to be an integral type.
-*[*Returns: ] The integer logarithm in base 2 of `value` rounded down.
+*[*Returns: ] The integer logarithm in base 2 of `value` rounded down or -1 if `value` is equal to 0.
+*[*Throws: ] `ilog2` raises an `undeterminate_result_error` if `value` is 0, and by default do not throw anything, but is
+possible to change the behaviour using Boost Math's policies. See section Examples.
+
+[section Examples]
+
+[@../../../../libs/integer/example/ilog2_policy_test.cpp `ilog2` policy example]
+
+``
+#define BOOST_TEST_MAIN
+#include <cerrno>
+#include <iostream>
+#include <stdexcept>
+#include <boost/test/included/unit_test.hpp>
+#include <boost/integer/ilog2.hpp>
+
+namespace boost { namespace math { namespace policies {
+
+template <class T>
+T user_indeterminate_result_error(const char* function, const char* message, const T& val)
+{
+ std::cout << "User Error on function: " << function
+ << " with message: " << message << std::endl;
+
+ (void)val;
+
+ // Returning a custom value
+ return -2;
+}
+
+} } }
+
+BOOST_AUTO_TEST_CASE( test )
+{
+ // Checking default behavior
+ BOOST_CHECK_EQUAL(boost::ilog2(0u), -1);
+
+ using namespace boost::math::policies;
+ policy< indeterminate_result_error<throw_on_error> > throw_pol;
+ policy< indeterminate_result_error<errno_on_error> > errno_pol;
+ policy< indeterminate_result_error<ignore_error> > ignore_pol;
+ policy< indeterminate_result_error<user_error> > user_pol;
+
+
+ // Checking throw_on_error policy
+ BOOST_CHECK_THROW(boost::ilog2(0u, throw_pol) , std::domain_error);
+
+ // Checking errno_on_error policy
+ errno = 0;
+ boost::ilog2(0u, errno_pol);
+ BOOST_CHECK_EQUAL(errno, EDOM);
+
+ // Checking ignore_error policy
+ BOOST_CHECK_EQUAL(boost::ilog2(0u, ignore_pol), -1);
+
+ // Checking user_error policy
+ BOOST_CHECK_EQUAL(boost::ilog2(0u, user_pol), -2);
+}
+``
+
+[endsect]
 
 [endsect]
 

Modified: sandbox/SOC/2010/bits_and_ints/libs/integer/doc/html/boost_integer/bits_and_ints.html
==============================================================================
--- sandbox/SOC/2010/bits_and_ints/libs/integer/doc/html/boost_integer/bits_and_ints.html (original)
+++ sandbox/SOC/2010/bits_and_ints/libs/integer/doc/html/boost_integer/bits_and_ints.html 2010-07-29 09:56:38 EDT (Thu, 29 Jul 2010)
@@ -790,7 +790,10 @@
 <a name="boost_integer.bits_and_ints.integer_logarithm_base_2__ilog2_function__"></a><a class="link" href="bits_and_ints.html#boost_integer.bits_and_ints.integer_logarithm_base_2__ilog2_function__" title="Integer Logarithm Base 2 (ilog2 function)">Integer
       Logarithm Base 2 (ilog2 function) </a>
 </h3></div></div></div>
-<div class="toc"><dl><dt><span class="section">Synopsis</span></dt></dl></div>
+<div class="toc"><dl>
+<dt><span class="section">Synopsis</span></dt>
+<dt><span class="section">Examples</span></dt>
+</dl></div>
 <p>
         The header file <boost/integer/ilog2.hpp>
         defines <code class="computeroutput"><span class="identifier">ilog2</span></code> function wich
@@ -802,6 +805,9 @@
 </h4></div></div></div>
 <pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
 <span class="keyword">inline</span> <span class="identifier">T</span> <span class="identifier">ilog2</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">value</span><span class="special">);</span>
+
+<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Policy</span><span class="special">&gt;</span>
+<span class="keyword">inline</span> <span class="identifier">T</span> <span class="identifier">ilog2</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">value</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">Policy</span><span class="special">&amp;</span> <span class="identifier">pol</span><span class="special">);</span>
 </pre>
 </div>
 <div class="itemizedlist"><ul class="itemizedlist" type="disc">
@@ -815,9 +821,81 @@
         </li>
 <li class="listitem">
 <span class="bold"><strong>Returns: </strong></span> The integer logarithm in base
- 2 of <code class="computeroutput"><span class="identifier">value</span></code> rounded down.
+ 2 of <code class="computeroutput"><span class="identifier">value</span></code> rounded down
+ or -1 if <code class="computeroutput"><span class="identifier">value</span></code> is equal
+ to 0.
+ </li>
+<li class="listitem">
+<span class="bold"><strong>Throws: </strong></span><code class="computeroutput"><span class="identifier">ilog2</span></code>
+ raises an <code class="computeroutput"><span class="identifier">undeterminate_result_error</span></code>
+ if <code class="computeroutput"><span class="identifier">value</span></code> is 0, and by default
+ do not throw anything, but is possible to change the behaviour using Boost
+ Math's policies. See section Examples.
         </li>
 </ul></div>
+<div class="section" title="Examples">
+<div class="titlepage"><div><div><h4 class="title">
+<a name="boost_integer.bits_and_ints.integer_logarithm_base_2__ilog2_function__.examples"></a><a class="link" href="bits_and_ints.html#boost_integer.bits_and_ints.integer_logarithm_base_2__ilog2_function__.examples" title="Examples">Examples</a>
+</h4></div></div></div>
+<p>
+ ilog2 policy example
+ </p>
+<p>
+
+</p>
+<pre class="programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_TEST_MAIN</span>
+<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cerrno</span><span class="special">&gt;</span>
+<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
+<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">stdexcept</span><span class="special">&gt;</span>
+<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>
+<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">integer</span><span class="special">/</span><span class="identifier">ilog2</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
+
+<span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">math</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">policies</span> <span class="special">{</span>
+
+<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
+<span class="identifier">T</span> <span class="identifier">user_indeterminate_result_error</span><span class="special">(</span><span class="keyword">const</span> <span class="keyword">char</span><span class="special">*</span> <span class="identifier">function</span><span class="special">,</span> <span class="keyword">const</span> <span class="keyword">char</span><span class="special">*</span> <span class="identifier">message</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">T</span><span class="special">&amp;</span> <span class="identifier">val</span><span class="special">)</span>
+<span class="special">{</span>
+ <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"User Error on function: "</span> <span class="special">&lt;&lt;</span> <span class="identifier">function</span>
+ <span class="special">&lt;&lt;</span> <span class="string">" with message: "</span> <span class="special">&lt;&lt;</span> <span class="identifier">message</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
+
+ <span class="special">(</span><span class="keyword">void</span><span class="special">)</span><span class="identifier">val</span><span class="special">;</span>
+
+ <span class="comment">// Returning a custom value
+</span> <span class="keyword">return</span> <span class="special">-</span><span class="number">2</span><span class="special">;</span>
+<span class="special">}</span>
+
+<span class="special">}</span> <span class="special">}</span> <span class="special">}</span>
+
+<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span> <span class="identifier">test</span> <span class="special">)</span>
+<span class="special">{</span>
+ <span class="comment">// Checking default behavior
+</span> <span class="identifier">BOOST_CHECK_EQUAL</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">ilog2</span><span class="special">(</span><span class="number">0u</span><span class="special">),</span> <span class="special">-</span><span class="number">1</span><span class="special">);</span>
+
+ <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">;</span>
+ <span class="identifier">policy</span><span class="special">&lt;</span> <span class="identifier">indeterminate_result_error</span><span class="special">&lt;</span><span class="identifier">throw_on_error</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">throw_pol</span><span class="special">;</span>
+ <span class="identifier">policy</span><span class="special">&lt;</span> <span class="identifier">indeterminate_result_error</span><span class="special">&lt;</span><span class="identifier">errno_on_error</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">errno_pol</span><span class="special">;</span>
+ <span class="identifier">policy</span><span class="special">&lt;</span> <span class="identifier">indeterminate_result_error</span><span class="special">&lt;</span><span class="identifier">ignore_error</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">ignore_pol</span><span class="special">;</span>
+ <span class="identifier">policy</span><span class="special">&lt;</span> <span class="identifier">indeterminate_result_error</span><span class="special">&lt;</span><span class="identifier">user_error</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">user_pol</span><span class="special">;</span>
+
+
+ <span class="comment">// Checking throw_on_error policy
+</span> <span class="identifier">BOOST_CHECK_THROW</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">ilog2</span><span class="special">(</span><span class="number">0u</span><span class="special">,</span> <span class="identifier">throw_pol</span><span class="special">)</span> <span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">domain_error</span><span class="special">);</span>
+
+ <span class="comment">// Checking errno_on_error policy
+</span> <span class="identifier">errno</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
+ <span class="identifier">boost</span><span class="special">::</span><span class="identifier">ilog2</span><span class="special">(</span><span class="number">0u</span><span class="special">,</span> <span class="identifier">errno_pol</span><span class="special">);</span>
+ <span class="identifier">BOOST_CHECK_EQUAL</span><span class="special">(</span><span class="identifier">errno</span><span class="special">,</span> <span class="identifier">EDOM</span><span class="special">);</span>
+
+ <span class="comment">// Checking ignore_error policy
+</span> <span class="identifier">BOOST_CHECK_EQUAL</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">ilog2</span><span class="special">(</span><span class="number">0u</span><span class="special">,</span> <span class="identifier">ignore_pol</span><span class="special">),</span> <span class="special">-</span><span class="number">1</span><span class="special">);</span>
+
+ <span class="comment">// Checking user_error policy
+</span> <span class="identifier">BOOST_CHECK_EQUAL</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">ilog2</span><span class="special">(</span><span class="number">0u</span><span class="special">,</span> <span class="identifier">user_pol</span><span class="special">),</span> <span class="special">-</span><span class="number">2</span><span class="special">);</span>
+<span class="special">}</span>
+</pre>
+<p>
+ </p>
+</div>
 </div>
 <div class="section" title="is_integral_constant&lt;&gt; metafunction">
 <div class="titlepage"><div><div><h3 class="title">

Modified: sandbox/SOC/2010/bits_and_ints/libs/integer/doc/html/boost_integer/history.html
==============================================================================
--- sandbox/SOC/2010/bits_and_ints/libs/integer/doc/html/boost_integer/history.html (original)
+++ sandbox/SOC/2010/bits_and_ints/libs/integer/doc/html/boost_integer/history.html 2010-07-29 09:56:38 EDT (Thu, 29 Jul 2010)
@@ -26,7 +26,7 @@
 <a name="boost_integer.history"></a><a class="link" href="history.html" title="History"> History</a>
 </h2></div></div></div>
 <a name="boost_integer.history.1_42_0"></a><h5>
-<a name="id36202477"></a>
+<a name="id36203718"></a>
       <a class="link" href="history.html#boost_integer.history.1_42_0">1.42.0</a>
     </h5>
 <div class="itemizedlist"><ul class="itemizedlist" type="disc">
@@ -55,7 +55,7 @@
       </li>
 </ul></div>
 <a name="boost_integer.history.1_32_0"></a><h5>
-<a name="id36202597"></a>
+<a name="id36203839"></a>
       <a class="link" href="history.html#boost_integer.history.1_32_0">1.32.0</a>
     </h5>
 <div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem">

Modified: sandbox/SOC/2010/bits_and_ints/libs/integer/doc/html/index.html
==============================================================================
--- sandbox/SOC/2010/bits_and_ints/libs/integer/doc/html/index.html (original)
+++ sandbox/SOC/2010/bits_and_ints/libs/integer/doc/html/index.html 2010-07-29 09:56:38 EDT (Thu, 29 Jul 2010)
@@ -255,7 +255,7 @@
 </div>
 </div>
 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
-<td align="left"><p><small>Last revised: July 14, 2010 at 19:47:00 GMT</small></p></td>
+<td align="left"><p><small>Last revised: July 29, 2010 at 13:53:45 GMT</small></p></td>
 <td align="right"><div class="copyright-footer"></div></td>
 </tr></table>
 <hr>

Added: sandbox/SOC/2010/bits_and_ints/libs/integer/example/ilog2_policy_test.cpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2010/bits_and_ints/libs/integer/example/ilog2_policy_test.cpp 2010-07-29 09:56:38 EDT (Thu, 29 Jul 2010)
@@ -0,0 +1,49 @@
+#define BOOST_TEST_MAIN
+#include <cerrno>
+#include <iostream>
+#include <stdexcept>
+#include <boost/test/included/unit_test.hpp>
+#include <boost/integer/ilog2.hpp>
+
+namespace boost { namespace math { namespace policies {
+
+template <class T>
+T user_indeterminate_result_error(const char* function, const char* message, const T& val)
+{
+ std::cout << "User Error on function: " << function
+ << " with message: " << message << std::endl;
+
+ (void)val;
+
+ // Returning a custom value
+ return -2;
+}
+
+} } }
+
+BOOST_AUTO_TEST_CASE( test )
+{
+ // Checking default behavior
+ BOOST_CHECK_EQUAL(boost::ilog2(0u), -1);
+
+ using namespace boost::math::policies;
+ policy< indeterminate_result_error<throw_on_error> > throw_pol;
+ policy< indeterminate_result_error<errno_on_error> > errno_pol;
+ policy< indeterminate_result_error<ignore_error> > ignore_pol;
+ policy< indeterminate_result_error<user_error> > user_pol;
+
+
+ // Checking throw_on_error policy
+ BOOST_CHECK_THROW(boost::ilog2(0u, throw_pol) , std::domain_error);
+
+ // Checking errno_on_error policy
+ errno = 0;
+ boost::ilog2(0u, errno_pol);
+ BOOST_CHECK_EQUAL(errno, EDOM);
+
+ // Checking ignore_error policy
+ BOOST_CHECK_EQUAL(boost::ilog2(0u, ignore_pol), -1);
+
+ // Checking user_error policy
+ BOOST_CHECK_EQUAL(boost::ilog2(0u, user_pol), -2);
+}

Modified: sandbox/SOC/2010/bits_and_ints/libs/integer/test/Jamfile.v2
==============================================================================
--- sandbox/SOC/2010/bits_and_ints/libs/integer/test/Jamfile.v2 (original)
+++ sandbox/SOC/2010/bits_and_ints/libs/integer/test/Jamfile.v2 2010-07-29 09:56:38 EDT (Thu, 29 Jul 2010)
@@ -32,6 +32,7 @@
                 [ run isqrt_test.cpp ]
                 [ run find_first_one_string_test.cpp ]
                 [ run rotate_test.cpp ]
+ [ run ilog2_policy_test.cpp ]
                 [ compile cstdint_include_test.cpp ]
                 [ compile integer_traits_include_test.cpp ]
                 [ compile integer_include_test.cpp ]


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk