]>
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 #include <boost/detail/lightweight_test.hpp>
14 #include <boost/format.hpp>
20 Rational (int an
, int ad
) : n(an
), d(ad
) {}
23 std::ostream
& operator<<( std::ostream
& os
, const Rational
& r
) {
24 os
<< r
.n
<< "/" << r
.d
;
28 int main(int, char* [])
32 using boost::io::group
;
37 s
= str( format("[%=6s] [%+6s] [%+6s] [% 6s] [%+6s]\n")
39 % group(internal
, setfill('W'), 234)
40 % group(internal
, setfill('X'), -345)
41 % group(setfill('Y'), 456)
42 % group(setfill('Z'), -10 ) );
44 if(s
!= "[ 123 ] [+WW234] [-XX345] [YY 456] [ZZZ-10]\n" ) {
46 BOOST_ERROR("formatting error. (with special paddings)");
49 s
= str( format("[% 6.8s] [% 8.6s] [% 7.7s]\n")
50 % group(internal
, setfill('x'), Rational(12345,54321))
51 % group(internal
, setfill('x'), Rational(123,45))
52 % group(internal
, setfill('x'), Rational(123,321))
54 if(s
!= (s2
="[ 12345/5] [ xx123/4] [ 123/32]\n" )) {
56 BOOST_ERROR("formatting error. (with special paddings)");
59 s
= str( format("[% 6.8s] [% 6.8s] [% 6.8s] [% 6.8s] [%6.8s]\n")
61 % group(setfill('x'), 12)
62 % group(internal
, setfill('x'), 12)
63 % group(internal
, setfill('x'), 1234567890)
64 % group(internal
, setfill('x'), 123456)
66 if(s
!= (s2
="[ 1234567] [xxx 12] [ xxx12] [ 1234567] [123456]\n") ) {
68 BOOST_ERROR("formatting error. (with special paddings)");
71 s
= str( format("[% 8.6s] [% 6.4s] [% 6.4s] [% 8.6s] [% 8.6s]\n")
73 % group(setfill('x'), 12)
74 % group(internal
, setfill('x'), 12)
75 % group(internal
, setfill('x'), 1234567890)
76 % group(internal
, setfill('x'), 12345)
78 if(s
!= (s2
="[ 12345] [xxx 12] [ xxx12] [ xx12345] [ xx12345]\n") ) {
80 BOOST_ERROR("formatting error. (with special paddings)");
84 s
= str( format("%2$014x [%1%] %|2$05|\n") % (format("%05s / %s") % -18 % 7)
85 %group(showbase
, -100)
87 if( s
!= "0x0000ffffff9c [-0018 / 7] -0100\n" ){
89 BOOST_ERROR("nesting did not work");
92 // bind args, and various arguments counts :
94 boost::format
bf("%1% %4% %1%");
95 bf
.bind_arg(1, "one") % 2 % "three" ;
96 BOOST_TEST_EQ(bf
.expected_args(), 4);
97 BOOST_TEST_EQ(bf
.fed_args(), 2);
98 BOOST_TEST_EQ(bf
.bound_args(), 1);
99 BOOST_TEST_EQ(bf
.remaining_args(), 1);
100 BOOST_TEST_EQ(bf
.cur_arg(), 4);
102 bf
% "one" % 2 % "three" ;
103 BOOST_TEST_EQ(bf
.expected_args(), 4);
104 BOOST_TEST_EQ(bf
.fed_args(), 3);
105 BOOST_TEST_EQ(bf
.bound_args(), 0);
106 BOOST_TEST_EQ(bf
.remaining_args(), 1);
107 BOOST_TEST_EQ(bf
.cur_arg(), 4);
109 // testcase for bug reported at
110 // http://lists.boost.org/boost-users/2006/05/19723.php
118 // testcase for bug reported at
119 // http://lists.boost.org/boost-users/2005/11/15454.php
121 std::string l_str
= (boost::format("here is an empty string: %1%") % l_param
).str();
122 BOOST_TEST_EQ(std::string("here is an empty string: "), l_str
);
124 // testcase for SourceForge bug #1506914
125 std::string arg
; // empty string
126 s
= str(format("%=8s") % arg
);
127 BOOST_TEST_EQ(std::string(" "), s
);
129 return boost::report_errors();