test_set_ops.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* Boost.MultiIndex test for standard set operations.
  2. *
  3. * Copyright 2003-2014 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_set_ops.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 type1{};
  19. struct type2
  20. {
  21. private:
  22. operator type1()const{return type1();}
  23. };
  24. struct type3
  25. {
  26. operator type1()const{return type1();}
  27. };
  28. struct less_type12
  29. {
  30. bool operator()(type1,type1)const{return false;}
  31. bool operator()(type1,type2)const{return false;}
  32. bool operator()(type2,type1)const{return false;}
  33. };
  34. bool less_type1_f(type1,type1){return false;}
  35. struct hash_type12
  36. {
  37. std::size_t operator()(type1)const{return 0;}
  38. std::size_t operator()(type2)const{return 0;}
  39. };
  40. struct eq_type12
  41. {
  42. bool operator()(type1,type1)const{return true;}
  43. bool operator()(type1,type2)const{return true;}
  44. bool operator()(type2,type1)const{return true;}
  45. };
  46. void test_set_ops()
  47. {
  48. employee_set es;
  49. employee_set_by_name& i1=get<by_name>(es);
  50. const employee_set_by_age& i2=get<age>(es);
  51. employee_set_by_ssn& i4=get<ssn>(es);
  52. es.insert(employee(0,"Joe",31,1123));
  53. es.insert(employee(1,"Robert",27,5601));
  54. es.insert(employee(2,"John",40,7889));
  55. es.insert(employee(3,"Albert",20,9012));
  56. es.insert(employee(4,"John",57,1002));
  57. BOOST_TEST(i1.find("John")->name=="John");
  58. BOOST_TEST(i2.find(41)==i2.end());
  59. BOOST_TEST(i4.find(5601)->name=="Robert");
  60. BOOST_TEST(i1.count("John")==2);
  61. BOOST_TEST(es.count(employee(10,"",-1,0))==0);
  62. BOOST_TEST(i4.count(7881)==0);
  63. BOOST_TEST(
  64. std::distance(
  65. i2.lower_bound(31),
  66. i2.upper_bound(60))==3);
  67. std::pair<employee_set_by_name::iterator,employee_set_by_name::iterator> p=
  68. i1.equal_range("John");
  69. BOOST_TEST(std::distance(p.first,p.second)==2);
  70. p=i1.equal_range("Serena");
  71. BOOST_TEST(p.first==i1.end()&&p.second==i1.end());
  72. std::pair<employee_set_by_age::iterator,employee_set_by_age::iterator> p2=
  73. i2.equal_range(30);
  74. BOOST_TEST(p2.first==p2.second&&p2.first->age==31);
  75. /* check promotion detection plays nice with private conversion */
  76. multi_index_container<
  77. type1,
  78. indexed_by<
  79. ordered_unique<identity<type1>,less_type12>,
  80. hashed_unique<identity<type1>,hash_type12,eq_type12>
  81. >
  82. > c;
  83. c.insert(type1());
  84. BOOST_TEST(c.find(type2())==c.begin());
  85. BOOST_TEST(c.count(type2())==1);
  86. BOOST_TEST(c.lower_bound(type2())==c.begin());
  87. BOOST_TEST(c.upper_bound(type2())==c.end());
  88. BOOST_TEST(c.equal_range(type2())==std::make_pair(c.begin(),c.end()));
  89. BOOST_TEST(c.get<1>().find(type2())==c.get<1>().begin());
  90. BOOST_TEST(c.get<1>().count(type2())==1);
  91. BOOST_TEST(c.get<1>().equal_range(type2())==
  92. std::make_pair(c.get<1>().begin(),c.get<1>().end()));
  93. /* check promotion detection does not break with functions */
  94. multi_index_container<
  95. type1,
  96. indexed_by<
  97. ordered_unique<identity<type1>,bool(*)(type1,type1)>
  98. >
  99. > c2(boost::make_tuple(boost::make_tuple(identity<type1>(),&less_type1_f)));
  100. c2.insert(type1());
  101. BOOST_TEST(c2.find(type3())==c2.begin());
  102. }