File: test_helpers3.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (115 lines) | stat: -rw-r--r-- 3,554 bytes parent folder | download | duplicates (11)
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
// Copyright (C) 2003, Fernando Luis Cacciola Carballal.
//
// Distributed under 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)
//


//
// NOTE: This file is intended to be used ONLY by the test files
//       from the Numeric Conversions Library
//

// The conversion test is performed using a class whose instances encapsulate
// a particular specific conversion defnied explicitely.
// A ConversionInstance object includes the source type, the target type,
// the source value and the expected result, including possible exceptions.
//

enum PostCondition { c_converted, c_overflow, c_neg_overflow, c_pos_overflow } ;

template<class Converter>
struct ConversionInstance
{
  typedef Converter converter ;

  typedef typename Converter::argument_type argument_type ;
  typedef typename Converter::result_type   result_type   ;

  typedef typename Converter::traits traits ;
  typedef typename traits::target_type target_type ;
  typedef typename traits::source_type source_type ;

  ConversionInstance ( result_type a_result, argument_type a_source, PostCondition a_post)
    :
    source(a_source),
    result(a_result),
    post(a_post)
  {}

  std::string to_string() const
    {
      return   std::string("converter<")
             + typeid(target_type).name()
             + std::string(",")
             + typeid(source_type).name()
             + std::string(">::convert(") ;
    }

  argument_type source ;
  result_type   result ;
  PostCondition post   ;
} ;

//
// Main conversion test point.
// Exercises a specific conversion described by 'conv'.
//
template<class Instance>
void test_conv_base( Instance const& conv )
{
  typedef typename Instance::argument_type argument_type ;
  typedef typename Instance::result_type   result_type   ;
  typedef typename Instance::converter     converter ;

  argument_type source = conv.source ;

  try
  {
    result_type result = converter::convert(source);

    if (BOOST_TEST_EQ(conv.post, c_converted))
    {
      BOOST_TEST_EQ(result, conv.result);
    }
  }
  catch ( boost::numeric::negative_overflow const& )
  { 
    BOOST_TEST_EQ(conv.post, c_neg_overflow);
  }
  catch ( boost::numeric::positive_overflow const& )
  {
    BOOST_TEST_EQ(conv.post, c_pos_overflow);
  }
  catch ( boost::numeric::bad_numeric_cast const& )
  {
    BOOST_TEST_EQ(conv.post, c_overflow);
  }
}


#define TEST_SUCCEEDING_CONVERSION(Conv,typeT,typeS,valueT,valueS) \
        test_conv_base( ConversionInstance< Conv >(valueT, valueS, c_converted ) )

#define TEST_POS_OVERFLOW_CONVERSION(Conv,typeT,typeS,valueS) \
        test_conv_base( ConversionInstance< Conv >( static_cast< typeT >(0), valueS, c_pos_overflow ) )

#define TEST_NEG_OVERFLOW_CONVERSION(Conv,typeT,typeS,valueS) \
        test_conv_base( ConversionInstance< Conv >( static_cast< typeT >(0), valueS, c_neg_overflow ) )

#define DEF_CONVERTER(T,S) boost::numeric::converter< T , S >

#define TEST_SUCCEEDING_CONVERSION_DEF(typeT,typeS,valueT,valueS) \
        TEST_SUCCEEDING_CONVERSION( DEF_CONVERTER(typeT,typeS), typeT, typeS, valueT, valueS )

#define TEST_POS_OVERFLOW_CONVERSION_DEF(typeT,typeS,valueS) \
        TEST_POS_OVERFLOW_CONVERSION( DEF_CONVERTER(typeT,typeS), typeT, typeS, valueS )

#define TEST_NEG_OVERFLOW_CONVERSION_DEF(typeT,typeS,valueS) \
        TEST_NEG_OVERFLOW_CONVERSION( DEF_CONVERTER(typeT,typeS), typeT, typeS, valueS )


//
///////////////////////////////////////////////////////////////////////////////////////////////