hex_test2.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. Test non-string cases; vector and list
  7. */
  8. #include <boost/config.hpp>
  9. #include <boost/algorithm/hex.hpp>
  10. #define BOOST_TEST_MAIN
  11. #include <boost/test/unit_test.hpp>
  12. #include <string>
  13. #include <iostream>
  14. #include <deque>
  15. #include <list>
  16. const char *tohex [] = {
  17. "",
  18. "a",
  19. "\001",
  20. "12",
  21. "asdfadsfsad",
  22. "01234567890ABCDEF",
  23. NULL // End of the list
  24. };
  25. void test_to_hex () {
  26. for ( const char **p = tohex; *p; p++ ) {
  27. std::deque<char> arg, argh;
  28. std::list<char> one, two, three;
  29. arg.assign ( *p, *p + strlen (*p));
  30. boost::algorithm::hex ( *p, std::back_inserter ( one ));
  31. boost::algorithm::hex ( arg, std::back_inserter ( two ));
  32. boost::algorithm::hex ( arg.begin (), arg.end (), std::back_inserter ( three ));
  33. BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ()));
  34. BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ()));
  35. std::copy ( one.begin (), one.end (), std::back_inserter ( argh ));
  36. one.clear (); two.clear (); three.clear ();
  37. // boost::algorithm::unhex ( argh.c_str (), std::back_inserter ( one ));
  38. boost::algorithm::unhex ( argh, std::back_inserter ( two ));
  39. boost::algorithm::unhex ( argh.begin (), argh.end (), std::back_inserter ( three ));
  40. // BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ()));
  41. BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ()));
  42. BOOST_CHECK ( std::equal ( two.begin (), two.end (), arg.begin ()));
  43. }
  44. // Again, with a front_inserter
  45. for ( const char **p = tohex; *p; p++ ) {
  46. std::deque<char> arg, argh;
  47. std::list<char> one, two, three;
  48. arg.assign ( *p, *p + strlen (*p));
  49. boost::algorithm::hex ( *p, std::front_inserter ( one ));
  50. boost::algorithm::hex ( arg, std::front_inserter ( two ));
  51. boost::algorithm::hex ( arg.begin (), arg.end (), std::front_inserter ( three ));
  52. BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ()));
  53. BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ()));
  54. // Copy, reversing
  55. std::copy ( one.begin (), one.end (), std::front_inserter ( argh ));
  56. one.clear (); two.clear (); three.clear ();
  57. // boost::algorithm::unhex ( argh.c_str (), std::front_inserter ( one ));
  58. boost::algorithm::unhex ( argh, std::front_inserter ( two ));
  59. boost::algorithm::unhex ( argh.begin (), argh.end (), std::front_inserter ( three ));
  60. // BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ()));
  61. BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ()));
  62. BOOST_CHECK ( std::equal ( two.begin (), two.end (), arg.rbegin ())); // reverse
  63. }
  64. }
  65. const char *fromhex [] = {
  66. "20",
  67. "2122234556FF",
  68. NULL // End of the list
  69. };
  70. void test_from_hex_success () {
  71. for ( const char **p = fromhex; *p; p++ ) {
  72. std::deque<char> arg, argh;
  73. std::list<char> one, two, three;
  74. arg.assign ( *p, *p + strlen (*p));
  75. boost::algorithm::unhex ( *p, std::back_inserter ( one ));
  76. boost::algorithm::unhex ( arg, std::back_inserter ( two ));
  77. boost::algorithm::unhex ( arg.begin (), arg.end (), std::back_inserter ( three ));
  78. BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ()));
  79. BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ()));
  80. std::copy ( one.begin (), one.end (), std::back_inserter ( argh ));
  81. one.clear (); two.clear (); three.clear ();
  82. // boost::algorithm::hex ( argh.c_str (), std::back_inserter ( one ));
  83. boost::algorithm::hex ( argh, std::back_inserter ( two ));
  84. boost::algorithm::hex ( argh.begin (), argh.end (), std::back_inserter ( three ));
  85. // BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ()));
  86. BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ()));
  87. BOOST_CHECK ( std::equal ( two.begin (), two.end (), arg.begin ()));
  88. }
  89. // Again, with a front_inserter
  90. for ( const char **p = fromhex; *p; p++ ) {
  91. std::deque<char> arg, argh;
  92. std::list<char> one, two, three;
  93. arg.assign ( *p, *p + strlen (*p));
  94. boost::algorithm::unhex ( *p, std::front_inserter ( one ));
  95. boost::algorithm::unhex ( arg, std::front_inserter ( two ));
  96. boost::algorithm::unhex ( arg.begin (), arg.end (), std::front_inserter ( three ));
  97. BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ()));
  98. BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ()));
  99. // Copy, reversing
  100. std::copy ( one.begin (), one.end (), std::front_inserter ( argh ));
  101. one.clear (); two.clear (); three.clear ();
  102. // boost::algorithm::hex ( argh.c_str (), std::front_inserter ( one ));
  103. boost::algorithm::hex ( argh, std::front_inserter ( two ));
  104. boost::algorithm::hex ( argh.begin (), argh.end (), std::front_inserter ( three ));
  105. // BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ()));
  106. BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ()));
  107. BOOST_CHECK ( std::equal ( two.begin (), two.end (), arg.rbegin ())); // reversed
  108. }
  109. }
  110. BOOST_AUTO_TEST_CASE( test_main )
  111. {
  112. test_to_hex ();
  113. test_from_hex_success ();
  114. }