compact_set_ut.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //===- llvm/unittest/ADT/SmallSetTest.cpp ---------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // CompactSet unit tests.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include <library/cpp/yt/small_containers/compact_set.h>
  13. #include <library/cpp/testing/gtest/gtest.h>
  14. #include <string>
  15. namespace NYT {
  16. namespace {
  17. ////////////////////////////////////////////////////////////////////////////////
  18. TEST(TCompactSetTest, Insert)
  19. {
  20. TCompactSet<int, 4> s1;
  21. for (int i = 0; i < 4; i++)
  22. s1.insert(i);
  23. for (int i = 0; i < 4; i++)
  24. s1.insert(i);
  25. EXPECT_EQ(4u, s1.size());
  26. for (int i = 0; i < 4; i++)
  27. EXPECT_EQ(1u, s1.count(i));
  28. EXPECT_EQ(0u, s1.count(4));
  29. }
  30. TEST(TCompactSetTest, Grow)
  31. {
  32. TCompactSet<int, 4> s1;
  33. for (int i = 0; i < 8; i++)
  34. s1.insert(i);
  35. EXPECT_EQ(8u, s1.size());
  36. for (int i = 0; i < 8; i++)
  37. EXPECT_EQ(1u, s1.count(i));
  38. EXPECT_EQ(0u, s1.count(8));
  39. }
  40. TEST(TCompactSetTest, Erase)
  41. {
  42. TCompactSet<int, 4> s1;
  43. for (int i = 0; i < 8; i++)
  44. s1.insert(i);
  45. EXPECT_EQ(8u, s1.size());
  46. // Remove elements one by one and check if all other elements are still there.
  47. for (int i = 0; i < 8; i++) {
  48. EXPECT_EQ(1u, s1.count(i));
  49. EXPECT_TRUE(s1.erase(i));
  50. EXPECT_EQ(0u, s1.count(i));
  51. EXPECT_EQ(8u - i - 1, s1.size());
  52. for (int j = i + 1; j < 8; j++)
  53. EXPECT_EQ(1u, s1.count(j));
  54. }
  55. EXPECT_EQ(0u, s1.count(8));
  56. }
  57. TEST(TCompactSetTest, IteratorInt)
  58. {
  59. TCompactSet<int, 4> s1;
  60. // Test the 'small' case.
  61. for (int i = 0; i < 3; i++)
  62. s1.insert(i);
  63. std::vector<int> V(s1.begin(), s1.end());
  64. // Make sure the elements are in the expected order.
  65. std::sort(V.begin(), V.end());
  66. for (int i = 0; i < 3; i++)
  67. EXPECT_EQ(i, V[i]);
  68. // Test the 'big' case by adding a few more elements to switch to std::set
  69. // internally.
  70. for (int i = 3; i < 6; i++)
  71. s1.insert(i);
  72. V.assign(s1.begin(), s1.end());
  73. // Make sure the elements are in the expected order.
  74. std::sort(V.begin(), V.end());
  75. for (int i = 0; i < 6; i++)
  76. EXPECT_EQ(i, V[i]);
  77. }
  78. TEST(TCompactSetTest, IteratorString)
  79. {
  80. // Test CompactSetIterator for TCompactSet with a type with non-trivial
  81. // ctors/dtors.
  82. TCompactSet<std::string, 2> s1;
  83. s1.insert("str 1");
  84. s1.insert("str 2");
  85. s1.insert("str 1");
  86. std::vector<std::string> V(s1.begin(), s1.end());
  87. std::sort(V.begin(), V.end());
  88. EXPECT_EQ(2u, s1.size());
  89. EXPECT_EQ("str 1", V[0]);
  90. EXPECT_EQ("str 2", V[1]);
  91. s1.insert("str 4");
  92. s1.insert("str 0");
  93. s1.insert("str 4");
  94. V.assign(s1.begin(), s1.end());
  95. // Make sure the elements are in the expected order.
  96. std::sort(V.begin(), V.end());
  97. EXPECT_EQ(4u, s1.size());
  98. EXPECT_EQ("str 0", V[0]);
  99. EXPECT_EQ("str 1", V[1]);
  100. EXPECT_EQ("str 2", V[2]);
  101. EXPECT_EQ("str 4", V[3]);
  102. }
  103. TEST(TCompactSetTest, IteratorIncMoveCopy)
  104. {
  105. // Test CompactSetIterator for TCompactSet with a type with non-trivial
  106. // ctors/dtors.
  107. TCompactSet<std::string, 2> s1;
  108. s1.insert("str 1");
  109. s1.insert("str 2");
  110. auto Iter = s1.begin();
  111. EXPECT_EQ("str 1", *Iter);
  112. ++Iter;
  113. EXPECT_EQ("str 2", *Iter);
  114. s1.insert("str 4");
  115. s1.insert("str 0");
  116. auto Iter2 = s1.begin();
  117. Iter = std::move(Iter2);
  118. EXPECT_EQ("str 0", *Iter);
  119. }
  120. // These test weren't taken from llvm.
  121. TEST(TCompactSetTest, Empty)
  122. {
  123. TCompactSet<int, 10> v;
  124. EXPECT_TRUE(v.empty());
  125. auto data = {1, 2, 3, 4, 5};
  126. v.insert(data.begin(), data.end()); // not crossing size threshold
  127. v.erase(4);
  128. v.erase(2);
  129. v.erase(3);
  130. v.erase(5);
  131. v.erase(1);
  132. EXPECT_TRUE(v.empty());
  133. auto data2 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  134. v.insert(data2.begin(), data2.end()); // crossing size threshold
  135. v.erase(7);
  136. v.erase(3);
  137. v.erase(1);
  138. v.erase(10);
  139. v.erase(9);
  140. v.erase(0);
  141. v.erase(2);
  142. v.erase(6);
  143. v.erase(4);
  144. v.erase(5);
  145. v.erase(8);
  146. EXPECT_TRUE(v.empty());
  147. }
  148. TEST(TCompactSetTest, ForEach)
  149. {
  150. TCompactSet<int, 10> v;
  151. auto data = {10, 9, 3, 4, 1, 5, 6, 8};
  152. v.insert(data.begin(), data.end());
  153. std::vector<int> buf(v.begin(), v.end());
  154. std::vector<int> expected{1, 3, 4, 5, 6, 8, 9, 10};
  155. EXPECT_EQ(expected, buf);
  156. auto data2 = {7, 1, 2, 0};
  157. v.insert(data2.begin(), data2.end());
  158. buf.assign(v.begin(), v.end());
  159. expected = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  160. EXPECT_EQ(expected, buf);
  161. }
  162. ////////////////////////////////////////////////////////////////////////////////
  163. } // namespace
  164. } // namespace NYT