From 28d8054fb55f46a0121b59ce3cdc132b193346a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=B6=80=EA=B1=B4?= Date: Sat, 28 Mar 2026 19:51:30 +0900 Subject: [PATCH 1/2] feat:solve circular-queue --- include/queue/queue.hpp | 1 + include/queue/queue.tpp | 11 +++++- test/queue/queue_test.cpp | 77 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 2 deletions(-) diff --git a/include/queue/queue.hpp b/include/queue/queue.hpp index 05f2dfe..f5230c1 100644 --- a/include/queue/queue.hpp +++ b/include/queue/queue.hpp @@ -9,6 +9,7 @@ class Queue { int sp; int ep; Data* buffer; + int cnt; public: Queue(); ~Queue(); diff --git a/include/queue/queue.tpp b/include/queue/queue.tpp index b7f341c..aaf395e 100644 --- a/include/queue/queue.tpp +++ b/include/queue/queue.tpp @@ -18,11 +18,17 @@ void Queue::clear() { template void Queue::push(Data data) { + if((ep+1)%QUEUE_SIZE==sp) throw std::overflow_error("Queue is full"); + buffer[ep] = data; + ep=(ep+1)%QUEUE_SIZE; } template Data Queue::pull() { - return buffer[sp]; + if(ep==sp) throw std::underflow_error("Queue is empty"); + Data tmp = buffer[sp]; + sp=(sp+1)%QUEUE_SIZE; + return tmp; } template @@ -32,5 +38,6 @@ Data Queue::top() { template int Queue::size() { - return 0; + int size = (ep-sp+QUEUE_SIZE)%QUEUE_SIZE; + return size; } \ No newline at end of file diff --git a/test/queue/queue_test.cpp b/test/queue/queue_test.cpp index 53b76a1..8513c36 100644 --- a/test/queue/queue_test.cpp +++ b/test/queue/queue_test.cpp @@ -42,4 +42,81 @@ TEST_F(QueueTest, push_and_pull) { // then EXPECT_EQ(second, v2); +} + +TEST_F(QueueTest, full_push){ + // given + for(int i=0;i<19;i++){ + int_queue.push(i); + } + // when + EXPECT_EQ(int_queue.size(), 19); + // then + EXPECT_THROW(int_queue.push(1),std::overflow_error); +} + +TEST_F(QueueTest, empty_pull) { + // given + // when + // then + EXPECT_THROW(int_queue.pull(),std::underflow_error); +} + +TEST_F(QueueTest, push_and_size){ + // given + int v1 = 1; + int v2 = 3; + int_queue.push(v1); + int_queue.push(v2); + + // when + int size = int_queue.size(); + + // then + EXPECT_EQ(size, 2); +} + +TEST_F(QueueTest, push19_size){ + // given + for(int i=0;i<19;i++){ + int_queue.push(i); + } + // when + int size = int_queue.size(); + + // then + EXPECT_EQ(size, 19); +} + +TEST_F(QueueTest, push19_pull10_and_size){ + // given + for(int i=0;i<19;i++){ + int_queue.push(i); + } + for(int i=0;i<10;i++){ + int_queue.pull(); + } + // when + int size = int_queue.size(); + + // then + EXPECT_EQ(size, 9); +} + +TEST_F(QueueTest, push19_pull10_push5_and_size){ + // given + for(int i=0;i<19;i++){ + int_queue.push(i); + } + for(int i=0;i<10;i++){ + int_queue.pull(); + } + for(int i=0;i<5;i++){ + int_queue.push(i); + } + // when + int size = int_queue.size(); + + // then + EXPECT_EQ(size, 14); } \ No newline at end of file From 0aa244247dd0c75ebc58a4707498504fa5049113 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=B6=80=EA=B1=B4?= Date: Sat, 28 Mar 2026 20:02:58 +0900 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20cnt=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/queue/queue.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/include/queue/queue.hpp b/include/queue/queue.hpp index f5230c1..05f2dfe 100644 --- a/include/queue/queue.hpp +++ b/include/queue/queue.hpp @@ -9,7 +9,6 @@ class Queue { int sp; int ep; Data* buffer; - int cnt; public: Queue(); ~Queue();