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