1
// ------------------------------------------------------------------------------
2
// format_test2.cpp : a few real, simple tests.
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/format.hpp"
18
#define BOOST_INCLUDE_MAIN
19
#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;
33
int test_main(int, char* [])
37
using boost::io::group;
43
s = str(format("%5%. %5$=6s . %1% format %5%, c'%3% %1% %2%.\n")
44
% "le" % "bonheur" % "est" % "trop" % group(setfill('_'), "bref") );
46
if(s != "bref. _bref_ . le format bref, c'est le bonheur.\n") {
48
BOOST_ERROR("centered alignement : formatting result incorrect");
52
s = str(format("%+8d %-8d\n") % r % r );
53
if(s != " +16/+9 16/9 \n") {
55
BOOST_ERROR("(user-type) formatting result incorrect");
58
s = str(format("[%0+4d %0+8d %-08d]\n") % 8 % r % r);
59
if(s != "[+008 +0016/+9 16/9 ]\n") {
61
BOOST_ERROR("(zero-padded user-type) formatting result incorrect");
65
s = str( format("%1%, %20T_ (%|2$5|,%|3$5|)\n") % "98765" % 1326 % 88 ) ;
66
if( s != "98765, _____________ ( 1326, 88)\n" )
67
BOOST_ERROR("(tabulation) formatting result incorrect");
68
s = str( format("%s, %|20t|=") % 88 ) ;
71
BOOST_ERROR("(tabulation) formatting result incorrect");
75
s = str(format("%.2s %8c.\n") % "root" % "user" );
78
BOOST_ERROR("(truncation) formatting result incorrect");
81
// width in format-string is overridden by setw manipulator :
82
s = str( format("%|1$4| %|1$|") % group(setfill('0'), setw(6), 1) );
83
if( s!= "000001 000001")
84
BOOST_ERROR("width in format VS in argument misbehaved");
86
s = str( format("%|=s|") % group(setfill('_'), setw(6), r) );
89
BOOST_ERROR("width in group context is not handled correctly");
93
// options that uses internal alignment : + 0 #
94
s = str( format("%+6d %0#6x %s\n") % 342 % 33 % "ok" );
95
if( s !=" +342 0x0021 ok\n")
96
BOOST_ERROR("(flags +, 0, or #) formatting result incorrect");
98
// flags in the format string are not sticky
99
// and hex in argument overrrides type-char d (->decimal) :
100
s = str( format("%2$#4d %|1$4| %|2$#4| %|3$|")
102
% group(setfill('_'), hex, 2)
104
if(s != "_0x2 101 _0x2 103")
105
BOOST_ERROR("formatting error. (not-restoring state ?)");
109
// flag '0' is tricky .
110
// left-align cancels '0':
111
s = str( format("%2$0#12X %2$0#-12d %1$0#10d \n") % -20 % 10 );
112
if( s != "0X000000000A 10 -000000020 \n"){
114
BOOST_ERROR("formatting error. (flag 0)");