Add hex/unhex algorithms suggested by Olaf

[SVN r77138]
This commit is contained in:
Marshall Clow
2012-02-28 23:17:52 +00:00
parent 49668cf66a
commit 1af25699b7
6 changed files with 722 additions and 0 deletions

View File

@ -39,6 +39,11 @@ import testing ;
[ run is_partitioned_test1.cpp : : : : is_partitioned_test1 ]
[ run partition_copy_test1.cpp : : : : partition_copy_test1 ]
# Hex tests
[ run hex_test1.cpp : : : : hex_test1 ]
[ run hex_test2.cpp : : : : hex_test2 ]
[ run hex_test3.cpp : : : : hex_test3 ]
[ compile-fail hex_fail1.cpp ]
;
}

25
test/hex_fail1.cpp Normal file
View File

@ -0,0 +1,25 @@
/*
Copyright (c) Marshall Clow 2011-2012.
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
For more information, see http://www.boost.org
*/
#include <boost/config.hpp>
#include <boost/algorithm/hex.hpp>
#include <boost/test/included/test_exec_monitor.hpp>
#include <string>
#include <iostream>
#include <vector>
// should not compile: vector is not an integral type
int test_main( int , char* [] )
{
std::vector<float> v;
std::string out;
boost::algorithm::unhex ( out, std::back_inserter(v));
return 0;
}

154
test/hex_test1.cpp Normal file
View File

@ -0,0 +1,154 @@
/*
Copyright (c) Marshall Clow 2011-2012.
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
For more information, see http://www.boost.org
*/
#include <boost/config.hpp>
#include <boost/algorithm/hex.hpp>
#include <boost/test/included/test_exec_monitor.hpp>
#include <string>
#include <iostream>
template<typename String>
void test_to_hex ( const typename String::value_type ** tests ) {
for ( const typename String::value_type **p = tests; *p; p++ ) {
String arg, argh, one, two, three, four;
arg.assign ( *p );
boost::algorithm::hex ( *p, std::back_inserter ( one ));
boost::algorithm::hex ( arg, std::back_inserter ( two ));
boost::algorithm::hex ( arg.begin (), arg.end (), std::back_inserter ( three ));
four = boost::algorithm::hex ( arg );
BOOST_CHECK ( one == two );
BOOST_CHECK ( one == three );
BOOST_CHECK ( one == four );
argh = one;
one.clear (); two.clear (); three.clear (); four.clear ();
boost::algorithm::unhex ( argh.c_str (), std::back_inserter ( one ));
boost::algorithm::unhex ( argh, std::back_inserter ( two ));
boost::algorithm::unhex ( argh.begin (), argh.end (), std::back_inserter ( three ));
four = boost::algorithm::unhex ( argh );
BOOST_CHECK ( one == two );
BOOST_CHECK ( one == three );
BOOST_CHECK ( one == four );
BOOST_CHECK ( one == arg );
}
}
template<typename String>
void test_from_hex_success ( const typename String::value_type ** tests ) {
for ( const typename String::value_type **p = tests; *p; p++ ) {
String arg, argh, one, two, three, four;
arg.assign ( *p );
boost::algorithm::unhex ( *p, std::back_inserter ( one ));
boost::algorithm::unhex ( arg, std::back_inserter ( two ));
boost::algorithm::unhex ( arg.begin (), arg.end (), std::back_inserter ( three ));
four = boost::algorithm::unhex ( arg );
BOOST_CHECK ( one == two );
BOOST_CHECK ( one == three );
BOOST_CHECK ( one == four );
argh = one;
one.clear (); two.clear (); three.clear (); four.clear ();
boost::algorithm::hex ( argh.c_str (), std::back_inserter ( one ));
boost::algorithm::hex ( argh, std::back_inserter ( two ));
boost::algorithm::hex ( argh.begin (), argh.end (), std::back_inserter ( three ));
four = boost::algorithm::hex ( argh );
BOOST_CHECK ( one == two );
BOOST_CHECK ( one == three );
BOOST_CHECK ( one == four );
BOOST_CHECK ( one == arg );
}
}
template<typename String>
void test_from_hex_failure ( const typename String::value_type ** tests ) {
int num_catches;
for ( const typename String::value_type **p = tests; *p; p++ ) {
String arg, one;
arg.assign ( *p );
num_catches = 0;
try { boost::algorithm::unhex ( *p, std::back_inserter ( one )); }
catch ( const boost::algorithm::hex_decode_error & /*ex*/ ) { num_catches++; }
try { boost::algorithm::unhex ( arg, std::back_inserter ( one )); }
catch ( const boost::algorithm::hex_decode_error & /*ex*/ ) { num_catches++; }
try { boost::algorithm::unhex ( arg.begin (), arg.end (), std::back_inserter ( one )); }
catch ( const boost::algorithm::hex_decode_error & /*ex*/ ) { num_catches++; }
BOOST_CHECK ( num_catches == 3 );
}
}
const char *tohex [] = {
"",
"a",
"\001",
"12",
"asdfadsfsad",
"01234567890ABCDEF",
NULL // End of the list
};
const wchar_t *tohex_w [] = {
L"",
L"a",
L"\001",
L"12",
L"asdfadsfsad",
L"01234567890ABCDEF",
NULL // End of the list
};
const char *fromhex [] = {
"20",
"2122234556FF",
NULL // End of the list
};
const wchar_t *fromhex_w [] = {
L"00101020",
L"2122234556FF3456",
NULL // End of the list
};
const char *fromhex_fail [] = {
"2",
"H",
"234",
"21222G4556FF",
NULL // End of the list
};
const wchar_t *fromhex_fail_w [] = {
L"2",
L"12",
L"H",
L"234",
L"21222G4556FF",
NULL // End of the list
};
int test_main( int , char* [] )
{
test_to_hex<std::string> ( tohex );
test_from_hex_success<std::string> ( fromhex );
test_from_hex_failure<std::string> ( fromhex_fail );
test_to_hex<std::wstring> ( tohex_w );
test_from_hex_success<std::wstring> ( fromhex_w );
test_from_hex_failure<std::wstring> ( fromhex_fail_w );
return 0;
}

137
test/hex_test2.cpp Normal file
View File

@ -0,0 +1,137 @@
/*
Copyright (c) Marshall Clow 2011-2012.
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
For more information, see http://www.boost.org
Test non-string cases; vector and list
*/
#include <boost/config.hpp>
#include <boost/algorithm/hex.hpp>
#include <boost/test/included/test_exec_monitor.hpp>
#include <string>
#include <iostream>
#include <deque>
#include <list>
const char *tohex [] = {
"",
"a",
"\001",
"12",
"asdfadsfsad",
"01234567890ABCDEF",
NULL // End of the list
};
void test_to_hex () {
for ( const char **p = tohex; *p; p++ ) {
std::deque<char> arg, argh;
std::list<char> one, two, three;
arg.assign ( *p, *p + strlen (*p));
boost::algorithm::hex ( *p, std::back_inserter ( one ));
boost::algorithm::hex ( arg, std::back_inserter ( two ));
boost::algorithm::hex ( arg.begin (), arg.end (), std::back_inserter ( three ));
BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ()));
BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ()));
std::copy ( one.begin (), one.end (), std::back_inserter ( argh ));
one.clear (); two.clear (); three.clear ();
// boost::algorithm::unhex ( argh.c_str (), std::back_inserter ( one ));
boost::algorithm::unhex ( argh, std::back_inserter ( two ));
boost::algorithm::unhex ( argh.begin (), argh.end (), std::back_inserter ( three ));
// BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ()));
BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ()));
BOOST_CHECK ( std::equal ( two.begin (), two.end (), arg.begin ()));
}
// Again, with a front_inserter
for ( const char **p = tohex; *p; p++ ) {
std::deque<char> arg, argh;
std::list<char> one, two, three;
arg.assign ( *p, *p + strlen (*p));
boost::algorithm::hex ( *p, std::front_inserter ( one ));
boost::algorithm::hex ( arg, std::front_inserter ( two ));
boost::algorithm::hex ( arg.begin (), arg.end (), std::front_inserter ( three ));
BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ()));
BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ()));
// Copy, reversing
std::copy ( one.begin (), one.end (), std::front_inserter ( argh ));
one.clear (); two.clear (); three.clear ();
// boost::algorithm::unhex ( argh.c_str (), std::front_inserter ( one ));
boost::algorithm::unhex ( argh, std::front_inserter ( two ));
boost::algorithm::unhex ( argh.begin (), argh.end (), std::front_inserter ( three ));
// BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ()));
BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ()));
BOOST_CHECK ( std::equal ( two.begin (), two.end (), arg.rbegin ())); // reverse
}
}
const char *fromhex [] = {
"20",
"2122234556FF",
NULL // End of the list
};
void test_from_hex_success () {
for ( const char **p = fromhex; *p; p++ ) {
std::deque<char> arg, argh;
std::list<char> one, two, three;
arg.assign ( *p, *p + strlen (*p));
boost::algorithm::unhex ( *p, std::back_inserter ( one ));
boost::algorithm::unhex ( arg, std::back_inserter ( two ));
boost::algorithm::unhex ( arg.begin (), arg.end (), std::back_inserter ( three ));
BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ()));
BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ()));
std::copy ( one.begin (), one.end (), std::back_inserter ( argh ));
one.clear (); two.clear (); three.clear ();
// boost::algorithm::hex ( argh.c_str (), std::back_inserter ( one ));
boost::algorithm::hex ( argh, std::back_inserter ( two ));
boost::algorithm::hex ( argh.begin (), argh.end (), std::back_inserter ( three ));
// BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ()));
BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ()));
BOOST_CHECK ( std::equal ( two.begin (), two.end (), arg.begin ()));
}
// Again, with a front_inserter
for ( const char **p = fromhex; *p; p++ ) {
std::deque<char> arg, argh;
std::list<char> one, two, three;
arg.assign ( *p, *p + strlen (*p));
boost::algorithm::unhex ( *p, std::front_inserter ( one ));
boost::algorithm::unhex ( arg, std::front_inserter ( two ));
boost::algorithm::unhex ( arg.begin (), arg.end (), std::front_inserter ( three ));
BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ()));
BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ()));
// Copy, reversing
std::copy ( one.begin (), one.end (), std::front_inserter ( argh ));
one.clear (); two.clear (); three.clear ();
// boost::algorithm::hex ( argh.c_str (), std::front_inserter ( one ));
boost::algorithm::hex ( argh, std::front_inserter ( two ));
boost::algorithm::hex ( argh.begin (), argh.end (), std::front_inserter ( three ));
// BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ()));
BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ()));
BOOST_CHECK ( std::equal ( two.begin (), two.end (), arg.rbegin ())); // reversed
}
}
int test_main( int , char* [] )
{
test_to_hex ();
test_from_hex_success ();
return 0;
}

123
test/hex_test3.cpp Normal file
View File

@ -0,0 +1,123 @@
/*
Copyright (c) Marshall Clow 2011-2012.
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
For more information, see http://www.boost.org
Try ostream_iterators
*/
#include <boost/config.hpp>
#include <boost/algorithm/hex.hpp>
#include <boost/test/included/test_exec_monitor.hpp>
#include <string>
#include <iostream>
#include <deque>
#include <list>
template <typename char_type>
void test_to_hex ( const char_type ** tests ) {
typedef std::basic_string<char_type> String;
typedef std::basic_ostringstream<char_type> Stream;
typedef std::ostream_iterator<char_type, char_type> Iter;
for ( const char_type **p = tests; *p; p++ ) {
String arg, argh;
Stream one, two, three;
arg.assign ( *p );
boost::algorithm::hex ( *p, Iter ( one ));
boost::algorithm::hex ( arg, Iter ( two ));
boost::algorithm::hex ( arg.begin (), arg.end (), Iter ( three ));
boost::algorithm::hex ( arg );
BOOST_CHECK ( one.str () == two.str ());
BOOST_CHECK ( one.str () == three.str ());
argh = one.str ();
one.str (String()); two.str (String()); three.str (String());
boost::algorithm::unhex ( argh.c_str (), Iter ( one ));
boost::algorithm::unhex ( argh, Iter ( two ));
boost::algorithm::unhex ( argh.begin (), argh.end (), Iter ( three ));
BOOST_CHECK ( one.str () == two.str ());
BOOST_CHECK ( one.str () == three.str ());
BOOST_CHECK ( one.str () == arg );
}
}
template <typename char_type>
void test_from_hex_success ( const char_type ** tests ) {
typedef std::basic_string<char_type> String;
typedef std::basic_ostringstream<char_type> Stream;
typedef std::ostream_iterator<char_type, char_type> Iter;
for ( const char_type **p = tests; *p; p++ ) {
String arg, argh;
Stream one, two, three;
arg.assign ( *p );
boost::algorithm::unhex ( *p, Iter ( one ));
boost::algorithm::unhex ( arg, Iter ( two ));
boost::algorithm::unhex ( arg.begin (), arg.end (), Iter ( three ));
BOOST_CHECK ( one.str () == two.str ());
BOOST_CHECK ( one.str () == three.str ());
argh = one.str ();
one.str (String()); two.str (String()); three.str (String());
boost::algorithm::hex ( argh.c_str (), Iter ( one ));
boost::algorithm::hex ( argh, Iter ( two ));
boost::algorithm::hex ( argh.begin (), argh.end (), Iter ( three ));
BOOST_CHECK ( one.str () == two.str ());
BOOST_CHECK ( one.str () == three.str ());
BOOST_CHECK ( one.str () == arg );
}
}
const char *tohex [] = {
"",
"a",
"\001",
"12",
"asdfadsfsad",
"01234567890ABCDEF",
NULL // End of the list
};
const wchar_t *tohex_w [] = {
L"",
L"a",
L"\001",
L"12",
L"asdfadsfsad",
L"01234567890ABCDEF",
NULL // End of the list
};
const char *fromhex [] = {
"20",
"2122234556FF",
NULL // End of the list
};
const wchar_t *fromhex_w [] = {
L"11223320",
L"21222345010256FF",
NULL // End of the list
};
int test_main( int , char* [] )
{
test_to_hex ( tohex );
test_to_hex ( tohex_w );
test_from_hex_success ( fromhex );
test_from_hex_success ( fromhex_w );
return 0;
}