1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
/* Boost.MultiIndex test for key extractors.
*
* Copyright 2003-2006 Joaqun M Lpez Muoz.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*
* See http://www.boost.org/libs/multi_index for library home page.
*/
#include "test_key_extractors.hpp"
#include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
#include "pre_multi_index.hpp"
#include <boost/multi_index/key_extractors.hpp>
#include <boost/ref.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/test/test_tools.hpp>
#include <list>
#include <memory>
using namespace boost::multi_index;
using namespace boost::tuples;
struct test_class
{
int int_member;
const int int_cmember;
bool bool_mem_fun_const()const{return true;}
bool bool_mem_fun(){return false;}
test_class(int i=0):int_member(i),int_cmember(i){}
test_class(int i,int j):int_member(i),int_cmember(j){}
test_class& operator=(const test_class& x)
{
int_member=x.int_member;
return *this;
}
bool operator<(const test_class& x)const
{
if(int_member<x.int_member)return true;
if(x.int_member<int_member)return false;
return int_cmember<x.int_cmember;
}
bool operator==(const test_class& x)const
{
return int_member==x.int_member&&int_cmember==x.int_cmember;
}
};
struct test_derived_class:test_class
{
test_derived_class(int i=0):test_class(i){}
test_derived_class(int i,int j):test_class(i,j){}
};
BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(test_class)
BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(test_derived_class)
typedef identity<test_class> idn;
typedef identity<const test_class> cidn;
typedef BOOST_MULTI_INDEX_MEMBER(test_class,int,int_member) key_m;
typedef BOOST_MULTI_INDEX_MEMBER(test_class,const int,int_member) ckey_m;
typedef BOOST_MULTI_INDEX_MEMBER(test_class,const int,int_cmember) key_cm;
typedef BOOST_MULTI_INDEX_CONST_MEM_FUN(
test_class,bool,bool_mem_fun_const) key_cmf;
typedef BOOST_MULTI_INDEX_MEM_FUN(test_class,bool,bool_mem_fun) key_mf;
typedef composite_key<
test_class,
idn,
key_m,
key_cm,
key_cmf
> compkey;
typedef composite_key<
test_class,
cidn,
ckey_m
> ccompkey;
typedef composite_key<
boost::reference_wrapper<test_class>,
key_mf
> ccompw_key;
#if !defined(BOOST_NO_SFINAE)
/* testcases for problems with non-copyable classes reported at
* http://lists.boost.org/Archives/boost/2006/04/103065.php
*/
struct test_nc_class
{
int int_member;
const int int_cmember;
bool bool_mem_fun_const()const{return true;}
bool bool_mem_fun(){return false;}
test_nc_class(int i=0):int_member(i),int_cmember(i){}
test_nc_class(int i,int j):int_member(i),int_cmember(j){}
bool operator==(const test_nc_class& x)const
{
return int_member==x.int_member&&int_cmember==x.int_cmember;
}
private:
test_nc_class(const test_nc_class&);
};
struct test_nc_derived_class:test_nc_class
{
test_nc_derived_class(int i=0):test_nc_class(i){}
test_nc_derived_class(int i,int j):test_nc_class(i,j){}
};
BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(test_nc_class)
BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(test_nc_derived_class)
typedef identity<test_nc_class> nc_idn;
typedef identity<const test_nc_class> nc_cidn;
typedef BOOST_MULTI_INDEX_MEMBER(test_nc_class,int,int_member) nc_key_m;
typedef BOOST_MULTI_INDEX_MEMBER(
test_nc_class,const int,int_member) nc_ckey_m;
typedef BOOST_MULTI_INDEX_CONST_MEM_FUN(
test_nc_class,bool,bool_mem_fun_const) nc_key_cmf;
typedef BOOST_MULTI_INDEX_MEM_FUN(
test_nc_class,bool,bool_mem_fun) nc_key_mf;
typedef composite_key<
test_nc_class,
nc_idn,
nc_key_m,
nc_ckey_m,
nc_key_cmf
> nc_compkey;
#endif
void test_key_extractors()
{
idn id;
cidn cid;
key_m k_m;
ckey_m ck_m;
key_cm k_cm;
key_cmf k_cmf;
key_mf k_mf;
compkey cmpk;
ccompkey ccmpk;
ccompw_key ccmpk_w;
test_derived_class td(-1,0);
const test_derived_class& ctdr=td;
test_class& tr=td;
const test_class& ctr=tr;
test_derived_class* tdp=&td;
const test_derived_class* ctdp=&ctdr;
test_class* tp=&tr;
const test_class* ctp=&tr;
test_class** tpp=&tp;
const test_class** ctpp=&ctp;
std::auto_ptr<test_class*> tap(new test_class*(tp));
std::auto_ptr<const test_class*> ctap(new const test_class*(ctp));
boost::reference_wrapper<test_class> tw(tr);
boost::reference_wrapper<const test_class> ctw(tr);
id(tr).int_member=0;
BOOST_CHECK(id(tr).int_member==0);
BOOST_CHECK(cid(tr).int_member==0);
BOOST_CHECK(k_m(tr)==0);
BOOST_CHECK(ck_m(tr)==0);
BOOST_CHECK(cmpk(tr)==make_tuple(test_class(0,0),0,0,true));
BOOST_CHECK(ccmpk(tr)==make_tuple(test_class(0,0),0));
BOOST_CHECK(id(ctr).int_member==0);
BOOST_CHECK(cid(ctr).int_member==0);
BOOST_CHECK(k_m(ctr)==0);
BOOST_CHECK(ck_m(ctr)==0);
BOOST_CHECK(cmpk(ctr)==make_tuple(test_class(0,0),0,0,true));
BOOST_CHECK(ccmpk(ctr)==make_tuple(test_class(0,0),0));
#if !defined(BOOST_NO_SFINAE)
BOOST_CHECK(id(td).int_member==0);
BOOST_CHECK(cid(td).int_member==0);
BOOST_CHECK(k_m(td)==0);
BOOST_CHECK(ck_m(td)==0);
BOOST_CHECK(cmpk(td)==make_tuple(test_class(0,0),0,0,true));
BOOST_CHECK(ccmpk(td)==make_tuple(test_class(0,0),0));
BOOST_CHECK(id(ctdr).int_member==0);
BOOST_CHECK(cid(ctdr).int_member==0);
BOOST_CHECK(k_m(ctdr)==0);
BOOST_CHECK(ck_m(ctdr)==0);
BOOST_CHECK(cmpk(ctdr)==make_tuple(test_class(0,0),0,0,true));
BOOST_CHECK(ccmpk(ctdr)==make_tuple(test_class(0,0),0));
#endif
k_m(tr)=1;
BOOST_CHECK(id(tp).int_member==1);
BOOST_CHECK(cid(tp).int_member==1);
BOOST_CHECK(k_m(tp)==1);
BOOST_CHECK(ck_m(tp)==1);
BOOST_CHECK(cmpk(tp)==make_tuple(test_class(1,0),1,0,true));
BOOST_CHECK(ccmpk(tp)==make_tuple(test_class(1,0),1));
BOOST_CHECK(cid(ctp).int_member==1);
BOOST_CHECK(ck_m(ctp)==1);
BOOST_CHECK(cmpk(ctp)==make_tuple(test_class(1,0),1,0,true));
BOOST_CHECK(ccmpk(ctp)==make_tuple(test_class(1,0),1));
#if !defined(BOOST_NO_SFINAE)
BOOST_CHECK(id(tdp).int_member==1);
BOOST_CHECK(cid(tdp).int_member==1);
BOOST_CHECK(k_m(tdp)==1);
BOOST_CHECK(ck_m(tdp)==1);
BOOST_CHECK(cmpk(tdp)==make_tuple(test_class(1,0),1,0,true));
BOOST_CHECK(ccmpk(tdp)==make_tuple(test_class(1,0),1));
BOOST_CHECK(cid(ctdp).int_member==1);
BOOST_CHECK(ck_m(ctdp)==1);
BOOST_CHECK(cmpk(ctdp)==make_tuple(test_class(1,0),1,0,true));
BOOST_CHECK(ccmpk(ctdp)==make_tuple(test_class(1,0),1));
#endif
k_m(tp)=2;
BOOST_CHECK(id(tpp).int_member==2);
BOOST_CHECK(cid(tpp).int_member==2);
BOOST_CHECK(k_m(tpp)==2);
BOOST_CHECK(ck_m(tpp)==2);
BOOST_CHECK(cmpk(tpp)==make_tuple(test_class(2,0),2,0,true));
BOOST_CHECK(ccmpk(tpp)==make_tuple(test_class(2,0),2));
BOOST_CHECK(cid(ctpp).int_member==2);
BOOST_CHECK(ck_m(ctpp)==2);
BOOST_CHECK(cmpk(ctpp)==make_tuple(test_class(2,0),2,0,true));
BOOST_CHECK(ccmpk(ctpp)==make_tuple(test_class(2,0),2));
k_m(tpp)=3;
BOOST_CHECK(id(tap).int_member==3);
BOOST_CHECK(cid(tap).int_member==3);
BOOST_CHECK(k_m(tap)==3);
BOOST_CHECK(ck_m(tap)==3);
BOOST_CHECK(cmpk(tap)==make_tuple(test_class(3,0),3,0,true));
BOOST_CHECK(ccmpk(tap)==make_tuple(test_class(3,0),3));
BOOST_CHECK(cid(ctap).int_member==3);
BOOST_CHECK(ck_m(ctap)==3);
BOOST_CHECK(cmpk(ctap)==make_tuple(test_class(3,0),3,0,true));
BOOST_CHECK(ccmpk(ctap)==make_tuple(test_class(3,0),3));
k_m(tap)=4;
BOOST_CHECK(id(tw).int_member==4);
BOOST_CHECK(cid(tw).int_member==4);
BOOST_CHECK(k_m(tw)==4);
BOOST_CHECK(ck_m(tw)==4);
BOOST_CHECK(cmpk(tw)==make_tuple(test_class(4,0),4,0,true));
BOOST_CHECK(ccmpk(tw)==make_tuple(test_class(4,0),4));
k_m(tw)=5;
BOOST_CHECK(id(ctw).int_member==5);
BOOST_CHECK(cid(ctw).int_member==5);
BOOST_CHECK(k_m(ctw)==5);
BOOST_CHECK(ck_m(ctw)==5);
BOOST_CHECK(cmpk(ctw)==make_tuple(test_class(5,0),5,0,true));
BOOST_CHECK(ccmpk(ctw)==make_tuple(test_class(5,0),5));
BOOST_CHECK(k_cm(tr)==0);
BOOST_CHECK(k_cm(ctr)==0);
#if !defined(BOOST_NO_SFINAE)
BOOST_CHECK(k_cm(td)==0);
BOOST_CHECK(k_cm(ctdr)==0);
#endif
BOOST_CHECK(k_cm(tp)==0);
BOOST_CHECK(k_cm(ctp)==0);
#if !defined(BOOST_NO_SFINAE)
BOOST_CHECK(k_cm(tdp)==0);
BOOST_CHECK(k_cm(ctdp)==0);
#endif
BOOST_CHECK(k_cm(tpp)==0);
BOOST_CHECK(k_cm(ctpp)==0);
BOOST_CHECK(k_cm(tap)==0);
BOOST_CHECK(k_cm(ctap)==0);
BOOST_CHECK(k_cm(tw)==0);
BOOST_CHECK(k_cm(ctw)==0);
BOOST_CHECK(k_cmf(tr));
BOOST_CHECK(k_cmf(ctr));
#if !defined(BOOST_NO_SFINAE)
BOOST_CHECK(k_cmf(td));
BOOST_CHECK(k_cmf(ctdr));
#endif
BOOST_CHECK(k_cmf(tp));
BOOST_CHECK(k_cmf(ctp));
#if !defined(BOOST_NO_SFINAE)
BOOST_CHECK(k_cmf(tdp));
BOOST_CHECK(k_cmf(ctdp));
#endif
BOOST_CHECK(k_cmf(tpp));
BOOST_CHECK(k_cmf(ctpp));
BOOST_CHECK(k_cmf(tap));
BOOST_CHECK(k_cmf(ctap));
BOOST_CHECK(k_cmf(tw));
BOOST_CHECK(k_cmf(ctw));
BOOST_CHECK(!k_mf(tr));
#if !defined(BOOST_NO_SFINAE)
BOOST_CHECK(!k_mf(td));
#endif
BOOST_CHECK(!k_mf(tp));
#if !defined(BOOST_NO_SFINAE)
BOOST_CHECK(!k_mf(tdp));
#endif
BOOST_CHECK(!k_mf(tpp));
BOOST_CHECK(!k_mf(tap));
BOOST_CHECK(!k_mf(tw));
BOOST_CHECK(ccmpk_w(tw)==make_tuple(false));
#if !defined(BOOST_NO_SFINAE)
/* testcases for problems with non-copyable classes reported at
* http://lists.boost.org/Archives/boost/2006/04/103065.php
*/
nc_idn nc_id;
nc_cidn nc_cid;
nc_key_m nc_k_m;
nc_ckey_m nc_ck_m;
nc_key_cmf nc_k_cmf;
nc_key_mf nc_k_mf;
nc_compkey nc_cmpk;
test_nc_derived_class nc_td(-1,0);
nc_id(nc_td).int_member=0;
BOOST_CHECK(nc_id(nc_td).int_member==0);
BOOST_CHECK(nc_cid(nc_td).int_member==0);
nc_k_m(&nc_td)=1;
BOOST_CHECK(nc_k_m(&nc_td)==1);
BOOST_CHECK(nc_ck_m(&nc_td)==1);
BOOST_CHECK(nc_k_cmf(nc_td));
BOOST_CHECK(!nc_k_mf(nc_td));
test_nc_class nc_t(1,0);
BOOST_CHECK(nc_cmpk(nc_td)==make_tuple(boost::cref(nc_t),1,1,true));
#endif
std::list<test_class> tl;
for(int i=0;i<20;++i)tl.push_back(test_class(i));
int j=0;
for(std::list<test_class>::iterator it=tl.begin();it!=tl.end();++it){
BOOST_CHECK(k_m(it)==j);
BOOST_CHECK(k_cm(it)==j);
BOOST_CHECK(k_cmf(it));
BOOST_CHECK(!k_mf(it));
BOOST_CHECK(cmpk(it)==make_tuple(test_class(j),j,j,true));
BOOST_CHECK(ccmpk(it)==make_tuple(test_class(j),j));
++j;
}
}
|