format_test_exceptions.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // ------------------------------------------------------------------------------
  2. // format_test_exceptions.cpp : exception handling
  3. // ------------------------------------------------------------------------------
  4. // Copyright 2017 James E. King, III - Use, modification, and distribution are
  5. // subject to the Boost Software License, Version 1.0. (See accompanying
  6. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. // see http://www.boost.org/libs/format for library home page
  8. // ------------------------------------------------------------------------------
  9. #include <boost/algorithm/string.hpp>
  10. #include <boost/detail/lightweight_test.hpp>
  11. #include <boost/format.hpp>
  12. #include <iomanip>
  13. #include <iostream>
  14. #define CHECK_INVALID_0(FMT, EX) { BOOST_TEST_THROWS((boost::format(FMT)).str(), EX); \
  15. boost::format safe; safe.exceptions(boost::io::no_error_bits); safe.parse(FMT); (safe).str(); }
  16. #define CHECK_INVALID_1(FMT, _1, EX) { BOOST_TEST_THROWS((boost::format(FMT) % _1).str(), EX); \
  17. boost::format safe; safe.exceptions(boost::io::no_error_bits); safe.parse(FMT); (safe % _1).str(); }
  18. #define CHECK_INVALID_2(FMT, _1, _2, EX) { BOOST_TEST_THROWS((boost::format(FMT) % _1 % _2).str(), EX); \
  19. boost::format safe; safe.exceptions(boost::io::no_error_bits); safe.parse(FMT); (safe % _1 % _2).str(); }
  20. int main(int, char* [])
  21. {
  22. using namespace boost::io;
  23. // https://svn.boost.org/trac10/ticket/8735
  24. CHECK_INVALID_0("%", bad_format_string); // no conversion specifier
  25. CHECK_INVALID_0("%|", bad_format_string); // truncated
  26. CHECK_INVALID_0("%|%", bad_format_string); // mismatched bars
  27. CHECK_INVALID_0("%|2%", bad_format_string); // mismatched bars
  28. CHECK_INVALID_0("%'", bad_format_string); // flag ' is ignored, no conversion specifier
  29. CHECK_INVALID_0("%2", bad_format_string); // no terminating percent
  30. CHECK_INVALID_0("%*", bad_format_string); // truncated
  31. CHECK_INVALID_1("%$2", 1, bad_format_string); // no conversion specifier
  32. CHECK_INVALID_1("%$2.*", 1, bad_format_string); // no conversion specifier
  33. CHECK_INVALID_0("%0%", bad_format_string); // positional arguments start at 1
  34. CHECK_INVALID_2("%1%", 1, 2, too_many_args);
  35. CHECK_INVALID_1("%1% %2%", 1, too_few_args);
  36. CHECK_INVALID_0("%I", bad_format_string);
  37. CHECK_INVALID_0("%I2", bad_format_string);
  38. CHECK_INVALID_0("%I2d", bad_format_string);
  39. CHECK_INVALID_0("%I3", bad_format_string);
  40. CHECK_INVALID_0("%I3d", bad_format_string);
  41. CHECK_INVALID_0("%I32", bad_format_string);
  42. CHECK_INVALID_0("%I33", bad_format_string);
  43. CHECK_INVALID_0("%I6", bad_format_string);
  44. CHECK_INVALID_0("%I62", bad_format_string);
  45. CHECK_INVALID_0("%I63", bad_format_string);
  46. CHECK_INVALID_0("%I63d", bad_format_string);
  47. CHECK_INVALID_0("%I64", bad_format_string);
  48. CHECK_INVALID_0("%I128d", bad_format_string);
  49. CHECK_INVALID_0("%3.*4d", bad_format_string);
  50. // mixing positional and non-positional
  51. CHECK_INVALID_2("%1% %2d", 1, 2, bad_format_string);
  52. CHECK_INVALID_2("%2d %2%", 1, 2, bad_format_string);
  53. // found while improving coverage - a number following the * for width
  54. // or precision was being eaten instead of being treated as an error
  55. CHECK_INVALID_0("%1$*4d", bad_format_string);
  56. CHECK_INVALID_0("%1$05.*32d", bad_format_string);
  57. CHECK_INVALID_0("%1$05.*6", bad_format_string);
  58. // found while improving coverage, this caused an unhandled exception
  59. // the "T" conversion specifier didn't handle the exception flags properly
  60. CHECK_INVALID_0("%1$T", bad_format_string); // missing fill character
  61. // test what() on exceptions
  62. format_error base_err;
  63. BOOST_TEST_GT(strlen(base_err.what()), 0u);
  64. bad_format_string bfs(2, 3);
  65. BOOST_TEST(boost::contains(bfs.what(), "bad_format_string"));
  66. too_few_args tfa(5, 4);
  67. BOOST_TEST(boost::contains(tfa.what(), "format-string"));
  68. too_many_args tma(4, 5);
  69. BOOST_TEST(boost::contains(tma.what(), "format-string"));
  70. boost::io::out_of_range oor(1, 2, 3);
  71. BOOST_TEST(boost::contains(oor.what(), "out_of_range"));
  72. // bind and unbind
  73. boost::format two("%1%, %2%");
  74. two.bind_arg(1, 1);
  75. two.bind_arg(2, 2);
  76. BOOST_TEST_EQ(two.str(), "1, 2");
  77. two.clear_bind(1);
  78. BOOST_TEST_THROWS(two.str(), too_few_args);
  79. BOOST_TEST_THROWS(two.clear_bind(1), boost::io::out_of_range);
  80. two.exceptions(no_error_bits);
  81. two.clear_bind(1);
  82. two.str();
  83. return boost::report_errors();
  84. }