test_compile_result.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright John Maddock 2007.
  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. // Note this header must NOT include any other headers, for its
  7. // use to be meaningful (because we use it in tests designed to
  8. // detect missing includes).
  9. //
  10. static const float f = 0;
  11. static const double d = 0;
  12. static const long double l = 0;
  13. static const unsigned u = 0;
  14. static const int i = 0;
  15. //template <class T>
  16. //inline void check_result_imp(T, T){}
  17. inline void check_result_imp(float, float){}
  18. inline void check_result_imp(double, double){}
  19. inline void check_result_imp(long double, long double){}
  20. inline void check_result_imp(int, int){}
  21. inline void check_result_imp(long, long){}
  22. #ifdef BOOST_HAS_LONG_LONG
  23. inline void check_result_imp(boost::long_long_type, boost::long_long_type){}
  24. #endif
  25. inline void check_result_imp(bool, bool){}
  26. //
  27. // If the compiler warns about unused typedefs then enable this:
  28. //
  29. #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 7)))
  30. # define BOOST_MATH_ASSERT_UNUSED_ATTRIBUTE __attribute__((unused))
  31. #else
  32. # define BOOST_MATH_ASSERT_UNUSED_ATTRIBUTE
  33. #endif
  34. template <class T, class U>
  35. struct local_is_same
  36. {
  37. enum{ value = false };
  38. };
  39. template <class T>
  40. struct local_is_same<T, T>
  41. {
  42. enum{ value = true };
  43. };
  44. template <class T1, class T2>
  45. inline void check_result_imp(T1, T2)
  46. {
  47. // This is a static assertion that should always fail to compile...
  48. #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)))
  49. #pragma GCC diagnostic push
  50. #pragma GCC diagnostic ignored "-Wunused-local-typedefs"
  51. #endif
  52. typedef BOOST_MATH_ASSERT_UNUSED_ATTRIBUTE int static_assertion[local_is_same<T1, T2>::value ? 1 : 0];
  53. #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)))
  54. #pragma GCC diagnostic pop
  55. #endif
  56. }
  57. template <class T1, class T2>
  58. inline void check_result(T2)
  59. {
  60. T1 a = T1();
  61. T2 b = T2();
  62. return check_result_imp(a, b);
  63. }
  64. union max_align_type
  65. {
  66. char c;
  67. short s;
  68. int i;
  69. long l;
  70. double d;
  71. long double ld;
  72. #ifdef BOOST_HAS_LONG_LONG
  73. long long ll;
  74. #endif
  75. };
  76. template <class Distribution>
  77. struct DistributionConcept
  78. {
  79. static void constraints()
  80. {
  81. typedef typename Distribution::value_type value_type;
  82. const Distribution& dist = DistributionConcept<Distribution>::get_object();
  83. value_type x = 0;
  84. // The result values are ignored in all these checks.
  85. check_result<value_type>(cdf(dist, x));
  86. check_result<value_type>(cdf(complement(dist, x)));
  87. check_result<value_type>(pdf(dist, x));
  88. check_result<value_type>(quantile(dist, x));
  89. check_result<value_type>(quantile(complement(dist, x)));
  90. check_result<value_type>(mean(dist));
  91. check_result<value_type>(mode(dist));
  92. check_result<value_type>(standard_deviation(dist));
  93. check_result<value_type>(variance(dist));
  94. check_result<value_type>(hazard(dist, x));
  95. check_result<value_type>(chf(dist, x));
  96. check_result<value_type>(coefficient_of_variation(dist));
  97. check_result<value_type>(skewness(dist));
  98. check_result<value_type>(kurtosis(dist));
  99. check_result<value_type>(kurtosis_excess(dist));
  100. check_result<value_type>(median(dist));
  101. //
  102. // we can't actually test that at std::pair is returned from these
  103. // because that would mean including some std lib headers....
  104. //
  105. range(dist);
  106. support(dist);
  107. check_result<value_type>(cdf(dist, f));
  108. check_result<value_type>(cdf(complement(dist, f)));
  109. check_result<value_type>(pdf(dist, f));
  110. check_result<value_type>(quantile(dist, f));
  111. check_result<value_type>(quantile(complement(dist, f)));
  112. check_result<value_type>(hazard(dist, f));
  113. check_result<value_type>(chf(dist, f));
  114. check_result<value_type>(cdf(dist, d));
  115. check_result<value_type>(cdf(complement(dist, d)));
  116. check_result<value_type>(pdf(dist, d));
  117. check_result<value_type>(quantile(dist, d));
  118. check_result<value_type>(quantile(complement(dist, d)));
  119. check_result<value_type>(hazard(dist, d));
  120. check_result<value_type>(chf(dist, d));
  121. check_result<value_type>(cdf(dist, l));
  122. check_result<value_type>(cdf(complement(dist, l)));
  123. check_result<value_type>(pdf(dist, l));
  124. check_result<value_type>(quantile(dist, l));
  125. check_result<value_type>(quantile(complement(dist, l)));
  126. check_result<value_type>(hazard(dist, l));
  127. check_result<value_type>(chf(dist, l));
  128. check_result<value_type>(cdf(dist, i));
  129. check_result<value_type>(cdf(complement(dist, i)));
  130. check_result<value_type>(pdf(dist, i));
  131. check_result<value_type>(quantile(dist, i));
  132. check_result<value_type>(quantile(complement(dist, i)));
  133. check_result<value_type>(hazard(dist, i));
  134. check_result<value_type>(chf(dist, i));
  135. unsigned long li = 1;
  136. check_result<value_type>(cdf(dist, li));
  137. check_result<value_type>(cdf(complement(dist, li)));
  138. check_result<value_type>(pdf(dist, li));
  139. check_result<value_type>(quantile(dist, li));
  140. check_result<value_type>(quantile(complement(dist, li)));
  141. check_result<value_type>(hazard(dist, li));
  142. check_result<value_type>(chf(dist, li));
  143. }
  144. private:
  145. static void* storage()
  146. {
  147. static max_align_type storage[sizeof(Distribution)];
  148. return storage;
  149. }
  150. static Distribution* get_object_p()
  151. {
  152. return static_cast<Distribution*>(storage());
  153. }
  154. static Distribution& get_object()
  155. {
  156. // will never get called:
  157. return *get_object_p();
  158. }
  159. }; // struct DistributionConcept
  160. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  161. #define TEST_DIST_FUNC(dist)\
  162. DistributionConcept< boost::math::dist##_distribution<float> >::constraints();\
  163. DistributionConcept< boost::math::dist##_distribution<double> >::constraints();\
  164. DistributionConcept< boost::math::dist##_distribution<long double> >::constraints();
  165. #else
  166. #define TEST_DIST_FUNC(dist)\
  167. DistributionConcept< boost::math::dist##_distribution<float> >::constraints();\
  168. DistributionConcept< boost::math::dist##_distribution<double> >::constraints();
  169. #endif