Ticket #7261: units_measure_width_test.cpp

File units_measure_width_test.cpp, 3.3 KB (added by Paul A. Bristow, 10 years ago)

Test file for changes to IO.hpp to correct width overflow

Line 
1// Boost.Units library
2
3// Copyright 2012 Paul A. Bristow Adding comments and examples of new types.
4//
5// Distributed under the Boost Software License Version 1.0. (See
6// accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8
9#ifdef _MSC_VER
10# pragma warning(disable :4512) // 'boost::tuples::tuple_manipulator<CharType>' : assignment operator could not be generated
11#endif
12
13
14#define BOOST_TEST_MAIN
15#include <boost/test/unit_test.hpp>
16
17#include <boost/units/cmath.hpp>
18#include <boost/units/io.hpp>
19#include <boost/units/systems/si.hpp>
20#include <boost/units/systems/si/codata/physico-chemical_constants.hpp>
21#include <boost/units/systems/si/io.hpp>
22#include <boost/units/quantity.hpp>
23
24#include <libs/units/example/measurement.hpp> // An uncertain type called measure.
25
26#include <iostream>
27#include <iomanip>
28
29BOOST_AUTO_TEST_CASE(test_output_width)
30{
31 using namespace boost::units;
32 using namespace boost::units::si;
33
34 //quantity<length> ql = 2345.6 * meters;
35 //std::cout << std::setw(20) << ql << std::endl;
36
37 std::size_t w = 25;
38
39 quantity<length, measurement<double> > qm = measurement<double>(45210.0, 1234.0) * meters;
40 // Value is big enough to switch to km if engineering_prefix is chosen.
41
42 std::ostringstream oss;
43 // Default right justify.
44 oss.fill('*'); // So can see the fill chars clearly.
45 std::cout.fill('*');
46
47 std::cout << boost::units::no_prefix << std::setw(w) << qm << std::endl;
48 oss << std::setw(w) << boost::units::no_prefix << qm << std::flush;
49 BOOST_CHECK_EQUAL(oss.str().size(), w);
50 BOOST_CHECK_EQUAL(oss.str(), "*********45210(+/-1234) m");
51
52 //std::cout << oss.str() <<"|" << oss.str().size() << std::endl;
53 oss.str("");
54 std::cout << boost::units::engineering_prefix << std::setw(w) << qm << std::endl;
55 oss << std::setw(w) << boost::units::engineering_prefix << qm << std::flush;
56 BOOST_CHECK_EQUAL(oss.str().size(), w);
57 BOOST_CHECK_EQUAL(oss.str(),"*******45.21(+/-1.234) km");
58 oss.str("");
59
60 std::cout << boost::units::binary_prefix << std::setw(w) << qm << std::endl;
61 oss << std::setw(w) << boost::units::binary_prefix << qm << std::flush;
62 BOOST_CHECK_EQUAL(oss.str().size(), w);
63 BOOST_CHECK_EQUAL(oss.str(),"**44.1504(+/-1.20508) Kim");
64 oss.str("");
65
66 // Left justify.
67 std::cout << std::left << boost::units::no_prefix << std::setw(w) << qm << std::endl;
68 oss << std::left << boost::units::no_prefix<< std::setw(w) << qm << std::flush;
69 BOOST_CHECK_EQUAL(oss.str().size(), w);
70 BOOST_CHECK_EQUAL(oss.str(), "45210(+/-1234) m*********");
71
72
73 oss.str("");
74 std::cout << std::left << boost::units::engineering_prefix << std::setw(w) << qm << std::endl;
75 oss << std::left << std::setw(w) << boost::units::engineering_prefix << qm << std::flush;
76 BOOST_CHECK_EQUAL(oss.str().size(), w);
77 BOOST_CHECK_EQUAL(oss.str(),"45.21(+/-1.234) km*******");
78 oss.str("");
79
80 std::cout << std::left << boost::units::binary_prefix << std::setw(w) << qm << std::endl;
81 oss << std::left << std::setw(w) << boost::units::binary_prefix << qm << std::flush;
82 BOOST_CHECK_EQUAL(oss.str().size(), w);
83 BOOST_CHECK_EQUAL(oss.str(),"44.1504(+/-1.20508) Kim**");
84 oss.str("");
85
86
87} //
88
89
90
91
92