queue_ut.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include "queue.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/generic/set.h>
  4. namespace NThreading {
  5. ////////////////////////////////////////////////////////////////////////////////
  6. Y_UNIT_TEST_SUITE(TOneOneQueueTest){
  7. Y_UNIT_TEST(ShouldBeEmptyAtStart){
  8. TOneOneQueue<int> queue;
  9. int result = 0;
  10. UNIT_ASSERT(queue.IsEmpty());
  11. UNIT_ASSERT(!queue.Dequeue(result));
  12. }
  13. Y_UNIT_TEST(ShouldReturnEntries) {
  14. TOneOneQueue<int> queue;
  15. queue.Enqueue(1);
  16. queue.Enqueue(2);
  17. queue.Enqueue(3);
  18. int result = 0;
  19. UNIT_ASSERT(!queue.IsEmpty());
  20. UNIT_ASSERT(queue.Dequeue(result));
  21. UNIT_ASSERT_EQUAL(result, 1);
  22. UNIT_ASSERT(!queue.IsEmpty());
  23. UNIT_ASSERT(queue.Dequeue(result));
  24. UNIT_ASSERT_EQUAL(result, 2);
  25. UNIT_ASSERT(!queue.IsEmpty());
  26. UNIT_ASSERT(queue.Dequeue(result));
  27. UNIT_ASSERT_EQUAL(result, 3);
  28. UNIT_ASSERT(queue.IsEmpty());
  29. UNIT_ASSERT(!queue.Dequeue(result));
  30. }
  31. Y_UNIT_TEST(ShouldStoreMultipleChunks) {
  32. TOneOneQueue<int, 100> queue;
  33. for (int i = 0; i < 1000; ++i) {
  34. queue.Enqueue(i);
  35. }
  36. for (int i = 0; i < 1000; ++i) {
  37. int result = 0;
  38. UNIT_ASSERT(!queue.IsEmpty());
  39. UNIT_ASSERT(queue.Dequeue(result));
  40. UNIT_ASSERT_EQUAL(result, i);
  41. }
  42. }
  43. }
  44. ////////////////////////////////////////////////////////////////////////////////
  45. Y_UNIT_TEST_SUITE(TManyOneQueueTest){
  46. Y_UNIT_TEST(ShouldBeEmptyAtStart){
  47. TManyOneQueue<int> queue;
  48. int result;
  49. UNIT_ASSERT(queue.IsEmpty());
  50. UNIT_ASSERT(!queue.Dequeue(result));
  51. }
  52. Y_UNIT_TEST(ShouldReturnEntries) {
  53. TManyOneQueue<int> queue;
  54. queue.Enqueue(1);
  55. queue.Enqueue(2);
  56. queue.Enqueue(3);
  57. int result = 0;
  58. UNIT_ASSERT(!queue.IsEmpty());
  59. UNIT_ASSERT(queue.Dequeue(result));
  60. UNIT_ASSERT_EQUAL(result, 1);
  61. UNIT_ASSERT(!queue.IsEmpty());
  62. UNIT_ASSERT(queue.Dequeue(result));
  63. UNIT_ASSERT_EQUAL(result, 2);
  64. UNIT_ASSERT(!queue.IsEmpty());
  65. UNIT_ASSERT(queue.Dequeue(result));
  66. UNIT_ASSERT_EQUAL(result, 3);
  67. UNIT_ASSERT(queue.IsEmpty());
  68. UNIT_ASSERT(!queue.Dequeue(result));
  69. }
  70. }
  71. ////////////////////////////////////////////////////////////////////////////////
  72. Y_UNIT_TEST_SUITE(TManyManyQueueTest){
  73. Y_UNIT_TEST(ShouldBeEmptyAtStart){
  74. TManyManyQueue<int> queue;
  75. int result = 0;
  76. UNIT_ASSERT(queue.IsEmpty());
  77. UNIT_ASSERT(!queue.Dequeue(result));
  78. }
  79. Y_UNIT_TEST(ShouldReturnEntries) {
  80. TManyManyQueue<int> queue;
  81. queue.Enqueue(1);
  82. queue.Enqueue(2);
  83. queue.Enqueue(3);
  84. int result = 0;
  85. UNIT_ASSERT(!queue.IsEmpty());
  86. UNIT_ASSERT(queue.Dequeue(result));
  87. UNIT_ASSERT_EQUAL(result, 1);
  88. UNIT_ASSERT(!queue.IsEmpty());
  89. UNIT_ASSERT(queue.Dequeue(result));
  90. UNIT_ASSERT_EQUAL(result, 2);
  91. UNIT_ASSERT(!queue.IsEmpty());
  92. UNIT_ASSERT(queue.Dequeue(result));
  93. UNIT_ASSERT_EQUAL(result, 3);
  94. UNIT_ASSERT(queue.IsEmpty());
  95. UNIT_ASSERT(!queue.Dequeue(result));
  96. }
  97. }
  98. ////////////////////////////////////////////////////////////////////////////////
  99. Y_UNIT_TEST_SUITE(TRelaxedManyOneQueueTest){
  100. Y_UNIT_TEST(ShouldBeEmptyAtStart){
  101. TRelaxedManyOneQueue<int> queue;
  102. int result;
  103. UNIT_ASSERT(queue.IsEmpty());
  104. UNIT_ASSERT(!queue.Dequeue(result));
  105. }
  106. Y_UNIT_TEST(ShouldReturnEntries) {
  107. TSet<int> items = {1, 2, 3};
  108. TRelaxedManyOneQueue<int> queue;
  109. for (int item : items) {
  110. queue.Enqueue(item);
  111. }
  112. int result = 0;
  113. UNIT_ASSERT(!queue.IsEmpty());
  114. UNIT_ASSERT(queue.Dequeue(result));
  115. UNIT_ASSERT(items.erase(result));
  116. UNIT_ASSERT(!queue.IsEmpty());
  117. UNIT_ASSERT(queue.Dequeue(result));
  118. UNIT_ASSERT(items.erase(result));
  119. UNIT_ASSERT(!queue.IsEmpty());
  120. UNIT_ASSERT(queue.Dequeue(result));
  121. UNIT_ASSERT(items.erase(result));
  122. UNIT_ASSERT(queue.IsEmpty());
  123. UNIT_ASSERT(!queue.Dequeue(result));
  124. }
  125. }
  126. ////////////////////////////////////////////////////////////////////////////////
  127. Y_UNIT_TEST_SUITE(TRelaxedManyManyQueueTest){
  128. Y_UNIT_TEST(ShouldBeEmptyAtStart){
  129. TRelaxedManyManyQueue<int> queue;
  130. int result = 0;
  131. UNIT_ASSERT(queue.IsEmpty());
  132. UNIT_ASSERT(!queue.Dequeue(result));
  133. }
  134. Y_UNIT_TEST(ShouldReturnEntries) {
  135. TSet<int> items = {1, 2, 3};
  136. TRelaxedManyManyQueue<int> queue;
  137. for (int item : items) {
  138. queue.Enqueue(item);
  139. }
  140. int result = 0;
  141. UNIT_ASSERT(!queue.IsEmpty());
  142. UNIT_ASSERT(queue.Dequeue(result));
  143. UNIT_ASSERT(items.erase(result));
  144. UNIT_ASSERT(!queue.IsEmpty());
  145. UNIT_ASSERT(queue.Dequeue(result));
  146. UNIT_ASSERT(items.erase(result));
  147. UNIT_ASSERT(!queue.IsEmpty());
  148. UNIT_ASSERT(queue.Dequeue(result));
  149. UNIT_ASSERT(items.erase(result));
  150. UNIT_ASSERT(queue.IsEmpty());
  151. UNIT_ASSERT(!queue.Dequeue(result));
  152. }
  153. }
  154. }