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