]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/format/test/format_test3.cpp
1 // ------------------------------------------------------------------------------
2 // format_test3.cpp : complicated format strings and / or advanced uses
3 // ------------------------------------------------------------------------------
5 // Copyright Samuel Krempp 2003. Use, modification, and distribution are
6 // subject to the Boost Software License, Version 1.0. (See accompanying
7 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 // see http://www.boost.org/libs/format for library home page
11 // ------------------------------------------------------------------------------
13 #define BOOST_FORMAT_STATIC_STREAM
14 #include "boost/format.hpp"
19 #define BOOST_INCLUDE_MAIN
20 #include <boost/test/test_tools.hpp>
24 Rational (int an
, int ad
) : n(an
), d(ad
) {}
27 std::ostream
& operator<<( std::ostream
& os
, const Rational
& r
) {
28 os
<< r
.n
<< "/" << r
.d
;
32 int test_main(int, char* [])
36 using boost::io::group
;
41 s
= str( format("[%=6s] [%+6s] [%+6s] [% 6s] [%+6s]\n")
43 % group(internal
, setfill('W'), 234)
44 % group(internal
, setfill('X'), -345)
45 % group(setfill('Y'), 456)
46 % group(setfill('Z'), -10 ) );
48 if(s
!= "[ 123 ] [+WW234] [-XX345] [YY 456] [ZZZ-10]\n" ) {
50 BOOST_ERROR("formatting error. (with special paddings)");
53 s
= str( format("[% 6.8s] [% 8.6s] [% 7.7s]\n")
54 % group(internal
, setfill('x'), Rational(12345,54321))
55 % group(internal
, setfill('x'), Rational(123,45))
56 % group(internal
, setfill('x'), Rational(123,321))
58 if(s
!= (s2
="[ 12345/5] [ xx123/4] [ 123/32]\n" )) {
60 BOOST_ERROR("formatting error. (with special paddings)");
63 s
= str( format("[% 6.8s] [% 6.8s] [% 6.8s] [% 6.8s] [%6.8s]\n")
65 % group(setfill('x'), 12)
66 % group(internal
, setfill('x'), 12)
67 % group(internal
, setfill('x'), 1234567890)
68 % group(internal
, setfill('x'), 123456)
70 if(s
!= (s2
="[ 1234567] [xxx 12] [ xxx12] [ 1234567] [123456]\n") ) {
72 BOOST_ERROR("formatting error. (with special paddings)");
75 s
= str( format("[% 8.6s] [% 6.4s] [% 6.4s] [% 8.6s] [% 8.6s]\n")
77 % group(setfill('x'), 12)
78 % group(internal
, setfill('x'), 12)
79 % group(internal
, setfill('x'), 1234567890)
80 % group(internal
, setfill('x'), 12345)
82 if(s
!= (s2
="[ 12345] [xxx 12] [ xxx12] [ xx12345] [ xx12345]\n") ) {
84 BOOST_ERROR("formatting error. (with special paddings)");
88 s
= str( format("%2$014x [%1%] %|2$05|\n") % (format("%05s / %s") % -18 % 7)
89 %group(showbase
, -100)
91 if( s
!= "0x0000ffffff9c [-0018 / 7] -0100\n" ){
93 BOOST_ERROR("nesting did not work");
96 // bind args, and various arguments counts :
98 boost::format
bf("%1% %4% %1%");
99 bf
.bind_arg(1, "one") % 2 % "three" ;
100 BOOST_CHECK_EQUAL(bf
.expected_args(), 4);
101 BOOST_CHECK_EQUAL(bf
.fed_args(), 2);
102 BOOST_CHECK_EQUAL(bf
.bound_args(), 1);
103 BOOST_CHECK_EQUAL(bf
.remaining_args(), 1);
104 BOOST_CHECK_EQUAL(bf
.cur_arg(), 4);
106 bf
% "one" % 2 % "three" ;
107 BOOST_CHECK_EQUAL(bf
.expected_args(), 4);
108 BOOST_CHECK_EQUAL(bf
.fed_args(), 3);
109 BOOST_CHECK_EQUAL(bf
.bound_args(), 0);
110 BOOST_CHECK_EQUAL(bf
.remaining_args(), 1);
111 BOOST_CHECK_EQUAL(bf
.cur_arg(), 4);
113 // testcase for bug reported at
114 // http://lists.boost.org/boost-users/2006/05/19723.php
122 // testcase for bug reported at
123 // http://lists.boost.org/boost-users/2005/11/15454.php
125 std::string l_str
= (boost::format("here is an empty string: %1%") % l_param
).str();
126 BOOST_CHECK_EQUAL(std::string("here is an empty string: "), l_str
);
128 // testcase for SourceForge bug #1506914
129 std::string arg
; // empty string
130 s
= str(format("%=8s") % arg
);
131 BOOST_CHECK_EQUAL(std::string(" "), s
);