]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/numeric/conversion/test/test_helpers3.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / numeric / conversion / test / test_helpers3.cpp
1 // Copyright (C) 2003, Fernando Luis Cacciola Carballal.
2 //
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)
6 //
7
8
9 //
10 // NOTE: This file is intended to be used ONLY by the test files
11 // from the Numeric Conversions Library
12 //
13
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.
18 //
19
20 enum PostCondition { c_converted, c_overflow, c_neg_overflow, c_pos_overflow } ;
21
22 template<class Converter>
23 struct ConversionInstance
24 {
25 typedef Converter converter ;
26
27 typedef typename Converter::argument_type argument_type ;
28 typedef typename Converter::result_type result_type ;
29
30 typedef typename Converter::traits traits ;
31 typedef typename traits::target_type target_type ;
32 typedef typename traits::source_type source_type ;
33
34 ConversionInstance ( result_type a_result, argument_type a_source, PostCondition a_post)
35 :
36 source(a_source),
37 result(a_result),
38 post(a_post)
39 {}
40
41 std::string to_string() const
42 {
43 return std::string("converter<")
44 + typeid(target_type).name()
45 + std::string(",")
46 + typeid(source_type).name()
47 + std::string(">::convert(") ;
48 }
49
50 argument_type source ;
51 result_type result ;
52 PostCondition post ;
53 } ;
54
55 //
56 // Main conversion test point.
57 // Exercises a specific conversion described by 'conv'.
58 //
59 template<class Instance>
60 void test_conv_base( Instance const& conv )
61 {
62 typedef typename Instance::argument_type argument_type ;
63 typedef typename Instance::result_type result_type ;
64 typedef typename Instance::converter converter ;
65
66 argument_type source = conv.source ;
67
68 try
69 {
70 result_type result = converter::convert(source);
71
72 if (BOOST_TEST_EQ(conv.post, c_converted))
73 {
74 BOOST_TEST_EQ(result, conv.result);
75 }
76 }
77 catch ( boost::numeric::negative_overflow const& )
78 {
79 BOOST_TEST_EQ(conv.post, c_neg_overflow);
80 }
81 catch ( boost::numeric::positive_overflow const& )
82 {
83 BOOST_TEST_EQ(conv.post, c_pos_overflow);
84 }
85 catch ( boost::numeric::bad_numeric_cast const& )
86 {
87 BOOST_TEST_EQ(conv.post, c_overflow);
88 }
89 }
90
91
92 #define TEST_SUCCEEDING_CONVERSION(Conv,typeT,typeS,valueT,valueS) \
93 test_conv_base( ConversionInstance< Conv >(valueT, valueS, c_converted ) )
94
95 #define TEST_POS_OVERFLOW_CONVERSION(Conv,typeT,typeS,valueS) \
96 test_conv_base( ConversionInstance< Conv >( static_cast< typeT >(0), valueS, c_pos_overflow ) )
97
98 #define TEST_NEG_OVERFLOW_CONVERSION(Conv,typeT,typeS,valueS) \
99 test_conv_base( ConversionInstance< Conv >( static_cast< typeT >(0), valueS, c_neg_overflow ) )
100
101 #define DEF_CONVERTER(T,S) boost::numeric::converter< T , S >
102
103 #define TEST_SUCCEEDING_CONVERSION_DEF(typeT,typeS,valueT,valueS) \
104 TEST_SUCCEEDING_CONVERSION( DEF_CONVERTER(typeT,typeS), typeT, typeS, valueT, valueS )
105
106 #define TEST_POS_OVERFLOW_CONVERSION_DEF(typeT,typeS,valueS) \
107 TEST_POS_OVERFLOW_CONVERSION( DEF_CONVERTER(typeT,typeS), typeT, typeS, valueS )
108
109 #define TEST_NEG_OVERFLOW_CONVERSION_DEF(typeT,typeS,valueS) \
110 TEST_NEG_OVERFLOW_CONVERSION( DEF_CONVERTER(typeT,typeS), typeT, typeS, valueS )
111
112
113 //
114 ///////////////////////////////////////////////////////////////////////////////////////////////
115