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..ac9353e 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,38 @@ template void Queue::clear() { sp = 0; ep = 0; + count = 0; } template void Queue::push(Data data) { + if (count == QUEUE_SIZE) { + sp = (sp + 1) % QUEUE_SIZE; + } else { + count++; + } + + buffer[ep] = data; + ep = (ep + 1) % QUEUE_SIZE; } 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 diff --git a/test/queue/queue_test.cpp b/test/queue/queue_test.cpp index 53b76a1..89e8a5d 100644 --- a/test/queue/queue_test.cpp +++ b/test/queue/queue_test.cpp @@ -42,4 +42,27 @@ 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 < 30; i++) { + int_queue.push(i); + } + + for (int i = 10; i < 30; i++) { + EXPECT_EQ(int_queue.pull(), i); + } + } \ No newline at end of file