1 // Copyright (C) 2003, Fernando Luis Cacciola Carballal.
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
10 // NOTE: This file is intended to be used ONLY by the test files
11 // from the Numeric Conversions Library
14 // The conversion test is performed using a class whose instances encapsulate
15 // a particular specific conversion defnied explicitely.
16 // A ConversionInstance object includes the source type, the target type,
17 // the source value and the expected result, including possible exceptions.
20 enum PostCondition
{ c_converted
, c_overflow
, c_neg_overflow
, c_pos_overflow
} ;
22 template<class Converter
>
23 struct ConversionInstance
25 typedef Converter converter
;
27 typedef typename
Converter::argument_type argument_type
;
28 typedef typename
Converter::result_type result_type
;
30 typedef typename
Converter::traits traits
;
31 typedef typename
traits::target_type target_type
;
32 typedef typename
traits::source_type source_type
;
34 ConversionInstance ( result_type a_result
, argument_type a_source
, PostCondition a_post
)
41 std::string
to_string() const
43 return std::string("converter<")
44 + typeid(target_type
).name()
46 + typeid(source_type
).name()
47 + std::string(">::convert(") ;
50 argument_type source
;
56 // Main conversion test point.
57 // Exercises a specific conversion described by 'conv'.
59 template<class Instance
>
60 void test_conv_base( Instance
const& conv
)
62 typedef typename
Instance::argument_type argument_type
;
63 typedef typename
Instance::result_type result_type
;
64 typedef typename
Instance::converter converter
;
66 argument_type source
= conv
.source
;
70 result_type result
= converter::convert(source
);
72 if (BOOST_TEST_EQ(conv
.post
, c_converted
))
74 BOOST_TEST_EQ(result
, conv
.result
);
77 catch ( boost::numeric::negative_overflow
const& )
79 BOOST_TEST_EQ(conv
.post
, c_neg_overflow
);
81 catch ( boost::numeric::positive_overflow
const& )
83 BOOST_TEST_EQ(conv
.post
, c_pos_overflow
);
85 catch ( boost::numeric::bad_numeric_cast
const& )
87 BOOST_TEST_EQ(conv
.post
, c_overflow
);
92 #define TEST_SUCCEEDING_CONVERSION(Conv,typeT,typeS,valueT,valueS) \
93 test_conv_base( ConversionInstance< Conv >(valueT, valueS, c_converted ) )
95 #define TEST_POS_OVERFLOW_CONVERSION(Conv,typeT,typeS,valueS) \
96 test_conv_base( ConversionInstance< Conv >( static_cast< typeT >(0), valueS, c_pos_overflow ) )
98 #define TEST_NEG_OVERFLOW_CONVERSION(Conv,typeT,typeS,valueS) \
99 test_conv_base( ConversionInstance< Conv >( static_cast< typeT >(0), valueS, c_neg_overflow ) )
101 #define DEF_CONVERTER(T,S) boost::numeric::converter< T , S >
103 #define TEST_SUCCEEDING_CONVERSION_DEF(typeT,typeS,valueT,valueS) \
104 TEST_SUCCEEDING_CONVERSION( DEF_CONVERTER(typeT,typeS), typeT, typeS, valueT, valueS )
106 #define TEST_POS_OVERFLOW_CONVERSION_DEF(typeT,typeS,valueS) \
107 TEST_POS_OVERFLOW_CONVERSION( DEF_CONVERTER(typeT,typeS), typeT, typeS, valueS )
109 #define TEST_NEG_OVERFLOW_CONVERSION_DEF(typeT,typeS,valueS) \
110 TEST_NEG_OVERFLOW_CONVERSION( DEF_CONVERTER(typeT,typeS), typeT, typeS, valueS )
114 ///////////////////////////////////////////////////////////////////////////////////////////////