summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/numeric/conversion/test/converter_test.cpp
blob: a460df1589360b8f9838d7671c8fc0672db530d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
//  (c) Copyright Fernando Luis Cacciola Carballal 2000-2004
//  Use, modification, and distribution is subject to the Boost Software
//  License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)

//  See library home page at http://www.boost.org/libs/numeric/conversion
//
// Contact the author at: fernando_cacciola@hotmail.com
//
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<string>
#include<typeinfo>
#include<vector>
#include<algorithm>

#include "boost/config.hpp"
#include "boost/cstdint.hpp"
#include "boost/utility.hpp"

//
// Borland 5.5 lacks the following math overloads
//
#if BOOST_WORKAROUND(__BORLANDC__, <= 0x551)
namespace std
{

inline float       ceil  (float       x) { return std::ceil  ( static_cast<double>(x)); }
inline float       floor (float       x) { return std::floor ( static_cast<double>(x)); }
inline long double ceil  (long double x) { return std::ceill (x); }
inline long double floor (long double x) { return std::floorl(x); }

} // namespace std
#endif

#include "boost/numeric/conversion/converter.hpp"
#include "boost/numeric/conversion/cast.hpp"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#include "test_helpers.cpp"
#include "test_helpers2.cpp"
#include "test_helpers3.cpp"

#include "boost/mpl/alias.hpp"

using std::cout ;

// A generic 'abs' function.
template<class N> inline N absG ( N v )
{
  return v < static_cast<N>(0) ? static_cast<N>(-v) : v ;
}
template<> inline unsigned char  absG<unsigned char>  ( unsigned char  v ) { return v ; }
template<> inline unsigned short absG<unsigned short> ( unsigned short v ) { return v ; }
template<> inline unsigned int   absG<unsigned int>   ( unsigned int   v ) { return v ; }
template<> inline unsigned long  absG<unsigned long>  ( unsigned long  v ) { return v ; }

template<class T> inline void unused_variable ( T const& ) {}
//
// The following function excersizes specific conversions that cover
// usual and boundary cases for each relevant combination.
//
void test_conversions()
{
  using namespace boost ;
  using namespace numeric ;

  // To help the test found possible bugs a random numbers are used.
#if !defined(BOOST_NO_STDC_NAMESPACE)
  using std::rand ;
#endif

  boost::int16_t v16 ;
  boost::uint16_t uv16 ;
  boost::int32_t v32 ;
  boost::uint32_t uv32 ;

  volatile float  fv ; // avoid this to be cached internally in some fpu register
  volatile double dv ; // avoid this to be cached internally in some fpu register

  //
  // sample (representative) conversions:
  //
  cout << "Testing representative conversions\n";

  // integral to integral

    // signed to signed

      // not subranged
      v16 = static_cast<boost::int16_t>(rand());
      TEST_SUCCEEDING_CONVERSION_DEF(boost::int32_t,boost::int16_t,v16,v16);

      // subranged
      v16 = static_cast<boost::int16_t>(rand());
      TEST_SUCCEEDING_CONVERSION_DEF(boost::int16_t,boost::int32_t,v16,v16);
      TEST_POS_OVERFLOW_CONVERSION_DEF(boost::int16_t,boost::int32_t,bounds<boost::int16_t>::highest() + boost::int32_t(1) ) ;
      TEST_NEG_OVERFLOW_CONVERSION_DEF(boost::int16_t,boost::int32_t,bounds<boost::int16_t>::lowest()  - boost::int32_t(1) ) ;

    // signed to unsigned

      // subranged
      v32 = absG(static_cast<boost::int32_t>(rand()));
      v16 = absG(static_cast<boost::int16_t>(rand()));
      TEST_SUCCEEDING_CONVERSION_DEF(boost::uint32_t,boost::int32_t,v32,v32);
      TEST_SUCCEEDING_CONVERSION_DEF(boost::uint16_t,boost::int32_t,v16,v16);
      TEST_POS_OVERFLOW_CONVERSION_DEF(boost::uint16_t,boost::int32_t,bounds<boost::uint16_t>::highest() + boost::int32_t(1) ) ;
      TEST_NEG_OVERFLOW_CONVERSION_DEF(boost::uint32_t,boost::int32_t,boost::int32_t(-1) ) ;

    // unsigned to signed

      // not subranged
      v32 = absG(static_cast<boost::int32_t>(rand()));
      TEST_SUCCEEDING_CONVERSION_DEF(boost::int32_t,boost::uint32_t,v32,v32);

      // subranged
      v16 = absG(static_cast<boost::int16_t>(rand()));
      TEST_SUCCEEDING_CONVERSION_DEF(boost::int16_t,boost::uint32_t,v16,v16);
      TEST_POS_OVERFLOW_CONVERSION_DEF(boost::int32_t,boost::uint32_t,bounds<boost::uint32_t>::highest() ) ;
      TEST_POS_OVERFLOW_CONVERSION_DEF(boost::int16_t,boost::uint32_t,bounds<boost::uint32_t>::highest() ) ;

    // unsigned to unsigned

      // not subranged
      uv16 = static_cast<boost::uint16_t>(rand());
      TEST_SUCCEEDING_CONVERSION_DEF(boost::uint32_t,boost::uint16_t,uv16,uv16);

      // subranged
      uv16 = static_cast<boost::uint16_t>(rand());
      TEST_SUCCEEDING_CONVERSION_DEF(boost::uint16_t,boost::uint32_t,uv16,uv16);
      TEST_POS_OVERFLOW_CONVERSION_DEF(boost::uint16_t,boost::uint32_t,bounds<boost::uint32_t>::highest() ) ;

  // integral to float

     // from signed integral
     v32 = static_cast<boost::int32_t>(rand());
     TEST_SUCCEEDING_CONVERSION_DEF(double,boost::int32_t,v32,v32);

     // from uint32_tegral
     uv32 = static_cast<boost::uint32_t>(rand());
     TEST_SUCCEEDING_CONVERSION_DEF(double,boost::uint32_t,uv32,uv32);

  // float to integral

     // to signed integral
     v32 =  static_cast<boost::int32_t>(rand());
     TEST_SUCCEEDING_CONVERSION_DEF(boost::int32_t,double,v32,v32);

     dv = static_cast<double>(bounds<boost::uint32_t>::highest()) + 1.0 ;
     TEST_POS_OVERFLOW_CONVERSION_DEF(boost::int32_t,double,dv) ;
     TEST_NEG_OVERFLOW_CONVERSION_DEF(boost::int32_t,double,-dv) ;

  // float to float

    // not subranged
    fv = static_cast<float>(rand()) / static_cast<float>(3) ;
    TEST_SUCCEEDING_CONVERSION_DEF(double,float,fv,fv);


    // subranged
    fv = static_cast<float>(rand()) / static_cast<float>(3) ;
    TEST_SUCCEEDING_CONVERSION_DEF(float,double,fv,fv);
    TEST_POS_OVERFLOW_CONVERSION_DEF(float,double,bounds<double>::highest()) ;
    TEST_NEG_OVERFLOW_CONVERSION_DEF(float,double,bounds<double>::lowest ()) ;
}

// Custom OverflowHandler
struct custom_overflow_handler
{
  void operator() ( boost::numeric::range_check_result r )
  {
    if ( r == boost::numeric::cNegOverflow )
      cout << "negative_overflow detected!\n" ;
    else if ( r == boost::numeric::cPosOverflow )
           cout << "positive_overflow detected!\n" ;
  }
} ;

template<class T, class S,class OverflowHandler>
void test_overflow_handler( MATCH_FNTPL_ARG(T), MATCH_FNTPL_ARG(S), MATCH_FNTPL_ARG(OverflowHandler),
                            PostCondition pos,
                            PostCondition neg
                          )
{
  typedef boost::numeric::conversion_traits<T,S> traits ;
  typedef boost::numeric::converter<T,S,traits,OverflowHandler> converter ;

  static const S psrc = boost::numeric::bounds<S>::highest();
  static const S nsrc = boost::numeric::bounds<S>::lowest ();

  static const T pres = static_cast<T>(psrc);
  static const T nres = static_cast<T>(nsrc);

  test_conv_base ( ConversionInstance<converter>(pres,psrc,pos) ) ;
  test_conv_base ( ConversionInstance<converter>(nres,nsrc,neg) ) ;
}

template<class T, class S>
void test_overflow_handlers( MATCH_FNTPL_ARG(T), MATCH_FNTPL_ARG(S) )
{
  cout << "Testing Silent Overflow Handler policy\n";

  test_overflow_handler( SET_FNTPL_ARG(T),
                         SET_FNTPL_ARG(S),
                         SET_FNTPL_ARG(boost::numeric::silent_overflow_handler),
                         c_converted,
                         c_converted
                       ) ;

  cout << "Testing Default Overflow Handler policy\n";

  test_overflow_handler( SET_FNTPL_ARG(T),
                         SET_FNTPL_ARG(S),
                         SET_FNTPL_ARG(boost::numeric::def_overflow_handler),
                         c_pos_overflow,
                         c_neg_overflow
                       ) ;

  cout << "Testing Custom (User-Defined) Overflow Handler policy\n";

  test_overflow_handler( SET_FNTPL_ARG(T),
                         SET_FNTPL_ARG(S),
                         SET_FNTPL_ARG(custom_overflow_handler),
                         c_converted,
                         c_converted
                       ) ;
}

// For a given float-type number 'n' of integer value (n.0), check the conversions
// within the range [n-1,n+1] taking values at: (n-1,n-0.5,n,n+0.5,n+1).
// For each sampled value there is an expected result and a PostCondition according to the
// specified round_style.
//
template<class T, class S, class Float2IntRounder>
void test_rounding_conversion ( MATCH_FNTPL_ARG(T), MATCH_FNTPL_ARG(Float2IntRounder),
                                S s,
                                PostCondition resl1,
                                PostCondition resl0,
                                PostCondition res,
                                PostCondition resr0,
                                PostCondition resr1
                              )
{
  typedef boost::numeric::conversion_traits<T,S> Traits ;

  typedef boost::numeric::converter<T,S, Traits, boost::numeric::def_overflow_handler,Float2IntRounder>
           Converter ;

  S sl1 = s - static_cast<S>(1);
  S sl0 = s - static_cast<S>(0.5);
  S sr0 = s + static_cast<S>(0.5);
  S sr1 = s + static_cast<S>(1);

  T tl1 = static_cast<T>( Converter::nearbyint(sl1) );
  T tl0 = static_cast<T>( Converter::nearbyint(sl0) );
  T t   = static_cast<T>( Converter::nearbyint(s)   );
  T tr0 = static_cast<T>( Converter::nearbyint(sr0) );
  T tr1 = static_cast<T>( Converter::nearbyint(sr1) );

  test_conv_base ( ConversionInstance<Converter>(tl1,sl1,resl1) ) ;
  test_conv_base ( ConversionInstance<Converter>(tl0,sl0,resl0) ) ;
  test_conv_base ( ConversionInstance<Converter>(t,s,res) ) ;
  test_conv_base ( ConversionInstance<Converter>(tr0,sr0,resr0) ) ;
  test_conv_base ( ConversionInstance<Converter>(tr1,sr1,resr1) ) ;
}


template<class T,class S>
void test_round_style( MATCH_FNTPL_ARG(T), MATCH_FNTPL_ARG(S) )
{
  S min = boost::numeric::bounds<T>::lowest();
  S max = boost::numeric::bounds<T>::highest();

  cout << "Testing 'Trunc' Float2IntRounder policy\n";

  test_rounding_conversion(SET_FNTPL_ARG(T),
                           SET_FNTPL_ARG(boost::numeric::Trunc<S>),
                           min,
                           c_neg_overflow,
                           c_converted,
                           c_converted,
                           c_converted,
                           c_converted
                          ) ;

  test_rounding_conversion(SET_FNTPL_ARG(T),
                           SET_FNTPL_ARG(boost::numeric::Trunc<S>),
                           max,
                           c_converted,
                           c_converted,
                           c_converted,
                           c_converted,
                           c_pos_overflow
                          ) ;

  cout << "Testing 'RoundEven' Float2IntRounder policy\n";

  test_rounding_conversion(SET_FNTPL_ARG(T),
                           SET_FNTPL_ARG(boost::numeric::RoundEven<S>),
                           min,
                           c_neg_overflow,
                           c_converted,
                           c_converted,
                           c_converted,
                           c_converted
                          ) ;

  test_rounding_conversion(SET_FNTPL_ARG(T),
                           SET_FNTPL_ARG(boost::numeric::RoundEven<S>),
                           max,
                           c_converted,
                           c_converted,
                           c_converted,
                           c_pos_overflow,
                           c_pos_overflow
                          ) ;

  cout << "Testing 'Ceil' Float2IntRounder policy\n";

  test_rounding_conversion(SET_FNTPL_ARG(T),
                           SET_FNTPL_ARG(boost::numeric::Ceil<S>),
                           min,
                           c_neg_overflow,
                           c_converted,
                           c_converted,
                           c_converted,
                           c_converted
                          ) ;

  test_rounding_conversion(SET_FNTPL_ARG(T),
                           SET_FNTPL_ARG(boost::numeric::Ceil<S>),
                           max,
                           c_converted,
                           c_converted,
                           c_converted,
                           c_pos_overflow,
                           c_pos_overflow
                          ) ;

  cout << "Testing 'Floor' Float2IntRounder policy\n" ;

  test_rounding_conversion(SET_FNTPL_ARG(T),
                           SET_FNTPL_ARG(boost::numeric::Floor<S>),
                           min,
                           c_neg_overflow,
                           c_neg_overflow,
                           c_converted,
                           c_converted,
                           c_converted
                          ) ;

  test_rounding_conversion(SET_FNTPL_ARG(T),
                           SET_FNTPL_ARG(boost::numeric::Floor<S>),
                           max,
                           c_converted,
                           c_converted,
                           c_converted,
                           c_converted,
                           c_pos_overflow
                          ) ;

}

void test_round_even( double n, double x )
{
  double r = boost::numeric::RoundEven<double>::nearbyint(n);
  BOOST_CHECK( r == x ) ;
}

void test_round_even()
{
  cout << "Testing 'RoundEven' tie-breaking\n";

  double min = boost::numeric::bounds<double>::lowest();
  double max = boost::numeric::bounds<double>::highest();

#if !defined(BOOST_NO_STDC_NAMESPACE)
  using std::floor ;
  using std::ceil ;
#endif
  test_round_even(min, floor(min));
  test_round_even(max, ceil (max));
  test_round_even(2.0, 2.0);
  test_round_even(2.3, 2.0);
  test_round_even(2.5, 2.0);
  test_round_even(2.7, 3.0);
  test_round_even(3.0, 3.0);
  test_round_even(3.3, 3.0);
  test_round_even(3.5, 4.0);
  test_round_even(3.7, 4.0);
}

int double_to_int ( double n ) { return static_cast<int>(n) ; }

void test_converter_as_function_object()
{
  cout << "Testing converter as function object.\n";

  // Create a sample sequence of double values.
  std::vector<double> S ;
  for ( int i = 0 ; i < 10 ; ++ i )
    S.push_back( i * ( 18.0 / 19.0 ) );

  // Create a sequence of int values from 's' using the standard conversion.
  std::vector<int> W ;
  std::transform(S.begin(),S.end(),std::back_inserter(W),double_to_int);

  // Create a sequence of int values from s using a default numeric::converter
  std::vector<int> I ;
  std::transform(S.begin(),
                 S.end(),
                 std::back_inserter(I),
                 boost::numeric::converter<int,double>()
                ) ;

  // Match 'w' and 'i' which should be equal.
  bool double_to_int_OK = std::equal(W.begin(),W.end(),I.begin()) ;
  BOOST_CHECK_MESSAGE(double_to_int_OK, "converter (int,double) as function object");

  // Create a sequence of double values from s using a default numeric::converter (which should be the trivial conv).
  std::vector<double> D ;
  std::transform(S.begin(),
                 S.end(),
                 std::back_inserter(D),
                 boost::numeric::converter<double,double>()
                ) ;

  // Match 's' and 'd' which should be equal.
  bool double_to_double_OK = std::equal(S.begin(),S.end(),D.begin()) ;
  BOOST_CHECK_MESSAGE(double_to_double_OK, "converter (double,double) as function object");
}

#if BOOST_WORKAROUND(__IBMCPP__, <= 600 ) // VCAPP6
#  define UNOPTIMIZED
#else
#  define UNOPTIMIZED volatile
#endif

void test_optimizations()
{
  using namespace boost;
  using namespace numeric;

  float fv0 = 18.0f / 19.0f ;

  // This code deosn't produce any output.
  // It is intended to show the optimization of numeric::converter<> by manual inspection
  // of the generated code.
  // Each test shows first the equivalent hand-coded version.
  // The numeric_cast<> code should be the same if full compiler optimization/inlining is used.

  //---------------------------------
  // trivial conversion.
  //
    // equivalent code:
      UNOPTIMIZED float fv1a = fv0 ;

    float fv1b = numeric_cast<float>(fv0);
    unused_variable(fv1a);
    unused_variable(fv1b);
  //
  //---------------------------------

  //---------------------------------
  // nonsubranged conversion.
  //
    // equivalent code:
      UNOPTIMIZED double dv1a = static_cast<double>(fv0);

    double dv1b = numeric_cast<double>(fv0);
    unused_variable(dv1a);
    unused_variable(dv1b);
  //
  //---------------------------------

  //------------------------------------------------------
  // subranged conversion with both-sided range checking.
  //

    // equivalent code:

      {
        double const& s = dv1b ;
        // range checking
        range_check_result r =  s < static_cast<double>(bounds<float>::lowest())
                                  ? cNegOverflow : cInRange ;
        if ( r == cInRange )
        {
          r = s > static_cast<double>(bounds<float>::highest()) ? cPosOverflow : cInRange ;
        }
        if ( r == cNegOverflow )
          throw negative_overflow() ;
        else if ( r == cPosOverflow )
               throw positive_overflow() ;
        // conversion
        UNOPTIMIZED float fv2a = static_cast<float>(s);
        unused_variable(fv2a);
      }

    float fv2b = numeric_cast<float>(dv1b);
    unused_variable(fv2b);
  //
  //---------------------------------


  //---------------------------------
  // subranged rounding conversion
  //
    // equivalent code:

      {
        double const& s = dv1b ;
        // range checking
        range_check_result r = s <= static_cast<double>(bounds<int>::lowest()) - static_cast<double>(1.0)
                                 ? cNegOverflow : cInRange ;
        if ( r == cInRange )
        {
          r = s >= static_cast<double>(bounds<int>::highest()) + static_cast<double>(1.0)
                ? cPosOverflow : cInRange ;
        }
        if ( r == cNegOverflow )
          throw negative_overflow() ;
        else if ( r == cPosOverflow )
               throw positive_overflow() ;
        // rounding

#if !defined(BOOST_NO_STDC_NAMESPACE)
        using std::floor ;
#endif

        double s1 = floor(dv1b + 0.5);

        // conversion
        UNOPTIMIZED int iv1a = static_cast<int>(s1);
        unused_variable(iv1a);
      }

    int iv1b = numeric_cast<int>(dv1b);
    unused_variable(iv1b);
  //
  //---------------------------------
}

int test_main( int, char* argv[] )
{
  std::cout << std::setprecision( std::numeric_limits<long double>::digits10 ) ;

  test_conversions();
  test_overflow_handlers( SET_FNTPL_ARG(boost::int16_t), SET_FNTPL_ARG(boost::int32_t));
  test_round_style(SET_FNTPL_ARG(boost::int32_t), SET_FNTPL_ARG(double) ) ;
  test_round_even() ;
  test_converter_as_function_object();
  test_optimizations() ;

  return 0;
}
//---------------------------------------------------------------------------