skiplist_ut.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include "skiplist.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. namespace NThreading {
  4. namespace {
  5. struct TTestObject {
  6. static size_t Count;
  7. int Tag;
  8. TTestObject(int tag)
  9. : Tag(tag)
  10. {
  11. ++Count;
  12. }
  13. TTestObject(const TTestObject& other)
  14. : Tag(other.Tag)
  15. {
  16. ++Count;
  17. }
  18. ~TTestObject() {
  19. --Count;
  20. }
  21. bool operator<(const TTestObject& other) const {
  22. return Tag < other.Tag;
  23. }
  24. };
  25. size_t TTestObject::Count = 0;
  26. }
  27. ////////////////////////////////////////////////////////////////////////////////
  28. Y_UNIT_TEST_SUITE(TSkipListTest) {
  29. Y_UNIT_TEST(ShouldBeEmptyAfterCreation) {
  30. TMemoryPool pool(1024);
  31. TSkipList<int> list(pool);
  32. UNIT_ASSERT_EQUAL(list.GetSize(), 0);
  33. }
  34. Y_UNIT_TEST(ShouldAllowInsertion) {
  35. TMemoryPool pool(1024);
  36. TSkipList<int> list(pool);
  37. UNIT_ASSERT(list.Insert(12345678));
  38. UNIT_ASSERT_EQUAL(list.GetSize(), 1);
  39. }
  40. Y_UNIT_TEST(ShouldNotAllowDuplicates) {
  41. TMemoryPool pool(1024);
  42. TSkipList<int> list(pool);
  43. UNIT_ASSERT(list.Insert(12345678));
  44. UNIT_ASSERT_EQUAL(list.GetSize(), 1);
  45. UNIT_ASSERT(!list.Insert(12345678));
  46. UNIT_ASSERT_EQUAL(list.GetSize(), 1);
  47. }
  48. Y_UNIT_TEST(ShouldContainInsertedItem) {
  49. TMemoryPool pool(1024);
  50. TSkipList<int> list(pool);
  51. UNIT_ASSERT(list.Insert(12345678));
  52. UNIT_ASSERT(list.Contains(12345678));
  53. }
  54. Y_UNIT_TEST(ShouldNotContainNotInsertedItem) {
  55. TMemoryPool pool(1024);
  56. TSkipList<int> list(pool);
  57. UNIT_ASSERT(list.Insert(12345678));
  58. UNIT_ASSERT(!list.Contains(87654321));
  59. }
  60. Y_UNIT_TEST(ShouldIterateAllItems) {
  61. TMemoryPool pool(1024);
  62. TSkipList<int> list(pool);
  63. for (int i = 8; i > 0; --i) {
  64. UNIT_ASSERT(list.Insert(i));
  65. }
  66. TSkipList<int>::TIterator it = list.SeekToFirst();
  67. for (int i = 1; i <= 8; ++i) {
  68. UNIT_ASSERT(it.IsValid());
  69. UNIT_ASSERT_EQUAL(it.GetValue(), i);
  70. it.Next();
  71. }
  72. UNIT_ASSERT(!it.IsValid());
  73. }
  74. Y_UNIT_TEST(ShouldIterateAllItemsInReverseDirection) {
  75. TMemoryPool pool(1024);
  76. TSkipList<int> list(pool);
  77. for (int i = 8; i > 0; --i) {
  78. UNIT_ASSERT(list.Insert(i));
  79. }
  80. TSkipList<int>::TIterator it = list.SeekToLast();
  81. for (int i = 8; i > 0; --i) {
  82. UNIT_ASSERT(it.IsValid());
  83. UNIT_ASSERT_EQUAL(it.GetValue(), i);
  84. it.Prev();
  85. }
  86. UNIT_ASSERT(!it.IsValid());
  87. }
  88. Y_UNIT_TEST(ShouldSeekToFirstItem) {
  89. TMemoryPool pool(1024);
  90. TSkipList<int> list(pool);
  91. for (int i = 1; i < 10; ++i) {
  92. UNIT_ASSERT(list.Insert(i));
  93. }
  94. TSkipList<int>::TIterator it = list.SeekToFirst();
  95. UNIT_ASSERT(it.IsValid());
  96. UNIT_ASSERT_EQUAL(it.GetValue(), 1);
  97. }
  98. Y_UNIT_TEST(ShouldSeekToLastItem) {
  99. TMemoryPool pool(1024);
  100. TSkipList<int> list(pool);
  101. for (int i = 1; i < 10; ++i) {
  102. UNIT_ASSERT(list.Insert(i));
  103. }
  104. TSkipList<int>::TIterator it = list.SeekToLast();
  105. UNIT_ASSERT(it.IsValid());
  106. UNIT_ASSERT_EQUAL(it.GetValue(), 9);
  107. }
  108. Y_UNIT_TEST(ShouldSeekToExistingItem) {
  109. TMemoryPool pool(1024);
  110. TSkipList<int> list(pool);
  111. UNIT_ASSERT(list.Insert(12345678));
  112. TSkipList<int>::TIterator it = list.SeekTo(12345678);
  113. UNIT_ASSERT(it.IsValid());
  114. }
  115. Y_UNIT_TEST(ShouldSeekAfterMissedItem) {
  116. TMemoryPool pool(1024);
  117. TSkipList<int> list(pool);
  118. UNIT_ASSERT(list.Insert(100));
  119. UNIT_ASSERT(list.Insert(300));
  120. TSkipList<int>::TIterator it = list.SeekTo(200);
  121. UNIT_ASSERT(it.IsValid());
  122. UNIT_ASSERT_EQUAL(it.GetValue(), 300);
  123. it.Prev();
  124. UNIT_ASSERT(it.IsValid());
  125. UNIT_ASSERT_EQUAL(it.GetValue(), 100);
  126. }
  127. Y_UNIT_TEST(ShouldCallDtorsOfNonPodTypes) {
  128. UNIT_ASSERT(!TTypeTraits<TTestObject>::IsPod);
  129. UNIT_ASSERT_EQUAL(TTestObject::Count, 0);
  130. {
  131. TMemoryPool pool(1024);
  132. TSkipList<TTestObject> list(pool);
  133. UNIT_ASSERT(list.Insert(TTestObject(1)));
  134. UNIT_ASSERT(list.Insert(TTestObject(2)));
  135. UNIT_ASSERT_EQUAL(TTestObject::Count, 2);
  136. }
  137. UNIT_ASSERT_EQUAL(TTestObject::Count, 0);
  138. }
  139. }
  140. }