hex_test1.cpp 6.2 KB

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