• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
test_short_input1()24 void test_short_input1 () {
25     std::string s;
26 
27     try { ba::unhex ( std::string ( "A" ), std::back_inserter(s)); }
28     catch ( const std::exception &ex ) { return; }
29     BOOST_TEST_MESSAGE ( "Failed to catch std::exception in test_short_input1" );
30     BOOST_CHECK ( false );
31     }
32 
test_short_input2()33 void test_short_input2 () {
34     std::string s;
35 
36     try { ba::unhex ( std::string ( "A" ), std::back_inserter(s)); }
37     catch ( const ba::hex_decode_error &ex ) { return; }
38     BOOST_TEST_MESSAGE ( "Failed to catch ba::hex_decode_error in test_short_input2" );
39     BOOST_CHECK ( false );
40     }
41 
test_short_input3()42 void test_short_input3 () {
43     std::string s;
44 
45     try { ba::unhex ( std::string ( "A" ), std::back_inserter(s)); }
46     catch ( const ba::not_enough_input &ex ) { 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
test_short_input4()52 void test_short_input4 () {
53     std::string s;
54 
55     try { ba::unhex ( std::string ( "A" ), std::back_inserter(s)); }
56     catch ( const ba::non_hex_input &ex ) { BOOST_CHECK ( false ); }
57     catch ( const ba::not_enough_input &ex ) { return; }
58     catch ( ... ) { BOOST_CHECK ( false ); }
59     BOOST_CHECK ( false );
60     }
61 
62 //  Make sure that the right thing is thrown
test_short_input5()63 void test_short_input5 () {
64     std::string s;
65 
66     try { ba::unhex ( "A", std::back_inserter(s)); }
67     catch ( const ba::non_hex_input &ex ) { BOOST_CHECK ( false ); }
68     catch ( const ba::not_enough_input &ex ) { return; }
69     catch ( ... ) { BOOST_CHECK ( false ); }
70     BOOST_CHECK ( false );
71     }
72 
73 
test_short_input()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 
test_nonhex_input1()84 void test_nonhex_input1 () {
85     std::string s;
86 
87     try { ba::unhex ( "01234FG1234", std::back_inserter(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 
test_nonhex_input2()97 void test_nonhex_input2 () {
98     std::string s;
99 
100     try { ba::unhex ( "012Z4FA1234", std::back_inserter(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 
test_nonhex_input3()110 void test_nonhex_input3 () {
111     std::string s;
112 
113     try { ba::unhex ( "01234FA12Q4", std::back_inserter(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
test_nonhex_input4()124 void test_nonhex_input4 () {
125     std::string s;
126 
127     try { ba::unhex ( "P1234FA1234", std::back_inserter(s)); }
128     catch ( const ba::not_enough_input &ex ) { BOOST_CHECK ( false ); }
129     catch ( const ba::non_hex_input &ex ) { return; }
130     catch ( ... ) { BOOST_CHECK ( false ); }
131     BOOST_CHECK ( false );
132     }
133 
test_nonhex_input()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 
BOOST_AUTO_TEST_CASE(test_main)142 BOOST_AUTO_TEST_CASE( test_main )
143 {
144     test_short_input ();
145     test_nonhex_input ();
146 }
147