top_keeper_ut.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include <library/cpp/containers/limited_heap/limited_heap.h>
  2. #include <library/cpp/containers/top_keeper/top_keeper.h>
  3. #include <library/cpp/testing/unittest/registar.h>
  4. #include <util/random/random.h>
  5. static ui32 seed = 3;
  6. ui32 Rnd() {
  7. seed = seed * 5 + 1;
  8. return seed;
  9. }
  10. /*
  11. * Tests for TTopKeeper
  12. */
  13. Y_UNIT_TEST_SUITE(TTopKeeperTest) {
  14. // Tests correctness on usual examples
  15. Y_UNIT_TEST(CorrectnessTest) {
  16. int m = 20000;
  17. TLimitedHeap<std::pair<int, int>> h1(m);
  18. TTopKeeper<std::pair<int, int>> h2(m);
  19. int n = 20000000;
  20. while (n--) {
  21. int r = int(Rnd());
  22. h1.Insert({r, -r});
  23. h2.Emplace(r, -r);
  24. }
  25. h2.Finalize();
  26. UNIT_ASSERT_EQUAL(h1.GetSize(), h2.GetSize());
  27. while (!h1.IsEmpty()) {
  28. UNIT_ASSERT_EQUAL(h1.GetMin(), h2.GetNext());
  29. h1.PopMin();
  30. h2.Pop();
  31. }
  32. }
  33. // Tests on zero-size correctness
  34. Y_UNIT_TEST(ZeroSizeCorrectnes) {
  35. TTopKeeper<int> h(0);
  36. for (int i = 0; i < 100; ++i) {
  37. h.Insert(i % 10 + i / 10);
  38. }
  39. h.Finalize();
  40. UNIT_ASSERT(h.IsEmpty());
  41. }
  42. // Tests SetMaxSize behaviour
  43. Y_UNIT_TEST(SetMaxSizeTest) {
  44. int m = 20000;
  45. TLimitedHeap<int> h1(m);
  46. TTopKeeper<int> h2(m);
  47. int n = 20000000;
  48. while (n--) {
  49. int r = int(Rnd());
  50. h1.Insert(r);
  51. h2.Insert(r);
  52. }
  53. h1.SetMaxSize(m / 3);
  54. h2.SetMaxSize(m / 3);
  55. h2.Finalize();
  56. UNIT_ASSERT_EQUAL(h1.GetSize(), h2.GetSize());
  57. while (!h1.IsEmpty()) {
  58. UNIT_ASSERT_EQUAL(h1.GetMin(), h2.GetNext());
  59. h1.PopMin();
  60. h2.Pop();
  61. }
  62. }
  63. // Tests reuse behavior
  64. Y_UNIT_TEST(ReuseTest) {
  65. int m = 20000;
  66. TLimitedHeap<int> h1(m);
  67. TTopKeeper<int> h2(m);
  68. int n = 20000000;
  69. while (n--) {
  70. int r = int(Rnd());
  71. h1.Insert(r);
  72. h2.Insert(r);
  73. }
  74. UNIT_ASSERT_EQUAL(h1.GetSize(), h2.GetSize());
  75. while (!h1.IsEmpty()) {
  76. UNIT_ASSERT_EQUAL(h1.GetMin(), h2.GetNext());
  77. h1.PopMin();
  78. h2.Pop();
  79. }
  80. n = 20000000;
  81. while (n--) {
  82. int r = int(Rnd());
  83. h1.Insert(r);
  84. h2.Insert(r);
  85. }
  86. UNIT_ASSERT_EQUAL(h1.GetSize(), h2.GetSize());
  87. while (!h1.IsEmpty()) {
  88. UNIT_ASSERT_EQUAL(h1.GetMin(), h2.GetNext());
  89. h1.PopMin();
  90. h2.Pop();
  91. }
  92. }
  93. // Tests reset behavior
  94. Y_UNIT_TEST(ResetTest) {
  95. int m = 20000;
  96. TLimitedHeap<int> h1(m);
  97. TTopKeeper<int> h2(m);
  98. int n = 20000000;
  99. while (n--) {
  100. int r = int(Rnd());
  101. h1.Insert(r);
  102. h2.Insert(r);
  103. }
  104. UNIT_ASSERT_EQUAL(h1.GetSize(), h2.GetSize());
  105. for (int i = 0; i < m / 2; ++i) {
  106. UNIT_ASSERT_EQUAL(h1.GetMin(), h2.GetNext());
  107. h1.PopMin();
  108. h2.Pop();
  109. }
  110. h2.Reset();
  111. while (!h1.IsEmpty()) {
  112. h1.PopMin();
  113. }
  114. n = 20000000;
  115. while (n--) {
  116. int r = int(Rnd());
  117. h1.Insert(r);
  118. h2.Insert(r);
  119. }
  120. UNIT_ASSERT_EQUAL(h1.GetSize(), h2.GetSize());
  121. while (!h1.IsEmpty()) {
  122. UNIT_ASSERT_EQUAL(h1.GetMin(), h2.GetNext());
  123. h1.PopMin();
  124. h2.Pop();
  125. }
  126. }
  127. Y_UNIT_TEST(PreRegressionTest) {
  128. typedef std::pair<float, unsigned int> TElementType;
  129. const size_t randomTriesCount = 128;
  130. for (size_t i1 = 0; i1 < randomTriesCount; ++i1) {
  131. const size_t desiredElementsCount = RandomNumber<size_t>(5) + 1;
  132. TLimitedHeap<TElementType> h1(desiredElementsCount);
  133. TTopKeeper<TElementType> h2(desiredElementsCount);
  134. const size_t elementsToInsert = RandomNumber<size_t>(10) + desiredElementsCount;
  135. UNIT_ASSERT_C(desiredElementsCount <= elementsToInsert, "Test internal invariant is broken");
  136. for (size_t i2 = 0; i2 < elementsToInsert; ++i2) {
  137. const auto f = RandomNumber<float>();
  138. const auto id = RandomNumber<unsigned int>();
  139. h1.Insert(TElementType(f, id));
  140. h2.Insert(TElementType(f, id));
  141. }
  142. h2.Finalize();
  143. //we inserted enough elements to guarantee this outcome
  144. UNIT_ASSERT_EQUAL(h1.GetSize(), desiredElementsCount);
  145. UNIT_ASSERT_EQUAL(h2.GetSize(), desiredElementsCount);
  146. const auto n = h2.GetSize();
  147. for (size_t i3 = 0; i3 < n; ++i3) {
  148. UNIT_ASSERT_EQUAL(h1.GetMin(), h2.GetNext());
  149. h1.PopMin();
  150. h2.Pop();
  151. }
  152. }
  153. }
  154. Y_UNIT_TEST(CopyKeeperRegressionCase) {
  155. using TKeeper = TTopKeeper<float>;
  156. TVector<TKeeper> v(2, TKeeper(200));
  157. auto& k = v[1];
  158. for (size_t i = 0; i < 100; ++i) {
  159. k.Insert(RandomNumber<float>());
  160. }
  161. k.Finalize();
  162. }
  163. Y_UNIT_TEST(ExtractTest) {
  164. TTopKeeper<size_t> keeper(100);
  165. for (size_t i = 0; i < 100; ++i) {
  166. keeper.Insert(i);
  167. }
  168. auto values = keeper.Extract();
  169. UNIT_ASSERT_EQUAL(values.size(), 100);
  170. Sort(values);
  171. for (size_t i = 0; i < 100; ++i) {
  172. UNIT_ASSERT_EQUAL(values[i], i);
  173. }
  174. UNIT_ASSERT(keeper.IsEmpty());
  175. }
  176. }