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

source code of boost/libs/format/test/format_test3.cpp