]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/format/test/format_test3.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / format / test / format_test3.cpp
CommitLineData
7c673cae
FG
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
b32b8144 16#include <iostream>
7c673cae
FG
17#include <iomanip>
18
b32b8144 19#define BOOST_INCLUDE_MAIN
7c673cae
FG
20#include <boost/test/test_tools.hpp>
21
22struct Rational {
23 int n,d;
24 Rational (int an, int ad) : n(an), d(ad) {}
25};
26
27std::ostream& operator<<( std::ostream& os, const Rational& r) {
28 os << r.n << "/" << r.d;
29 return os;
30}
31
32int 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;
b32b8144
FG
40 // special paddings
41 s = str( format("[%=6s] [%+6s] [%+6s] [% 6s] [%+6s]\n")
7c673cae
FG
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
b32b8144 53 s = str( format("[% 6.8s] [% 8.6s] [% 7.7s]\n")
7c673cae
FG
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
b32b8144 63 s = str( format("[% 6.8s] [% 6.8s] [% 6.8s] [% 6.8s] [%6.8s]\n")
7c673cae
FG
64 % 1234567897
65 % group(setfill('x'), 12)
66 % group(internal, setfill('x'), 12)
67 % group(internal, setfill('x'), 1234567890)
b32b8144 68 % group(internal, setfill('x'), 123456)
7c673cae
FG
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)
b32b8144 80 % group(internal, setfill('x'), 12345)
7c673cae
FG
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 }
b32b8144 113 // testcase for bug reported at
7c673cae
FG
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;
b32b8144
FG
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);
7c673cae
FG
127
128 // testcase for SourceForge bug #1506914
b32b8144 129 std::string arg; // empty string
7c673cae 130 s = str(format("%=8s") % arg);
b32b8144 131 BOOST_CHECK_EQUAL(std::string(" "), s);
7c673cae
FG
132
133 return 0;
134}