~ubuntu-branches/debian/sid/boost1.49/sid

« back to all changes in this revision

Viewing changes to libs/format/test/format_test2.cpp

  • Committer: Package Import Robot
  • Author(s): Steve M. Robbins
  • Date: 2012-02-26 00:31:44 UTC
  • Revision ID: package-import@ubuntu.com-20120226003144-eaytp12cbf6ubpms
Tags: upstream-1.49.0
ImportĀ upstreamĀ versionĀ 1.49.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ------------------------------------------------------------------------------
 
2
// format_test2.cpp :  a few real, simple tests.
 
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/format.hpp"
 
14
 
 
15
#include <iostream> 
 
16
#include <iomanip>
 
17
 
 
18
#define BOOST_INCLUDE_MAIN 
 
19
#include <boost/test/test_tools.hpp>
 
20
 
 
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
 
 
33
int test_main(int, char* [])
 
34
{
 
35
    using namespace std;
 
36
    using boost::format;
 
37
    using boost::io::group;
 
38
    using boost::str;
 
39
 
 
40
    Rational r(16,9);
 
41
 
 
42
    string s;
 
43
    s = str(format("%5%. %5$=6s . %1% format %5%, c'%3% %1% %2%.\n") 
 
44
            % "le" % "bonheur" % "est" % "trop" % group(setfill('_'), "bref") );
 
45
 
 
46
    if(s  != "bref. _bref_ . le format bref, c'est le bonheur.\n") {
 
47
      cerr << s;
 
48
      BOOST_ERROR("centered alignement : formatting result incorrect");
 
49
    }
 
50
 
 
51
 
 
52
    s = str(format("%+8d %-8d\n") % r % r );
 
53
    if(s  != "  +16/+9 16/9    \n") {
 
54
      cerr << s;  
 
55
      BOOST_ERROR("(user-type) formatting result incorrect");
 
56
    }
 
57
 
 
58
    s = str(format("[%0+4d %0+8d %-08d]\n") % 8 % r % r);
 
59
    if(s  != "[+008 +0016/+9 16/9    ]\n") {
 
60
      cerr << s;  
 
61
      BOOST_ERROR("(zero-padded user-type) formatting result incorrect");
 
62
    }
 
63
 
 
64
 
 
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 ) ;
 
69
    if( s != "88,                 =" ) {
 
70
      cout << s << endl;
 
71
      BOOST_ERROR("(tabulation) formatting result incorrect");
 
72
    }
 
73
 
 
74
 
 
75
    s = str(format("%.2s %8c.\n") % "root" % "user" );
 
76
    if(s  != "ro        u.\n") {
 
77
      cerr << s;  
 
78
      BOOST_ERROR("(truncation) formatting result incorrect");
 
79
    }
 
80
 
 
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");
 
85
 
 
86
    s = str( format("%|=s|") % group(setfill('_'), setw(6), r) );
 
87
    if( s!= "_16/9_") {
 
88
      cerr << s << endl;
 
89
      BOOST_ERROR("width in group context is not handled correctly");
 
90
    }
 
91
 
 
92
 
 
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");
 
97
 
 
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$|")  
 
101
             % 101
 
102
             % group(setfill('_'), hex, 2)
 
103
             % 103 );
 
104
    if(s != "_0x2  101 _0x2 103")
 
105
      BOOST_ERROR("formatting error. (not-restoring state ?)");
 
106
 
 
107
 
 
108
 
 
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"){
 
113
      cerr << s;
 
114
      BOOST_ERROR("formatting error. (flag 0)");
 
115
    }
 
116
 
 
117
    return 0;
 
118
}