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#include <deque>
21#include <list>
22
23
24template <typename char_type>
25void test_to_hex ( const char_type ** tests ) {
26 typedef std::basic_string<char_type> String;
27 typedef std::basic_ostringstream<char_type> Stream;
28 typedef std::ostream_iterator<char_type, char_type> Iter;
29
30 for ( const char_type **p = tests; *p; p++ ) {
31 String arg, argh;
32 Stream one, two, three;
33 arg.assign ( *p );
34 boost::algorithm::hex ( *p, Iter ( one ));
35 boost::algorithm::hex ( arg, Iter ( two ));
36 boost::algorithm::hex ( arg.begin (), arg.end (), Iter ( three ));
37 boost::algorithm::hex ( arg );
38 BOOST_CHECK ( one.str () == two.str ());
39 BOOST_CHECK ( one.str () == three.str ());
40 argh = one.str ();
41 one.str (String()); two.str (String()); three.str (String());
42 boost::algorithm::unhex ( argh.c_str (), Iter ( one ));
43 boost::algorithm::unhex ( argh, Iter ( two ));
44 boost::algorithm::unhex ( argh.begin (), argh.end (), Iter ( three ));
45 BOOST_CHECK ( one.str () == two.str ());
46 BOOST_CHECK ( one.str () == three.str ());
47 BOOST_CHECK ( one.str () == arg );
48 }
49 }
50
51
52template <typename char_type>
53void test_from_hex_success ( const char_type ** tests ) {
54 typedef std::basic_string<char_type> String;
55 typedef std::basic_ostringstream<char_type> Stream;
56 typedef std::ostream_iterator<char_type, char_type> Iter;
57
58 for ( const char_type **p = tests; *p; p++ ) {
59 String arg, argh;
60 Stream one, two, three;
61 arg.assign ( *p );
62 boost::algorithm::unhex ( *p, Iter ( one ));
63 boost::algorithm::unhex ( arg, Iter ( two ));
64 boost::algorithm::unhex ( arg.begin (), arg.end (), Iter ( three ));
65
66 BOOST_CHECK ( one.str () == two.str ());
67 BOOST_CHECK ( one.str () == three.str ());
68
69 argh = one.str ();
70 one.str (String()); two.str (String()); three.str (String());
71
72 boost::algorithm::hex ( argh.c_str (), Iter ( one ));
73 boost::algorithm::hex ( argh, Iter ( two ));
74 boost::algorithm::hex ( argh.begin (), argh.end (), Iter ( three ));
75
76 BOOST_CHECK ( one.str () == two.str ());
77 BOOST_CHECK ( one.str () == three.str ());
78 BOOST_CHECK ( one.str () == arg );
79 }
80
81 }
82
83const char *tohex [] = {
84 "",
85 "a",
86 "\001",
87 "12",
88 "asdfadsfsad",
89 "01234567890ABCDEF",
90 NULL // End of the list
91 };
92
93const wchar_t *tohex_w [] = {
94 L"",
95 L"a",
96 L"\001",
97 L"12",
98 L"asdfadsfsad",
99 L"01234567890ABCDEF",
100 NULL // End of the list
101 };
102
103
104const char *fromhex [] = {
105 "20",
106 "2122234556FF",
107 NULL // End of the list
108 };
109
110const wchar_t *fromhex_w [] = {
111 L"11223320",
112 L"21222345010256FF",
113 NULL // End of the list
114 };
115
116
117
118BOOST_AUTO_TEST_CASE( test_main )
119{
120 test_to_hex ( tests: tohex );
121 test_to_hex ( tests: tohex_w );
122 test_from_hex_success ( tests: fromhex );
123 test_from_hex_success ( tests: fromhex_w );
124}
125

source code of boost/libs/algorithm/test/hex_test3.cpp