switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Utils / Format.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2009
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief Format.test unit tests */
30
31 //#include "Format.test.hh"
32 //#include "Format.test.ih"
33
34 // Custom includes
35 #include <sstream>
36 #include <iomanip>
37 #include "Format.hh"
38 #include <boost/cstdint.hpp>
39 #include "String.hh"
40
41 #include <senf/Utils/auto_unit_test.hh>
42 #include <boost/test/test_tools.hpp>
43
44 #define prefix_
45 //-/////////////////////////////////////////////////////////////////////////////////////////////////
46
47 SENF_AUTO_UNIT_TEST(formatEng)
48 {
49     std::stringstream ss;
50
51 #   define CheckFormat(v,s)                                             \
52         {                                                               \
53             ss.str("");                                                 \
54             ss << senf::format::eng v;                                  \
55             BOOST_CHECK_EQUAL( ss.str(), s );                           \
56         }
57
58     ss << std::setw(21);
59
60     CheckFormat( (0.0),                      "            0.000    " );
61     CheckFormat( (-1.4431288e5, 2.26338e4),  "-144.313+-022.634e+03" );
62     CheckFormat( (1.4444e-13, 2.111111e-16), " 144.440+-000.211e-15" );
63     CheckFormat( (1.345e-3, 3.456),          "   0.001+-003.456    " );
64
65     ss << std::setprecision(4) << std::uppercase << std::showpoint;
66
67     CheckFormat( (0.0),                      "              0.0E+00" );
68     CheckFormat( (-1.4431288e5, 2.26338e4),  "    -144.3+-022.6E+03" );
69     CheckFormat( (1.4444e-13, 2.111111e-16), "     144.4+-000.2E-15" );
70     CheckFormat( (1.345e-3, 3.456),          "       0.0+-003.5E+00" );
71
72     ss << std::showbase << std::internal << std::showpos << std::setfill('0');
73
74     CheckFormat( (0.0),                      "+00000000000000000.0 " );
75     CheckFormat( (-1.4431288e5, 2.26338e4),  "-0000000144.3+-022.6k" );
76     CheckFormat( (1.4444e-13, 2.111111e-16), "+0000000144.4+-000.2f" );
77     CheckFormat( (1.345e-3, 3.456),          "+0000000000.0+-003.5 " );
78
79     ss << std::left << std::noshowpos << std::setfill('*');
80
81     CheckFormat( (0.0),                      "   0.0 **************" );
82     CheckFormat( (-1.4431288e5, 2.26338e4),  "-144.3+-022.6k*******" );
83     CheckFormat( (1.4444e-13, 2.111111e-16), " 144.4+-000.2f*******" );
84     CheckFormat( (1.345e-3, 3.456),          "   0.0+-003.5 *******" );
85
86     ss << std::setw(0);
87
88     CheckFormat( (0.0),                      "0.0" );
89     CheckFormat( (-1.4431288e5, 2.26338e4),  "-144.3+-22.6k" );
90     CheckFormat( (1.4444e-13, 2.111111e-16), "144.4+-0.2f" );
91     CheckFormat( (1.345e-3, 3.456),          "0.0+-3.5" );
92
93 #   undef CheckFormat
94
95     // From documentation
96
97     {
98         std::stringstream ss;
99         ss << senf::format::eng(1.23);
100         BOOST_CHECK_EQUAL(ss.str(), "1.230");
101     }
102
103     {
104         std::stringstream ss;
105         ss << std::setw(1) << senf::format::eng(1.23);
106         BOOST_CHECK_EQUAL( ss.str(), "   1.230    " );
107     }
108
109     {
110         std::stringstream ss;
111         ss << std::setw(25) << std::setprecision(5) << std::showpos << std::uppercase
112            << std::internal << senf::format::eng(12345,67);
113         BOOST_CHECK_EQUAL( ss.str(), "+       12.35+-000.07E+03" );
114     }
115
116     {
117         std::stringstream ss;
118         ss << std::showbase << senf::format::eng(12345,67);
119         BOOST_CHECK_EQUAL( ss.str(), "12.345+-0.067k" );
120     }
121
122     // class member formatting
123     BOOST_CHECK_EQUAL( senf::str(senf::format::eng(12345, 67)
124                                  .setw()
125                                  .setprecision(5)
126                                  .setfill('0')
127                                  .showbase()
128                                  .showpos()
129                                  .internal()),
130                        "+012.35+-000.07k" );
131
132     BOOST_CHECK_EQUAL( senf::str(senf::format::eng(12.345, 67)
133                                  .setw()
134                                  .setprecision(5)
135                                  .showpoint()
136                                  .uppercase()),
137                        "  12.35+-067.00E+00" );
138 }
139
140 SENF_AUTO_UNIT_TEST(dumpint)
141 {
142     std::stringstream ss;
143
144 #   define CheckFormat(v,s)                                             \
145         {                                                               \
146             ss.str("");                                                 \
147             ss << senf::format::dumpint(v);                             \
148             BOOST_CHECK_EQUAL( ss.str(), s );                           \
149         }
150
151     CheckFormat( boost::int8_t(32), " 0x20 (  32) ( )" );
152     CheckFormat( boost::uint16_t(1234), "0x04d2 ( 1234) (..)" );
153
154 #   undef CheckFormat
155 }
156
157 namespace {
158     void f1(std::ostream & os) {
159         senf::format::IndentHelper indent;
160         os << indent << "f1\n";
161     }
162     void f2(std::ostream & os) {
163         senf::format::IndentHelper indent;
164         os << indent << "f2_1\n";
165         f1( os);
166         os << indent << "f2_2\n";
167         indent.increase();
168         os << indent << "f2_3\n";
169     }
170 }
171 SENF_AUTO_UNIT_TEST(indent)
172 {
173     std::stringstream ss;
174     f2(ss);
175     BOOST_CHECK_EQUAL( ss.str(),
176             "  f2_1\n"
177             "    f1\n"
178             "  f2_2\n"
179             "    f2_3\n");
180 }
181
182 //-/////////////////////////////////////////////////////////////////////////////////////////////////
183 #undef prefix_
184
185 \f
186 // Local Variables:
187 // mode: c++
188 // fill-column: 100
189 // comment-column: 40
190 // c-file-style: "senf"
191 // indent-tabs-mode: nil
192 // ispell-local-dictionary: "american"
193 // compile-command: "scons -u test"
194 // End: