summaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/compact_containers/unittests/compact_queue_ut.cpp
diff options
context:
space:
mode:
authorbabenko <[email protected]>2025-01-12 08:43:05 +0300
committerbabenko <[email protected]>2025-01-12 09:02:29 +0300
commit6065cf56d7ca8909c36e1f5c38daac25b2e584da (patch)
tree0f0842364b043541a144ceff0b9ca45cb46fc329 /library/cpp/yt/compact_containers/unittests/compact_queue_ut.cpp
parentf05fb708c73e4f5a484e9546c92a9ae8c5e799e8 (diff)
YT-18571: library/cpp/yt/small_containers -> library/cpp/yt/compact_containers
commit_hash:fc31d2770ebeffeb513c4535bd146c731b7f78fb
Diffstat (limited to 'library/cpp/yt/compact_containers/unittests/compact_queue_ut.cpp')
-rw-r--r--library/cpp/yt/compact_containers/unittests/compact_queue_ut.cpp114
1 files changed, 114 insertions, 0 deletions
diff --git a/library/cpp/yt/compact_containers/unittests/compact_queue_ut.cpp b/library/cpp/yt/compact_containers/unittests/compact_queue_ut.cpp
new file mode 100644
index 00000000000..463d4ec4970
--- /dev/null
+++ b/library/cpp/yt/compact_containers/unittests/compact_queue_ut.cpp
@@ -0,0 +1,114 @@
+#include <library/cpp/yt/compact_containers/compact_queue.h>
+
+#include <library/cpp/testing/gtest/gtest.h>
+
+#include <queue>
+#include <random>
+
+namespace NYT {
+namespace {
+
+////////////////////////////////////////////////////////////////////////////////
+
+TEST(TCompactQueueTest, Simple)
+{
+ TCompactQueue<int, 4> queue;
+ queue.Push(1);
+ queue.Push(2);
+ queue.Push(3);
+
+ for (int i = 1; i <= 10; i++) {
+ EXPECT_EQ(queue.Size(), 3u);
+ EXPECT_EQ(queue.Front(), i);
+ EXPECT_EQ(queue.Pop(), i);
+ queue.Push(i + 3);
+ }
+
+ for (int i = 11; i <= 13; i++) {
+ EXPECT_EQ(queue.Front(), i);
+ queue.Pop();
+ }
+
+ EXPECT_TRUE(queue.Empty());
+}
+
+TEST(TCompactQueueTest, Reallocation1)
+{
+ TCompactQueue<int, 2> queue;
+ queue.Push(1);
+ queue.Push(2);
+ queue.Push(3);
+
+ for (int i = 1; i <= 10; i++) {
+ EXPECT_EQ(queue.Size(), 3u);
+ EXPECT_EQ(queue.Front(), i);
+ EXPECT_EQ(queue.Pop(), i);
+ queue.Push(i + 3);
+ }
+
+ for (int i = 11; i <= 13; i++) {
+ EXPECT_EQ(queue.Front(), i);
+ queue.Pop();
+ }
+
+ EXPECT_TRUE(queue.Empty());
+}
+
+TEST(TCompactQueueTest, Reallocation2)
+{
+ TCompactQueue<int, 3> queue;
+ queue.Push(1);
+ queue.Push(2);
+ queue.Push(3);
+ EXPECT_EQ(queue.Pop(), 1);
+ queue.Push(4);
+ queue.Push(5);
+
+ EXPECT_EQ(queue.Size(), 4u);
+
+ for (int i = 2; i <= 10; i++) {
+ EXPECT_EQ(queue.Size(), 4u);
+ EXPECT_EQ(queue.Front(), i);
+ EXPECT_EQ(queue.Pop(), i);
+ queue.Push(i + 4);
+ }
+
+ for (int i = 11; i <= 14; i++) {
+ EXPECT_EQ(queue.Front(), i);
+ queue.Pop();
+ }
+
+ EXPECT_TRUE(queue.Empty());
+}
+
+TEST(TCompactQueueTest, Stress)
+{
+ std::mt19937_64 rng(42);
+
+ for (int iteration = 0; iteration < 1000; ++iteration) {
+ TCompactQueue<int, 4> queue1;
+ std::queue<int> queue2;
+
+ for (int step = 0; step < 10'000; ++step) {
+ EXPECT_EQ(queue1.Size(), queue2.size());
+ EXPECT_EQ(queue1.Empty(), queue2.empty());
+ if (!queue1.Empty()) {
+ EXPECT_EQ(queue1.Front(), queue2.front());
+ }
+
+ if (queue2.empty() || rng() % 2 == 0) {
+ int value = rng() % 1'000'000;
+ queue1.Push(value);
+ queue2.push(value);
+ } else {
+ queue1.Pop();
+ queue2.pop();
+ }
+ }
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+} // namespace
+} // namespace NYT