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
10#include <boost/config.hpp>
11#include <boost/algorithm/hex.hpp>
12
13#define BOOST_TEST_MAIN
14#include <boost/test/unit_test.hpp>
15
16#include <string>
17#include <iostream>
18
19
20template<typename String>
21void test_to_hex ( const typename String::value_type ** tests ) {
22 for ( const typename String::value_type **p = tests; *p; p++ ) {
23 String arg, argh, one, two, three, four;
24 arg.assign ( *p );
25 boost::algorithm::hex ( *p, std::back_inserter ( one ));
26 boost::algorithm::hex ( arg, std::back_inserter ( two ));
27 boost::algorithm::hex ( arg.begin (), arg.end (), std::back_inserter ( three ));
28 four = boost::algorithm::hex ( arg );
29 BOOST_CHECK ( one == two );
30 BOOST_CHECK ( one == three );
31 BOOST_CHECK ( one == four );
32 argh = one;
33 one.clear (); two.clear (); three.clear (); four.clear ();
34 boost::algorithm::unhex ( argh.c_str (), std::back_inserter ( one ));
35 boost::algorithm::unhex ( argh, std::back_inserter ( two ));
36 boost::algorithm::unhex ( argh.begin (), argh.end (), std::back_inserter ( three ));
37 four = boost::algorithm::unhex ( argh );
38 BOOST_CHECK ( one == two );
39 BOOST_CHECK ( one == three );
40 BOOST_CHECK ( one == four );
41 BOOST_CHECK ( one == arg );
42 }
43 }
44
45
46template<typename String>
47void test_from_hex_success ( const typename String::value_type ** tests ) {
48 for ( const typename String::value_type **p = tests; *p; p++ ) {
49 String arg, argh, one, two, three, four;
50 arg.assign ( *p );
51 boost::algorithm::unhex ( *p, std::back_inserter ( one ));
52 boost::algorithm::unhex ( arg, std::back_inserter ( two ));
53 boost::algorithm::unhex ( arg.begin (), arg.end (), std::back_inserter ( three ));
54 four = boost::algorithm::unhex ( arg );
55 BOOST_CHECK ( one == two );
56 BOOST_CHECK ( one == three );
57 BOOST_CHECK ( one == four );
58 argh = one;
59 one.clear (); two.clear (); three.clear (); four.clear ();
60 boost::algorithm::hex ( argh.c_str (), std::back_inserter ( one ));
61 boost::algorithm::hex ( argh, std::back_inserter ( two ));
62 boost::algorithm::hex ( argh.begin (), argh.end (), std::back_inserter ( three ));
63 four = boost::algorithm::hex ( argh );
64 BOOST_CHECK ( one == two );
65 BOOST_CHECK ( one == three );
66 BOOST_CHECK ( one == four );
67 BOOST_CHECK ( one == arg );
68 }
69 }
70
71template<typename String>
72void test_from_hex_failure ( const typename String::value_type ** tests ) {
73 int num_catches;
74 for ( const typename String::value_type **p = tests; *p; p++ ) {
75 String arg, one;
76 arg.assign ( *p );
77 num_catches = 0;
78
79 try { boost::algorithm::unhex ( *p, std::back_inserter ( one )); }
80 catch ( const boost::algorithm::hex_decode_error & /*ex*/ ) { num_catches++; }
81 try { boost::algorithm::unhex ( arg, std::back_inserter ( one )); }
82 catch ( const boost::algorithm::hex_decode_error & /*ex*/ ) { num_catches++; }
83 try { boost::algorithm::unhex ( arg.begin (), arg.end (), std::back_inserter ( one )); }
84 catch ( const boost::algorithm::hex_decode_error & /*ex*/ ) { num_catches++; }
85 BOOST_CHECK ( num_catches == 3 );
86 }
87 }
88
89
90
91const char *tohex [] = {
92 "",
93 "a",
94 "\001",
95 "12",
96 "asdfadsfsad",
97 "01234567890ABCDEF",
98 NULL // End of the list
99 };
100
101
102const wchar_t *tohex_w [] = {
103 L"",
104 L"a",
105 L"\001",
106 L"12",
107 L"asdfadsfsad",
108 L"01234567890ABCDEF",
109 NULL // End of the list
110 };
111
112
113const char *fromhex [] = {
114 "20",
115 "2122234556FF",
116 NULL // End of the list
117 };
118
119
120const wchar_t *fromhex_w [] = {
121 L"00101020",
122 L"2122234556FF3456",
123 NULL // End of the list
124 };
125
126
127const char *fromhex_fail [] = {
128 "2",
129 "H",
130 "234",
131 "21222G4556FF",
132 NULL // End of the list
133 };
134
135
136const wchar_t *fromhex_fail_w [] = {
137 L"2",
138 L"12",
139 L"H",
140 L"234",
141 L"21222G4556FF",
142 NULL // End of the list
143 };
144
145
146BOOST_AUTO_TEST_CASE( test_main )
147{
148 test_to_hex<std::string> ( tohex );
149 test_from_hex_success<std::string> ( fromhex );
150 test_from_hex_failure<std::string> ( fromhex_fail );
151
152 test_to_hex<std::wstring> ( tohex_w );
153 test_from_hex_success<std::wstring> ( fromhex_w );
154 test_from_hex_failure<std::wstring> ( fromhex_fail_w );
155}
156