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");