]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/tr1/test/run_complex_overloads.cpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / tr1 / test / run_complex_overloads.cpp
CommitLineData
7c673cae
FG
1// (C) Copyright John Maddock 2005.
2// Use, modification and distribution are subject to the
3// Boost Software License, Version 1.0. (See accompanying file
4// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6#ifdef TEST_STD_HEADERS
7#include <complex>
8#else
9#include <boost/tr1/complex.hpp>
10#endif
11
12#define BOOST_TEST_MODULE run_complex_overloads
13#include <boost/test/included/unit_test.hpp>
14#include <boost/type_traits/is_same.hpp>
15#include <boost/type_traits/is_floating_point.hpp>
16#include <boost/mpl/if.hpp>
17#include <boost/static_assert.hpp>
18
19#include <iostream>
20#include <iomanip>
21
22#ifndef VERBOSE
23#undef BOOST_TEST_MESSAGE
24#define BOOST_TEST_MESSAGE(x)
25#endif
26
27//
28// This test verifies that the complex-algorithms that are
29// overloaded for scalar types produce the same result as casting
30// the argument to a complex type, and calling the complex version
31// of the algorithm. Relative errors must be within 2e in order for
32// the tests to pass.
33//
34
35template <class T, class U>
36void do_check(const T& t, const U& u)
37{
38 static const T two = 2;
39 static const T factor = std::pow(two, 1-std::numeric_limits<T>::digits) * 200;
40 BOOST_STATIC_ASSERT((::boost::is_same<T,U>::value));
41 BOOST_CHECK_CLOSE(t, u, factor);
42}
43
44template <class T, class U>
45void do_check(const std::complex<T>& t, const std::complex<U>& u)
46{
47 BOOST_STATIC_ASSERT((::boost::is_same<T,U>::value));
48 do_check(t.real(), u.real());
49 do_check(t.imag(), u.imag());
50}
51
52template <class T>
53void check_val(const T& val)
54{
55 typedef typename boost::mpl::if_< boost::is_floating_point<T>, T, double>::type real_type;
56 typedef std::complex<real_type> complex_type;
57
58 real_type rval = static_cast<real_type>(val);
59 complex_type cval = rval;
60
61 if(val)
62 {
63 std::cout << " Testing std::arg.\n";
64 do_check(std::arg(cval), std::arg(rval));
65 do_check(std::arg(cval), std::arg(val));
66 }
67 std::cout << " Testing std::norm.\n";
68 do_check(std::norm(cval), std::norm(rval));
69 do_check(std::norm(cval), std::norm(val));
70 std::cout << " Testing std::conj.\n";
71 do_check(std::conj(cval), std::conj(rval));
72 do_check(std::conj(cval), std::conj(val));
73 std::cout << " Testing std::polar.\n";
74 do_check(std::polar(val), std::polar(rval));
75 do_check(std::polar(val, 0), std::polar(rval, 0));
76 do_check(std::polar(val, val), std::polar(rval, rval));
77 do_check(std::polar(val, rval), std::polar(rval, val));
78 std::cout << " Testing std::real.\n";
79 do_check(std::real(cval), std::real(rval));
80 do_check(std::real(cval), std::real(val));
81 std::cout << " Testing std::imaj.\n";
82 do_check(std::imag(cval), std::imag(rval));
83 do_check(std::imag(cval), std::imag(val));
84 if(val && !boost::is_floating_point<T>::value)
85 {
86 //
87 // Note that these tests are not run for floating point
88 // types as that would only test the std lib vendor's
89 // implementation of pow, not our additional overloads.
90 // Note that some std lib's do fail these tests, gcc on
91 // Darwin is a particularly bad example !
92 //
93 std::cout << " Testing std::pow.\n";
94 do_check(std::pow(cval, cval), std::pow(cval, val));
95 do_check(std::pow(cval, cval), std::pow(cval, rval));
96 do_check(std::pow(cval, cval), std::pow(val, cval));
97 do_check(std::pow(cval, cval), std::pow(rval, cval));
98 }
99}
100
101void do_check(double i)
102{
103 std::cout << "Checking type double with value " << i << std::endl;
104 check_val(i);
105 std::cout << "Checking type float with value " << i << std::endl;
106 check_val(static_cast<float>(i));
107 std::cout << "Checking type long double with value " << i << std::endl;
108 check_val(static_cast<long double>(i));
109}
110
111void do_check(int i)
112{
113 std::cout << "Checking type char with value " << i << std::endl;
114 check_val(static_cast<char>(i));
115 std::cout << "Checking type unsigned char with value " << i << std::endl;
116 check_val(static_cast<unsigned char>(i));
117 std::cout << "Checking type signed char with value " << i << std::endl;
118 check_val(static_cast<signed char>(i));
119 std::cout << "Checking type short with value " << i << std::endl;
120 check_val(static_cast<short>(i));
121 std::cout << "Checking type unsigned short with value " << i << std::endl;
122 check_val(static_cast<unsigned short>(i));
123 std::cout << "Checking type int with value " << i << std::endl;
124 check_val(static_cast<int>(i));
125 std::cout << "Checking type unsigned int with value " << i << std::endl;
126 check_val(static_cast<unsigned int>(i));
127 std::cout << "Checking type long with value " << i << std::endl;
128 check_val(static_cast<long>(i));
129 std::cout << "Checking type unsigned long with value " << i << std::endl;
130 check_val(static_cast<unsigned long>(i));
131#ifdef BOOST_HAS_LONG_LONG
132 std::cout << "Checking type long long with value " << i << std::endl;
133 check_val(static_cast<long long>(i));
134 std::cout << "Checking type unsigned long long with value " << i << std::endl;
135 check_val(static_cast<unsigned long long>(i));
136#elif defined(BOOST_HAS_MS_INT64)
137 std::cout << "Checking type __int64 with value " << i << std::endl;
138 check_val(static_cast<__int64>(i));
139 std::cout << "Checking type unsigned __int64 with value " << i << std::endl;
140 check_val(static_cast<unsigned __int64>(i));
141#endif
142 do_check(static_cast<double>(i));
143}
144
145BOOST_AUTO_TEST_CASE( test_main )
146{
147 do_check(0);
148 do_check(0.0);
149 do_check(1);
150 do_check(1.5);
151 do_check(0.5);
152}
153