]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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" | |
b32b8144 FG |
14 | #include <boost/algorithm/string.hpp> |
15 | #include <boost/config.hpp> | |
16 | #include <boost/predef.h> | |
7c673cae | 17 | |
b32b8144 | 18 | #include <iostream> |
7c673cae FG |
19 | #include <iomanip> |
20 | ||
b32b8144 FG |
21 | #if !defined(BOOST_NO_STD_LOCALE) |
22 | #include <locale> | |
23 | #endif | |
24 | ||
25 | #define BOOST_INCLUDE_MAIN | |
7c673cae FG |
26 | #include <boost/test/test_tools.hpp> |
27 | ||
28 | ||
29 | struct Rational { | |
30 | int n,d; | |
31 | Rational (int an, int ad) : n(an), d(ad) {} | |
32 | }; | |
33 | ||
34 | std::ostream& operator<<( std::ostream& os, const Rational& r) { | |
35 | os << r.n << "/" << r.d; | |
36 | return os; | |
37 | } | |
38 | ||
b32b8144 FG |
39 | #if !defined(BOOST_NO_STD_LOCALE) |
40 | // in C++03 this has to be globally defined or gcc complains | |
41 | struct custom_tf : std::numpunct<char> { | |
42 | std::string do_truename() const { return "POSITIVE"; } | |
43 | std::string do_falsename() const { return "NEGATIVE"; } | |
44 | }; | |
45 | #endif | |
7c673cae FG |
46 | |
47 | int test_main(int, char* []) | |
48 | { | |
49 | using namespace std; | |
50 | using boost::format; | |
51 | using boost::io::group; | |
52 | using boost::str; | |
53 | ||
54 | Rational r(16,9); | |
b32b8144 | 55 | const Rational cr(9,16); |
7c673cae FG |
56 | |
57 | string s; | |
b32b8144 | 58 | s = str(format("%5%. %5$=6s . %1% format %5%, c'%3% %1% %2%.\n") |
7c673cae FG |
59 | % "le" % "bonheur" % "est" % "trop" % group(setfill('_'), "bref") ); |
60 | ||
61 | if(s != "bref. _bref_ . le format bref, c'est le bonheur.\n") { | |
62 | cerr << s; | |
63 | BOOST_ERROR("centered alignement : formatting result incorrect"); | |
64 | } | |
65 | ||
66 | ||
b32b8144 FG |
67 | s = str(format("%+8d %-8d\n") % r % cr ); |
68 | if(s != " +16/+9 9/16 \n") { | |
69 | cerr << s; | |
7c673cae FG |
70 | BOOST_ERROR("(user-type) formatting result incorrect"); |
71 | } | |
72 | ||
73 | s = str(format("[%0+4d %0+8d %-08d]\n") % 8 % r % r); | |
74 | if(s != "[+008 +0016/+9 16/9 ]\n") { | |
b32b8144 | 75 | cerr << s; |
7c673cae FG |
76 | BOOST_ERROR("(zero-padded user-type) formatting result incorrect"); |
77 | } | |
78 | ||
79 | ||
80 | s = str( format("%1%, %20T_ (%|2$5|,%|3$5|)\n") % "98765" % 1326 % 88 ) ; | |
b32b8144 | 81 | if( s != "98765, _____________ ( 1326, 88)\n" ) |
7c673cae FG |
82 | BOOST_ERROR("(tabulation) formatting result incorrect"); |
83 | s = str( format("%s, %|20t|=") % 88 ) ; | |
84 | if( s != "88, =" ) { | |
85 | cout << s << endl; | |
86 | BOOST_ERROR("(tabulation) formatting result incorrect"); | |
87 | } | |
88 | ||
89 | ||
90 | s = str(format("%.2s %8c.\n") % "root" % "user" ); | |
91 | if(s != "ro u.\n") { | |
b32b8144 | 92 | cerr << s; |
7c673cae FG |
93 | BOOST_ERROR("(truncation) formatting result incorrect"); |
94 | } | |
95 | ||
96 | // width in format-string is overridden by setw manipulator : | |
97 | s = str( format("%|1$4| %|1$|") % group(setfill('0'), setw(6), 1) ); | |
98 | if( s!= "000001 000001") | |
99 | BOOST_ERROR("width in format VS in argument misbehaved"); | |
100 | ||
101 | s = str( format("%|=s|") % group(setfill('_'), setw(6), r) ); | |
102 | if( s!= "_16/9_") { | |
103 | cerr << s << endl; | |
104 | BOOST_ERROR("width in group context is not handled correctly"); | |
105 | } | |
106 | ||
107 | ||
108 | // options that uses internal alignment : + 0 # | |
109 | s = str( format("%+6d %0#6x %s\n") % 342 % 33 % "ok" ); | |
110 | if( s !=" +342 0x0021 ok\n") | |
111 | BOOST_ERROR("(flags +, 0, or #) formatting result incorrect"); | |
112 | ||
113 | // flags in the format string are not sticky | |
114 | // and hex in argument overrrides type-char d (->decimal) : | |
b32b8144 | 115 | s = str( format("%2$#4d %|1$4| %|2$#4| %|3$|") |
7c673cae FG |
116 | % 101 |
117 | % group(setfill('_'), hex, 2) | |
118 | % 103 ); | |
119 | if(s != "_0x2 101 _0x2 103") | |
120 | BOOST_ERROR("formatting error. (not-restoring state ?)"); | |
121 | ||
122 | ||
123 | ||
b32b8144 | 124 | // flag '0' is tricky . |
7c673cae FG |
125 | // left-align cancels '0': |
126 | s = str( format("%2$0#12X %2$0#-12d %1$0#10d \n") % -20 % 10 ); | |
127 | if( s != "0X000000000A 10 -000000020 \n"){ | |
128 | cerr << s; | |
129 | BOOST_ERROR("formatting error. (flag 0)"); | |
130 | } | |
131 | ||
b32b8144 FG |
132 | // actually testing floating point output is implementation |
133 | // specific so we're just going to do minimal checking... | |
134 | double dbl = 1234567.890123f; | |
135 | ||
136 | #if (__cplusplus >= 201103L) || (BOOST_VERSION_NUMBER_MAJOR(BOOST_COMP_MSVC) >= 12) | |
137 | // msvc-12.0 and later have support for hexfloat but do not set __cplusplus to a C++11 value | |
138 | BOOST_CHECK(boost::starts_with((boost::format("%A") % dbl).str(), "0X")); | |
139 | BOOST_CHECK(boost::starts_with((boost::format("%a") % dbl).str(), "0x")); | |
140 | #endif | |
141 | ||
142 | BOOST_CHECK(boost::contains((boost::format("%E") % dbl).str(), "E")); | |
143 | BOOST_CHECK(boost::contains((boost::format("%e") % dbl).str(), "e")); | |
144 | BOOST_CHECK(boost::contains((boost::format("%F") % dbl).str(), ".")); | |
145 | BOOST_CHECK(boost::contains((boost::format("%f") % dbl).str(), ".")); | |
146 | BOOST_CHECK(!(boost::format("%G") % dbl).str().empty()); | |
147 | BOOST_CHECK(!(boost::format("%g") % dbl).str().empty()); | |
148 | ||
149 | // testing argument type parsing - remember argument types are ignored | |
150 | // because operator % presents the argument type. | |
151 | unsigned int value = 456; | |
152 | BOOST_CHECK_EQUAL((boost::format("%hhu") % value).str(), "456"); | |
153 | BOOST_CHECK_EQUAL((boost::format("%hu") % value).str(), "456"); | |
154 | BOOST_CHECK_EQUAL((boost::format("%lu") % value).str(), "456"); | |
155 | BOOST_CHECK_EQUAL((boost::format("%llu") % value).str(), "456"); | |
156 | BOOST_CHECK_EQUAL((boost::format("%ju") % value).str(), "456"); | |
157 | BOOST_CHECK_EQUAL((boost::format("%zu") % value).str(), "456"); | |
158 | BOOST_CHECK(boost::starts_with((boost::format("%Lf") % value).str(), "456")); | |
159 | ||
160 | #if !defined(BOOST_NO_STD_LOCALE) | |
161 | // boolalpha support | |
162 | std::locale loc; | |
163 | const std::numpunct<char>& punk(std::use_facet<std::numpunct<char> >(loc)); | |
164 | ||
165 | // Demonstrates how to modify the default string to something else | |
166 | std::locale custom(std::locale(), new custom_tf); | |
167 | boost::ignore_unused(locale::global(custom)); | |
168 | BOOST_CHECK_EQUAL((boost::format("%b") % false).str(), "NEGATIVE"); | |
169 | BOOST_CHECK_EQUAL((boost::format("%b") % true).str(), "POSITIVE"); | |
170 | ||
171 | // restore system default | |
172 | locale::global(loc); | |
173 | BOOST_CHECK_EQUAL((boost::format("%b") % false).str(), punk.falsename()); | |
174 | BOOST_CHECK_EQUAL((boost::format("%b") % true).str(), punk.truename()); | |
175 | #endif | |
176 | ||
177 | // Support for microsoft argument type specifiers: 'w' (same as 'l'), I, I32, I64 | |
178 | BOOST_CHECK_EQUAL((boost::format("%wc") % '5').str(), "5"); | |
179 | BOOST_CHECK_EQUAL((boost::format("%Id") % 123).str(), "123"); | |
180 | BOOST_CHECK_EQUAL((boost::format("%I32d") % 456).str(), "456"); | |
181 | BOOST_CHECK_EQUAL((boost::format("%I64d") % 789).str(), "789"); | |
182 | ||
183 | // issue-36 volatile (and const) keyword | |
184 | volatile int vint = 1234567; | |
185 | BOOST_CHECK_EQUAL((boost::format("%1%") % vint).str(), "1234567"); | |
186 | volatile const int vcint = 7654321; | |
187 | BOOST_CHECK_EQUAL((boost::format("%1%") % vcint).str(), "7654321"); | |
188 | ||
189 | // skip width if '*' | |
190 | BOOST_CHECK_EQUAL((boost::format("%*d") % vint).str(), "1234567"); | |
191 | ||
192 | // internal ios flag | |
193 | BOOST_CHECK_EQUAL((boost::format("%_6d") % -77).str(), "- 77"); | |
194 | ||
195 | // combining some flags | |
196 | BOOST_CHECK_EQUAL((boost::format("%+05.5d" ) % 77).str(), "+0077"); | |
197 | BOOST_CHECK_EQUAL((boost::format("%+ 5.5d" ) % 77).str(), " +77"); | |
198 | BOOST_CHECK_EQUAL((boost::format("%+_ 5.5d" ) % 77).str(), "+ 77"); | |
199 | BOOST_CHECK_EQUAL((boost::format("%+- 5.5d" ) % 77).str(), "+77 "); | |
200 | BOOST_CHECK_EQUAL((boost::format("%+05.5d" ) % -77).str(), "-0077"); | |
201 | BOOST_CHECK_EQUAL((boost::format("%+ 5.5d" ) % -77).str(), " -77"); | |
202 | BOOST_CHECK_EQUAL((boost::format("%+_ 5.5d" ) % -77).str(), "- 77"); | |
203 | BOOST_CHECK_EQUAL((boost::format("%+- 5.5d" ) % -77).str(), "-77 "); | |
204 | ||
205 | // reuse state and reset format flags | |
206 | std::string mystr("abcdefghijklmnop"); | |
207 | BOOST_CHECK_EQUAL((boost::format("%2.2s %-4.4s % 8.8s") | |
208 | % mystr % mystr % mystr).str(), "ab abcd abcdefg"); | |
209 | ||
210 | // coverage | |
211 | format fmt("%1%%2%%3%"); | |
212 | fmt = fmt; | |
213 | ||
7c673cae FG |
214 | return 0; |
215 | } |