hex_test3.cpp 3.5 KB

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