Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r78838 - in trunk: boost/algorithm libs/algorithm/test
From: marshall_at_[hidden]
Date: 2012-06-06 16:38:05


Author: marshall
Date: 2012-06-06 16:38:04 EDT (Wed, 06 Jun 2012)
New Revision: 78838
URL: http://svn.boost.org/trac/boost/changeset/78838

Log:
Reworked boost::algorithm::unhex error reporting to play nicer with Boost.Exception (thanks to Emil); added tests
Added:
   trunk/libs/algorithm/test/hex_test4.cpp (contents, props changed)
Text files modified:
   trunk/boost/algorithm/hex.hpp | 14 +++++---------
   trunk/libs/algorithm/test/Jamfile.v2 | 1 +
   2 files changed, 6 insertions(+), 9 deletions(-)

Modified: trunk/boost/algorithm/hex.hpp
==============================================================================
--- trunk/boost/algorithm/hex.hpp (original)
+++ trunk/boost/algorithm/hex.hpp 2012-06-06 16:38:04 EDT (Wed, 06 Jun 2012)
@@ -51,14 +51,10 @@
     \brief Thrown when the input sequence unexpectedly ends
     
 */
-struct hex_decode_error: virtual boost::exception, virtual std::exception {};
-struct not_enough_input : public hex_decode_error {};
-struct non_hex_input : public hex_decode_error {
- non_hex_input ( char ch ) : bad_char ( ch ) {}
- char bad_char;
-private:
- non_hex_input (); // don't allow creation w/o a char
- };
+struct hex_decode_error : virtual boost::exception, virtual std::exception {};
+struct not_enough_input : virtual hex_decode_error {};
+struct non_hex_input : virtual hex_decode_error {};
+typedef boost::error_info<struct bad_char_,char> bad_char;
 
 namespace detail {
 /// \cond DOXYGEN_HIDE
@@ -77,7 +73,7 @@
         if ( c >= '0' && c <= '9' ) return c - '0';
         if ( c >= 'A' && c <= 'F' ) return c - 'A' + 10;
         if ( c >= 'a' && c <= 'f' ) return c - 'a' + 10;
- BOOST_THROW_EXCEPTION (non_hex_input (c));
+ BOOST_THROW_EXCEPTION (non_hex_input() << boost::algorithm::bad_char (c));
         return 0; // keep dumb compilers happy
         }
     

Modified: trunk/libs/algorithm/test/Jamfile.v2
==============================================================================
--- trunk/libs/algorithm/test/Jamfile.v2 (original)
+++ trunk/libs/algorithm/test/Jamfile.v2 2012-06-06 16:38:04 EDT (Wed, 06 Jun 2012)
@@ -43,6 +43,7 @@
      [ run hex_test1.cpp : : : : hex_test1 ]
      [ run hex_test2.cpp : : : : hex_test2 ]
      [ run hex_test3.cpp : : : : hex_test3 ]
+ [ run hex_test4.cpp : : : : hex_test4 ]
          [ compile-fail hex_fail1.cpp ]
    ;
 }

Added: trunk/libs/algorithm/test/hex_test4.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/algorithm/test/hex_test4.cpp 2012-06-06 16:38:04 EDT (Wed, 06 Jun 2012)
@@ -0,0 +1,145 @@
+/*
+ Copyright (c) Marshall Clow 2011-2012.
+
+ Distributed under the Boost Software License, Version 1.0. (See accompanying
+ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+ For more information, see http://www.boost.org
+
+Try ostream_iterators
+*/
+
+#include <boost/config.hpp>
+#include <boost/algorithm/hex.hpp>
+#include <boost/test/included/test_exec_monitor.hpp>
+
+#include <string>
+#include <iostream>
+
+namespace ba = boost::algorithm;
+
+void test_short_input1 () {
+ std::string s;
+
+ try { ba::unhex ( std::string ( "A" ), std::back_inserter(s)); }
+ catch ( const std::exception &ex ) { return; }
+ BOOST_TEST_MESSAGE ( "Failed to catch std::exception in test_short_input1" );
+ BOOST_CHECK ( false );
+ }
+
+void test_short_input2 () {
+ std::string s;
+
+ try { ba::unhex ( std::string ( "A" ), std::back_inserter(s)); }
+ catch ( const ba::hex_decode_error &ex ) { return; }
+ BOOST_TEST_MESSAGE ( "Failed to catch ba::hex_decode_error in test_short_input2" );
+ BOOST_CHECK ( false );
+ }
+
+void test_short_input3 () {
+ std::string s;
+
+ try { ba::unhex ( std::string ( "A" ), std::back_inserter(s)); }
+ catch ( const ba::not_enough_input &ex ) { return; }
+ BOOST_TEST_MESSAGE ( "Failed to catch ba::not_enough_input in test_short_input3" );
+ BOOST_CHECK ( false );
+ }
+
+// Make sure that the right thing is thrown
+void test_short_input4 () {
+ std::string s;
+
+ try { ba::unhex ( std::string ( "A" ), std::back_inserter(s)); }
+ catch ( const ba::non_hex_input &ex ) { BOOST_CHECK ( false ); }
+ catch ( const ba::not_enough_input &ex ) { return; }
+ catch ( ... ) { BOOST_CHECK ( false ); }
+ BOOST_CHECK ( false );
+ }
+
+void test_short_input () {
+// BOOST_TEST_MESSAGE ( "Short input tests for boost::algorithm::unhex" );
+ test_short_input1 ();
+ test_short_input2 ();
+ test_short_input3 ();
+ test_short_input4 ();
+ }
+
+
+void test_nonhex_input1 () {
+ std::string s;
+
+ try { ba::unhex ( "01234FG1234", std::back_inserter(s)); }
+ catch ( const std::exception &ex ) {
+ BOOST_CHECK ( 'G' == *boost::get_error_info<ba::bad_char>(ex));
+ return;
+ }
+ BOOST_TEST_MESSAGE ( "Failed to catch std::exception in test_nonhex_input1" );
+ BOOST_CHECK ( false );
+ }
+
+void test_nonhex_input2 () {
+ std::string s;
+
+ try { ba::unhex ( "012Z4FA1234", std::back_inserter(s)); }
+ catch ( const ba::hex_decode_error &ex ) {
+ BOOST_CHECK ( 'Z' == *boost::get_error_info<ba::bad_char>(ex));
+ return;
+ }
+ BOOST_TEST_MESSAGE ( "Failed to catch ba::hex_decode_error in test_nonhex_input2" );
+ BOOST_CHECK ( false );
+ }
+
+void test_nonhex_input3 () {
+ std::string s;
+
+ try { ba::unhex ( "01234FA12Q4", std::back_inserter(s)); }
+ catch ( const ba::non_hex_input &ex ) {
+ BOOST_CHECK ( 'Q' == *boost::get_error_info<ba::bad_char>(ex));
+ return;
+ }
+ BOOST_TEST_MESSAGE ( "Failed to catch ba::non_hex_input in test_nonhex_input3" );
+ BOOST_CHECK ( false );
+ }
+
+// Make sure that the right thing is thrown
+void test_nonhex_input4 () {
+ std::string s;
+
+ try { ba::unhex ( "P1234FA1234", std::back_inserter(s)); }
+ catch ( const ba::not_enough_input &ex ) { BOOST_CHECK ( false ); }
+ catch ( const ba::non_hex_input &ex ) { return; }
+ catch ( ... ) { BOOST_CHECK ( false ); }
+ BOOST_CHECK ( false );
+ }
+
+// Make sure that the right thing is thrown
+void test_nonhex_input5 () {
+ std::string s;
+
+ try { ba::unhex ( "012", std::back_inserter(s)); }
+ catch ( const ba::non_hex_input &ex ) {
+ BOOST_CHECK ( '\000' == *boost::get_error_info<ba::bad_char>(ex));
+ return;
+ }
+ BOOST_TEST_MESSAGE ( "Failed to catch ba::non_hex_input in test_nonhex_input4" );
+ BOOST_CHECK ( false );
+ }
+
+void test_nonhex_input () {
+// BOOST_TEST_MESSAGE ( "Non hex input tests for for boost::algorithm::unhex" );
+ test_nonhex_input1 ();
+ test_nonhex_input2 ();
+ test_nonhex_input3 ();
+ test_nonhex_input4 ();
+ test_nonhex_input5 ();
+ }
+
+
+
+int test_main( int , char* [] )
+{
+ test_short_input ();
+ test_nonhex_input ();
+
+ return 0;
+}


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