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
|
/* Boost.Flyweight test of flyweight forwarding and initializer_list ctors.
*
* Copyright 2006-2024 Joaquin M Lopez Munoz.
* 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/flyweight for library home page.
*/
#include "test_multictor.hpp"
#include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
#include <boost/config/workaround.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/flyweight.hpp>
#include <boost/functional/hash.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_comparison.hpp>
using boost::flyweight;
#if BOOST_WORKAROUND(BOOST_MSVC,<1300)
#define NONCONST const
#else
#define NONCONST
#endif
struct multictor
{
typedef multictor type;
multictor():
t(0,0,0.0,"",false){}
multictor(NONCONST int& x0):
t(x0,0,0.0,"",false){}
multictor(int x0,NONCONST char& x1):
t(x0,x1,0.0,"",false){}
multictor(int x0,char x1,NONCONST double& x2):
t(x0,x1,x2,"",false){}
multictor(int x0,char x1,double x2,NONCONST std::string& x3):
t(x0,x1,x2,x3,false){}
multictor(int x0,char x1,double x2,const std::string& x3,NONCONST bool& x4):
t(x0,x1,x2,x3,x4){}
friend bool operator==(const type& x,const type& y){return x.t==y.t;}
friend bool operator< (const type& x,const type& y){return x.t< y.t;}
friend bool operator!=(const type& x,const type& y){return x.t!=y.t;}
friend bool operator> (const type& x,const type& y){return x.t> y.t;}
friend bool operator>=(const type& x,const type& y){return x.t>=y.t;}
friend bool operator<=(const type& x,const type& y){return x.t<=y.t;}
boost::tuples::tuple<int,char,double,std::string,bool> t;
};
#if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
namespace boost{
#endif
inline std::size_t hash_value(const multictor& x)
{
std::size_t res=0;
boost::hash_combine(res,boost::tuples::get<0>(x.t));
boost::hash_combine(res,boost::tuples::get<1>(x.t));
boost::hash_combine(res,boost::tuples::get<2>(x.t));
boost::hash_combine(res,boost::tuples::get<3>(x.t));
boost::hash_combine(res,boost::tuples::get<4>(x.t));
return res;
}
#if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
} /* namespace boost */
#endif
#if !defined(BOOST_NO_SFINAE)&&!defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
#if !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX)
#define INIT0(_) {}
#define INIT1(a) {a}
#define INIT2(a,b) {a,b}
#define INIT_LIST1(a) {a}
#define INIT_LIST2(a,b) {a,b}
#else
#define INIT0(_)
#define INIT1(a) ((a))
#define INIT2(a,b) ((a),(b))
#define INIT_LIST1(a) ({a})
#define INIT_LIST2(a,b) ({a,b})
#endif
struct initctor
{
struct arg{arg(int= 0){}};
initctor():res(-1){}
initctor(arg,arg):res(-2){}
initctor(int,unsigned int):res(-3){}
initctor(std::initializer_list<int> list):res(0)
{
typedef const int* iterator;
for(iterator it=list.begin(),it_end=list.end();it!=it_end;++it){
res+=*it;
}
}
initctor(std::initializer_list<unsigned int> list):res(0)
{
typedef const unsigned int* iterator;
for(iterator it=list.begin(),it_end=list.end();it!=it_end;++it){
res+=(int)(*it)*2;
}
}
friend bool operator==(const initctor& x,const initctor& y)
{
return x.res==y.res;
}
int res;
};
#if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
namespace boost{
#endif
inline std::size_t hash_value(const initctor& x)
{
return (std::size_t)(x.res);
}
#if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
} /* namespace boost */
#endif
#endif
void test_multictor()
{
flyweight<multictor> f;
multictor m;
BOOST_TEST(f==m);
int x0=1;
flyweight<multictor> f0(x0);
multictor m0(x0);
BOOST_TEST(f0==m0);
char x1='a';
flyweight<multictor> f1(1,x1);
multictor m1(1,x1);
BOOST_TEST(f1==m1);
double x2=3.1416;
flyweight<multictor> f2(1,'a',x2);
multictor m2(1,'a',x2);
BOOST_TEST(f2==m2);
std::string x3("boost");
flyweight<multictor> f3(1,'a',3.1416,x3);
multictor m3(1,'a',3.1416,x3);
BOOST_TEST(f3==m3);
bool x4=true;
flyweight<multictor> f4(1,'a',3.1416,"boost",x4);
multictor m4(1,'a',3.1416,"boost",x4);
BOOST_TEST(f4==m4);
#if !defined(BOOST_NO_SFINAE)&&!defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
flyweight<initctor> ff INIT0(~);
BOOST_TEST(ff.get().res==-1);
ff=flyweight<initctor> INIT2(initctor::arg(),1);
BOOST_TEST(ff.get().res==-2);
flyweight<initctor> ff0 INIT2(initctor::arg(),initctor::arg());
BOOST_TEST(ff0.get().res==-2);
ff0={1};
BOOST_TEST(ff0.get().res==1);
flyweight<initctor> ff1 INIT_LIST2(1,2);
BOOST_TEST(ff1.get().res==3);
ff1={1u,2u,3u};
BOOST_TEST(ff1.get().res==12);
flyweight<initctor> ff2 INIT_LIST1(1u);
BOOST_TEST(ff2.get().res==2);
#endif
}
|