converter_test.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. // (c) Copyright Fernando Luis Cacciola Carballal 2000-2004
  2. // Use, modification, and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // See library home page at http://www.boost.org/libs/numeric/conversion
  6. //
  7. // Contact the author at: fernando_cacciola@hotmail.com
  8. //
  9. #include<cstdlib>
  10. #include<iostream>
  11. #include<iomanip>
  12. #include<string>
  13. #include<typeinfo>
  14. #include<vector>
  15. #include<algorithm>
  16. #include "boost/config.hpp"
  17. #include "boost/cstdint.hpp"
  18. #include "boost/utility.hpp"
  19. //
  20. // Borland 5.5 lacks the following math overloads
  21. //
  22. #if BOOST_WORKAROUND(__BORLANDC__, <= 0x551)
  23. namespace std
  24. {
  25. inline float ceil (float x) { return std::ceil ( static_cast<double>(x)); }
  26. inline float floor (float x) { return std::floor ( static_cast<double>(x)); }
  27. inline long double ceil (long double x) { return std::ceill (x); }
  28. inline long double floor (long double x) { return std::floorl(x); }
  29. } // namespace std
  30. #endif
  31. #include "boost/numeric/conversion/converter.hpp"
  32. #include "boost/numeric/conversion/cast.hpp"
  33. #ifdef __BORLANDC__
  34. #pragma hdrstop
  35. #endif
  36. #include "test_helpers.cpp"
  37. #include "test_helpers2.cpp"
  38. #include "test_helpers3.cpp"
  39. #include "boost/mpl/alias.hpp"
  40. using std::cout ;
  41. // A generic 'abs' function.
  42. template<class N> inline N absG ( N v )
  43. {
  44. return v < static_cast<N>(0) ? static_cast<N>(-v) : v ;
  45. }
  46. template<> inline unsigned char absG<unsigned char> ( unsigned char v ) { return v ; }
  47. template<> inline unsigned short absG<unsigned short> ( unsigned short v ) { return v ; }
  48. template<> inline unsigned int absG<unsigned int> ( unsigned int v ) { return v ; }
  49. template<> inline unsigned long absG<unsigned long> ( unsigned long v ) { return v ; }
  50. template<class T> inline void unused_variable ( T const& ) {}
  51. //
  52. // The following function excersizes specific conversions that cover
  53. // usual and boundary cases for each relevant combination.
  54. //
  55. void test_conversions()
  56. {
  57. using namespace boost ;
  58. using namespace numeric ;
  59. // To help the test found possible bugs a random numbers are used.
  60. #if !defined(BOOST_NO_STDC_NAMESPACE)
  61. using std::rand ;
  62. #endif
  63. boost::int16_t v16 ;
  64. boost::uint16_t uv16 ;
  65. boost::int32_t v32 ;
  66. boost::uint32_t uv32 ;
  67. volatile float fv ; // avoid this to be cached internally in some fpu register
  68. volatile double dv ; // avoid this to be cached internally in some fpu register
  69. //
  70. // sample (representative) conversions:
  71. //
  72. cout << "Testing representative conversions\n";
  73. // integral to integral
  74. // signed to signed
  75. // not subranged
  76. v16 = static_cast<boost::int16_t>(rand());
  77. TEST_SUCCEEDING_CONVERSION_DEF(boost::int32_t,boost::int16_t,v16,v16);
  78. // subranged
  79. v16 = static_cast<boost::int16_t>(rand());
  80. TEST_SUCCEEDING_CONVERSION_DEF(boost::int16_t,boost::int32_t,v16,v16);
  81. TEST_POS_OVERFLOW_CONVERSION_DEF(boost::int16_t,boost::int32_t,bounds<boost::int16_t>::highest() + boost::int32_t(1) ) ;
  82. TEST_NEG_OVERFLOW_CONVERSION_DEF(boost::int16_t,boost::int32_t,bounds<boost::int16_t>::lowest() - boost::int32_t(1) ) ;
  83. // signed to unsigned
  84. // subranged
  85. v32 = absG(static_cast<boost::int32_t>(rand()));
  86. v16 = absG(static_cast<boost::int16_t>(rand()));
  87. TEST_SUCCEEDING_CONVERSION_DEF(boost::uint32_t,boost::int32_t,v32,v32);
  88. TEST_SUCCEEDING_CONVERSION_DEF(boost::uint16_t,boost::int32_t,v16,v16);
  89. TEST_POS_OVERFLOW_CONVERSION_DEF(boost::uint16_t,boost::int32_t,bounds<boost::uint16_t>::highest() + boost::int32_t(1) ) ;
  90. TEST_NEG_OVERFLOW_CONVERSION_DEF(boost::uint32_t,boost::int32_t,boost::int32_t(-1) ) ;
  91. // unsigned to signed
  92. // not subranged
  93. v32 = absG(static_cast<boost::int32_t>(rand()));
  94. TEST_SUCCEEDING_CONVERSION_DEF(boost::int32_t,boost::uint32_t,v32,v32);
  95. // subranged
  96. v16 = absG(static_cast<boost::int16_t>(rand()));
  97. TEST_SUCCEEDING_CONVERSION_DEF(boost::int16_t,boost::uint32_t,v16,v16);
  98. TEST_POS_OVERFLOW_CONVERSION_DEF(boost::int32_t,boost::uint32_t,bounds<boost::uint32_t>::highest() ) ;
  99. TEST_POS_OVERFLOW_CONVERSION_DEF(boost::int16_t,boost::uint32_t,bounds<boost::uint32_t>::highest() ) ;
  100. // unsigned to unsigned
  101. // not subranged
  102. uv16 = static_cast<boost::uint16_t>(rand());
  103. TEST_SUCCEEDING_CONVERSION_DEF(boost::uint32_t,boost::uint16_t,uv16,uv16);
  104. // subranged
  105. uv16 = static_cast<boost::uint16_t>(rand());
  106. TEST_SUCCEEDING_CONVERSION_DEF(boost::uint16_t,boost::uint32_t,uv16,uv16);
  107. TEST_POS_OVERFLOW_CONVERSION_DEF(boost::uint16_t,boost::uint32_t,bounds<boost::uint32_t>::highest() ) ;
  108. // integral to float
  109. // from signed integral
  110. v32 = static_cast<boost::int32_t>(rand());
  111. TEST_SUCCEEDING_CONVERSION_DEF(double,boost::int32_t,v32,v32);
  112. // from uint32_tegral
  113. uv32 = static_cast<boost::uint32_t>(rand());
  114. TEST_SUCCEEDING_CONVERSION_DEF(double,boost::uint32_t,uv32,uv32);
  115. // float to integral
  116. // to signed integral
  117. v32 = static_cast<boost::int32_t>(rand());
  118. TEST_SUCCEEDING_CONVERSION_DEF(boost::int32_t,double,v32,v32);
  119. dv = static_cast<double>(bounds<boost::uint32_t>::highest()) + 1.0 ;
  120. TEST_POS_OVERFLOW_CONVERSION_DEF(boost::int32_t,double,dv) ;
  121. TEST_NEG_OVERFLOW_CONVERSION_DEF(boost::int32_t,double,-dv) ;
  122. // float to float
  123. // not subranged
  124. fv = static_cast<float>(rand()) / static_cast<float>(3) ;
  125. TEST_SUCCEEDING_CONVERSION_DEF(double,float,fv,fv);
  126. // subranged
  127. fv = static_cast<float>(rand()) / static_cast<float>(3) ;
  128. TEST_SUCCEEDING_CONVERSION_DEF(float,double,fv,fv);
  129. TEST_POS_OVERFLOW_CONVERSION_DEF(float,double,bounds<double>::highest()) ;
  130. TEST_NEG_OVERFLOW_CONVERSION_DEF(float,double,bounds<double>::lowest ()) ;
  131. }
  132. // Custom OverflowHandler
  133. struct custom_overflow_handler
  134. {
  135. void operator() ( boost::numeric::range_check_result r )
  136. {
  137. if ( r == boost::numeric::cNegOverflow )
  138. cout << "negative_overflow detected!\n" ;
  139. else if ( r == boost::numeric::cPosOverflow )
  140. cout << "positive_overflow detected!\n" ;
  141. }
  142. } ;
  143. template<class T, class S,class OverflowHandler>
  144. void test_overflow_handler( MATCH_FNTPL_ARG(T), MATCH_FNTPL_ARG(S), MATCH_FNTPL_ARG(OverflowHandler),
  145. PostCondition pos,
  146. PostCondition neg
  147. )
  148. {
  149. typedef boost::numeric::conversion_traits<T,S> traits ;
  150. typedef boost::numeric::converter<T,S,traits,OverflowHandler> converter ;
  151. static const S psrc = boost::numeric::bounds<S>::highest();
  152. static const S nsrc = boost::numeric::bounds<S>::lowest ();
  153. static const T pres = static_cast<T>(psrc);
  154. static const T nres = static_cast<T>(nsrc);
  155. test_conv_base ( ConversionInstance<converter>(pres,psrc,pos) ) ;
  156. test_conv_base ( ConversionInstance<converter>(nres,nsrc,neg) ) ;
  157. }
  158. template<class T, class S>
  159. void test_overflow_handlers( MATCH_FNTPL_ARG(T), MATCH_FNTPL_ARG(S) )
  160. {
  161. cout << "Testing Silent Overflow Handler policy\n";
  162. test_overflow_handler( SET_FNTPL_ARG(T),
  163. SET_FNTPL_ARG(S),
  164. SET_FNTPL_ARG(boost::numeric::silent_overflow_handler),
  165. c_converted,
  166. c_converted
  167. ) ;
  168. cout << "Testing Default Overflow Handler policy\n";
  169. test_overflow_handler( SET_FNTPL_ARG(T),
  170. SET_FNTPL_ARG(S),
  171. SET_FNTPL_ARG(boost::numeric::def_overflow_handler),
  172. c_pos_overflow,
  173. c_neg_overflow
  174. ) ;
  175. cout << "Testing Custom (User-Defined) Overflow Handler policy\n";
  176. test_overflow_handler( SET_FNTPL_ARG(T),
  177. SET_FNTPL_ARG(S),
  178. SET_FNTPL_ARG(custom_overflow_handler),
  179. c_converted,
  180. c_converted
  181. ) ;
  182. }
  183. // For a given float-type number 'n' of integer value (n.0), check the conversions
  184. // within the range [n-1,n+1] taking values at: (n-1,n-0.5,n,n+0.5,n+1).
  185. // For each sampled value there is an expected result and a PostCondition according to the
  186. // specified round_style.
  187. //
  188. template<class T, class S, class Float2IntRounder>
  189. void test_rounding_conversion ( MATCH_FNTPL_ARG(T), MATCH_FNTPL_ARG(Float2IntRounder),
  190. S s,
  191. PostCondition resl1,
  192. PostCondition resl0,
  193. PostCondition res,
  194. PostCondition resr0,
  195. PostCondition resr1
  196. )
  197. {
  198. typedef boost::numeric::conversion_traits<T,S> Traits ;
  199. typedef boost::numeric::converter<T,S, Traits, boost::numeric::def_overflow_handler,Float2IntRounder>
  200. Converter ;
  201. S sl1 = s - static_cast<S>(1);
  202. S sl0 = s - static_cast<S>(0.5);
  203. S sr0 = s + static_cast<S>(0.5);
  204. S sr1 = s + static_cast<S>(1);
  205. T tl1 = static_cast<T>( Converter::nearbyint(sl1) );
  206. T tl0 = static_cast<T>( Converter::nearbyint(sl0) );
  207. T t = static_cast<T>( Converter::nearbyint(s) );
  208. T tr0 = static_cast<T>( Converter::nearbyint(sr0) );
  209. T tr1 = static_cast<T>( Converter::nearbyint(sr1) );
  210. test_conv_base ( ConversionInstance<Converter>(tl1,sl1,resl1) ) ;
  211. test_conv_base ( ConversionInstance<Converter>(tl0,sl0,resl0) ) ;
  212. test_conv_base ( ConversionInstance<Converter>(t,s,res) ) ;
  213. test_conv_base ( ConversionInstance<Converter>(tr0,sr0,resr0) ) ;
  214. test_conv_base ( ConversionInstance<Converter>(tr1,sr1,resr1) ) ;
  215. }
  216. template<class T,class S>
  217. void test_round_style( MATCH_FNTPL_ARG(T), MATCH_FNTPL_ARG(S) )
  218. {
  219. S min = boost::numeric::bounds<T>::lowest();
  220. S max = boost::numeric::bounds<T>::highest();
  221. cout << "Testing 'Trunc' Float2IntRounder policy\n";
  222. test_rounding_conversion(SET_FNTPL_ARG(T),
  223. SET_FNTPL_ARG(boost::numeric::Trunc<S>),
  224. min,
  225. c_neg_overflow,
  226. c_converted,
  227. c_converted,
  228. c_converted,
  229. c_converted
  230. ) ;
  231. test_rounding_conversion(SET_FNTPL_ARG(T),
  232. SET_FNTPL_ARG(boost::numeric::Trunc<S>),
  233. max,
  234. c_converted,
  235. c_converted,
  236. c_converted,
  237. c_converted,
  238. c_pos_overflow
  239. ) ;
  240. cout << "Testing 'RoundEven' Float2IntRounder policy\n";
  241. test_rounding_conversion(SET_FNTPL_ARG(T),
  242. SET_FNTPL_ARG(boost::numeric::RoundEven<S>),
  243. min,
  244. c_neg_overflow,
  245. c_converted,
  246. c_converted,
  247. c_converted,
  248. c_converted
  249. ) ;
  250. test_rounding_conversion(SET_FNTPL_ARG(T),
  251. SET_FNTPL_ARG(boost::numeric::RoundEven<S>),
  252. max,
  253. c_converted,
  254. c_converted,
  255. c_converted,
  256. c_pos_overflow,
  257. c_pos_overflow
  258. ) ;
  259. cout << "Testing 'Ceil' Float2IntRounder policy\n";
  260. test_rounding_conversion(SET_FNTPL_ARG(T),
  261. SET_FNTPL_ARG(boost::numeric::Ceil<S>),
  262. min,
  263. c_neg_overflow,
  264. c_converted,
  265. c_converted,
  266. c_converted,
  267. c_converted
  268. ) ;
  269. test_rounding_conversion(SET_FNTPL_ARG(T),
  270. SET_FNTPL_ARG(boost::numeric::Ceil<S>),
  271. max,
  272. c_converted,
  273. c_converted,
  274. c_converted,
  275. c_pos_overflow,
  276. c_pos_overflow
  277. ) ;
  278. cout << "Testing 'Floor' Float2IntRounder policy\n" ;
  279. test_rounding_conversion(SET_FNTPL_ARG(T),
  280. SET_FNTPL_ARG(boost::numeric::Floor<S>),
  281. min,
  282. c_neg_overflow,
  283. c_neg_overflow,
  284. c_converted,
  285. c_converted,
  286. c_converted
  287. ) ;
  288. test_rounding_conversion(SET_FNTPL_ARG(T),
  289. SET_FNTPL_ARG(boost::numeric::Floor<S>),
  290. max,
  291. c_converted,
  292. c_converted,
  293. c_converted,
  294. c_converted,
  295. c_pos_overflow
  296. ) ;
  297. }
  298. void test_round_even( double n, double x )
  299. {
  300. double r = boost::numeric::RoundEven<double>::nearbyint(n);
  301. BOOST_CHECK( r == x ) ;
  302. }
  303. void test_round_even()
  304. {
  305. cout << "Testing 'RoundEven' tie-breaking\n";
  306. double min = boost::numeric::bounds<double>::lowest();
  307. double max = boost::numeric::bounds<double>::highest();
  308. #if !defined(BOOST_NO_STDC_NAMESPACE)
  309. using std::floor ;
  310. using std::ceil ;
  311. #endif
  312. test_round_even(min, floor(min));
  313. test_round_even(max, ceil (max));
  314. test_round_even(2.0, 2.0);
  315. test_round_even(2.3, 2.0);
  316. test_round_even(2.5, 2.0);
  317. test_round_even(2.7, 3.0);
  318. test_round_even(3.0, 3.0);
  319. test_round_even(3.3, 3.0);
  320. test_round_even(3.5, 4.0);
  321. test_round_even(3.7, 4.0);
  322. }
  323. int double_to_int ( double n ) { return static_cast<int>(n) ; }
  324. void test_converter_as_function_object()
  325. {
  326. cout << "Testing converter as function object.\n";
  327. // Create a sample sequence of double values.
  328. std::vector<double> S ;
  329. for ( int i = 0 ; i < 10 ; ++ i )
  330. S.push_back( i * ( 18.0 / 19.0 ) );
  331. // Create a sequence of int values from 's' using the standard conversion.
  332. std::vector<int> W ;
  333. std::transform(S.begin(),S.end(),std::back_inserter(W),double_to_int);
  334. // Create a sequence of int values from s using a default numeric::converter
  335. std::vector<int> I ;
  336. std::transform(S.begin(),
  337. S.end(),
  338. std::back_inserter(I),
  339. boost::numeric::converter<int,double>()
  340. ) ;
  341. // Match 'w' and 'i' which should be equal.
  342. bool double_to_int_OK = std::equal(W.begin(),W.end(),I.begin()) ;
  343. BOOST_CHECK_MESSAGE(double_to_int_OK, "converter (int,double) as function object");
  344. // Create a sequence of double values from s using a default numeric::converter (which should be the trivial conv).
  345. std::vector<double> D ;
  346. std::transform(S.begin(),
  347. S.end(),
  348. std::back_inserter(D),
  349. boost::numeric::converter<double,double>()
  350. ) ;
  351. // Match 's' and 'd' which should be equal.
  352. bool double_to_double_OK = std::equal(S.begin(),S.end(),D.begin()) ;
  353. BOOST_CHECK_MESSAGE(double_to_double_OK, "converter (double,double) as function object");
  354. }
  355. #if BOOST_WORKAROUND(__IBMCPP__, <= 600 ) // VCAPP6
  356. # define UNOPTIMIZED
  357. #else
  358. # define UNOPTIMIZED volatile
  359. #endif
  360. void test_optimizations()
  361. {
  362. using namespace boost;
  363. using namespace numeric;
  364. float fv0 = 18.0f / 19.0f ;
  365. // This code deosn't produce any output.
  366. // It is intended to show the optimization of numeric::converter<> by manual inspection
  367. // of the generated code.
  368. // Each test shows first the equivalent hand-coded version.
  369. // The numeric_cast<> code should be the same if full compiler optimization/inlining is used.
  370. //---------------------------------
  371. // trivial conversion.
  372. //
  373. // equivalent code:
  374. UNOPTIMIZED float fv1a = fv0 ;
  375. float fv1b = numeric_cast<float>(fv0);
  376. unused_variable(fv1a);
  377. unused_variable(fv1b);
  378. //
  379. //---------------------------------
  380. //---------------------------------
  381. // nonsubranged conversion.
  382. //
  383. // equivalent code:
  384. UNOPTIMIZED double dv1a = static_cast<double>(fv0);
  385. double dv1b = numeric_cast<double>(fv0);
  386. unused_variable(dv1a);
  387. unused_variable(dv1b);
  388. //
  389. //---------------------------------
  390. //------------------------------------------------------
  391. // subranged conversion with both-sided range checking.
  392. //
  393. // equivalent code:
  394. {
  395. double const& s = dv1b ;
  396. // range checking
  397. range_check_result r = s < static_cast<double>(bounds<float>::lowest())
  398. ? cNegOverflow : cInRange ;
  399. if ( r == cInRange )
  400. {
  401. r = s > static_cast<double>(bounds<float>::highest()) ? cPosOverflow : cInRange ;
  402. }
  403. if ( r == cNegOverflow )
  404. throw negative_overflow() ;
  405. else if ( r == cPosOverflow )
  406. throw positive_overflow() ;
  407. // conversion
  408. UNOPTIMIZED float fv2a = static_cast<float>(s);
  409. unused_variable(fv2a);
  410. }
  411. float fv2b = numeric_cast<float>(dv1b);
  412. unused_variable(fv2b);
  413. //
  414. //---------------------------------
  415. //---------------------------------
  416. // subranged rounding conversion
  417. //
  418. // equivalent code:
  419. {
  420. double const& s = dv1b ;
  421. // range checking
  422. range_check_result r = s <= static_cast<double>(bounds<int>::lowest()) - static_cast<double>(1.0)
  423. ? cNegOverflow : cInRange ;
  424. if ( r == cInRange )
  425. {
  426. r = s >= static_cast<double>(bounds<int>::highest()) + static_cast<double>(1.0)
  427. ? cPosOverflow : cInRange ;
  428. }
  429. if ( r == cNegOverflow )
  430. throw negative_overflow() ;
  431. else if ( r == cPosOverflow )
  432. throw positive_overflow() ;
  433. // rounding
  434. #if !defined(BOOST_NO_STDC_NAMESPACE)
  435. using std::floor ;
  436. #endif
  437. double s1 = floor(dv1b + 0.5);
  438. // conversion
  439. UNOPTIMIZED int iv1a = static_cast<int>(s1);
  440. unused_variable(iv1a);
  441. }
  442. int iv1b = numeric_cast<int>(dv1b);
  443. unused_variable(iv1b);
  444. //
  445. //---------------------------------
  446. }
  447. int test_main( int, char* argv[] )
  448. {
  449. std::cout << std::setprecision( std::numeric_limits<long double>::digits10 ) ;
  450. test_conversions();
  451. test_overflow_handlers( SET_FNTPL_ARG(boost::int16_t), SET_FNTPL_ARG(boost::int32_t));
  452. test_round_style(SET_FNTPL_ARG(boost::int32_t), SET_FNTPL_ARG(double) ) ;
  453. test_round_even() ;
  454. test_converter_as_function_object();
  455. test_optimizations() ;
  456. return 0;
  457. }
  458. //---------------------------------------------------------------------------