~ubuntu-branches/ubuntu/wily/bombono-dvd/wily

« back to all changes in this revision

Viewing changes to libs/boost-lib/libs/format/test/format_test3.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2010-03-12 08:42:05 UTC
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20100312084205-kugmzrqqv7zm3k7n
Tags: upstream-0.5.5
ImportĀ upstreamĀ versionĀ 0.5.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
    return 0;
 
97
}