From 8674c7bd4286b73ba8593a5f67e8b81791359cde Mon Sep 17 00:00:00 2001 From: HGU Date: Mon, 30 Mar 2026 06:29:13 +0900 Subject: [PATCH 1/3] queue --- include/queue/queue.hpp | 1 + include/queue/queue.tpp | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/include/queue/queue.hpp b/include/queue/queue.hpp index 05f2dfe..81c4d4e 100644 --- a/include/queue/queue.hpp +++ b/include/queue/queue.hpp @@ -8,6 +8,7 @@ class Queue { private: int sp; int ep; + int count; Data* buffer; public: Queue(); diff --git a/include/queue/queue.tpp b/include/queue/queue.tpp index b7f341c..bd2c5af 100644 --- a/include/queue/queue.tpp +++ b/include/queue/queue.tpp @@ -1,7 +1,7 @@ #include "queue/queue.hpp" template -Queue::Queue() : sp(0), ep(0) { +Queue::Queue() : sp(0), ep(0), count(0){ buffer = new Data[QUEUE_SIZE]; } @@ -14,23 +14,36 @@ template void Queue::clear() { sp = 0; ep = 0; + count = 0; } template void Queue::push(Data data) { + if (count == QUEUE_SIZE) throw std::overflow_error("Queue is full"); + + + buffer[ep] = data; + ep = (ep + 1) % QUEUE_SIZE; + count++; } template Data Queue::pull() { - return buffer[sp]; + if (count == 0) throw std::underflow_error("Queue is empty"); + + Data data = buffer[sp]; + sp = (sp + 1) % QUEUE_SIZE; + count--; + return data; } template Data Queue::top() { + if (count == 0) throw std::underflow_error("Queue is empty"); return buffer[sp]; } template int Queue::size() { - return 0; + return (ep - sp + QUEUE_SIZE) % QUEUE_SIZE; } \ No newline at end of file From 45b765541d54a7a87ba29c1dacf9f1e126cdde76 Mon Sep 17 00:00:00 2001 From: HGU Date: Mon, 30 Mar 2026 06:33:25 +0900 Subject: [PATCH 2/3] queue --- test/queue/queue_test.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/queue/queue_test.cpp b/test/queue/queue_test.cpp index 53b76a1..466cbf7 100644 --- a/test/queue/queue_test.cpp +++ b/test/queue/queue_test.cpp @@ -42,4 +42,25 @@ TEST_F(QueueTest, push_and_pull) { // then EXPECT_EQ(second, v2); +} + + +TEST_F(QueueTest, throw_exception_when_empty) { + // 큐가 비어있는 상태에서 + int_queue.clear(); + + // 에러가 발생하는지 확인 + EXPECT_THROW(int_queue.pull(), std::underflow_error); + EXPECT_THROW(int_queue.top(), std::underflow_error); +} + + +TEST_F(QueueTest, throw_exception_when_full) { + // 큐를 가득 채움 (QUEUE_SIZE가 20이라고 가정) + for(int i = 0; i < 20; i++) { + int_queue.push(i); + } + + // 21번째 데이터를 넣으려고 할 때 에러 발생 확인 + EXPECT_THROW(int_queue.push(99), std::overflow_error); } \ No newline at end of file From d6dde63974177e216688473fb23a0c05fd56d13b Mon Sep 17 00:00:00 2001 From: HGU Date: Mon, 30 Mar 2026 06:43:50 +0900 Subject: [PATCH 3/3] queue --- include/queue/queue.tpp | 8 +++++--- test/queue/queue_test.cpp | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/include/queue/queue.tpp b/include/queue/queue.tpp index bd2c5af..ac9353e 100644 --- a/include/queue/queue.tpp +++ b/include/queue/queue.tpp @@ -19,12 +19,14 @@ void Queue::clear() { template void Queue::push(Data data) { - if (count == QUEUE_SIZE) throw std::overflow_error("Queue is full"); - + if (count == QUEUE_SIZE) { + sp = (sp + 1) % QUEUE_SIZE; + } else { + count++; + } buffer[ep] = data; ep = (ep + 1) % QUEUE_SIZE; - count++; } template diff --git a/test/queue/queue_test.cpp b/test/queue/queue_test.cpp index 466cbf7..89e8a5d 100644 --- a/test/queue/queue_test.cpp +++ b/test/queue/queue_test.cpp @@ -57,10 +57,12 @@ TEST_F(QueueTest, throw_exception_when_empty) { TEST_F(QueueTest, throw_exception_when_full) { // 큐를 가득 채움 (QUEUE_SIZE가 20이라고 가정) - for(int i = 0; i < 20; i++) { + for(int i = 0; i < 30; i++) { int_queue.push(i); } - // 21번째 데이터를 넣으려고 할 때 에러 발생 확인 - EXPECT_THROW(int_queue.push(99), std::overflow_error); + for (int i = 10; i < 30; i++) { + EXPECT_EQ(int_queue.pull(), i); + } + } \ No newline at end of file