extract.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright (C) 2016-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++17 } }
  16. #include <set>
  17. #include <algorithm>
  18. #include <testsuite_hooks.h>
  19. using test_type = std::set<int>;
  20. void
  21. test01()
  22. {
  23. test_type c{ 1, 2, 3 };
  24. test_type::node_type node;
  25. test_type::insert_return_type ins;
  26. test_type::iterator pos;
  27. node = c.extract(0);
  28. VERIFY( !node );
  29. VERIFY( node.empty() );
  30. VERIFY( c.size() == 3 );
  31. ins = c.insert(std::move(node));
  32. VERIFY( !node );
  33. VERIFY( node.empty() );
  34. VERIFY( c.size() == 3 );
  35. VERIFY( !ins.inserted );
  36. VERIFY( !ins.node );
  37. VERIFY( ins.position == c.end() );
  38. node = c.extract(1);
  39. VERIFY( (bool)node );
  40. VERIFY( !node.empty() );
  41. VERIFY( c.size() == 2 );
  42. VERIFY( node.get_allocator() == c.get_allocator() );
  43. VERIFY( node.value() == 1 );
  44. node.value() = 4;
  45. VERIFY( node.value() == 4 );
  46. ins = c.insert(std::move(node));
  47. VERIFY( !node );
  48. VERIFY( node.empty() );
  49. VERIFY( c.size() == 3 );
  50. VERIFY( ins.inserted );
  51. VERIFY( !ins.node );
  52. VERIFY( ins.position != c.end() );
  53. VERIFY( *ins.position == 4 );
  54. VERIFY( c.count(1) == 0 );
  55. VERIFY( c.count(4) == 1 );
  56. VERIFY( std::is_sorted(c.begin(), c.end()) );
  57. pos = c.insert(c.begin(), std::move(node));
  58. VERIFY( !node );
  59. VERIFY( node.empty() );
  60. VERIFY( c.size() == 3 );
  61. VERIFY( pos == c.end() );
  62. node = c.extract(2);
  63. pos = c.insert(c.begin(), std::move(node));
  64. VERIFY( c.size() == 3 );
  65. VERIFY( pos != c.end() );
  66. VERIFY( *pos == 2 );
  67. test_type c2 = c;
  68. node = c2.extract(3);
  69. ins = c.insert(std::move(node));
  70. VERIFY( node.empty() );
  71. VERIFY( ins.position != c.end() );
  72. VERIFY( !ins.inserted );
  73. VERIFY( !ins.node.empty() );
  74. VERIFY( ins.node.value() == 3 );
  75. VERIFY( *ins.position == ins.node.value() );
  76. }
  77. void
  78. test02()
  79. {
  80. test_type c{ 1, 2, 3 };
  81. test_type::node_type node;
  82. test_type::insert_return_type ins;
  83. node = c.extract(c.begin());
  84. VERIFY( (bool)node );
  85. VERIFY( !node.empty() );
  86. VERIFY( c.size() == 2 );
  87. VERIFY( node.get_allocator() == c.get_allocator() );
  88. VERIFY( node.value() == 1 );
  89. ins = c.insert(std::move(node));
  90. VERIFY( node.empty() );
  91. VERIFY( c.size() == 3 );
  92. VERIFY( ins.inserted );
  93. VERIFY( !ins.node );
  94. VERIFY( ins.position != c.end() );
  95. VERIFY( *ins.position == 1 );
  96. }
  97. void
  98. test03()
  99. {
  100. struct less : std::less<int> { };
  101. using std::is_same_v;
  102. using compat_type1 = std::set<int, less>;
  103. static_assert( is_same_v<test_type::node_type, compat_type1::node_type> );
  104. using compat_type2 = std::multiset<int>;
  105. static_assert( is_same_v<test_type::node_type, compat_type2::node_type> );
  106. using compat_type3 = std::multiset<int, less>;
  107. static_assert( is_same_v<test_type::node_type, compat_type3::node_type> );
  108. }
  109. void
  110. test04()
  111. {
  112. // Check order of members in insert_return_type
  113. auto [pos, ins, node] = test_type::insert_return_type{};
  114. using std::is_same_v;
  115. static_assert( is_same_v<test_type::iterator, decltype(pos)> );
  116. static_assert( is_same_v<bool, decltype(ins)> );
  117. static_assert( is_same_v<test_type::node_type, decltype(node)> );
  118. }
  119. int
  120. main()
  121. {
  122. test01();
  123. test02();
  124. test03();
  125. }