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
11 changes: 9 additions & 2 deletions include/queue/queue.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ void Queue<Data>::clear() {

template<class Data>
void Queue<Data>::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<class Data>
Data Queue<Data>::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<class Data>
Expand All @@ -32,5 +38,6 @@ Data Queue<Data>::top() {

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