Fix documentation build under maverick (doxygen 1.7.1)
[senf.git] / senf / Utils / Format.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2009
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief Format.test unit tests */
25
26 //#include "Format.test.hh"
27 //#include "Format.test.ih"
28
29 // Custom includes
30 #include <sstream>
31 #include <iomanip>
32 #include "Format.hh"
33 #include <boost/cstdint.hpp>
34 #include "String.hh"
35
36 #include <senf/Utils/auto_unit_test.hh>
37 #include <boost/test/test_tools.hpp>
38
39 #define prefix_
40 //-/////////////////////////////////////////////////////////////////////////////////////////////////
41
42 SENF_AUTO_UNIT_TEST(formatEng)
43 {
44     std::stringstream ss;
45
46 #   define CheckFormat(v,s)                                             \
47         {                                                               \
48             ss.str("");                                                 \
49             ss << senf::format::eng v;                                  \
50             BOOST_CHECK_EQUAL( ss.str(), s );                           \
51         }
52
53     ss << std::setw(21);
54
55     CheckFormat( (0.0),                      "            0.000    " );
56     CheckFormat( (-1.4431288e5, 2.26338e4),  "-144.313+-022.634e+03" );
57     CheckFormat( (1.4444e-13, 2.111111e-16), " 144.440+-000.211e-15" );
58     CheckFormat( (1.345e-3, 3.456),          "   0.001+-003.456    " );
59
60     ss << std::setprecision(4) << std::uppercase << std::showpoint;
61
62     CheckFormat( (0.0),                      "              0.0E+00" );
63     CheckFormat( (-1.4431288e5, 2.26338e4),  "    -144.3+-022.6E+03" );
64     CheckFormat( (1.4444e-13, 2.111111e-16), "     144.4+-000.2E-15" );
65     CheckFormat( (1.345e-3, 3.456),          "       0.0+-003.5E+00" );
66
67     ss << std::showbase << std::internal << std::showpos << std::setfill('0');
68
69     CheckFormat( (0.0),                      "+00000000000000000.0 " );
70     CheckFormat( (-1.4431288e5, 2.26338e4),  "-0000000144.3+-022.6k" );
71     CheckFormat( (1.4444e-13, 2.111111e-16), "+0000000144.4+-000.2f" );
72     CheckFormat( (1.345e-3, 3.456),          "+0000000000.0+-003.5 " );
73
74     ss << std::left << std::noshowpos << std::setfill('*');
75
76     CheckFormat( (0.0),                      "   0.0 **************" );
77     CheckFormat( (-1.4431288e5, 2.26338e4),  "-144.3+-022.6k*******" );
78     CheckFormat( (1.4444e-13, 2.111111e-16), " 144.4+-000.2f*******" );
79     CheckFormat( (1.345e-3, 3.456),          "   0.0+-003.5 *******" );
80
81     ss << std::setw(0);
82
83     CheckFormat( (0.0),                      "0.0" );
84     CheckFormat( (-1.4431288e5, 2.26338e4),  "-144.3+-22.6k" );
85     CheckFormat( (1.4444e-13, 2.111111e-16), "144.4+-0.2f" );
86     CheckFormat( (1.345e-3, 3.456),          "0.0+-3.5" );
87
88 #   undef CheckFormat
89
90     // From documentation
91
92     {
93         std::stringstream ss;
94         ss << senf::format::eng(1.23);
95         BOOST_CHECK_EQUAL(ss.str(), "1.230");
96     }
97
98     {
99         std::stringstream ss;
100         ss << std::setw(1) << senf::format::eng(1.23);
101         BOOST_CHECK_EQUAL( ss.str(), "   1.230    " );
102     }
103
104     {
105         std::stringstream ss;
106         ss << std::setw(25) << std::setprecision(5) << std::showpos << std::uppercase
107            << std::internal << senf::format::eng(12345,67);
108         BOOST_CHECK_EQUAL( ss.str(), "+       12.35+-000.07E+03" );
109     }
110
111     {
112         std::stringstream ss;
113         ss << std::showbase << senf::format::eng(12345,67);
114         BOOST_CHECK_EQUAL( ss.str(), "12.345+-0.067k" );
115     }
116
117     // class member formatting
118     BOOST_CHECK_EQUAL( senf::str(senf::format::eng(12345, 67)
119                                  .setw()
120                                  .setprecision(5)
121                                  .setfill('0')
122                                  .showbase()
123                                  .showpos()
124                                  .internal()),
125                        "+012.35+-000.07k" );
126
127     BOOST_CHECK_EQUAL( senf::str(senf::format::eng(12.345, 67)
128                                  .setw()
129                                  .setprecision(5)
130                                  .showpoint()
131                                  .uppercase()),
132                        "  12.35+-067.00E+00" );
133 }
134
135 SENF_AUTO_UNIT_TEST(dumpint)
136 {
137     std::stringstream ss;
138
139 #   define CheckFormat(v,s)                                             \
140         {                                                               \
141             ss.str("");                                                 \
142             ss << senf::format::dumpint(v);                             \
143             BOOST_CHECK_EQUAL( ss.str(), s );                           \
144         }
145
146     CheckFormat( boost::int8_t(32), " 0x20 (  32) ( )" );
147     CheckFormat( boost::uint16_t(1234), "0x04d2 ( 1234) (..)" );
148
149 #   undef CheckFormat
150 }
151
152 namespace {
153     void f1(std::ostream & os) {
154         senf::format::IndentHelper indent;
155         os << indent << "f1\n";
156     }
157     void f2(std::ostream & os) {
158         senf::format::IndentHelper indent;
159         os << indent << "f2_1\n";
160         f1( os);
161         os << indent << "f2_2\n";
162         indent.increase();
163         os << indent << "f2_3\n";
164     }
165 }
166 SENF_AUTO_UNIT_TEST(indent)
167 {
168     std::stringstream ss;
169     f2(ss);
170     BOOST_CHECK_EQUAL( ss.str(),
171             "  f2_1\n"
172             "    f1\n"
173             "  f2_2\n"
174             "    f2_3\n");
175 }
176
177 //-/////////////////////////////////////////////////////////////////////////////////////////////////
178 #undef prefix_
179
180 \f
181 // Local Variables:
182 // mode: c++
183 // fill-column: 100
184 // comment-column: 40
185 // c-file-style: "senf"
186 // indent-tabs-mode: nil
187 // ispell-local-dictionary: "american"
188 // compile-command: "scons -u test"
189 // End: