hex_test4.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #include <boost/exception/get_error_info.hpp>
  11. #define BOOST_TEST_MAIN
  12. #include <boost/test/unit_test.hpp>
  13. #include <string>
  14. #include <iostream>
  15. namespace ba = boost::algorithm;
  16. void test_short_input1 () {
  17. std::string s;
  18. try { ba::unhex ( std::string ( "A" ), std::back_inserter(s)); }
  19. catch ( const std::exception &ex ) { return; }
  20. BOOST_TEST_MESSAGE ( "Failed to catch std::exception in test_short_input1" );
  21. BOOST_CHECK ( false );
  22. }
  23. void test_short_input2 () {
  24. std::string s;
  25. try { ba::unhex ( std::string ( "A" ), std::back_inserter(s)); }
  26. catch ( const ba::hex_decode_error &ex ) { return; }
  27. BOOST_TEST_MESSAGE ( "Failed to catch ba::hex_decode_error in test_short_input2" );
  28. BOOST_CHECK ( false );
  29. }
  30. void test_short_input3 () {
  31. std::string s;
  32. try { ba::unhex ( std::string ( "A" ), std::back_inserter(s)); }
  33. catch ( const ba::not_enough_input &ex ) { return; }
  34. BOOST_TEST_MESSAGE ( "Failed to catch ba::not_enough_input in test_short_input3" );
  35. BOOST_CHECK ( false );
  36. }
  37. // Make sure that the right thing is thrown
  38. void test_short_input4 () {
  39. std::string s;
  40. try { ba::unhex ( std::string ( "A" ), std::back_inserter(s)); }
  41. catch ( const ba::non_hex_input &ex ) { BOOST_CHECK ( false ); }
  42. catch ( const ba::not_enough_input &ex ) { return; }
  43. catch ( ... ) { BOOST_CHECK ( false ); }
  44. BOOST_CHECK ( false );
  45. }
  46. // Make sure that the right thing is thrown
  47. void test_short_input5 () {
  48. std::string s;
  49. try { ba::unhex ( "A", std::back_inserter(s)); }
  50. catch ( const ba::non_hex_input &ex ) { BOOST_CHECK ( false ); }
  51. catch ( const ba::not_enough_input &ex ) { return; }
  52. catch ( ... ) { BOOST_CHECK ( false ); }
  53. BOOST_CHECK ( false );
  54. }
  55. void test_short_input () {
  56. // BOOST_TEST_MESSAGE ( "Short input tests for boost::algorithm::unhex" );
  57. test_short_input1 ();
  58. test_short_input2 ();
  59. test_short_input3 ();
  60. test_short_input4 ();
  61. test_short_input5 ();
  62. }
  63. void test_nonhex_input1 () {
  64. std::string s;
  65. try { ba::unhex ( "01234FG1234", std::back_inserter(s)); }
  66. catch ( const std::exception &ex ) {
  67. BOOST_CHECK ( 'G' == *boost::get_error_info<ba::bad_char>(ex));
  68. return;
  69. }
  70. catch ( ... ) {}
  71. BOOST_TEST_MESSAGE ( "Failed to catch std::exception in test_nonhex_input1" );
  72. BOOST_CHECK ( false );
  73. }
  74. void test_nonhex_input2 () {
  75. std::string s;
  76. try { ba::unhex ( "012Z4FA1234", std::back_inserter(s)); }
  77. catch ( const ba::hex_decode_error &ex ) {
  78. BOOST_CHECK ( 'Z' == *boost::get_error_info<ba::bad_char>(ex));
  79. return;
  80. }
  81. catch ( ... ) {}
  82. BOOST_TEST_MESSAGE ( "Failed to catch ba::hex_decode_error in test_nonhex_input2" );
  83. BOOST_CHECK ( false );
  84. }
  85. void test_nonhex_input3 () {
  86. std::string s;
  87. try { ba::unhex ( "01234FA12Q4", std::back_inserter(s)); }
  88. catch ( const ba::non_hex_input &ex ) {
  89. BOOST_CHECK ( 'Q' == *boost::get_error_info<ba::bad_char>(ex));
  90. return;
  91. }
  92. catch ( ... ) {}
  93. BOOST_TEST_MESSAGE ( "Failed to catch ba::non_hex_input in test_nonhex_input3" );
  94. BOOST_CHECK ( false );
  95. }
  96. // Make sure that the right thing is thrown
  97. void test_nonhex_input4 () {
  98. std::string s;
  99. try { ba::unhex ( "P1234FA1234", std::back_inserter(s)); }
  100. catch ( const ba::not_enough_input &ex ) { BOOST_CHECK ( false ); }
  101. catch ( const ba::non_hex_input &ex ) { return; }
  102. catch ( ... ) { BOOST_CHECK ( false ); }
  103. BOOST_CHECK ( false );
  104. }
  105. void test_nonhex_input () {
  106. // BOOST_TEST_MESSAGE ( "Non hex input tests for boost::algorithm::unhex" );
  107. test_nonhex_input1 ();
  108. test_nonhex_input2 ();
  109. test_nonhex_input3 ();
  110. test_nonhex_input4 ();
  111. }
  112. BOOST_AUTO_TEST_CASE( test_main )
  113. {
  114. test_short_input ();
  115. test_nonhex_input ();
  116. }