test_construction.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright (c) 2012 Robert Ramey
  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. // test constructors
  7. #include <iostream>
  8. #include <boost/safe_numerics/safe_compare.hpp>
  9. #include <boost/safe_numerics/safe_integer.hpp>
  10. template<class T2, class T1>
  11. bool test_construction(T1 t1, const char *t2_name, const char *t1_name){
  12. using namespace boost::safe_numerics;
  13. std::cout
  14. << "testing constructions to " << t2_name << " from " << t1_name
  15. << std::endl;
  16. {
  17. /* (1) test construction of safe<T1> from T1 type */
  18. try{
  19. safe<T1> s1(t1);
  20. // should always arrive here!
  21. }
  22. catch(const std::exception &){
  23. // should never, ever arrive here
  24. std::cout
  25. << "erroneously detected error in construction "
  26. << "safe<" << t1_name << "> (" << t1_name << ")"
  27. << std::endl;
  28. try{
  29. safe<T1> s1(t1); // try again for debugging
  30. }
  31. catch(const std::exception &){}
  32. return false;
  33. }
  34. }
  35. {
  36. /* (2) test construction of safe<T2> from T1 type */
  37. T2 t2;
  38. try{
  39. t2 = safe<T2>(t1);
  40. if(! safe_compare::equal(t2 , t1)){
  41. std::cout
  42. << "failed to detect error in construction "
  43. << "safe<" << t2_name << "> (" << t1_name << ")"
  44. << std::endl;
  45. safe<T2> s2x(t1);
  46. return false;
  47. }
  48. }
  49. catch(const std::exception &){
  50. if(safe_compare::equal(t2, t1)){
  51. std::cout
  52. << "erroneously detected error in construction "
  53. << "safe<" << t2_name << "> (" << t1_name << ")"
  54. << std::endl;
  55. try{
  56. safe<T2> sx2(t1); // try again for debugging
  57. }
  58. catch(const std::exception &){}
  59. return false;
  60. }
  61. }
  62. }
  63. {
  64. /* (3) test construction of safe<T1> from safe<T1> type */
  65. safe<T1> s1x(t1);
  66. try{
  67. safe<T1> s1(s1x);
  68. if(! (s1 == s1x)){
  69. std::cout
  70. << "copy constructor altered value "
  71. << "safe<" << t1_name << "> (safe<" << t1_name << ">(" << t1 << "))"
  72. << std::endl;
  73. //safe<T1> s1(s1x);
  74. return false;
  75. }
  76. }
  77. catch(const std::exception &){
  78. // should never arrive here
  79. std::cout
  80. << "erroneously detected error in construction "
  81. << "safe<" << t1_name << "> (safe<" << t1_name << ">(" << t1 << "))"
  82. << std::endl;
  83. try{
  84. safe<T1> s1(t1);
  85. }
  86. catch(const std::exception &){}
  87. return false;
  88. }
  89. }
  90. {
  91. /* (4) test construction of safe<T2> from safe<T1> type */
  92. T2 t2;
  93. try{
  94. safe<T1> s1(t1);
  95. safe<T2> s2(s1);
  96. t2 = static_cast<T2>(s2);
  97. if(! (safe_compare::equal(t1, t2))){
  98. std::cout
  99. << "failed to detect error in construction "
  100. << "safe<" << t1_name << "> (safe<" << t2_name << ">(" << t1 << "))"
  101. << std::endl;
  102. safe<T2> s1x(t1);
  103. return false;
  104. }
  105. }
  106. catch(const std::exception &){
  107. if(safe_compare::equal(t1, t2)){
  108. std::cout
  109. << "erroneously detected error in construction "
  110. << "safe<" << t2_name << "> (safe<" << t1_name << ">(" << t1 << "))"
  111. << std::endl;
  112. try{
  113. safe<T2> s1(t1);
  114. }
  115. catch(const std::exception &){}
  116. return false;
  117. }
  118. }
  119. }
  120. return true;
  121. }
  122. #include <boost/mp11/algorithm.hpp>
  123. #include <boost/core/demangle.hpp>
  124. #include "test_values.hpp"
  125. using namespace boost::mp11;
  126. struct test {
  127. bool m_error;
  128. test(bool b = true) : m_error(b) {}
  129. operator bool(){
  130. return m_error;
  131. }
  132. template<typename T>
  133. void operator()(const T &){
  134. static_assert(mp_is_list<T>(), "must be a list of two types");
  135. using T1 = mp_first<T>; // first element is a type
  136. // second element is an integral constant
  137. using T2 = typename mp_second<T>::value_type; // get it's type
  138. constexpr T2 v2 = mp_second<T>(); // get it's value
  139. m_error &= test_construction<T1>(
  140. v2,
  141. boost::core::demangle(typeid(T1).name()).c_str(),
  142. boost::core::demangle(typeid(T2).name()).c_str()
  143. );
  144. }
  145. };
  146. template<typename T>
  147. using extract_value_type = typename T::value_type;
  148. using test_types = mp_unique<
  149. mp_transform<
  150. extract_value_type,
  151. test_values
  152. >
  153. >;
  154. int main(){
  155. test rval(true);
  156. mp_for_each<
  157. mp_product<mp_list, test_types, test_values>
  158. >(rval);
  159. std::cout << (rval ? "success!" : "failure") << std::endl;
  160. return ! rval ;
  161. }