2.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Copyright (C) 2015-2022 Free Software Foundation, Inc.
  2. //
  3. // This file is part of the GNU ISO C++ Library. This library is free
  4. // software; you can redistribute it and/or modify it under the
  5. // terms of the GNU General Public License as published by the
  6. // Free Software Foundation; either version 3, or (at your option)
  7. // any later version.
  8. // This library is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. // You should have received a copy of the GNU General Public License along
  13. // with this library; see the file COPYING3. If not see
  14. // <http://www.gnu.org/licenses/>.
  15. // { dg-do run { target c++14 } }
  16. #include <set>
  17. #include <testsuite_hooks.h>
  18. struct Cmp
  19. {
  20. typedef void is_transparent;
  21. bool operator()(int i, long l) const { return i < l; }
  22. bool operator()(long l, int i) const { return l < i; }
  23. bool operator()(int i, int j) const { ++count; return i < j; }
  24. static int count;
  25. };
  26. int Cmp::count = 0;
  27. using test_type = std::set<int, Cmp>;
  28. test_type x{ 1, 3, 5 };
  29. const test_type& cx = x;
  30. void
  31. test01()
  32. {
  33. Cmp::count = 0;
  34. auto it = x.find(1L);
  35. VERIFY( it != x.end() && *it == 1 );
  36. it = x.find(2L);
  37. VERIFY( it == x.end() );
  38. auto cit = cx.find(3L);
  39. VERIFY( cit != cx.end() && *cit == 3 );
  40. cit = cx.find(2L);
  41. VERIFY( cit == cx.end() );
  42. VERIFY( Cmp::count == 0 );
  43. static_assert(std::is_same<decltype(it), test_type::iterator>::value,
  44. "find returns iterator");
  45. static_assert(std::is_same<decltype(cit), test_type::const_iterator>::value,
  46. "const find returns const_iterator");
  47. }
  48. void
  49. test02()
  50. {
  51. Cmp::count = 0;
  52. auto n = x.count(1L);
  53. VERIFY( n == 1 );
  54. n = x.count(2L);
  55. VERIFY( n == 0 );
  56. auto cn = cx.count(3L);
  57. VERIFY( cn == 1 );
  58. cn = cx.count(2L);
  59. VERIFY( cn == 0 );
  60. VERIFY( Cmp::count == 0 );
  61. }
  62. void
  63. test03()
  64. {
  65. Cmp::count = 0;
  66. auto it = x.lower_bound(1L);
  67. VERIFY( it != x.end() && *it == 1 );
  68. it = x.lower_bound(2L);
  69. VERIFY( it != x.end() && *it == 3 );
  70. auto cit = cx.lower_bound(1L);
  71. VERIFY( cit != cx.end() && *cit == 1 );
  72. cit = cx.lower_bound(2L);
  73. VERIFY( cit != cx.end() && *cit == 3 );
  74. VERIFY( Cmp::count == 0 );
  75. static_assert(std::is_same<decltype(it), test_type::iterator>::value,
  76. "lower_bound returns iterator");
  77. static_assert(std::is_same<decltype(cit), test_type::const_iterator>::value,
  78. "const lower_bound returns const_iterator");
  79. }
  80. void
  81. test04()
  82. {
  83. Cmp::count = 0;
  84. auto it = x.upper_bound(1L);
  85. VERIFY( it != x.end() && *it == 3 );
  86. it = x.upper_bound(5L);
  87. VERIFY( it == x.end() );
  88. auto cit = cx.upper_bound(1L);
  89. VERIFY( cit != cx.end() && *cit == 3 );
  90. cit = cx.upper_bound(5L);
  91. VERIFY( cit == cx.end() );
  92. VERIFY( Cmp::count == 0 );
  93. static_assert(std::is_same<decltype(it), test_type::iterator>::value,
  94. "upper_bound returns iterator");
  95. static_assert(std::is_same<decltype(cit), test_type::const_iterator>::value,
  96. "const upper_bound returns const_iterator");
  97. }
  98. void
  99. test05()
  100. {
  101. Cmp::count = 0;
  102. auto it = x.equal_range(1L);
  103. VERIFY( it.first != it.second && *it.first == 1 );
  104. it = x.equal_range(2L);
  105. VERIFY( it.first == it.second && it.first != x.end() );
  106. auto cit = cx.equal_range(1L);
  107. VERIFY( cit.first != cit.second && *cit.first == 1 );
  108. cit = cx.equal_range(2L);
  109. VERIFY( cit.first == cit.second && cit.first != cx.end() );
  110. VERIFY( Cmp::count == 0 );
  111. using pair = std::pair<test_type::iterator, test_type::iterator>;
  112. static_assert(std::is_same<decltype(it), pair>::value,
  113. "equal_range returns pair<iterator, iterator>");
  114. using cpair = std::pair<test_type::const_iterator, test_type::const_iterator>;
  115. static_assert(std::is_same<decltype(cit), cpair>::value,
  116. "const equal_range returns pair<const_iterator, const_iterator>");
  117. }
  118. void
  119. test06()
  120. {
  121. // https://gcc.gnu.org/ml/libstdc++/2015-01/msg00176.html
  122. // Verify the new function template overloads do not cause problems
  123. // when the comparison function is not transparent.
  124. struct I
  125. {
  126. int i;
  127. operator int() const { return i; }
  128. };
  129. std::set<int> s;
  130. I i = { };
  131. s.find(i);
  132. }
  133. void
  134. test07()
  135. {
  136. // PR libstdc++/78273
  137. struct C {
  138. bool operator()(int l, int r) const { return l < r; }
  139. struct Partition { };
  140. bool operator()(int l, Partition) const { return l < 2; }
  141. bool operator()(Partition, int r) const { return 4 < r; }
  142. using is_transparent = void;
  143. };
  144. std::set<int, C> s{ 1, 2, 3, 4, 5 };
  145. auto n = s.count(C::Partition{});
  146. VERIFY( n == 3 );
  147. }
  148. int
  149. main()
  150. {
  151. test01();
  152. test02();
  153. test03();
  154. test04();
  155. test05();
  156. test06();
  157. test07();
  158. }