test_mutant_relation.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // Boost.Bimap
  2. //
  3. // Copyright (c) 2006-2007 Matias Capeletto
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. // VC++ 8.0 warns on usage of certain Standard Library and API functions that
  9. // can be cause buffer overruns or other possible security issues if misused.
  10. // See https://web.archive.org/web/20071014014301/http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx
  11. // But the wording of the warning is misleading and unsettling, there are no
  12. // portable alternative functions, and VC++ 8.0's own libraries use the
  13. // functions in question. So turn off the warnings.
  14. #define _CRT_SECURE_NO_DEPRECATE
  15. #define _SCL_SECURE_NO_DEPRECATE
  16. #include <boost/config.hpp>
  17. // std
  18. #include <string>
  19. // Boost.Test
  20. #include <boost/test/minimal.hpp>
  21. // Boost.MPL
  22. #include <boost/mpl/assert.hpp>
  23. #include <boost/type_traits/is_same.hpp>
  24. // Boost.Bimap
  25. #include <boost/bimap/detail/test/check_metadata.hpp>
  26. #include <boost/bimap/tags/tagged.hpp>
  27. // Boost.Bimap.Relation
  28. #include <boost/bimap/relation/mutant_relation.hpp>
  29. #include <boost/bimap/relation/member_at.hpp>
  30. #include <boost/bimap/relation/support/get.hpp>
  31. #include <boost/bimap/relation/support/pair_by.hpp>
  32. #include <boost/bimap/relation/support/pair_type_by.hpp>
  33. #include <boost/bimap/relation/support/value_type_of.hpp>
  34. #include <boost/bimap/relation/support/member_with_tag.hpp>
  35. #include <boost/bimap/relation/support/is_tag_of_member_at.hpp>
  36. // Bimap Test Utilities
  37. #include "test_relation.hpp"
  38. BOOST_BIMAP_TEST_STATIC_FUNCTION( untagged_static_test )
  39. {
  40. using namespace boost::bimaps::relation::member_at;
  41. using namespace boost::bimaps::relation;
  42. using namespace boost::bimaps::tags;
  43. struct left_data {};
  44. struct right_data {};
  45. typedef mutant_relation< left_data, right_data > rel;
  46. BOOST_BIMAP_CHECK_METADATA(rel,left_value_type ,left_data);
  47. BOOST_BIMAP_CHECK_METADATA(rel,right_value_type,right_data);
  48. BOOST_BIMAP_CHECK_METADATA(rel,left_tag ,left );
  49. BOOST_BIMAP_CHECK_METADATA(rel,right_tag,right);
  50. typedef tagged<left_data ,left > desired_tagged_left_type;
  51. BOOST_BIMAP_CHECK_METADATA(rel,tagged_left_type,desired_tagged_left_type);
  52. typedef tagged<right_data,right> desired_tagged_right_type;
  53. BOOST_BIMAP_CHECK_METADATA(rel,tagged_right_type,desired_tagged_right_type);
  54. }
  55. BOOST_BIMAP_TEST_STATIC_FUNCTION( tagged_static_test)
  56. {
  57. using namespace boost::bimaps::relation::member_at;
  58. using namespace boost::bimaps::relation;
  59. using namespace boost::bimaps::tags;
  60. struct left_data {};
  61. struct right_data {};
  62. struct left_tag {};
  63. struct right_tag {};
  64. typedef mutant_relation<
  65. tagged<left_data,left_tag>, tagged<right_data,right_tag> > rel;
  66. BOOST_BIMAP_CHECK_METADATA(rel,left_value_type ,left_data);
  67. BOOST_BIMAP_CHECK_METADATA(rel,right_value_type,right_data);
  68. BOOST_BIMAP_CHECK_METADATA(rel,left_tag ,left_tag );
  69. BOOST_BIMAP_CHECK_METADATA(rel,right_tag,right_tag );
  70. typedef tagged<left_data ,left_tag > desired_tagged_left_type;
  71. BOOST_BIMAP_CHECK_METADATA(rel,tagged_left_type,desired_tagged_left_type);
  72. typedef tagged<right_data,right_tag> desired_tagged_right_type;
  73. BOOST_BIMAP_CHECK_METADATA(rel,tagged_right_type,desired_tagged_right_type);
  74. }
  75. struct mutant_relation_builder
  76. {
  77. template< class LeftType, class RightType >
  78. struct build
  79. {
  80. typedef boost::bimaps::relation::
  81. mutant_relation<LeftType,RightType,::boost::mpl::na,true> type;
  82. };
  83. };
  84. // Complex classes
  85. class cc1
  86. {
  87. public:
  88. cc1(int s = 0) : a(s+100), b(s+101) {}
  89. static int sd;
  90. int a;
  91. const int b;
  92. };
  93. bool operator==(const cc1 & da, const cc1 & db)
  94. {
  95. return da.a == db.a && da.b == db.b;
  96. }
  97. int cc1::sd = 102;
  98. class cc2_base
  99. {
  100. public:
  101. cc2_base(int s) : a(s+200) {}
  102. int a;
  103. };
  104. class cc2 : public cc2_base
  105. {
  106. public:
  107. cc2(int s = 0) : cc2_base(s), b(s+201) {}
  108. int b;
  109. };
  110. bool operator==(const cc2 & da, const cc2 & db)
  111. {
  112. return da.a == db.a && da.b == db.b;
  113. }
  114. class cc3_base
  115. {
  116. public:
  117. cc3_base(int s = 0) : a(s+300) {}
  118. const int a;
  119. };
  120. class cc3 : virtual public cc3_base
  121. {
  122. public:
  123. cc3(int s = 0) : cc3_base(s), b(s+301) {}
  124. int b;
  125. };
  126. bool operator==(const cc3 & da, const cc3 & db)
  127. {
  128. return da.a == db.a && da.b == db.b;
  129. }
  130. class cc4_base
  131. {
  132. public:
  133. cc4_base(int s) : a(s+400) {}
  134. virtual ~cc4_base() {}
  135. const int a;
  136. };
  137. class cc4 : public cc4_base
  138. {
  139. public:
  140. cc4(int s = 0) : cc4_base(s), b(s+401) {}
  141. int b;
  142. };
  143. bool operator==(const cc4 & da, const cc4 & db)
  144. {
  145. return da.a == db.a && da.b == db.b;
  146. }
  147. class cc5 : public cc1, public cc3, public cc4
  148. {
  149. public:
  150. cc5(int s = 0) : cc1(s), cc3(s), cc4(s) {}
  151. };
  152. bool operator==(const cc5 & da, const cc5 & db)
  153. {
  154. return da.cc1::a == db.cc1::a && da.cc1::b == db.cc1::b &&
  155. da.cc3::a == db.cc3::a && da.cc3::b == db.cc3::b &&
  156. da.cc4::a == db.cc4::a && da.cc4::b == db.cc4::b;
  157. }
  158. class cc6
  159. {
  160. public:
  161. cc6(int s = 0) : a(s+600), b(a) {}
  162. int a;
  163. int & b;
  164. };
  165. bool operator==(const cc6 & da, const cc6 & db)
  166. {
  167. return da.a == db.a && da.b == db.b;
  168. }
  169. void test_mutant_relation()
  170. {
  171. test_relation< mutant_relation_builder, char , double >( 'l', 2.5 );
  172. test_relation< mutant_relation_builder, double, char >( 2.5, 'r' );
  173. test_relation<mutant_relation_builder, int , int >( 1 , 2 );
  174. test_relation<mutant_relation_builder, std::string, int* >("left value",0);
  175. test_relation<mutant_relation_builder, cc1, cc2>(0,0);
  176. test_relation<mutant_relation_builder, cc2, cc3>(0,0);
  177. test_relation<mutant_relation_builder, cc3, cc4>(0,0);
  178. test_relation<mutant_relation_builder, cc4, cc5>(0,0);
  179. }
  180. int test_main( int, char* [] )
  181. {
  182. // Test metadata correctness with untagged relation version
  183. BOOST_BIMAP_CALL_TEST_STATIC_FUNCTION( tagged_static_test );
  184. // Test metadata correctness with tagged relation version
  185. BOOST_BIMAP_CALL_TEST_STATIC_FUNCTION( untagged_static_test );
  186. // Test basic
  187. test_mutant_relation();
  188. return 0;
  189. }