test_projection.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Boost.MultiIndex test for projection capabilities.
  2. *
  3. * Copyright 2003-2013 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/multi_index for library home page.
  9. */
  10. #include "test_projection.hpp"
  11. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  12. #include "pre_multi_index.hpp"
  13. #include "employee.hpp"
  14. #include <boost/detail/lightweight_test.hpp>
  15. using namespace boost::multi_index;
  16. void test_projection()
  17. {
  18. employee_set es;
  19. es.insert(employee(0,"Joe",31,1123));
  20. es.insert(employee(1,"Robert",27,5601));
  21. es.insert(employee(2,"John",40,7889));
  22. es.insert(employee(3,"Albert",20,9012));
  23. es.insert(employee(4,"John",57,1002));
  24. employee_set::iterator it,itbis;
  25. employee_set_by_name::iterator it1;
  26. employee_set_by_age::iterator it2;
  27. employee_set_as_inserted::iterator it3;
  28. employee_set_by_ssn::iterator it4;
  29. employee_set_randomly::iterator it5;
  30. BOOST_STATIC_ASSERT((boost::is_same<
  31. employee_set::iterator,
  32. nth_index_iterator<employee_set,0>::type >::value));
  33. BOOST_STATIC_ASSERT((boost::is_same<
  34. employee_set_by_name::iterator,
  35. nth_index_iterator<employee_set,1>::type >::value));
  36. #if defined(BOOST_NO_MEMBER_TEMPLATES)
  37. BOOST_STATIC_ASSERT((boost::is_same<
  38. employee_set_by_age::iterator,
  39. index_iterator<employee_set,age>::type >::value));
  40. #else
  41. BOOST_STATIC_ASSERT((boost::is_same<
  42. employee_set_by_age::iterator,
  43. employee_set::index_iterator<age>::type >::value));
  44. #endif
  45. BOOST_STATIC_ASSERT((boost::is_same<
  46. employee_set_as_inserted::iterator,
  47. nth_index_iterator<employee_set,3>::type >::value));
  48. BOOST_STATIC_ASSERT((boost::is_same<
  49. employee_set_by_ssn::iterator,
  50. nth_index_iterator<employee_set,4>::type >::value));
  51. BOOST_STATIC_ASSERT((boost::is_same<
  52. employee_set_randomly::iterator,
  53. nth_index_iterator<employee_set,5>::type >::value));
  54. it= es.find(employee(1,"Robert",27,5601));
  55. it1= project<name>(es,it);
  56. it2= project<age>(es,it1);
  57. it3= project<as_inserted>(es,it2);
  58. it4= project<ssn>(es,it3);
  59. it5= project<randomly>(es,it4);
  60. #if defined(BOOST_NO_MEMBER_TEMPLATES)
  61. itbis=project<0>(es,it5);
  62. #else
  63. itbis=es.project<0>(it5);
  64. #endif
  65. BOOST_TEST(
  66. *it==*it1&&*it1==*it2&&*it2==*it3&&*it3==*it4&&*it4==*it5&&itbis==it);
  67. BOOST_TEST(project<name>(es,es.end())==get<name>(es).end());
  68. BOOST_TEST(project<age>(es,es.end())==get<age>(es).end());
  69. BOOST_TEST(project<as_inserted>(es,es.end())==get<as_inserted>(es).end());
  70. BOOST_TEST(project<ssn>(es,es.end())==get<ssn>(es).end());
  71. BOOST_TEST(project<randomly>(es,es.end())==get<randomly>(es).end());
  72. const employee_set& ces=es;
  73. employee_set::const_iterator cit,citbis;
  74. employee_set_by_name::const_iterator cit1;
  75. employee_set_by_age::const_iterator cit2;
  76. employee_set_as_inserted::const_iterator cit3;
  77. employee_set_by_ssn::const_iterator cit4;
  78. employee_set_randomly::const_iterator cit5;
  79. BOOST_STATIC_ASSERT((boost::is_same<
  80. employee_set::const_iterator,
  81. nth_index_const_iterator<employee_set,0>::type >::value));
  82. BOOST_STATIC_ASSERT((boost::is_same<
  83. employee_set_by_name::const_iterator,
  84. nth_index_const_iterator<employee_set,1>::type >::value));
  85. #if defined(BOOST_NO_MEMBER_TEMPLATES)
  86. BOOST_STATIC_ASSERT((boost::is_same<
  87. employee_set_by_age::const_iterator,
  88. index_const_iterator<employee_set,age>::type >::value));
  89. #else
  90. BOOST_STATIC_ASSERT((boost::is_same<
  91. employee_set_by_age::const_iterator,
  92. employee_set::index_const_iterator<age>::type >::value));
  93. #endif
  94. BOOST_STATIC_ASSERT((boost::is_same<
  95. employee_set_as_inserted::const_iterator,
  96. nth_index_const_iterator<employee_set,3>::type >::value));
  97. BOOST_STATIC_ASSERT((boost::is_same<
  98. employee_set_by_ssn::const_iterator,
  99. nth_index_const_iterator<employee_set,4>::type >::value));
  100. BOOST_STATIC_ASSERT((boost::is_same<
  101. employee_set_randomly::const_iterator,
  102. nth_index_const_iterator<employee_set,5>::type >::value));
  103. cit= ces.find(employee(4,"John",57,1002));
  104. #if defined(BOOST_NO_MEMBER_TEMPLATES)
  105. cit1= project<by_name>(ces,cit);
  106. #else
  107. cit1= ces.project<by_name>(cit);
  108. #endif
  109. cit2= project<age>(ces,cit1);
  110. #if defined(BOOST_NO_MEMBER_TEMPLATES)
  111. cit3= project<as_inserted>(ces,cit2);
  112. #else
  113. cit3= ces.project<as_inserted>(cit2);
  114. #endif
  115. cit4= project<ssn>(ces,cit3);
  116. cit5= project<randomly>(ces,cit4);
  117. citbis=project<0>(ces,cit5);
  118. BOOST_TEST(
  119. *cit==*cit1&&*cit1==*cit2&&*cit2==*cit3&&*cit3==*cit4&&*cit4==*cit5&&
  120. citbis==cit);
  121. }