]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/format/test/format_test3.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / format / test / format_test3.cpp
1 // ------------------------------------------------------------------------------
2 // format_test3.cpp : complicated format strings and / or advanced uses
3 // ------------------------------------------------------------------------------
4
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)
8
9 // see http://www.boost.org/libs/format for library home page
10
11 // ------------------------------------------------------------------------------
12
13 #define BOOST_FORMAT_STATIC_STREAM
14 #include "boost/format.hpp"
15
16 #include <iostream>
17 #include <iomanip>
18
19 #define BOOST_INCLUDE_MAIN
20 #include <boost/test/test_tools.hpp>
21
22 struct Rational {
23 int n,d;
24 Rational (int an, int ad) : n(an), d(ad) {}
25 };
26
27 std::ostream& operator<<( std::ostream& os, const Rational& r) {
28 os << r.n << "/" << r.d;
29 return os;
30 }
31
32 int test_main(int, char* [])
33 {
34 using namespace std;
35 using boost::format;
36 using boost::io::group;
37 using boost::str;
38
39 string s, s2;
40 // special paddings
41 s = str( format("[%=6s] [%+6s] [%+6s] [% 6s] [%+6s]\n")
42 % 123
43 % group(internal, setfill('W'), 234)
44 % group(internal, setfill('X'), -345)
45 % group(setfill('Y'), 456)
46 % group(setfill('Z'), -10 ) );
47
48 if(s != "[ 123 ] [+WW234] [-XX345] [YY 456] [ZZZ-10]\n" ) {
49 cerr << s ;
50 BOOST_ERROR("formatting error. (with special paddings)");
51 }
52
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))
57 );
58 if(s != (s2="[ 12345/5] [ xx123/4] [ 123/32]\n" )) {
59 cerr << s << s2;
60 BOOST_ERROR("formatting error. (with special paddings)");
61 }
62
63 s = str( format("[% 6.8s] [% 6.8s] [% 6.8s] [% 6.8s] [%6.8s]\n")
64 % 1234567897
65 % group(setfill('x'), 12)
66 % group(internal, setfill('x'), 12)
67 % group(internal, setfill('x'), 1234567890)
68 % group(internal, setfill('x'), 123456)
69 );
70 if(s != (s2="[ 1234567] [xxx 12] [ xxx12] [ 1234567] [123456]\n") ) {
71 cerr << s << s2;
72 BOOST_ERROR("formatting error. (with special paddings)");
73 }
74
75 s = str( format("[% 8.6s] [% 6.4s] [% 6.4s] [% 8.6s] [% 8.6s]\n")
76 % 1234567897
77 % group(setfill('x'), 12)
78 % group(internal, setfill('x'), 12)
79 % group(internal, setfill('x'), 1234567890)
80 % group(internal, setfill('x'), 12345)
81 );
82 if(s != (s2="[ 12345] [xxx 12] [ xxx12] [ xx12345] [ xx12345]\n") ) {
83 cerr << s << s2;
84 BOOST_ERROR("formatting error. (with special paddings)");
85 }
86
87 // nesting formats :
88 s = str( format("%2$014x [%1%] %|2$05|\n") % (format("%05s / %s") % -18 % 7)
89 %group(showbase, -100)
90 );
91 if( s != "0x0000ffffff9c [-0018 / 7] -0100\n" ){
92 cerr << s ;
93 BOOST_ERROR("nesting did not work");
94 }
95
96 // bind args, and various arguments counts :
97 {
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);
105 bf.clear_binds();
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);
112 }
113 // testcase for bug reported at
114 // http://lists.boost.org/boost-users/2006/05/19723.php
115 {
116 format f("%40t%1%");
117 int x = 0;
118 f.bind_arg(1, x);
119 f.clear();
120 }
121
122 // testcase for bug reported at
123 // http://lists.boost.org/boost-users/2005/11/15454.php
124 std::string l_param;
125 std::string l_str = (boost::format("here is an empty string: %1%") % l_param).str();
126
127 // testcase for SourceForge bug #1506914
128 std::string arg; // empty string
129 s = str(format("%=8s") % arg);
130
131 return 0;
132 }