test.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. // (C) Copyright John Maddock 2000.
  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. #ifndef TT_TEST_HPP
  6. #define TT_TEST_HPP
  7. #include <boost/config.hpp>
  8. #include <boost/detail/workaround.hpp>
  9. #if defined(_WIN32_WCE) && defined(BOOST_MSVC)
  10. #pragma warning(disable:4201)
  11. #endif
  12. #include <boost/noncopyable.hpp>
  13. #include <iostream>
  14. #include <typeinfo>
  15. #ifdef __BORLANDC__
  16. // we have to turn off these warnings otherwise we get swamped by the things:
  17. #pragma option -w-8008 -w-8066
  18. #endif
  19. #ifdef _MSC_VER
  20. // We have to turn off warnings that occur within the test suite:
  21. #pragma warning(disable:4127)
  22. #endif
  23. #ifdef BOOST_INTEL
  24. // remark #1418: external function definition with no prior declaration
  25. // remark #981: operands are evaluated in unspecified order
  26. #pragma warning(disable:1418 981)
  27. #endif
  28. #ifdef BOOST_INTEL
  29. // turn off warnings from this header:
  30. #pragma warning(push)
  31. #pragma warning(disable:444)
  32. #endif
  33. //
  34. // basic configuration:
  35. //
  36. #ifdef TEST_STD
  37. #define tt std::tr1
  38. //#define TYPE_TRAITS(x) <type_traits>
  39. //#define TYPE_COMPARE(x) <type_compare>
  40. //#define TYPE_TRANSFORM(x) <type_transform>
  41. #else
  42. #define tt boost
  43. //#define TYPE_TRAITS(x) BOOST_STRINGIZE(boost/type_traits/x.hpp)
  44. //#define TYPE_COMPARE(x) BOOST_STRINGIZE(boost/type_traits/x.hpp)
  45. //#define TYPE_TRANSFORM(x) BOOST_STRINGIZE(boost/type_traits/x.hpp)
  46. #endif
  47. //
  48. // replacements for Unit test macros:
  49. //
  50. int error_count = 0;
  51. #define BOOST_CHECK_MESSAGE(pred, message)\
  52. do{\
  53. if(!(pred))\
  54. {\
  55. std::cerr << __FILE__ << ":" << __LINE__ << ": " << message << std::endl;\
  56. ++error_count;\
  57. }\
  58. }while(0)
  59. #define BOOST_WARN_MESSAGE(pred, message)\
  60. do{\
  61. if(!(pred))\
  62. {\
  63. std::cerr << __FILE__ << ":" << __LINE__ << ": " << message << std::endl;\
  64. }\
  65. }while(0)
  66. #define BOOST_TEST_MESSAGE(message)\
  67. do{ std::cout << __FILE__ << ":" << __LINE__ << ": " << message << std::endl; }while(0)
  68. #define BOOST_CHECK(pred)\
  69. do{ \
  70. if(!(pred)){\
  71. std::cout << __FILE__ << ":" << __LINE__ << ": Error in " << BOOST_STRINGIZE(pred) << std::endl;\
  72. ++error_count;\
  73. } \
  74. }while(0)
  75. #define TT_TEST_BEGIN(trait_name)\
  76. int main(){
  77. #define TT_TEST_END return error_count; }
  78. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) && !BOOST_WORKAROUND(BOOST_GCC, < 40704)
  79. #define TRANSFORM_CHECK_ALIASES(name, from_suffix, to_suffix)\
  80. BOOST_CHECK_TYPE(bool to_suffix, name##_t<bool from_suffix>);\
  81. BOOST_CHECK_TYPE(char to_suffix, name##_t<char from_suffix>);\
  82. BOOST_CHECK_TYPE(wchar_t to_suffix, name##_t<wchar_t from_suffix>);\
  83. BOOST_CHECK_TYPE(signed char to_suffix, name##_t<signed char from_suffix>);\
  84. BOOST_CHECK_TYPE(unsigned char to_suffix, name##_t<unsigned char from_suffix>);\
  85. BOOST_CHECK_TYPE(short to_suffix, name##_t<short from_suffix>);\
  86. BOOST_CHECK_TYPE(unsigned short to_suffix, name##_t<unsigned short from_suffix>);\
  87. BOOST_CHECK_TYPE(int to_suffix, name##_t<int from_suffix>);\
  88. BOOST_CHECK_TYPE(unsigned int to_suffix, name##_t<unsigned int from_suffix>);\
  89. BOOST_CHECK_TYPE(long to_suffix, name##_t<long from_suffix>);\
  90. BOOST_CHECK_TYPE(unsigned long to_suffix, name##_t<unsigned long from_suffix>);\
  91. BOOST_CHECK_TYPE(float to_suffix, name##_t<float from_suffix>);\
  92. BOOST_CHECK_TYPE(long double to_suffix, name##_t<long double from_suffix>);\
  93. BOOST_CHECK_TYPE(double to_suffix, name##_t<double from_suffix>);\
  94. BOOST_CHECK_TYPE(UDT to_suffix, name##_t<UDT from_suffix>);\
  95. BOOST_CHECK_TYPE(enum1 to_suffix, name##_t<enum1 from_suffix>);
  96. #else
  97. #define TRANSFORM_CHECK_ALIASES(name, from_suffix, to_suffix) /**/
  98. #endif
  99. #define TRANSFORM_CHECK(name, from_suffix, to_suffix)\
  100. TRANSFORM_CHECK_ALIASES(name, from_suffix, to_suffix)\
  101. BOOST_CHECK_TYPE(bool to_suffix, name<bool from_suffix>::type);\
  102. BOOST_CHECK_TYPE(char to_suffix, name<char from_suffix>::type);\
  103. BOOST_CHECK_TYPE(wchar_t to_suffix, name<wchar_t from_suffix>::type);\
  104. BOOST_CHECK_TYPE(signed char to_suffix, name<signed char from_suffix>::type);\
  105. BOOST_CHECK_TYPE(unsigned char to_suffix, name<unsigned char from_suffix>::type);\
  106. BOOST_CHECK_TYPE(short to_suffix, name<short from_suffix>::type);\
  107. BOOST_CHECK_TYPE(unsigned short to_suffix, name<unsigned short from_suffix>::type);\
  108. BOOST_CHECK_TYPE(int to_suffix, name<int from_suffix>::type);\
  109. BOOST_CHECK_TYPE(unsigned int to_suffix, name<unsigned int from_suffix>::type);\
  110. BOOST_CHECK_TYPE(long to_suffix, name<long from_suffix>::type);\
  111. BOOST_CHECK_TYPE(unsigned long to_suffix, name<unsigned long from_suffix>::type);\
  112. BOOST_CHECK_TYPE(float to_suffix, name<float from_suffix>::type);\
  113. BOOST_CHECK_TYPE(long double to_suffix, name<long double from_suffix>::type);\
  114. BOOST_CHECK_TYPE(double to_suffix, name<double from_suffix>::type);\
  115. BOOST_CHECK_TYPE(UDT to_suffix, name<UDT from_suffix>::type);\
  116. BOOST_CHECK_TYPE(enum1 to_suffix, name<enum1 from_suffix>::type);
  117. #define BOOST_DUMMY_MACRO_PARAM /**/
  118. #define BOOST_DECL_TRANSFORM_TEST(name, type, from, to)\
  119. void name(){ TRANSFORM_CHECK(type, from, to) }
  120. #define BOOST_DECL_TRANSFORM_TEST3(name, type, from)\
  121. void name(){ TRANSFORM_CHECK(type, from, BOOST_DUMMY_MACRO_PARAM) }
  122. #define BOOST_DECL_TRANSFORM_TEST2(name, type, to)\
  123. void name(){ TRANSFORM_CHECK(type, BOOST_DUMMY_MACRO_PARAM, to) }
  124. #define BOOST_DECL_TRANSFORM_TEST0(name, type)\
  125. void name(){ TRANSFORM_CHECK(type, BOOST_DUMMY_MACRO_PARAM, BOOST_DUMMY_MACRO_PARAM) }
  126. //
  127. // VC++ emits an awful lot of warnings unless we define these:
  128. #ifdef BOOST_MSVC
  129. # pragma warning(disable:4800)
  130. #endif
  131. //
  132. // define some test types:
  133. //
  134. enum enum_UDT{ one, two, three };
  135. struct UDT
  136. {
  137. UDT();
  138. ~UDT();
  139. UDT(const UDT&);
  140. UDT& operator=(const UDT&);
  141. int i;
  142. void f1();
  143. int f2();
  144. int f3(int);
  145. int f4(int, float);
  146. #if __cpp_noexcept_function_type
  147. void f5()noexcept;
  148. int f6(int)noexcept(true);
  149. double f7()noexcept(false);
  150. #endif
  151. };
  152. typedef void(*f1)();
  153. typedef int(*f2)(int);
  154. typedef int(*f3)(int, bool);
  155. typedef void (UDT::*mf1)();
  156. typedef int (UDT::*mf2)();
  157. typedef int (UDT::*mf3)(int);
  158. typedef int (UDT::*mf4)(int, float);
  159. typedef int (UDT::*mp);
  160. typedef int (UDT::*cmf)(int) const;
  161. #if __cpp_noexcept_function_type
  162. typedef void (UDT::*mf5)()noexcept;
  163. typedef int (UDT::*mf6)(int)noexcept;
  164. typedef double (UDT::*mf7)()noexcept;
  165. #endif
  166. typedef int (UDT::*mf8)(...);
  167. // cv-qualifiers applied to reference types should have no effect
  168. // declare these here for later use with is_reference and remove_reference:
  169. # ifdef BOOST_MSVC
  170. # pragma warning(push)
  171. # pragma warning(disable: 4181)
  172. # elif defined(BOOST_INTEL)
  173. # pragma warning(push)
  174. # pragma warning(disable: 21)
  175. # elif defined(BOOST_CLANG)
  176. # pragma clang diagnostic push
  177. # pragma clang diagnostic ignored "-Wignored-qualifiers"
  178. # endif
  179. //
  180. // This is intentional:
  181. // r_type and cr_type should be the same type
  182. // but some compilers wrongly apply cv-qualifiers
  183. // to reference types (this may generate a warning
  184. // on some compilers):
  185. //
  186. typedef int& r_type;
  187. #ifndef BOOST_INTEL
  188. typedef const r_type cr_type;
  189. #else
  190. // recent Intel compilers generate a hard error on the above:
  191. typedef r_type cr_type;
  192. #endif
  193. # ifdef BOOST_MSVC
  194. # pragma warning(pop)
  195. # elif defined(BOOST_INTEL)
  196. # pragma warning(pop)
  197. # pragma warning(disable: 985) // identifier truncated in debug information
  198. # elif defined(BOOST_CLANG)
  199. # pragma clang diagnostic pop
  200. # endif
  201. struct POD_UDT { int x; };
  202. struct empty_UDT
  203. {
  204. empty_UDT();
  205. empty_UDT(const empty_UDT&);
  206. ~empty_UDT();
  207. empty_UDT& operator=(const empty_UDT&);
  208. bool operator==(const empty_UDT&)const;
  209. };
  210. struct empty_POD_UDT
  211. {
  212. bool operator==(const empty_POD_UDT&)const
  213. { return true; }
  214. };
  215. union union_UDT
  216. {
  217. int x;
  218. double y;
  219. ~union_UDT(){}
  220. };
  221. union POD_union_UDT
  222. {
  223. int x;
  224. double y;
  225. };
  226. union empty_union_UDT
  227. {
  228. ~empty_union_UDT(){}
  229. };
  230. union empty_POD_union_UDT{};
  231. struct nothrow_copy_UDT
  232. {
  233. nothrow_copy_UDT();
  234. nothrow_copy_UDT(const nothrow_copy_UDT&)throw();
  235. ~nothrow_copy_UDT(){}
  236. nothrow_copy_UDT& operator=(const nothrow_copy_UDT&);
  237. bool operator==(const nothrow_copy_UDT&)const
  238. { return true; }
  239. };
  240. struct nothrow_assign_UDT
  241. {
  242. nothrow_assign_UDT();
  243. nothrow_assign_UDT(const nothrow_assign_UDT&);
  244. ~nothrow_assign_UDT(){};
  245. nothrow_assign_UDT& operator=(const nothrow_assign_UDT&)throw(){ return *this; }
  246. bool operator==(const nothrow_assign_UDT&)const
  247. { return true; }
  248. };
  249. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  250. struct nothrow_move_UDT
  251. {
  252. nothrow_move_UDT();
  253. nothrow_move_UDT(nothrow_move_UDT&&) throw();
  254. nothrow_move_UDT& operator=(nothrow_move_UDT&&) throw();
  255. bool operator==(const nothrow_move_UDT&)const
  256. { return true; }
  257. };
  258. #endif
  259. struct nothrow_construct_UDT
  260. {
  261. nothrow_construct_UDT()throw();
  262. nothrow_construct_UDT(const nothrow_construct_UDT&);
  263. ~nothrow_construct_UDT(){};
  264. nothrow_construct_UDT& operator=(const nothrow_construct_UDT&){ return *this; }
  265. bool operator==(const nothrow_construct_UDT&)const
  266. { return true; }
  267. };
  268. class Base { };
  269. class Derived : public Base { };
  270. class Derived2 : public Base { };
  271. class MultiBase : public Derived, public Derived2 {};
  272. class PrivateBase : private Base {};
  273. class NonDerived { };
  274. enum enum1
  275. {
  276. one_,two_
  277. };
  278. enum enum2
  279. {
  280. three_,four_
  281. };
  282. #ifndef BOOST_NO_CXX11_SCOPED_ENUMS
  283. enum class scoped_enum { one, two, three };
  284. #endif
  285. struct VB
  286. {
  287. virtual ~VB(){};
  288. };
  289. struct VD : public VB
  290. {
  291. ~VD(){};
  292. };
  293. //
  294. // struct non_pointer:
  295. // used to verify that is_pointer does not return
  296. // true for class types that implement operator void*()
  297. //
  298. struct non_pointer
  299. {
  300. operator void*(){return this;}
  301. };
  302. struct non_int_pointer
  303. {
  304. int i;
  305. operator int*(){return &i;}
  306. };
  307. struct int_constructible
  308. {
  309. int_constructible(int);
  310. };
  311. struct int_convertible
  312. {
  313. operator int();
  314. };
  315. //
  316. // struct non_empty:
  317. // used to verify that is_empty does not emit
  318. // spurious warnings or errors.
  319. //
  320. struct non_empty : private boost::noncopyable
  321. {
  322. int i;
  323. };
  324. //
  325. // abstract base classes:
  326. struct test_abc1
  327. {
  328. test_abc1();
  329. virtual ~test_abc1();
  330. test_abc1(const test_abc1&);
  331. test_abc1& operator=(const test_abc1&);
  332. virtual void foo() = 0;
  333. virtual void foo2() = 0;
  334. };
  335. struct test_abc2
  336. {
  337. virtual ~test_abc2();
  338. virtual void foo() = 0;
  339. virtual void foo2() = 0;
  340. };
  341. struct test_abc3 : public test_abc1
  342. {
  343. virtual void foo3() = 0;
  344. };
  345. struct incomplete_type;
  346. struct polymorphic_base
  347. {
  348. virtual ~polymorphic_base();
  349. virtual void method();
  350. };
  351. struct polymorphic_derived1 : public polymorphic_base
  352. {
  353. };
  354. struct polymorphic_derived2 : public polymorphic_base
  355. {
  356. virtual void method();
  357. };
  358. #ifndef BOOST_NO_CXX11_FINAL
  359. struct final_UDT final
  360. {};
  361. struct polymorphic_derived_final final : public polymorphic_derived2
  362. {};
  363. #endif
  364. struct virtual_inherit1 : public virtual Base { };
  365. struct virtual_inherit2 : public virtual_inherit1 { };
  366. struct virtual_inherit3 : private virtual Base {};
  367. struct virtual_inherit4 : public virtual boost::noncopyable {};
  368. struct virtual_inherit5 : public virtual int_convertible {};
  369. struct virtual_inherit6 : public virtual Base { virtual ~virtual_inherit6()throw(); };
  370. typedef void foo0_t();
  371. typedef void foo1_t(int);
  372. typedef void foo2_t(int&, double);
  373. typedef void foo3_t(int&, bool, int, int);
  374. typedef void foo4_t(int, bool, int*, int[], int, int, int, int, int);
  375. struct trivial_except_construct
  376. {
  377. trivial_except_construct();
  378. int i;
  379. };
  380. struct trivial_except_destroy
  381. {
  382. ~trivial_except_destroy();
  383. int i;
  384. };
  385. struct trivial_except_copy
  386. {
  387. trivial_except_copy(trivial_except_copy const&);
  388. int i;
  389. };
  390. struct trivial_except_assign
  391. {
  392. trivial_except_assign& operator=(trivial_except_assign const&);
  393. int i;
  394. };
  395. template <class T>
  396. struct wrap
  397. {
  398. T t;
  399. int j;
  400. protected:
  401. wrap();
  402. wrap(const wrap&);
  403. wrap& operator=(const wrap&);
  404. };
  405. #ifdef BOOST_INTEL
  406. #pragma warning(pop)
  407. #endif
  408. #endif