symbols_add_null.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*=============================================================================
  2. Copyright (c) 2004 Joao Abecasis
  3. http://spirit.sourceforge.net/
  4. Use, modification and distribution is subject to the Boost Software
  5. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. // This test requires NDEBUG to be undefined, because it depends on
  9. // BOOST_SPIRIT_ASSERT throwing an exception.
  10. #ifdef NDEBUG
  11. # undef NDEBUG
  12. #endif
  13. #include <boost/config.hpp>
  14. #include <stdexcept>
  15. #define BOOST_SPIRIT_ASSERT_EXCEPTION ::spirit_exception
  16. struct spirit_exception : std::exception
  17. {
  18. spirit_exception(char const * msg)
  19. : message(msg)
  20. {
  21. }
  22. ~spirit_exception() BOOST_NOEXCEPT_OR_NOTHROW {}
  23. char const* what() const BOOST_NOEXCEPT_OR_NOTHROW { return message; }
  24. char const * message;
  25. };
  26. #include <boost/spirit/include/classic_scanner.hpp>
  27. #include <boost/spirit/home/classic/symbols/impl/tst.ipp>
  28. #include <boost/utility/addressof.hpp>
  29. #include <boost/detail/lightweight_test.hpp>
  30. typedef char char_type;
  31. typedef char const * iterator;
  32. char_type data_[] = "whatever";
  33. iterator begin = data_;
  34. iterator end = data_
  35. + sizeof(data_)/sizeof(char_type); // Yes, this is an intentional bug ;)
  36. char_type data2_[] = "\0something";
  37. iterator begin2 = data2_;
  38. iterator end2 = data2_ + sizeof(data2_)/sizeof(char_type) - 1;
  39. int main()
  40. {
  41. typedef BOOST_SPIRIT_CLASSIC_NS::impl::tst<void *, char_type> symbols;
  42. symbols symbols_;
  43. try
  44. {
  45. // It is not ok to add strings containing the null character.
  46. symbols_.add(begin, end, (void*) boost::addressof(symbols_));
  47. BOOST_TEST(0);
  48. }
  49. catch (spirit_exception &/*e*/)
  50. {
  51. }
  52. try
  53. {
  54. // It is not ok to add strings containing the null character.
  55. symbols_.add(begin2, end2, (void*) boost::addressof(symbols_));
  56. BOOST_TEST(0);
  57. }
  58. catch (spirit_exception &/*e*/)
  59. {
  60. }
  61. return boost::report_errors();
  62. }