Utils: Formating helpers (senf::format::eng, senf::format::dumpint)
[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
35 #include <senf/Utils/auto_unit_test.hh>
36 #include <boost/test/test_tools.hpp>
37
38 #define prefix_
39 ///////////////////////////////cc.p////////////////////////////////////////
40
41 BOOST_AUTO_UNIT_TEST(formatEng)
42 {
43     std::stringstream ss;
44
45 #   define CheckFormat(v,s)                                             \
46         {                                                               \
47             ss.str("");                                                 \
48             ss << senf::format::eng v;                                  \
49             BOOST_CHECK_EQUAL( ss.str(), s );                           \
50         }
51
52     ss << std::setw(21);
53
54     CheckFormat( (0.0),                      "            0.000    " );
55     CheckFormat( (-1.4431288e5, 2.26338e4),  "-144.313+-022.634e+03" );
56     CheckFormat( (1.4444e-13, 2.111111e-16), " 144.440+-000.211e-15" );
57     CheckFormat( (1.345e-3, 3.456),          "   0.001+-003.456    " );
58
59     ss << std::setprecision(4) << std::uppercase << std::showpoint;
60
61     CheckFormat( (0.0),                      "              0.0E+00" );
62     CheckFormat( (-1.4431288e5, 2.26338e4),  "    -144.3+-022.6E+03" );
63     CheckFormat( (1.4444e-13, 2.111111e-16), "     144.4+-000.2E-15" );
64     CheckFormat( (1.345e-3, 3.456),          "       0.0+-003.5E+00" );
65
66     ss << std::showbase << std::internal << std::showpos << std::setfill('0');
67
68     CheckFormat( (0.0),                      "+00000000000000000.0 " );
69     CheckFormat( (-1.4431288e5, 2.26338e4),  "-0000000144.3+-022.6k" );
70     CheckFormat( (1.4444e-13, 2.111111e-16), "+0000000144.4+-000.2f" );
71     CheckFormat( (1.345e-3, 3.456),          "+0000000000.0+-003.5 " );
72
73     ss << std::left << std::noshowpos << std::setfill('*');
74
75     CheckFormat( (0.0),                      "   0.0 **************" );
76     CheckFormat( (-1.4431288e5, 2.26338e4),  "-144.3+-022.6k*******" );
77     CheckFormat( (1.4444e-13, 2.111111e-16), " 144.4+-000.2f*******" );
78     CheckFormat( (1.345e-3, 3.456),          "   0.0+-003.5 *******" );
79
80     ss << std::setw(0);
81
82     CheckFormat( (0.0),                      "0.0" );
83     CheckFormat( (-1.4431288e5, 2.26338e4),  "-144.3+-22.6k" );
84     CheckFormat( (1.4444e-13, 2.111111e-16), "144.4+-0.2f" );
85     CheckFormat( (1.345e-3, 3.456),          "0.0+-3.5" );
86
87 #   undef CheckFormat
88     
89     // From documentation
90     
91     {
92         std::stringstream ss;
93         ss << senf::format::eng(1.23);
94         BOOST_CHECK_EQUAL(ss.str(), "1.230");
95     }
96
97     {
98         std::stringstream ss;
99         ss << std::setw(1) << senf::format::eng(1.23);
100         BOOST_CHECK_EQUAL( ss.str(), "   1.230    " );
101     }
102
103     {
104         std::stringstream ss;
105         ss << std::setw(25) << std::setprecision(5) << std::showpos << std::uppercase 
106            << std::internal << senf::format::eng(12345,67);
107         BOOST_CHECK_EQUAL( ss.str(), "+       12.35+-000.07E+03" );
108     }
109
110     {
111         std::stringstream ss;
112         ss << std::showbase << senf::format::eng(12345,67);
113         BOOST_CHECK_EQUAL( ss.str(), "12.345+-0.067k" );
114     }
115 }
116
117 BOOST_AUTO_UNIT_TEST(dumpint)
118 {
119     std::stringstream ss;
120     
121 #   define CheckFormat(v,s)                                             \
122         {                                                               \
123             ss.str("");                                                 \
124             ss << senf::format::dumpint(v);                             \
125             BOOST_CHECK_EQUAL( ss.str(), s );                           \
126         }
127
128     CheckFormat( boost::int8_t(32), " 0x20 (  32) ( )" );
129     CheckFormat( boost::uint16_t(1234), "0x04d2 ( 1234) (..)" );
130
131 #   undef CheckFormat
132 }
133
134 ///////////////////////////////cc.e////////////////////////////////////////
135 #undef prefix_
136
137 \f
138 // Local Variables:
139 // mode: c++
140 // fill-column: 100
141 // comment-column: 40
142 // c-file-style: "senf"
143 // indent-tabs-mode: nil
144 // ispell-local-dictionary: "american"
145 // compile-command: "scons -u test"
146 // End: