form_c_decor.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2015.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file form_c_decor.cpp
  9. * \author Andrey Semashev
  10. * \date 26.09.2015
  11. *
  12. * \brief This header contains tests for the \c c_decor and \c c_ascii_decor formatter.
  13. */
  14. #define BOOST_TEST_MODULE form_c_decor
  15. #include <string>
  16. #include <boost/test/unit_test.hpp>
  17. #include <boost/log/attributes/constant.hpp>
  18. #include <boost/log/attributes/attribute_set.hpp>
  19. #include <boost/log/utility/type_dispatch/standard_types.hpp>
  20. #include <boost/log/utility/formatting_ostream.hpp>
  21. #include <boost/log/expressions/attr.hpp>
  22. #include <boost/log/expressions/formatter.hpp>
  23. #include <boost/log/expressions/formatters/c_decorator.hpp>
  24. #include <boost/log/expressions/formatters/stream.hpp>
  25. #include <boost/log/core/record.hpp>
  26. #include "char_definitions.hpp"
  27. #include "make_record.hpp"
  28. namespace logging = boost::log;
  29. namespace attrs = logging::attributes;
  30. namespace expr = logging::expressions;
  31. namespace {
  32. template< typename >
  33. struct test_strings;
  34. #ifdef BOOST_LOG_USE_CHAR
  35. template< >
  36. struct test_strings< char > : public test_data< char >
  37. {
  38. static const char* chars() { return "\a\b\f\n\r\t\v !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; }
  39. static const char* escaped_chars() { return "\\a\\b\\f\\n\\r\\t\\v !\\\"#$%&\\'()*+,-./0123456789:;<=>\\?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; }
  40. static const char* special_chars() { return "\a\b\f\n\r\t\v !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82"; }
  41. static const char* escaped_special_chars() { return "\\a\\b\\f\\n\\r\\t\\v !\\\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\x7f\\x80\\x81\\x82"; }
  42. };
  43. #endif
  44. #ifdef BOOST_LOG_USE_WCHAR_T
  45. template< >
  46. struct test_strings< wchar_t > : public test_data< wchar_t >
  47. {
  48. static const wchar_t* chars() { return L"\a\b\f\n\r\t\v !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; }
  49. static const wchar_t* escaped_chars() { return L"\\a\\b\\f\\n\\r\\t\\v !\\\"#$%&\\'()*+,-./0123456789:;<=>\\?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; }
  50. static const wchar_t* special_chars() { return L"\a\b\f\n\r\t\v !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82"; }
  51. static const wchar_t* escaped_special_chars()
  52. {
  53. if (sizeof(wchar_t) == 1)
  54. return L"\\a\\b\\f\\n\\r\\t\\v !\\\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\x7f\\x80\\x81\\x82";
  55. if (sizeof(wchar_t) == 2)
  56. return L"\\a\\b\\f\\n\\r\\t\\v !\\\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\x007f\\x0080\\x0081\\x0082";
  57. if (sizeof(wchar_t) == 4)
  58. return L"\\a\\b\\f\\n\\r\\t\\v !\\\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\x0000007f\\x00000080\\x00000081\\x00000082";
  59. return L"\\a\\b\\f\\n\\r\\t\\v !\\\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\x000000000000007f\\x0000000000000080\\x0000000000000081\\x0000000000000082";
  60. }
  61. };
  62. #endif
  63. } // namespace
  64. BOOST_AUTO_TEST_CASE_TEMPLATE(c_decorator_formatting, CharT, char_types)
  65. {
  66. typedef logging::record_view record_view;
  67. typedef logging::attribute_set attr_set;
  68. typedef std::basic_string< CharT > string;
  69. typedef logging::basic_formatting_ostream< CharT > osstream;
  70. typedef logging::basic_formatter< CharT > formatter;
  71. typedef test_strings< CharT > data;
  72. attrs::constant< string > attr1(data::chars());
  73. attr_set set1;
  74. set1[data::attr1()] = attr1;
  75. record_view rec = make_record_view(set1);
  76. {
  77. string str1, str2;
  78. osstream strm1(str1), strm2(str2);
  79. formatter f = expr::stream << expr::make_c_decor< CharT >()[ expr::stream << expr::attr< string >(data::attr1()) ];
  80. f(rec, strm1);
  81. strm2 << data::escaped_chars();
  82. BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
  83. }
  84. }
  85. BOOST_AUTO_TEST_CASE_TEMPLATE(c_ascii_decorator_formatting, CharT, char_types)
  86. {
  87. typedef logging::record_view record_view;
  88. typedef logging::attribute_set attr_set;
  89. typedef std::basic_string< CharT > string;
  90. typedef logging::basic_formatting_ostream< CharT > osstream;
  91. typedef logging::basic_formatter< CharT > formatter;
  92. typedef test_strings< CharT > data;
  93. attrs::constant< string > attr1(data::chars());
  94. attr_set set1;
  95. set1[data::attr1()] = attr1;
  96. record_view rec = make_record_view(set1);
  97. {
  98. string str1, str2;
  99. osstream strm1(str1), strm2(str2);
  100. formatter f = expr::stream << expr::make_c_ascii_decor< CharT >()[ expr::stream << expr::attr< string >(data::attr1()) ];
  101. f(rec, strm1);
  102. strm2 << data::escaped_chars();
  103. BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
  104. }
  105. }