| 1 | /* |
| 2 | Copyright (c) Marshall Clow 2011-2012. |
| 3 | |
| 4 | Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | For more information, see http://www.boost.org |
| 8 | |
| 9 | Try ostream_iterators |
| 10 | */ |
| 11 | |
| 12 | #include <boost/config.hpp> |
| 13 | #include <boost/algorithm/hex.hpp> |
| 14 | #include <boost/exception/get_error_info.hpp> |
| 15 | |
| 16 | #define BOOST_TEST_MAIN |
| 17 | #include <boost/test/unit_test.hpp> |
| 18 | |
| 19 | #include <string> |
| 20 | #include <iostream> |
| 21 | |
| 22 | namespace ba = boost::algorithm; |
| 23 | |
| 24 | void test_short_input1 () { |
| 25 | std::string s; |
| 26 | |
| 27 | try { ba::unhex ( r: std::string ( "A" ), out: std::back_inserter(x&: s)); } |
| 28 | catch ( const std::exception & ) { return; } |
| 29 | BOOST_TEST_MESSAGE ( "Failed to catch std::exception in test_short_input1" ); |
| 30 | BOOST_CHECK ( false ); |
| 31 | } |
| 32 | |
| 33 | void test_short_input2 () { |
| 34 | std::string s; |
| 35 | |
| 36 | try { ba::unhex ( r: std::string ( "A" ), out: std::back_inserter(x&: s)); } |
| 37 | catch ( const ba::hex_decode_error & ) { return; } |
| 38 | BOOST_TEST_MESSAGE ( "Failed to catch ba::hex_decode_error in test_short_input2" ); |
| 39 | BOOST_CHECK ( false ); |
| 40 | } |
| 41 | |
| 42 | void test_short_input3 () { |
| 43 | std::string s; |
| 44 | |
| 45 | try { ba::unhex ( r: std::string ( "A" ), out: std::back_inserter(x&: s)); } |
| 46 | catch ( const ba::not_enough_input & ) { return; } |
| 47 | BOOST_TEST_MESSAGE ( "Failed to catch ba::not_enough_input in test_short_input3" ); |
| 48 | BOOST_CHECK ( false ); |
| 49 | } |
| 50 | |
| 51 | // Make sure that the right thing is thrown |
| 52 | void test_short_input4 () { |
| 53 | std::string s; |
| 54 | |
| 55 | try { ba::unhex ( r: std::string ( "A" ), out: std::back_inserter(x&: s)); } |
| 56 | catch ( const ba::non_hex_input & ) { BOOST_CHECK ( false ); } |
| 57 | catch ( const ba::not_enough_input & ) { return; } |
| 58 | catch ( ... ) { BOOST_CHECK ( false ); } |
| 59 | BOOST_CHECK ( false ); |
| 60 | } |
| 61 | |
| 62 | // Make sure that the right thing is thrown |
| 63 | void test_short_input5 () { |
| 64 | std::string s; |
| 65 | |
| 66 | try { ba::unhex ( ptr: "A" , out: std::back_inserter(x&: s)); } |
| 67 | catch ( const ba::non_hex_input & ) { BOOST_CHECK ( false ); } |
| 68 | catch ( const ba::not_enough_input & ) { return; } |
| 69 | catch ( ... ) { BOOST_CHECK ( false ); } |
| 70 | BOOST_CHECK ( false ); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | void test_short_input () { |
| 75 | // BOOST_TEST_MESSAGE ( "Short input tests for boost::algorithm::unhex" ); |
| 76 | test_short_input1 (); |
| 77 | test_short_input2 (); |
| 78 | test_short_input3 (); |
| 79 | test_short_input4 (); |
| 80 | test_short_input5 (); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | void test_nonhex_input1 () { |
| 85 | std::string s; |
| 86 | |
| 87 | try { ba::unhex ( ptr: "01234FG1234" , out: std::back_inserter(x&: s)); } |
| 88 | catch ( const std::exception &ex ) { |
| 89 | BOOST_CHECK ( 'G' == *boost::get_error_info<ba::bad_char>(ex)); |
| 90 | return; |
| 91 | } |
| 92 | catch ( ... ) {} |
| 93 | BOOST_TEST_MESSAGE ( "Failed to catch std::exception in test_nonhex_input1" ); |
| 94 | BOOST_CHECK ( false ); |
| 95 | } |
| 96 | |
| 97 | void test_nonhex_input2 () { |
| 98 | std::string s; |
| 99 | |
| 100 | try { ba::unhex ( ptr: "012Z4FA1234" , out: std::back_inserter(x&: s)); } |
| 101 | catch ( const ba::hex_decode_error &ex ) { |
| 102 | BOOST_CHECK ( 'Z' == *boost::get_error_info<ba::bad_char>(ex)); |
| 103 | return; |
| 104 | } |
| 105 | catch ( ... ) {} |
| 106 | BOOST_TEST_MESSAGE ( "Failed to catch ba::hex_decode_error in test_nonhex_input2" ); |
| 107 | BOOST_CHECK ( false ); |
| 108 | } |
| 109 | |
| 110 | void test_nonhex_input3 () { |
| 111 | std::string s; |
| 112 | |
| 113 | try { ba::unhex ( ptr: "01234FA12Q4" , out: std::back_inserter(x&: s)); } |
| 114 | catch ( const ba::non_hex_input &ex ) { |
| 115 | BOOST_CHECK ( 'Q' == *boost::get_error_info<ba::bad_char>(ex)); |
| 116 | return; |
| 117 | } |
| 118 | catch ( ... ) {} |
| 119 | BOOST_TEST_MESSAGE ( "Failed to catch ba::non_hex_input in test_nonhex_input3" ); |
| 120 | BOOST_CHECK ( false ); |
| 121 | } |
| 122 | |
| 123 | // Make sure that the right thing is thrown |
| 124 | void test_nonhex_input4 () { |
| 125 | std::string s; |
| 126 | |
| 127 | try { ba::unhex ( ptr: "P1234FA1234" , out: std::back_inserter(x&: s)); } |
| 128 | catch ( const ba::not_enough_input & ) { BOOST_CHECK ( false ); } |
| 129 | catch ( const ba::non_hex_input & ) { return; } |
| 130 | catch ( ... ) { BOOST_CHECK ( false ); } |
| 131 | BOOST_CHECK ( false ); |
| 132 | } |
| 133 | |
| 134 | void test_nonhex_input () { |
| 135 | // BOOST_TEST_MESSAGE ( "Non hex input tests for boost::algorithm::unhex" ); |
| 136 | test_nonhex_input1 (); |
| 137 | test_nonhex_input2 (); |
| 138 | test_nonhex_input3 (); |
| 139 | test_nonhex_input4 (); |
| 140 | } |
| 141 | |
| 142 | BOOST_AUTO_TEST_CASE( test_main ) |
| 143 | { |
| 144 | test_short_input (); |
| 145 | test_nonhex_input (); |
| 146 | } |
| 147 | |