test_basic.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* Boost.MultiIndex basic test.
  2. *
  3. * Copyright 2003-2017 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_basic.hpp"
  11. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  12. #include <algorithm>
  13. #include <vector>
  14. #include "pre_multi_index.hpp"
  15. #include "employee.hpp"
  16. #include <boost/detail/lightweight_test.hpp>
  17. using namespace boost::multi_index;
  18. struct less_by_employee_age
  19. {
  20. bool operator()(const employee& e1,const employee& e2)const
  21. {
  22. return e1.age<e2.age;
  23. }
  24. };
  25. struct no_addressof_type
  26. {
  27. no_addressof_type(int n):n(n){}
  28. void operator&()const{}
  29. int n;
  30. };
  31. bool operator==(const no_addressof_type& x,const no_addressof_type& y)
  32. {
  33. return x.n==y.n;
  34. }
  35. bool operator<(const no_addressof_type& x,const no_addressof_type& y)
  36. {
  37. return x.n<y.n;
  38. }
  39. #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
  40. namespace boost{
  41. #endif
  42. inline std::size_t hash_value(const no_addressof_type& x)
  43. {
  44. boost::hash<int> h;
  45. return h(x.n);
  46. }
  47. #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
  48. } /* namespace boost */
  49. #endif
  50. void test_basic()
  51. {
  52. employee_set es;
  53. std::vector<employee> v;
  54. #if defined(BOOST_NO_MEMBER_TEMPLATES)
  55. employee_set_by_name& i1=get<by_name>(es);
  56. #else
  57. employee_set_by_name& i1=es.get<by_name>();
  58. #endif
  59. const employee_set_by_age& i2=get<2>(es);
  60. employee_set_as_inserted& i3=get<3>(es);
  61. employee_set_by_ssn& i4=get<ssn>(es);
  62. employee_set_randomly& i5=get<randomly>(es);
  63. es.insert(employee(0,"Joe",31,1123));
  64. es.insert(employee(5,"Anna",41,1123)); /* clash*/
  65. i1.insert(employee(1,"Robert",27,5601));
  66. es.insert(employee(2,"John",40,7889));
  67. i3.push_back(employee(3,"Albert",20,9012));
  68. i4.insert(employee(4,"John",57,1002));
  69. i5.push_back(employee(0,"Andrew",60,2302)); /* clash */
  70. v.push_back(employee(0,"Joe",31,1123));
  71. v.push_back(employee(1,"Robert",27,5601));
  72. v.push_back(employee(2,"John",40,7889));
  73. v.push_back(employee(3,"Albert",20,9012));
  74. v.push_back(employee(4,"John",57,1002));
  75. {
  76. /* by insertion order */
  77. BOOST_TEST(std::equal(i3.begin(),i3.end(),v.begin()));
  78. BOOST_TEST(std::equal(i5.begin(),i5.end(),v.begin()));
  79. }
  80. {
  81. /* by id */
  82. std::sort(v.begin(),v.end());
  83. BOOST_TEST(std::equal(es.begin(),es.end(),v.begin()));
  84. }
  85. {
  86. /* by age */
  87. std::sort(v.begin(),v.end(),less_by_employee_age());
  88. BOOST_TEST(std::equal(i2.begin(),i2.end(),v.begin()));
  89. }
  90. {
  91. /* testcase for https://svn.boost.org/trac10/ticket/13307 */
  92. typedef multi_index_container<
  93. no_addressof_type,
  94. indexed_by<
  95. random_access<>,
  96. ordered_non_unique<identity<no_addressof_type> >,
  97. sequenced<>,
  98. hashed_non_unique<identity<no_addressof_type> >
  99. >
  100. > multi_index_t;
  101. multi_index_t c;
  102. const multi_index_t& cc=c;
  103. no_addressof_type x(0);
  104. int a[]={1,2};
  105. int b[]={6,7};
  106. c.push_back(x);
  107. c.insert(c.end(),a,a+2);
  108. c.push_back(no_addressof_type(3));
  109. c.emplace_back(4);
  110. c.get<1>().emplace_hint(c.get<1>().begin(),5);
  111. c.get<1>().insert(b,b+2);
  112. (void)c.begin()->n;
  113. (void)c.get<1>().begin()->n;
  114. (void)c.get<2>().begin()->n;
  115. (void)c.get<3>().begin()->n;
  116. (void)c.get<3>().begin(0)->n;
  117. (void)c.iterator_to(c.front());
  118. (void)cc.iterator_to(c.front());
  119. (void)c.get<1>().iterator_to(c.front());
  120. (void)cc.get<1>().iterator_to(c.front());
  121. (void)c.get<2>().iterator_to(c.front());
  122. (void)cc.get<2>().iterator_to(c.front());
  123. (void)c.get<3>().iterator_to(c.front());
  124. (void)cc.get<3>().iterator_to(c.front());
  125. (void)c.get<3>().local_iterator_to(c.front());
  126. (void)cc.get<3>().local_iterator_to(c.front());
  127. multi_index_t c2=c;(void)c2;
  128. c.erase(c.begin());
  129. c.erase(c.begin(),c.end());
  130. }
  131. }