| 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 | Test non-string cases; vector and list |
| 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 | |
| 24 | const char *tohex [] = { |
| 25 | "" , |
| 26 | "a" , |
| 27 | "\001" , |
| 28 | "12" , |
| 29 | "asdfadsfsad" , |
| 30 | "01234567890ABCDEF" , |
| 31 | NULL // End of the list |
| 32 | }; |
| 33 | |
| 34 | void test_to_hex () { |
| 35 | for ( const char **p = tohex; *p; p++ ) { |
| 36 | std::deque<char> arg, argh; |
| 37 | std::list<char> one, two, three; |
| 38 | arg.assign ( first: *p, last: *p + strlen (s: *p)); |
| 39 | boost::algorithm::hex ( ptr: *p, out: std::back_inserter ( x&: one )); |
| 40 | boost::algorithm::hex ( r: arg, out: std::back_inserter ( x&: two )); |
| 41 | boost::algorithm::hex ( first: arg.begin (), last: arg.end (), out: std::back_inserter ( x&: three )); |
| 42 | BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ())); |
| 43 | BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ())); |
| 44 | |
| 45 | std::copy ( first: one.begin (), last: one.end (), result: std::back_inserter ( x&: argh )); |
| 46 | one.clear (); two.clear (); three.clear (); |
| 47 | |
| 48 | // boost::algorithm::unhex ( argh.c_str (), std::back_inserter ( one )); |
| 49 | boost::algorithm::unhex ( r: argh, out: std::back_inserter ( x&: two )); |
| 50 | boost::algorithm::unhex ( first: argh.begin (), last: argh.end (), out: std::back_inserter ( x&: three )); |
| 51 | // BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ())); |
| 52 | BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ())); |
| 53 | BOOST_CHECK ( std::equal ( two.begin (), two.end (), arg.begin ())); |
| 54 | } |
| 55 | |
| 56 | // Again, with a front_inserter |
| 57 | for ( const char **p = tohex; *p; p++ ) { |
| 58 | std::deque<char> arg, argh; |
| 59 | std::list<char> one, two, three; |
| 60 | arg.assign ( first: *p, last: *p + strlen (s: *p)); |
| 61 | boost::algorithm::hex ( ptr: *p, out: std::front_inserter ( x&: one )); |
| 62 | boost::algorithm::hex ( r: arg, out: std::front_inserter ( x&: two )); |
| 63 | boost::algorithm::hex ( first: arg.begin (), last: arg.end (), out: std::front_inserter ( x&: three )); |
| 64 | BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ())); |
| 65 | BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ())); |
| 66 | |
| 67 | // Copy, reversing |
| 68 | std::copy ( first: one.begin (), last: one.end (), result: std::front_inserter ( x&: argh )); |
| 69 | one.clear (); two.clear (); three.clear (); |
| 70 | |
| 71 | // boost::algorithm::unhex ( argh.c_str (), std::front_inserter ( one )); |
| 72 | boost::algorithm::unhex ( r: argh, out: std::front_inserter ( x&: two )); |
| 73 | boost::algorithm::unhex ( first: argh.begin (), last: argh.end (), out: std::front_inserter ( x&: three )); |
| 74 | // BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ())); |
| 75 | BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ())); |
| 76 | BOOST_CHECK ( std::equal ( two.begin (), two.end (), arg.rbegin ())); // reverse |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | const char *fromhex [] = { |
| 81 | "20" , |
| 82 | "2122234556FF" , |
| 83 | NULL // End of the list |
| 84 | }; |
| 85 | |
| 86 | |
| 87 | void test_from_hex_success () { |
| 88 | for ( const char **p = fromhex; *p; p++ ) { |
| 89 | std::deque<char> arg, argh; |
| 90 | std::list<char> one, two, three; |
| 91 | arg.assign ( first: *p, last: *p + strlen (s: *p)); |
| 92 | boost::algorithm::unhex ( ptr: *p, out: std::back_inserter ( x&: one )); |
| 93 | boost::algorithm::unhex ( r: arg, out: std::back_inserter ( x&: two )); |
| 94 | boost::algorithm::unhex ( first: arg.begin (), last: arg.end (), out: std::back_inserter ( x&: three )); |
| 95 | BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ())); |
| 96 | BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ())); |
| 97 | |
| 98 | std::copy ( first: one.begin (), last: one.end (), result: std::back_inserter ( x&: argh )); |
| 99 | one.clear (); two.clear (); three.clear (); |
| 100 | |
| 101 | // boost::algorithm::hex ( argh.c_str (), std::back_inserter ( one )); |
| 102 | boost::algorithm::hex ( r: argh, out: std::back_inserter ( x&: two )); |
| 103 | boost::algorithm::hex ( first: argh.begin (), last: argh.end (), out: std::back_inserter ( x&: three )); |
| 104 | // BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ())); |
| 105 | BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ())); |
| 106 | BOOST_CHECK ( std::equal ( two.begin (), two.end (), arg.begin ())); |
| 107 | } |
| 108 | |
| 109 | // Again, with a front_inserter |
| 110 | for ( const char **p = fromhex; *p; p++ ) { |
| 111 | std::deque<char> arg, argh; |
| 112 | std::list<char> one, two, three; |
| 113 | arg.assign ( first: *p, last: *p + strlen (s: *p)); |
| 114 | boost::algorithm::unhex ( ptr: *p, out: std::front_inserter ( x&: one )); |
| 115 | boost::algorithm::unhex ( r: arg, out: std::front_inserter ( x&: two )); |
| 116 | boost::algorithm::unhex ( first: arg.begin (), last: arg.end (), out: std::front_inserter ( x&: three )); |
| 117 | BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ())); |
| 118 | BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ())); |
| 119 | |
| 120 | // Copy, reversing |
| 121 | std::copy ( first: one.begin (), last: one.end (), result: std::front_inserter ( x&: argh )); |
| 122 | one.clear (); two.clear (); three.clear (); |
| 123 | |
| 124 | // boost::algorithm::hex ( argh.c_str (), std::front_inserter ( one )); |
| 125 | boost::algorithm::hex ( r: argh, out: std::front_inserter ( x&: two )); |
| 126 | boost::algorithm::hex ( first: argh.begin (), last: argh.end (), out: std::front_inserter ( x&: three )); |
| 127 | // BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ())); |
| 128 | BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ())); |
| 129 | BOOST_CHECK ( std::equal ( two.begin (), two.end (), arg.rbegin ())); // reversed |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | |
| 134 | BOOST_AUTO_TEST_CASE( test_main ) |
| 135 | { |
| 136 | test_to_hex (); |
| 137 | test_from_hex_success (); |
| 138 | } |
| 139 | |