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