format_test3.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // ------------------------------------------------------------------------------
  2. // format_test3.cpp : complicated format strings and / or advanced uses
  3. // ------------------------------------------------------------------------------
  4. // Copyright Samuel Krempp 2003. 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/detail/lightweight_test.hpp>
  10. #include <boost/format.hpp>
  11. #include <iostream>
  12. #include <iomanip>
  13. struct Rational {
  14. int n,d;
  15. Rational (int an, int ad) : n(an), d(ad) {}
  16. };
  17. std::ostream& operator<<( std::ostream& os, const Rational& r) {
  18. os << r.n << "/" << r.d;
  19. return os;
  20. }
  21. int main(int, char* [])
  22. {
  23. using namespace std;
  24. using boost::format;
  25. using boost::io::group;
  26. using boost::str;
  27. string s, s2;
  28. // special paddings
  29. s = str( format("[%=6s] [%+6s] [%+6s] [% 6s] [%+6s]\n")
  30. % 123
  31. % group(internal, setfill('W'), 234)
  32. % group(internal, setfill('X'), -345)
  33. % group(setfill('Y'), 456)
  34. % group(setfill('Z'), -10 ) );
  35. if(s != "[ 123 ] [+WW234] [-XX345] [YY 456] [ZZZ-10]\n" ) {
  36. cerr << s ;
  37. BOOST_ERROR("formatting error. (with special paddings)");
  38. }
  39. s = str( format("[% 6.8s] [% 8.6s] [% 7.7s]\n")
  40. % group(internal, setfill('x'), Rational(12345,54321))
  41. % group(internal, setfill('x'), Rational(123,45))
  42. % group(internal, setfill('x'), Rational(123,321))
  43. );
  44. if(s != (s2="[ 12345/5] [ xx123/4] [ 123/32]\n" )) {
  45. cerr << s << s2;
  46. BOOST_ERROR("formatting error. (with special paddings)");
  47. }
  48. s = str( format("[% 6.8s] [% 6.8s] [% 6.8s] [% 6.8s] [%6.8s]\n")
  49. % 1234567897
  50. % group(setfill('x'), 12)
  51. % group(internal, setfill('x'), 12)
  52. % group(internal, setfill('x'), 1234567890)
  53. % group(internal, setfill('x'), 123456)
  54. );
  55. if(s != (s2="[ 1234567] [xxx 12] [ xxx12] [ 1234567] [123456]\n") ) {
  56. cerr << s << s2;
  57. BOOST_ERROR("formatting error. (with special paddings)");
  58. }
  59. s = str( format("[% 8.6s] [% 6.4s] [% 6.4s] [% 8.6s] [% 8.6s]\n")
  60. % 1234567897
  61. % group(setfill('x'), 12)
  62. % group(internal, setfill('x'), 12)
  63. % group(internal, setfill('x'), 1234567890)
  64. % group(internal, setfill('x'), 12345)
  65. );
  66. if(s != (s2="[ 12345] [xxx 12] [ xxx12] [ xx12345] [ xx12345]\n") ) {
  67. cerr << s << s2;
  68. BOOST_ERROR("formatting error. (with special paddings)");
  69. }
  70. // nesting formats :
  71. s = str( format("%2$014x [%1%] %|2$05|\n") % (format("%05s / %s") % -18 % 7)
  72. %group(showbase, -100)
  73. );
  74. if( s != "0x0000ffffff9c [-0018 / 7] -0100\n" ){
  75. cerr << s ;
  76. BOOST_ERROR("nesting did not work");
  77. }
  78. // bind args, and various arguments counts :
  79. {
  80. boost::format bf("%1% %4% %1%");
  81. bf.bind_arg(1, "one") % 2 % "three" ;
  82. BOOST_TEST_EQ(bf.expected_args(), 4);
  83. BOOST_TEST_EQ(bf.fed_args(), 2);
  84. BOOST_TEST_EQ(bf.bound_args(), 1);
  85. BOOST_TEST_EQ(bf.remaining_args(), 1);
  86. BOOST_TEST_EQ(bf.cur_arg(), 4);
  87. bf.clear_binds();
  88. bf % "one" % 2 % "three" ;
  89. BOOST_TEST_EQ(bf.expected_args(), 4);
  90. BOOST_TEST_EQ(bf.fed_args(), 3);
  91. BOOST_TEST_EQ(bf.bound_args(), 0);
  92. BOOST_TEST_EQ(bf.remaining_args(), 1);
  93. BOOST_TEST_EQ(bf.cur_arg(), 4);
  94. }
  95. // testcase for bug reported at
  96. // http://lists.boost.org/boost-users/2006/05/19723.php
  97. {
  98. format f("%40t%1%");
  99. int x = 0;
  100. f.bind_arg(1, x);
  101. f.clear();
  102. }
  103. // testcase for bug reported at
  104. // http://lists.boost.org/boost-users/2005/11/15454.php
  105. std::string l_param;
  106. std::string l_str = (boost::format("here is an empty string: %1%") % l_param).str();
  107. BOOST_TEST_EQ(std::string("here is an empty string: "), l_str);
  108. // testcase for SourceForge bug #1506914
  109. std::string arg; // empty string
  110. s = str(format("%=8s") % arg);
  111. BOOST_TEST_EQ(std::string(" "), s);
  112. return boost::report_errors();
  113. }