test_comparison.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* Boost.MultiIndex test for comparison functions.
  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_comparison.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 "pair_of_ints.hpp"
  15. #include <boost/detail/lightweight_test.hpp>
  16. using namespace boost::multi_index;
  17. template<typename Value>
  18. struct lookup_list{
  19. typedef multi_index_container<
  20. Value,
  21. indexed_by<
  22. sequenced<>,
  23. ordered_non_unique<identity<Value> >
  24. >
  25. > type;
  26. };
  27. template<typename Value>
  28. struct lookup_vector{
  29. typedef multi_index_container<
  30. Value,
  31. indexed_by<
  32. random_access<>,
  33. ordered_non_unique<identity<Value> >
  34. >
  35. > type;
  36. };
  37. void test_comparison()
  38. {
  39. employee_set es;
  40. employee_set_by_name& i1=get<1>(es);
  41. employee_set_by_age& i2=get<2>(es);
  42. employee_set_as_inserted& i3=get<3>(es);
  43. employee_set_by_ssn& i4=get<4>(es);
  44. employee_set_randomly& i5=get<5>(es);
  45. es.insert(employee(0,"Joe",31,1123));
  46. es.insert(employee(1,"Robert",27,5601));
  47. es.insert(employee(2,"John",40,7889));
  48. es.insert(employee(3,"Albert",20,9012));
  49. es.insert(employee(4,"John",57,1002));
  50. employee_set es2;
  51. employee_set_by_name& i12=get<by_name>(es2);
  52. employee_set_by_age& i22=get<age>(es2);
  53. employee_set_as_inserted& i32=get<3>(es2);
  54. employee_set_by_ssn& i42=get<4>(es2);
  55. employee_set_randomly& i52=get<5>(es2);
  56. es2.insert(employee(0,"Joe",31,1123));
  57. es2.insert(employee(1,"Robert",27,5601));
  58. es2.insert(employee(2,"John",40,7889));
  59. es2.insert(employee(3,"Albert",20,9012));
  60. BOOST_TEST(es==es&&es<=es&&es>=es&&
  61. i12==i12&&
  62. i22==i22&&i22<=i22&&i22>=i22&&
  63. i32==i32&&i32<=i32&&i32>=i32&&
  64. i42==i42&&
  65. i52==i52&&i52<=i52&&i52>=i52);
  66. BOOST_TEST(es!=es2&&es2<es&&es>es2&&!(es<=es2)&&!(es2>=es));
  67. BOOST_TEST(i1!=i12);
  68. BOOST_TEST(i2!=i22&&i22<i2&&i2>i22&&!(i2<=i22)&&!(i22>=i2));
  69. BOOST_TEST(i3!=i32&&i32<i3&&i3>i32&&!(i3<=i32)&&!(i32>=i3));
  70. BOOST_TEST(i4!=i42);
  71. BOOST_TEST(i5!=i52&&i52<i5&&i5>i52&&!(i5<=i52)&&!(i52>=i5));
  72. multi_index_container<
  73. pair_of_ints,
  74. indexed_by<
  75. hashed_non_unique<BOOST_MULTI_INDEX_MEMBER(pair_of_ints,int,first)>
  76. >
  77. > hc1,hc2;
  78. hc1.insert(pair_of_ints(0,0));
  79. hc1.insert(pair_of_ints(0,1));
  80. hc1.insert(pair_of_ints(0,2));
  81. hc1.insert(pair_of_ints(0,3));
  82. hc1.insert(pair_of_ints(1,0));
  83. hc1.insert(pair_of_ints(1,1));
  84. hc2.insert(pair_of_ints(0,2));
  85. hc2.insert(pair_of_ints(0,1));
  86. hc2.insert(pair_of_ints(1,1));
  87. hc2.insert(pair_of_ints(1,0));
  88. hc2.insert(pair_of_ints(0,3));
  89. hc2.insert(pair_of_ints(0,0));
  90. BOOST_TEST(hc1==hc2);
  91. hc1.insert(pair_of_ints(0,4));
  92. hc2.insert(pair_of_ints(0,5));
  93. BOOST_TEST(hc1!=hc2);
  94. lookup_list<int>::type l1;
  95. lookup_list<char>::type l2;
  96. lookup_vector<char>::type l3;
  97. lookup_list<long>::type l4;
  98. lookup_vector<long>::type l5;
  99. l1.push_back(3);
  100. l1.push_back(4);
  101. l1.push_back(5);
  102. l1.push_back(1);
  103. l1.push_back(2);
  104. l2.push_back(char(3));
  105. l2.push_back(char(4));
  106. l2.push_back(char(5));
  107. l2.push_back(char(1));
  108. l2.push_back(char(2));
  109. l3.push_back(char(3));
  110. l3.push_back(char(4));
  111. l3.push_back(char(5));
  112. l3.push_back(char(1));
  113. l3.push_back(char(2));
  114. l4.push_back(long(3));
  115. l4.push_back(long(4));
  116. l4.push_back(long(5));
  117. l4.push_back(long(1));
  118. l5.push_back(long(3));
  119. l5.push_back(long(4));
  120. l5.push_back(long(5));
  121. l5.push_back(long(1));
  122. BOOST_TEST(l1==l2&&l1<=l2&&l1>=l2);
  123. BOOST_TEST(
  124. get<1>(l1)==get<1>(l2)&&get<1>(l1)<=get<1>(l2)&&get<1>(l1)>=get<1>(l2));
  125. BOOST_TEST(
  126. get<1>(l1)==get<1>(l3)&&get<1>(l1)<=get<1>(l3)&&get<1>(l1)>=get<1>(l3));
  127. BOOST_TEST(l1!=l4&&l4<l1&&l1>l4);
  128. BOOST_TEST(
  129. get<1>(l1)!=get<1>(l4)&&get<1>(l1)<get<1>(l4)&&get<1>(l4)>get<1>(l1));
  130. BOOST_TEST(l3!=l5&&l5<l3&&l3>l5);
  131. BOOST_TEST(
  132. get<1>(l3)!=get<1>(l5)&&get<1>(l3)<get<1>(l5)&&get<1>(l5)>get<1>(l3));
  133. }