test_multictor.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* Boost.Flyweight test of flyweight forwarding and initializer_list ctors.
  2. *
  3. * Copyright 2006-2015 Joaquin M Lopez Munoz.
  4. * Distributed under the Boost Software License, Version 1.0.
  5. * (See accompanying file LICENSE_1_0.txt or copy at
  6. * http://www.boost.org/LICENSE_1_0.txt)
  7. *
  8. * See http://www.boost.org/libs/flyweight for library home page.
  9. */
  10. #include "test_multictor.hpp"
  11. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  12. #include <boost/detail/lightweight_test.hpp>
  13. #include <boost/detail/workaround.hpp>
  14. #include <boost/flyweight.hpp>
  15. #include <boost/functional/hash.hpp>
  16. #include <boost/tuple/tuple.hpp>
  17. #include <boost/tuple/tuple_comparison.hpp>
  18. using boost::flyweight;
  19. #if BOOST_WORKAROUND(BOOST_MSVC,<1300)
  20. #define NONCONST const
  21. #else
  22. #define NONCONST
  23. #endif
  24. struct multictor
  25. {
  26. typedef multictor type;
  27. multictor():
  28. t(0,0,0.0,"",false){}
  29. multictor(NONCONST int& x0):
  30. t(x0,0,0.0,"",false){}
  31. multictor(int x0,NONCONST char& x1):
  32. t(x0,x1,0.0,"",false){}
  33. multictor(int x0,char x1,NONCONST double& x2):
  34. t(x0,x1,x2,"",false){}
  35. multictor(int x0,char x1,double x2,NONCONST std::string& x3):
  36. t(x0,x1,x2,x3,false){}
  37. multictor(int x0,char x1,double x2,const std::string& x3,NONCONST bool& x4):
  38. t(x0,x1,x2,x3,x4){}
  39. friend bool operator==(const type& x,const type& y){return x.t==y.t;}
  40. friend bool operator< (const type& x,const type& y){return x.t< y.t;}
  41. friend bool operator!=(const type& x,const type& y){return x.t!=y.t;}
  42. friend bool operator> (const type& x,const type& y){return x.t> y.t;}
  43. friend bool operator>=(const type& x,const type& y){return x.t>=y.t;}
  44. friend bool operator<=(const type& x,const type& y){return x.t<=y.t;}
  45. boost::tuples::tuple<int,char,double,std::string,bool> t;
  46. };
  47. #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
  48. namespace boost{
  49. #endif
  50. inline std::size_t hash_value(const multictor& x)
  51. {
  52. std::size_t res=0;
  53. boost::hash_combine(res,boost::tuples::get<0>(x.t));
  54. boost::hash_combine(res,boost::tuples::get<1>(x.t));
  55. boost::hash_combine(res,boost::tuples::get<2>(x.t));
  56. boost::hash_combine(res,boost::tuples::get<3>(x.t));
  57. boost::hash_combine(res,boost::tuples::get<4>(x.t));
  58. return res;
  59. }
  60. #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
  61. } /* namespace boost */
  62. #endif
  63. #if !defined(BOOST_NO_SFINAE)&&!defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  64. #if !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX)
  65. #define INIT0(_) {}
  66. #define INIT1(a) {a}
  67. #define INIT2(a,b) {a,b}
  68. #define INIT_LIST1(a) {a}
  69. #define INIT_LIST2(a,b) {a,b}
  70. #else
  71. #define INIT0(_)
  72. #define INIT1(a) ((a))
  73. #define INIT2(a,b) ((a),(b))
  74. #define INIT_LIST1(a) ({a})
  75. #define INIT_LIST2(a,b) ({a,b})
  76. #endif
  77. struct initctor
  78. {
  79. struct arg{arg(int= 0){}};
  80. initctor():res(-1){}
  81. initctor(arg,arg):res(-2){}
  82. initctor(int,unsigned int):res(-3){}
  83. initctor(std::initializer_list<int> list):res(0)
  84. {
  85. typedef const int* iterator;
  86. for(iterator it=list.begin(),it_end=list.end();it!=it_end;++it){
  87. res+=*it;
  88. }
  89. }
  90. initctor(std::initializer_list<unsigned int> list):res(0)
  91. {
  92. typedef const unsigned int* iterator;
  93. for(iterator it=list.begin(),it_end=list.end();it!=it_end;++it){
  94. res+=(int)(*it)*2;
  95. }
  96. }
  97. friend bool operator==(const initctor& x,const initctor& y)
  98. {
  99. return x.res==y.res;
  100. }
  101. int res;
  102. };
  103. #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
  104. namespace boost{
  105. #endif
  106. inline std::size_t hash_value(const initctor& x)
  107. {
  108. return (std::size_t)(x.res);
  109. }
  110. #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
  111. } /* namespace boost */
  112. #endif
  113. #endif
  114. void test_multictor()
  115. {
  116. flyweight<multictor> f;
  117. multictor m;
  118. BOOST_TEST(f==m);
  119. int x0=1;
  120. flyweight<multictor> f0(x0);
  121. multictor m0(x0);
  122. BOOST_TEST(f0==m0);
  123. char x1='a';
  124. flyweight<multictor> f1(1,x1);
  125. multictor m1(1,x1);
  126. BOOST_TEST(f1==m1);
  127. double x2=3.1416;
  128. flyweight<multictor> f2(1,'a',x2);
  129. multictor m2(1,'a',x2);
  130. BOOST_TEST(f2==m2);
  131. std::string x3("boost");
  132. flyweight<multictor> f3(1,'a',3.1416,x3);
  133. multictor m3(1,'a',3.1416,x3);
  134. BOOST_TEST(f3==m3);
  135. bool x4=true;
  136. flyweight<multictor> f4(1,'a',3.1416,"boost",x4);
  137. multictor m4(1,'a',3.1416,"boost",x4);
  138. BOOST_TEST(f4==m4);
  139. #if !defined(BOOST_NO_SFINAE)&&!defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  140. flyweight<initctor> ff INIT0(~);
  141. BOOST_TEST(ff.get().res==-1);
  142. ff=flyweight<initctor> INIT2(initctor::arg(),1);
  143. BOOST_TEST(ff.get().res==-2);
  144. flyweight<initctor> ff0 INIT2(initctor::arg(),initctor::arg());
  145. BOOST_TEST(ff0.get().res==-2);
  146. ff0={1};
  147. BOOST_TEST(ff0.get().res==1);
  148. flyweight<initctor> ff1 INIT_LIST2(1,2);
  149. BOOST_TEST(ff1.get().res==3);
  150. ff1={1u,2u,3u};
  151. BOOST_TEST(ff1.get().res==12);
  152. flyweight<initctor> ff2 INIT_LIST1(1u);
  153. BOOST_TEST(ff2.get().res==2);
  154. #endif
  155. }