Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/queue/queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Queue {
private:
int sp;
int ep;
int count;
Data* buffer;
public:
Queue();
Expand Down
21 changes: 18 additions & 3 deletions include/queue/queue.tpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "queue/queue.hpp"

template<class Data>
Queue<Data>::Queue() : sp(0), ep(0) {
Queue<Data>::Queue() : sp(0), ep(0), count(0){
buffer = new Data[QUEUE_SIZE];
}

Expand All @@ -14,23 +14,38 @@ template<class Data>
void Queue<Data>::clear() {
sp = 0;
ep = 0;
count = 0;
}

template<class Data>
void Queue<Data>::push(Data data) {
if (count == QUEUE_SIZE) {
sp = (sp + 1) % QUEUE_SIZE;
} else {
count++;
}

buffer[ep] = data;
ep = (ep + 1) % QUEUE_SIZE;
}

template<class Data>
Data Queue<Data>::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<class Data>
Data Queue<Data>::top() {
if (count == 0) throw std::underflow_error("Queue is empty");
return buffer[sp];
}

template<class Data>
int Queue<Data>::size() {
return 0;
return (ep - sp + QUEUE_SIZE) % QUEUE_SIZE;
}
23 changes: 23 additions & 0 deletions test/queue/queue_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}