1// Boost.Units - A C++ library for zero-overhead dimensional analysis and
2// unit/quantity manipulation and conversion
3//
4// Copyright (C) 2009 Steven Watanabe
5// Copyright Paul A. Bristow 2010
6//
7// Distributed under the Boost Software License, Version 1.0. (See
8// accompanying file LICENSE_1_0.txt or copy at
9// http://www.boost.org/LICENSE_1_0.txt)
10
11/**
12\file test_output.cpp
13
14\brief
15Test unit and quantity printing
16\details
17Tests for output from various units, name, symbol and raw formats, and automatic prefixing in engineering and binary units.
18**/
19
20#include <boost/units/quantity.hpp>
21#include <boost/units/io.hpp>
22#include <boost/units/unit.hpp>
23#include <boost/units/scale.hpp>
24#include <boost/units/scaled_base_unit.hpp>
25#include <boost/units/make_scaled_unit.hpp>
26#include <boost/units/base_unit.hpp>
27#include <boost/units/make_system.hpp>
28#include <boost/units/absolute.hpp>
29#include <boost/units/physical_dimensions/length.hpp>
30#include <boost/units/physical_dimensions/time.hpp>
31#include <boost/units/physical_dimensions/velocity.hpp>
32#include <boost/units/physical_dimensions/volume.hpp>
33#include <boost/units/physical_dimensions/acceleration.hpp>
34#include <boost/units/physical_dimensions/area.hpp>
35
36#include <boost/regex.hpp>
37
38#include <iostream>
39#include <sstream>
40#include <boost/config.hpp>
41#include <limits>
42
43#include <boost/core/lightweight_test.hpp>
44
45struct meter_base_unit : boost::units::base_unit<meter_base_unit, boost::units::length_dimension, 1> {
46 static BOOST_CONSTEXPR const char* name() { return("meter"); }
47 static BOOST_CONSTEXPR const char* symbol() { return("m"); }
48};
49
50struct second_base_unit : boost::units::base_unit<second_base_unit, boost::units::time_dimension, 2> {
51 static BOOST_CONSTEXPR const char* name() { return("second"); }
52 static BOOST_CONSTEXPR const char* symbol() { return("s"); }
53};
54
55struct byte_base_unit : boost::units::base_unit<byte_base_unit, boost::units::dimensionless_type, 3> {
56 static BOOST_CONSTEXPR const char* name() { return("byte"); }
57 static BOOST_CONSTEXPR const char* symbol() { return("b"); }
58};
59
60typedef boost::units::make_system<meter_base_unit, second_base_unit>::type my_system;
61
62typedef boost::units::unit<boost::units::length_dimension, my_system> length;
63typedef boost::units::unit<boost::units::velocity_dimension, my_system> velocity;
64
65typedef boost::units::make_scaled_unit<length, boost::units::scale<10, boost::units::static_rational<3> > >::type scaled_length;
66typedef boost::units::make_scaled_unit<velocity, boost::units::scale<10, boost::units::static_rational<3> > >::type scaled_velocity1;
67
68typedef boost::units::scaled_base_unit<second_base_unit, boost::units::scale<10, boost::units::static_rational<-3> > > millisecond_base_unit;
69
70typedef boost::units::make_system<meter_base_unit, millisecond_base_unit>::type scaled_system;
71
72typedef boost::units::unit<boost::units::time_dimension, scaled_system> scaled_time;
73typedef boost::units::unit<boost::units::velocity_dimension, scaled_system> scaled_velocity2;
74
75typedef boost::units::unit<boost::units::area_dimension, my_system> area;
76typedef boost::units::make_scaled_unit<area, boost::units::scale<10, boost::units::static_rational<3> > >::type scaled_area;
77
78typedef boost::units::make_scaled_unit<scaled_length, boost::units::scale<2, boost::units::static_rational<10> > >::type double_scaled_length;
79
80typedef boost::units::scaled_base_unit<meter_base_unit, boost::units::scale<100, boost::units::static_rational<1> > > scaled_length_base_unit;
81namespace boost {
82namespace units {
83template<>
84struct base_unit_info<scaled_length_base_unit> {
85 static BOOST_CONSTEXPR const char* symbol() { return("scm"); }
86 static BOOST_CONSTEXPR const char* name() { return("scaled_meter"); }
87};
88}
89}
90typedef boost::units::scaled_base_unit<scaled_length_base_unit, boost::units::scale<10, boost::units::static_rational<3> > > double_scaled_length_base_unit;
91typedef double_scaled_length_base_unit::unit_type double_scaled_length2;
92
93typedef boost::units::reduce_unit<boost::units::unit<boost::units::volume_dimension, my_system> >::type custom1;
94
95std::string name_string(const custom1&) { return("custom1"); }
96std::string symbol_string(const custom1&) { return("c1"); }
97
98typedef boost::units::reduce_unit<boost::units::unit<boost::units::acceleration_dimension, my_system> >::type custom2;
99
100BOOST_CONSTEXPR const char* name_string(const custom2&) { return("custom2"); }
101BOOST_CONSTEXPR const char* symbol_string(const custom2&) { return("c2"); }
102
103typedef boost::units::make_scaled_unit<custom1, boost::units::scale<10, boost::units::static_rational<3> > >::type scaled_custom1;
104typedef boost::units::make_scaled_unit<custom2, boost::units::scale<10, boost::units::static_rational<3> > >::type scaled_custom2;
105
106#ifndef BOOST_NO_CWCHAR
107
108#define BOOST_UNITS_TEST_OUTPUT(v, expected) \
109{ \
110 std::ostringstream ss; \
111 ss FORMATTERS << v; \
112 BOOST_TEST_EQ(ss.str(), expected); \
113} \
114{ \
115 std::wostringstream ss; \
116 ss FORMATTERS << v; \
117 BOOST_TEST(ss.str() == BOOST_PP_CAT(L, expected)); \
118}
119
120#define BOOST_UNITS_TEST_OUTPUT_REGEX(v, expected) \
121{ \
122 std::ostringstream ss; \
123 ss FORMATTERS << v; \
124 boost::regex r(expected); \
125 BOOST_TEST(boost::regex_match(ss.str(), r)); \
126} \
127{ \
128 std::wostringstream ss; \
129 ss FORMATTERS << v; \
130 boost::wregex r(BOOST_PP_CAT(L, expected)); \
131 BOOST_TEST(boost::regex_match(ss.str(), r)); \
132}
133
134#define BOOST_UNITS_TEST_OUTPUT_DISPLAY(v) \
135{ \
136 std::ostringstream ss; \
137 ss FORMATTERS << v; \
138 std::cout << #v << ": " << ss.str() << std::endl; \
139} \
140{ \
141 std::wostringstream ss; \
142 ss FORMATTERS << v; \
143 std::wcout << #v << ": " << ss.str() << std::endl; \
144}
145
146#else
147
148#define BOOST_UNITS_TEST_OUTPUT(v, expected) \
149{ \
150 std::ostringstream ss; \
151 ss FORMATTERS << v; \
152 BOOST_TEST_EQ(ss.str(), expected); \
153}
154
155#define BOOST_UNITS_TEST_OUTPUT_REGEX(v, expected) \
156{ \
157 std::ostringstream ss; \
158 ss FORMATTERS << v; \
159 boost::regex r(expected); \
160 BOOST_TEST(boost::regex_match(ss.str(), r)); \
161}
162
163#define BOOST_UNITS_TEST_OUTPUT_DISPLAY(v) \
164{ \
165 std::ostringstream ss; \
166 ss FORMATTERS << v; \
167 std::cout << #v << ": " << ss.str() << std::endl; \
168}
169
170#endif
171
172void test_output_unit_symbol()
173{ // base units using default symbol_format (no format specified) and no auto prefixing.
174#define FORMATTERS
175 BOOST_UNITS_TEST_OUTPUT(meter_base_unit::unit_type(), "m");
176 BOOST_UNITS_TEST_OUTPUT(velocity(), "m s^-1");
177 BOOST_UNITS_TEST_OUTPUT(scaled_length(), "km");
178 BOOST_UNITS_TEST_OUTPUT(scaled_velocity1(), "k(m s^-1)");
179 BOOST_UNITS_TEST_OUTPUT(millisecond_base_unit::unit_type(), "ms");
180 BOOST_UNITS_TEST_OUTPUT(scaled_time(), "ms");
181 BOOST_UNITS_TEST_OUTPUT(scaled_velocity2(), "m ms^-1");
182 BOOST_UNITS_TEST_OUTPUT(area(), "m^2");
183 BOOST_UNITS_TEST_OUTPUT(scaled_area(), "k(m^2)");
184 BOOST_UNITS_TEST_OUTPUT(double_scaled_length(), "Kikm");
185 BOOST_UNITS_TEST_OUTPUT(double_scaled_length2(), "kscm");
186 BOOST_UNITS_TEST_OUTPUT(custom1(), "c1");
187 BOOST_UNITS_TEST_OUTPUT(custom2(), "c2");
188 BOOST_UNITS_TEST_OUTPUT(scaled_custom1(), "kc1");
189 BOOST_UNITS_TEST_OUTPUT(scaled_custom2(), "kc2");
190 BOOST_UNITS_TEST_OUTPUT(boost::units::absolute<meter_base_unit::unit_type>(), "absolute m");
191#undef FORMATTERS
192}
193
194void test_output_unit_raw()
195{ // raw format specified
196#define FORMATTERS << boost::units::raw_format
197 BOOST_UNITS_TEST_OUTPUT(meter_base_unit::unit_type(), "m");
198 BOOST_UNITS_TEST_OUTPUT(velocity(), "m s^-1");
199 BOOST_UNITS_TEST_OUTPUT(scaled_length(), "km");
200 BOOST_UNITS_TEST_OUTPUT(scaled_velocity1(), "k(m s^-1)");
201 BOOST_UNITS_TEST_OUTPUT(millisecond_base_unit::unit_type(), "ms");
202 BOOST_UNITS_TEST_OUTPUT(scaled_time(), "ms");
203 BOOST_UNITS_TEST_OUTPUT(scaled_velocity2(), "m ms^-1");
204 BOOST_UNITS_TEST_OUTPUT(area(), "m^2");
205 BOOST_UNITS_TEST_OUTPUT(scaled_area(), "k(m^2)");
206 BOOST_UNITS_TEST_OUTPUT(double_scaled_length(), "Kikm");
207 BOOST_UNITS_TEST_OUTPUT(double_scaled_length2(), "kscm");
208 // when using raw format, we ignore the user defined overloads
209 BOOST_UNITS_TEST_OUTPUT(custom1(), "m^3");
210 BOOST_UNITS_TEST_OUTPUT(custom2(), "m s^-2");
211 BOOST_UNITS_TEST_OUTPUT(scaled_custom1(), "k(m^3)");
212 BOOST_UNITS_TEST_OUTPUT(scaled_custom2(), "k(m s^-2)");
213 BOOST_UNITS_TEST_OUTPUT(boost::units::absolute<meter_base_unit::unit_type>(), "absolute m");
214#undef FORMATTERS
215}
216
217void test_output_unit_name()
218{ // name format specified.
219#define FORMATTERS << boost::units::name_format
220 BOOST_UNITS_TEST_OUTPUT(meter_base_unit::unit_type(), "meter");
221 BOOST_UNITS_TEST_OUTPUT(velocity(), "meter second^-1");
222 BOOST_UNITS_TEST_OUTPUT(scaled_length(), "kilometer");
223 BOOST_UNITS_TEST_OUTPUT(scaled_velocity1(), "kilo(meter second^-1)");
224 BOOST_UNITS_TEST_OUTPUT(millisecond_base_unit::unit_type(), "millisecond");
225 BOOST_UNITS_TEST_OUTPUT(scaled_time(), "millisecond");
226 BOOST_UNITS_TEST_OUTPUT(scaled_velocity2(), "meter millisecond^-1");
227 BOOST_UNITS_TEST_OUTPUT(area(), "meter^2");
228 BOOST_UNITS_TEST_OUTPUT(scaled_area(), "kilo(meter^2)");
229 BOOST_UNITS_TEST_OUTPUT(double_scaled_length(), "kibikilometer");
230 BOOST_UNITS_TEST_OUTPUT(double_scaled_length2(), "kiloscaled_meter");
231 BOOST_UNITS_TEST_OUTPUT(custom1(), "custom1");
232 BOOST_UNITS_TEST_OUTPUT(custom2(), "custom2");
233 BOOST_UNITS_TEST_OUTPUT(scaled_custom1(), "kilocustom1");
234 BOOST_UNITS_TEST_OUTPUT(scaled_custom2(), "kilocustom2");
235 BOOST_UNITS_TEST_OUTPUT(boost::units::absolute<meter_base_unit::unit_type>(), "absolute meter");
236#undef FORMATTERS
237}
238
239
240void test_output_quantity_symbol()
241{ // quantity symbols using default format.
242#define FORMATTERS
243 BOOST_UNITS_TEST_OUTPUT(1.5*meter_base_unit::unit_type(), "1.5 m");
244 BOOST_UNITS_TEST_OUTPUT(1.5*velocity(), "1.5 m s^-1");
245 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_length(), "1.5 km");
246 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_velocity1(), "1.5 k(m s^-1)");
247 BOOST_UNITS_TEST_OUTPUT(1.5*millisecond_base_unit::unit_type(), "1.5 ms");
248 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_time(), "1.5 ms");
249 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_velocity2(), "1.5 m ms^-1");
250 BOOST_UNITS_TEST_OUTPUT(1.5*area(), "1.5 m^2");
251 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_area(), "1.5 k(m^2)");
252 BOOST_UNITS_TEST_OUTPUT(1.5*double_scaled_length(), "1.5 Kikm");
253 BOOST_UNITS_TEST_OUTPUT(1.5*double_scaled_length2(), "1.5 kscm");
254 BOOST_UNITS_TEST_OUTPUT(1.5*custom1(), "1.5 c1");
255 BOOST_UNITS_TEST_OUTPUT(1.5*custom2(), "1.5 c2");
256 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_custom1(), "1.5 kc1");
257 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_custom2(), "1.5 kc2");
258 BOOST_UNITS_TEST_OUTPUT(1.5*boost::units::absolute<meter_base_unit::unit_type>(), "1.5 absolute m");
259 BOOST_UNITS_TEST_OUTPUT(std::pow(2., 10) * byte_base_unit::unit_type(), "1024 b");
260
261#undef FORMATTERS
262}
263
264void test_output_quantity_raw()
265{ // quantity symbols using raw format.
266#define FORMATTERS << boost::units::raw_format
267 BOOST_UNITS_TEST_OUTPUT(1.5*meter_base_unit::unit_type(), "1.5 m");
268 BOOST_UNITS_TEST_OUTPUT(1.5*velocity(), "1.5 m s^-1");
269 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_length(), "1.5 km");
270 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_velocity1(), "1.5 k(m s^-1)");
271 BOOST_UNITS_TEST_OUTPUT(1.5*millisecond_base_unit::unit_type(), "1.5 ms");
272 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_time(), "1.5 ms");
273 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_velocity2(), "1.5 m ms^-1");
274 BOOST_UNITS_TEST_OUTPUT(1.5*area(), "1.5 m^2");
275 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_area(), "1.5 k(m^2)");
276 BOOST_UNITS_TEST_OUTPUT(1.5*double_scaled_length(), "1.5 Kikm");
277 BOOST_UNITS_TEST_OUTPUT(1.5*double_scaled_length2(), "1.5 kscm");
278 // when using raw format, we ignore the user defined overloads
279 BOOST_UNITS_TEST_OUTPUT(1.5*custom1(), "1.5 m^3");
280 BOOST_UNITS_TEST_OUTPUT(1.5*custom2(), "1.5 m s^-2");
281 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_custom1(), "1.5 k(m^3)");
282 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_custom2(), "1.5 k(m s^-2)");
283 BOOST_UNITS_TEST_OUTPUT(1.5*boost::units::absolute<meter_base_unit::unit_type>(), "1.5 absolute m");
284#undef FORMATTERS
285}
286
287void test_output_quantity_name()
288{ // // quantity symbols using name format.
289#define FORMATTERS << boost::units::name_format
290 BOOST_UNITS_TEST_OUTPUT(1.5*meter_base_unit::unit_type(), "1.5 meter");
291 BOOST_UNITS_TEST_OUTPUT(1.5*velocity(), "1.5 meter second^-1");
292 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_length(), "1.5 kilometer");
293 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_velocity1(), "1.5 kilo(meter second^-1)");
294 BOOST_UNITS_TEST_OUTPUT(1.5*millisecond_base_unit::unit_type(), "1.5 millisecond");
295 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_time(), "1.5 millisecond");
296 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_velocity2(), "1.5 meter millisecond^-1");
297 BOOST_UNITS_TEST_OUTPUT(1.5*area(), "1.5 meter^2");
298 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_area(), "1.5 kilo(meter^2)");
299 BOOST_UNITS_TEST_OUTPUT(1.5*double_scaled_length(), "1.5 kibikilometer");
300 BOOST_UNITS_TEST_OUTPUT(1.5*double_scaled_length2(), "1.5 kiloscaled_meter");
301 BOOST_UNITS_TEST_OUTPUT(1.5*custom1(), "1.5 custom1");
302 BOOST_UNITS_TEST_OUTPUT(1.5*custom2(), "1.5 custom2");
303 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_custom1(), "1.5 kilocustom1");
304 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_custom2(), "1.5 kilocustom2");
305 BOOST_UNITS_TEST_OUTPUT(1.5*boost::units::absolute<meter_base_unit::unit_type>(), "1.5 absolute meter");
306#undef FORMATTERS
307}
308
309void test_output_autoprefixed_quantity_name()
310{ // Engineering autoprefix, with name format.
311#define FORMATTERS << boost::units::name_format << boost::units::engineering_prefix
312 // Single base unit like meter.
313 BOOST_UNITS_TEST_OUTPUT(1.5*meter_base_unit::unit_type(), "1.5 meter");
314 BOOST_UNITS_TEST_OUTPUT(1500.0*meter_base_unit::unit_type(), "1.5 kilometer");
315 BOOST_UNITS_TEST_OUTPUT(1.5e7*meter_base_unit::unit_type(), "15 megameter");
316 BOOST_UNITS_TEST_OUTPUT(1.5e-3*meter_base_unit::unit_type(), "1.5 millimeter");
317 BOOST_UNITS_TEST_OUTPUT(1.5e-9*meter_base_unit::unit_type(), "1.5 nanometer");
318 BOOST_UNITS_TEST_OUTPUT(1.5e-8*meter_base_unit::unit_type(), "15 nanometer");
319 BOOST_UNITS_TEST_OUTPUT(1.5e-10*meter_base_unit::unit_type(), "150 picometer");
320 BOOST_UNITS_TEST_OUTPUT(0.0000000012345 * meter_base_unit::unit_type(), "1.2345 nanometer");
321
322 // Too small or large for a multiple name.
323 BOOST_UNITS_TEST_OUTPUT_REGEX(9.99999e-25 * meter_base_unit::unit_type(), "9\\.99999e-0?25 meter"); // Just too small for multiple.
324 BOOST_UNITS_TEST_OUTPUT_REGEX(1e+28 * meter_base_unit::unit_type(), "1e\\+0?28 meter"); // Just too large for multiple.
325 BOOST_UNITS_TEST_OUTPUT_REGEX(1.5e-25 * meter_base_unit::unit_type(), "1\\.5e-0?25 meter"); // Too small for multiple.
326 BOOST_UNITS_TEST_OUTPUT_REGEX(1.5e+28 * meter_base_unit::unit_type(), "1\\.5e\\+0?28 meter"); // Too large for multiple.
327 // Too 'biggest or too smallest'.
328 BOOST_UNITS_TEST_OUTPUT_REGEX((std::numeric_limits<float>::max)()*meter_base_unit::unit_type(), "3\\.40282e\\+0?38 meter");
329 BOOST_UNITS_TEST_OUTPUT_REGEX((std::numeric_limits<float>::min)()*meter_base_unit::unit_type(), "1\\.17549e-0?38 meter");
330 BOOST_UNITS_TEST_OUTPUT((std::numeric_limits<double>::max)()*meter_base_unit::unit_type(), "1.79769e+308 meter");
331 BOOST_UNITS_TEST_OUTPUT((std::numeric_limits<double>::min)()*meter_base_unit::unit_type(), "2.22507e-308 meter");
332 // Infinity and NaN
333 BOOST_UNITS_TEST_OUTPUT_REGEX(std::numeric_limits<float>::infinity()*meter_base_unit::unit_type(), "(1\\.#INF|inf|INF|Inf) meter");
334 BOOST_UNITS_TEST_OUTPUT_REGEX(-std::numeric_limits<float>::infinity()*meter_base_unit::unit_type(), "-(1\\.#INF|inf|INF|Inf) meter");
335 BOOST_UNITS_TEST_OUTPUT_REGEX(std::numeric_limits<double>::quiet_NaN()*meter_base_unit::unit_type(), "(1\\.#QNAN|nan|NaNQ|NaN) meter");
336 BOOST_UNITS_TEST_OUTPUT_REGEX(-std::numeric_limits<double>::quiet_NaN()*meter_base_unit::unit_type(), "-?(1\\.#IND|nan|nan\\(ind\\)|NaNQ|NaN) meter");
337
338 BOOST_UNITS_TEST_OUTPUT(1.5*velocity(), "1.5 meter second^-1");
339 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_length(), "1.5 kilometer");
340 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_velocity1(), "1.5 kilo(meter second^-1)");
341 BOOST_UNITS_TEST_OUTPUT(1.5*millisecond_base_unit::unit_type(), "1.5 millisecond");
342 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_time(), "1.5 millisecond");
343 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_velocity2(), "1.5 meter millisecond^-1");
344 BOOST_UNITS_TEST_OUTPUT(1.5*area(), "1.5 meter^2");
345 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_area(), "1.5 kilo(meter^2)");
346 BOOST_UNITS_TEST_OUTPUT(1.5*double_scaled_length(), "1.536 megameter"); // 1.5 * 2^10 = 1.5 * 1024 = 1.536
347 BOOST_UNITS_TEST_OUTPUT(1.5*double_scaled_length2(), "1.5 kiloscaled_meter");
348 BOOST_UNITS_TEST_OUTPUT(1.5*custom1(), "1.5 custom1");
349 BOOST_UNITS_TEST_OUTPUT(1.5*custom2(), "1.5 custom2");
350 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_custom1(), "1.5 kilocustom1");
351 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_custom2(), "1.5 kilocustom2");
352 BOOST_UNITS_TEST_OUTPUT(1.5*boost::units::absolute<meter_base_unit::unit_type>(), "1.5 absolute meter");
353 BOOST_UNITS_TEST_OUTPUT(std::pow(2., 10) * byte_base_unit::unit_type(), "1.024 kilobyte");
354
355 BOOST_UNITS_TEST_OUTPUT(1.5, "1.5"); // scalar.
356 BOOST_UNITS_TEST_OUTPUT(1567., "1567"); // scalars are *not* autoprefixed.
357 BOOST_UNITS_TEST_OUTPUT(0.00015, "0.00015"); // scalars are *not* autoprefixed.
358 BOOST_UNITS_TEST_OUTPUT(-1.5, "-1.5"); // scalar.
359 BOOST_UNITS_TEST_OUTPUT(-1567., "-1567"); // scalars are *not* autoprefixed.
360 BOOST_UNITS_TEST_OUTPUT(-0.00015, "-0.00015"); // scalars are *not* autoprefixed.
361#undef FORMATTERS
362}
363
364void test_output_autoprefixed_quantity_symbol()
365{ // Engineering autoprefix, with symbol format.
366#define FORMATTERS << boost::units::symbol_format << boost::units::engineering_prefix
367 // Single base unit like m.
368 BOOST_UNITS_TEST_OUTPUT(1.5*meter_base_unit::unit_type(), "1.5 m");
369 BOOST_UNITS_TEST_OUTPUT(1500.0*meter_base_unit::unit_type(), "1.5 km");
370 BOOST_UNITS_TEST_OUTPUT(1.5e7*meter_base_unit::unit_type(), "15 Mm");
371 BOOST_UNITS_TEST_OUTPUT(1.5e-3*meter_base_unit::unit_type(), "1.5 mm");
372 BOOST_UNITS_TEST_OUTPUT(1.5e-9*meter_base_unit::unit_type(), "1.5 nm");
373 BOOST_UNITS_TEST_OUTPUT(1.5e-8*meter_base_unit::unit_type(), "15 nm");
374 BOOST_UNITS_TEST_OUTPUT(1.5e-10*meter_base_unit::unit_type(), "150 pm");
375 // Too small or large for a multiple name.
376 BOOST_UNITS_TEST_OUTPUT_REGEX(9.99999e-25 * meter_base_unit::unit_type(), "9\\.99999e-0?25 m"); // Just too small for multiple.
377 BOOST_UNITS_TEST_OUTPUT_REGEX(1e+28 * meter_base_unit::unit_type(), "1e\\+0?28 m"); // Just too large for multiple.
378 BOOST_UNITS_TEST_OUTPUT_REGEX(1.5e-25 * meter_base_unit::unit_type(), "1\\.5e-0?25 m"); // Too small for multiple.
379 BOOST_UNITS_TEST_OUTPUT_REGEX(1.5e+28 * meter_base_unit::unit_type(), "1\\.5e\\+0?28 m"); // Too large for multiple.
380 //
381 BOOST_UNITS_TEST_OUTPUT_REGEX((std::numeric_limits<float>::max)()*meter_base_unit::unit_type(), "3\\.40282e\\+0?38 m");
382 BOOST_UNITS_TEST_OUTPUT_REGEX((std::numeric_limits<float>::min)()*meter_base_unit::unit_type(), "1\\.17549e-0?38 m");
383 BOOST_UNITS_TEST_OUTPUT((std::numeric_limits<double>::max)()*meter_base_unit::unit_type(), "1.79769e+308 m");
384 BOOST_UNITS_TEST_OUTPUT((std::numeric_limits<double>::min)()*meter_base_unit::unit_type(), "2.22507e-308 m");
385
386 BOOST_UNITS_TEST_OUTPUT(1.5*velocity(), "1.5 m s^-1");
387 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_length(), "1.5 km");
388 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_velocity1(), "1.5 k(m s^-1)");
389 BOOST_UNITS_TEST_OUTPUT(1.5*millisecond_base_unit::unit_type(), "1.5 ms");
390 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_time(), "1.5 ms");
391 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_velocity2(), "1.5 m ms^-1");
392 BOOST_UNITS_TEST_OUTPUT(1.5*area(), "1.5 m^2");
393 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_area(), "1.5 k(m^2)");
394 BOOST_UNITS_TEST_OUTPUT(1.5*double_scaled_length(), "1.536 Mm"); // 1.5 * 2^10 = 1.5 * 1024 = 1.536
395 BOOST_UNITS_TEST_OUTPUT(1.5*double_scaled_length2(), "1.5 kscm");
396 BOOST_UNITS_TEST_OUTPUT(1.5*custom1(), "1.5 c1");
397 BOOST_UNITS_TEST_OUTPUT(1.5*custom2(), "1.5 c2");
398 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_custom1(), "1.5 kc1");
399 BOOST_UNITS_TEST_OUTPUT(1.5*scaled_custom2(), "1.5 kc2");
400 BOOST_UNITS_TEST_OUTPUT(1.5*boost::units::absolute<meter_base_unit::unit_type>(), "1.5 absolute m");
401 BOOST_UNITS_TEST_OUTPUT(std::pow(2., 10) * byte_base_unit::unit_type(), "1.024 kb");
402
403#undef FORMATTERS
404}
405
406void test_output_auto_binary_prefixed_quantity_symbol()
407{ // Binary prefix with symbol format.
408#define FORMATTERS << boost::units::symbol_format << boost::units::binary_prefix
409 BOOST_UNITS_TEST_OUTPUT(1024 * byte_base_unit::unit_type(), "1 Kib");
410 BOOST_UNITS_TEST_OUTPUT(std::pow(2., 20) * byte_base_unit::unit_type(), "1 Mib");
411 BOOST_UNITS_TEST_OUTPUT(std::pow(2., 30) * byte_base_unit::unit_type(), "1 Gib");
412 BOOST_UNITS_TEST_OUTPUT(std::pow(2., 40) * byte_base_unit::unit_type(), "1 Tib");
413 BOOST_UNITS_TEST_OUTPUT(std::pow(2., 50) * byte_base_unit::unit_type(), "1 Pib");
414 BOOST_UNITS_TEST_OUTPUT(std::pow(2., 60) * byte_base_unit::unit_type(), "1 Eib");
415 BOOST_UNITS_TEST_OUTPUT(std::pow(2., 70) * byte_base_unit::unit_type(), "1 Zib");
416 BOOST_UNITS_TEST_OUTPUT(std::pow(2., 80) * byte_base_unit::unit_type(), "1 Yib");
417 BOOST_UNITS_TEST_OUTPUT(42, "42"); // integer scalar.
418 BOOST_UNITS_TEST_OUTPUT(-42, "-42"); // integer scalar.
419 BOOST_UNITS_TEST_OUTPUT(1567, "1567"); // scalars are *not* autoprefixed.
420 BOOST_UNITS_TEST_OUTPUT(-1567, "-1567"); // scalars are *not* autoprefixed.
421#undef FORMATTERS
422}
423
424void test_output_auto_binary_prefixed_quantity_name()
425{ // Binary prefix with name format.
426 // http://physics.nist.gov/cuu/Units/binary.html
427 // 1998 the International Electrotechnical Commission (IEC) approved
428 // IEC 60027-2, Second edition, 2000-11, Letter symbols to be used in electrical technology
429 // - Part 2: Telecommunications and electronics.
430 // IEC 80000-13:2008, Quantities and units
431 // - Part 13: Information science and technology
432#define FORMATTERS << boost::units::name_format << boost::units::binary_prefix
433 BOOST_UNITS_TEST_OUTPUT(2048 * byte_base_unit::unit_type(), "2 kibibyte");
434 BOOST_UNITS_TEST_OUTPUT(std::pow(2., 32) *byte_base_unit::unit_type(), "4 gibibyte");
435 BOOST_UNITS_TEST_OUTPUT(std::pow(2., 41) *byte_base_unit::unit_type(), "2 tebibyte"); // http://en.wikipedia.org/wiki/Tebibyte
436 BOOST_UNITS_TEST_OUTPUT(std::pow(2., 50) *byte_base_unit::unit_type(), "1 pebibyte");
437 BOOST_UNITS_TEST_OUTPUT(std::pow(2., 60) *byte_base_unit::unit_type(), "1 exbibyte");
438 BOOST_UNITS_TEST_OUTPUT(std::pow(2., 70) *byte_base_unit::unit_type(), "1 zebibyte");
439 BOOST_UNITS_TEST_OUTPUT(std::pow(2., 80) *byte_base_unit::unit_type(), "1 yobibyte");
440 BOOST_UNITS_TEST_OUTPUT(2048, "2048"); // scalars are *not* autoprefixed.
441 BOOST_UNITS_TEST_OUTPUT(-4096, "-4096"); // scalars are *not* autoprefixed.
442#undef FORMATTERS
443}
444
445// Tests on using more than one format or prefix - only the last specified should be used.
446// (This may indicate a programming mistake, but it is ignored).
447void test_output_quantity_name_duplicate()
448{ // Ensure that if more than one format specified, only the last is used.
449#define FORMATTERS << boost::units::symbol_format << boost::units::name_format
450 BOOST_UNITS_TEST_OUTPUT(1.5*meter_base_unit::unit_type(), "1.5 meter");
451#undef FORMATTERS
452}
453
454void test_output_quantity_symbol_duplicate()
455{ // Ensure that if more than one format specified, only the last is used.
456#define FORMATTERS << boost::units::name_format << boost::units::symbol_format
457 BOOST_UNITS_TEST_OUTPUT(1.5*meter_base_unit::unit_type(), "1.5 m");
458#undef FORMATTERS
459}
460
461void test_output_auto_binary_prefixed_quantity_name_duplicate()
462{ // Ensure that if more than one auto prefix specified, only the last is used.
463#define FORMATTERS << boost::units::name_format << boost::units::binary_prefix << boost::units::engineering_prefix
464 BOOST_UNITS_TEST_OUTPUT(2048 * byte_base_unit::unit_type(), "2.048 kilobyte");
465#undef FORMATTERS
466}
467
468void test_output_auto_binary_prefixed_quantity_symbol_duplicate()
469{ // Ensure that if more than one auto prefix specified, only the last is used.
470#define FORMATTERS << boost::units::symbol_format << boost::units::engineering_prefix << boost::units::binary_prefix
471 BOOST_UNITS_TEST_OUTPUT(2048 * byte_base_unit::unit_type(), "2 Kib");
472#undef FORMATTERS
473}
474
475void test_output_typename_format()
476{ // Displays typename formatting result. The test doesn't check the formatting result
477 // and thus doesn't fail because the formatting result is platform-dependent.
478#define FORMATTERS << boost::units::typename_format
479 BOOST_UNITS_TEST_OUTPUT_DISPLAY(meter_base_unit::unit_type());
480 BOOST_UNITS_TEST_OUTPUT_DISPLAY(velocity());
481 BOOST_UNITS_TEST_OUTPUT_DISPLAY(scaled_length());
482 BOOST_UNITS_TEST_OUTPUT_DISPLAY(scaled_velocity1());
483 BOOST_UNITS_TEST_OUTPUT_DISPLAY(millisecond_base_unit::unit_type());
484 BOOST_UNITS_TEST_OUTPUT_DISPLAY(scaled_time());
485 BOOST_UNITS_TEST_OUTPUT_DISPLAY(scaled_velocity2());
486 BOOST_UNITS_TEST_OUTPUT_DISPLAY(area());
487 BOOST_UNITS_TEST_OUTPUT_DISPLAY(scaled_area());
488 BOOST_UNITS_TEST_OUTPUT_DISPLAY(double_scaled_length());
489 BOOST_UNITS_TEST_OUTPUT_DISPLAY(double_scaled_length2());
490 BOOST_UNITS_TEST_OUTPUT_DISPLAY(custom1());
491 BOOST_UNITS_TEST_OUTPUT_DISPLAY(custom2());
492 BOOST_UNITS_TEST_OUTPUT_DISPLAY(scaled_custom1());
493 BOOST_UNITS_TEST_OUTPUT_DISPLAY(scaled_custom2());
494 BOOST_UNITS_TEST_OUTPUT_DISPLAY(boost::units::absolute<meter_base_unit::unit_type>());
495#undef FORMATTERS
496}
497
498int main()
499{
500 test_output_unit_symbol();
501 test_output_unit_raw();
502 test_output_unit_name();
503 test_output_quantity_symbol();
504 test_output_quantity_raw();
505 test_output_quantity_name();
506 test_output_autoprefixed_quantity_name();
507 test_output_autoprefixed_quantity_symbol();
508 test_output_auto_binary_prefixed_quantity_symbol();
509 test_output_auto_binary_prefixed_quantity_name();
510 test_output_quantity_name_duplicate();
511 test_output_quantity_symbol_duplicate();
512 test_output_auto_binary_prefixed_quantity_name_duplicate();
513 test_output_auto_binary_prefixed_quantity_symbol_duplicate();
514 test_output_typename_format();
515 return boost::report_errors();
516}
517

source code of boost/libs/units/test/test_output.cpp